Loading core/jni/android/graphics/SurfaceTexture.cpp +31 −10 Original line number Diff line number Diff line Loading @@ -91,7 +91,8 @@ public: virtual void onFrameAvailable(); private: static JNIEnv* getJNIEnv(); static JNIEnv* getJNIEnv(bool* needsDetach); static void detachJNI(); jobject mWeakThiz; jclass mClazz; Loading @@ -103,8 +104,10 @@ JNISurfaceTextureContext::JNISurfaceTextureContext(JNIEnv* env, mClazz((jclass)env->NewGlobalRef(clazz)) {} JNIEnv* JNISurfaceTextureContext::getJNIEnv() { JNIEnv* env; 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); Loading @@ -112,28 +115,46 @@ JNIEnv* JNISurfaceTextureContext::getJNIEnv() { LOGE("thread attach failed: %#x", result); return NULL; } *needsDetach = true; } return env; } void JNISurfaceTextureContext::detachJNI() { JavaVM* vm = AndroidRuntime::getJavaVM(); int result = vm->DetachCurrentThread(); if (result != JNI_OK) { LOGE("thread detach failed: %#x", result); } } 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(); } } // ---------------------------------------------------------------------------- Loading Loading
core/jni/android/graphics/SurfaceTexture.cpp +31 −10 Original line number Diff line number Diff line Loading @@ -91,7 +91,8 @@ public: virtual void onFrameAvailable(); private: static JNIEnv* getJNIEnv(); static JNIEnv* getJNIEnv(bool* needsDetach); static void detachJNI(); jobject mWeakThiz; jclass mClazz; Loading @@ -103,8 +104,10 @@ JNISurfaceTextureContext::JNISurfaceTextureContext(JNIEnv* env, mClazz((jclass)env->NewGlobalRef(clazz)) {} JNIEnv* JNISurfaceTextureContext::getJNIEnv() { JNIEnv* env; 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); Loading @@ -112,28 +115,46 @@ JNIEnv* JNISurfaceTextureContext::getJNIEnv() { LOGE("thread attach failed: %#x", result); return NULL; } *needsDetach = true; } return env; } void JNISurfaceTextureContext::detachJNI() { JavaVM* vm = AndroidRuntime::getJavaVM(); int result = vm->DetachCurrentThread(); if (result != JNI_OK) { LOGE("thread detach failed: %#x", result); } } 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(); } } // ---------------------------------------------------------------------------- Loading