Add GL utility methods to get 4x4 identity and set identity

PiperOrigin-RevId: 483671580
(cherry picked from commit f5ad4e098dd71bd4f99b87350e042acab4f95746)
This commit is contained in:
claincly 2022-10-25 14:47:52 +00:00 committed by microkatz
parent f7f371fa3e
commit 053e14aaaa
12 changed files with 35 additions and 32 deletions

View File

@ -29,6 +29,7 @@ import android.opengl.EGLSurface;
import android.opengl.GLES11Ext; import android.opengl.GLES11Ext;
import android.opengl.GLES20; import android.opengl.GLES20;
import android.opengl.GLES30; import android.opengl.GLES30;
import android.opengl.Matrix;
import androidx.annotation.DoNotInline; import androidx.annotation.DoNotInline;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi; 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. */ /** Flattens the list of 4 element NDC coordinate vectors into a buffer. */
public static float[] createVertexBuffer(List<float[]> vertexList) { public static float[] createVertexBuffer(List<float[]> vertexList) {
float[] vertexBuffer = new float[HOMOGENEOUS_COORDINATE_VECTOR_SIZE * vertexList.size()]; float[] vertexBuffer = new float[HOMOGENEOUS_COORDINATE_VECTOR_SIZE * vertexList.size()];

View File

@ -16,6 +16,7 @@
package com.google.android.exoplayer2.video.spherical; package com.google.android.exoplayer2.video.spherical;
import android.opengl.Matrix; import android.opengl.Matrix;
import com.google.android.exoplayer2.util.GlUtil;
import com.google.android.exoplayer2.util.TimedValueQueue; import com.google.android.exoplayer2.util.TimedValueQueue;
/** /**
@ -96,7 +97,7 @@ import com.google.android.exoplayer2.util.TimedValueQueue;
// | 0 1 0 0| // | 0 1 0 0|
// recenter = | temp[8] 0 temp[10] 0| // recenter = | temp[8] 0 temp[10] 0|
// | 0 0 0 1| // | 0 0 0 1|
Matrix.setIdentityM(recenterMatrix, 0); GlUtil.setToIdentity(recenterMatrix);
float normRowSqr = float normRowSqr =
rotationMatrix[10] * rotationMatrix[10] + rotationMatrix[8] * rotationMatrix[8]; rotationMatrix[10] * rotationMatrix[10] + rotationMatrix[8] * rotationMatrix[8];
float normRow = (float) Math.sqrt(normRowSqr); float normRow = (float) Math.sqrt(normRowSqr);
@ -118,7 +119,7 @@ import com.google.android.exoplayer2.util.TimedValueQueue;
float angleDeg = (float) Math.toDegrees(angleRad); float angleDeg = (float) Math.toDegrees(angleRad);
Matrix.setRotateM(matrix, 0, angleDeg, x / angleRad, y / angleRad, z / angleRad); Matrix.setRotateM(matrix, 0, angleDeg, x / angleRad, y / angleRad, z / angleRad);
} else { } else {
Matrix.setIdentityM(matrix, 0); GlUtil.setToIdentity(matrix);
} }
} }
} }

View File

@ -129,7 +129,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
Log.e(TAG, "Failed to draw a frame", e); Log.e(TAG, "Failed to draw a frame", e);
} }
if (resetRotationAtNextFrame.compareAndSet(true, false)) { if (resetRotationAtNextFrame.compareAndSet(true, false)) {
Matrix.setIdentityM(rotationMatrix, 0); GlUtil.setToIdentity(rotationMatrix);
} }
long lastFrameTimestampNs = surfaceTexture.getTimestamp(); long lastFrameTimestampNs = surfaceTexture.getTimestamp();
Long sampleTimestampUs = sampleTimestampQueue.poll(lastFrameTimestampNs); Long sampleTimestampUs = sampleTimestampQueue.poll(lastFrameTimestampNs);

View File

@ -37,6 +37,7 @@ import androidx.annotation.UiThread;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.util.Assertions; 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.util.Util;
import com.google.android.exoplayer2.video.VideoFrameMetadataListener; import com.google.android.exoplayer2.video.VideoFrameMetadataListener;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
@ -284,9 +285,9 @@ public final class SphericalGLSurfaceView extends GLSurfaceView {
public Renderer(SceneRenderer scene) { public Renderer(SceneRenderer scene) {
this.scene = scene; this.scene = scene;
Matrix.setIdentityM(deviceOrientationMatrix, 0); GlUtil.setToIdentity(deviceOrientationMatrix);
Matrix.setIdentityM(touchPitchMatrix, 0); GlUtil.setToIdentity(touchPitchMatrix);
Matrix.setIdentityM(touchYawMatrix, 0); GlUtil.setToIdentity(touchYawMatrix);
deviceRoll = UPRIGHT_ROLL; deviceRoll = UPRIGHT_ROLL;
} }

View File

@ -19,6 +19,7 @@ import static com.google.common.truth.Truth.assertThat;
import android.opengl.Matrix; import android.opengl.Matrix;
import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.util.GlUtil;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -65,9 +66,7 @@ public class FrameRotationQueueTest {
float[] actualMatrix = float[] actualMatrix =
getRotationMatrixFromAngleAxis( getRotationMatrixFromAngleAxis(
/* angleRadian= */ (float) Math.PI, /* x= */ 0, /* y= */ 1, /* z= */ 0); /* angleRadian= */ (float) Math.PI, /* x= */ 0, /* y= */ 1, /* z= */ 0);
float[] expectedMatrix = new float[16]; assertEquals(actualMatrix, GlUtil.create4x4IdentityMatrix());
Matrix.setIdentityM(expectedMatrix, 0);
assertEquals(actualMatrix, expectedMatrix);
} }
@Test @Test

