rk
昨天 4a8ff39b0fab0627ef8f7459587d514cc01c3676
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
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
249
250
251
252
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package com.example.datalibrary.gl.shape;
 
import android.graphics.Rect;
import android.opengl.GLES11Ext;
import android.opengl.GLES20;
 
import com.example.datalibrary.gl.utils.ShaderUtils;
 
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
 
 
public class GLFrame {
 
    private int width;
    private int height;
    private int screenWidth;
    private int screenHeight;
 
    public float getWd() {
        return wd;
    }
 
    public float getHd() {
        return hd;
    }
    private float wd = 1;
 
    private float hd = 1;
 
    private float[] vertexData = {
            1f, -1f, 0f,
            -1f, -1f, 0f,
            1f, 1f, 0f,
            -1f, 1f, 0f
    };
    private final float[] textureVertexReverseData = {
            0f, 0f,
            1f, 0f,
            0f, 1f,
            1f, 1f,
    };
    private final float[] textureVertexData = {
            1f, 0f,
            0f, 0f,
            1f, 1f,
            0f, 1f
    };
    private FloatBuffer vertexBuffer;
    private int mirrorRGB;
 
    private FloatBuffer textureVertexBuffer;
 
    private int programId = -1;
    private int aPositionHandle;
    private int uTextureSamplerHandle;
    private int iTextureSamplerHandle;
    private int aTextureCoordHandle;
    private int uSTMMatrixHandle;
 
    private int sHandle;
    private int hHandle;
    private int lHandle;
 
    private int iHandle;
 
 
    private int[] vertexBuffers;
 
 
 
    private String fragmentShader = "#extension GL_OES_EGL_image_external : require\n" +
            "varying highp vec2 vTexCoord;\n" +
            "uniform samplerExternalOES sTexture;\n" +
            "uniform sampler2D iTexture;\n" +
            "uniform highp mat4 usTMatrix;\n" +
            "uniform highp float S;\n" +
            "uniform highp float H;\n"+
            "uniform highp float L;\n"+
            "uniform highp float i;\n"+
            "highp vec3 rgb2hsv(highp vec3 c){\n" +
            "    highp vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);\n" +
            "    highp vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));\n" +
            "    highp vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));\n" +
            "    highp float d = q.x - min(q.w, q.y);\n" +
            "    highp float e = 1.0e-10;\n" +
            "    return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);\n" +
            "}\n" +
            "highp vec3 hsv2rgb(highp vec3 c){\n" +
            "    highp vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);\n" +
            "    highp vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);\n" +
            "    return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);\n" +
            "}"+
            "void main() {\n" +
            "    highp vec2 tx_transformed = (usTMatrix * vec4(vTexCoord, 0, 1.0)).xy;\n" +
            "    highp vec4 video = texture2D(sTexture ,  tx_transformed);\n" +
            "    highp vec4 rgba;\n"+
            "    if(i == 0.0){\n" +
            "       rgba = video;\n" +
            "    }\n"+
            "    else{\n" +
            "       highp vec4 image = texture2D(iTexture ,  vTexCoord);\n" +
            "       rgba = mix(video,image , image.a);\n"+
            "    }\n"+
            "    highp vec3 hsl = rgb2hsv(rgba.xyz);\n"+
            "    if(H != 0.0)hsl.x = H;\n" +
            "    if(hsl.x<0.0)hsl.x = hsl.x+1.0;\n" +
            "    else if(hsl.x>1.0)hsl.x = hsl.x-1.0;\n"+
            "    if(S != 1.0)hsl.y = hsl.y*S;\n"+
            "    highp vec3 rgb = hsv2rgb(hsl);\n" +
            "    if (L < 0.0) rgb = rgb + rgb * vec3(L);\n"+
            "    else rgb = rgb + (1.0 - rgb) * vec3(L);\n"+
            "    gl_FragColor = vec4(rgb,rgba.w);\n" +
            "}";
    private  String vertexShader = "attribute vec4 aPosition;\n" +
            "attribute vec2 aTexCoord;\n" +
            "varying vec2 vTexCoord;\n" +
            "void main() {\n" +
            "    vTexCoord = aTexCoord;\n" +
            "    gl_Position = aPosition;\n" +
            "}";
 
