diff options
author | Jamie Gennis <jgennis@google.com> | 2011-01-13 14:43:36 -0800 |
---|---|---|
committer | Jamie Gennis <jgennis@google.com> | 2011-01-16 17:28:39 -0800 |
commit | 376590d668e22a918439877b55faf075427b13f3 (patch) | |
tree | d55b9baf0efec6a49de60786cc996f6ef699d067 /graphics | |
parent | 0fb736c0937d9d65001e0176d90e1011226594bf (diff) | |
download | frameworks_base-376590d668e22a918439877b55faf075427b13f3.zip frameworks_base-376590d668e22a918439877b55faf075427b13f3.tar.gz frameworks_base-376590d668e22a918439877b55faf075427b13f3.tar.bz2 |
Implement SurfaceTexture frame-available callback.
This change implements the onFrameAvailable callback for the
SurfaceTexture java class. It includes the C++ SurfaceTexture code as
well as the JNI and Java code to enable the callback.
Change-Id: Ifd8b8e7ad46ee70cba6da1c2e96dab8045d1ea30
Diffstat (limited to 'graphics')
-rw-r--r-- | graphics/java/android/graphics/SurfaceTexture.java | 67 |
1 files changed, 60 insertions, 7 deletions
diff --git a/graphics/java/android/graphics/SurfaceTexture.java b/graphics/java/android/graphics/SurfaceTexture.java index 3eb0b03..64c209a 100644 --- a/graphics/java/android/graphics/SurfaceTexture.java +++ b/graphics/java/android/graphics/SurfaceTexture.java @@ -16,6 +16,11 @@ package android.graphics; +import java.lang.ref.WeakReference; +import android.os.Handler; +import android.os.Looper; +import android.os.Message; + /** * Captures frames from an image stream as an OpenGL ES texture. * @@ -32,6 +37,9 @@ package android.graphics; */ public class SurfaceTexture { + private EventHandler mEventHandler; + private OnFrameAvailableListener mOnFrameAvailableListener; + @SuppressWarnings("unused") private int mSurfaceTexture; @@ -59,7 +67,15 @@ public class SurfaceTexture { * @param texName the OpenGL texture object name (e.g. generated via glGenTextures) */ public SurfaceTexture(int texName) { - init(texName); + Looper looper; + if ((looper = Looper.myLooper()) != null) { + mEventHandler = new EventHandler(looper); + } else if ((looper = Looper.getMainLooper()) != null) { + mEventHandler = new EventHandler(looper); + } else { + mEventHandler = null; + } + nativeInit(texName, new WeakReference<SurfaceTexture>(this)); } /** @@ -69,7 +85,7 @@ public class SurfaceTexture { * thread invoking the callback. */ public void setOnFrameAvailableListener(OnFrameAvailableListener l) { - // TODO: Implement this! + mOnFrameAvailableListener = l; } /** @@ -77,8 +93,9 @@ public class SurfaceTexture { * called while the OpenGL ES context that owns the texture is bound to the thread. It will * implicitly bind its texture to the GL_TEXTURE_EXTERNAL_OES texture target. */ - public native void updateTexImage(); - + public void updateTexImage() { + nativeUpdateTexImage(); + } /** * Retrieve the 4x4 texture coordinate transform matrix associated with the texture image set by @@ -99,12 +116,48 @@ public class SurfaceTexture { if (mtx.length != 16) { throw new IllegalArgumentException(); } - getTransformMatrixImpl(mtx); + nativeGetTransformMatrix(mtx); } - private native void getTransformMatrixImpl(float[] mtx); + protected void finalize() throws Throwable { + try { + nativeFinalize(); + } finally { + super.finalize(); + } + } + + private class EventHandler extends Handler { + public EventHandler(Looper looper) { + super(looper); + } + + @Override + public void handleMessage(Message msg) { + if (mOnFrameAvailableListener != null) { + mOnFrameAvailableListener.onFrameAvailable(SurfaceTexture.this); + } + return; + } + } + + private static void postEventFromNative(Object selfRef) { + WeakReference weakSelf = (WeakReference)selfRef; + SurfaceTexture st = (SurfaceTexture)weakSelf.get(); + if (st == null) { + return; + } + + if (st.mEventHandler != null) { + Message m = st.mEventHandler.obtainMessage(); + st.mEventHandler.sendMessage(m); + } + } - private native void init(int texName); + private native void nativeInit(int texName, Object weakSelf); + private native void nativeFinalize(); + private native void nativeGetTransformMatrix(float[] mtx); + private native void nativeUpdateTexImage(); /* * We use a class initializer to allow the native code to cache some |