Android camera: Replace custom matrix operations with android.opengl.Matrix

Bug: webrtc:9412, webrtc:9487
Change-Id: I68e5a03026b1ab8236a05ece79690d4a8755c093
Reviewed-on: https://webrtc-review.googlesource.com/86947
Commit-Queue: Magnus Jedvert <magjed@webrtc.org>
Reviewed-by: Sami Kalliomäki <sakal@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23851}
diff --git a/sdk/android/api/org/webrtc/SurfaceTextureHelper.java b/sdk/android/api/org/webrtc/SurfaceTextureHelper.java
index 0b5bcf8..2d6d13d 100644
--- a/sdk/android/api/org/webrtc/SurfaceTextureHelper.java
+++ b/sdk/android/api/org/webrtc/SurfaceTextureHelper.java
@@ -287,7 +287,7 @@
    * The returned TextureBuffer holds a reference to the SurfaceTextureHelper that created it. The
    * buffer calls returnTextureFrame() when it is released.
    */
-  public TextureBuffer createTextureBuffer(int width, int height, Matrix transformMatrix) {
+  public TextureBufferImpl createTextureBuffer(int width, int height, Matrix transformMatrix) {
     return new TextureBufferImpl(width, height, TextureBuffer.Type.OES, oesTextureId,
         transformMatrix, handler, yuvConverter, this ::returnTextureFrame);
   }
diff --git a/sdk/android/api/org/webrtc/TextureBufferImpl.java b/sdk/android/api/org/webrtc/TextureBufferImpl.java
index c34728a..96d7d43 100644
--- a/sdk/android/api/org/webrtc/TextureBufferImpl.java
+++ b/sdk/android/api/org/webrtc/TextureBufferImpl.java
@@ -84,15 +84,27 @@
   @Override
   public VideoFrame.Buffer cropAndScale(
       int cropX, int cropY, int cropWidth, int cropHeight, int scaleWidth, int scaleHeight) {
-    final Matrix newMatrix = new Matrix(transformMatrix);
+    final Matrix cropAndScaleMatrix = new Matrix();
     // In WebRTC, Y=0 is the top row, while in OpenGL Y=0 is the bottom row. This means that the Y
     // direction is effectively reversed.
     final int cropYFromBottom = height - (cropY + cropHeight);
-    newMatrix.preTranslate(cropX / (float) width, cropYFromBottom / (float) height);
-    newMatrix.preScale(cropWidth / (float) width, cropHeight / (float) height);
+    cropAndScaleMatrix.preTranslate(cropX / (float) width, cropYFromBottom / (float) height);
+    cropAndScaleMatrix.preScale(cropWidth / (float) width, cropHeight / (float) height);
 
+    return applyTransformMatrix(cropAndScaleMatrix, scaleWidth, scaleHeight);
+  }
+
+  /**
+   * Create a new TextureBufferImpl with an applied transform matrix and a new size. The
+   * existing buffer is unchanged. The given transform matrix is applied first when texture
+   * coordinates are still in the unmodified [0, 1] range.
+   */
+  public TextureBufferImpl applyTransformMatrix(
+      Matrix transformMatrix, int newWidth, int newHeight) {
+    final Matrix newMatrix = new Matrix(this.transformMatrix);
+    newMatrix.preConcat(transformMatrix);
     retain();
     return new TextureBufferImpl(
-        scaleWidth, scaleHeight, type, id, newMatrix, toI420Handler, yuvConverter, this ::release);
+        newWidth, newHeight, type, id, newMatrix, toI420Handler, yuvConverter, this ::release);
   }
 }
diff --git a/sdk/android/api/org/webrtc/VideoFrame.java b/sdk/android/api/org/webrtc/VideoFrame.java
index 6d19260..4afe782 100644
--- a/sdk/android/api/org/webrtc/VideoFrame.java
+++ b/sdk/android/api/org/webrtc/VideoFrame.java
@@ -118,7 +118,7 @@
      * homogeneous coordinates of the form (s, t, 1) with s and t in the inclusive range [0, 1] to
      * the coordinate that should be used to sample that location from the buffer.
      */
-    public Matrix getTransformMatrix();
+    Matrix getTransformMatrix();
   }
 
   private final Buffer buffer;