mirror of
https://github.com/samsonjs/media.git
synced 2026-03-25 09:25:53 +00:00
Add GL utility methods to get 4x4 identity and set identity
PiperOrigin-RevId: 483671580
(cherry picked from commit be7bb0eea4)
This commit is contained in:
parent
88a413b2cb
commit
5974bee7c5
12 changed files with 35 additions and 32 deletions
|
|
@ -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;
|
||||
|
|
@ -114,6 +115,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()];
|
||||
|
|
|
|||
|
|
@ -20,7 +20,6 @@ import static androidx.media3.common.util.Assertions.checkArgument;
|
|||
|
||||
import android.content.Context;
|
||||
import android.opengl.GLES20;
|
||||
import android.opengl.Matrix;
|
||||
import android.util.Pair;
|
||||
import androidx.media3.common.FrameProcessingException;
|
||||
import androidx.media3.common.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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,6 @@ package androidx.media3.effect;
|
|||
|
||||
import android.content.Context;
|
||||
import android.opengl.GLES20;
|
||||
import android.opengl.Matrix;
|
||||
import android.util.Pair;
|
||||
import androidx.media3.common.FrameProcessingException;
|
||||
import androidx.media3.common.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);
|
||||
|
|
|
|||
|
|
@ -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<>();
|
||||
|
|
|
|||
|
|
@ -20,7 +20,6 @@ import static androidx.media3.common.util.Assertions.checkArgument;
|
|||
|
||||
import android.content.Context;
|
||||
import android.opengl.GLES20;
|
||||
import android.opengl.Matrix;
|
||||
import android.util.Pair;
|
||||
import androidx.media3.common.FrameProcessingException;
|
||||
import androidx.media3.common.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);
|
||||
|
||||
|
|
|
|||
|
|
@ -356,10 +356,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;
|
||||
}
|
||||
|
|
@ -375,8 +373,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;
|
||||
}
|
||||
|
|
@ -444,7 +441,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(
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ package androidx.media3.effect;
|
|||
import static androidx.media3.common.util.Assertions.checkArgument;
|
||||
|
||||
import android.opengl.Matrix;
|
||||
import androidx.media3.common.util.GlUtil;
|
||||
import androidx.media3.common.util.UnstableApi;
|
||||
import com.google.errorprone.annotations.CanIgnoreReturnValue;
|
||||
|
||||
|
|
@ -80,8 +81,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);
|
||||
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ import static com.google.common.truth.Truth.assertThat;
|
|||
import static org.junit.Assert.assertThrows;
|
||||
|
||||
import android.opengl.Matrix;
|
||||
import androidx.media3.common.util.GlUtil;
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import org.junit.Test;
|
||||
|
|
@ -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 =
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
package androidx.media3.exoplayer.video.spherical;
|
||||
|
||||
import android.opengl.Matrix;
|
||||
import androidx.media3.common.util.GlUtil;
|
||||
import androidx.media3.common.util.TimedValueQueue;
|
||||
|
||||
/**
|
||||
|
|
@ -96,7 +97,7 @@ import androidx.media3.common.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 androidx.media3.common.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@ import androidx.annotation.UiThread;
|
|||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.media3.common.C;
|
||||
import androidx.media3.common.util.Assertions;
|
||||
import androidx.media3.common.util.GlUtil;
|
||||
import androidx.media3.common.util.UnstableApi;
|
||||
import androidx.media3.common.util.Util;
|
||||
import androidx.media3.exoplayer.video.VideoFrameMetadataListener;
|
||||
|
|
@ -286,9 +287,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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ package androidx.media3.exoplayer.video.spherical;
|
|||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import android.opengl.Matrix;
|
||||
import androidx.media3.common.util.GlUtil;
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue