Home | History | Annotate | Download | only in nativemedia
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.nativemedia;
     18 
     19 import android.graphics.SurfaceTexture;
     20 import android.util.Log;
     21 
     22 import java.nio.ByteBuffer;
     23 import java.nio.ByteOrder;
     24 import java.nio.FloatBuffer;
     25 
     26 import javax.microedition.khronos.egl.EGLConfig;
     27 import javax.microedition.khronos.opengles.GL10;
     28 
     29 import android.content.Context;
     30 
     31 import android.opengl.GLES20;
     32 import android.opengl.GLSurfaceView;
     33 import android.opengl.Matrix;
     34 
     35 import android.util.AttributeSet;
     36 
     37 public class MyGLSurfaceView extends GLSurfaceView {
     38 
     39     MyRenderer mRenderer;
     40 
     41     public MyGLSurfaceView(Context context) {
     42         this(context, null);
     43     }
     44 
     45     public MyGLSurfaceView(Context context, AttributeSet attributeSet) {
     46         super(context, attributeSet);
     47         init();
     48     }
     49 
     50     private void init() {
     51         setEGLContextClientVersion(2);
     52         mRenderer = new MyRenderer();
     53         setRenderer(mRenderer);
     54     }
     55 
     56     @Override
     57     public void onPause() {
     58         super.onPause();
     59     }
     60 
     61     @Override
     62     public void onResume() {
     63         super.onResume();
     64     }
     65 
     66     public SurfaceTexture getSurfaceTexture() {
     67         return mRenderer.getSurfaceTexture();
     68     }
     69 }
     70 
     71 class MyRenderer implements GLSurfaceView.Renderer, SurfaceTexture.OnFrameAvailableListener {
     72 
     73     public MyRenderer() {
     74         mVertices = ByteBuffer.allocateDirect(mVerticesData.length
     75                 * FLOAT_SIZE_BYTES).order(ByteOrder.nativeOrder()).asFloatBuffer();
     76         mVertices.put(mVerticesData).position(0);
     77 
     78         Matrix.setIdentityM(mSTMatrix, 0);
     79         Matrix.setIdentityM(mMMatrix, 0);
     80         Matrix.rotateM(mMMatrix, 0, 20, 0, 1, 0);
     81     }
     82 
     83     public void onDrawFrame(GL10 glUnused) {
     84         synchronized(this) {
     85             if (updateSurface) {
     86                 mSurface.updateTexImage();
     87 
     88                 mSurface.getTransformMatrix(mSTMatrix);
     89                 updateSurface = false;
     90             }
     91         }
     92 
     93         // Ignore the passed-in GL10 interface, and use the GLES20
     94         // class's static methods instead.
     95         GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
     96         GLES20.glUseProgram(mProgram);
     97         checkGlError("glUseProgram");
     98 
     99         GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    100         GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureID);
    101 
    102         mVertices.position(VERTICES_DATA_POS_OFFSET);
    103         GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false,
    104                 VERTICES_DATA_STRIDE_BYTES, mVertices);
    105         checkGlError("glVertexAttribPointer maPosition");
    106         GLES20.glEnableVertexAttribArray(maPositionHandle);
    107         checkGlError("glEnableVertexAttribArray maPositionHandle");
    108 
    109         mVertices.position(VERTICES_DATA_UV_OFFSET);
    110         GLES20.glVertexAttribPointer(maTextureHandle, 3, GLES20.GL_FLOAT, false,
    111                 VERTICES_DATA_STRIDE_BYTES, mVertices);
    112         checkGlError("glVertexAttribPointer maTextureHandle");
    113         GLES20.glEnableVertexAttribArray(maTextureHandle);
    114         checkGlError("glEnableVertexAttribArray maTextureHandle");
    115 
    116         Matrix.multiplyMM(mMVPMatrix, 0, mVMatrix, 0, mMMatrix, 0);
    117         Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0);
    118 
    119         GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0);
    120         GLES20.glUniformMatrix4fv(muSTMatrixHandle, 1, false, mSTMatrix, 0);
    121 
    122         GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
    123         checkGlError("glDrawArrays");
    124     }
    125 
    126     public void onSurfaceChanged(GL10 glUnused, int width, int height) {
    127         // Ignore the passed-in GL10 interface, and use the GLES20
    128         // class's static methods instead.
    129         GLES20.glViewport(0, 0, width, height);
    130         mRatio = (float) width / height;
    131         Matrix.frustumM(mProjMatrix, 0, -mRatio, mRatio, -1, 1, 3, 7);
    132     }
    133 
    134     public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
    135         // Ignore the passed-in GL10 interface, and use the GLES20
    136         // class's static methods instead.
    137 
    138         /* Set up alpha blending and an Android background color */
    139         GLES20.glEnable(GLES20.GL_BLEND);
    140         GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
    141         GLES20.glClearColor(0.643f, 0.776f, 0.223f, 1.0f);
    142 
    143         /* Set up shaders and handles to their variables */
    144         mProgram = createProgram(mVertexShader, mFragmentShader);
    145         if (mProgram == 0) {
    146             return;
    147         }
    148         maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
    149         checkGlError("glGetAttribLocation aPosition");
    150         if (maPositionHandle == -1) {
    151             throw new RuntimeException("Could not get attrib location for aPosition");
    152         }
    153         maTextureHandle = GLES20.glGetAttribLocation(mProgram, "aTextureCoord");
    154         checkGlError("glGetAttribLocation aTextureCoord");
    155         if (maTextureHandle == -1) {
    156             throw new RuntimeException("Could not get attrib location for aTextureCoord");
    157         }
    158 
    159         muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
    160         checkGlError("glGetUniformLocation uMVPMatrix");
    161         if (muMVPMatrixHandle == -1) {
    162             throw new RuntimeException("Could not get attrib location for uMVPMatrix");
    163         }
    164 
    165         muSTMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uSTMatrix");
    166         checkGlError("glGetUniformLocation uSTMatrix");
    167         if (muMVPMatrixHandle == -1) {
    168             throw new RuntimeException("Could not get attrib location for uSTMatrix");
    169         }
    170 
    171         checkGlError("glGetUniformLocation uCRatio");
    172         if (muMVPMatrixHandle == -1) {
    173             throw new RuntimeException("Could not get attrib location for uCRatio");
    174         }
    175 
    176         /*
    177          * Create our texture. This has to be done each time the
    178          * surface is created.
    179          */
    180 
    181         int[] textures = new int[1];
    182         GLES20.glGenTextures(1, textures, 0);
    183 
    184         mTextureID = textures[0];
    185         GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureID);
    186         checkGlError("glBindTexture mTextureID");
    187 
    188         // Can't do mipmapping with camera source
    189         GLES20.glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER,
    190                 GLES20.GL_NEAREST);
    191         GLES20.glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER,
    192                 GLES20.GL_LINEAR);
    193         // Clamp to edge is the only option
    194         GLES20.glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S,
    195                 GLES20.GL_CLAMP_TO_EDGE);
    196         GLES20.glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T,
    197                 GLES20.GL_CLAMP_TO_EDGE);
    198         checkGlError("glTexParameteri mTextureID");
    199 
    200         /*
    201          * Create the SurfaceTexture that will feed this textureID, and pass it to the camera
    202          */
    203 
    204         mSurface = new SurfaceTexture(mTextureID);
    205         mSurface.setOnFrameAvailableListener(this);
    206 
    207         Matrix.setLookAtM(mVMatrix, 0, 0, 0, 4f, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
    208 
    209         synchronized(this) {
    210             updateSurface = false;
    211         }
    212     }
    213 
    214     synchronized public void onFrameAvailable(SurfaceTexture surface) {
    215         /* For simplicity, SurfaceTexture calls here when it has new
    216          * data available.  Call may come in from some random thread,
    217          * so let's be safe and use synchronize. No OpenGL calls can be done here.
    218          */
    219         updateSurface = true;
    220         //Log.v(TAG, "onFrameAvailable " + surface.getTimestamp());
    221     }
    222 
    223     private int loadShader(int shaderType, String source) {
    224         int shader = GLES20.glCreateShader(shaderType);
    225         if (shader != 0) {
    226             GLES20.glShaderSource(shader, source);
    227             GLES20.glCompileShader(shader);
    228             int[] compiled = new int[1];
    229             GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
    230             if (compiled[0] == 0) {
    231                 Log.e(TAG, "Could not compile shader " + shaderType + ":");
    232                 Log.e(TAG, GLES20.glGetShaderInfoLog(shader));
    233                 GLES20.glDeleteShader(shader);
    234                 shader = 0;
    235             }
    236         }
    237         return shader;
    238     }
    239 
    240     private int createProgram(String vertexSource, String fragmentSource) {
    241         int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource);
    242         if (vertexShader == 0) {
    243             return 0;
    244         }
    245         int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);
    246         if (pixelShader == 0) {
    247             return 0;
    248         }
    249 
    250         int program = GLES20.glCreateProgram();
    251         if (program != 0) {
    252             GLES20.glAttachShader(program, vertexShader);
    253             checkGlError("glAttachShader");
    254             GLES20.glAttachShader(program, pixelShader);
    255             checkGlError("glAttachShader");
    256             GLES20.glLinkProgram(program);
    257             int[] linkStatus = new int[1];
    258             GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0);
    259             if (linkStatus[0] != GLES20.GL_TRUE) {
    260                 Log.e(TAG, "Could not link program: ");
    261                 Log.e(TAG, GLES20.glGetProgramInfoLog(program));
    262                 GLES20.glDeleteProgram(program);
    263                 program = 0;
    264             }
    265         }
    266         return program;
    267     }
    268 
    269     private void checkGlError(String op) {
    270         int error;
    271         while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
    272             Log.e(TAG, op + ": glError " + error);
    273             throw new RuntimeException(op + ": glError " + error);
    274         }
    275     }
    276 
    277     private static final int FLOAT_SIZE_BYTES = 4;
    278     private static final int VERTICES_DATA_STRIDE_BYTES = 5 * FLOAT_SIZE_BYTES;
    279     private static final int VERTICES_DATA_POS_OFFSET = 0;
    280     private static final int VERTICES_DATA_UV_OFFSET = 3;
    281     private final float[] mVerticesData = {
    282         // X, Y, Z, U, V
    283         -1.0f, -1.0f, 0, 0.f, 0.f,
    284         1.0f, -1.0f, 0, 1.f, 0.f,
    285         -1.0f,  1.0f, 0, 0.f, 1.f,
    286         1.0f,   1.0f, 0, 1.f, 1.f,
    287     };
    288 
    289     private FloatBuffer mVertices;
    290 
    291     private final String mVertexShader =
    292         "uniform mat4 uMVPMatrix;\n" +
    293         "uniform mat4 uSTMatrix;\n" +
    294         "attribute vec4 aPosition;\n" +
    295         "attribute vec4 aTextureCoord;\n" +
    296         "varying vec2 vTextureCoord;\n" +
    297         "void main() {\n" +
    298         "  gl_Position = uMVPMatrix * aPosition;\n" +
    299         "  vTextureCoord = (uSTMatrix * aTextureCoord).xy;\n" +
    300         "}\n";
    301 
    302     private final String mFragmentShader =
    303         "#extension GL_OES_EGL_image_external : require\n" +
    304         "precision mediump float;\n" +
    305         "varying vec2 vTextureCoord;\n" +
    306         "uniform samplerExternalOES sTexture;\n" +
    307         "void main() {\n" +
    308         "  gl_FragColor = texture2D(sTexture, vTextureCoord);\n" +
    309         "}\n";
    310 
    311     private float[] mMVPMatrix = new float[16];
    312     private float[] mProjMatrix = new float[16];
    313     private float[] mMMatrix = new float[16];
    314     private float[] mVMatrix = new float[16];
    315     private float[] mSTMatrix = new float[16];
    316 
    317     private int mProgram;
    318     private int mTextureID;
    319     private int muMVPMatrixHandle;
    320     private int muSTMatrixHandle;
    321     private int maPositionHandle;
    322     private int maTextureHandle;
    323 
    324     private float mRatio = 1.0f;
    325     private SurfaceTexture mSurface;
    326     private boolean updateSurface = false;
    327 
    328     private static final String TAG = "MyRenderer";
    329 
    330     // Magic key
    331     private static final int GL_TEXTURE_EXTERNAL_OES = 0x8D65;
    332 
    333     public SurfaceTexture getSurfaceTexture() {
    334         return mSurface;
    335     }
    336 }
    337