GSYVideoGLViewSimpleRender.java 9.3 KB
Newer Older
G
guoshuyu 已提交
1
package com.shuyu.gsyvideoplayer.render.glrender;
2 3

import android.annotation.SuppressLint;
G
guoshuyu 已提交
4
import android.graphics.Bitmap;
5 6 7 8 9
import android.graphics.SurfaceTexture;
import android.opengl.GLES20;
import android.opengl.Matrix;
import android.view.Surface;

G
guoshuyu 已提交
10 11
import com.shuyu.gsyvideoplayer.render.view.GSYVideoGLView;
import com.shuyu.gsyvideoplayer.render.effect.NoEffect;
G
guoshuyu 已提交
12
import com.shuyu.gsyvideoplayer.listener.GSYVideoShotListener;
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;


/**
 * 在videffects的基础上调整的
 * <p>
 * 原 @author sheraz.khilji
 */
@SuppressLint("ViewConstructor")
28
public class GSYVideoGLViewSimpleRender extends GSYVideoGLViewBaseRender {
29 30

    private static final int FLOAT_SIZE_BYTES = 4;
31

32
    private static final int TRIANGLE_VERTICES_DATA_STRIDE_BYTES = 5 * FLOAT_SIZE_BYTES;
33

34
    private static final int TRIANGLE_VERTICES_DATA_POS_OFFSET = 0;
35

36
    private static final int TRIANGLE_VERTICES_DATA_UV_OFFSET = 3;
37

G
guoshuyu 已提交
38
    protected static final int GL_TEXTURE_EXTERNAL_OES = 0x8D65;
39 40 41

    private final float[] mTriangleVerticesData = {
            // X, Y, Z, U, V
G
guoshuyu 已提交
42 43 44 45 46 47 48
            -1.0f, -1.0f, 0.0f,
            0.0f, 0.0f, 1.0f,
            -1.0f, 0.0f, 1.0f,
            0.0f, -1.0f, 1.0f,
            0.0f, 0.0f, 1.0f,
            1.0f, 1.0f, 0.0f,
            1.0f, 1.0f,};
49 50 51 52 53 54 55 56 57 58

    private final String mVertexShader = "uniform mat4 uMVPMatrix;\n"
            + "uniform mat4 uSTMatrix;\n"
            + "attribute vec4 aPosition;\n"
            + "attribute vec4 aTextureCoord;\n"
            + "varying vec2 vTextureCoord;\n"
            + "void main() {\n"
            + "  gl_Position = uMVPMatrix * aPosition;\n"
            + "  vTextureCoord = (uSTMatrix * aTextureCoord).xy;\n"
            + "}\n";
G
guoshuyu 已提交
59

G
guoshuyu 已提交
60
    private float[] mMVPMatrix = new float[16];
G
guoshuyu 已提交
61

62 63
    private float[] mSTMatrix = new float[16];

G
guoshuyu 已提交
64
    private int mProgram;
65

G
guoshuyu 已提交
66
    private int mTextureID[] = new int[2];
67

G
guoshuyu 已提交
68
    private int muMVPMatrixHandle;
69

70
    private int muSTMatrixHandle;
71

72
    private int maPositionHandle;
73

74
    private int maTextureHandle;
75

G
guoshuyu 已提交
76 77
    private boolean mUpdateSurface = false;

78
    private boolean mTakeShotPic = false;
G
guoshuyu 已提交
79 80

    private FloatBuffer mTriangleVertices;
81 82 83

    private SurfaceTexture mSurface;

G
guoshuyu 已提交
84 85
    private GSYVideoShotListener mGSYVideoShotListener;

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    private GSYVideoGLView.ShaderInterface mEffect = new NoEffect();

    public GSYVideoGLViewSimpleRender() {
        mTriangleVertices = ByteBuffer
                .allocateDirect(
                        mTriangleVerticesData.length * FLOAT_SIZE_BYTES)
                .order(ByteOrder.nativeOrder()).asFloatBuffer();
        mTriangleVertices.put(mTriangleVerticesData).position(0);

        Matrix.setIdentityM(mSTMatrix, 0);
        Matrix.setIdentityM(mMVPMatrix, 0);
    }

    @Override
    public void onDrawFrame(GL10 glUnused) {
        synchronized (this) {
G
guoshuyu 已提交
102
            if (mUpdateSurface) {
103 104
                mSurface.updateTexImage();
                mSurface.getTransformMatrix(mSTMatrix);
G
guoshuyu 已提交
105
                mUpdateSurface = false;
106 107
            }
        }
108
        initDrawFrame();
109

110
        bindDrawFrameTexture();
111

112
        initPointerAndDraw();
113

114
        takeBitmap(glUnused);
G
guoshuyu 已提交
115

116 117 118 119 120 121 122 123 124 125 126 127
        GLES20.glFinish();

    }

    @Override
    public void onSurfaceChanged(GL10 glUnused, int width, int height) {
        GLES20.glViewport(0, 0, width, height);
    }

