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