[vlc-devel] [PATCH 01/14] android: utils: reorder android JNIEnv helper

Alexandre Janniaux ajanni at videolabs.io
Fri Jun 12 11:40:44 CEST 2020


Those helpers are used everywhere else in the file so having them at the
beginning is easier.
---
 modules/video_output/android/utils.c | 169 ++++++++++++++-------------
 1 file changed, 85 insertions(+), 84 deletions(-)

diff --git a/modules/video_output/android/utils.c b/modules/video_output/android/utils.c
index 1bc00444e63..3f57489cf6d 100644
--- a/modules/video_output/android/utils.c
+++ b/modules/video_output/android/utils.c
@@ -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)
 {
-- 
2.27.0



More information about the vlc-devel mailing list