From a875fa72a8461987e308eb096f01fb3e3acf96ab Mon Sep 17 00:00:00 2001 From: huangdarwin Date: Fri, 30 Jun 2023 10:50:10 +0000 Subject: [PATCH] GL: Move fallback to glFinish to GlUtil. This may happen, for example, on older OpenGL versions. This may be used in the compositor too. PiperOrigin-RevId: 544613566 --- .../androidx/media3/common/util/GlUtil.java | 20 ++++++++++++------- ...oFrameProcessorTextureOutputPixelTest.java | 8 +------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/libraries/common/src/main/java/androidx/media3/common/util/GlUtil.java b/libraries/common/src/main/java/androidx/media3/common/util/GlUtil.java index 495314e8cf..9a28f11be8 100644 --- a/libraries/common/src/main/java/androidx/media3/common/util/GlUtil.java +++ b/libraries/common/src/main/java/androidx/media3/common/util/GlUtil.java @@ -374,7 +374,8 @@ public final class GlUtil { /** * Returns a newly created sync object and inserts it into the GL command stream. * - *

Returns 0 if the operation failed or the {@link EGLContext} version is less than 3.0. + *

Returns {@code 0} if the operation failed or the {@link EGLContext} version is less than + * 3.0. */ public static long createGlSyncFence() throws GlException { int[] currentEglContextVersion = new int[1]; @@ -404,13 +405,18 @@ public final class GlUtil { } /** - * Ensures that the following commands on the current OpenGL context will not be executed until - * the sync point has been reached. This does not block the CPU, and only affects the current - * OpenGL context. + * Ensures that following commands on the current OpenGL context will not be executed until the + * sync point has been reached. If {@code syncObject} equals {@code 0}, this does not block the + * CPU, and only affects the current OpenGL context. Otherwise, this will block the CPU. */ - public static void waitOnGpu(long syncObject) throws GlException { - GLES30.glWaitSync(syncObject, /* flags= */ 0, GLES30.GL_TIMEOUT_IGNORED); - checkGlError(); + public static void awaitSyncObject(long syncObject) throws GlException { + if (syncObject == 0) { + // Fallback to using glFinish for synchronization when fence creation failed. + GLES20.glFinish(); + } else { + GLES30.glWaitSync(syncObject, /* flags= */ 0, GLES30.GL_TIMEOUT_IGNORED); + checkGlError(); + } } /** Gets the current {@link EGLContext context}. */ diff --git a/libraries/transformer/src/androidTest/java/androidx/media3/transformer/mh/DefaultVideoFrameProcessorTextureOutputPixelTest.java b/libraries/transformer/src/androidTest/java/androidx/media3/transformer/mh/DefaultVideoFrameProcessorTextureOutputPixelTest.java index 8c762d0c49..fcad183b73 100644 --- a/libraries/transformer/src/androidTest/java/androidx/media3/transformer/mh/DefaultVideoFrameProcessorTextureOutputPixelTest.java +++ b/libraries/transformer/src/androidTest/java/androidx/media3/transformer/mh/DefaultVideoFrameProcessorTextureOutputPixelTest.java @@ -32,7 +32,6 @@ import static com.google.common.truth.Truth.assertThat; import android.content.Context; import android.graphics.Bitmap; -import android.opengl.GLES20; import androidx.media3.common.ColorInfo; import androidx.media3.common.Effect; import androidx.media3.common.Format; @@ -554,12 +553,7 @@ public final class DefaultVideoFrameProcessorTextureOutputPixelTest { .setInputType(VideoFrameProcessor.INPUT_TYPE_TEXTURE_ID) .setEffects(effects) .build(); - if (syncObject == GL_FENCE_SYNC_FAILED) { - // Fallback to using glFinish for synchronization when fence creation failed. - GLES20.glFinish(); - } else { - GlUtil.waitOnGpu(syncObject); - } + GlUtil.awaitSyncObject(syncObject); videoFrameProcessorTestRunner.queueInputTexture(texture, presentationTimeUs); try { videoFrameProcessorTestRunner.endFrameProcessing(VIDEO_FRAME_PROCESSING_WAIT_MS / 2);