    private void againInit(){
        vertexBuffer = ByteBuffer.allocateDirect(vertexData.length * 4)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer()
                .put(vertexData);
        vertexBuffer.position(0);
 
 
        if (mirrorRGB == 0){
            textureVertexBuffer = ByteBuffer.allocateDirect(textureVertexData.length * 4)
                    .order(ByteOrder.nativeOrder())
                    .asFloatBuffer()
                    .put(textureVertexData);
        }else {
            textureVertexBuffer = ByteBuffer.allocateDirect(textureVertexReverseData.length * 4)
                    .order(ByteOrder.nativeOrder())
                    .asFloatBuffer()
                    .put(textureVertexReverseData);
        }
        textureVertexBuffer.position(0);
        initFrame();
    }
    public GLFrame(int mirrorRGB){
        vertexBuffer = ByteBuffer.allocateDirect(vertexData.length * 4)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer()
                .put(vertexData);
        vertexBuffer.position(0);
        this.mirrorRGB = mirrorRGB;
        if (mirrorRGB == 0){
            textureVertexBuffer = ByteBuffer.allocateDirect(textureVertexData.length * 4)
                    .order(ByteOrder.nativeOrder())
                    .asFloatBuffer()
                    .put(textureVertexData);
        }else {
            textureVertexBuffer = ByteBuffer.allocateDirect(textureVertexReverseData.length * 4)
                    .order(ByteOrder.nativeOrder())
                    .asFloatBuffer()
                    .put(textureVertexReverseData);
        }
        textureVertexBuffer.position(0);
    }
    public void initFrame(){
        programId = ShaderUtils.createProgram(vertexShader, fragmentShader);
        aPositionHandle = GLES20.glGetAttribLocation(programId, "aPosition");
        uSTMMatrixHandle = GLES20.glGetUniformLocation(programId, "usTMatrix");
        uTextureSamplerHandle = GLES20.glGetUniformLocation(programId, "sTexture");
        iTextureSamplerHandle = GLES20.glGetUniformLocation(programId, "iTexture");
        aTextureCoordHandle = GLES20.glGetAttribLocation(programId, "aTexCoord");
        sHandle = GLES20.glGetUniformLocation(programId , "S");
        hHandle = GLES20.glGetUniformLocation(programId , "H");
        lHandle = GLES20.glGetUniformLocation(programId , "L");
 
        iHandle = GLES20.glGetUniformLocation(programId , "i");
 
        vertexBuffers = new int[2];
        GLES20.glGenBuffers(2 , vertexBuffers , 0);
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vertexBuffers[0]);
        GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, vertexData.length * 4, vertexBuffer , GLES20.GL_STATIC_DRAW);
 
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vertexBuffers[1]);
        GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, textureVertexData.length * 4,
                textureVertexBuffer , GLES20.GL_STATIC_DRAW);
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
    }
 
    private float s = 1.0f;
    public void setS(float s){
        this.s = s;
    }
    private float h = 0.0f;
    public void setH(float h) {
        this.h = h;
    }
    private float l = 1.0f;
    public void setL(float l) {
        this.l = l;
    }
 
    private boolean isInit;
    private Rect rect = new Rect();
    public void setSize(int screenWidth , int screenHeight , int videoWidth , int videoHeight){
        if (isInit){
            return;
        }
        isInit = true;
        this.screenWidth = screenWidth;
        this.screenHeight = screenHeight;
        this.width = videoWidth;
        this.height = videoHeight;
        if (screenHeight > screenWidth && videoWidth > videoHeight){
            wd = (float) screenHeight / (float) videoHeight;
 
        }else if (screenHeight < screenWidth && videoWidth < videoHeight){
            hd = (float) screenWidth / (float) videoWidth;
        }else if (videoWidth < screenWidth && videoHeight < screenHeight){
            float ywd = (float) videoWidth / (float) screenWidth;
            float yhd = (float) videoHeight / (float) screenHeight;
            float ywd1 = 1 - ywd;
            float yhd1 = 1 - yhd;
            if (ywd > yhd){
                wd = ywd + yhd1;
            }else {
                hd += yhd + ywd1;
            }
        }
//        vertexData = new float[]{
//                1f, -1f, 0f,
//                -1f, -1f, 0f,
//                1f, 1f, 0f,
//                -1f, 1f, 0f
//        };
        vertexData = new float[]{
                wd, -hd, 0f,
                -wd, -hd, 0f,
                wd, hd, 0f,
                -wd, hd, 0f
        };
        againInit();
        rect();
    }
 
    public void correctSize(int videoWidth , int videoHeight){
        if (this.width == videoWidth && this.height == videoHeight){
            return;
        }
//        setSize(screenHeight , screenWidth , videoWidth , videoHeight);
    }
 
    private void rect(){
        /*int left,top,viewWidth,viewHeight;
        float sh = screenWidth*1.0f/screenHeight;
        float vh = width *1.0f/ height;
        if(sh < vh){
            left = 0;
            viewWidth = screenWidth;
            viewHeight = (int)(height *1.0f/ width *viewWidth);
            top = (screenHeight - viewHeight)/2;
        }else{
            top = 0;
            viewHeight = screenHeight;
            viewWidth = (int)(width *1.0f/ height *viewHeight);
            left = (screenWidth - viewWidth)/2;
        }*/
//        rect.left = 0;
//        rect.top = 0;
//        rect.right = screenWidth;
//        rect.bottom = screenHeight;
 
        rect.left = 0;
        rect.top = 0;
        rect.right = screenWidth;
        rect.bottom = screenHeight;
    }
 
    public void drawFrame(int tId , int textureId , float[] sTMatrix){
        GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
        GLES20.glViewport(rect.left, rect.top, rect.right, rect.bottom);
        GLES20.glUseProgram(programId);
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vertexBuffers[0]);
        GLES20.glEnableVertexAttribArray(aPositionHandle);
        GLES20.glVertexAttribPointer(aPositionHandle ,  3, GLES20.GL_FLOAT, false , 
                0, 0);
 
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vertexBuffers[1]);
        GLES20.glEnableVertexAttribArray(aTextureCoordHandle);
        GLES20.glVertexAttribPointer(aTextureCoordHandle ,  2, GLES20.GL_FLOAT, false ,  0, 0);
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
 
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES ,  textureId);
        GLES20.glUniform1i(uTextureSamplerHandle , 0);
        GLES20.glUniformMatrix4fv(uSTMMatrixHandle ,  1, false ,  sTMatrix, 0);
 
        GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, tId);
        GLES20.glUniform1i(iTextureSamplerHandle , 1);
 
        GLES20.glUniform1f(sHandle , s);
        GLES20.glUniform1f(hHandle , h);
        GLES20.glUniform1f(lHandle , l);
        GLES20.glUniform1f(iHandle , tId);
 
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
    }
 
    public void release(){
        if (vertexBuffers == null){
            return;
        }
        GLES20.glDeleteProgram(programId);
        GLES20.glDeleteBuffers(2 , vertexBuffers , 0);
    }
}