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

PiperOrigin-RevId: 483671580
This commit is contained in:
claincly 2022-10-25 14:47:52 +00:00 committed by microkatz
parent eaf1f2153a
commit f5ad4e098d
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.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<float[]> vertexList) {
float[] vertexBuffer = new float[HOMOGENEOUS_COORDINATE_VECTOR_SIZE * vertexList.size()];

View File

@ -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);
}
}
}

View File

@ -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);

View File

@ -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;
}

View File

@ -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

View File

@ -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);
}

View File

@ -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);

View File

@ -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<>();

View File

@ -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);

View File

@ -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(

View File

@ -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);

View File

@ -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<float[]> 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<float[]> actualTransformedPoints =