From 1cbdad34a23765b088a8a26d8d2797a18f8d37d6 Mon Sep 17 00:00:00 2001 From: hschlueter Date: Mon, 4 Apr 2022 15:42:19 +0100 Subject: [PATCH] Support android.opengl.Matrix in AdvancedFrameProcessor. This allows apps to use AdvancedFrameProcessor to apply transformations in 3D space. This functionality is not used in transformer otherwise. PiperOrigin-RevId: 439313406 --- .../transformer/AdvancedFrameProcessor.java | 30 ++++++++++--- .../AdvancedFrameProcessorTest.java | 45 +++++++++++++++++++ 2 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 library/transformer/src/test/java/com/google/android/exoplayer2/transformer/AdvancedFrameProcessorTest.java diff --git a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/AdvancedFrameProcessor.java b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/AdvancedFrameProcessor.java index ec3f6bdb85..6e40a1e9e8 100644 --- a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/AdvancedFrameProcessor.java +++ b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/AdvancedFrameProcessor.java @@ -15,6 +15,7 @@ */ package com.google.android.exoplayer2.transformer; +import static com.google.android.exoplayer2.util.Assertions.checkArgument; import static com.google.android.exoplayer2.util.Assertions.checkStateNotNull; import android.content.Context; @@ -44,7 +45,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. */ @@ -85,7 +87,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; @@ -94,13 +96,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 @@ -115,7 +131,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/library/transformer/src/test/java/com/google/android/exoplayer2/transformer/AdvancedFrameProcessorTest.java b/library/transformer/src/test/java/com/google/android/exoplayer2/transformer/AdvancedFrameProcessorTest.java new file mode 100644 index 0000000000..5b20b961b4 --- /dev/null +++ b/library/transformer/src/test/java/com/google/android/exoplayer2/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 com.google.android.exoplayer2.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]); + } +}