1 #include <jni.h>
2 #include <android/log.h>
3 #include <android/bitmap.h>
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <math.h>
8 
9 #include <RenderScript.h>
10 
11 #include "ScriptC_mono.h"
12 
13 #define  LOG_TAG    "HelloComputeNDK"
14 #define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
15 #define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
16 
17 using namespace android::RSC;
18 
19 extern "C" JNIEXPORT void JNICALL
Java_com_example_android_rs_hellocomputendk_HelloComputeNDK_nativeMono(JNIEnv * env,jclass,jstring pathObj,jint X,jint Y,jobject jbitmapIn,jobject jbitmapOut)20 Java_com_example_android_rs_hellocomputendk_HelloComputeNDK_nativeMono(JNIEnv * env,
21                                                                        jclass,
22                                                                        jstring pathObj,
23                                                                        jint X,
24                                                                        jint Y,
25                                                                        jobject jbitmapIn,
26                                                                        jobject jbitmapOut
27                                                                        )
28 {
29 
30     void* inputPtr = nullptr;
31     void* outputPtr = nullptr;
32 
33     AndroidBitmap_lockPixels(env, jbitmapIn, &inputPtr);
34     AndroidBitmap_lockPixels(env, jbitmapOut, &outputPtr);
35 
36     const char * path = env->GetStringUTFChars(pathObj, nullptr);
37     sp<RS> rs = new RS();
38     rs->init(path);
39     env->ReleaseStringUTFChars(pathObj, path);
40 
41     sp<const Element> e = Element::RGBA_8888(rs);
42 
43     sp<const Type> t = Type::create(rs, e, X, Y, 0);
44 
45     sp<Allocation> inputAlloc = Allocation::createTyped(rs, t, RS_ALLOCATION_MIPMAP_NONE,
46                                                         RS_ALLOCATION_USAGE_SHARED | RS_ALLOCATION_USAGE_SCRIPT,
47                                                         inputPtr);
48     sp<Allocation> outputAlloc = Allocation::createTyped(rs, t, RS_ALLOCATION_MIPMAP_NONE,
49                                                          RS_ALLOCATION_USAGE_SHARED | RS_ALLOCATION_USAGE_SCRIPT,
50                                                          outputPtr);
51 
52 
53     inputAlloc->copy2DRangeFrom(0, 0, X, Y, inputPtr);
54     ScriptC_mono* sc = new ScriptC_mono(rs);
55     sc->forEach_root(inputAlloc, outputAlloc);
56     outputAlloc->copy2DRangeTo(0, 0, X, Y, outputPtr);
57 
58 
59     AndroidBitmap_unlockPixels(env, jbitmapIn);
60     AndroidBitmap_unlockPixels(env, jbitmapOut);
61 
62 }
63