diff --git a/libraries/transformer/src/main/java/androidx/media3/transformer/AdvancedFrameProcessor.java b/libraries/transformer/src/main/java/androidx/media3/transformer/AdvancedFrameProcessor.java index cc5b23ec57..583be441e3 100644 --- a/libraries/transformer/src/main/java/androidx/media3/transformer/AdvancedFrameProcessor.java +++ b/libraries/transformer/src/main/java/androidx/media3/transformer/AdvancedFrameProcessor.java @@ -15,6 +15,7 @@ */ package androidx.media3.transformer; +import static androidx.media3.common.util.Assertions.checkArgument; import static androidx.media3.common.util.Assertions.checkStateNotNull; import android.content.Context; @@ -46,7 +47,8 @@ public final class AdvancedFrameProcessor implements GlFrameProcessor { private static final String FRAGMENT_SHADER_PATH = "shaders/fragment_shader_copy_es2.glsl"; /** - * Returns a 4x4, column-major Matrix float array, from an input {@link Matrix}. + * Returns a 4x4, column-major {@link android.opengl.Matrix} float array, from an input {@link + * Matrix}. * *

This is useful for converting to the 4x4 column-major format commonly used in OpenGL. */ @@ -87,7 +89,7 @@ public final class AdvancedFrameProcessor implements GlFrameProcessor { } private final Context context; - private final Matrix transformationMatrix; + private final float[] transformationMatrix; private @MonotonicNonNull Size size; private @MonotonicNonNull GlProgram glProgram; @@ -96,13 +98,27 @@ public final class AdvancedFrameProcessor implements GlFrameProcessor { * Creates a new instance. * * @param context The {@link Context}. - * @param transformationMatrix The transformation matrix to apply to each frame. Operations are - * done on normalized device coordinates (-1 to 1 on x and y), and no automatic adjustments - * are applied on the transformation matrix. + * @param transformationMatrix The transformation {@link Matrix} to apply to each frame. + * Operations are done on normalized device coordinates (-1 to 1 on x and y), and no automatic + * adjustments are applied on the transformation matrix. */ public AdvancedFrameProcessor(Context context, Matrix transformationMatrix) { + this(context, getGlMatrixArray(transformationMatrix)); + } + + /** + * Creates a new instance. + * + * @param context The {@link Context}. + * @param transformationMatrix The 4x4 transformation {@link android.opengl.Matrix} to apply to + * each frame. Operations are done on normalized device coordinates (-1 to 1 on x and y), and + * no automatic adjustments are applied on the transformation matrix. + */ + public AdvancedFrameProcessor(Context context, float[] transformationMatrix) { + checkArgument( + transformationMatrix.length == 16, "A 4x4 transformation matrix must have 16 elements."); this.context = context; - this.transformationMatrix = new Matrix(transformationMatrix); + this.transformationMatrix = transformationMatrix.clone(); } @Override @@ -117,7 +133,7 @@ public final class AdvancedFrameProcessor implements GlFrameProcessor { "aFramePosition", GlUtil.getNormalizedCoordinateBounds(), GlUtil.RECTANGLE_VERTICES_COUNT); glProgram.setBufferAttribute( "aTexSamplingCoord", GlUtil.getTextureCoordinateBounds(), GlUtil.RECTANGLE_VERTICES_COUNT); - glProgram.setFloatsUniform("uTransformationMatrix", getGlMatrixArray(transformationMatrix)); + glProgram.setFloatsUniform("uTransformationMatrix", transformationMatrix); } @Override diff --git a/libraries/transformer/src/test/java/androidx/media3/transformer/AdvancedFrameProcessorTest.java b/libraries/transformer/src/test/java/androidx/media3/transformer/AdvancedFrameProcessorTest.java new file mode 100644 index 0000000000..bd4e16d567 --- /dev/null +++ b/libraries/transformer/src/test/java/androidx/media3/transformer/AdvancedFrameProcessorTest.java @@ -0,0 +1,45 @@ +/* + * Copyright 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package androidx.media3.transformer; + +import static androidx.test.core.app.ApplicationProvider.getApplicationContext; +import static org.junit.Assert.assertThrows; + +import androidx.test.ext.junit.runners.AndroidJUnit4; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * Unit tests for {@link AdvancedFrameProcessor}. + * + *

See {@code AdvancedFrameProcessorPixelTest} for pixel tests testing {@link + * AdvancedFrameProcessor} given a transformation matrix. + */ +@RunWith(AndroidJUnit4.class) +public final class AdvancedFrameProcessorTest { + + @Test + public void construct_withInvalidMatrixSize_throwsException() { + assertThrows( + IllegalArgumentException.class, + () -> new AdvancedFrameProcessor(getApplicationContext(), new float[4])); + } + + @Test + public void construct_withValidMatrixSize_completesSucessfully() { + new AdvancedFrameProcessor(getApplicationContext(), new float[16]); + } +}