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 6737b2f37d..57e3390d74 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 @@ -469,6 +469,16 @@ public final class GlUtil { 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. * @@ -734,6 +744,16 @@ public final class GlUtil { 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/libraries/effect/src/main/java/androidx/media3/effect/FinalMatrixTextureProcessorWrapper.java b/libraries/effect/src/main/java/androidx/media3/effect/FinalMatrixTextureProcessorWrapper.java index 2f3125606d..0b78b6e779 100644 --- a/libraries/effect/src/main/java/androidx/media3/effect/FinalMatrixTextureProcessorWrapper.java +++ b/libraries/effect/src/main/java/androidx/media3/effect/FinalMatrixTextureProcessorWrapper.java @@ -183,10 +183,15 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; @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 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; 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 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; matrixTextureProcessor.release(); matrixTextureProcessor = null; } + GlUtil.destroyEglSurface(eglDisplay, outputEglSurface); outputEglSurface = null; return false; }