1 /*
2  * Copyright (C) 2015 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 <jni.h>
18 #include <android/log.h>
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <math.h>
23 
24 #include <RenderScript.h>
25 
26 #define  LOG_TAG    "rscpptest"
27 #define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
28 #define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
29 
30 #include "ScriptC_foreach.h"
31 #include "ScriptC_fe_all.h"
32 #include "ScriptC_noroot.h"
33 
34 using namespace android::RSC;
35 
Java_android_cts_rscpp_RSForEachTest_testForEach(JNIEnv * env,jclass obj,jstring pathObj)36 extern "C" JNIEXPORT jboolean JNICALL Java_android_cts_rscpp_RSForEachTest_testForEach(JNIEnv * env,
37                                                                                        jclass obj,
38                                                                                        jstring pathObj)
39 {
40     const char * path = env->GetStringUTFChars(pathObj, NULL);
41     sp<RS> mRS = new RS();
42     mRS->init(path);
43     env->ReleaseStringUTFChars(pathObj, path);
44 
45     bool passed = true;
46     int x = 7;
47 
48     sp<ScriptC_fe_all> fe_all = new ScriptC_fe_all(mRS);
49     sp<const Type> t = Type::create(mRS, Element::I8(mRS), x, 0, 0);
50 
51     // I8
52     sp<Allocation> in = Allocation::createTyped(mRS, t);
53     t = Type::create(mRS, Element::U8(mRS), x, 0, 0);
54     sp<Allocation> out = Allocation::createTyped(mRS, t);
55     fe_all->forEach_test_i8(in, out);
56     mRS->finish();
57 
58     // I8_2
59     t = Type::create(mRS, Element::I8_2(mRS), x, 0, 0);
60     in = Allocation::createTyped(mRS, t);
61     t = Type::create(mRS, Element::U8_2(mRS), x, 0, 0);
62     out = Allocation::createTyped(mRS, t);
63     fe_all->forEach_test_i8_2(in, out);
64     mRS->finish();
65 
66     // I8_3
67     t = Type::create(mRS, Element::I8_3(mRS), x, 0, 0);
68     in = Allocation::createTyped(mRS, t);
69     t = Type::create(mRS, Element::U8_3(mRS), x, 0, 0);
70     out = Allocation::createTyped(mRS, t);
71     fe_all->forEach_test_i8_3(in, out);
72     mRS->finish();
73 
74     // I8_4
75     t = Type::create(mRS, Element::I8_4(mRS), x, 0, 0);
76     in = Allocation::createTyped(mRS, t);
77     t = Type::create(mRS, Element::U8_4(mRS), x, 0, 0);
78     out = Allocation::createTyped(mRS, t);
79     fe_all->forEach_test_i8_4(in, out);
80     mRS->finish();
81 
82     // I16
83     t = Type::create(mRS, Element::I16(mRS), x, 0, 0);
84     in = Allocation::createTyped(mRS, t);
85     t = Type::create(mRS, Element::U16(mRS), x, 0, 0);
86     out = Allocation::createTyped(mRS, t);
87     fe_all->forEach_test_i16(in, out);
88     mRS->finish();
89 
90     // I16_2
91     t = Type::create(mRS, Element::I16_2(mRS), x, 0, 0);
92     in = Allocation::createTyped(mRS, t);
93     t = Type::create(mRS, Element::U16_2(mRS), x, 0, 0);
94     out = Allocation::createTyped(mRS, t);
95     fe_all->forEach_test_i16_2(in, out);
96     mRS->finish();
97 
98     // I16_3
99     t = Type::create(mRS, Element::I16_3(mRS), x, 0, 0);
100     in = Allocation::createTyped(mRS, t);
101     t = Type::create(mRS, Element::U16_3(mRS), x, 0, 0);
102     out = Allocation::createTyped(mRS, t);
103     fe_all->forEach_test_i16_3(in, out);
104     mRS->finish();
105 
106     // I16_4
107     t = Type::create(mRS, Element::I16_4(mRS), x, 0, 0);
108     in = Allocation::createTyped(mRS, t);
109     t = Type::create(mRS, Element::U16_4(mRS), x, 0, 0);
110     out = Allocation::createTyped(mRS, t);
111     fe_all->forEach_test_i16_4(in, out);
112     mRS->finish();
113 
114     // I32
115     t = Type::create(mRS, Element::I32(mRS), x, 0, 0);
116     in = Allocation::createTyped(mRS, t);
117     t = Type::create(mRS, Element::U32(mRS), x, 0, 0);
118     out = Allocation::createTyped(mRS, t);
119     fe_all->forEach_test_i32(in, out);
120     mRS->finish();
121 
122     // I32_2
123     t = Type::create(mRS, Element::I32_2(mRS), x, 0, 0);
124     in = Allocation::createTyped(mRS, t);
125     t = Type::create(mRS, Element::U32_2(mRS), x, 0, 0);
126     out = Allocation::createTyped(mRS, t);
127     fe_all->forEach_test_i32_2(in, out);
128     mRS->finish();
129 
130     // I32_3
131     t = Type::create(mRS, Element::I32_3(mRS), x, 0, 0);
132     in = Allocation::createTyped(mRS, t);
133     t = Type::create(mRS, Element::U32_3(mRS), x, 0, 0);
134     out = Allocation::createTyped(mRS, t);
135     fe_all->forEach_test_i32_3(in, out);
136     mRS->finish();
137 
138     // I32_4
139     t = Type::create(mRS, Element::I32_4(mRS), x, 0, 0);
140     in = Allocation::createTyped(mRS, t);
141     t = Type::create(mRS, Element::U32_4(mRS), x, 0, 0);
142     out = Allocation::createTyped(mRS, t);
143     fe_all->forEach_test_i32_4(in, out);
144     mRS->finish();
145 
146     // I64
147     t = Type::create(mRS, Element::I64(mRS), x, 0, 0);
148     in = Allocation::createTyped(mRS, t);
149     t = Type::create(mRS, Element::U64(mRS), x, 0, 0);
150     out = Allocation::createTyped(mRS, t);
151     fe_all->forEach_test_i64(in, out);
152     mRS->finish();
153 
154     // I64_2
155     t = Type::create(mRS, Element::I64_2(mRS), x, 0, 0);
156     in = Allocation::createTyped(mRS, t);
157     t = Type::create(mRS, Element::U64_2(mRS), x, 0, 0);
158     out = Allocation::createTyped(mRS, t);
159     fe_all->forEach_test_i64_2(in, out);
160     mRS->finish();
161 
162     // I64_3
163     t = Type::create(mRS, Element::I64_3(mRS), x, 0, 0);
164     in = Allocation::createTyped(mRS, t);
165     t = Type::create(mRS, Element::U64_3(mRS), x, 0, 0);
166     out = Allocation::createTyped(mRS, t);
167     fe_all->forEach_test_i64_3(in, out);
168     mRS->finish();
169 
170     // I64_4
171     t = Type::create(mRS, Element::I64_4(mRS), x, 0, 0);
172     in = Allocation::createTyped(mRS, t);
173     t = Type::create(mRS, Element::U64_4(mRS), x, 0, 0);
174     out = Allocation::createTyped(mRS, t);
175     fe_all->forEach_test_i64_4(in, out);
176     mRS->finish();
177 
178     // F32
179     t = Type::create(mRS, Element::F32(mRS), x, 0, 0);
180     in = Allocation::createTyped(mRS, t);
181     out = Allocation::createTyped(mRS, t);
182     fe_all->forEach_test_f32(in, out);
183     mRS->finish();
184 
185     // F32_2
186     t = Type::create(mRS, Element::F32_2(mRS), x, 0, 0);
187     in = Allocation::createTyped(mRS, t);
188     t = Type::create(mRS, Element::F32_2(mRS), x, 0, 0);
189     out = Allocation::createTyped(mRS, t);
190     fe_all->forEach_test_f32_2(in, out);
191     mRS->finish();
192 
193     // F32_3
194     t = Type::create(mRS, Element::F32_3(mRS), x, 0, 0);
195     in = Allocation::createTyped(mRS, t);
196     out = Allocation::createTyped(mRS, t);
197     fe_all->forEach_test_f32_3(in, out);
198     mRS->finish();
199 
200     // F32_4
201     t = Type::create(mRS, Element::F32_4(mRS), x, 0, 0);
202     in = Allocation::createTyped(mRS, t);
203     out = Allocation::createTyped(mRS, t);
204     fe_all->forEach_test_f32_4(in, out);
205     mRS->finish();
206 
207     // F64
208     t = Type::create(mRS, Element::F64(mRS), x, 0, 0);
209     in = Allocation::createTyped(mRS, t);
210     out = Allocation::createTyped(mRS, t);
211     fe_all->forEach_test_f64(in, out);
212     mRS->finish();
213 
214     // F64_2
215     t = Type::create(mRS, Element::F64_2(mRS), x, 0, 0);
216     in = Allocation::createTyped(mRS, t);
217     out = Allocation::createTyped(mRS, t);
218     fe_all->forEach_test_f64_2(in, out);
219     mRS->finish();
220 
221     // F64_3
222     t = Type::create(mRS, Element::F64_3(mRS), x, 0, 0);
223     in = Allocation::createTyped(mRS, t);
224     out = Allocation::createTyped(mRS, t);
225     fe_all->forEach_test_f64_3(in, out);
226     mRS->finish();
227 
228     // F64_4
229     t = Type::create(mRS, Element::F64_4(mRS), x, 0, 0);
230     in = Allocation::createTyped(mRS, t);
231     out = Allocation::createTyped(mRS, t);
232     fe_all->forEach_test_f64_4(in, out);
233     mRS->finish();
234 
235     // BOOLEAN
236     t = Type::create(mRS, Element::BOOLEAN(mRS), x, 0, 0);
237     in = Allocation::createTyped(mRS, t);
238     out = Allocation::createTyped(mRS, t);
239     fe_all->forEach_test_bool(in, out);
240     mRS->finish();
241 
242     // A_8
243     t = Type::create(mRS, Element::I8(mRS), x, 0, 0);
244     in = Allocation::createTyped(mRS, t);
245     t = Type::create(mRS, Element::A_8(mRS), x, 0, 0);
246     out = Allocation::createTyped(mRS, t);
247     fe_all->forEach_test_i8(in, out);
248     mRS->finish();
249 
250     // RGBA_8888
251     t = Type::create(mRS, Element::I8_4(mRS), x, 0, 0);
252     in = Allocation::createTyped(mRS, t);
253     t = Type::create(mRS, Element::RGBA_8888(mRS), x, 0, 0);
254     out = Allocation::createTyped(mRS, t);
255     fe_all->forEach_test_i8_4(in, out);
256     mRS->finish();
257 
258     // RGB_888
259     t = Type::create(mRS, Element::I8_3(mRS), x, 0, 0);
260     in = Allocation::createTyped(mRS, t);
261     t = Type::create(mRS, Element::RGB_888(mRS), x, 0, 0);
262     out = Allocation::createTyped(mRS, t);
263     fe_all->forEach_test_i8_3(in, out);
264     mRS->finish();
265 
266     return passed;
267 }
268 
269 #define RS_MSG_TEST_PASSED 100
270 #define RS_MSG_TEST_FAILED 101
271 
272 static int result = 0;
rsMsgHandler(uint32_t msgNum,const void * msgData,size_t msgLen)273 static void rsMsgHandler(uint32_t msgNum, const void *msgData, size_t msgLen) {
274     if (result == 0) {
275         result = msgNum;
276     }
277 }
278 
Java_android_cts_rscpp_RSForEachTest_testMultipleForEach(JNIEnv * env,jclass obj,jstring pathObj)279 extern "C" JNIEXPORT jboolean JNICALL Java_android_cts_rscpp_RSForEachTest_testMultipleForEach(JNIEnv * env,
280                                                                                                jclass obj,
281                                                                                                jstring pathObj)
282 {
283     const char * path = env->GetStringUTFChars(pathObj, NULL);
284     sp<RS> mRS = new RS();
285     mRS->init(path);
286     env->ReleaseStringUTFChars(pathObj, path);
287     MessageHandlerFunc_t mHandler = rsMsgHandler;
288     mRS->setMessageHandler(mHandler);
289 
290     bool passed = true;
291     sp<ScriptC_foreach> s = new ScriptC_foreach(mRS);
292 
293     int X = 5;
294     int Y = 7;
295     s->set_dimX(X);
296     s->set_dimY(Y);
297     sp<const Type> t = Type::create(mRS, Element::I32(mRS), X, Y, 0);
298     sp<Allocation> A = Allocation::createTyped(mRS, t);
299     s->set_aRaw(A);
300     s->forEach_root(A);
301     s->invoke_verify_root();
302     s->forEach_foo(A, A);
303     s->invoke_verify_foo();
304     s->invoke_foreach_test();
305     mRS->finish();
306     if (result == RS_MSG_TEST_FAILED) {
307         passed = false;
308     }
309     result = 0;
310     return passed;
311 }
312 
Java_android_cts_rscpp_RSForEachTest_testNoRoot(JNIEnv * env,jclass obj,jstring pathObj)313 extern "C" JNIEXPORT jboolean JNICALL Java_android_cts_rscpp_RSForEachTest_testNoRoot(JNIEnv * env,
314                                                                                       jclass obj,
315                                                                                       jstring pathObj)
316 {
317     const char * path = env->GetStringUTFChars(pathObj, NULL);
318     sp<RS> mRS = new RS();
319     mRS->init(path);
320     env->ReleaseStringUTFChars(pathObj, path);
321     MessageHandlerFunc_t mHandler = rsMsgHandler;
322     mRS->setMessageHandler(mHandler);
323 
324     bool passed = true;
325     sp<ScriptC_noroot> s = new ScriptC_noroot(mRS);
326 
327     int X = 5;
328     int Y = 7;
329     s->set_dimX(X);
330     s->set_dimY(Y);
331     sp<const Type> t = Type::create(mRS, Element::I32(mRS), X, Y, 0);
332     sp<Allocation> A = Allocation::createTyped(mRS, t);
333     s->set_aRaw(A);
334     s->forEach_foo(A, A);
335     s->invoke_verify_foo();
336     s->invoke_noroot_test();
337     mRS->finish();
338     if (result == RS_MSG_TEST_FAILED) {
339         passed = false;
340     }
341     result = 0;
342     return passed;
343 }
344 
345