Skip to content

Commit

Permalink
[android] Set envvar for Qualcomm related filters
Browse files Browse the repository at this point in the history
- QNN / SNPE needs some additional tasks. This job has been done by
  nnstreamer's each sub-plugin.
- Let the task be done in nnstreamer-native-api.c

Signed-off-by: Yongjoo Ahn <yongjoo1.ahn@samsung.com>
  • Loading branch information
anyj0527 authored and jaeyun-jung committed Sep 3, 2024
1 parent 06af5d1 commit b3a2f2c
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ TFLITE_ENABLE_QNN_DELEGATE := false
endif

ifeq ($(TFLITE_ENABLE_QNN_DELEGATE),true)
NNS_API_FLAGS += -DENABLE_TFLITE_QNN_DELEGATE=1
TFLITE_FLAGS += -DTFLITE_QNN_DELEGATE_SUPPORTED=1
QNN_DELEGATE_DIR := $(LOCAL_PATH)/tensorflow-lite-QNN-delegate
QNN_DELEGATE_INCLUDE := $(QNN_DELEGATE_DIR)/include
Expand Down
120 changes: 116 additions & 4 deletions java/android/nnstreamer/src/main/jni/nnstreamer-native-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,113 @@ extern void init_filter_cpp (void);
extern void init_filter_custom (void);
extern void init_filter_custom_easy (void);

/**
* @brief Set additional environment (ADSP_LIBRARY_PATH) for Qualcomm Android.
*/
static gboolean
_qc_android_set_env (JNIEnv *env, jobject context)
{
gboolean 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;

g_return_val_if_fail (env != NULL, FALSE);
g_return_val_if_fail (context != NULL, FALSE);

context_class = (*env)->GetObjectClass (env, context);
if (!context_class) {
_ml_loge ("Failed to get context class.");
goto done;
}

get_application_info_method_id = (*env)->GetMethodID (env, context_class,
"getApplicationInfo", "()Landroid/content/pm/ApplicationInfo;");
if (!get_application_info_method_id) {
_ml_loge ("Failed to get method ID for `ApplicationInfo()`.");
goto done;
}

application_info_object = (*env)->CallObjectMethod (env, context, get_application_info_method_id);
if ((*env)->ExceptionCheck (env)) {
(*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
_ml_loge ("Failed to call method `ApplicationInfo()`.");
goto done;
}

application_info_object_class = (*env)->GetObjectClass (env, application_info_object);
if (!application_info_object_class) {
_ml_loge ("Failed to get `ApplicationInfo` object class");
goto done;
}

native_library_dir_field_id = (*env)->GetFieldID (env,
application_info_object_class, "nativeLibraryDir", "Ljava/lang/String;");
if (!native_library_dir_field_id) {
_ml_loge ("Failed to get field ID for `nativeLibraryDir`.");
goto done;
}

native_library_dir_path = (jstring) (
(*env)->GetObjectField (env, application_info_object, native_library_dir_field_id));
if (!native_library_dir_path) {
_ml_loge ("Failed to get field `nativeLibraryDir`.");
goto done;
}

native_library_dir_path_str = (*env)->GetStringUTFChars (env, native_library_dir_path, NULL);
if ((*env)->ExceptionCheck (env)) {
(*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
_ml_loge ("Failed to get string `nativeLibraryDir`");
goto done;
}

new_path = g_strconcat (native_library_dir_path_str,
";/vendor/dsp/cdsp;/vendor/lib/rfsa/adsp;/system/lib/rfsa/adsp;/system/vendor/lib/rfsa/adsp;/dsp", NULL);

/**
* See https://docs.qualcomm.com/bundle/publicresource/topics/80-63442-2/dsp_runtime.html for details
*/
_ml_logi ("Set env ADSP_LIBRARY_PATH for Qualcomm SoC: %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);

failed = FALSE;

done:

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);
}

return !(failed);
}

#if defined (ENABLE_TENSORFLOW_LITE)
extern void init_filter_tflite (JNIEnv * env, jobject context);
extern void init_filter_tflite (void);
#endif
#if defined (ENABLE_SNAP)
extern void init_filter_snap (void);
Expand All @@ -48,7 +153,7 @@ extern void init_filter_snap (void);
extern void init_filter_nnfw (void);
#endif
#if defined (ENABLE_SNPE)
extern void init_filter_snpe (JNIEnv * env, jobject context);
extern void init_filter_snpe (void);
#endif
#if defined (ENABLE_PYTORCH)
extern void init_filter_torch (void);
Expand Down Expand Up @@ -813,13 +918,20 @@ nnstreamer_native_initialize (JNIEnv * env, jobject context)
#endif /* ENABLE_FLATBUF */
#endif

#if defined (ENABLE_SNPE) || defined (ENABLE_TFLITE_QNN_DELEGATE)
/* some filters require additional tasks */
if (!_qc_android_set_env (env, context)) {
_ml_logw ("Failed to set environment variables for QC Android. Some features may not work properly.");
}
#endif

/* tensor-filter sub-plugins */
init_filter_cpp ();
init_filter_custom ();
init_filter_custom_easy ();

#if defined (ENABLE_TENSORFLOW_LITE)
init_filter_tflite (env, context);
init_filter_tflite ();
#endif
#if defined (ENABLE_SNAP)
init_filter_snap ();
Expand All @@ -828,7 +940,7 @@ nnstreamer_native_initialize (JNIEnv * env, jobject context)
init_filter_nnfw ();
#endif
#if defined (ENABLE_SNPE)
init_filter_snpe (env, context);
init_filter_snpe ();
#endif
#if defined (ENABLE_PYTORCH)
init_filter_torch ();
Expand Down

0 comments on commit b3a2f2c

Please sign in to comment.