View File

@ -20,7 +20,6 @@ import static com.google.android.exoplayer2.util.Assertions.checkArgument;
import android.content.Context; import android.content.Context;
import android.opengl.GLES20; import android.opengl.GLES20;
import android.opengl.Matrix;
import android.util.Pair; import android.util.Pair;
import com.google.android.exoplayer2.util.FrameProcessingException; import com.google.android.exoplayer2.util.FrameProcessingException;
import com.google.android.exoplayer2.util.GlProgram; import com.google.android.exoplayer2.util.GlProgram;
@ -63,8 +62,7 @@ import java.io.IOException;
GlUtil.getNormalizedCoordinateBounds(), GlUtil.getNormalizedCoordinateBounds(),
GlUtil.HOMOGENEOUS_COORDINATE_VECTOR_SIZE); GlUtil.HOMOGENEOUS_COORDINATE_VECTOR_SIZE);
float[] identityMatrix = new float[16]; float[] identityMatrix = GlUtil.create4x4IdentityMatrix();
Matrix.setIdentityM(identityMatrix, /* smOffset= */ 0);
glProgram.setFloatsUniform("uTransformationMatrix", identityMatrix); glProgram.setFloatsUniform("uTransformationMatrix", identityMatrix);
glProgram.setFloatsUniform("uTexTransformationMatrix", identityMatrix); glProgram.setFloatsUniform("uTexTransformationMatrix", identityMatrix);
} }

View File

@ -18,7 +18,6 @@ package com.google.android.exoplayer2.effect;
import android.content.Context; import android.content.Context;
import android.opengl.GLES20; import android.opengl.GLES20;
import android.opengl.Matrix;
import android.util.Pair; import android.util.Pair;
import com.google.android.exoplayer2.util.FrameProcessingException; import com.google.android.exoplayer2.util.FrameProcessingException;
import com.google.android.exoplayer2.util.GlProgram; import com.google.android.exoplayer2.util.GlProgram;
@ -59,8 +58,7 @@ import java.io.IOException;
GlUtil.getNormalizedCoordinateBounds(), GlUtil.getNormalizedCoordinateBounds(),
GlUtil.HOMOGENEOUS_COORDINATE_VECTOR_SIZE); GlUtil.HOMOGENEOUS_COORDINATE_VECTOR_SIZE);
float[] identityMatrix = new float[16]; float[] identityMatrix = GlUtil.create4x4IdentityMatrix();
Matrix.setIdentityM(identityMatrix, /* smOffset= */ 0);
glProgram.setFloatsUniform("uTransformationMatrix", identityMatrix); glProgram.setFloatsUniform("uTransformationMatrix", identityMatrix);
glProgram.setFloatsUniform("uTexTransformationMatrix", identityMatrix); glProgram.setFloatsUniform("uTexTransformationMatrix", identityMatrix);
glProgram.setFloatUniform("uContrastFactor", contrastFactor); glProgram.setFloatUniform("uContrastFactor", contrastFactor);

