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_primitives.h>
31 #include <ScriptC_instance.h>
32 #include <ScriptC_vector.h>
33
34 using namespace android::RSC;
35
36 #define RS_MSG_TEST_PASSED 100
37 #define RS_MSG_TEST_FAILED 101
38
39 static int result = 0;
rsMsgHandler(uint32_t msgNum,const void * msgData,size_t msgLen)40 static void rsMsgHandler(uint32_t msgNum, const void *msgData, size_t msgLen) {
41 if (result == 0) {
42 result = msgNum;
43 }
44 }
45
Java_android_cts_rscpp_RSScriptTest_testSet(JNIEnv * env,jclass obj,jstring pathObj)46 extern "C" JNIEXPORT jboolean JNICALL Java_android_cts_rscpp_RSScriptTest_testSet(JNIEnv * env,
47 jclass obj,
48 jstring pathObj)
49 {
50 const char * path = env->GetStringUTFChars(pathObj, nullptr);
51 sp<RS> mRS = new RS();
52 mRS->init(path);
53 env->ReleaseStringUTFChars(pathObj, path);
54 MessageHandlerFunc_t mHandler = rsMsgHandler;
55 mRS->setMessageHandler(mHandler);
56
57 bool passed = true;
58
59 sp<const Type> t = Type::create(mRS, Element::I32(mRS), 8, 0, 0);
60 sp<Allocation> alloc = Allocation::createTyped(mRS, t);
61
62 sp<ScriptC_primitives> script = new ScriptC_primitives(mRS);
63 script->set_floatTest(2.99f); // floatTest
64 script->set_doubleTest(3.05); // doubleTest
65 script->set_charTest(-16); // charTest
66 script->set_shortTest(-32); // shortTest
67 script->set_intTest(-64); // intTest
68 script->set_longTest(17179869185L); // longTest
69 script->set_longlongTest(68719476735L); //longlongTest
70 script->set_ulongTest(4611686018427387903L); // boolTest
71 script->set_uint64_tTest(117179869185L); //uint64_tTest
72 script->set_allocationTest(alloc); // allocationTest
73
74 script->invoke_test_primitive_types();
75 mRS->finish();
76 if (result == RS_MSG_TEST_FAILED) {
77 passed = false;
78 }
79
80 return passed;
81 }
82
Java_android_cts_rscpp_RSScriptTest_testInstance(JNIEnv * env,jclass obj,jstring pathObj)83 extern "C" JNIEXPORT jboolean JNICALL Java_android_cts_rscpp_RSScriptTest_testInstance(JNIEnv * env,
84 jclass obj,
85 jstring pathObj)
86 {
87 /**
88 * Test script instancing.
89 */
90 const char * path = env->GetStringUTFChars(pathObj, nullptr);
91 sp<RS> mRS = new RS();
92 mRS->init(path);
93 env->ReleaseStringUTFChars(pathObj, path);
94 MessageHandlerFunc_t mHandler = rsMsgHandler;
95 mRS->setMessageHandler(mHandler);
96
97 bool passed = true;
98
99 sp<const Type> t = Type::create(mRS, Element::I32(mRS), 1, 0, 0);
100 sp<Allocation> ai1 = Allocation::createTyped(mRS, t);
101 sp<Allocation> ai2 = Allocation::createTyped(mRS, t);
102 sp<ScriptC_instance> instance_1 = new ScriptC_instance(mRS);
103 sp<ScriptC_instance> instance_2 = new ScriptC_instance(mRS);
104
105 instance_1->set_i(1);
106 instance_2->set_i(2);
107 instance_1->set_ai(ai1);
108 instance_2->set_ai(ai2);
109
110 // We now check to ensure that the global is not being shared across
111 // our separate script instances. Our invoke here merely sets the
112 // instanced allocation with the instanced global variable's value.
113 // If globals are being shared (i.e. not instancing scripts), then
114 // both instanced allocations will have the same resulting value
115 // (depending on the order in which the invokes complete).
116 instance_1->invoke_instance_test();
117 instance_2->invoke_instance_test();
118
119 int i1[1];
120 int i2[1];
121
122 ai1->copy1DTo(i1);
123 ai2->copy1DTo(i2);
124
125 // 3-step check ensures that a fortunate race condition wouldn't let us
126 // pass accidentally.
127 passed &= (2 == i2[0]);
128 passed &= (1 == i1[0]);
129 passed &= (2 == i2[0]);
130 mRS->finish();
131 if (result == RS_MSG_TEST_FAILED) {
132 passed = false;
133 }
134
135 return passed;
136 }
137
138 // Define some reasonable types for use with the vector invoke testing.
139 typedef unsigned char uchar;
140 typedef unsigned short ushort;
141 typedef unsigned int uint;
142 typedef unsigned long ulong;
143
144 #define TEST_VECTOR_INVOKE(L, U) \
145 L temp_##L = 0; \
146 vector->invoke_vector_test_##L(temp_##L); \
147 U##2 temp_##L##2; \
148 vector->invoke_vector_test_##L##2(temp_##L##2); \
149 U##3 temp_##L##3; \
150 vector->invoke_vector_test_##L##3(temp_##L##3); \
151 U##4 temp_##L##4; \
152 vector->invoke_vector_test_##L##4(temp_##L##4);
153
154
155 /*
156 * Test that vector invoke C++ reflection is working/present.
157 */
Java_android_cts_rscpp_RSScriptTest_testVector(JNIEnv * env,jclass obj,jstring pathObj)158 extern "C" JNIEXPORT jboolean JNICALL Java_android_cts_rscpp_RSScriptTest_testVector(JNIEnv * env,
159 jclass obj,
160 jstring pathObj)
161 {
162 const char * path = env->GetStringUTFChars(pathObj, nullptr);
163 sp<RS> mRS = new RS();
164 mRS->init(path);
165 env->ReleaseStringUTFChars(pathObj, path);
166 MessageHandlerFunc_t mHandler = rsMsgHandler;
167 mRS->setMessageHandler(mHandler);
168
169 bool passed = true;
170 sp<ScriptC_vector> vector = new ScriptC_vector(mRS);
171
172 TEST_VECTOR_INVOKE(float, Float)
173 TEST_VECTOR_INVOKE(double, Double)
174 TEST_VECTOR_INVOKE(char, Byte)
175 TEST_VECTOR_INVOKE(uchar, UByte)
176 TEST_VECTOR_INVOKE(short, Short)
177 TEST_VECTOR_INVOKE(ushort, UShort)
178 TEST_VECTOR_INVOKE(int, Int)
179 TEST_VECTOR_INVOKE(uint, UInt)
180 TEST_VECTOR_INVOKE(long, Long)
181 TEST_VECTOR_INVOKE(ulong, ULong)
182
183 return passed;
184 }
185