summaryrefslogtreecommitdiff
path: root/samples/PhotoEditor/jni/_jni.cpp
blob: a5935bbff43aa8d01f4a0940497e434aa90af9e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

#include <jni.h>
#include <android/log.h>

#if !defined(__GDK__) && !defined(__NOGDK__)
#include <bcc/bcc.h>
#include <dlfcn.h>
#endif // !__GDK__ && !__NOGDK__

#define LOG_TAG "libjni_photoeditor"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)

#include "_jni.h"

#define DEFINE(f) { #f, 0 },
JNIFuncType JNIFunc[JNI_max] =
{
#include "_jnif.h"
};
#undef DEFINE

jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
    LOGI("JNI_OnLoad\n");
   
#define DEFINE(f) JNIFunc[ JNI_ ## f ].func_ptr = (void *)f;
   #include "_jnif.h"
#undef DEFINE   

    return JNI_VERSION_1_4;
}


#if !defined(__GDK__) && !defined(__NOGDK__)
static void* lookupSymbol(void* pContext, const char* name)
{
    return (void*) dlsym(RTLD_DEFAULT, name);
}
#endif // !__GDK__ && !__NOGDK__

extern "C" JNIEXPORT jboolean JNICALL Java_com_android_photoeditor_filters_ImageUtils_gdk(JNIEnv *env, jobject obj)
{
#if !defined(__NOGDK__)
   return JNI_TRUE;
#else
   return JNI_FALSE;
#endif
}

extern "C" JNIEXPORT jboolean JNICALL Java_com_android_photoeditor_filters_ImageUtils_init(
    JNIEnv *env, jobject obj, jbyteArray scriptRef, jint length)
{   
#if !defined(__GDK__) && !defined(__NOGDK__)
    void *new_func_ptr[JNI_max];
    int i, all_func_found = 1;

    BCCScriptRef script_ref = bccCreateScript();
    jbyte* script_ptr = (jbyte *)env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
    LOGI("BCC Script Len: %d", length);
    if (bccReadBC(script_ref, "libjni_photoeditor_portable.bc", (const char*)script_ptr, length, 0)) {
        LOGE("Error! Cannot bccReadBc");
        return JNI_FALSE;
    }
    if (script_ptr) {
        env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr, 0);
    }
  #if 0
    if (bccLinkFile(script_ref, "/system/lib/libclcore.bc", 0)) {
        LOGE("Error! Cannot bccLinkBC");
        return JNI_FALSE;
    }
  #endif
    bccRegisterSymbolCallback(script_ref, lookupSymbol, NULL);

  #ifdef OLD_BCC
    if (bccPrepareExecutable(script_ref, "/data/data/com.android.photoeditor/photoeditorLLVM.oBCC", 0)) {
        LOGE("Error! Cannot bccPrepareExecutable");
        return JNI_FALSE;
    }
  #else
    if (bccPrepareExecutable(script_ref, "/data/data/com.android.photoeditor/", "photoeditorLLVM", 0)) {
        LOGE("Error! Cannot bccPrepareExecutable");
        return JNI_FALSE;
    }
  #endif // OLD_BCC

    for(i=0; i<JNI_max; i++) {
        new_func_ptr[i] = bccGetFuncAddr(script_ref, JNIFunc[i].func_name);
        if (new_func_ptr[i] == NULL) {
	    LOGE("Error! Cannot find %s()\n", JNIFunc[i].func_name);
	    all_func_found = 0;
          //return JNI_FALSE;
	} else 
            LOGI("Found %s() @ 0x%x", JNIFunc[i].func_name, (unsigned)new_func_ptr[i]);
    }

   //bccDisposeScript(script_ref);

    if (all_func_found)
    {
        LOGI("Use LLVM version");
        for(i=0; i<JNI_max; i++)
	     JNIFunc[i].func_ptr = new_func_ptr[i];
    }

    return JNI_TRUE;
#else

    return JNI_FALSE;

#endif // !__GDK__ && !__NOGDK__
}