From 053e14aaaa7a4e0eb8af4d17c54bdde2acb6eaa0 Mon Sep 17 00:00:00 2001 From: claincly Date: Tue, 25 Oct 2022 14:47:52 +0000 Subject: [PATCH] Add GL utility methods to get 4x4 identity and set identity PiperOrigin-RevId: 483671580 (cherry picked from commit f5ad4e098dd71bd4f99b87350e042acab4f95746) --- .../com/google/android/exoplayer2/util/GlUtil.java | 13 +++++++++++++ .../video/spherical/FrameRotationQueue.java | 5 +++-- .../exoplayer2/video/spherical/SceneRenderer.java | 2 +- .../video/spherical/SphericalGLSurfaceView.java | 7 ++++--- .../video/spherical/FrameRotationQueueTest.java | 5 ++--- .../exoplayer2/effect/ColorLutProcessor.java | 4 +--- .../exoplayer2/effect/ContrastProcessor.java | 4 +--- .../effect/FinalMatrixTextureProcessorWrapper.java | 4 +--- .../android/exoplayer2/effect/HslProcessor.java | 4 +--- .../exoplayer2/effect/MatrixTextureProcessor.java | 11 ++++------- .../android/exoplayer2/effect/RgbAdjustment.java | 4 ++-- .../android/exoplayer2/effect/MatrixUtilsTest.java | 4 ++-- 12 files changed, 35 insertions(+), 32 deletions(-) diff --git a/library/common/src/main/java/com/google/android/exoplayer2/util/GlUtil.java b/library/common/src/main/java/com/google/android/exoplayer2/util/GlUtil.java index 367e6c0ff8..ca0968e823 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/util/GlUtil.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/util/GlUtil.java @@ -29,6 +29,7 @@ import android.opengl.EGLSurface; import android.opengl.GLES11Ext; import android.opengl.GLES20; import android.opengl.GLES30; +import android.opengl.Matrix; import androidx.annotation.DoNotInline; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; @@ -113,6 +114,18 @@ public final class GlUtil { }; } + /** Creates a 4x4 identity matrix. */ + public static float[] create4x4IdentityMatrix() { + float[] matrix = new float[16]; + setToIdentity(matrix); + return matrix; + } + + /** Sets the input {@code matrix} to an identity matrix. */ + public static void setToIdentity(float[] matrix) { + Matrix.setIdentityM(matrix, /* smOffset= */ 0); + } + /** Flattens the list of 4 element NDC coordinate vectors into a buffer. */ public static float[] createVertexBuffer(List vertexList) { float[] vertexBuffer = new float[HOMOGENEOUS_COORDINATE_VECTOR_SIZE * vertexList.size()]; diff --git a/library/core/src/main/java/com/google/android/exoplayer2/video/spherical/FrameRotationQueue.java b/library/core/src/main/java/com/google/android/exoplayer2/video/spherical/FrameRotationQueue.java index 84bdcb7277..e2cecbbe4d 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/video/spherical/FrameRotationQueue.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/video/spherical/FrameRotationQueue.java @@ -16,6 +16,7 @@ package com.google.android.exoplayer2.video.spherical; import android.opengl.Matrix; +import com.google.android.exoplayer2.util.GlUtil; import com.google.android.exoplayer2.util.TimedValueQueue; /** @@ -96,7 +97,7 @@ import com.google.android.exoplayer2.util.TimedValueQueue; // | 0 1 0 0| // recenter = | temp[8] 0 temp[10] 0| // | 0 0 0 1| - Matrix.setIdentityM(recenterMatrix, 0); + GlUtil.setToIdentity(recenterMatrix); float normRowSqr = rotationMatrix[10] * rotationMatrix[10] + rotationMatrix[8] * rotationMatrix[8]; float normRow = (float) Math.sqrt(normRowSqr); @@ -118,7 +119,7 @@ import com.google.android.exoplayer2.util.TimedValueQueue; float angleDeg = (float) Math.toDegrees(angleRad); Matrix.setRotateM(matrix, 0, angleDeg, x / angleRad, y / angleRad, z / angleRad); } else { - Matrix.setIdentityM(matrix, 0); + GlUtil.setToIdentity(matrix); } } } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/video/spherical/SceneRenderer.java b/library/core/src/main/java/com/google/android/exoplayer2/video/spherical/SceneRenderer.java index d9dfc3ad88..0719af26f9 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/video/spherical/SceneRenderer.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/video/spherical/SceneRenderer.java @@ -129,7 +129,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; Log.e(TAG, "Failed to draw a frame", e); } if (resetRotationAtNextFrame.compareAndSet(true, false)) { - Matrix.setIdentityM(rotationMatrix, 0); + GlUtil.setToIdentity(rotationMatrix); } long lastFrameTimestampNs = surfaceTexture.getTimestamp(); Long sampleTimestampUs = sampleTimestampQueue.poll(lastFrameTimestampNs); diff --git a/library/core/src/main/java/com/google/android/exoplayer2/video/spherical/SphericalGLSurfaceView.java b/library/core/src/main/java/com/google/android/exoplayer2/video/spherical/SphericalGLSurfaceView.java index 5f1610350a..a578c8ec73 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/video/spherical/SphericalGLSurfaceView.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/video/spherical/SphericalGLSurfaceView.java @@ -37,6 +37,7 @@ import androidx.annotation.UiThread; import androidx.annotation.VisibleForTesting; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.util.Assertions; +import com.google.android.exoplayer2.util.GlUtil; import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.video.VideoFrameMetadataListener; import java.util.concurrent.CopyOnWriteArrayList; @@ -284,9 +285,9 @@ public final class SphericalGLSurfaceView extends GLSurfaceView { public Renderer(SceneRenderer scene) { this.scene = scene; - Matrix.setIdentityM(deviceOrientationMatrix, 0); - Matrix.setIdentityM(touchPitchMatrix, 0); - Matrix.setIdentityM(touchYawMatrix, 0); + GlUtil.setToIdentity(deviceOrientationMatrix); + GlUtil.setToIdentity(touchPitchMatrix); + GlUtil.setToIdentity(touchYawMatrix); deviceRoll = UPRIGHT_ROLL; } diff --git a/library/core/src/test/java/com/google/android/exoplayer2/video/spherical/FrameRotationQueueTest.java b/library/core/src/test/java/com/google/android/exoplayer2/video/spherical/FrameRotationQueueTest.java index 227a827679..5271f698d8 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/video/spherical/FrameRotationQueueTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/video/spherical/FrameRotationQueueTest.java @@ -19,6 +19,7 @@ import static com.google.common.truth.Truth.assertThat; import android.opengl.Matrix; import androidx.test.ext.junit.runners.AndroidJUnit4; +import com.google.android.exoplayer2.util.GlUtil; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -65,9 +66,7 @@ public class FrameRotationQueueTest { float[] actualMatrix = getRotationMatrixFromAngleAxis( /* angleRadian= */ (float) Math.PI, /* x= */ 0, /* y= */ 1, /* z= */ 0); - float[] expectedMatrix = new float[16]; - Matrix.setIdentityM(expectedMatrix, 0); - assertEquals(actualMatrix, expectedMatrix); + assertEquals(actualMatrix, GlUtil.create4x4IdentityMatrix()); } @Test diff --git a/library/effect/src/main/java/com/google/android/exoplayer2/effect/ColorLutProcessor.java b/library/effect/src/main/java/com/google/android/exoplayer2/effect/ColorLutProcessor.java index 29b5c257ba..d6d2db164f 100644 --- a/library/effect/src/main/java/com/google/android/exoplayer2/effect/ColorLutProcessor.java +++ b/library/effect/src/main/java/com/google/android/exoplayer2/effect/ColorLutProcessor.java @@ -20,7 +20,6 @@ import static com.google.android.exoplayer2.util.Assertions.checkArgument; import android.content.Context; import android.opengl.GLES20; -import android.opengl.Matrix; import android.util.Pair; import com.google.android.exoplayer2.util.FrameProcessingException; import com.google.android.exoplayer2.util.GlProgram; @@ -63,8 +62,7 @@ import java.io.IOException; GlUtil.getNormalizedCoordinateBounds(), GlUtil.HOMOGENEOUS_COORDINATE_VECTOR_SIZE); - float[] identityMatrix = new float[16]; - Matrix.setIdentityM(identityMatrix, /* smOffset= */ 0); + float[] identityMatrix = GlUtil.create4x4IdentityMatrix(); glProgram.setFloatsUniform("uTransformationMatrix", identityMatrix); glProgram.setFloatsUniform("uTexTransformationMatrix", identityMatrix); } diff --git a/library/effect/src/main/java/com/google/android/exoplayer2/effect/ContrastProcessor.java b/library/effect/src/main/java/com/google/android/exoplayer2/effect/ContrastProcessor.java index 947b7956ec..842a88d42c 100644 --- a/library/effect/src/main/java/com/google/android/exoplayer2/effect/ContrastProcessor.java +++ b/library/effect/src/main/java/com/google/android/exoplayer2/effect/ContrastProcessor.java @@ -18,7 +18,6 @@ package com.google.android.exoplayer2.effect; import android.content.Context; import android.opengl.GLES20; -import android.opengl.Matrix; import android.util.Pair; import com.google.android.exoplayer2.util.FrameProcessingException; import com.google.android.exoplayer2.util.GlProgram; @@ -59,8 +58,7 @@ import java.io.IOException; GlUtil.getNormalizedCoordinateBounds(), GlUtil.HOMOGENEOUS_COORDINATE_VECTOR_SIZE); - float[] identityMatrix = new float[16]; - Matrix.setIdentityM(identityMatrix, /* smOffset= */ 0); + float[] identityMatrix = GlUtil.create4x4IdentityMatrix(); glProgram.setFloatsUniform("uTransformationMatrix", identityMatrix); glProgram.setFloatsUniform("uTexTransformationMatrix", identityMatrix); glProgram.setFloatUniform("uContrastFactor", contrastFactor); diff --git a/library/effect/src/main/java/com/google/android/exoplayer2/effect/FinalMatrixTextureProcessorWrapper.java b/library/effect/src/main/java/com/google/android/exoplayer2/effect/FinalMatrixTextureProcessorWrapper.java index f129a3c959..416a732d43 100644 --- a/library/effect/src/main/java/com/google/android/exoplayer2/effect/FinalMatrixTextureProcessorWrapper.java +++ b/library/effect/src/main/java/com/google/android/exoplayer2/effect/FinalMatrixTextureProcessorWrapper.java @@ -25,7 +25,6 @@ import android.opengl.EGLDisplay; import android.opengl.EGLExt; import android.opengl.EGLSurface; import android.opengl.GLES20; -import android.opengl.Matrix; import android.util.Pair; import android.view.Surface; import android.view.SurfaceHolder; @@ -117,8 +116,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; this.colorInfo = colorInfo; this.releaseFramesAutomatically = releaseFramesAutomatically; - textureTransformMatrix = new float[16]; - Matrix.setIdentityM(textureTransformMatrix, /* smOffset= */ 0); + textureTransformMatrix = GlUtil.create4x4IdentityMatrix(); streamOffsetUsQueue = new ConcurrentLinkedQueue<>(); inputListener = new InputListener() {}; availableFrames = new ConcurrentLinkedQueue<>(); diff --git a/library/effect/src/main/java/com/google/android/exoplayer2/effect/HslProcessor.java b/library/effect/src/main/java/com/google/android/exoplayer2/effect/HslProcessor.java index 2e9170d5d9..9132bc0fdc 100644 --- a/library/effect/src/main/java/com/google/android/exoplayer2/effect/HslProcessor.java +++ b/library/effect/src/main/java/com/google/android/exoplayer2/effect/HslProcessor.java @@ -20,7 +20,6 @@ import static com.google.android.exoplayer2.util.Assertions.checkArgument; import android.content.Context; import android.opengl.GLES20; -import android.opengl.Matrix; import android.util.Pair; import com.google.android.exoplayer2.util.FrameProcessingException; import com.google.android.exoplayer2.util.GlProgram; @@ -61,8 +60,7 @@ import java.io.IOException; GlUtil.getNormalizedCoordinateBounds(), GlUtil.HOMOGENEOUS_COORDINATE_VECTOR_SIZE); - float[] identityMatrix = new float[16]; - Matrix.setIdentityM(identityMatrix, /* smOffset= */ 0); + float[] identityMatrix = GlUtil.create4x4IdentityMatrix(); glProgram.setFloatsUniform("uTransformationMatrix", identityMatrix); glProgram.setFloatsUniform("uTexTransformationMatrix", identityMatrix); diff --git a/library/effect/src/main/java/com/google/android/exoplayer2/effect/MatrixTextureProcessor.java b/library/effect/src/main/java/com/google/android/exoplayer2/effect/MatrixTextureProcessor.java index cd8c433460..673767e63c 100644 --- a/library/effect/src/main/java/com/google/android/exoplayer2/effect/MatrixTextureProcessor.java +++ b/library/effect/src/main/java/com/google/android/exoplayer2/effect/MatrixTextureProcessor.java @@ -354,10 +354,8 @@ import java.util.List; transformationMatrixCache = new float[matrixTransformations.size()][16]; rgbMatrixCache = new float[rgbMatrices.size()][16]; - compositeTransformationMatrixArray = new float[16]; - Matrix.setIdentityM(compositeTransformationMatrixArray, /* smOffset= */ 0); - compositeRgbMatrixArray = new float[16]; - Matrix.setIdentityM(compositeRgbMatrixArray, /* smOffset= */ 0); + compositeTransformationMatrixArray = GlUtil.create4x4IdentityMatrix(); + compositeRgbMatrixArray = GlUtil.create4x4IdentityMatrix(); tempResultMatrix = new float[16]; visiblePolygon = NDC_SQUARE; } @@ -373,8 +371,7 @@ import java.util.List; throw new FrameProcessingException(e); } - float[] identityMatrix = new float[16]; - Matrix.setIdentityM(identityMatrix, /* smOffset= */ 0); + float[] identityMatrix = GlUtil.create4x4IdentityMatrix(); glProgram.setFloatsUniform("uTexTransformationMatrix", identityMatrix); return glProgram; } @@ -442,7 +439,7 @@ import java.util.List; // Compute the compositeTransformationMatrix and transform and clip the visiblePolygon for each // MatrixTransformation's matrix. - Matrix.setIdentityM(compositeTransformationMatrixArray, /* smOffset= */ 0); + GlUtil.setToIdentity(compositeTransformationMatrixArray); visiblePolygon = NDC_SQUARE; for (float[] transformationMatrix : transformationMatrixCache) { Matrix.multiplyMM( diff --git a/library/effect/src/main/java/com/google/android/exoplayer2/effect/RgbAdjustment.java b/library/effect/src/main/java/com/google/android/exoplayer2/effect/RgbAdjustment.java index b820737448..d917d0acff 100644 --- a/library/effect/src/main/java/com/google/android/exoplayer2/effect/RgbAdjustment.java +++ b/library/effect/src/main/java/com/google/android/exoplayer2/effect/RgbAdjustment.java @@ -19,6 +19,7 @@ package com.google.android.exoplayer2.effect; import static com.google.android.exoplayer2.util.Assertions.checkArgument; import android.opengl.Matrix; +import com.google.android.exoplayer2.util.GlUtil; import com.google.errorprone.annotations.CanIgnoreReturnValue; /** Scales the red, green, and blue color channels of a frame. */ @@ -78,8 +79,7 @@ public final class RgbAdjustment implements RgbMatrix { /** Creates a new {@link RgbAdjustment} instance. */ public RgbAdjustment build() { - float[] rgbMatrix = new float[16]; - Matrix.setIdentityM(rgbMatrix, /* smOffset= */ 0); + float[] rgbMatrix = GlUtil.create4x4IdentityMatrix(); Matrix.scaleM( rgbMatrix, /* smOffset= */ 0, /* x= */ redScale, /* y= */ greenScale, /* z= */ blueScale); diff --git a/library/effect/src/test/java/com/google/android/exoplayer2/effect/MatrixUtilsTest.java b/library/effect/src/test/java/com/google/android/exoplayer2/effect/MatrixUtilsTest.java index eceab81b2d..f7b7218492 100644 --- a/library/effect/src/test/java/com/google/android/exoplayer2/effect/MatrixUtilsTest.java +++ b/library/effect/src/test/java/com/google/android/exoplayer2/effect/MatrixUtilsTest.java @@ -20,6 +20,7 @@ import static org.junit.Assert.assertThrows; import android.opengl.Matrix; import androidx.test.ext.junit.runners.AndroidJUnit4; +import com.google.android.exoplayer2.util.GlUtil; import com.google.common.collect.ImmutableList; import org.junit.Test; import org.junit.runner.RunWith; @@ -162,8 +163,7 @@ public class MatrixUtilsTest { ImmutableList points = ImmutableList.of( new float[] {-1, 0, 1, 1}, new float[] {1, 0, 1, 1}, new float[] {0, 1, 1, 1}); - float[] scaleMatrix = new float[16]; - Matrix.setIdentityM(scaleMatrix, /* smOffset= */ 0); + float[] scaleMatrix = GlUtil.create4x4IdentityMatrix(); Matrix.scaleM(scaleMatrix, /* mOffset= */ 0, /* x= */ 2, /* y= */ 3, /* z= */ 4); ImmutableList actualTransformedPoints =