diff options
| author | 2011-06-20 18:39:35 -0700 | |
|---|---|---|
| committer | 2011-06-23 15:12:16 -0700 | |
| commit | 0a8fd9b610b2de92930c92d71ac184dc9e2bcb4d (patch) | |
| tree | 5554d3625c094b11cc8ac5359d18680c2dad31f9 | |
| parent | 090117774a4eeca850cca9ceac41cd8187772c81 (diff) | |
SurfaceTexture: detach from Dalvik when necessary.
This change adds a call to detach from the Davlik VM in SurfaceTexture
JNI calls that caused an attach.
Change-Id: I8e0d7d596680bd25ac42f8db4ca8343a62827255
| -rw-r--r-- | core/jni/android/graphics/SurfaceTexture.cpp | 41 |
1 files changed, 31 insertions, 10 deletions
diff --git a/core/jni/android/graphics/SurfaceTexture.cpp b/core/jni/android/graphics/SurfaceTexture.cpp index a8cb6f753565..6e082168a077 100644 --- a/core/jni/android/graphics/SurfaceTexture.cpp +++ b/core/jni/android/graphics/SurfaceTexture.cpp @@ -91,7 +91,8 @@ public: virtual void onFrameAvailable(); private: - static JNIEnv* getJNIEnv(); + static JNIEnv* getJNIEnv(bool* needsDetach); + static void detachJNI(); jobject mWeakThiz; jclass mClazz; @@ -103,37 +104,57 @@ JNISurfaceTextureContext::JNISurfaceTextureContext(JNIEnv* env, mClazz((jclass)env->NewGlobalRef(clazz)) {} -JNIEnv* JNISurfaceTextureContext::getJNIEnv() { - JNIEnv* env; - JavaVMAttachArgs args = {JNI_VERSION_1_4, NULL, NULL}; +JNIEnv* JNISurfaceTextureContext::getJNIEnv(bool* needsDetach) { + *needsDetach = false; + JNIEnv* env = AndroidRuntime::getJNIEnv(); + if (env == NULL) { + JavaVMAttachArgs args = {JNI_VERSION_1_4, NULL, NULL}; + JavaVM* vm = AndroidRuntime::getJavaVM(); + int result = vm->AttachCurrentThread(&env, (void*) &args); + if (result != JNI_OK) { + LOGE("thread attach failed: %#x", result); + return NULL; + } + *needsDetach = true; + } + return env; +} + +void JNISurfaceTextureContext::detachJNI() { JavaVM* vm = AndroidRuntime::getJavaVM(); - int result = vm->AttachCurrentThread(&env, (void*) &args); + int result = vm->DetachCurrentThread(); if (result != JNI_OK) { - LOGE("thread attach failed: %#x", result); - return NULL; + LOGE("thread detach failed: %#x", result); } - return env; } JNISurfaceTextureContext::~JNISurfaceTextureContext() { - JNIEnv* env = getJNIEnv(); + bool needsDetach = false; + JNIEnv* env = getJNIEnv(&needsDetach); if (env != NULL) { env->DeleteGlobalRef(mWeakThiz); env->DeleteGlobalRef(mClazz); } else { LOGW("leaking JNI object references"); } + if (needsDetach) { + detachJNI(); + } } void JNISurfaceTextureContext::onFrameAvailable() { - JNIEnv *env = getJNIEnv(); + bool needsDetach = false; + JNIEnv* env = getJNIEnv(&needsDetach); if (env != NULL) { env->CallStaticVoidMethod(mClazz, fields.postEvent, mWeakThiz); } else { LOGW("onFrameAvailable event will not posted"); } + if (needsDetach) { + detachJNI(); + } } // ---------------------------------------------------------------------------- |