Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 84293fb9 authored by Jamie Gennis's avatar Jamie Gennis
Browse files

SurfaceTexture: attach to Dalvik when needed.

This change fixes a bug in the SurfaceTexture JNI where a thread that
the Dalvik VM was not aware of calls the onFrameAvailable callback.
When this happens the callback needs to first attach the thread to the
VM before attempting to post the onFrameAvailable event for Java code to
handle.

Change-Id: I6a5470c32611ea6f38e9167779450f50635cabd3
parent f71e5469
Loading
Loading
Loading
Loading
+27 −5
Original line number Diff line number Diff line
@@ -91,6 +91,8 @@ public:
    virtual void onFrameAvailable();

private:
    static JNIEnv* getJNIEnv();

    jobject mWeakThiz;
    jclass mClazz;
};
@@ -101,17 +103,37 @@ JNISurfaceTextureContext::JNISurfaceTextureContext(JNIEnv* env,
    mClazz((jclass)env->NewGlobalRef(clazz))
{}

JNIEnv* JNISurfaceTextureContext::getJNIEnv() {
    JNIEnv* env;
    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;
    }
    return env;
}

JNISurfaceTextureContext::~JNISurfaceTextureContext()
{
    JNIEnv *env = AndroidRuntime::getJNIEnv();
    JNIEnv* env = getJNIEnv();
    if (env != NULL) {
        env->DeleteGlobalRef(mWeakThiz);
        env->DeleteGlobalRef(mClazz);
    } else {
        LOGW("leaking JNI object references");
    }
}

void JNISurfaceTextureContext::onFrameAvailable()
{
    JNIEnv *env = AndroidRuntime::getJNIEnv();
    JNIEnv *env = getJNIEnv();
    if (env != NULL) {
        env->CallStaticVoidMethod(mClazz, fields.postEvent, mWeakThiz);
    } else {
        LOGW("onFrameAvailable event will not posted");
    }
}

// ----------------------------------------------------------------------------