View File

@ -25,7 +25,6 @@ import android.opengl.EGLDisplay;
import android.opengl.EGLExt; import android.opengl.EGLExt;
import android.opengl.EGLSurface; import android.opengl.EGLSurface;
import android.opengl.GLES20; import android.opengl.GLES20;
import android.opengl.Matrix;
import android.util.Pair; import android.util.Pair;
import android.view.Surface; import android.view.Surface;
import android.view.SurfaceHolder; import android.view.SurfaceHolder;
@ -117,8 +116,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
this.colorInfo = colorInfo; this.colorInfo = colorInfo;
this.releaseFramesAutomatically = releaseFramesAutomatically; this.releaseFramesAutomatically = releaseFramesAutomatically;
textureTransformMatrix = new float[16]; textureTransformMatrix = GlUtil.create4x4IdentityMatrix();
Matrix.setIdentityM(textureTransformMatrix, /* smOffset= */ 0);
streamOffsetUsQueue = new ConcurrentLinkedQueue<>(); streamOffsetUsQueue = new ConcurrentLinkedQueue<>();
inputListener = new InputListener() {}; inputListener = new InputListener() {};
availableFrames = new ConcurrentLinkedQueue<>(); availableFrames = new ConcurrentLinkedQueue<>();

View File

@ -20,7 +20,6 @@ import static com.google.android.exoplayer2.util.Assertions.checkArgument;
import android.content.Context; import android.content.Context;
import android.opengl.GLES20; import android.opengl.GLES20;
import android.opengl.Matrix;
import android.util.Pair; import android.util.Pair;
import com.google.android.exoplayer2.util.FrameProcessingException; import com.google.android.exoplayer2.util.FrameProcessingException;
import com.google.android.exoplayer2.util.GlProgram; import com.google.android.exoplayer2.util.GlProgram;
@ -61,8 +60,7 @@ import java.io.IOException;
GlUtil.getNormalizedCoordinateBounds(), GlUtil.getNormalizedCoordinateBounds(),
GlUtil.HOMOGENEOUS_COORDINATE_VECTOR_SIZE); GlUtil.HOMOGENEOUS_COORDINATE_VECTOR_SIZE);
float[] identityMatrix = new float[16]; float[] identityMatrix = GlUtil.create4x4IdentityMatrix();
Matrix.setIdentityM(identityMatrix, /* smOffset= */ 0);
glProgram.setFloatsUniform("uTransformationMatrix", identityMatrix); glProgram.setFloatsUniform("uTransformationMatrix", identityMatrix);
glProgram.setFloatsUniform("uTexTransformationMatrix", identityMatrix); glProgram.setFloatsUniform("uTexTransformationMatrix", identityMatrix);

View File

