@@ -90,9 +90,286 @@ RGB三个通道的颜色取反,而alpha通道不变。
9090- onDestroy():销毁,用于资源回收。
9191
9292
93+ ``` java
94+ public class BaseFilter {
95+ private static final int POSITION_COMPONENT_COUNT = 2 ;
96+ private static final int TEXTURE_COMPONENT_COUNT = 2 ;
97+
98+ protected int mProgram;
99+ @RawRes
100+ private int mVertexShaderResId;
101+ @RawRes
102+ private int mFragmentShaderResId;
103+
104+ private boolean mInited;
105+
106+ private int vertexPosition;
107+ private int texturePosition;
108+ private int samplerTexturePosition;
109+
110+ // 渲染线程
111+ private LinkedList<Runnable > mRunOnDraw = new LinkedList ();
112+
113+ public BaseFilter () {
114+ this (R . raw. video_no_filter_vertex_shader, R . raw. video_no_filter_fragment_shader);
115+ }
116+
117+ public BaseFilter (@RawRes int vertexShaderResId , @RawRes int fragmentShaderResId ) {
118+ mVertexShaderResId = vertexShaderResId;
119+ mFragmentShaderResId = fragmentShaderResId;
120+ }
121+
122+ public void init () {
123+ if (! mInited) {
124+ onInit();
125+ mInited = true ;
126+ onInited();
127+ }
128+ }
129+
130+ public void onInit () {
131+ handleProgram(MyApplication . getInstance(), mVertexShaderResId, mFragmentShaderResId);
132+ vertexPosition = glGetAttribLocation(" vPosition" );
133+ texturePosition = glGetAttribLocation(" vCoordPosition" );
134+ samplerTexturePosition = glGetUniformLocation(" uSamplerTexture" );
135+ }
136+
137+ /**
138+ * readResource -> compileShader -> linkProgram -> useProgram
139+ *
140+ * @param context
141+ * @param vertexShader
142+ * @param fragmentShader
143+ */
144+ protected void handleProgram (@NonNull Context context , @RawRes int vertexShader , @RawRes int fragmentShader ) {
145+ String vertexShaderStr = ResReadUtils . readResource(context, vertexShader);
146+ int vertexShaderId = ShaderUtils . compileVertexShader(vertexShaderStr);
147+ // 编译片段着色程序
148+ String fragmentShaderStr = ResReadUtils . readResource(context, fragmentShader);
149+ int fragmentShaderId = ShaderUtils . compileFragmentShader(fragmentShaderStr);
150+ // 连接程序
151+ mProgram = ShaderUtils . linkProgram(vertexShaderId, fragmentShaderId);
152+ // 在OpenGLES环境中使用程序
153+ GLES30 . glUseProgram(mProgram);
154+ }
155+
156+ public void onInited () {
157+
158+ }
159+
160+ public final void destroy () {
161+ mInited = false ;
162+ GLES30 . glDeleteProgram(mProgram);
163+ onDestroy();
164+ }
165+
166+ public void onDestroy () {
167+
168+ }
169+
170+ public void onDraw (final int textureId , final FloatBuffer mVertextBuffer ,
171+ final FloatBuffer mTextureBuffer ) {
172+ runPendingOnDrawTasks();
173+ if (! mInited) {
174+ return ;
175+ }
176+
177+ glVertexAttribPointer(vertexPosition, POSITION_COMPONENT_COUNT , GLES30. GL_FLOAT , false , 0 , mVertextBuffer);
178+ glVertexAttribPointer(texturePosition, TEXTURE_COMPONENT_COUNT , GLES30. GL_FLOAT , false , 0 , mTextureBuffer);
179+ GLES30 . glEnableVertexAttribArray(vertexPosition);
180+ GLES30 . glEnableVertexAttribArray(texturePosition);
181+ GLES30 . glUniform1i(samplerTexturePosition, 0 );
182+ // if (textureId != GL.NO_TEXTURE) {
183+ // GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
184+ // GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
185+ // GLES20.glUniform1i(glUniformTexture, 0);
186+ // }
187+
188+ onDrawArraysPre();
189+ GLES30 . glDrawArrays(GLES30. GL_TRIANGLE_STRIP , 0 , 4 );
190+ GLES30 . glDisableVertexAttribArray(vertexPosition);
191+ GLES30 . glDisableVertexAttribArray(texturePosition);
192+ }
193+
194+ protected void runPendingOnDrawTasks () {
195+ while (! mRunOnDraw. isEmpty()) {
196+ mRunOnDraw. removeFirst(). run();
197+ }
198+ }
199+
200+
201+ /**
202+ * 设置着色器中对象float值
203+ */
204+ protected void setFloat (final int location , final float floatValue ) {
205+ runOnDraw(new Runnable () {
206+ @Override
207+ public void run () {
208+ GLES30 . glUniform1f(location, floatValue);
209+ }
210+ });
211+ }
212+
213+ /**
214+ * 设置着色器中对象组值float值
215+ */
216+ protected void setFloatVec2 (final int location , final float [] arrayValue ) {
217+ runOnDraw(new Runnable () {
218+ @Override
219+ public void run () {
220+ GLES30 . glUniform2fv(location, 1 , FloatBuffer . wrap(arrayValue));
221+ }
222+ });
223+ }
224+
225+ /**
226+ * 设置着色中数组值
227+ */
228+ protected void setFloatVec3 (final int location , final float [] arrayValue ) {
229+ runOnDraw(new Runnable () {
230+ @Override
231+ public void run () {
232+ GLES20 . glUniform3fv(location, 1 , FloatBuffer . wrap(arrayValue));
233+ }
234+ });
235+ }
236+
237+ /**
238+ * 设置着色器中对象组值float值
239+ */
240+ protected void setFloatVec4 (final int location , final float [] arrayValue ) {
241+ runOnDraw(new Runnable () {
242+ @Override
243+ public void run () {
244+ GLES20 . glUniform4fv(location, 1 , FloatBuffer . wrap(arrayValue));
245+ }
246+ });
247+ }
248+
249+ /**
250+ * 设置着色器中3维矩阵的值
251+ */
252+ protected void setUniformMatrix3f (final int location , final float [] matrix ) {
253+ runOnDraw(new Runnable () {
254+ @Override
255+ public void run () {
256+ GLES20 . glUniformMatrix3fv(location, 1 , false , matrix, 0 );
257+ }
258+ });
259+ }
260+
261+ /**
262+ * 设置着色器中4维矩阵的值
263+ */
264+ protected void setUniformMatrix4f (final int location , final float [] matrix ) {
265+ runOnDraw(new Runnable () {
266+ @Override
267+ public void run () {
268+ GLES20 . glUniformMatrix4fv(location, 1 , false , matrix, 0 );
269+ }
270+ });
271+ }
272+
273+
274+ protected void runOnDraw (Runnable runnable ) {
275+ synchronized (mRunOnDraw) {
276+ mRunOnDraw. addLast(runnable);
277+ }
278+ }
279+
280+ protected void onDrawArraysPre () {
281+
282+ }
283+
284+
285+ protected void glViewport (int x , int y , int width , int height ) {
286+ GLES30 . glViewport(x, y, width, height);
287+ }
288+
289+ protected void glClearColor (float red , float green , float blue , float alpha ) {
290+ GLES30 . glClearColor(red, green, blue, alpha);
291+ }
292+
293+ protected void glClear (int mask ) {
294+ GLES30 . glClear(mask);
295+ }
296+
297+ protected static void glEnableVertexAttribArray (int index ) {
298+ GLES30 . glEnableVertexAttribArray(index);
299+ }
300+
301+ protected void glDisableVertexAttribArray (int index ) {
302+ GLES30 . glDisableVertexAttribArray(index);
303+ }
304+
305+ protected int glGetAttribLocation (String name ) {
306+ return GLES30 . glGetAttribLocation(mProgram, name);
307+ }
308+
309+ protected int glGetUniformLocation (String name ) {
310+ return GLES30 . glGetUniformLocation(mProgram, name);
311+ }
312+
313+ protected void glUniformMatrix4fv (int location , int count , boolean transpose , float [] value , int offset ) {
314+ GLES30 . glUniformMatrix4fv(location, count, transpose, value, offset);
315+ }
316+
317+ protected void glDrawArrays (int mode , int first , int count ) {
318+ GLES30 . glDrawArrays(mode, first, count);
319+ }
320+
321+ protected void glDrawElements (int mode , int count , int type , java.nio. Buffer indices ) {
322+ GLES30 . glDrawElements(mode, count, type, indices);
323+ }
324+
325+ protected void orthoM (String name , int width , int height ) {
326+ ProjectionMatrixUtil . orthoM(mProgram, width, height, name);
327+ }
328+
329+ protected void glVertexAttribPointer (
330+ int indx ,
331+ int size ,
332+ int type ,
333+ boolean normalized ,
334+ int stride ,
335+ java.nio. Buffer ptr ) {
336+ GLES30 . glVertexAttribPointer(indx, size, type, normalized, stride, ptr);
337+ }
338+
339+ protected void glActiveTexture (int texture ) {
340+ GLES30 . glActiveTexture(GLES30. GL_TEXTURE0 );
341+ }
342+
343+ protected void glBindTexture (int target , int texture ) {
344+ GLES30 . glBindTexture(GLES30. GL_TEXTURE_2D , texture);
345+ }
346+
347+ protected void glUniform1i (int location , int x ) {
348+ GLES20 . glUniform1i(location, x);
349+ }
350+
351+ public int getProgram () {
352+ return mProgram;
353+ }
354+ }
355+ ```
93356
94-
95-
357+ 在Render中改变的地方就是onDrawFrame()方法中去调用Filter.onDraw()方法:
358+ ``` java
359+ @Override
360+ public void onDrawFrame(GL10 gl) {
361+ glClear(GLES30. GL_DEPTH_BUFFER_BIT | GLES30. GL_COLOR_BUFFER_BIT );
362+ adjustVideoSize();
363+ synchronized (this ) {
364+ if (mUpdateSurfaceTexture) {
365+ mSurfaceTexture. updateTexImage();
366+ mUpdateSurfaceTexture = false ;
367+ }
368+ }
369+ runAll(mRunOnDraw);
370+ mFilter. onDraw(mTextureId, mVertextBuffer, mTextureBuffer);
371+ }
372+ ```
96373
97374
98375
0 commit comments