From 47d63504c2b0f7ea9e83083bfd40f7a608076e5c Mon Sep 17 00:00:00 2001 From: claincly Date: Sat, 19 Nov 2022 00:46:47 +0000 Subject: [PATCH] Destroy eglSurface in FrameProcessor Previously, FrameProcessor never had the usecase in which the output surface is replaced, while previewing introduced this usecase. When switching output surfaces, we need to destroy the EGL Surface linked to the surface that is being swapped out, because an EGL surface is linked to the EGL display (which is not destroyed even when releasing FrameProcessor). A GL exception will be thrown in the following scenario if we don't destroy the EGL surface: 1. Creates a Surface, the surface is identified by address 0x11 2. Sets Surface(0x11) on FrameProcessor. Eventually an EGL surface is created to wrap Surface(0x11) 3. Release FrameProcess, this releases the EGL context 4. Instantiate a new FrameProcessor, sets Surface(0x11) as the output 5. When FrameProcessor creates an EGL surface to wrap Surface(0x11), GL throws an exception, becasue Surface(0x11) has previouly been connected to an EGL surface. PiperOrigin-RevId: 489590072 --- .../android/exoplayer2/util/GlUtil.java | 20 +++++++++++++++++++ .../FinalMatrixTextureProcessorWrapper.java | 13 +++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) 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 1254bb3f99e..5f3fe470895 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 @@ -468,6 +468,16 @@ public static void destroyEglContext( Api17.destroyEglContext(eglDisplay, eglContext); } + /** + * Destroys the {@link EGLSurface} identified by the provided {@link EGLDisplay} and {@link + * EGLSurface}. + */ + @RequiresApi(17) + public static void destroyEglSurface( + @Nullable EGLDisplay eglDisplay, @Nullable EGLSurface eglSurface) throws GlException { + Api17.destroyEglSurface(eglDisplay, eglSurface); + } + /** * Allocates a FloatBuffer with the given data. * @@ -733,6 +743,16 @@ public static void destroyEglContext( checkEglException("Error terminating display"); } + @DoNotInline + public static void destroyEglSurface( + @Nullable EGLDisplay eglDisplay, @Nullable EGLSurface eglSurface) throws GlException { + if (eglDisplay == null || eglSurface == null) { + return; + } + EGL14.eglDestroySurface(eglDisplay, eglSurface); + checkEglException("Error destroying surface"); + } + @DoNotInline private static EGLConfig getEglConfig(EGLDisplay eglDisplay, int[] attributes) throws GlException { diff --git a/library/effect/src/main/java/com/google/android/exoplayer2/effect/FinalMatrixTextureProcessorWrapper.java b/library/effect/src/main/java/com/google/android/exoplayer2/effect/FinalMatrixTextureProcessorWrapper.java index cd0c474e93e..ac7b669c1ec 100644 --- a/library/effect/src/main/java/com/google/android/exoplayer2/effect/FinalMatrixTextureProcessorWrapper.java +++ b/library/effect/src/main/java/com/google/android/exoplayer2/effect/FinalMatrixTextureProcessorWrapper.java @@ -183,10 +183,15 @@ public void signalEndOfCurrentInputStream() { @Override @WorkerThread - public void release() throws FrameProcessingException { + public synchronized void release() throws FrameProcessingException { if (matrixTextureProcessor != null) { matrixTextureProcessor.release(); } + try { + GlUtil.destroyEglSurface(eglDisplay, outputEglSurface); + } catch (GlUtil.GlException e) { + throw new FrameProcessingException(e); + } } @Override @@ -226,6 +231,11 @@ public synchronized void setOutputSurfaceInfo(@Nullable SurfaceInfo outputSurfac if (outputSurfaceInfo != null && this.outputSurfaceInfo != null && !this.outputSurfaceInfo.surface.equals(outputSurfaceInfo.surface)) { + try { + GlUtil.destroyEglSurface(eglDisplay, outputEglSurface); + } catch (GlUtil.GlException e) { + frameProcessorListener.onFrameProcessingError(FrameProcessingException.from(e)); + } this.outputEglSurface = null; } outputSizeOrRotationChanged = @@ -307,6 +317,7 @@ private synchronized boolean ensureConfigured(int inputWidth, int inputHeight) matrixTextureProcessor.release(); matrixTextureProcessor = null; } + GlUtil.destroyEglSurface(eglDisplay, outputEglSurface); outputEglSurface = null; return false; }