jnibranchingfuncalls.cpp revision 7d72c9e5ad10e85789a0bc2e63c3f918c609ae49
1#include <memory>
2
3#include <jni.h>
4#include <RenderScript.h>
5
6#include "ScriptC_scalars.h"
7
8extern "C" void JNICALL
9Java_com_android_rs_jnibranchingfuncalls_MainActivity_nativeRS(
10    JNIEnv * env,
11    jclass,
12    jstring pathObj)
13{
14    static const int size = 64;
15    sp<RS> rs = new RS();
16
17    const char * path = env->GetStringUTFChars(pathObj, nullptr);
18    rs->init(path, RS_INIT_LOW_LATENCY | RS_INIT_WAIT_FOR_ATTACH);
19    env->ReleaseStringUTFChars(pathObj, path);
20
21    auto e = Element::I32(rs);
22    Type::Builder tb(rs, e);
23    tb.setX(size);
24    tb.setY(size);
25    auto t = tb.create();
26
27    auto a = Allocation::createTyped(rs, t);
28    auto b = Allocation::createTyped(rs, t);
29
30    int * input = new int[size*size];
31    for(int i = 0; i < size*size; ++i) {
32        input[i] = i - (size*size / 2);
33    }
34    a->copy2DRangeFrom(0, 0, size, size, input);
35    delete [] input;
36
37    // Script is executed once, then the data is copied back when finished
38    sp<ScriptC_scalars> s = new ScriptC_scalars(rs);
39    s->invoke_addToGlobal(234);
40    s->forEach_simple_kernel(a, b);
41    rs->finish();
42    int32_t * output = new int32_t[size*size];
43    b->copy2DRangeTo(0, 0, size, size, output);
44    delete [] output;
45}
46
47