| 1 | /* |
| 2 | Copyright (c) 2013, Broadcom Europe Ltd |
| 3 | Copyright (c) 2013, Tim Gover |
| 4 | All rights reserved. |
| 5 | |
| 6 | |
| 7 | Redistribution and use in source and binary forms, with or without |
| 8 | modification, are permitted provided that the following conditions are met: |
| 9 | * Redistributions of source code must retain the above copyright |
| 10 | notice, this list of conditions and the following disclaimer. |
| 11 | * Redistributions in binary form must reproduce the above copyright |
| 12 | notice, this list of conditions and the following disclaimer in the |
| 13 | documentation and/or other materials provided with the distribution. |
| 14 | * Neither the name of the copyright holder nor the |
| 15 | names of its contributors may be used to endorse or promote products |
| 16 | derived from this software without specific prior written permission. |
| 17 | |
| 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 19 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY |
| 22 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 25 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #include "yuv.h" |
| 31 | #include "RaspiTex.h" |
| 32 | #include "RaspiTexUtil.h" |
| 33 | #include <GLES2/gl2.h> |
| 34 | #include <EGL/egl.h> |
| 35 | #include <EGL/eglext.h> |
| 36 | |
| 37 | /* Draw a scaled quad showing the the entire texture with the |
| 38 | * origin defined as an attribute */ |
| 39 | static RASPITEXUTIL_SHADER_PROGRAM_T yuv_shader = |
| 40 | { |
| 41 | .vertex_source = |
| 42 | "attribute vec2 vertex;\n" |
| 43 | "attribute vec2 top_left;\n" |
| 44 | "varying vec2 texcoord;\n" |
| 45 | "void main(void) {\n" |
| 46 | " texcoord = vertex + vec2(0.0, 1.0);\n" |
| 47 | " gl_Position = vec4(top_left + vertex, 0.0, 1.0);\n" |
| 48 | "}\n" , |
| 49 | |
| 50 | .fragment_source = |
| 51 | "#extension GL_OES_EGL_image_external : require\n" |
| 52 | "uniform samplerExternalOES tex;\n" |
| 53 | "varying vec2 texcoord;\n" |
| 54 | "void main(void) {\n" |
| 55 | " gl_FragColor = texture2D(tex, texcoord);\n" |
| 56 | "}\n" , |
| 57 | .uniform_names = {"tex" }, |
| 58 | .attribute_names = {"vertex" , "top_left" }, |
| 59 | }; |
| 60 | |
| 61 | static GLfloat varray[] = |
| 62 | { |
| 63 | 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, -1.0f, |
| 64 | 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, |
| 65 | }; |
| 66 | |
| 67 | static const EGLint yuv_egl_config_attribs[] = |
| 68 | { |
| 69 | EGL_RED_SIZE, 8, |
| 70 | EGL_GREEN_SIZE, 8, |
| 71 | EGL_BLUE_SIZE, 8, |
| 72 | EGL_ALPHA_SIZE, 8, |
| 73 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 74 | EGL_NONE |
| 75 | }; |
| 76 | |
| 77 | /** |
| 78 | * Creates the OpenGL ES 2.X context and builds the shaders. |
| 79 | * @param raspitex_state A pointer to the GL preview state. |
| 80 | * @return Zero if successful. |
| 81 | */ |
| 82 | static int yuv_init(RASPITEX_STATE *state) |
| 83 | { |
| 84 | int rc; |
| 85 | state->egl_config_attribs = yuv_egl_config_attribs; |
| 86 | rc = raspitexutil_gl_init_2_0(state); |
| 87 | if (rc != 0) |
| 88 | goto end; |
| 89 | |
| 90 | rc = raspitexutil_build_shader_program(&yuv_shader); |
| 91 | GLCHK(glUseProgram(yuv_shader.program)); |
| 92 | GLCHK(glUniform1i(yuv_shader.uniform_locations[0], 0)); // tex unit |
| 93 | end: |
| 94 | return rc; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Draws a 2x2 grid with each shell showing the entire MMAL buffer from a |
| 99 | * different EGL image target. |
| 100 | */ |
| 101 | static int yuv_redraw(RASPITEX_STATE *raspitex_state) |
| 102 | { |
| 103 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 104 | |
| 105 | GLCHK(glUseProgram(yuv_shader.program)); |
| 106 | GLCHK(glActiveTexture(GL_TEXTURE0)); |
| 107 | GLCHK(glEnableVertexAttribArray(yuv_shader.attribute_locations[0])); |
| 108 | GLCHK(glVertexAttribPointer(yuv_shader.attribute_locations[0], |
| 109 | 2, GL_FLOAT, GL_FALSE, 0, varray)); |
| 110 | |
| 111 | // Y plane |
| 112 | GLCHK(glBindTexture(GL_TEXTURE_EXTERNAL_OES, raspitex_state->y_texture)); |
| 113 | GLCHK(glVertexAttrib2f(yuv_shader.attribute_locations[1], -1.0f, 1.0f)); |
| 114 | GLCHK(glDrawArrays(GL_TRIANGLES, 0, 6)); |
| 115 | |
| 116 | // U plane |
| 117 | GLCHK(glBindTexture(GL_TEXTURE_EXTERNAL_OES, raspitex_state->u_texture)); |
| 118 | GLCHK(glVertexAttrib2f(yuv_shader.attribute_locations[1], 0.0f, 1.0f)); |
| 119 | GLCHK(glDrawArrays(GL_TRIANGLES, 0, 6)); |
| 120 | |
| 121 | // V plane |
| 122 | GLCHK(glBindTexture(GL_TEXTURE_EXTERNAL_OES, raspitex_state->v_texture)); |
| 123 | GLCHK(glVertexAttrib2f(yuv_shader.attribute_locations[1], 0.0f, 0.0f)); |
| 124 | GLCHK(glDrawArrays(GL_TRIANGLES, 0, 6)); |
| 125 | |
| 126 | // RGB plane |
| 127 | GLCHK(glBindTexture(GL_TEXTURE_EXTERNAL_OES, raspitex_state->texture)); |
| 128 | GLCHK(glVertexAttrib2f(yuv_shader.attribute_locations[1], -1.0f, 0.0f)); |
| 129 | GLCHK(glDrawArrays(GL_TRIANGLES, 0, 6)); |
| 130 | |
| 131 | GLCHK(glDisableVertexAttribArray(yuv_shader.attribute_locations[0])); |
| 132 | GLCHK(glUseProgram(0)); |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | int yuv_open(RASPITEX_STATE *state) |
| 137 | { |
| 138 | state->ops.gl_init = yuv_init; |
| 139 | state->ops.redraw = yuv_redraw; |
| 140 | state->ops.update_texture = raspitexutil_update_texture; |
| 141 | state->ops.update_y_texture = raspitexutil_update_y_texture; |
| 142 | state->ops.update_u_texture = raspitexutil_update_u_texture; |
| 143 | state->ops.update_v_texture = raspitexutil_update_v_texture; |
| 144 | return 0; |
| 145 | } |
| 146 | |