1
2 #include <jni.h>
3 #include <android/log.h>
4
5 #if !defined(__GDK__) && !defined(__NOGDK__)
6 #include <bcc/bcc.h>
7 #include <dlfcn.h>
8 #endif // !__GDK__ && !__NOGDK__
9
10 #define LOG_TAG "libjni_photoeditor"
11 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
12 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
13
14 #include "_jni.h"
15
16 #define DEFINE(f) { #f, 0 },
17 JNIFuncType JNIFunc[JNI_max] =
18 {
19 #include "_jnif.h"
20 };
21 #undef DEFINE
22
JNI_OnLoad(JavaVM * vm,void * reserved)23 jint JNI_OnLoad(JavaVM* vm, void* reserved)
24 {
25 LOGI("JNI_OnLoad\n");
26
27 #define DEFINE(f) JNIFunc[ JNI_ ## f ].func_ptr = (void *)f;
28 #include "_jnif.h"
29 #undef DEFINE
30
31 return JNI_VERSION_1_4;
32 }
33
34
35 #if !defined(__GDK__) && !defined(__NOGDK__)
lookupSymbol(void * pContext,const char * name)36 static void* lookupSymbol(void* pContext, const char* name)
37 {
38 return (void*) dlsym(RTLD_DEFAULT, name);
39 }
40 #endif // !__GDK__ && !__NOGDK__
41
Java_com_android_photoeditor_filters_ImageUtils_gdk(JNIEnv * env,jobject obj)42 extern "C" JNIEXPORT jboolean JNICALL Java_com_android_photoeditor_filters_ImageUtils_gdk(JNIEnv *env, jobject obj)
43 {
44 #if !defined(__NOGDK__)
45 return JNI_TRUE;
46 #else
47 return JNI_FALSE;
48 #endif
49 }
50
Java_com_android_photoeditor_filters_ImageUtils_init(JNIEnv * env,jobject obj,jbyteArray scriptRef,jint length)51 extern "C" JNIEXPORT jboolean JNICALL Java_com_android_photoeditor_filters_ImageUtils_init(
52 JNIEnv *env, jobject obj, jbyteArray scriptRef, jint length)
53 {
54 #if !defined(__GDK__) && !defined(__NOGDK__)
55 void *new_func_ptr[JNI_max];
56 int i, all_func_found = 1;
57
58 BCCScriptRef script_ref = bccCreateScript();
59 jbyte* script_ptr = (jbyte *)env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
60 LOGI("BCC Script Len: %d", length);
61 if (bccReadBC(script_ref, "libjni_photoeditor_portable.bc", (const char*)script_ptr, length, 0)) {
62 LOGE("Error! Cannot bccReadBc");
63 return JNI_FALSE;
64 }
65 if (script_ptr) {
66 env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr, 0);
67 }
68 #if 0
69 if (bccLinkFile(script_ref, "/system/lib/libclcore.bc", 0)) {
70 LOGE("Error! Cannot bccLinkBC");
71 return JNI_FALSE;
72 }
73 #endif
74 bccRegisterSymbolCallback(script_ref, lookupSymbol, NULL);
75
76 #ifdef OLD_BCC
77 if (bccPrepareExecutable(script_ref, "/data/data/com.android.photoeditor/photoeditorLLVM.oBCC", 0)) {
78 LOGE("Error! Cannot bccPrepareExecutable");
79 return JNI_FALSE;
80 }
81 #else
82 if (bccPrepareExecutable(script_ref, "/data/data/com.android.photoeditor/", "photoeditorLLVM", 0)) {
83 LOGE("Error! Cannot bccPrepareExecutable");
84 return JNI_FALSE;
85 }
86 #endif // OLD_BCC
87
88 for(i=0; i<JNI_max; i++) {
89 new_func_ptr[i] = bccGetFuncAddr(script_ref, JNIFunc[i].func_name);
90 if (new_func_ptr[i] == NULL) {
91 LOGE("Error! Cannot find %s()\n", JNIFunc[i].func_name);
92 all_func_found = 0;
93 //return JNI_FALSE;
94 } else
95 LOGI("Found %s() @ 0x%x", JNIFunc[i].func_name, (unsigned)new_func_ptr[i]);
96 }
97
98 //bccDisposeScript(script_ref);
99
100 if (all_func_found)
101 {
102 LOGI("Use LLVM version");
103 for(i=0; i<JNI_max; i++)
104 JNIFunc[i].func_ptr = new_func_ptr[i];
105 }
106
107 return JNI_TRUE;
108 #else
109
110 return JNI_FALSE;
111
112 #endif // !__GDK__ && !__NOGDK__
113 }
114
115