package com.yezi.testmedia.filter.video;

import android.opengl.GLES20;
import android.opengl.Matrix;

import com.yezi.testmedia.R;
import com.yezi.testmedia.filter.BaseFilter;
import com.yezi.testmedia.utils.enums.FilterType;
import com.yezi.testmedia.utils.GL2Utils;
import com.yezi.testmedia.utils.enums.VideoType;
import com.yezi.testmedia.utils.camera.CameraEngine;

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

public class VideoFilter extends BaseFilter {

    private int glSTMatrix;
    private float[] mTransformMatrix = new float[16];
    private VideoType mVideoType = VideoType.VIDEO;
    private float[] mMvpMatrix;

    public void setTransformMatrix(float[] matrix) {
        mTransformMatrix = matrix;
    }

    public VideoFilter() {
        this(0, 0);
    }

    public VideoFilter(int vertexRes, int fragmentRes) {
        super(vertexRes == 0 ? R.raw.default_video_vertex : vertexRes,
                fragmentRes == 0 ? R.raw.default_video_fragment : fragmentRes);
        setFilterType(FilterType.VIDEO);
    }

    public void setVideoType(VideoType videoType) {
        mVideoType = videoType;
    }

    @Override
    public void initTextureBuffer() {
        float[] position = mVideoType == VideoType.VIDEO ? GL2Utils.FRAGMENT_POSITION_180 : GL2Utils.FRAGMENT_POSITION_90;
        mCoord = ByteBuffer
                .allocateDirect(position.length * 4)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer()
                .put(position);
        mCoord.position(0);
    }

    @Override
    public void onDraw() {
        if (mVideoType == VideoType.CAMERA) {
            if (CameraEngine.isFrontCamera()) {
                float[] flipMatrix = new float[16];
                Matrix.multiplyMM(flipMatrix, 0, mMvpMatrix, 0, GL2Utils.flip(GL2Utils.getOriginalMatrix(), false, true), 0);
                setMVPMatrix(flipMatrix);
            } else {
                setMVPMatrix(mMvpMatrix);
            }
        }

        GLES20.glUniformMatrix4fv(glSTMatrix, 1, false, mTransformMatrix, 0);
    }

    @Override
    public void onCreated(int mProgram) {
        glSTMatrix = GLES20.glGetUniformLocation(mProgram, "uSTMatrix");
    }

    @Override
    public void onChanged(int width, int height) {
        mMvpMatrix = getMVPMatrix();
    }
}

@@ -174,58 +174,8 @@
 er;%0A
-import com.yezi.testmedia.utils.enums.FilterType;%0A
 impo
@@ -250,21 +250,22 @@
 s.enums.
-Video
+Filter
 Type;%0Aim
@@ -298,26 +298,22 @@
 ils.
-camera.CameraEngin
+enums.VideoTyp
 e;%0A%0A
@@ -493,24 +493,73 @@
  float%5B16%5D;%0A
+    private float%5B%5D mFlipMatrix = new float%5B16%5D;%0A
     private 
@@ -601,40 +601,8 @@
 DEO;
-%0A    private float%5B%5D mMvpMatrix;
 %0A%0A  
@@ -1143,24 +1143,59 @@
 eBuffer() %7B%0A
+        super.initTextureBuffer();%0A
         floa
@@ -1579,410 +1579,8 @@
 ) %7B%0A
-        if (mVideoType == VideoType.CAMERA) %7B%0A            if (CameraEngine.isFrontCamera()) %7B%0A                float%5B%5D flipMatrix = new float%5B16%5D;%0A                Matrix.multiplyMM(flipMatrix, 0, mMvpMatrix, 0, GL2Utils.flip(GL2Utils.getOriginalMatrix(), false, true), 0);%0A                setMVPMatrix(flipMatrix);%0A            %7D else %7B%0A                setMVPMatrix(mMvpMatrix);%0A            %7D%0A        %7D%0A%0A
     
@@ -1873,20 +1873,91 @@
     
-mMv
+if (mVideoType == VideoType.CAMERA) %7B%0A            Matrix.multiplyMM(mFli
 pMatrix
- =
+, 0,
  get
@@ -1967,17 +1967,131 @@
 Matrix()
-;
+, 0, GL2Utils.flip(GL2Utils.getOriginalMatrix(), true, false), 0);%0A            setMVPMatrix(mFlipMatrix);%0A        %7D
 %0A    %7D%0A%7D

fix camear bug