The previous scenario assumes that the Java layer is updating textures using RGB or RBGA format data, but in the scenario where the video is played, the decoded data of the decoder is more cumbersome to render than if it is in YUV format. One way is to use the CPU for YUV to RGB, and then render, but this way performance is very poor, another way is to use the GPU to transform, using the GPU's parallel computing power to accelerate the conversion. We need to write shader to achieve this. As mentioned earlier, unity only needs the Java layer's texture ID, when using shader for YUV to RGB, how to update the texture of the data? The answer is render to Texture (see [1]). By creating a framebuffer, call Glframebuffertexture2d to associate the texture with framebuffer so that the drawing on the framebuffer is written to the texture. The Java code is as follows:
Public voidSETUPGL (intWidthintheight) { //Creating Textures intTempbuffer[] =New int[1]; Gles20.glgentextures (1, Tempbuffer, 0); Mtextureid= Tempbuffer[0]; if(Mtextureid = = 0) {Glloge ("SETUPGL, Glgentextures for render texture failed"); return; } gles20.glbindtexture (gles20.gl_texture_2d, 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); Gles20.glteximage2d (gles20.gl_texture_2d,0, Gles20.gl_rgba, width, height, 0, Gles20.gl_rgba, Gles20.gl_unsigned_byte,NULL); Gles20.glbindtexture (gles20.gl_texture_2d,0); //Create a YUV textureGles20.glgentextures (3, myuvtextures, 0); if(Myuvtextures[0] = = 0 | | myuvtextures[1] = = 0 | | myuvtextures[2] = = 0) {mylog.e (TAG,"SETUPGL, Glgentextures for YUV texture failed"); return; } for(intyuvtexture:myuvtextures) {gles20.glbindtexture (gles20.gl_texture_2d, yuvtexture); 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); } gles20.glbindtexture (gles20.gl_texture_2d,0); //Create a Frame bufferGles20.glgenframebuffers (1, tempbuffer, 0); Mframebuffer= Tempbuffer[0]; if(Mframebuffer = = 0) {Glloge ("SETUPGL, Glgenframebuffers failed"); return; } gles20.glbindframebuffer (Gles20.gl_framebuffer, Mframebuffer); Gles20.glframebuffertexture2d (Gles20.gl_framebuffer, Gles20.gl_color_attachment0, GLES20.GL_TEXTURE_2D, MTextur EId,0); intErrcode =Gles20.glcheckframebufferstatus (Gles20.gl_framebuffer); if(Errcode! =gles20.gl_framebuffer_complete) {Glloge ("SETUPGL, Glcheckframebufferstatus failed, errcode=0x" +integer.tohexstring (Errcode)); return; } gles20.glbindframebuffer (Gles20.gl_framebuffer,0);}
When drawing, bind Framebuffer first, then draw operation, Java code is as follows:
Public void updatetexture () { gles20.glbindframebuffer (Gles20.gl_framebuffer, mframebuffer); Gles20.glviewport (0, 0, Mwidth, mheight); Gles20.glclearcolor (0, 0, 0, 1); Gles20.glclear (gles20.gl_color_buffer_bit); // Add drawing operations here 0);}
To realize the rendering of YUV data, we need to write shader. Here, we render the YUV data format for YUV420 (YUV420 format of the specific introduction, please readers themselves Baidu), Y, U, v channel data are stored in three buffers. Assign YUV data to three textures separately, then specify the vertex and texture coordinates of the shader, and draw a rectangle (see [2]).
First, three textures are generated for YUV in the SETUPGL function, and the Java code is as follows:
//Create a YUV textureGles20.glgentextures (3, myuvtextures, 0);if(Myuvtextures[0] = = 0 | | myuvtextures[1] = = 0 | | myuvtextures[2] = = 0) {mylog.e (TAG,"SETUPGL, Glgentextures for YUV texture failed"); return;} for(intyuvtexture:myuvtextures) {gles20.glbindtexture (gles20.gl_texture_2d, yuvtexture); 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);} Gles20.glbindtexture (gles20.gl_texture_2d,0);
When drawing, the YUV data is assigned to three textures, respectively, and three textures are bound to Gles20.gl_texture0,gles20.gl_texture1,gles20.gl_texture2, the Java code is as follows:
for (int i = 0; i < 3; + +i) {+ i); Gles20.glbindtexture (gles20.gl_texture_2d, myuvtextures[i]); int W = i = = 0? Yuvframe.yuvstrides[0]: (Yuvframe.yuvstrides[0]/2); int h = i = = 0? Yuvframe.height: (YUVFRAME.HEIGHT/2); 0, Gles20.gl_luminance, W, h, 0, gles20.gl_luminance, Gles20.gl_unsigned_byte, Yuvframe.yuvplanes[i]);}
Then we need to create program to create vertex Shader and fragment Shader for program (see [2]). The Java code is as follows:
Public Static FinalString vertex_shader_string = "attribute vec4 in_pos;\n" + "attribute vec2 in_tc;\n" + "varying vec2 out_tc; \ n "+" void Main () {\ n "+" gl_position = in_pos;\n "+" OUT_TC = in_tc;\n "+"}\n "; Public Static FinalString fragment_shader_string = "Precision mediump float;\n" + "Uniform sampler2d tex_y;\n" + "uniform Sampl er2d tex_u;\n "+" Uniform sampler2d tex_v;\n "+" varying vec2 out_tc;\n "+" void Main () {\ n "+ "Vec4 C = Vec4 ((texture2d (tex_y, OUT_TC). r-16./255.) * 1.164); \ n "+" vec4 U = Vec4 (Texture2d (Tex_u, OUT_TC). r-128./255.); N "+" vec4 V = Vec4 (Texture2d (Tex_v, OUT_TC). r-128./255.); N "+" c + = V * VEC4 (1.596, -0.813, 0, 0); \ n "+" c + = U * VEC4 (0, -0.392, 2.017, 0); \ n "+" C.A = 1.0;\n "+" Gl_fragcolor = c;\n "+"}\n ";protected Final voidAddshaderto (intType, String source,intProgramthrowsRuntimeException {intShader =Gles20.glcreateshader (type); if(Shader = = 0) { Throw NewRuntimeException ("Create shader failed, err=" +gles10.glgeterror ()); } gles20.glshadersource (shader, source); Gles20.glcompileshader (shader); int[] result =New int[]{gles20.gl_false}; Gles20.glgetshaderiv (shader, Gles20.gl_compile_status, result,0); if(result[0]! =gles20.gl_true) {Gles20.gldeleteshader (shader); Throw NewRuntimeException ("Compile shader failed, err=" +gles10.glgeterror ()); } gles20.glattachshader (program, shader); Gles20.gldeleteshader (shader);} Public voidSETUPGL (intWidthintheight) { ... //Create ProgramMprogram =Gles20.glcreateprogram (); Addshaderto (Gles20.gl_vertex_shader, eglrender.vertex_shader_string, Mprogram); Addshaderto (Gles20.gl_fragment_shader, eglrender.fragment_shader_string, Mprogram); Gles20.gllinkprogram (Mprogram); GLES20.GLGETPROGRAMIV (Mprogram, Gles20.gl_link_status, Tempbuffer,0); if(tempbuffer[0]! =gles20.gl_true) {Glloge ("SETUPGL, create program failed"); return; } gles20.gluseprogram (Mprogram); intY_tex = Gles20.glgetuniformlocation (Mprogram, "tex_y"); Gles20.gluniform1i (Y_tex,0); intU_tex = Gles20.glgetuniformlocation (Mprogram, "Tex_u"); Gles20.gluniform1i (U_tex,1); intV_tex = Gles20.glgetuniformlocation (Mprogram, "Tex_v"); Gles20.gluniform1i (V_tex,2); Mvertexlocation= Gles20.glgetattriblocation (Mprogram, "In_pos"); Mtexturelocation= Gles20.glgetattriblocation (Mprogram, "IN_TC"); Gles20.gluseprogram (0);}
The complete drawing code is as follows:
1 Public voidupdatetexture () {2 Gles20.glbindframebuffer (Gles20.gl_framebuffer, mframebuffer);3 4Gles20.glviewport (0, 0, Mwidth, mheight);5Gles20.glclearcolor (0, 0, 0, 1);6 gles20.glclear (gles20.gl_color_buffer_bit);7 gles20.gldisable (gles20.gl_cull_face);8 gles20.gldisable (gles20.gl_depth_test);9Gles20.glbindbuffer (gles20.gl_array_buffer, 0);TenGles20.glbindbuffer (gles20.gl_element_array_buffer, 0); One A Gles20.gluseprogram (mprogram); - //Updating YUV data - for(inti = 0; I < 3; ++i) { theGles20.glactivetexture (Gles20.gl_texture0 +i); - gles20.glbindtexture (gles20.gl_texture_2d, myuvtextures[i]); - intw = i = = 0? Yuvframe.yuvstrides[0]: (Yuvframe.yuvstrides[0]/2); - inth = i = = 0? Yuvframe.height: (YUVFRAME.HEIGHT/2); +Gles20.glteximage2d (gles20.gl_texture_2d, 0, Gles20.gl_luminance, W, h, 0, - gles20.gl_luminance, Gles20.gl_unsigned_byte, Yuvframe.yuvplanes[i]); + } A //set vertex coordinates and texture coordinates at Gles20.glenablevertexattribarray (mvertexlocation); -Mvertexcoord.position (0); -Gles20.glvertexattribpointer (Mvertexlocation, 2, Gles20.gl_float,false, 0, Mvertexcoord); - Gles20.glenablevertexattribarray (mtexturelocation); -Mtexturecoord.position (0); -Gles20.glvertexattribpointer (Mtexturelocation, 2, Gles20.gl_float,false, 0, Mtexturecoord); in //Draw a rectangle -Gles20.gldrawarrays (Gles20.gl_triangle_strip, 0, 4); to + Gles20.gldisablevertexattribarray (mvertexlocation); - Gles20.gldisablevertexattribarray (mtexturelocation); the for(inti = 0; I < 3; ++i) { *Gles20.glactivetexture (Gles20.gl_texture0 +i); $Gles20.glbindtexture (gles20.gl_texture_2d, 0);Panax Notoginseng } - theGles20.gluseprogram (0); +Gles20.glbindframebuffer (gles20.gl_framebuffer, 0); AGles20.glclear (0); the}
Where the 9~10 line is required, otherwise, the gldrawarrays call fails, and the gl_invalid_operation error is reported (see [3]). When drawing is complete, you need to notify Unity3d and call Gl.invalidatestate in C #, otherwise it will affect Unity's drawing (see [4]). The C # code is as follows:
void Update () { mplugintexture.call ("updatetexture"); Gl. Invalidatestate ();}
Where the update function is Unity's callback every time the frame is refreshed, we call the Java layer's updatetexture function in the callback to update the texture data, and then call Gl.invalidatestate to notify Unity to reset the OpenGL state. At first, when Java decodes a frame, the author updates the texture data to draw and then notifies C # to call Gl.invalidatestate. But this way there are two problems, one is that when the app back to the background, the drawing operation will still be done, but the painting will fail; The specific reasons have not been identified. Later, instead of updating the texture from the update callback in C #, the two issues were resolved.
Summarize:
Unity's official plugin is drawn by calling the gl.issuepluginevent,c++ layer in the C # layer to receive callbacks from the unity render thread, updating the texture in the callback (see [5]). This scenario requires writing JNI and C + +, which is cumbersome to implement. All of the scenarios presented in this paper can be implemented in the Java layer. For interested readers.
[References]
[1] Tutorial 14:render to Texture
[2] Simplest AV Playback example 6:opengl play yuv420p (via texture, using shader)
[3] Robertcastle/unityfbo
[4] GL. Invalidatestate
[5] Low-level Native Plugin Interface
Multi-threaded OpenGL sharing context under Android (iv)