Skip to content

Commit

Permalink
Destroy eglSurface in FrameProcessor
Browse files Browse the repository at this point in the history
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
  • Loading branch information
claincly authored and microkatz committed Nov 21, 2022
1 parent b81cd08 commit 47d6350
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -307,6 +317,7 @@ private synchronized boolean ensureConfigured(int inputWidth, int inputHeight)
matrixTextureProcessor.release();
matrixTextureProcessor = null;
}
GlUtil.destroyEglSurface(eglDisplay, outputEglSurface);
outputEglSurface = null;
return false;
}
Expand Down

0 comments on commit 47d6350

Please sign in to comment.