1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 
17 #include <memory>
18 
19 #include <jni.h>
20 #include <RenderScript.h>
21 
22 #include "ScriptC_scalars.h"
23 
24 extern "C" void JNICALL
Java_com_android_rs_jnibranchingfuncalls_MainActivity_nativeRS(JNIEnv * env,jclass,jstring pathObj)25 Java_com_android_rs_jnibranchingfuncalls_MainActivity_nativeRS(
26     JNIEnv * env,
27     jclass,
28     jstring pathObj)
29 {
30     static const int size = 64;
31     sp<RS> rs = new RS();
32 
33     const char * path = env->GetStringUTFChars(pathObj, nullptr);
34     rs->init(path, RS_INIT_LOW_LATENCY | RS_INIT_WAIT_FOR_ATTACH);
35     env->ReleaseStringUTFChars(pathObj, path);
36 
37     auto e = Element::I32(rs);
38     Type::Builder tb(rs, e);
39     tb.setX(size);
40     tb.setY(size);
41     auto t = tb.create();
42 
43     auto a = Allocation::createTyped(rs, t);
44     auto b = Allocation::createTyped(rs, t);
45 
46     int * input = new int[size*size];
47     for(int i = 0; i < size*size; ++i) {
48         input[i] = i - (size*size / 2);
49     }
50     a->copy2DRangeFrom(0, 0, size, size, input);
51     delete [] input;
52 
53     // Script is executed once, then the data is copied back when finished
54     sp<ScriptC_scalars> s = new ScriptC_scalars(rs);
55     s->invoke_addToGlobal(234);
56     s->forEach_simple_kernel(a, b);
57     rs->finish();
58     int32_t * output = new int32_t[size*size];
59     b->copy2DRangeTo(0, 0, size, size, output);
60     delete [] output;
61 }
62 
63