From b939760679caa9fdd06c862cf8218cc8f4a90ef1 Mon Sep 17 00:00:00 2001 From: Pannag Sanketi Date: Mon, 7 May 2012 22:51:58 -0700 Subject: [PATCH] Adding disconnect call to the SurfaceTextureTarget Related to b/5873421. When an app using the SurfaceTextureTarget filter and the activity goes out of focus such as onPause(), the underlying surfacetexture might not be available anymore to the filter. The filter needs to be told about that so that it does try to render into the surface corresponding to the surfacetexture. For example, in Camera app, the VideoCamera activity has to let the underlying effects graph know about the disconnect in the onPause() method. Change-Id: Iee3af16715432d84fef4438d1671f4d6d261b3d7 --- .../videosrc/SurfaceTextureTarget.java | 39 +++++++++++++++---- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/media/mca/filterpacks/java/android/filterpacks/videosrc/SurfaceTextureTarget.java b/media/mca/filterpacks/java/android/filterpacks/videosrc/SurfaceTextureTarget.java index 20e4b32058bd3..b023e4295ec0f 100644 --- a/media/mca/filterpacks/java/android/filterpacks/videosrc/SurfaceTextureTarget.java +++ b/media/mca/filterpacks/java/android/filterpacks/videosrc/SurfaceTextureTarget.java @@ -110,7 +110,7 @@ public class SurfaceTextureTarget extends Filter { } @Override - public void setupPorts() { + public synchronized void setupPorts() { // Make sure we have a SurfaceView if (mSurfaceTexture == null) { throw new RuntimeException("Null SurfaceTexture passed to SurfaceTextureTarget"); @@ -158,7 +158,7 @@ public class SurfaceTextureTarget extends Filter { } @Override - public void open(FilterContext context) { + public synchronized void open(FilterContext context) { // Set up SurfaceTexture internals mSurfaceId = context.getGLEnvironment().registerSurfaceTexture( mSurfaceTexture, mScreenWidth, mScreenHeight); @@ -169,17 +169,42 @@ public class SurfaceTextureTarget extends Filter { @Override - public void close(FilterContext context) { + public synchronized void close(FilterContext context) { if (mSurfaceId > 0) { context.getGLEnvironment().unregisterSurfaceId(mSurfaceId); + mSurfaceId = -1; + // Once the surface is unregistered, remove the surfacetexture reference. + // The surfaceId could not have been valid without a valid surfacetexture. + mSurfaceTexture = null; } } + // This should be called from the client side when the surfacetexture is no longer + // valid. e.g. from onPause() in the application using the filter graph. + public synchronized void disconnect(FilterContext context) { + if (mLogVerbose) Log.v(TAG, "disconnect"); + if (mSurfaceTexture == null) { + Log.d(TAG, "SurfaceTexture is already null. Nothing to disconnect."); + return; + } + mSurfaceTexture = null; + // Make sure we unregister the surface as well if a surface was registered. + // There can be a situation where the surface was not registered but the + // surfacetexture was valid. For example, the disconnect can be called before + // the filter was opened. Hence, the surfaceId may not be a valid one here, + // and need to check for its validity. + if (mSurfaceId > 0) { + context.getGLEnvironment().unregisterSurfaceId(mSurfaceId); + mSurfaceId = -1; + } + } @Override - public void process(FilterContext context) { - if (mLogVerbose) Log.v(TAG, "Starting frame processing"); - + public synchronized void process(FilterContext context) { + // Surface is not registered. Nothing to render into. + if (mSurfaceId <= 0) { + return; + } GLEnvironment glEnv = context.getGLEnvironment(); // Get input frame @@ -197,8 +222,6 @@ public class SurfaceTextureTarget extends Filter { // See if we need to copy to GPU Frame gpuFrame = null; - if (mLogVerbose) Log.v("SurfaceTextureTarget", "Got input format: " + input.getFormat()); - int target = input.getFormat().getTarget(); if (target != FrameFormat.TARGET_GPU) { gpuFrame = context.getFrameManager().duplicateFrameToTarget(input,