magjed | 59a677a | 2015-06-24 03:59:37 -0700 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
magjed | 59a677a | 2015-06-24 03:59:37 -0700 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
magjed | 59a677a | 2015-06-24 03:59:37 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | package org.webrtc; |
| 12 | |
| 13 | import android.opengl.GLES11Ext; |
| 14 | import android.opengl.GLES20; |
magjed | 59a677a | 2015-06-24 03:59:37 -0700 | [diff] [blame] | 15 | import java.nio.FloatBuffer; |
Magnus Jedvert | ed4224f | 2015-09-02 12:52:03 +0200 | [diff] [blame] | 16 | import java.util.IdentityHashMap; |
| 17 | import java.util.Map; |
magjed | 59a677a | 2015-06-24 03:59:37 -0700 | [diff] [blame] | 18 | |
| 19 | /** |
Magnus Jedvert | 5125433 | 2015-12-15 16:22:29 +0100 | [diff] [blame] | 20 | * Helper class to draw an opaque quad on the target viewport location. Rotation, mirror, and |
| 21 | * cropping is specified using a 4x4 texture coordinate transform matrix. The frame input can either |
| 22 | * be an OES texture or YUV textures in I420 format. The GL state must be preserved between draw |
| 23 | * calls, this is intentional to maximize performance. The function release() must be called |
| 24 | * manually to free the resources held by this object. |
magjed | 59a677a | 2015-06-24 03:59:37 -0700 | [diff] [blame] | 25 | */ |
Magnus Jedvert | 5125433 | 2015-12-15 16:22:29 +0100 | [diff] [blame] | 26 | public class GlRectDrawer implements RendererCommon.GlDrawer { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 27 | // clang-format off |
magjed | 59a677a | 2015-06-24 03:59:37 -0700 | [diff] [blame] | 28 | // Simple vertex shader, used for both YUV and OES. |
| 29 | private static final String VERTEX_SHADER_STRING = |
Magnus Jedvert | ed4224f | 2015-09-02 12:52:03 +0200 | [diff] [blame] | 30 | "varying vec2 interp_tc;\n" |
magjed | 59a677a | 2015-06-24 03:59:37 -0700 | [diff] [blame] | 31 | + "attribute vec4 in_pos;\n" |
| 32 | + "attribute vec4 in_tc;\n" |
| 33 | + "\n" |
| 34 | + "uniform mat4 texMatrix;\n" |
| 35 | + "\n" |
| 36 | + "void main() {\n" |
| 37 | + " gl_Position = in_pos;\n" |
| 38 | + " interp_tc = (texMatrix * in_tc).xy;\n" |
| 39 | + "}\n"; |
| 40 | |
| 41 | private static final String YUV_FRAGMENT_SHADER_STRING = |
Magnus Jedvert | ed4224f | 2015-09-02 12:52:03 +0200 | [diff] [blame] | 42 | "precision mediump float;\n" |
magjed | 59a677a | 2015-06-24 03:59:37 -0700 | [diff] [blame] | 43 | + "varying vec2 interp_tc;\n" |
| 44 | + "\n" |
| 45 | + "uniform sampler2D y_tex;\n" |
| 46 | + "uniform sampler2D u_tex;\n" |
| 47 | + "uniform sampler2D v_tex;\n" |
| 48 | + "\n" |
| 49 | + "void main() {\n" |
| 50 | // CSC according to http://www.fourcc.org/fccyvrgb.php |
| 51 | + " float y = texture2D(y_tex, interp_tc).r;\n" |
| 52 | + " float u = texture2D(u_tex, interp_tc).r - 0.5;\n" |
| 53 | + " float v = texture2D(v_tex, interp_tc).r - 0.5;\n" |
| 54 | + " gl_FragColor = vec4(y + 1.403 * v, " |
| 55 | + " y - 0.344 * u - 0.714 * v, " |
| 56 | + " y + 1.77 * u, 1);\n" |
| 57 | + "}\n"; |
| 58 | |
Magnus Jedvert | ed4224f | 2015-09-02 12:52:03 +0200 | [diff] [blame] | 59 | private static final String RGB_FRAGMENT_SHADER_STRING = |
| 60 | "precision mediump float;\n" |
| 61 | + "varying vec2 interp_tc;\n" |
| 62 | + "\n" |
| 63 | + "uniform sampler2D rgb_tex;\n" |
| 64 | + "\n" |
| 65 | + "void main() {\n" |
| 66 | + " gl_FragColor = texture2D(rgb_tex, interp_tc);\n" |
| 67 | + "}\n"; |
| 68 | |
magjed | 59a677a | 2015-06-24 03:59:37 -0700 | [diff] [blame] | 69 | private static final String OES_FRAGMENT_SHADER_STRING = |
Magnus Jedvert | ed4224f | 2015-09-02 12:52:03 +0200 | [diff] [blame] | 70 | "#extension GL_OES_EGL_image_external : require\n" |
magjed | 59a677a | 2015-06-24 03:59:37 -0700 | [diff] [blame] | 71 | + "precision mediump float;\n" |
| 72 | + "varying vec2 interp_tc;\n" |
| 73 | + "\n" |
| 74 | + "uniform samplerExternalOES oes_tex;\n" |
| 75 | + "\n" |
| 76 | + "void main() {\n" |
| 77 | + " gl_FragColor = texture2D(oes_tex, interp_tc);\n" |
| 78 | + "}\n"; |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 79 | // clang-format on |
magjed | 59a677a | 2015-06-24 03:59:37 -0700 | [diff] [blame] | 80 | |
Magnus Jedvert | 27551c9 | 2015-09-30 18:24:54 +0200 | [diff] [blame] | 81 | // Vertex coordinates in Normalized Device Coordinates, i.e. (-1, -1) is bottom-left and (1, 1) is |
| 82 | // top-right. |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 83 | private static final FloatBuffer FULL_RECTANGLE_BUF = GlUtil.createFloatBuffer(new float[] { |
| 84 | -1.0f, -1.0f, // Bottom left. |
| 85 | 1.0f, -1.0f, // Bottom right. |
| 86 | -1.0f, 1.0f, // Top left. |
| 87 | 1.0f, 1.0f, // Top right. |
| 88 | }); |
magjed | 59a677a | 2015-06-24 03:59:37 -0700 | [diff] [blame] | 89 | |
Magnus Jedvert | 27551c9 | 2015-09-30 18:24:54 +0200 | [diff] [blame] | 90 | // Texture coordinates - (0, 0) is bottom-left and (1, 1) is top-right. |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 91 | private static final FloatBuffer FULL_RECTANGLE_TEX_BUF = GlUtil.createFloatBuffer(new float[] { |
| 92 | 0.0f, 0.0f, // Bottom left. |
| 93 | 1.0f, 0.0f, // Bottom right. |
| 94 | 0.0f, 1.0f, // Top left. |
| 95 | 1.0f, 1.0f // Top right. |
| 96 | }); |
magjed | 59a677a | 2015-06-24 03:59:37 -0700 | [diff] [blame] | 97 | |
Magnus Jedvert | 5125433 | 2015-12-15 16:22:29 +0100 | [diff] [blame] | 98 | private static class Shader { |
| 99 | public final GlShader glShader; |
| 100 | public final int texMatrixLocation; |
Magnus Jedvert | 7afc12f | 2015-09-03 12:40:38 +0200 | [diff] [blame] | 101 | |
Magnus Jedvert | 5125433 | 2015-12-15 16:22:29 +0100 | [diff] [blame] | 102 | public Shader(String fragmentShader) { |
| 103 | this.glShader = new GlShader(VERTEX_SHADER_STRING, fragmentShader); |
| 104 | this.texMatrixLocation = glShader.getUniformLocation("texMatrix"); |
Magnus Jedvert | 7afc12f | 2015-09-03 12:40:38 +0200 | [diff] [blame] | 105 | } |
| 106 | } |
magjed | 59a677a | 2015-06-24 03:59:37 -0700 | [diff] [blame] | 107 | |
Magnus Jedvert | 5125433 | 2015-12-15 16:22:29 +0100 | [diff] [blame] | 108 | // The keys are one of the fragments shaders above. |
| 109 | private final Map<String, Shader> shaders = new IdentityHashMap<String, Shader>(); |
| 110 | |
magjed | 59a677a | 2015-06-24 03:59:37 -0700 | [diff] [blame] | 111 | /** |
| 112 | * Draw an OES texture frame with specified texture transformation matrix. Required resources are |
| 113 | * allocated at the first call to this function. |
| 114 | */ |
Magnus Jedvert | 5125433 | 2015-12-15 16:22:29 +0100 | [diff] [blame] | 115 | @Override |
ivoc | 1aa435c | 2016-05-04 07:14:14 -0700 | [diff] [blame] | 116 | public void drawOes(int oesTextureId, float[] texMatrix, int frameWidth, int frameHeight, |
| 117 | int viewportX, int viewportY, int viewportWidth, int viewportHeight) { |
Magnus Jedvert | 5125433 | 2015-12-15 16:22:29 +0100 | [diff] [blame] | 118 | prepareShader(OES_FRAGMENT_SHADER_STRING, texMatrix); |
| 119 | GLES20.glActiveTexture(GLES20.GL_TEXTURE0); |
magjed | 59a677a | 2015-06-24 03:59:37 -0700 | [diff] [blame] | 120 | // updateTexImage() may be called from another thread in another EGL context, so we need to |
| 121 | // bind/unbind the texture in each draw call so that GLES understads it's a new texture. |
| 122 | GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, oesTextureId); |
ivoc | 1aa435c | 2016-05-04 07:14:14 -0700 | [diff] [blame] | 123 | drawRectangle(viewportX, viewportY, viewportWidth, viewportHeight); |
magjed | 59a677a | 2015-06-24 03:59:37 -0700 | [diff] [blame] | 124 | GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0); |
| 125 | } |
| 126 | |
| 127 | /** |
Magnus Jedvert | ed4224f | 2015-09-02 12:52:03 +0200 | [diff] [blame] | 128 | * Draw a RGB(A) texture frame with specified texture transformation matrix. Required resources |
| 129 | * are allocated at the first call to this function. |
| 130 | */ |
Magnus Jedvert | 5125433 | 2015-12-15 16:22:29 +0100 | [diff] [blame] | 131 | @Override |
ivoc | 1aa435c | 2016-05-04 07:14:14 -0700 | [diff] [blame] | 132 | public void drawRgb(int textureId, float[] texMatrix, int frameWidth, int frameHeight, |
| 133 | int viewportX, int viewportY, int viewportWidth, int viewportHeight) { |
Magnus Jedvert | 5125433 | 2015-12-15 16:22:29 +0100 | [diff] [blame] | 134 | prepareShader(RGB_FRAGMENT_SHADER_STRING, texMatrix); |
| 135 | GLES20.glActiveTexture(GLES20.GL_TEXTURE0); |
Magnus Jedvert | ed4224f | 2015-09-02 12:52:03 +0200 | [diff] [blame] | 136 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId); |
ivoc | 1aa435c | 2016-05-04 07:14:14 -0700 | [diff] [blame] | 137 | drawRectangle(viewportX, viewportY, viewportWidth, viewportHeight); |
Magnus Jedvert | ed4224f | 2015-09-02 12:52:03 +0200 | [diff] [blame] | 138 | // Unbind the texture as a precaution. |
| 139 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0); |
| 140 | } |
| 141 | |
| 142 | /** |
magjed | 59a677a | 2015-06-24 03:59:37 -0700 | [diff] [blame] | 143 | * Draw a YUV frame with specified texture transformation matrix. Required resources are |
| 144 | * allocated at the first call to this function. |
| 145 | */ |
Magnus Jedvert | 5125433 | 2015-12-15 16:22:29 +0100 | [diff] [blame] | 146 | @Override |
ivoc | 1aa435c | 2016-05-04 07:14:14 -0700 | [diff] [blame] | 147 | public void drawYuv(int[] yuvTextures, float[] texMatrix, int frameWidth, int frameHeight, |
| 148 | int viewportX, int viewportY, int viewportWidth, int viewportHeight) { |
Magnus Jedvert | 5125433 | 2015-12-15 16:22:29 +0100 | [diff] [blame] | 149 | prepareShader(YUV_FRAGMENT_SHADER_STRING, texMatrix); |
magjed | 59a677a | 2015-06-24 03:59:37 -0700 | [diff] [blame] | 150 | // Bind the textures. |
| 151 | for (int i = 0; i < 3; ++i) { |
| 152 | GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + i); |
| 153 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, yuvTextures[i]); |
| 154 | } |
ivoc | 1aa435c | 2016-05-04 07:14:14 -0700 | [diff] [blame] | 155 | drawRectangle(viewportX, viewportY, viewportWidth, viewportHeight); |
Magnus Jedvert | ed4224f | 2015-09-02 12:52:03 +0200 | [diff] [blame] | 156 | // Unbind the textures as a precaution.. |
| 157 | for (int i = 0; i < 3; ++i) { |
| 158 | GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + i); |
| 159 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0); |
| 160 | } |
magjed | 59a677a | 2015-06-24 03:59:37 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Magnus Jedvert | 5125433 | 2015-12-15 16:22:29 +0100 | [diff] [blame] | 163 | private void drawRectangle(int x, int y, int width, int height) { |
magjed | 59a677a | 2015-06-24 03:59:37 -0700 | [diff] [blame] | 164 | // Draw quad. |
Magnus Jedvert | 5125433 | 2015-12-15 16:22:29 +0100 | [diff] [blame] | 165 | GLES20.glViewport(x, y, width, height); |
magjed | 59a677a | 2015-06-24 03:59:37 -0700 | [diff] [blame] | 166 | GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); |
| 167 | } |
| 168 | |
Magnus Jedvert | 5125433 | 2015-12-15 16:22:29 +0100 | [diff] [blame] | 169 | private void prepareShader(String fragmentShader, float[] texMatrix) { |
| 170 | final Shader shader; |
| 171 | if (shaders.containsKey(fragmentShader)) { |
| 172 | shader = shaders.get(fragmentShader); |
| 173 | } else { |
| 174 | // Lazy allocation. |
| 175 | shader = new Shader(fragmentShader); |
Magnus Jedvert | ed4224f | 2015-09-02 12:52:03 +0200 | [diff] [blame] | 176 | shaders.put(fragmentShader, shader); |
Magnus Jedvert | 5125433 | 2015-12-15 16:22:29 +0100 | [diff] [blame] | 177 | shader.glShader.useProgram(); |
Magnus Jedvert | ed4224f | 2015-09-02 12:52:03 +0200 | [diff] [blame] | 178 | // Initialize fragment shader uniform values. |
| 179 | if (fragmentShader == YUV_FRAGMENT_SHADER_STRING) { |
Magnus Jedvert | 5125433 | 2015-12-15 16:22:29 +0100 | [diff] [blame] | 180 | GLES20.glUniform1i(shader.glShader.getUniformLocation("y_tex"), 0); |
| 181 | GLES20.glUniform1i(shader.glShader.getUniformLocation("u_tex"), 1); |
| 182 | GLES20.glUniform1i(shader.glShader.getUniformLocation("v_tex"), 2); |
Magnus Jedvert | ed4224f | 2015-09-02 12:52:03 +0200 | [diff] [blame] | 183 | } else if (fragmentShader == RGB_FRAGMENT_SHADER_STRING) { |
Magnus Jedvert | 5125433 | 2015-12-15 16:22:29 +0100 | [diff] [blame] | 184 | GLES20.glUniform1i(shader.glShader.getUniformLocation("rgb_tex"), 0); |
Magnus Jedvert | ed4224f | 2015-09-02 12:52:03 +0200 | [diff] [blame] | 185 | } else if (fragmentShader == OES_FRAGMENT_SHADER_STRING) { |
Magnus Jedvert | 5125433 | 2015-12-15 16:22:29 +0100 | [diff] [blame] | 186 | GLES20.glUniform1i(shader.glShader.getUniformLocation("oes_tex"), 0); |
Magnus Jedvert | ed4224f | 2015-09-02 12:52:03 +0200 | [diff] [blame] | 187 | } else { |
| 188 | throw new IllegalStateException("Unknown fragment shader: " + fragmentShader); |
| 189 | } |
| 190 | GlUtil.checkNoGLES2Error("Initialize fragment shader uniform values."); |
| 191 | // Initialize vertex shader attributes. |
Magnus Jedvert | 5125433 | 2015-12-15 16:22:29 +0100 | [diff] [blame] | 192 | shader.glShader.setVertexAttribArray("in_pos", 2, FULL_RECTANGLE_BUF); |
| 193 | shader.glShader.setVertexAttribArray("in_tc", 2, FULL_RECTANGLE_TEX_BUF); |
Magnus Jedvert | ed4224f | 2015-09-02 12:52:03 +0200 | [diff] [blame] | 194 | } |
Magnus Jedvert | 5125433 | 2015-12-15 16:22:29 +0100 | [diff] [blame] | 195 | shader.glShader.useProgram(); |
| 196 | // Copy the texture transformation matrix over. |
| 197 | GLES20.glUniformMatrix4fv(shader.texMatrixLocation, 1, false, texMatrix, 0); |
Magnus Jedvert | ed4224f | 2015-09-02 12:52:03 +0200 | [diff] [blame] | 198 | } |
| 199 | |
magjed | 59a677a | 2015-06-24 03:59:37 -0700 | [diff] [blame] | 200 | /** |
| 201 | * Release all GLES resources. This needs to be done manually, otherwise the resources are leaked. |
| 202 | */ |
Magnus Jedvert | 5125433 | 2015-12-15 16:22:29 +0100 | [diff] [blame] | 203 | @Override |
magjed | 59a677a | 2015-06-24 03:59:37 -0700 | [diff] [blame] | 204 | public void release() { |
Magnus Jedvert | 5125433 | 2015-12-15 16:22:29 +0100 | [diff] [blame] | 205 | for (Shader shader : shaders.values()) { |
| 206 | shader.glShader.release(); |
magjed | 59a677a | 2015-06-24 03:59:37 -0700 | [diff] [blame] | 207 | } |
Magnus Jedvert | ed4224f | 2015-09-02 12:52:03 +0200 | [diff] [blame] | 208 | shaders.clear(); |
magjed | 59a677a | 2015-06-24 03:59:37 -0700 | [diff] [blame] | 209 | } |
| 210 | } |