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