Browse Source
android_utils depends on GLES2_LIBS and EGL_LIBS, but it isn't forwarded correctly from the convenience library. In particlar, it contains mostly video-related code, and some helpers for the android environment state, which is why the non-video code is using the utils library. Move the code in a separate convenience library so that it can be imported without constraints.pull/144/head
committed by
Thomas Guillem
14 changed files with 165 additions and 102 deletions
@ -0,0 +1,103 @@ |
|||
/*****************************************************************************
|
|||
* env.c: shared code between Android modules. |
|||
***************************************************************************** |
|||
* Copyright (C) 2014-2015 VLC authors and VideoLAN |
|||
* |
|||
* Authors: Felix Abecassis <felix.abecassis@gmail.com> |
|||
* Thomas Guillem <thomas@gllm.fr> |
|||
* |
|||
* This program is free software; you can redistribute it and/or modify it |
|||
* under the terms of the GNU Lesser General Public License as published by |
|||
* the Free Software Foundation; either version 2.1 of the License, or |
|||
* (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU Lesser General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU Lesser General Public License |
|||
* along with this program; if not, write to the Free Software Foundation, |
|||
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. |
|||
*****************************************************************************/ |
|||
|
|||
#ifdef HAVE_CONFIG_H |
|||
# include <config.h> |
|||
#endif |
|||
|
|||
#include "utils.h" |
|||
#include <dlfcn.h> |
|||
#include <jni.h> |
|||
#include <pthread.h> |
|||
#include <assert.h> |
|||
|
|||
/*
|
|||
* Android 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); |
|||
} |
|||
|
|||
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; |
|||
} |
|||
@ -0,0 +1,33 @@ |
|||
/*****************************************************************************
|
|||
* env.h: shared code between Android modules |
|||
***************************************************************************** |
|||
* Copyright (C) 2014-2015 VLC authors and VideoLAN |
|||
* |
|||
* Authors: Felix Abecassis <felix.abecassis@gmail.com> |
|||
* Thomas Guillem <thomas@gllm.fr> |
|||
* |
|||
* This program is free software; you can redistribute it and/or modify it |
|||
* under the terms of the GNU Lesser General Public License as published by |
|||
* the Free Software Foundation; either version 2.1 of the License, or |
|||
* (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU Lesser General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU Lesser General Public License |
|||
* along with this program; if not, write to the Free Software Foundation, |
|||
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. |
|||
*****************************************************************************/ |
|||
|
|||
#include <jni.h> |
|||
|
|||
JNIEnv * |
|||
android_getEnvCommon(vlc_object_t *p_obj, JavaVM *jvm, const char *psz_name); |
|||
|
|||
static inline JNIEnv * |
|||
android_getEnv(vlc_object_t *p_obj, const char *psz_name) |
|||
{ |
|||
return android_getEnvCommon(p_obj, NULL, psz_name); |
|||
} |
|||
Loading…
Reference in new issue