@ -354,10 +354,8 @@ import java.util.List;
transformationMatrixCache = new float[matrixTransformations.size()][16]; transformationMatrixCache = new float[matrixTransformations.size()][16];
rgbMatrixCache = new float[rgbMatrices.size()][16]; rgbMatrixCache = new float[rgbMatrices.size()][16];
compositeTransformationMatrixArray = new float[16]; compositeTransformationMatrixArray = GlUtil.create4x4IdentityMatrix();
Matrix.setIdentityM(compositeTransformationMatrixArray, /* smOffset= */ 0); compositeRgbMatrixArray = GlUtil.create4x4IdentityMatrix();
compositeRgbMatrixArray = new float[16];
Matrix.setIdentityM(compositeRgbMatrixArray, /* smOffset= */ 0);
tempResultMatrix = new float[16]; tempResultMatrix = new float[16];
visiblePolygon = NDC_SQUARE; visiblePolygon = NDC_SQUARE;
} }
@ -373,8 +371,7 @@ import java.util.List;
throw new FrameProcessingException(e); throw new FrameProcessingException(e);
} }
float[] identityMatrix = new float[16]; float[] identityMatrix = GlUtil.create4x4IdentityMatrix();
Matrix.setIdentityM(identityMatrix, /* smOffset= */ 0);
glProgram.setFloatsUniform("uTexTransformationMatrix", identityMatrix); glProgram.setFloatsUniform("uTexTransformationMatrix", identityMatrix);
return glProgram; return glProgram;
} }
@ -442,7 +439,7 @@ import java.util.List;
// Compute the compositeTransformationMatrix and transform and clip the visiblePolygon for each // Compute the compositeTransformationMatrix and transform and clip the visiblePolygon for each
// MatrixTransformation's matrix. // MatrixTransformation's matrix.
Matrix.setIdentityM(compositeTransformationMatrixArray, /* smOffset= */ 0); GlUtil.setToIdentity(compositeTransformationMatrixArray);
visiblePolygon = NDC_SQUARE; visiblePolygon = NDC_SQUARE;
for (float[] transformationMatrix : transformationMatrixCache) { for (float[] transformationMatrix : transformationMatrixCache) {
Matrix.multiplyMM( Matrix.multiplyMM(

View File

@ -19,6 +19,7 @@ package com.google.android.exoplayer2.effect;
import static com.google.android.exoplayer2.util.Assertions.checkArgument; import static com.google.android.exoplayer2.util.Assertions.checkArgument;
import android.opengl.Matrix; import android.opengl.Matrix;
import com.google.android.exoplayer2.util.GlUtil;
import com.google.errorprone.annotations.CanIgnoreReturnValue; import com.google.errorprone.annotations.CanIgnoreReturnValue;
/** Scales the red, green, and blue color channels of a frame. */ /** 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. */ /** Creates a new {@link RgbAdjustment} instance. */
public RgbAdjustment build() { public RgbAdjustment build() {
float[] rgbMatrix = new float[16]; float[] rgbMatrix = GlUtil.create4x4IdentityMatrix();
Matrix.setIdentityM(rgbMatrix, /* smOffset= */ 0);
Matrix.scaleM( Matrix.scaleM(
rgbMatrix, /* smOffset= */ 0, /* x= */ redScale, /* y= */ greenScale, /* z= */ blueScale); rgbMatrix, /* smOffset= */ 0, /* x= */ redScale, /* y= */ greenScale, /* z= */ blueScale);

View File

@ -20,6 +20,7 @@ import static org.junit.Assert.assertThrows;
import android.opengl.Matrix; import android.opengl.Matrix;
import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.util.GlUtil;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -162,8 +163,7 @@ public class MatrixUtilsTest {
ImmutableList<float[]> points = ImmutableList<float[]> points =
ImmutableList.of( ImmutableList.of(
new float[] {-1, 0, 1, 1}, new float[] {1, 0, 1, 1}, new float[] {0, 1, 1, 1}); new float[] {-1, 0, 1, 1}, new float[] {1, 0, 1, 1}, new float[] {0, 1, 1, 1});
float[] scaleMatrix = new float[16]; float[] scaleMatrix = GlUtil.create4x4IdentityMatrix();
Matrix.setIdentityM(scaleMatrix, /* smOffset= */ 0);
Matrix.scaleM(scaleMatrix, /* mOffset= */ 0, /* x= */ 2, /* y= */ 3, /* z= */ 4); Matrix.scaleM(scaleMatrix, /* mOffset= */ 0, /* x= */ 2, /* y= */ 3, /* z= */ 4);
ImmutableList<float[]> actualTransformedPoints = ImmutableList<float[]> actualTransformedPoints =