OSDN Git Service

Add a 'release' method to the SurfaceTexture public Java API
authorMathias Agopian <mathias@google.com>
Wed, 3 Aug 2011 22:18:36 +0000 (15:18 -0700)
committerMathias Agopian <mathias@google.com>
Thu, 4 Aug 2011 00:23:44 +0000 (17:23 -0700)
Bug: 5063618
Change-Id: I689cb0c01c14e597ccfb4eb0972e64fa570bd4e8

api/current.txt
core/java/android/view/TextureView.java
core/jni/android/graphics/SurfaceTexture.cpp
graphics/java/android/graphics/SurfaceTexture.java
libs/gui/SurfaceTexture.cpp

index d18b734..ffafd23 100644 (file)
@@ -8454,6 +8454,7 @@ package android.graphics {
     ctor public SurfaceTexture(int, boolean);
     method public long getTimestamp();
     method public void getTransformMatrix(float[]);
+    method public void release();
     method public void setOnFrameAvailableListener(android.graphics.SurfaceTexture.OnFrameAvailableListener);
     method public void updateTexImage();
   }
index 96d6f09..76aa21f 100644 (file)
@@ -204,6 +204,7 @@ public class TextureView extends View {
             }
 
             mLayer.destroy();
+            mSurface.release();
             mSurface = null;
             mLayer = null;
         }
index 2de0932..ffcd1a0 100644 (file)
@@ -233,6 +233,12 @@ static jlong SurfaceTexture_getTimestamp(JNIEnv* env, jobject thiz)
     return surfaceTexture->getTimestamp();
 }
 
+static void SurfaceTexture_release(JNIEnv* env, jobject thiz)
+{
+    sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
+    surfaceTexture->abandon();
+}
+
 // ----------------------------------------------------------------------------
 
 static JNINativeMethod gSurfaceTextureMethods[] = {
@@ -243,6 +249,7 @@ static JNINativeMethod gSurfaceTextureMethods[] = {
     {"nativeUpdateTexImage",     "()V",   (void*)SurfaceTexture_updateTexImage },
     {"nativeGetTransformMatrix", "([F)V", (void*)SurfaceTexture_getTransformMatrix },
     {"nativeGetTimestamp",       "()J",   (void*)SurfaceTexture_getTimestamp },
+    {"nativeRelease",            "()V",   (void*)SurfaceTexture_release },
 };
 
 int register_android_graphics_SurfaceTexture(JNIEnv* env)
index 1647ff3..d62fd67 100644 (file)
@@ -187,6 +187,25 @@ public class SurfaceTexture {
         return nativeGetTimestamp();
     }
 
+    /**
+     * release() frees all the buffers and puts the SurfaceTexture into the
+     * 'abandoned' state. Once put in this state the SurfaceTexture can never
+     * leave it. When in the 'abandoned' state, all methods of the
+     * ISurfaceTexture interface will fail with the NO_INIT error.
+     *
+     * Note that while calling this method causes all the buffers to be freed
+     * from the perspective of the the SurfaceTexture, if there are additional
+     * references on the buffers (e.g. if a buffer is referenced by a client or
+     * by OpenGL ES as a texture) then those buffer will remain allocated.
+     *
+     * Always call this method when you are done with SurfaceTexture. Failing
+     * to do so may delay resource deallocation for a significant amount of
+     * time.
+     */
+    public void release() {
+        nativeRelease();
+    }
+
     protected void finalize() throws Throwable {
         try {
             nativeFinalize();
@@ -232,6 +251,7 @@ public class SurfaceTexture {
     private native void nativeSetDefaultBufferSize(int width, int height);
     private native void nativeUpdateTexImage();
     private native int nativeGetQueuedCount();
+    private native void nativeRelease();
 
     /*
      * We use a class initializer to allow the native code to cache some
index 4f51f03..1a036ee 100644 (file)
@@ -910,6 +910,7 @@ void SurfaceTexture::abandon() {
     Mutex::Autolock lock(mMutex);
     freeAllBuffers();
     mAbandoned = true;
+    mCurrentTextureBuf.clear();
     mDequeueCondition.signal();
 }