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

Commit fd0ffd2a authored by Jamie Gennis's avatar Jamie Gennis Committed by Android (Google) Code Review
Browse files

Merge "SurfaceTexture: attach to Dalvik when needed."

parents 466ebb1d 84293fb9
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");
    }
}

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