Android: Add helper class for generating OpenGL shaders

This CL adds a helper class GlShaderBuilder to build an instances of
RendererCommon.GlDrawer that can accept multiple input sources
(OES, RGB, or YUV) using a generic fragment shader as input.

Bug: webrtc:9355
Change-Id: I14a0a280d2b6f838984f7b60897cc0c58e2a948a
Reviewed-on: https://webrtc-review.googlesource.com/80940
Commit-Queue: Magnus Jedvert <magjed@webrtc.org>
Reviewed-by: Sami Kalliomäki <sakal@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23622}
diff --git a/sdk/android/api/org/webrtc/YuvConverter.java b/sdk/android/api/org/webrtc/YuvConverter.java
index f7922d6..be8d43e 100644
--- a/sdk/android/api/org/webrtc/YuvConverter.java
+++ b/sdk/android/api/org/webrtc/YuvConverter.java
@@ -10,10 +10,9 @@
 
 package org.webrtc;
 
-import android.opengl.GLES11Ext;
+import android.graphics.Matrix;
 import android.opengl.GLES20;
 import java.nio.ByteBuffer;
-import java.nio.FloatBuffer;
 import org.webrtc.VideoFrame.I420Buffer;
 import org.webrtc.VideoFrame.TextureBuffer;
 
