From dc668f2b59bb0df12cdbb77cb88072babe0218eb Mon Sep 17 00:00:00 2001 From: hschlueter Date: Thu, 9 Jun 2022 17:53:39 +0000 Subject: [PATCH] Make GlUtil.GlException checked and remove flag to disable it. Transformer always enabled glAssertionsEnabled, so there should be no functional change. ExoPlayer previously disabled glAssertionsEnabled, so GlUtil logged GlExceptions instead of throwing them. The GlExceptions are now caught and logged by the callers so that there should also be no functional change overall. This change also replaces EGLSurfaceTexture#GlException with GlUtil#GlException. PiperOrigin-RevId: 453963741 --- .../gldemo/BitmapOverlayVideoProcessor.java | 29 +++- .../gldemo/VideoProcessingGLSurfaceView.java | 8 +- .../BitmapOverlayProcessor.java | 27 ++-- .../PeriodicVignetteProcessor.java | 23 +-- .../demo/transformer/MediaPipeProcessor.java | 16 +- .../exoplayer2/util/EGLSurfaceTexture.java | 51 +++---- .../android/exoplayer2/util/GlProgram.java | 30 ++-- .../android/exoplayer2/util/GlUtil.java | 139 +++++++++--------- .../exoplayer2/video/PlaceholderSurface.java | 5 +- .../video/VideoDecoderGLSurfaceView.java | 61 +++++--- .../video/spherical/ProjectionRenderer.java | 49 ++++-- .../video/spherical/SceneRenderer.java | 32 +++- .../transformer/BitmapTestUtil.java | 5 +- .../exoplayer2/transformer/CropPixelTest.java | 26 ++-- ...atrixTransformationProcessorPixelTest.java | 18 +-- .../transformer/PresentationPixelTest.java | 26 ++-- .../android/exoplayer2/transformer/Crop.java | 4 - .../EncoderCompatibilityTransformation.java | 5 - .../transformer/ExternalTextureProcessor.java | 24 +-- .../transformer/FrameProcessingException.java | 20 +++ .../transformer/FrameProcessorChain.java | 27 ++-- .../exoplayer2/transformer/GlEffect.java | 4 +- .../transformer/GlMatrixTransformation.java | 4 +- .../transformer/GlTextureProcessor.java | 8 +- .../MatrixTransformationProcessor.java | 32 ++-- .../exoplayer2/transformer/Presentation.java | 5 - .../transformer/ScaleToFitTransformation.java | 4 - .../SingleFrameGlTextureProcessor.java | 12 +- 28 files changed, 397 insertions(+), 297 deletions(-) diff --git a/demos/gl/src/main/java/com/google/android/exoplayer2/gldemo/BitmapOverlayVideoProcessor.java b/demos/gl/src/main/java/com/google/android/exoplayer2/gldemo/BitmapOverlayVideoProcessor.java index 42dbc5b2b7..f941fefe13 100644 --- a/demos/gl/src/main/java/com/google/android/exoplayer2/gldemo/BitmapOverlayVideoProcessor.java +++ b/demos/gl/src/main/java/com/google/android/exoplayer2/gldemo/BitmapOverlayVideoProcessor.java @@ -29,6 +29,7 @@ import android.opengl.GLUtils; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.util.GlProgram; import com.google.android.exoplayer2.util.GlUtil; +import com.google.android.exoplayer2.util.Log; import java.io.IOException; import java.util.Locale; import javax.microedition.khronos.opengles.GL10; @@ -41,6 +42,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; /* package */ final class BitmapOverlayVideoProcessor implements VideoProcessingGLSurfaceView.VideoProcessor { + private static final String TAG = "BitmapOverlayVP"; private static final int OVERLAY_WIDTH = 512; private static final int OVERLAY_HEIGHT = 256; @@ -85,6 +87,9 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; /* fragmentShaderFilePath= */ "bitmap_overlay_video_processor_fragment.glsl"); } catch (IOException e) { throw new IllegalStateException(e); + } catch (GlUtil.GlException e) { + Log.e(TAG, "Failed to initialize the shader program", e); + return; } program.setBufferAttribute( "aFramePosition", @@ -119,7 +124,11 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; GLES20.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]); GLUtils.texSubImage2D( GL10.GL_TEXTURE_2D, /* level= */ 0, /* xoffset= */ 0, /* yoffset= */ 0, overlayBitmap); - GlUtil.checkGlError(); + try { + GlUtil.checkGlError(); + } catch (GlUtil.GlException e) { + Log.e(TAG, "Failed to populate the texture", e); + } // Run the shader program. GlProgram program = checkNotNull(this.program); @@ -128,16 +137,28 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; program.setFloatUniform("uScaleX", bitmapScaleX); program.setFloatUniform("uScaleY", bitmapScaleY); program.setFloatsUniform("uTexTransform", transformMatrix); - program.bindAttributesAndUniforms(); + try { + program.bindAttributesAndUniforms(); + } catch (GlUtil.GlException e) { + Log.e(TAG, "Failed to update the shader program", e); + } GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, /* first= */ 0, /* count= */ 4); - GlUtil.checkGlError(); + try { + GlUtil.checkGlError(); + } catch (GlUtil.GlException e) { + Log.e(TAG, "Failed to draw a frame", e); + } } @Override public void release() { if (program != null) { - program.delete(); + try { + program.delete(); + } catch (GlUtil.GlException e) { + Log.e(TAG, "Failed to delete the shader program", e); + } } } } diff --git a/demos/gl/src/main/java/com/google/android/exoplayer2/gldemo/VideoProcessingGLSurfaceView.java b/demos/gl/src/main/java/com/google/android/exoplayer2/gldemo/VideoProcessingGLSurfaceView.java index 2bdd087251..648609284f 100644 --- a/demos/gl/src/main/java/com/google/android/exoplayer2/gldemo/VideoProcessingGLSurfaceView.java +++ b/demos/gl/src/main/java/com/google/android/exoplayer2/gldemo/VideoProcessingGLSurfaceView.java @@ -29,6 +29,7 @@ import com.google.android.exoplayer2.ExoPlayer; import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.GlUtil; +import com.google.android.exoplayer2.util.Log; import com.google.android.exoplayer2.util.TimedValueQueue; import com.google.android.exoplayer2.video.VideoFrameMetadataListener; import java.util.concurrent.atomic.AtomicBoolean; @@ -70,6 +71,7 @@ public final class VideoProcessingGLSurfaceView extends GLSurfaceView { } private static final int EGL_PROTECTED_CONTENT_EXT = 0x32C0; + private static final String TAG = "VPGlSurfaceView"; private final VideoRenderer renderer; private final Handler mainHandler; @@ -239,7 +241,11 @@ public final class VideoProcessingGLSurfaceView extends GLSurfaceView { @Override public synchronized void onSurfaceCreated(GL10 gl, EGLConfig config) { - texture = GlUtil.createExternalTexture(); + try { + texture = GlUtil.createExternalTexture(); + } catch (GlUtil.GlException e) { + Log.e(TAG, "Failed to create an external texture", e); + } surfaceTexture = new SurfaceTexture(texture); surfaceTexture.setOnFrameAvailableListener( surfaceTexture -> { diff --git a/demos/transformer/src/main/java/com/google/android/exoplayer2/transformerdemo/BitmapOverlayProcessor.java b/demos/transformer/src/main/java/com/google/android/exoplayer2/transformerdemo/BitmapOverlayProcessor.java index 17f921cf37..9e71e15c87 100644 --- a/demos/transformer/src/main/java/com/google/android/exoplayer2/transformerdemo/BitmapOverlayProcessor.java +++ b/demos/transformer/src/main/java/com/google/android/exoplayer2/transformerdemo/BitmapOverlayProcessor.java @@ -45,9 +45,6 @@ import java.util.Locale; // TODO(b/227625365): Delete this class and use a texture processor from the Transformer library, // once overlaying a bitmap and text is supported in Transformer. /* package */ final class BitmapOverlayProcessor extends SingleFrameGlTextureProcessor { - static { - GlUtil.glAssertionsEnabled = true; - } private static final String VERTEX_SHADER_PATH = "vertex_shader_copy_es2.glsl"; private static final String FRAGMENT_SHADER_PATH = "fragment_shader_bitmap_overlay_es2.glsl"; @@ -67,9 +64,9 @@ import java.util.Locale; /** * Creates a new instance. * - * @throws IOException If a problem occurs while reading shader files. + * @throws FrameProcessingException If a problem occurs while reading shader files. */ - public BitmapOverlayProcessor(Context context) throws IOException { + public BitmapOverlayProcessor(Context context) throws FrameProcessingException { paint = new Paint(); paint.setTextSize(64); paint.setAntiAlias(true); @@ -87,10 +84,14 @@ import java.util.Locale; } catch (PackageManager.NameNotFoundException e) { throw new IllegalStateException(e); } - bitmapTexId = GlUtil.createTexture(BITMAP_WIDTH_HEIGHT, BITMAP_WIDTH_HEIGHT); - GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, /* level= */ 0, overlayBitmap, /* border= */ 0); + try { + bitmapTexId = GlUtil.createTexture(BITMAP_WIDTH_HEIGHT, BITMAP_WIDTH_HEIGHT); + GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, /* level= */ 0, overlayBitmap, /* border= */ 0); - glProgram = new GlProgram(context, VERTEX_SHADER_PATH, FRAGMENT_SHADER_PATH); + glProgram = new GlProgram(context, VERTEX_SHADER_PATH, FRAGMENT_SHADER_PATH); + } catch (GlUtil.GlException | IOException e) { + throw new FrameProcessingException(e); + } // Draw the frame on the entire normalized device coordinate space, from -1 to 1, for x and y. glProgram.setBufferAttribute( "aFramePosition", @@ -141,15 +142,19 @@ import java.util.Locale; GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, /* first= */ 0, /* count= */ 4); GlUtil.checkGlError(); } catch (GlUtil.GlException e) { - throw new FrameProcessingException(e); + throw new FrameProcessingException(e, presentationTimeUs); } } @Override - public void release() { + public void release() throws FrameProcessingException { super.release(); if (glProgram != null) { - glProgram.delete(); + try { + glProgram.delete(); + } catch (GlUtil.GlException e) { + throw new FrameProcessingException(e); + } } } diff --git a/demos/transformer/src/main/java/com/google/android/exoplayer2/transformerdemo/PeriodicVignetteProcessor.java b/demos/transformer/src/main/java/com/google/android/exoplayer2/transformerdemo/PeriodicVignetteProcessor.java index eb4f97af2e..f48f221b39 100644 --- a/demos/transformer/src/main/java/com/google/android/exoplayer2/transformerdemo/PeriodicVignetteProcessor.java +++ b/demos/transformer/src/main/java/com/google/android/exoplayer2/transformerdemo/PeriodicVignetteProcessor.java @@ -31,9 +31,6 @@ import java.io.IOException; * darker the further they are away from the frame center. */ /* package */ final class PeriodicVignetteProcessor extends SingleFrameGlTextureProcessor { - static { - GlUtil.glAssertionsEnabled = true; - } private static final String VERTEX_SHADER_PATH = "vertex_shader_copy_es2.glsl"; private static final String FRAGMENT_SHADER_PATH = "fragment_shader_vignette_es2.glsl"; @@ -60,7 +57,7 @@ import java.io.IOException; * @param minInnerRadius The lower bound of the radius that is unaffected by the effect. * @param maxInnerRadius The upper bound of the radius that is unaffected by the effect. * @param outerRadius The radius after which all pixels are black. - * @throws IOException If a problem occurs while reading shader files. + * @throws FrameProcessingException If a problem occurs while reading shader files. */ public PeriodicVignetteProcessor( Context context, @@ -69,12 +66,16 @@ import java.io.IOException; float minInnerRadius, float maxInnerRadius, float outerRadius) - throws IOException { + throws FrameProcessingException { checkArgument(minInnerRadius <= maxInnerRadius); checkArgument(maxInnerRadius <= outerRadius); this.minInnerRadius = minInnerRadius; this.deltaInnerRadius = maxInnerRadius - minInnerRadius; - glProgram = new GlProgram(context, VERTEX_SHADER_PATH, FRAGMENT_SHADER_PATH); + try { + glProgram = new GlProgram(context, VERTEX_SHADER_PATH, FRAGMENT_SHADER_PATH); + } catch (IOException | GlUtil.GlException e) { + throw new FrameProcessingException(e); + } glProgram.setFloatsUniform("uCenter", new float[] {centerX, centerY}); glProgram.setFloatsUniform("uOuterRadius", new float[] {outerRadius}); // Draw the frame on the entire normalized device coordinate space, from -1 to 1, for x and y. @@ -102,15 +103,19 @@ import java.io.IOException; // The four-vertex triangle strip forms a quad. GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, /* first= */ 0, /* count= */ 4); } catch (GlUtil.GlException e) { - throw new FrameProcessingException(e); + throw new FrameProcessingException(e, presentationTimeUs); } } @Override - public void release() { + public void release() throws FrameProcessingException { super.release(); if (glProgram != null) { - glProgram.delete(); + try { + glProgram.delete(); + } catch (GlUtil.GlException e) { + throw new FrameProcessingException(e); + } } } } diff --git a/demos/transformer/src/withMediaPipe/java/androidx/media3/demo/transformer/MediaPipeProcessor.java b/demos/transformer/src/withMediaPipe/java/androidx/media3/demo/transformer/MediaPipeProcessor.java index 2654980eb6..424a76b2be 100644 --- a/demos/transformer/src/withMediaPipe/java/androidx/media3/demo/transformer/MediaPipeProcessor.java +++ b/demos/transformer/src/withMediaPipe/java/androidx/media3/demo/transformer/MediaPipeProcessor.java @@ -79,12 +79,12 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; * @param graphName Name of a MediaPipe graph asset to load. * @param inputStreamName Name of the input video stream in the graph. * @param outputStreamName Name of the input video stream in the graph. - * @throws IOException If a problem occurs while reading shader files or initializing MediaPipe - * resources. + * @throws FrameProcessingException If a problem occurs while reading shader files or initializing + * MediaPipe resources. */ public MediaPipeProcessor( Context context, String graphName, String inputStreamName, String outputStreamName) - throws IOException { + throws FrameProcessingException { checkState(LOADER.isAvailable()); frameProcessorConditionVariable = new ConditionVariable(); @@ -104,7 +104,11 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; frameProcessorPendingError = error; frameProcessorConditionVariable.open(); }); - glProgram = new GlProgram(context, COPY_VERTEX_SHADER_NAME, COPY_FRAGMENT_SHADER_NAME); + try { + glProgram = new GlProgram(context, COPY_VERTEX_SHADER_NAME, COPY_FRAGMENT_SHADER_NAME); + } catch (IOException | GlUtil.GlException e) { + throw new FrameProcessingException(e); + } } @Override @@ -152,14 +156,14 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, /* first= */ 0, /* count= */ 4); GlUtil.checkGlError(); } catch (GlUtil.GlException e) { - throw new FrameProcessingException(e); + throw new FrameProcessingException(e, presentationTimeUs); } finally { checkStateNotNull(outputFrame).release(); } } @Override - public void release() { + public void release() throws FrameProcessingException { super.release(); checkStateNotNull(frameProcessor).close(); } diff --git a/library/common/src/main/java/com/google/android/exoplayer2/util/EGLSurfaceTexture.java b/library/common/src/main/java/com/google/android/exoplayer2/util/EGLSurfaceTexture.java index 011a48cd8c..7855a4ca3e 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/util/EGLSurfaceTexture.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/util/EGLSurfaceTexture.java @@ -78,13 +78,6 @@ public final class EGLSurfaceTexture implements SurfaceTexture.OnFrameAvailableL private static final int EGL_PROTECTED_CONTENT_EXT = 0x32C0; - /** A runtime exception to be thrown if some EGL operations failed. */ - public static final class GlException extends RuntimeException { - private GlException(String msg) { - super(msg); - } - } - private final Handler handler; private final int[] textureIdHolder; @Nullable private final TextureImageListener callback; @@ -124,7 +117,7 @@ public final class EGLSurfaceTexture implements SurfaceTexture.OnFrameAvailableL * * @param secureMode The {@link SecureMode} to be used for EGL surface. */ - public void init(@SecureMode int secureMode) { + public void init(@SecureMode int secureMode) throws GlUtil.GlException { display = getDefaultDisplay(); EGLConfig config = chooseEGLConfig(display); context = createEGLContext(display, config, secureMode); @@ -205,22 +198,18 @@ public final class EGLSurfaceTexture implements SurfaceTexture.OnFrameAvailableL } } - private static EGLDisplay getDefaultDisplay() { + private static EGLDisplay getDefaultDisplay() throws GlUtil.GlException { EGLDisplay display = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY); - if (display == null) { - throw new GlException("eglGetDisplay failed"); - } + GlUtil.checkGlException(display != null, "eglGetDisplay failed"); int[] version = new int[2]; boolean eglInitialized = EGL14.eglInitialize(display, version, /* majorOffset= */ 0, version, /* minorOffset= */ 1); - if (!eglInitialized) { - throw new GlException("eglInitialize failed"); - } + GlUtil.checkGlException(eglInitialized, "eglInitialize failed"); return display; } - private static EGLConfig chooseEGLConfig(EGLDisplay display) { + private static EGLConfig chooseEGLConfig(EGLDisplay display) throws GlUtil.GlException { EGLConfig[] configs = new EGLConfig[1]; int[] numConfigs = new int[1]; boolean success = @@ -233,18 +222,17 @@ public final class EGLSurfaceTexture implements SurfaceTexture.OnFrameAvailableL /* config_size= */ 1, numConfigs, /* num_configOffset= */ 0); - if (!success || numConfigs[0] <= 0 || configs[0] == null) { - throw new GlException( - Util.formatInvariant( - /* format= */ "eglChooseConfig failed: success=%b, numConfigs[0]=%d, configs[0]=%s", - success, numConfigs[0], configs[0])); - } + GlUtil.checkGlException( + success && numConfigs[0] > 0 && configs[0] != null, + Util.formatInvariant( + /* format= */ "eglChooseConfig failed: success=%b, numConfigs[0]=%d, configs[0]=%s", + success, numConfigs[0], configs[0])); return configs[0]; } private static EGLContext createEGLContext( - EGLDisplay display, EGLConfig config, @SecureMode int secureMode) { + EGLDisplay display, EGLConfig config, @SecureMode int secureMode) throws GlUtil.GlException { int[] glAttributes; if (secureMode == SECURE_MODE_NONE) { glAttributes = new int[] {EGL14.EGL_CONTEXT_CLIENT_VERSION, 2, EGL14.EGL_NONE}; @@ -261,14 +249,13 @@ public final class EGLSurfaceTexture implements SurfaceTexture.OnFrameAvailableL EGLContext context = EGL14.eglCreateContext( display, config, android.opengl.EGL14.EGL_NO_CONTEXT, glAttributes, 0); - if (context == null) { - throw new GlException("eglCreateContext failed"); - } + GlUtil.checkGlException(context != null, "eglCreateContext failed"); return context; } private static EGLSurface createEGLSurface( - EGLDisplay display, EGLConfig config, EGLContext context, @SecureMode int secureMode) { + EGLDisplay display, EGLConfig config, EGLContext context, @SecureMode int secureMode) + throws GlUtil.GlException { EGLSurface surface; if (secureMode == SECURE_MODE_SURFACELESS_CONTEXT) { surface = EGL14.EGL_NO_SURFACE; @@ -296,20 +283,16 @@ public final class EGLSurfaceTexture implements SurfaceTexture.OnFrameAvailableL }; } surface = EGL14.eglCreatePbufferSurface(display, config, pbufferAttributes, /* offset= */ 0); - if (surface == null) { - throw new GlException("eglCreatePbufferSurface failed"); - } + GlUtil.checkGlException(surface != null, "eglCreatePbufferSurface failed"); } boolean eglMadeCurrent = EGL14.eglMakeCurrent(display, /* draw= */ surface, /* read= */ surface, context); - if (!eglMadeCurrent) { - throw new GlException("eglMakeCurrent failed"); - } + GlUtil.checkGlException(eglMadeCurrent, "eglMakeCurrent failed"); return surface; } - private static void generateTextureIds(int[] textureIdHolder) { + private static void generateTextureIds(int[] textureIdHolder) throws GlUtil.GlException { GLES20.glGenTextures(/* n= */ 1, textureIdHolder, /* offset= */ 0); GlUtil.checkGlError(); } diff --git a/library/common/src/main/java/com/google/android/exoplayer2/util/GlProgram.java b/library/common/src/main/java/com/google/android/exoplayer2/util/GlProgram.java index 8f48b9cc60..58e2d325f2 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/util/GlProgram.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/util/GlProgram.java @@ -53,7 +53,7 @@ public final class GlProgram { * @throws IOException When failing to read shader files. */ public GlProgram(Context context, String vertexShaderFilePath, String fragmentShaderFilePath) - throws IOException { + throws IOException, GlUtil.GlException { this( GlUtil.loadAsset(context, vertexShaderFilePath), GlUtil.loadAsset(context, fragmentShaderFilePath)); @@ -68,7 +68,7 @@ public final class GlProgram { * @param vertexShaderGlsl The vertex shader program. * @param fragmentShaderGlsl The fragment shader program. */ - public GlProgram(String vertexShaderGlsl, String fragmentShaderGlsl) { + public GlProgram(String vertexShaderGlsl, String fragmentShaderGlsl) throws GlUtil.GlException { programId = GLES20.glCreateProgram(); GlUtil.checkGlError(); @@ -80,10 +80,9 @@ public final class GlProgram { GLES20.glLinkProgram(programId); int[] linkStatus = new int[] {GLES20.GL_FALSE}; GLES20.glGetProgramiv(programId, GLES20.GL_LINK_STATUS, linkStatus, /* offset= */ 0); - if (linkStatus[0] != GLES20.GL_TRUE) { - GlUtil.throwGlException( - "Unable to link shader program: \n" + GLES20.glGetProgramInfoLog(programId)); - } + GlUtil.checkGlException( + linkStatus[0] == GLES20.GL_TRUE, + "Unable to link shader program: \n" + GLES20.glGetProgramInfoLog(programId)); GLES20.glUseProgram(programId); attributeByName = new HashMap<>(); int[] attributeCount = new int[1]; @@ -106,16 +105,15 @@ public final class GlProgram { GlUtil.checkGlError(); } - private static void addShader(int programId, int type, String glsl) { + private static void addShader(int programId, int type, String glsl) throws GlUtil.GlException { int shader = GLES20.glCreateShader(type); GLES20.glShaderSource(shader, glsl); GLES20.glCompileShader(shader); int[] result = new int[] {GLES20.GL_FALSE}; GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, result, /* offset= */ 0); - if (result[0] != GLES20.GL_TRUE) { - GlUtil.throwGlException(GLES20.glGetShaderInfoLog(shader) + ", source: " + glsl); - } + GlUtil.checkGlException( + result[0] == GLES20.GL_TRUE, GLES20.glGetShaderInfoLog(shader) + ", source: " + glsl); GLES20.glAttachShader(programId, shader); GLES20.glDeleteShader(shader); @@ -145,13 +143,13 @@ public final class GlProgram { * *

