From 8b1b2c949f6edc074e2f05dbbeda6d92b224fa5e Mon Sep 17 00:00:00 2001 From: Yongjoo Ahn Date: Mon, 1 Jun 2020 16:38:17 +0900 Subject: [PATCH] [Android/SNPE] Decouple SNPE env setting from native-api - Move function for android SNPE env setting from nnstreamer-native-api.c to tensor_filter_snpe.cc Signed-off-by: Yongjoo Ahn --- .../api/src/main/jni/nnstreamer-native-api.c | 121 +------------------ ext/nnstreamer/tensor_filter/tensor_filter_snpe.cc | 131 ++++++++++++++++++++- 2 files changed, 129 insertions(+), 123 deletions(-) diff --git a/api/android/api/src/main/jni/nnstreamer-native-api.c b/api/android/api/src/main/jni/nnstreamer-native-api.c index 1b1a6f2..4728377 100644 --- a/api/android/api/src/main/jni/nnstreamer-native-api.c +++ b/api/android/api/src/main/jni/nnstreamer-native-api.c @@ -48,7 +48,7 @@ extern void init_filter_snap (void); extern void init_filter_nnfw (void); #endif #if defined (ENABLE_SNPE) -extern void _init_filter_snpe (void); +extern void init_filter_snpe (JNIEnv * env, jobject context); #endif /** @@ -637,118 +637,6 @@ nns_get_nnfw_type (jint fw_type, ml_nnfw_type_e * nnfw) } /** - * @brief Set additional environment - */ -static gboolean -nns_set_env (JNIEnv * env, jobject context) -{ - gboolean ret = TRUE; - -#if defined (ENABLE_SNPE) - gboolean snpe_failed = TRUE; - - jclass context_class = NULL; - jmethodID get_application_info_method_id = NULL; - jobject application_info_object = NULL; - jclass application_info_object_class = NULL; - jfieldID native_library_dir_field_id = NULL; - jstring native_library_dir_path = NULL; - - const gchar *native_library_dir_path_str; - gchar *new_path; - - context_class = (*env)->GetObjectClass (env, context); - if (!context_class) { - nns_loge ("Failed to get context class."); - goto snpe_done; - } - - get_application_info_method_id = (*env)->GetMethodID (env, context_class, - "getApplicationInfo", "()Landroid/content/pm/ApplicationInfo;"); - if (!get_application_info_method_id) { - nns_loge ("Failed to get method ID for `ApplicationInfo()`."); - goto snpe_done; - } - - application_info_object = (*env)->CallObjectMethod (env, context, - get_application_info_method_id); - if ((*env)->ExceptionCheck (env)) { - (*env)->ExceptionDescribe (env); - (*env)->ExceptionClear (env); - nns_loge ("Failed to call method `ApplicationInfo()`."); - goto snpe_done; - } - - application_info_object_class = (*env)->GetObjectClass (env, - application_info_object); - if (!application_info_object_class) { - nns_loge ("Failed to get `ApplicationInfo` object class"); - goto snpe_done; - } - - native_library_dir_field_id = (*env)->GetFieldID (env, - application_info_object_class, - "nativeLibraryDir", "Ljava/lang/String;"); - if (!native_library_dir_field_id) { - nns_loge ("Failed to get field ID for `nativeLibraryDir`."); - goto snpe_done; - } - - native_library_dir_path = (*env)->GetObjectField (env, - application_info_object, native_library_dir_field_id); - if (!native_library_dir_path) { - nns_loge ("Failed to get field `nativeLibraryDir`."); - goto snpe_done; - } - - native_library_dir_path_str = (*env)->GetStringUTFChars (env, - native_library_dir_path, NULL); - if ((*env)->ExceptionCheck (env)) { - (*env)->ExceptionDescribe (env); - (*env)->ExceptionClear (env); - nns_loge ("Failed to get string `nativeLibraryDir`"); - goto snpe_done; - } - - new_path = g_strconcat (native_library_dir_path_str, - ";/system/lib/rfsa/adsp;/system/vendor/lib/rfsa/adsp;/dsp", NULL); - - /* See https://developer.qualcomm.com/docs/snpe/dsp_runtime.html for details */ - nns_logi ("Set env ADSP_LIBRARY_PATH for snpe DSP/AIP runtime: %s", - new_path); - g_setenv ("ADSP_LIBRARY_PATH", new_path, TRUE); - - g_free (new_path); - (*env)->ReleaseStringUTFChars (env, native_library_dir_path, - native_library_dir_path_str); - - snpe_failed = FALSE; - -snpe_done: - ret = ret && !(snpe_failed); - - if (native_library_dir_path) { - (*env)->DeleteLocalRef (env, native_library_dir_path); - } - - if (application_info_object_class) { - (*env)->DeleteLocalRef (env, application_info_object_class); - } - - if (application_info_object) { - (*env)->DeleteLocalRef (env, application_info_object); - } - - if (context_class) { - (*env)->DeleteLocalRef (env, context_class); - } - -#endif - - return ret; -} - -/** * @brief Initialize NNStreamer, register required plugins. */ jboolean @@ -769,11 +657,6 @@ nnstreamer_native_initialize (JNIEnv * env, jobject context) goto done; } - if (!nns_set_env (env, context)) { - nns_loge ("Failed to set extra env"); - goto done; - } - if (nns_is_initilaized == FALSE) { /* register nnstreamer plugins */ #if !defined (NNS_SINGLE_ONLY) @@ -805,7 +688,7 @@ nnstreamer_native_initialize (JNIEnv * env, jobject context) init_filter_nnfw (); #endif #if defined (ENABLE_SNPE) - _init_filter_snpe (); + init_filter_snpe (env, context); #endif nns_is_initilaized = TRUE; diff --git a/ext/nnstreamer/tensor_filter/tensor_filter_snpe.cc b/ext/nnstreamer/tensor_filter/tensor_filter_snpe.cc index b06f9ee..8c039cb 100644 --- a/ext/nnstreamer/tensor_filter/tensor_filter_snpe.cc +++ b/ext/nnstreamer/tensor_filter/tensor_filter_snpe.cc @@ -35,6 +35,10 @@ #include #include +#if defined(__ANDROID__) +#include +#endif + /** * @brief Macro for debug mode. */ @@ -46,8 +50,12 @@ namespace nnstreamer { namespace tensor_filter_snpe { extern "C" { - void _init_filter_snpe (void) __attribute__ ((constructor)); - void _fini_filter_snpe (void) __attribute__ ((destructor)); +#if defined(__ANDROID__) + void init_filter_snpe (JNIEnv * env, jobject context); +#else + void init_filter_snpe (void) __attribute__ ((constructor)); +#endif + void fini_filter_snpe (void) __attribute__ ((destructor)); } class snpe_subplugin final : public tensor_filter_subplugin { @@ -424,16 +432,131 @@ void snpe_subplugin::fini_filter_snpe (void) tensor_filter_subplugin::unregister_subplugin (registeredRepresentation); } -void _init_filter_snpe () +#if defined(__ANDROID__) +/** + * @brief Set additional environment (ADSP_LIBRARY_PATH) for snpe + */ +static gboolean _snpe_set_env (JNIEnv * env, jobject context) +{ + gboolean snpe_failed = TRUE; + jclass context_class = NULL; + jmethodID get_application_info_method_id = NULL; + jobject application_info_object = NULL; + jclass application_info_object_class = NULL; + jfieldID native_library_dir_field_id = NULL; + jstring native_library_dir_path = NULL; + + const gchar *native_library_dir_path_str; + gchar *new_path; + + context_class = env->GetObjectClass (context); + if (!context_class) { + nns_loge ("Failed to get context class."); + goto done; + } + + get_application_info_method_id = env->GetMethodID (context_class, + "getApplicationInfo", "()Landroid/content/pm/ApplicationInfo;"); + if (!get_application_info_method_id) { + nns_loge ("Failed to get method ID for `ApplicationInfo()`."); + goto done; + } + + application_info_object = env->CallObjectMethod (context, + get_application_info_method_id); + if (env->ExceptionCheck ()) { + env->ExceptionDescribe (); + env->ExceptionClear (); + nns_loge ("Failed to call method `ApplicationInfo()`."); + goto done; + } + + application_info_object_class = env->GetObjectClass (application_info_object); + if (!application_info_object_class) { + nns_loge ("Failed to get `ApplicationInfo` object class"); + goto done; + } + + native_library_dir_field_id = env->GetFieldID (application_info_object_class, + "nativeLibraryDir", "Ljava/lang/String;"); + if (!native_library_dir_field_id) { + nns_loge ("Failed to get field ID for `nativeLibraryDir`."); + goto done; + } + + native_library_dir_path = static_cast(env->GetObjectField( + application_info_object, native_library_dir_field_id)); + if (!native_library_dir_path) { + nns_loge ("Failed to get field `nativeLibraryDir`."); + goto done; + } + + native_library_dir_path_str = env->GetStringUTFChars (native_library_dir_path, + NULL); + if (env->ExceptionCheck ()) { + env->ExceptionDescribe (); + env->ExceptionClear (); + nns_loge ("Failed to get string `nativeLibraryDir`"); + goto done; + } + + new_path = g_strconcat (native_library_dir_path_str, + ";/system/lib/rfsa/adsp;/system/vendor/lib/rfsa/adsp;/dsp", NULL); + + /* See https://developer.qualcomm.com/docs/snpe/dsp_runtime.html for details */ + nns_logi ("Set env ADSP_LIBRARY_PATH for snpe DSP/AIP runtime: %s", + new_path); + g_setenv ("ADSP_LIBRARY_PATH", new_path, TRUE); + + g_free (new_path); + env->ReleaseStringUTFChars (native_library_dir_path, + native_library_dir_path_str); + + snpe_failed = FALSE; + +done: + + if (native_library_dir_path) { + env->DeleteLocalRef (native_library_dir_path); + } + + if (application_info_object_class) { + env->DeleteLocalRef (application_info_object_class); + } + + if (application_info_object) { + env->DeleteLocalRef (application_info_object); + } + + if (context_class) { + env->DeleteLocalRef (context_class); + } + + return !(snpe_failed); +} + +void init_filter_snpe (JNIEnv * env, jobject context) { if (nnstreamer_filter_find ("snap")) { nns_loge ("Cannot use SNPE and SNAP both. Won't register this SNPE subplugin."); return; } + + if (!_snpe_set_env (env, context)) { + nns_loge ("Failed to set extra env"); + return; + } + snpe_subplugin::init_filter_snpe (); } +#else +void init_filter_snpe () +{ + snpe_subplugin::init_filter_snpe (); +} +#endif -void _fini_filter_snpe () +void fini_filter_snpe () { snpe_subplugin::fini_filter_snpe (); } -- 2.7.4