Skip to content
Snippets Groups Projects
Commit eab0e0d7 authored by Alexandre Janniaux's avatar Alexandre Janniaux
Browse files

android: utils: reorder android JNIEnv helper

Those helpers are used everywhere else in the file so having them at the
beginning is easier.
parent ec0b151f
No related branches found
No related tags found
No related merge requests found
......@@ -126,6 +126,91 @@ static struct
} SurfaceTexture;
} jfields;
#define JNI_CALL(what, obj, method, ...) \
(*p_env)->what(p_env, obj, jfields.method, ##__VA_ARGS__)
#define JNI_ANWCALL(what, method, ...) \
(*p_env)->what(p_env, p_awh->jobj, jfields.AWindow.method, ##__VA_ARGS__)
#define JNI_STEXCALL(what, method, ...) \
(*p_env)->what(p_env, p_awh->jobj, jfields.AWindow.method, ##__VA_ARGS__)
/*
* Andoid JNIEnv helper
*/
static pthread_key_t jni_env_key;
static pthread_once_t jni_env_key_once = PTHREAD_ONCE_INIT;
/* This function is called when a thread attached to the Java VM is canceled or
* exited */
static void
jni_detach_thread(void *data)
{
JNIEnv *env = data;
JavaVM *jvm;
(*env)->GetJavaVM(env, &jvm);
assert(jvm);
(*jvm)->DetachCurrentThread(jvm);
}
static void jni_env_key_create()
{
/* Create a TSD area and setup a destroy callback when a thread that
* previously set the jni_env_key is canceled or exited */
pthread_key_create(&jni_env_key, jni_detach_thread);
}
static JNIEnv *
android_getEnvCommon(vlc_object_t *p_obj, JavaVM *jvm, const char *psz_name)
{
assert((p_obj && !jvm) || (!p_obj && jvm));
JNIEnv *env;
pthread_once(&jni_env_key_once, jni_env_key_create);
env = pthread_getspecific(jni_env_key);
if (env == NULL)
{
if (!jvm)
jvm = var_InheritAddress(p_obj, "android-jvm");
if (!jvm)
return NULL;
/* if GetEnv returns JNI_OK, the thread is already attached to the
* JavaVM, so we are already in a java thread, and we don't have to
* setup any destroy callbacks */
if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_2) != JNI_OK)
{
/* attach the thread to the Java VM */
JavaVMAttachArgs args;
args.version = JNI_VERSION_1_2;
args.name = psz_name;
args.group = NULL;
if ((*jvm)->AttachCurrentThread(jvm, &env, &args) != JNI_OK)
return NULL;
/* Set the attached env to the thread-specific data area (TSD) */
if (pthread_setspecific(jni_env_key, env) != 0)
{
(*jvm)->DetachCurrentThread(jvm);
return NULL;
}
}
}
return env;
}
JNIEnv *
android_getEnv(vlc_object_t *p_obj, const char *psz_name)
{
return android_getEnvCommon(p_obj, NULL, psz_name);
}
/*
* Android Surface (pre android 2.3)
*/
......@@ -423,83 +508,6 @@ LoadNativeWindowAPI(AWindowHandler *p_awh, JNIEnv *p_env)
}
}
/*
* Andoid JNIEnv helper
*/
static pthread_key_t jni_env_key;
static pthread_once_t jni_env_key_once = PTHREAD_ONCE_INIT;
/* This function is called when a thread attached to the Java VM is canceled or
* exited */
static void
jni_detach_thread(void *data)
{
JNIEnv *env = data;
JavaVM *jvm;
(*env)->GetJavaVM(env, &jvm);
assert(jvm);
(*jvm)->DetachCurrentThread(jvm);
}
static void jni_env_key_create()
{
/* Create a TSD area and setup a destroy callback when a thread that
* previously set the jni_env_key is canceled or exited */
pthread_key_create(&jni_env_key, jni_detach_thread);
}
static JNIEnv *
android_getEnvCommon(vlc_object_t *p_obj, JavaVM *jvm, const char *psz_name)
{
assert((p_obj && !jvm) || (!p_obj && jvm));
JNIEnv *env;
pthread_once(&jni_env_key_once, jni_env_key_create);
env = pthread_getspecific(jni_env_key);
if (env == NULL)
{
if (!jvm)
jvm = var_InheritAddress(p_obj, "android-jvm");
if (!jvm)
return NULL;
/* if GetEnv returns JNI_OK, the thread is already attached to the
* JavaVM, so we are already in a java thread, and we don't have to
* setup any destroy callbacks */
if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_2) != JNI_OK)
{
/* attach the thread to the Java VM */
JavaVMAttachArgs args;
args.version = JNI_VERSION_1_2;
args.name = psz_name;
args.group = NULL;
if ((*jvm)->AttachCurrentThread(jvm, &env, &args) != JNI_OK)
return NULL;
/* Set the attached env to the thread-specific data area (TSD) */
if (pthread_setspecific(jni_env_key, env) != 0)
{
(*jvm)->DetachCurrentThread(jvm);
return NULL;
}
}
}
return env;
}
JNIEnv *
android_getEnv(vlc_object_t *p_obj, const char *psz_name)
{
return android_getEnvCommon(p_obj, NULL, psz_name);
}
static void
AndroidNativeWindow_onMouseEvent(JNIEnv*, jobject, jlong, jint, jint, jint, jint);
static void
......@@ -585,13 +593,6 @@ end:
return ret;
}
#define JNI_CALL(what, obj, method, ...) \
(*p_env)->what(p_env, obj, jfields.method, ##__VA_ARGS__)
#define JNI_ANWCALL(what, method, ...) \
(*p_env)->what(p_env, p_awh->jobj, jfields.AWindow.method, ##__VA_ARGS__)
#define JNI_STEXCALL(what, method, ...) \
(*p_env)->what(p_env, p_awh->jobj, jfields.AWindow.method, ##__VA_ARGS__)
static JNIEnv*
AWindowHandler_getEnv(AWindowHandler *p_awh)
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment