blob: 6b36ede824ebef5b24a89f800741f0eb55bc0a61 [file] [log] [blame]
magjed59a677a2015-06-24 03:59:37 -07001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
magjed59a677a2015-06-24 03:59:37 -07003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
magjed59a677a2015-06-24 03:59:37 -07009 */
10
11package org.webrtc;
12
13import android.opengl.GLES11Ext;
14import android.opengl.GLES20;
magjed59a677a2015-06-24 03:59:37 -070015import java.nio.FloatBuffer;
Magnus Jedverted4224f2015-09-02 12:52:03 +020016import java.util.IdentityHashMap;
17import java.util.Map;
magjed59a677a2015-06-24 03:59:37 -070018
19/**
Magnus Jedvert51254332015-12-15 16:22:29 +010020 * 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.
magjed59a677a2015-06-24 03:59:37 -070025 */
Magnus Jedvert51254332015-12-15 16:22:29 +010026public class GlRectDrawer implements RendererCommon.GlDrawer {
sakalb6760f92016-09-29 04:12:44 -070027 // clang-format off
magjed59a677a2015-06-24 03:59:37 -070028 // Simple vertex shader, used for both YUV and OES.
29 private static final String VERTEX_SHADER_STRING =
Magnus Jedverted4224f2015-09-02 12:52:03 +020030 "varying vec2 interp_tc;\n"
magjed59a677a2015-06-24 03:59:37 -070031 + "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 Jedverted4224f2015-09-02 12:52:03 +020042 "precision mediump float;\n"
magjed59a677a2015-06-24 03:59:37 -070043 + "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 Jedverted4224f2015-09-02 12:52:03 +020059 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
magjed59a677a2015-06-24 03:59:37 -070069 private static final String OES_FRAGMENT_SHADER_STRING =
Magnus Jedverted4224f2015-09-02 12:52:03 +020070 "#extension GL_OES_EGL_image_external : require\n"
magjed59a677a2015-06-24 03:59:37 -070071 + "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";
sakalb6760f92016-09-29 04:12:44 -070079 // clang-format on
magjed59a677a2015-06-24 03:59:37 -070080
Magnus Jedvert27551c92015-09-30 18:24:54 +020081 // Vertex coordinates in Normalized Device Coordinates, i.e. (-1, -1) is bottom-left and (1, 1) is
82 // top-right.
sakalb6760f92016-09-29 04:12:44 -070083 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 });
magjed59a677a2015-06-24 03:59:37 -070089
Magnus Jedvert27551c92015-09-30 18:24:54 +020090 // Texture coordinates - (0, 0) is bottom-left and (1, 1) is top-right.
sakalb6760f92016-09-29 04:12:44 -070091 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 });
magjed59a677a2015-06-24 03:59:37 -070097
Magnus Jedvert51254332015-12-15 16:22:29 +010098 private static class Shader {
99 public final GlShader glShader;
100 public final int texMatrixLocation;
Magnus Jedvert7afc12f2015-09-03 12:40:38 +0200101
Magnus Jedvert51254332015-12-15 16:22:29 +0100102 public Shader(String fragmentShader) {
103 this.glShader = new GlShader(VERTEX_SHADER_STRING, fragmentShader);
104 this.texMatrixLocation = glShader.getUniformLocation("texMatrix");
Magnus Jedvert7afc12f2015-09-03 12:40:38 +0200105 }
106 }
magjed59a677a2015-06-24 03:59:37 -0700107
Magnus Jedvert51254332015-12-15 16:22:29 +0100108 // The keys are one of the fragments shaders above.
109 private final Map<String, Shader> shaders = new IdentityHashMap<String, Shader>();
110
magjed59a677a2015-06-24 03:59:37 -0700111 /**
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 Jedvert51254332015-12-15 16:22:29 +0100115 @Override
ivoc1aa435c2016-05-04 07:14:14 -0700116 public void drawOes(int oesTextureId, float[] texMatrix, int frameWidth, int frameHeight,
117 int viewportX, int viewportY, int viewportWidth, int viewportHeight) {
Magnus Jedvert51254332015-12-15 16:22:29 +0100118 prepareShader(OES_FRAGMENT_SHADER_STRING, texMatrix);
119 GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
magjed59a677a2015-06-24 03:59:37 -0700120 // 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);
ivoc1aa435c2016-05-04 07:14:14 -0700123 drawRectangle(viewportX, viewportY, viewportWidth, viewportHeight);
magjed59a677a2015-06-24 03:59:37 -0700124 GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0);
125 }
126
127 /**
Magnus Jedverted4224f2015-09-02 12:52:03 +0200128 * 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 Jedvert51254332015-12-15 16:22:29 +0100131 @Override
ivoc1aa435c2016-05-04 07:14:14 -0700132 public void drawRgb(int textureId, float[] texMatrix, int frameWidth, int frameHeight,
133 int viewportX, int viewportY, int viewportWidth, int viewportHeight) {
Magnus Jedvert51254332015-12-15 16:22:29 +0100134 prepareShader(RGB_FRAGMENT_SHADER_STRING, texMatrix);
135 GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
Magnus Jedverted4224f2015-09-02 12:52:03 +0200136 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
ivoc1aa435c2016-05-04 07:14:14 -0700137 drawRectangle(viewportX, viewportY, viewportWidth, viewportHeight);
Magnus Jedverted4224f2015-09-02 12:52:03 +0200138 // Unbind the texture as a precaution.
139 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
140 }
141
142 /**
magjed59a677a2015-06-24 03:59:37 -0700143 * Draw a YUV frame with specified texture transformation matrix. Required resources are
144 * allocated at the first call to this function.
145 */
Magnus Jedvert51254332015-12-15 16:22:29 +0100146 @Override
ivoc1aa435c2016-05-04 07:14:14 -0700147 public void drawYuv(int[] yuvTextures, float[] texMatrix, int frameWidth, int frameHeight,
148 int viewportX, int viewportY, int viewportWidth, int viewportHeight) {
Magnus Jedvert51254332015-12-15 16:22:29 +0100149 prepareShader(YUV_FRAGMENT_SHADER_STRING, texMatrix);
magjed59a677a2015-06-24 03:59:37 -0700150 // 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 }
ivoc1aa435c2016-05-04 07:14:14 -0700155 drawRectangle(viewportX, viewportY, viewportWidth, viewportHeight);
Magnus Jedverted4224f2015-09-02 12:52:03 +0200156 // 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 }
magjed59a677a2015-06-24 03:59:37 -0700161 }
162
Magnus Jedvert51254332015-12-15 16:22:29 +0100163 private void drawRectangle(int x, int y, int width, int height) {
magjed59a677a2015-06-24 03:59:37 -0700164 // Draw quad.
Magnus Jedvert51254332015-12-15 16:22:29 +0100165 GLES20.glViewport(x, y, width, height);
magjed59a677a2015-06-24 03:59:37 -0700166 GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
167 }
168
Magnus Jedvert51254332015-12-15 16:22:29 +0100169 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 Jedverted4224f2015-09-02 12:52:03 +0200176 shaders.put(fragmentShader, shader);
Magnus Jedvert51254332015-12-15 16:22:29 +0100177 shader.glShader.useProgram();
Magnus Jedverted4224f2015-09-02 12:52:03 +0200178 // Initialize fragment shader uniform values.
179 if (fragmentShader == YUV_FRAGMENT_SHADER_STRING) {
Magnus Jedvert51254332015-12-15 16:22:29 +0100180 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 Jedverted4224f2015-09-02 12:52:03 +0200183 } else if (fragmentShader == RGB_FRAGMENT_SHADER_STRING) {
Magnus Jedvert51254332015-12-15 16:22:29 +0100184 GLES20.glUniform1i(shader.glShader.getUniformLocation("rgb_tex"), 0);
Magnus Jedverted4224f2015-09-02 12:52:03 +0200185 } else if (fragmentShader == OES_FRAGMENT_SHADER_STRING) {
Magnus Jedvert51254332015-12-15 16:22:29 +0100186 GLES20.glUniform1i(shader.glShader.getUniformLocation("oes_tex"), 0);
Magnus Jedverted4224f2015-09-02 12:52:03 +0200187 } else {
188 throw new IllegalStateException("Unknown fragment shader: " + fragmentShader);
189 }
190 GlUtil.checkNoGLES2Error("Initialize fragment shader uniform values.");
191 // Initialize vertex shader attributes.
Magnus Jedvert51254332015-12-15 16:22:29 +0100192 shader.glShader.setVertexAttribArray("in_pos", 2, FULL_RECTANGLE_BUF);
193 shader.glShader.setVertexAttribArray("in_tc", 2, FULL_RECTANGLE_TEX_BUF);
Magnus Jedverted4224f2015-09-02 12:52:03 +0200194 }
Magnus Jedvert51254332015-12-15 16:22:29 +0100195 shader.glShader.useProgram();
196 // Copy the texture transformation matrix over.
197 GLES20.glUniformMatrix4fv(shader.texMatrixLocation, 1, false, texMatrix, 0);
Magnus Jedverted4224f2015-09-02 12:52:03 +0200198 }
199
magjed59a677a2015-06-24 03:59:37 -0700200 /**
201 * Release all GLES resources. This needs to be done manually, otherwise the resources are leaked.
202 */
Magnus Jedvert51254332015-12-15 16:22:29 +0100203 @Override
magjed59a677a2015-06-24 03:59:37 -0700204 public void release() {
Magnus Jedvert51254332015-12-15 16:22:29 +0100205 for (Shader shader : shaders.values()) {
206 shader.glShader.release();
magjed59a677a2015-06-24 03:59:37 -0700207 }
Magnus Jedverted4224f2015-09-02 12:52:03 +0200208 shaders.clear();
magjed59a677a2015-06-24 03:59:37 -0700209 }
210}