@@ -22,45 +21,10 @@
  * should only be operated from a single thread with an active EGL context.
  */
 public class YuvConverter {
-  // Vertex coordinates in Normalized Device Coordinates, i.e.
-  // (-1, -1) is bottom-left and (1, 1) is top-right.
-  private static final FloatBuffer DEVICE_RECTANGLE = GlUtil.createFloatBuffer(new float[] {
-      -1.0f, -1.0f, // Bottom left.
-      1.0f, -1.0f, // Bottom right.
-      -1.0f, 1.0f, // Top left.
-      1.0f, 1.0f, // Top right.
-  });
-
-  // Texture coordinates - (0, 0) is bottom-left and (1, 1) is top-right.
-  private static final FloatBuffer TEXTURE_RECTANGLE = GlUtil.createFloatBuffer(new float[] {
-      0.0f, 0.0f, // Bottom left.
-      1.0f, 0.0f, // Bottom right.
-      0.0f, 1.0f, // Top left.
-      1.0f, 1.0f // Top right.
-  });
-
-  // clang-format off
-  private static final String VERTEX_SHADER =
-        "varying vec2 interp_tc;\n"
-      + "attribute vec4 in_pos;\n"
-      + "attribute vec4 in_tc;\n"
-      + "\n"
-      + "uniform mat4 texMatrix;\n"
-      + "\n"
-      + "void main() {\n"
-      + "    gl_Position = in_pos;\n"
-      + "    interp_tc = (texMatrix * in_tc).xy;\n"
-      + "}\n";
-
-  private static final String OES_FRAGMENT_SHADER =
-        "#extension GL_OES_EGL_image_external : require\n"
-      + "precision mediump float;\n"
-      + "varying vec2 interp_tc;\n"
-      + "\n"
-      + "uniform samplerExternalOES tex;\n"
+  private static final String FRAGMENT_SHADER =
       // Difference in texture coordinate corresponding to one
       // sub-pixel in the x direction.
-      + "uniform vec2 xUnit;\n"
+      "uniform vec2 xUnit;\n"
       // Color conversion coefficients, including constant term
       + "uniform vec4 coeffs;\n"
       + "\n"
@@ -72,52 +36,66 @@
       // try to do it as a vec3 x mat3x4, followed by an add in of a
       // constant vector.
       + "  gl_FragColor.r = coeffs.a + dot(coeffs.rgb,\n"
-      + "      texture2D(tex, interp_tc - 1.5 * xUnit).rgb);\n"
+      + "      sample(tc - 1.5 * xUnit).rgb);\n"
       + "  gl_FragColor.g = coeffs.a + dot(coeffs.rgb,\n"
-      + "      texture2D(tex, interp_tc - 0.5 * xUnit).rgb);\n"
+      + "      sample(tc - 0.5 * xUnit).rgb);\n"
       + "  gl_FragColor.b = coeffs.a + dot(coeffs.rgb,\n"
-      + "      texture2D(tex, interp_tc + 0.5 * xUnit).rgb);\n"
+      + "      sample(tc + 0.5 * xUnit).rgb);\n"
       + "  gl_FragColor.a = coeffs.a + dot(coeffs.rgb,\n"
-      + "      texture2D(tex, interp_tc + 1.5 * xUnit).rgb);\n"
+      + "      sample(tc + 1.5 * xUnit).rgb);\n"
       + "}\n";
 
-  private static final String RGB_FRAGMENT_SHADER =
-        "precision mediump float;\n"
-      + "varying vec2 interp_tc;\n"
-      + "\n"
-      + "uniform sampler2D tex;\n"
-      // Difference in texture coordinate corresponding to one
-      // sub-pixel in the x direction.
-      + "uniform vec2 xUnit;\n"
-      // Color conversion coefficients, including constant term
-      + "uniform vec4 coeffs;\n"
-      + "\n"
-      + "void main() {\n"
-      // Since the alpha read from the texture is always 1, this could
-      // be written as a mat4 x vec4 multiply. However, that seems to
-      // give a worse framerate, possibly because the additional
-      // multiplies by 1.0 consume resources. TODO(nisse): Could also
-      // try to do it as a vec3 x mat3x4, followed by an add in of a
-      // constant vector.
-      + "  gl_FragColor.r = coeffs.a + dot(coeffs.rgb,\n"
-      + "      texture2D(tex, interp_tc - 1.5 * xUnit).rgb);\n"
-      + "  gl_FragColor.g = coeffs.a + dot(coeffs.rgb,\n"
-      + "      texture2D(tex, interp_tc - 0.5 * xUnit).rgb);\n"
-      + "  gl_FragColor.b = coeffs.a + dot(coeffs.rgb,\n"
-      + "      texture2D(tex, interp_tc + 0.5 * xUnit).rgb);\n"
-      + "  gl_FragColor.a = coeffs.a + dot(coeffs.rgb,\n"
-      + "      texture2D(tex, interp_tc + 1.5 * xUnit).rgb);\n"
-      + "}\n";
-  // clang-format on
+  private static class ShaderCallbacks implements GlGenericDrawer.ShaderCallbacks {
+    // Y'UV444 to RGB888, see https://en.wikipedia.org/wiki/YUV#Y.27UV444_to_RGB888_conversion. We
+    // use the ITU-R coefficients for U and V.
+    private static final float[] yCoeffs = new float[] {0.2987856f, 0.5871095f, 0.1141049f, 0.0f};
+    private static final float[] uCoeffs =
+        new float[] {-0.168805420f, -0.3317003f, 0.5005057f, 0.5f};
+    private static final float[] vCoeffs = new float[] {0.4997964f, -0.4184672f, -0.0813292f, 0.5f};
+
+    private int xUnitLoc;
+    private int coeffsLoc;
+
+    private float[] coeffs;
+    private float stepSize;
+
+    public void setPlaneY() {
+      coeffs = yCoeffs;
+      stepSize = 1.0f;
+    }
+
+    public void setPlaneU() {
+      coeffs = uCoeffs;
+      stepSize = 2.0f;
+    }
+
+    public void setPlaneV() {
+      coeffs = vCoeffs;
+      stepSize = 2.0f;
+    }
+
+    @Override
+    public void onNewShader(GlShader shader) {
+      xUnitLoc = shader.getUniformLocation("xUnit");
+      coeffsLoc = shader.getUniformLocation("coeffs");
+    }
+
+    @Override
+    public void onPrepareShader(GlShader shader, float[] texMatrix, int frameWidth, int frameHeight,
+        int viewportWidth, int viewportHeight) {
+      GLES20.glUniform4fv(coeffsLoc, /* count= */ 1, coeffs, /* offset= */ 0);
+      // Matrix * (1;0;0;0) / (width / stepSize). Note that OpenGL uses column major order.
+      GLES20.glUniform2f(
+          xUnitLoc, stepSize * texMatrix[0] / frameWidth, stepSize * texMatrix[1] / frameWidth);
+    }
+  }
 
   private final ThreadUtils.ThreadChecker threadChecker = new ThreadUtils.ThreadChecker();
-  private final GlTextureFrameBuffer textureFrameBuffer = new GlTextureFrameBuffer(GLES20.GL_RGBA);
-  private TextureBuffer.Type shaderTextureType;
-  private GlShader shader;
-  private int texMatrixLoc;
-  private int xUnitLoc;
-  private int coeffsLoc;
+  private final GlTextureFrameBuffer i420TextureFrameBuffer =
+      new GlTextureFrameBuffer(GLES20.GL_RGBA);
   private boolean released = false;
+  private final ShaderCallbacks shaderCallbacks = new ShaderCallbacks();
+  private final GlGenericDrawer drawer = new GlGenericDrawer(FRAGMENT_SHADER, shaderCallbacks);
 
   /**
    * This class should be constructed on a thread that has an active EGL context.
@@ -127,96 +105,11 @@
   }
 
   /** Converts the texture buffer to I420. */
-  public I420Buffer convert(TextureBuffer textureBuffer) {
-    final int width = textureBuffer.getWidth();
-    final int height = textureBuffer.getHeight();
-
-    // SurfaceTextureHelper requires a stride that is divisible by 8.  Round width up.
-    // See SurfaceTextureHelper for details on the size and format.
-    final int stride = ((width + 7) / 8) * 8;
-    final int uvHeight = (height + 1) / 2;
-    // Due to the layout used by SurfaceTextureHelper, vPos + stride * uvHeight would overrun the
-    // buffer.  Add one row at the bottom to compensate for this.  There will never be data in the
-    // extra row, but now other code does not have to deal with v stride * v height exceeding the
-    // buffer's capacity.
-    final int size = stride * (height + uvHeight + 1);
-    ByteBuffer buffer = JniCommon.nativeAllocateByteBuffer(size);
-    convert(buffer, width, height, stride, textureBuffer.getTextureId(),
-        RendererCommon.convertMatrixFromAndroidGraphicsMatrix(textureBuffer.getTransformMatrix()),
-        textureBuffer.getType());
-
-    final int yPos = 0;
-    final int uPos = yPos + stride * height;
-    // Rows of U and V alternate in the buffer, so V data starts after the first row of U.
-    final int vPos = uPos + stride / 2;
-
-    buffer.position(yPos);
-    buffer.limit(yPos + stride * height);
-    ByteBuffer dataY = buffer.slice();
-
-    buffer.position(uPos);
-    buffer.limit(uPos + stride * uvHeight);
-    ByteBuffer dataU = buffer.slice();
-
-    buffer.position(vPos);
-    buffer.limit(vPos + stride * uvHeight);
-    ByteBuffer dataV = buffer.slice();
-
-    // SurfaceTextureHelper uses the same stride for Y, U, and V data.
-    return JavaI420Buffer.wrap(width, height, dataY, stride, dataU, stride, dataV, stride,
-        () -> { JniCommon.nativeFreeByteBuffer(buffer); });
-  }
-
-  /** Deprecated, use convert(TextureBuffer). */
-  @Deprecated
-  void convert(ByteBuffer buf, int width, int height, int stride, int srcTextureId,
-      float[] transformMatrix) {
-    convert(buf, width, height, stride, srcTextureId, transformMatrix, TextureBuffer.Type.OES);
-  }
-
-  private void initShader(TextureBuffer.Type textureType) {
-    if (shader != null) {
-      shader.release();
-    }
-
-    final String fragmentShader;
-    switch (textureType) {
-      case OES:
-        fragmentShader = OES_FRAGMENT_SHADER;
-        break;
-      case RGB:
-        fragmentShader = RGB_FRAGMENT_SHADER;
-        break;
-      default:
-        throw new IllegalArgumentException("Unsupported texture type.");
-    }
-
-    shaderTextureType = textureType;
-    shader = new GlShader(VERTEX_SHADER, fragmentShader);
-    shader.useProgram();
-    texMatrixLoc = shader.getUniformLocation("texMatrix");
-    xUnitLoc = shader.getUniformLocation("xUnit");
-    coeffsLoc = shader.getUniformLocation("coeffs");
-    GLES20.glUniform1i(shader.getUniformLocation("tex"), 0);
-    GlUtil.checkNoGLES2Error("Initialize fragment shader uniform values.");
-    // Initialize vertex shader attributes.
-    shader.setVertexAttribArray("in_pos", 2, DEVICE_RECTANGLE);
-    // If the width is not a multiple of 4 pixels, the texture
-    // will be scaled up slightly and clipped at the right border.
-    shader.setVertexAttribArray("in_tc", 2, TEXTURE_RECTANGLE);
-  }
-
-  private void convert(ByteBuffer buf, int width, int height, int stride, int srcTextureId,
-      float[] transformMatrix, TextureBuffer.Type textureType) {
+  public I420Buffer convert(TextureBuffer inputTextureBuffer) {
     threadChecker.checkIsOnValidThread();
     if (released) {
       throw new IllegalStateException("YuvConverter.convert called on released object");
     }
-    if (textureType != shaderTextureType) {
-      initShader(textureType);
-    }
-    shader.useProgram();
-
     // We draw into a buffer laid out like
     //
     //    +---------+
@@ -245,83 +138,83 @@
     // Since the V data needs to start on a boundary of such a
     // larger pixel, it is not sufficient that |stride| is even, it
     // has to be a multiple of 8 pixels.
+    final int frameWidth = inputTextureBuffer.getWidth();
+    final int frameHeight = inputTextureBuffer.getHeight();
+    final int stride = ((frameWidth + 7) / 8) * 8;
+    final int uvHeight = (frameHeight + 1) / 2;
+    // Total height of the combined memory layout.
+    final int totalHeight = frameHeight + uvHeight;
+    final ByteBuffer i420ByteBuffer = JniCommon.nativeAllocateByteBuffer(stride * totalHeight);
+    // Viewport width is divided by four since we are squeezing in four color bytes in each RGBA
+    // pixel.
+    final int viewportWidth = stride / 4;
 
-    if (stride % 8 != 0) {
-      throw new IllegalArgumentException("Invalid stride, must be a multiple of 8");
-    }
-    if (stride < width) {
-      throw new IllegalArgumentException("Invalid stride, must >= width");
-    }
+    // Produce a frame buffer starting at top-left corner, not bottom-left.
+    final Matrix renderMatrix = new Matrix();
+    renderMatrix.preTranslate(0.5f, 0.5f);
+    renderMatrix.preScale(1f, -1f);
+    renderMatrix.preTranslate(-0.5f, -0.5f);
 
-    int y_width = (width + 3) / 4;
-    int uv_width = (width + 7) / 8;
-    int uv_height = (height + 1) / 2;
-    int total_height = height + uv_height;
-    int size = stride * total_height;
-
-    if (buf.capacity() < size) {
-      throw new IllegalArgumentException("YuvConverter.convert called with too small buffer");
-    }
-    // Produce a frame buffer starting at top-left corner, not
-    // bottom-left.
-    transformMatrix =
-        RendererCommon.multiplyMatrices(transformMatrix, RendererCommon.verticalFlipMatrix());
-
-    final int frameBufferWidth = stride / 4;
-    final int frameBufferHeight = total_height;
-    textureFrameBuffer.setSize(frameBufferWidth, frameBufferHeight);
+    i420TextureFrameBuffer.setSize(viewportWidth, totalHeight);
 
     // Bind our framebuffer.
-    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, textureFrameBuffer.getFrameBufferId());
+    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, i420TextureFrameBuffer.getFrameBufferId());
     GlUtil.checkNoGLES2Error("glBindFramebuffer");
 
-    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
-    GLES20.glBindTexture(textureType.getGlTarget(), srcTextureId);
-    GLES20.glUniformMatrix4fv(texMatrixLoc, 1, false, transformMatrix, 0);
+    // Draw Y.
+    shaderCallbacks.setPlaneY();
+    VideoFrameDrawer.drawTexture(drawer, inputTextureBuffer, renderMatrix, frameWidth, frameHeight,
+        /* viewportX= */ 0, /* viewportY= */ 0, viewportWidth,
+        /* viewportHeight= */ frameHeight);
 
-    // Draw Y
-    GLES20.glViewport(0, 0, y_width, height);
-    // Matrix * (1;0;0;0) / width. Note that opengl uses column major order.
-    GLES20.glUniform2f(xUnitLoc, transformMatrix[0] / width, transformMatrix[1] / width);
-    // Y'UV444 to RGB888, see
-    // https://en.wikipedia.org/wiki/YUV#Y.27UV444_to_RGB888_conversion.
-    // We use the ITU-R coefficients for U and V */
-    GLES20.glUniform4f(coeffsLoc, 0.2987856f, 0.5871095f, 0.1141049f, 0.0f);
-    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
+    // Draw U.
+    shaderCallbacks.setPlaneU();
+    VideoFrameDrawer.drawTexture(drawer, inputTextureBuffer, renderMatrix, frameWidth, frameHeight,
+        /* viewportX= */ 0, /* viewportY= */ frameHeight, viewportWidth / 2,
+        /* viewportHeight= */ uvHeight);
 
-    // Draw U
-    GLES20.glViewport(0, height, uv_width, uv_height);
-    // Matrix * (1;0;0;0) / (width / 2). Note that opengl uses column major order.
-    GLES20.glUniform2f(
-        xUnitLoc, 2.0f * transformMatrix[0] / width, 2.0f * transformMatrix[1] / width);
-    GLES20.glUniform4f(coeffsLoc, -0.168805420f, -0.3317003f, 0.5005057f, 0.5f);
-    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
+    // Draw V.
+    shaderCallbacks.setPlaneV();
+    VideoFrameDrawer.drawTexture(drawer, inputTextureBuffer, renderMatrix, frameWidth, frameHeight,
+        /* viewportX= */ viewportWidth / 2, /* viewportY= */ frameHeight, viewportWidth / 2,
+        /* viewportHeight= */ uvHeight);
 
-    // Draw V
-    GLES20.glViewport(stride / 8, height, uv_width, uv_height);
-    GLES20.glUniform4f(coeffsLoc, 0.4997964f, -0.4184672f, -0.0813292f, 0.5f);
-    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
-
-    GLES20.glReadPixels(
-        0, 0, frameBufferWidth, frameBufferHeight, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buf);
+    GLES20.glReadPixels(0, 0, i420TextureFrameBuffer.getWidth(), i420TextureFrameBuffer.getHeight(),
+        GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, i420ByteBuffer);
 
     GlUtil.checkNoGLES2Error("YuvConverter.convert");
 
     // Restore normal framebuffer.
     GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
-    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
 
-    // Unbind texture. Reportedly needed on some devices to get
-    // the texture updated from the camera.
-    GLES20.glBindTexture(textureType.getGlTarget(), 0);
+    // Prepare Y, U, and V ByteBuffer slices.
+    final int yPos = 0;
+    final int uPos = yPos + stride * frameHeight;
+    // Rows of U and V alternate in the buffer, so V data starts after the first row of U.
+    final int vPos = uPos + stride / 2;
+
+    i420ByteBuffer.position(yPos);
+    i420ByteBuffer.limit(yPos + stride * frameHeight);
+    final ByteBuffer dataY = i420ByteBuffer.slice();
+
+    i420ByteBuffer.position(uPos);
+    // The last row does not have padding.
+    final int uvSize = stride * (uvHeight - 1) + stride / 2;
+    i420ByteBuffer.limit(uPos + uvSize);
+    final ByteBuffer dataU = i420ByteBuffer.slice();
+
+    i420ByteBuffer.position(vPos);
+    i420ByteBuffer.limit(vPos + uvSize);
+    final ByteBuffer dataV = i420ByteBuffer.slice();
+
+    return JavaI420Buffer.wrap(frameWidth, frameHeight, dataY, stride, dataU, stride, dataV, stride,
+        () -> { JniCommon.nativeFreeByteBuffer(i420ByteBuffer); });
   }
 
   public void release() {
     threadChecker.checkIsOnValidThread();
     released = true;
-    if (shader != null) {
-      shader.release();
-    }
-    textureFrameBuffer.release();
+    drawer.release();
+    i420TextureFrameBuffer.release();
   }
 }