Call this in the rendering loop to switch between different programs. */ - public void use() { + public void use() throws GlUtil.GlException { GLES20.glUseProgram(programId); GlUtil.checkGlError(); } /** Deletes the program. Deleted programs cannot be used again. */ - public void delete() { + public void delete() throws GlUtil.GlException { GLES20.glDeleteProgram(programId); GlUtil.checkGlError(); } @@ -160,7 +158,7 @@ public final class GlProgram { * Returns the location of an {@link Attribute}, which has been enabled as a vertex attribute * array. */ - public int getAttributeArrayLocationAndEnable(String attributeName) { + public int getAttributeArrayLocationAndEnable(String attributeName) throws GlUtil.GlException { int location = getAttributeLocation(attributeName); GLES20.glEnableVertexAttribArray(location); GlUtil.checkGlError(); @@ -195,7 +193,7 @@ public final class GlProgram { } /** Binds all attributes and uniforms in the program. */ - public void bindAttributesAndUniforms() { + public void bindAttributesAndUniforms() throws GlUtil.GlException { for (Attribute attribute : attributes) { attribute.bind(); } @@ -276,7 +274,7 @@ public final class GlProgram { * *

Should be called before each drawing call. */ - public void bind() { + public void bind() throws GlUtil.GlException { Buffer buffer = checkNotNull(this.buffer, "call setBuffer before bind"); GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, /* buffer= */ 0); GLES20.glVertexAttribPointer( @@ -362,7 +360,7 @@ public final class GlProgram { * *

Should be called before each drawing call. */ - public void bind() { + public void bind() throws GlUtil.GlException { switch (type) { case GLES20.GL_FLOAT: GLES20.glUniform1fv(location, /* count= */ 1, value, /* offset= */ 0); diff --git a/library/common/src/main/java/com/google/android/exoplayer2/util/GlUtil.java b/library/common/src/main/java/com/google/android/exoplayer2/util/GlUtil.java index af26525560..83bd545117 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/util/GlUtil.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/util/GlUtil.java @@ -42,19 +42,14 @@ import javax.microedition.khronos.egl.EGL10; @SuppressWarnings("InlinedApi") // GLES constants are used safely based on the API version. public final class GlUtil { - /** Thrown when an OpenGL error occurs and {@link #glAssertionsEnabled} is {@code true}. */ - public static final class GlException extends RuntimeException { + /** Thrown when an OpenGL error occurs. */ + public static final class GlException extends Exception { /** Creates an instance with the specified error message. */ public GlException(String message) { super(message); } } - // TODO(b/231937416): Consider removing this flag, enabling assertions by default, and making - // GlException checked. - /** Whether to throw a {@link GlException} in case of an OpenGL error. */ - public static boolean glAssertionsEnabled = false; - /** Number of elements in a 3d homogeneous coordinate vector describing a vertex. */ public static final int HOMOGENEOUS_COORDINATE_VECTOR_SIZE = 4; @@ -183,13 +178,13 @@ public final class GlUtil { /** Returns an initialized default {@link EGLDisplay}. */ @RequiresApi(17) - public static EGLDisplay createEglDisplay() { + public static EGLDisplay createEglDisplay() throws GlException { return Api17.createEglDisplay(); } /** Returns a new {@link EGLContext} for the specified {@link EGLDisplay}. */ @RequiresApi(17) - public static EGLContext createEglContext(EGLDisplay eglDisplay) { + public static EGLContext createEglContext(EGLDisplay eglDisplay) throws GlException { return Api17.createEglContext(eglDisplay, /* version= */ 2, EGL_CONFIG_ATTRIBUTES_RGBA_8888); } @@ -198,7 +193,8 @@ public final class GlUtil { * RGBA 1010102 config. */ @RequiresApi(17) - public static EGLContext createEglContextEs3Rgba1010102(EGLDisplay eglDisplay) { + public static EGLContext createEglContextEs3Rgba1010102(EGLDisplay eglDisplay) + throws GlException { return Api17.createEglContext(eglDisplay, /* version= */ 3, EGL_CONFIG_ATTRIBUTES_RGBA_1010102); } @@ -209,7 +205,7 @@ public final class GlUtil { * @param surface The surface to wrap; must be a surface, surface texture or surface holder. */ @RequiresApi(17) - public static EGLSurface getEglSurface(EGLDisplay eglDisplay, Object surface) { + public static EGLSurface getEglSurface(EGLDisplay eglDisplay, Object surface) throws GlException { return Api17.getEglSurface( eglDisplay, surface, EGL_CONFIG_ATTRIBUTES_RGBA_8888, EGL_WINDOW_SURFACE_ATTRIBUTES_NONE); } @@ -222,7 +218,8 @@ public final class GlUtil { * @param surface The surface to wrap; must be a surface, surface texture or surface holder. */ @RequiresApi(17) - public static EGLSurface getEglSurfaceBt2020Pq(EGLDisplay eglDisplay, Object surface) { + public static EGLSurface getEglSurfaceBt2020Pq(EGLDisplay eglDisplay, Object surface) + throws GlException { return Api17.getEglSurface( eglDisplay, surface, @@ -238,7 +235,8 @@ public final class GlUtil { * @param height The height of the pixel buffer. */ @RequiresApi(17) - private static EGLSurface createPbufferSurface(EGLDisplay eglDisplay, int width, int height) { + private static EGLSurface createPbufferSurface(EGLDisplay eglDisplay, int width, int height) + throws GlException { int[] pbufferAttributes = new int[] { EGL14.EGL_WIDTH, width, @@ -257,7 +255,7 @@ public final class GlUtil { * @return {@link EGL14#EGL_NO_SURFACE} if supported and a 1x1 pixel buffer surface otherwise. */ @RequiresApi(17) - public static EGLSurface createPlaceholderEglSurface(EGLDisplay eglDisplay) { + public static EGLSurface createPlaceholderEglSurface(EGLDisplay eglDisplay) throws GlException { return isSurfacelessContextExtensionSupported() ? EGL14.EGL_NO_SURFACE : createPbufferSurface(eglDisplay, /* width= */ 1, /* height= */ 1); @@ -270,7 +268,8 @@ public final class GlUtil { * @param eglDisplay The {@link EGLDisplay} to attach the surface to. */ @RequiresApi(17) - public static void focusPlaceholderEglSurface(EGLContext eglContext, EGLDisplay eglDisplay) { + public static void focusPlaceholderEglSurface(EGLContext eglContext, EGLDisplay eglDisplay) + throws GlException { EGLSurface eglSurface = createPbufferSurface(eglDisplay, /* width= */ 1, /* height= */ 1); focusEglSurface(eglDisplay, eglContext, eglSurface, /* width= */ 1, /* height= */ 1); } @@ -284,7 +283,7 @@ public final class GlUtil { */ @RequiresApi(17) public static void focusPlaceholderEglSurfaceBt2020Pq( - EGLContext eglContext, EGLDisplay eglDisplay) { + EGLContext eglContext, EGLDisplay eglDisplay) throws GlException { int[] pbufferAttributes = new int[] { EGL14.EGL_WIDTH, @@ -302,10 +301,10 @@ public final class GlUtil { } /** - * If there is an OpenGl error, logs the error and if {@link #glAssertionsEnabled} is true throws - * a {@link GlException}. + * Logs all OpenGL errors that occurred since this method was last called and throws a {@link + * GlException} for the last error. */ - public static void checkGlError() { + public static void checkGlError() throws GlException { int lastError = GLES20.GL_NO_ERROR; int error; while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) { @@ -313,7 +312,7 @@ public final class GlUtil { lastError = error; } if (lastError != GLES20.GL_NO_ERROR) { - throwGlException("glError: " + gluErrorString(lastError)); + throw new GlException("glError: " + gluErrorString(lastError)); } } @@ -324,7 +323,7 @@ public final class GlUtil { * @param height The height for a texture. * @throws GlException If the texture width or height is invalid. */ - public static void assertValidTextureSize(int width, int height) { + public static void assertValidTextureSize(int width, int height) throws GlException { // TODO(b/201293185): Consider handling adjustments for sizes > GL_MAX_TEXTURE_SIZE // (ex. downscaling appropriately) in a texture processor instead of asserting incorrect // values. @@ -335,15 +334,16 @@ public final class GlUtil { GLES20.glGetIntegerv(GLES20.GL_MAX_TEXTURE_SIZE, maxTextureSizeBuffer, 0); int maxTextureSize = maxTextureSizeBuffer[0]; if (width < 0 || height < 0) { - throwGlException("width or height is less than 0"); + throw new GlException("width or height is less than 0"); } if (width > maxTextureSize || height > maxTextureSize) { - throwGlException("width or height is greater than GL_MAX_TEXTURE_SIZE " + maxTextureSize); + throw new GlException( + "width or height is greater than GL_MAX_TEXTURE_SIZE " + maxTextureSize); } } /** Fills the pixels in the current output render target with (r=0, g=0, b=0, a=0). */ - public static void clearOutputFrame() { + public static void clearOutputFrame() throws GlException { GLES20.glClearColor(/* red= */ 0, /* green= */ 0, /* blue= */ 0, /* alpha= */ 0); GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); GlUtil.checkGlError(); @@ -355,7 +355,8 @@ public final class GlUtil { */ @RequiresApi(17) public static void focusEglSurface( - EGLDisplay eglDisplay, EGLContext eglContext, EGLSurface eglSurface, int width, int height) { + EGLDisplay eglDisplay, EGLContext eglContext, EGLSurface eglSurface, int width, int height) + throws GlException { Api17.focusRenderTarget( eglDisplay, eglContext, eglSurface, /* framebuffer= */ 0, width, height); } @@ -371,7 +372,8 @@ public final class GlUtil { EGLSurface eglSurface, int framebuffer, int width, - int height) { + int height) + throws GlException { Api17.focusRenderTarget(eglDisplay, eglContext, eglSurface, framebuffer, width, height); } @@ -387,7 +389,8 @@ public final class GlUtil { * @param height The viewport height, in pixels. */ @RequiresApi(17) - public static void focusFramebufferUsingCurrentContext(int framebuffer, int width, int height) { + public static void focusFramebufferUsingCurrentContext(int framebuffer, int width, int height) + throws GlException { Api17.focusFramebufferUsingCurrentContext(framebuffer, width, height); } @@ -396,7 +399,7 @@ public final class GlUtil { * * @param textureId The ID of the texture to delete. */ - public static void deleteTexture(int textureId) { + public static void deleteTexture(int textureId) throws GlException { GLES20.glDeleteTextures(/* n= */ 1, new int[] {textureId}, /* offset= */ 0); checkGlError(); } @@ -407,7 +410,7 @@ public final class GlUtil { */ @RequiresApi(17) public static void destroyEglContext( - @Nullable EGLDisplay eglDisplay, @Nullable EGLContext eglContext) { + @Nullable EGLDisplay eglDisplay, @Nullable EGLContext eglContext) throws GlException { Api17.destroyEglContext(eglDisplay, eglContext); } @@ -452,7 +455,7 @@ public final class GlUtil { * Creates a GL_TEXTURE_EXTERNAL_OES with default configuration of GL_LINEAR filtering and * GL_CLAMP_TO_EDGE wrapping. */ - public static int createExternalTexture() { + public static int createExternalTexture() throws GlException { int texId = generateTexture(); bindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, texId); return texId; @@ -464,7 +467,7 @@ public final class GlUtil { * @param width of the new texture in pixels * @param height of the new texture in pixels */ - public static int createTexture(int width, int height) { + public static int createTexture(int width, int height) throws GlException { assertValidTextureSize(width, height); int texId = generateTexture(); bindTexture(GLES20.GL_TEXTURE_2D, texId); @@ -484,8 +487,8 @@ public final class GlUtil { } /** Returns a new GL texture identifier. */ - private static int generateTexture() { - checkEglException( + private static int generateTexture() throws GlException { + checkGlException( !Util.areEqual(EGL14.eglGetCurrentContext(), EGL14.EGL_NO_CONTEXT), "No current context"); int[] texId = new int[1]; @@ -503,7 +506,7 @@ public final class GlUtil { * GLES20#GL_TEXTURE_2D} for a two-dimensional texture or {@link * GLES11Ext#GL_TEXTURE_EXTERNAL_OES} for an external texture. */ - public static void bindTexture(int textureTarget, int texId) { + public static void bindTexture(int textureTarget, int texId) throws GlException { GLES20.glBindTexture(textureTarget, texId); checkGlError(); GLES20.glTexParameteri(textureTarget, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR); @@ -521,8 +524,8 @@ public final class GlUtil { * * @param texId The identifier of the texture to attach to the framebuffer. */ - public static int createFboForTexture(int texId) { - checkEglException( + public static int createFboForTexture(int texId) throws GlException { + checkGlException( !Util.areEqual(EGL14.eglGetCurrentContext(), EGL14.EGL_NO_CONTEXT), "No current context"); int[] fboId = new int[1]; @@ -536,23 +539,19 @@ public final class GlUtil { return fboId[0]; } - /* package */ static void throwGlException(String errorMsg) { - if (glAssertionsEnabled) { - throw new GlException(errorMsg); - } else { - Log.e(TAG, errorMsg); - } - } - - private static void checkEglException(boolean expression, String errorMessage) { + /** + * Throws a {@link GlException} with the given message if {@code expression} evaluates to {@code + * false}. + */ + public static void checkGlException(boolean expression, String errorMessage) throws GlException { if (!expression) { - throwGlException(errorMessage); + throw new GlException(errorMessage); } } - private static void checkEglException(String errorMessage) { + private static void checkEglException(String errorMessage) throws GlException { int error = EGL14.eglGetError(); - checkEglException(error == EGL14.EGL_SUCCESS, errorMessage + ", error code: " + error); + checkGlException(error == EGL14.EGL_SUCCESS, errorMessage + ", error code: " + error); } @RequiresApi(17) @@ -560,24 +559,24 @@ public final class GlUtil { private Api17() {} @DoNotInline - public static EGLDisplay createEglDisplay() { + public static EGLDisplay createEglDisplay() throws GlException { EGLDisplay eglDisplay = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY); - checkEglException(!eglDisplay.equals(EGL14.EGL_NO_DISPLAY), "No EGL display."); - if (!EGL14.eglInitialize( - eglDisplay, - /* unusedMajor */ new int[1], - /* majorOffset= */ 0, - /* unusedMinor */ new int[1], - /* minorOffset= */ 0)) { - throwGlException("Error in eglInitialize."); - } + checkGlException(!eglDisplay.equals(EGL14.EGL_NO_DISPLAY), "No EGL display."); + checkGlException( + EGL14.eglInitialize( + eglDisplay, + /* unusedMajor */ new int[1], + /* majorOffset= */ 0, + /* unusedMinor */ new int[1], + /* minorOffset= */ 0), + "Error in eglInitialize."); checkGlError(); return eglDisplay; } @DoNotInline public static EGLContext createEglContext( - EGLDisplay eglDisplay, int version, int[] configAttributes) { + EGLDisplay eglDisplay, int version, int[] configAttributes) throws GlException { int[] contextAttributes = {EGL14.EGL_CONTEXT_CLIENT_VERSION, version, EGL14.EGL_NONE}; EGLContext eglContext = EGL14.eglCreateContext( @@ -588,7 +587,7 @@ public final class GlUtil { /* offset= */ 0); if (eglContext == null) { EGL14.eglTerminate(eglDisplay); - throwGlException( + throw new GlException( "eglCreateContext() failed to create a valid context. The device may not support EGL" + " version " + version); @@ -602,7 +601,8 @@ public final class GlUtil { EGLDisplay eglDisplay, Object surface, int[] configAttributes, - int[] windowSurfaceAttributes) { + int[] windowSurfaceAttributes) + throws GlException { EGLSurface eglSurface = EGL14.eglCreateWindowSurface( eglDisplay, @@ -616,7 +616,7 @@ public final class GlUtil { @DoNotInline public static EGLSurface createEglPbufferSurface( - EGLDisplay eglDisplay, int[] configAttributes, int[] pbufferAttributes) { + EGLDisplay eglDisplay, int[] configAttributes, int[] pbufferAttributes) throws GlException { EGLSurface eglSurface = EGL14.eglCreatePbufferSurface( eglDisplay, @@ -634,15 +634,17 @@ public final class GlUtil { EGLSurface eglSurface, int framebuffer, int width, - int height) { + int height) + throws GlException { EGL14.eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext); checkEglException("Error making context current"); focusFramebufferUsingCurrentContext(framebuffer, width, height); } @DoNotInline - public static void focusFramebufferUsingCurrentContext(int framebuffer, int width, int height) { - checkEglException( + public static void focusFramebufferUsingCurrentContext(int framebuffer, int width, int height) + throws GlException { + checkGlException( !Util.areEqual(EGL14.eglGetCurrentContext(), EGL14.EGL_NO_CONTEXT), "No current context"); int[] boundFramebuffer = new int[1]; @@ -657,7 +659,7 @@ public final class GlUtil { @DoNotInline public static void destroyEglContext( - @Nullable EGLDisplay eglDisplay, @Nullable EGLContext eglContext) { + @Nullable EGLDisplay eglDisplay, @Nullable EGLContext eglContext) throws GlException { if (eglDisplay == null) { return; } @@ -675,7 +677,8 @@ public final class GlUtil { } @DoNotInline - private static EGLConfig getEglConfig(EGLDisplay eglDisplay, int[] attributes) { + private static EGLConfig getEglConfig(EGLDisplay eglDisplay, int[] attributes) + throws GlException { EGLConfig[] eglConfigs = new EGLConfig[1]; if (!EGL14.eglChooseConfig( eglDisplay, @@ -686,7 +689,7 @@ public final class GlUtil { /* config_size= */ 1, /* unusedNumConfig */ new int[1], /* num_configOffset= */ 0)) { - throwGlException("eglChooseConfig failed."); + throw new GlException("eglChooseConfig failed."); } return eglConfigs[0]; } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/video/PlaceholderSurface.java b/library/core/src/main/java/com/google/android/exoplayer2/video/PlaceholderSurface.java index f151e1d35a..885e34b4d7 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/video/PlaceholderSurface.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/video/PlaceholderSurface.java @@ -177,6 +177,9 @@ public final class PlaceholderSurface extends Surface { } catch (RuntimeException e) { Log.e(TAG, "Failed to initialize placeholder surface", e); initException = e; + } catch (GlUtil.GlException e) { + Log.e(TAG, "Failed to initialize placeholder surface", e); + initException = new IllegalStateException(e); } catch (Error e) { Log.e(TAG, "Failed to initialize placeholder surface", e); initError = e; @@ -200,7 +203,7 @@ public final class PlaceholderSurface extends Surface { } } - private void initInternal(@SecureMode int secureMode) { + private void initInternal(@SecureMode int secureMode) throws GlUtil.GlException { Assertions.checkNotNull(eglSurfaceTexture); eglSurfaceTexture.init(secureMode); this.surface = diff --git a/library/core/src/main/java/com/google/android/exoplayer2/video/VideoDecoderGLSurfaceView.java b/library/core/src/main/java/com/google/android/exoplayer2/video/VideoDecoderGLSurfaceView.java index 0f99388022..2dc3592e3f 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/video/VideoDecoderGLSurfaceView.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/video/VideoDecoderGLSurfaceView.java @@ -21,6 +21,7 @@ import android.content.Context; import android.opengl.GLES20; import android.opengl.GLSurfaceView; import android.util.AttributeSet; +import android.util.Log; import androidx.annotation.Nullable; import com.google.android.exoplayer2.decoder.VideoDecoderOutputBuffer; import com.google.android.exoplayer2.util.Assertions; @@ -46,6 +47,8 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull; public final class VideoDecoderGLSurfaceView extends GLSurfaceView implements VideoDecoderOutputBufferRenderer { + private static final String TAG = "VideoDecoderGLSV"; + private final Renderer renderer; /** @@ -170,22 +173,26 @@ public final class VideoDecoderGLSurfaceView extends GLSurfaceView @Override public void onSurfaceCreated(GL10 unused, EGLConfig config) { - program = new GlProgram(VERTEX_SHADER, FRAGMENT_SHADER); - int posLocation = program.getAttributeArrayLocationAndEnable("in_pos"); - GLES20.glVertexAttribPointer( - posLocation, - 2, - GLES20.GL_FLOAT, - /* normalized= */ false, - /* stride= */ 0, - TEXTURE_VERTICES); - texLocations[0] = program.getAttributeArrayLocationAndEnable("in_tc_y"); - texLocations[1] = program.getAttributeArrayLocationAndEnable("in_tc_u"); - texLocations[2] = program.getAttributeArrayLocationAndEnable("in_tc_v"); - colorMatrixLocation = program.getUniformLocation("mColorConversion"); - GlUtil.checkGlError(); - setupTextures(); - GlUtil.checkGlError(); + try { + program = new GlProgram(VERTEX_SHADER, FRAGMENT_SHADER); + int posLocation = program.getAttributeArrayLocationAndEnable("in_pos"); + GLES20.glVertexAttribPointer( + posLocation, + 2, + GLES20.GL_FLOAT, + /* normalized= */ false, + /* stride= */ 0, + TEXTURE_VERTICES); + texLocations[0] = program.getAttributeArrayLocationAndEnable("in_tc_y"); + texLocations[1] = program.getAttributeArrayLocationAndEnable("in_tc_u"); + texLocations[2] = program.getAttributeArrayLocationAndEnable("in_tc_v"); + colorMatrixLocation = program.getUniformLocation("mColorConversion"); + GlUtil.checkGlError(); + setupTextures(); + GlUtil.checkGlError(); + } catch (GlUtil.GlException e) { + Log.e(TAG, "Failed to set up the textures and program", e); + } } @Override @@ -282,7 +289,11 @@ public final class VideoDecoderGLSurfaceView extends GLSurfaceView GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, /* first= */ 0, /* count= */ 4); - GlUtil.checkGlError(); + try { + GlUtil.checkGlError(); + } catch (GlUtil.GlException e) { + Log.e(TAG, "Failed to draw a frame", e); + } } public void setOutputBuffer(VideoDecoderOutputBuffer outputBuffer) { @@ -298,13 +309,17 @@ public final class VideoDecoderGLSurfaceView extends GLSurfaceView @RequiresNonNull("program") private void setupTextures() { - GLES20.glGenTextures(/* n= */ 3, yuvTextures, /* offset= */ 0); - for (int i = 0; i < 3; i++) { - GLES20.glUniform1i(program.getUniformLocation(TEXTURE_UNIFORMS[i]), i); - GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + i); - GlUtil.bindTexture(GLES20.GL_TEXTURE_2D, yuvTextures[i]); + try { + GLES20.glGenTextures(/* n= */ 3, yuvTextures, /* offset= */ 0); + for (int i = 0; i < 3; i++) { + GLES20.glUniform1i(program.getUniformLocation(TEXTURE_UNIFORMS[i]), i); + GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + i); + GlUtil.bindTexture(GLES20.GL_TEXTURE_2D, yuvTextures[i]); + } + GlUtil.checkGlError(); + } catch (GlUtil.GlException e) { + Log.e(TAG, "Failed to set up the textures", e); } - GlUtil.checkGlError(); } } } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/video/spherical/ProjectionRenderer.java b/library/core/src/main/java/com/google/android/exoplayer2/video/spherical/ProjectionRenderer.java index e647cc4bc8..c7ba8c4b4c 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/video/spherical/ProjectionRenderer.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/video/spherical/ProjectionRenderer.java @@ -19,6 +19,7 @@ import static com.google.android.exoplayer2.util.GlUtil.checkGlError; import android.opengl.GLES11Ext; import android.opengl.GLES20; +import android.util.Log; import androidx.annotation.Nullable; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.util.GlProgram; @@ -45,6 +46,8 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; && rightMesh.getSubMesh(0).textureId == Projection.SubMesh.VIDEO_TEXTURE_ID; } + private static final String TAG = "ProjectionRenderer"; + // Basic vertex & fragment shaders to render a mesh with 3D position & 2D texture data. private static final String VERTEX_SHADER = "uniform mat4 uMvpMatrix;\n" @@ -115,12 +118,16 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; /** Initializes of the GL components. */ public void init() { - program = new GlProgram(VERTEX_SHADER, FRAGMENT_SHADER); - mvpMatrixHandle = program.getUniformLocation("uMvpMatrix"); - uTexMatrixHandle = program.getUniformLocation("uTexMatrix"); - positionHandle = program.getAttributeArrayLocationAndEnable("aPosition"); - texCoordsHandle = program.getAttributeArrayLocationAndEnable("aTexCoords"); - textureHandle = program.getUniformLocation("uTexture"); + try { + program = new GlProgram(VERTEX_SHADER, FRAGMENT_SHADER); + mvpMatrixHandle = program.getUniformLocation("uMvpMatrix"); + uTexMatrixHandle = program.getUniformLocation("uTexMatrix"); + positionHandle = program.getAttributeArrayLocationAndEnable("aPosition"); + texCoordsHandle = program.getAttributeArrayLocationAndEnable("aTexCoords"); + textureHandle = program.getUniformLocation("uTexture"); + } catch (GlUtil.GlException e) { + Log.e(TAG, "Failed to initialize the program", e); + } } /** @@ -154,7 +161,11 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; GLES20.glActiveTexture(GLES20.GL_TEXTURE0); GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textureId); GLES20.glUniform1i(textureHandle, 0); - checkGlError(); + try { + checkGlError(); + } catch (GlUtil.GlException e) { + Log.e(TAG, "Failed to bind uniforms", e); + } // Load position data. GLES20.glVertexAttribPointer( @@ -164,7 +175,11 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; false, Projection.POSITION_COORDS_PER_VERTEX * C.BYTES_PER_FLOAT, meshData.vertexBuffer); - checkGlError(); + try { + checkGlError(); + } catch (GlUtil.GlException e) { + Log.e(TAG, "Failed to load position data", e); + } // Load texture data. GLES20.glVertexAttribPointer( @@ -174,17 +189,29 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; false, Projection.TEXTURE_COORDS_PER_VERTEX * C.BYTES_PER_FLOAT, meshData.textureBuffer); - checkGlError(); + try { + checkGlError(); + } catch (GlUtil.GlException e) { + Log.e(TAG, "Failed to load texture data", e); + } // Render. GLES20.glDrawArrays(meshData.drawMode, /* first= */ 0, meshData.vertexCount); - checkGlError(); + try { + checkGlError(); + } catch (GlUtil.GlException e) { + Log.e(TAG, "Failed to render", e); + } } /** Cleans up GL resources. */ public void shutdown() { if (program != null) { - program.delete(); + try { + program.delete(); + } catch (GlUtil.GlException e) { + Log.e(TAG, "Failed to delete the shader program", e); + } } } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/video/spherical/SceneRenderer.java b/library/core/src/main/java/com/google/android/exoplayer2/video/spherical/SceneRenderer.java index b84d17176c..d9dfc3ad88 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/video/spherical/SceneRenderer.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/video/spherical/SceneRenderer.java @@ -26,6 +26,7 @@ import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.GlUtil; +import com.google.android.exoplayer2.util.Log; import com.google.android.exoplayer2.util.TimedValueQueue; import com.google.android.exoplayer2.video.VideoFrameMetadataListener; import java.util.Arrays; @@ -36,6 +37,8 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; /* package */ final class SceneRenderer implements VideoFrameMetadataListener, CameraMotionListener { + private static final String TAG = "SceneRenderer"; + private final AtomicBoolean frameAvailable; private final AtomicBoolean resetRotationAtNextFrame; private final ProjectionRenderer projectionRenderer; @@ -83,14 +86,19 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; /** Initializes the renderer. */ public SurfaceTexture init() { - // Set the background frame color. This is only visible if the display mesh isn't a full sphere. - GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f); - checkGlError(); + try { + // Set the background frame color. This is only visible if the display mesh isn't a full + // sphere. + GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f); + checkGlError(); - projectionRenderer.init(); - checkGlError(); + projectionRenderer.init(); + checkGlError(); - textureId = GlUtil.createExternalTexture(); + textureId = GlUtil.createExternalTexture(); + } catch (GlUtil.GlException e) { + Log.e(TAG, "Failed to initialize the renderer", e); + } surfaceTexture = new SurfaceTexture(textureId); surfaceTexture.setOnFrameAvailableListener(surfaceTexture -> frameAvailable.set(true)); return surfaceTexture; @@ -107,11 +115,19 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; // glClear isn't strictly necessary when rendering fully spherical panoramas, but it can improve // performance on tiled renderers by causing the GPU to discard previous data. GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); - checkGlError(); + try { + checkGlError(); + } catch (GlUtil.GlException e) { + Log.e(TAG, "Failed to draw a frame", e); + } if (frameAvailable.compareAndSet(true, false)) { Assertions.checkNotNull(surfaceTexture).updateTexImage(); - checkGlError(); + try { + checkGlError(); + } catch (GlUtil.GlException e) { + Log.e(TAG, "Failed to draw a frame", e); + } if (resetRotationAtNextFrame.compareAndSet(true, false)) { Matrix.setIdentityM(rotationMatrix, 0); } diff --git a/library/transformer/src/androidTest/java/com/google/android/exoplayer2/transformer/BitmapTestUtil.java b/library/transformer/src/androidTest/java/com/google/android/exoplayer2/transformer/BitmapTestUtil.java index 8ee749396b..f91967a980 100644 --- a/library/transformer/src/androidTest/java/com/google/android/exoplayer2/transformer/BitmapTestUtil.java +++ b/library/transformer/src/androidTest/java/com/google/android/exoplayer2/transformer/BitmapTestUtil.java @@ -186,7 +186,8 @@ public class BitmapTestUtil { * @param height The height of the pixel rectangle to read. * @return A {@link Bitmap} with the framebuffer's values. */ - public static Bitmap createArgb8888BitmapFromCurrentGlFramebuffer(int width, int height) { + public static Bitmap createArgb8888BitmapFromCurrentGlFramebuffer(int width, int height) + throws GlUtil.GlException { ByteBuffer rgba8888Buffer = ByteBuffer.allocateDirect(width * height * 4); GLES20.glReadPixels( 0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, rgba8888Buffer); @@ -206,7 +207,7 @@ public class BitmapTestUtil { * @param bitmap A {@link Bitmap}. * @return The identifier of the newly created texture. */ - public static int createGlTextureFromBitmap(Bitmap bitmap) { + public static int createGlTextureFromBitmap(Bitmap bitmap) throws GlUtil.GlException { int texId = GlUtil.createTexture(bitmap.getWidth(), bitmap.getHeight()); // Put the flipped bitmap in the OpenGL texture as the bitmap's positive y-axis points down // while OpenGL's positive y-axis points up. diff --git a/library/transformer/src/androidTest/java/com/google/android/exoplayer2/transformer/CropPixelTest.java b/library/transformer/src/androidTest/java/com/google/android/exoplayer2/transformer/CropPixelTest.java index a7c7db9b0a..065e6ae8bd 100644 --- a/library/transformer/src/androidTest/java/com/google/android/exoplayer2/transformer/CropPixelTest.java +++ b/library/transformer/src/androidTest/java/com/google/android/exoplayer2/transformer/CropPixelTest.java @@ -52,13 +52,9 @@ public final class CropPixelTest { public static final String CROP_LARGER_PNG_ASSET_PATH = "media/bitmap/sample_mp4_first_frame/crop_larger.png"; - static { - GlUtil.glAssertionsEnabled = true; - } - - private final Context context = getApplicationContext(); - private final EGLDisplay eglDisplay = GlUtil.createEglDisplay(); - private final EGLContext eglContext = GlUtil.createEglContext(eglDisplay); + private Context context = getApplicationContext(); + private @MonotonicNonNull EGLDisplay eglDisplay; + private @MonotonicNonNull EGLContext eglContext; private @MonotonicNonNull SingleFrameGlTextureProcessor cropTextureProcessor; private @MonotonicNonNull EGLSurface placeholderEglSurface; private int inputTexId; @@ -67,7 +63,9 @@ public final class CropPixelTest { private int inputHeight; @Before - public void createTextures() throws IOException { + public void createGlObjects() throws IOException, GlUtil.GlException { + eglDisplay = GlUtil.createEglDisplay(); + eglContext = GlUtil.createEglContext(eglDisplay); Bitmap inputBitmap = BitmapTestUtil.readBitmap(ORIGINAL_PNG_ASSET_PATH); inputWidth = inputBitmap.getWidth(); inputHeight = inputBitmap.getHeight(); @@ -77,11 +75,13 @@ public final class CropPixelTest { } @After - public void release() { + public void release() throws GlUtil.GlException, FrameProcessingException { if (cropTextureProcessor != null) { cropTextureProcessor.release(); } - GlUtil.destroyEglContext(eglDisplay, eglContext); + if (eglContext != null && eglDisplay != null) { + GlUtil.destroyEglContext(eglDisplay, eglContext); + } } @Test @@ -156,12 +156,12 @@ public final class CropPixelTest { assertThat(averagePixelAbsoluteDifference).isAtMost(MAXIMUM_AVERAGE_PIXEL_ABSOLUTE_DIFFERENCE); } - private void setupOutputTexture(int outputWidth, int outputHeight) { + private void setupOutputTexture(int outputWidth, int outputHeight) throws GlUtil.GlException { outputTexId = GlUtil.createTexture(outputWidth, outputHeight); int frameBuffer = GlUtil.createFboForTexture(outputTexId); GlUtil.focusFramebuffer( - eglDisplay, - eglContext, + checkNotNull(eglDisplay), + checkNotNull(eglContext), checkNotNull(placeholderEglSurface), frameBuffer, outputWidth, diff --git a/library/transformer/src/androidTest/java/com/google/android/exoplayer2/transformer/MatrixTransformationProcessorPixelTest.java b/library/transformer/src/androidTest/java/com/google/android/exoplayer2/transformer/MatrixTransformationProcessorPixelTest.java index 3bde752f28..95136c4b17 100644 --- a/library/transformer/src/androidTest/java/com/google/android/exoplayer2/transformer/MatrixTransformationProcessorPixelTest.java +++ b/library/transformer/src/androidTest/java/com/google/android/exoplayer2/transformer/MatrixTransformationProcessorPixelTest.java @@ -53,13 +53,9 @@ public final class MatrixTransformationProcessorPixelTest { public static final String ROTATE_90_PNG_ASSET_PATH = "media/bitmap/sample_mp4_first_frame/rotate90.png"; - static { - GlUtil.glAssertionsEnabled = true; - } - private final Context context = getApplicationContext(); - private final EGLDisplay eglDisplay = GlUtil.createEglDisplay(); - private final EGLContext eglContext = GlUtil.createEglContext(eglDisplay); + private @MonotonicNonNull EGLDisplay eglDisplay; + private @MonotonicNonNull EGLContext eglContext; private @MonotonicNonNull SingleFrameGlTextureProcessor matrixTransformationFrameProcessor; private int inputTexId; private int outputTexId; @@ -67,7 +63,9 @@ public final class MatrixTransformationProcessorPixelTest { private int height; @Before - public void createTextures() throws IOException { + public void createGlObjects() throws IOException, GlUtil.GlException { + eglDisplay = GlUtil.createEglDisplay(); + eglContext = GlUtil.createEglContext(eglDisplay); Bitmap inputBitmap = BitmapTestUtil.readBitmap(ORIGINAL_PNG_ASSET_PATH); width = inputBitmap.getWidth(); height = inputBitmap.getHeight(); @@ -81,11 +79,13 @@ public final class MatrixTransformationProcessorPixelTest { } @After - public void release() { + public void release() throws GlUtil.GlException, FrameProcessingException { if (matrixTransformationFrameProcessor != null) { matrixTransformationFrameProcessor.release(); } - GlUtil.destroyEglContext(eglDisplay, eglContext); + if (eglContext != null && eglDisplay != null) { + GlUtil.destroyEglContext(eglDisplay, eglContext); + } } @Test diff --git a/library/transformer/src/androidTest/java/com/google/android/exoplayer2/transformer/PresentationPixelTest.java b/library/transformer/src/androidTest/java/com/google/android/exoplayer2/transformer/PresentationPixelTest.java index e04820ed9c..f631e52594 100644 --- a/library/transformer/src/androidTest/java/com/google/android/exoplayer2/transformer/PresentationPixelTest.java +++ b/library/transformer/src/androidTest/java/com/google/android/exoplayer2/transformer/PresentationPixelTest.java @@ -60,13 +60,9 @@ public final class PresentationPixelTest { public static final String ASPECT_RATIO_STRETCH_TO_FIT_WIDE_PNG_ASSET_PATH = "media/bitmap/sample_mp4_first_frame/aspect_ratio_stretch_to_fit_wide.png"; - static { - GlUtil.glAssertionsEnabled = true; - } - - private final Context context = getApplicationContext(); - private final EGLDisplay eglDisplay = GlUtil.createEglDisplay(); - private final EGLContext eglContext = GlUtil.createEglContext(eglDisplay); + private Context context = getApplicationContext(); + private @MonotonicNonNull EGLDisplay eglDisplay; + private @MonotonicNonNull EGLContext eglContext; private @MonotonicNonNull SingleFrameGlTextureProcessor presentationTextureProcessor; private @MonotonicNonNull EGLSurface placeholderEglSurface; private int inputTexId; @@ -75,7 +71,9 @@ public final class PresentationPixelTest { private int inputHeight; @Before - public void createTextures() throws IOException { + public void createGlObjects() throws IOException, GlUtil.GlException { + eglDisplay = GlUtil.createEglDisplay(); + eglContext = GlUtil.createEglContext(eglDisplay); Bitmap inputBitmap = BitmapTestUtil.readBitmap(ORIGINAL_PNG_ASSET_PATH); inputWidth = inputBitmap.getWidth(); inputHeight = inputBitmap.getHeight(); @@ -85,11 +83,13 @@ public final class PresentationPixelTest { } @After - public void release() { + public void release() throws GlUtil.GlException, FrameProcessingException { if (presentationTextureProcessor != null) { presentationTextureProcessor.release(); } - GlUtil.destroyEglContext(eglDisplay, eglContext); + if (eglContext != null && eglDisplay != null) { + GlUtil.destroyEglContext(eglDisplay, eglContext); + } } @Test @@ -282,12 +282,12 @@ public final class PresentationPixelTest { assertThat(averagePixelAbsoluteDifference).isAtMost(MAXIMUM_AVERAGE_PIXEL_ABSOLUTE_DIFFERENCE); } - private void setupOutputTexture(int outputWidth, int outputHeight) { + private void setupOutputTexture(int outputWidth, int outputHeight) throws GlUtil.GlException { outputTexId = GlUtil.createTexture(outputWidth, outputHeight); int frameBuffer = GlUtil.createFboForTexture(outputTexId); GlUtil.focusFramebuffer( - eglDisplay, - eglContext, + checkNotNull(eglDisplay), + checkNotNull(eglContext), checkNotNull(placeholderEglSurface), frameBuffer, outputWidth, diff --git a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/Crop.java b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/Crop.java index f843f83a45..3433a9e01d 100644 --- a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/Crop.java +++ b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/Crop.java @@ -30,10 +30,6 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; */ public final class Crop implements MatrixTransformation { - static { - GlUtil.glAssertionsEnabled = true; - } - private final float left; private final float right; private final float bottom; diff --git a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/EncoderCompatibilityTransformation.java b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/EncoderCompatibilityTransformation.java index b8cbce68f6..f09f423007 100644 --- a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/EncoderCompatibilityTransformation.java +++ b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/EncoderCompatibilityTransformation.java @@ -23,7 +23,6 @@ import android.graphics.Matrix; import android.util.Size; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.Format; -import com.google.android.exoplayer2.util.GlUtil; import org.checkerframework.checker.nullness.qual.MonotonicNonNull; /** @@ -38,10 +37,6 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; // TODO(b/218488308): Allow reconfiguration of the output size, as encoders may not support the // requested output resolution. - static { - GlUtil.glAssertionsEnabled = true; - } - private int outputRotationDegrees; private @MonotonicNonNull Matrix transformationMatrix; diff --git a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/ExternalTextureProcessor.java b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/ExternalTextureProcessor.java index 4414fd0383..927acd4ad7 100644 --- a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/ExternalTextureProcessor.java +++ b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/ExternalTextureProcessor.java @@ -28,10 +28,6 @@ import java.io.IOException; /** Copies frames from an external texture and applies color transformations for HDR if needed. */ /* package */ class ExternalTextureProcessor extends SingleFrameGlTextureProcessor { - static { - GlUtil.glAssertionsEnabled = true; - } - private static final String VERTEX_SHADER_TEX_TRANSFORM_PATH = "shaders/vertex_shader_tex_transform_es2.glsl"; private static final String VERTEX_SHADER_TEX_TRANSFORM_ES3_PATH = @@ -54,10 +50,10 @@ import java.io.IOException; * Creates a new instance. * * @param enableExperimentalHdrEditing Whether to attempt to process the input as an HDR signal. - * @throws IOException If a problem occurs while reading shader files. + * @throws FrameProcessingException If a problem occurs while reading shader files. */ public ExternalTextureProcessor(Context context, boolean enableExperimentalHdrEditing) - throws IOException { + throws FrameProcessingException { String vertexShaderFilePath = enableExperimentalHdrEditing ? VERTEX_SHADER_TEX_TRANSFORM_ES3_PATH @@ -66,7 +62,11 @@ import java.io.IOException; enableExperimentalHdrEditing ? FRAGMENT_SHADER_COPY_EXTERNAL_YUV_ES3_PATH : FRAGMENT_SHADER_COPY_EXTERNAL_PATH; - glProgram = new GlProgram(context, vertexShaderFilePath, fragmentShaderFilePath); + try { + glProgram = new GlProgram(context, vertexShaderFilePath, fragmentShaderFilePath); + } catch (IOException | GlUtil.GlException e) { + throw new FrameProcessingException(e); + } // Draw the frame on the entire normalized device coordinate space, from -1 to 1, for x and y. glProgram.setBufferAttribute( "aFramePosition", @@ -109,15 +109,19 @@ import java.io.IOException; GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, /* first= */ 0, /* count= */ 4); GlUtil.checkGlError(); } catch (GlUtil.GlException e) { - throw new FrameProcessingException(e); + throw new FrameProcessingException(e, presentationTimeUs); } } @Override - public void release() { + public void release() throws FrameProcessingException { super.release(); if (glProgram != null) { - glProgram.delete(); + try { + glProgram.delete(); + } catch (GlUtil.GlException e) { + throw new FrameProcessingException(e); + } } } } diff --git a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/FrameProcessingException.java b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/FrameProcessingException.java index d5bea89ea3..31239faa94 100644 --- a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/FrameProcessingException.java +++ b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/FrameProcessingException.java @@ -20,6 +20,26 @@ import com.google.android.exoplayer2.C; /** Thrown when an exception occurs while applying effects to video frames. */ public final class FrameProcessingException extends Exception { + /** + * Wraps the given exception in a {@code FrameProcessingException} if it is not already a {@code + * FrameProcessingException} and returns the exception otherwise. + */ + public static FrameProcessingException from(Exception exception) { + return from(exception, /* presentationTimeUs= */ C.TIME_UNSET); + } + + /** + * Wraps the given exception in a {@code FrameProcessingException} with the given timestamp if it + * is not already a {@code FrameProcessingException} and returns the exception otherwise. + */ + public static FrameProcessingException from(Exception exception, long presentationTimeUs) { + if (exception instanceof FrameProcessingException) { + return (FrameProcessingException) exception; + } else { + return new FrameProcessingException(exception, presentationTimeUs); + } + } + /** * The microsecond timestamp of the frame being processed while the exception occurred or {@link * C#TIME_UNSET} if unknown. diff --git a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/FrameProcessorChain.java b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/FrameProcessorChain.java index 5825dbc1fd..43698e604f 100644 --- a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/FrameProcessorChain.java +++ b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/FrameProcessorChain.java @@ -41,7 +41,6 @@ import com.google.android.exoplayer2.util.GlUtil; import com.google.android.exoplayer2.util.Log; import com.google.android.exoplayer2.util.Util; import com.google.common.collect.ImmutableList; -import java.io.IOException; import java.util.List; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ExecutionException; @@ -65,10 +64,6 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; // TODO(b/227625423): Factor out FrameProcessor interface and rename this class to GlFrameProcessor. /* package */ final class FrameProcessorChain { - static { - GlUtil.glAssertionsEnabled = true; - } - /** * Listener for asynchronous frame processing events. * @@ -150,7 +145,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; List effects, boolean enableExperimentalHdrEditing, ExecutorService singleThreadExecutorService) - throws IOException { + throws GlUtil.GlException, FrameProcessingException { checkState(Thread.currentThread().getName().equals(THREAD_NAME)); EGLDisplay eglDisplay = GlUtil.createEglDisplay(); @@ -204,7 +199,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ExternalTextureProcessor externalTextureProcessor, float pixelWidthHeightRatio, List effects) - throws IOException { + throws FrameProcessingException { ImmutableList.Builder textureProcessors = new ImmutableList.Builder().add(externalTextureProcessor); @@ -532,22 +527,19 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; int finalInputTexId = inputTexId; debugSurfaceViewWrapper.maybeRenderToSurfaceView( () -> { - GlUtil.clearOutputFrame(); try { + GlUtil.clearOutputFrame(); getLast(textureProcessors).drawFrame(finalInputTexId, finalPresentationTimeUs); - } catch (FrameProcessingException e) { + } catch (GlUtil.GlException | FrameProcessingException e) { Log.d(TAG, "Error rendering to debug preview", e); } }); } checkState(pendingFrameCount.getAndDecrement() > 0); - } catch (FrameProcessingException | RuntimeException e) { + } catch (FrameProcessingException | GlUtil.GlException | RuntimeException e) { if (!stopProcessing.getAndSet(true)) { - listener.onFrameProcessingError( - e instanceof FrameProcessingException - ? (FrameProcessingException) e - : new FrameProcessingException(e, presentationTimeUs)); + listener.onFrameProcessingError(FrameProcessingException.from(e, presentationTimeUs)); } } } @@ -565,8 +557,8 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; textureProcessors.get(i).release(); } GlUtil.destroyEglContext(eglDisplay, eglContext); - } catch (RuntimeException e) { - listener.onFrameProcessingError(new FrameProcessingException(e)); + } catch (FrameProcessingException | GlUtil.GlException | RuntimeException e) { + listener.onFrameProcessingError(FrameProcessingException.from(e)); } } @@ -600,7 +592,8 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; * otherwise. */ @WorkerThread - public synchronized void maybeRenderToSurfaceView(Runnable renderRunnable) { + public synchronized void maybeRenderToSurfaceView(Runnable renderRunnable) + throws GlUtil.GlException { if (surface == null) { return; } diff --git a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/GlEffect.java b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/GlEffect.java index 160604efb0..0a470e75af 100644 --- a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/GlEffect.java +++ b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/GlEffect.java @@ -16,7 +16,6 @@ package com.google.android.exoplayer2.transformer; import android.content.Context; -import java.io.IOException; /** * Interface for a video frame effect with a {@link SingleFrameGlTextureProcessor} implementation. @@ -29,5 +28,6 @@ public interface GlEffect { /** Returns a {@link SingleFrameGlTextureProcessor} that applies the effect. */ // TODO(b/227625423): use GlTextureProcessor here once this interface exists. - SingleFrameGlTextureProcessor toGlTextureProcessor(Context context) throws IOException; + SingleFrameGlTextureProcessor toGlTextureProcessor(Context context) + throws FrameProcessingException; } diff --git a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/GlMatrixTransformation.java b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/GlMatrixTransformation.java index 01acf02070..aa40f2e785 100644 --- a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/GlMatrixTransformation.java +++ b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/GlMatrixTransformation.java @@ -18,7 +18,6 @@ package com.google.android.exoplayer2.transformer; import android.content.Context; import android.opengl.Matrix; import android.util.Size; -import java.io.IOException; /** * Specifies a 4x4 transformation {@link Matrix} to apply in the vertex shader for each frame. @@ -50,7 +49,8 @@ public interface GlMatrixTransformation extends GlEffect { float[] getGlMatrixArray(long presentationTimeUs); @Override - default SingleFrameGlTextureProcessor toGlTextureProcessor(Context context) throws IOException { + default SingleFrameGlTextureProcessor toGlTextureProcessor(Context context) + throws FrameProcessingException { return new MatrixTransformationProcessor(context, this); } } diff --git a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/GlTextureProcessor.java b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/GlTextureProcessor.java index b134058e27..493e10d37f 100644 --- a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/GlTextureProcessor.java +++ b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/GlTextureProcessor.java @@ -110,6 +110,10 @@ public interface GlTextureProcessor { /** Notifies the texture processor that no further input frames will become available. */ void signalEndOfInputStream(); - /** Releases all resources. */ - void release(); + /** + * Releases all resources. + * + * @throws FrameProcessingException If an error occurs while releasing resources. + */ + void release() throws FrameProcessingException; } diff --git a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/MatrixTransformationProcessor.java b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/MatrixTransformationProcessor.java index 338cef1873..dd6224756c 100644 --- a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/MatrixTransformationProcessor.java +++ b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/MatrixTransformationProcessor.java @@ -41,10 +41,6 @@ import java.util.Arrays; @SuppressWarnings("FunctionalInterfaceClash") // b/228192298 /* package */ final class MatrixTransformationProcessor extends SingleFrameGlTextureProcessor { - static { - GlUtil.glAssertionsEnabled = true; - } - private static final String VERTEX_SHADER_TRANSFORMATION_PATH = "shaders/vertex_shader_transformation_es2.glsl"; private static final String FRAGMENT_SHADER_PATH = "shaders/fragment_shader_copy_es2.glsl"; @@ -88,10 +84,10 @@ import java.util.Arrays; * @param context The {@link Context}. * @param matrixTransformation A {@link MatrixTransformation} that specifies the transformation * matrix to use for each frame. - * @throws IOException If a problem occurs while reading shader files. + * @throws FrameProcessingException If a problem occurs while reading shader files. */ public MatrixTransformationProcessor(Context context, MatrixTransformation matrixTransformation) - throws IOException { + throws FrameProcessingException { this(context, ImmutableList.of(matrixTransformation)); } @@ -101,10 +97,10 @@ import java.util.Arrays; * @param context The {@link Context}. * @param matrixTransformation A {@link GlMatrixTransformation} that specifies the transformation * matrix to use for each frame. - * @throws IOException If a problem occurs while reading shader files. + * @throws FrameProcessingException If a problem occurs while reading shader files. */ public MatrixTransformationProcessor(Context context, GlMatrixTransformation matrixTransformation) - throws IOException { + throws FrameProcessingException { this(context, ImmutableList.of(matrixTransformation)); } @@ -114,11 +110,11 @@ import java.util.Arrays; * @param context The {@link Context}. * @param matrixTransformations The {@link GlMatrixTransformation GlMatrixTransformations} to * apply to each frame in order. - * @throws IOException If a problem occurs while reading shader files. + * @throws FrameProcessingException If a problem occurs while reading shader files. */ public MatrixTransformationProcessor( Context context, ImmutableList matrixTransformations) - throws IOException { + throws FrameProcessingException { this.matrixTransformations = matrixTransformations; transformationMatrixCache = new float[matrixTransformations.size()][16]; @@ -126,7 +122,11 @@ import java.util.Arrays; tempResultMatrix = new float[16]; Matrix.setIdentityM(compositeTransformationMatrix, /* smOffset= */ 0); visiblePolygon = NDC_SQUARE; - glProgram = new GlProgram(context, VERTEX_SHADER_TRANSFORMATION_PATH, FRAGMENT_SHADER_PATH); + try { + glProgram = new GlProgram(context, VERTEX_SHADER_TRANSFORMATION_PATH, FRAGMENT_SHADER_PATH); + } catch (IOException | GlUtil.GlException e) { + throw new FrameProcessingException(e); + } } @Override @@ -168,9 +168,15 @@ import java.util.Arrays; } @Override - public void release() { + public void release() throws FrameProcessingException { super.release(); - glProgram.delete(); + if (glProgram != null) { + try { + glProgram.delete(); + } catch (GlUtil.GlException e) { + throw new FrameProcessingException(e); + } + } } /** diff --git a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/Presentation.java b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/Presentation.java index c9d6c4efbd..32e696a22f 100644 --- a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/Presentation.java +++ b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/Presentation.java @@ -24,7 +24,6 @@ import android.graphics.Matrix; import android.util.Size; import androidx.annotation.IntDef; import com.google.android.exoplayer2.C; -import com.google.android.exoplayer2.util.GlUtil; import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.Target; @@ -162,10 +161,6 @@ public final class Presentation implements MatrixTransformation { } } - static { - GlUtil.glAssertionsEnabled = true; - } - private final int requestedHeightPixels; private final float requestedAspectRatio; private final @Layout int layout; diff --git a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/ScaleToFitTransformation.java b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/ScaleToFitTransformation.java index f59bca16d9..b1f5fcb764 100644 --- a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/ScaleToFitTransformation.java +++ b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/ScaleToFitTransformation.java @@ -83,10 +83,6 @@ public final class ScaleToFitTransformation implements MatrixTransformation { } } - static { - GlUtil.glAssertionsEnabled = true; - } - private final Matrix transformationMatrix; private @MonotonicNonNull Matrix adjustedTransformationMatrix; diff --git a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/SingleFrameGlTextureProcessor.java b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/SingleFrameGlTextureProcessor.java index a4fd42f750..3fda004afd 100644 --- a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/SingleFrameGlTextureProcessor.java +++ b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/SingleFrameGlTextureProcessor.java @@ -94,7 +94,7 @@ public abstract class SingleFrameGlTextureProcessor implements GlTextureProcesso listener.onInputFrameProcessed(inputTexture); listener.onOutputFrameAvailable(outputTexture, presentationTimeUs); } - } catch (FrameProcessingException | RuntimeException e) { + } catch (FrameProcessingException | GlUtil.GlException | RuntimeException e) { if (listener != null) { listener.onFrameProcessingError( e instanceof FrameProcessingException @@ -106,7 +106,7 @@ public abstract class SingleFrameGlTextureProcessor implements GlTextureProcesso } @EnsuresNonNull("outputTexture") - private void configureOutputTexture(int inputWidth, int inputHeight) { + private void configureOutputTexture(int inputWidth, int inputHeight) throws GlUtil.GlException { this.inputWidth = inputWidth; this.inputHeight = inputHeight; Size outputSize = configure(inputWidth, inputHeight); @@ -137,9 +137,13 @@ public abstract class SingleFrameGlTextureProcessor implements GlTextureProcesso @Override @CallSuper - public void release() { + public void release() throws FrameProcessingException { if (outputTexture != null) { - GlUtil.deleteTexture(outputTexture.texId); + try { + GlUtil.deleteTexture(outputTexture.texId); + } catch (GlUtil.GlException e) { + throw new FrameProcessingException(e); + } } } }