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