    @Override
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {

128
        mProgram = createProgram(getVertexShader(), getFragmentShader());
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
        if (mProgram == 0) {
            return;
        }
        maPositionHandle = GLES20
                .glGetAttribLocation(mProgram, "aPosition");
        checkGlError("glGetAttribLocation aPosition");
        if (maPositionHandle == -1) {
            throw new RuntimeException(
                    "Could not get attrib location for aPosition");
        }
        maTextureHandle = GLES20.glGetAttribLocation(mProgram,
                "aTextureCoord");
        checkGlError("glGetAttribLocation aTextureCoord");
        if (maTextureHandle == -1) {
            throw new RuntimeException(
                    "Could not get attrib location for aTextureCoord");
        }

        muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram,
                "uMVPMatrix");
        checkGlError("glGetUniformLocation uMVPMatrix");
        if (muMVPMatrixHandle == -1) {
            throw new RuntimeException(
                    "Could not get attrib location for uMVPMatrix");
        }

        muSTMatrixHandle = GLES20.glGetUniformLocation(mProgram,
                "uSTMatrix");
        checkGlError("glGetUniformLocation uSTMatrix");
        if (muSTMatrixHandle == -1) {
            throw new RuntimeException(
                    "Could not get attrib location for uSTMatrix");
        }

        GLES20.glGenTextures(2, mTextureID, 0);

        GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureID[0]);
        checkGlError("glBindTexture mTextureID");

        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
                GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
                GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
                GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
                GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);

        mSurface = new SurfaceTexture(mTextureID[0]);
        mSurface.setOnFrameAvailableListener(this);

        Surface surface = new Surface(mSurface);
181 182

        sendSurfaceForPlayer(surface);
183 184 185 186
    }

    @Override
    synchronized public void onFrameAvailable(SurfaceTexture surface) {
G
guoshuyu 已提交
187
        mUpdateSurface = true;
188 189
    }

190 191 192
    @Override
    public void releaseAll() {

193 194
    }

195 196 197 198 199
    protected void initDrawFrame() {
        mProgram = createProgram(getVertexShader(), getFragmentShader());
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT
                | GLES20.GL_COLOR_BUFFER_BIT);
200

201 202
        GLES20.glUseProgram(mProgram);
        checkGlError("glUseProgram");
203 204
    }

205 206 207 208

    protected void bindDrawFrameTexture() {
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureID[0]);
209 210
    }

211 212 213 214 215 216 217

    protected void takeBitmap(GL10 glUnused) {
        if (mTakeShotPic) {
            mTakeShotPic = false;
            if (mGSYVideoShotListener != null) {
                Bitmap bitmap = createBitmapFromGLSurface(0, 0, mSurfaceView.getWidth(), mSurfaceView.getHeight(), glUnused);
                mGSYVideoShotListener.getBitmap(bitmap);
G
guoshuyu 已提交
218 219 220 221 222
            }
        }
    }


223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
    protected void initPointerAndDraw() {
        mTriangleVertices.position(TRIANGLE_VERTICES_DATA_POS_OFFSET);
        GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT,
                false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES,
                mTriangleVertices);
        checkGlError("glVertexAttribPointer maPosition");
        GLES20.glEnableVertexAttribArray(maPositionHandle);
        checkGlError("glEnableVertexAttribArray maPositionHandle");

        mTriangleVertices.position(TRIANGLE_VERTICES_DATA_UV_OFFSET);
        GLES20.glVertexAttribPointer(maTextureHandle, 3, GLES20.GL_FLOAT,
                false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES,
                mTriangleVertices);
        checkGlError("glVertexAttribPointer maTextureHandle");
        GLES20.glEnableVertexAttribArray(maTextureHandle);
        checkGlError("glEnableVertexAttribArray maTextureHandle");

        GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix,
                0);
        GLES20.glUniformMatrix4fv(muSTMatrixHandle, 1, false, mSTMatrix, 0);

        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
        checkGlError("glDrawArrays");

    }

G
guoshuyu 已提交
249 250 251 252
    public int getProgram() {
        return mProgram;
    }

G
guoshuyu 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    public int getMuMVPMatrixHandle() {
        return muMVPMatrixHandle;
    }

    public int getMuSTMatrixHandle() {
        return muSTMatrixHandle;
    }

    public int getMaPositionHandle() {
        return maPositionHandle;
    }

    public int getMaTextureHandle() {
        return maTextureHandle;
    }

    public float[] getMVPMatrix() {
        return mMVPMatrix;
    }

    public float[] getSTMatrix() {
        return mSTMatrix;
    }

    public int[] getTextureID() {
        return mTextureID;
    }

281 282
    protected String getVertexShader() {
        return mVertexShader;
G
guoshuyu 已提交
283 284
    }

285 286 287
    protected String getFragmentShader() {
        return mEffect.getShader(mSurfaceView);
    }
G
guoshuyu 已提交
288

289 290 291 292 293 294 295 296 297 298 299 300
    /**
     * 形变动画
     */
    public void setMVPMatrix(float[] MVPMatrix) {
        this.mMVPMatrix = MVPMatrix;
    }

    /**
     * 打开截图
     */
    public void takeShotPic() {
        mTakeShotPic = true;
G
guoshuyu 已提交
301 302
    }

303 304 305
    /**
     * 截图监听
     */
G
guoshuyu 已提交
306 307 308 309 310
    public void setGSYVideoShotListener(GSYVideoShotListener listener, boolean high) {
        this.mGSYVideoShotListener = listener;
        this.mHighShot = high;
    }

311 312
    /**
     * 设置滤镜效果
G
guoshuyu 已提交
313
     *
314 315 316 317 318 319 320
     * @param shaderEffect
     */
    public void setEffect(GSYVideoGLView.ShaderInterface shaderEffect) {
        if (shaderEffect != null)
            mEffect = shaderEffect;
    }

321 322 323
}