blob: fed7c6eb922077655642804aa065f877cc4ae865 [file] [log] [blame]
Michael Butler2d4d6d92017-03-01 15:32:30 -08001/*
2 * Copyright (C) 2017 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 "VtsHalRenderscriptV1_0TargetTest.h"
18
19/*
20 * Create a Blur intrinsic with scriptIntrinsicCreate, and call
21 * scriptSetTimeZone to make sure it is not crashing.
22 *
23 * Calls: elementCreate, scriptIntrinsicCreate, scriptSetTimeZone
24 */
25TEST_F(RenderscriptHidlTest, IntrinsicTest) {
26 // uint8
27 Element element = context->elementCreate(DataType::UNSIGNED_8, DataKind::USER, false, 1);
Michael Butlerb2039ad2017-03-30 17:20:12 -070028 EXPECT_NE(Element(0), element);
29
Michael Butler2d4d6d92017-03-01 15:32:30 -080030 Script script = context->scriptIntrinsicCreate(ScriptIntrinsicID::ID_BLUR, element);
31 EXPECT_NE(Script(0), script);
32
33 context->scriptSetTimeZone(script, "UTF-8");
34}
35
36/*
37 * Create a user script “struct_test”, and verified the setters and getters work
38 * for the global variables.
39 *
40 * Calls: scriptCCreate, scriptGetVarV, scriptSetVarI, scriptSetVarJ,
41 * scriptSetVarF, scriptSetVarD, elementCreate, typeCreate,
42 * allocationCreateTyped, scriptSetVarObj, scriptSetVarV, scriptSetVarVE
43 */
44TEST_F(RenderscriptHidlTest, ScriptVarTest) {
45 hidl_vec<uint8_t> bitcode;
46 bitcode.setToExternal((uint8_t*)bitCode, bitCodeLength);
47 Script script = context->scriptCCreate("struct_test", "/data/local/tmp/", bitcode);
Michael Butlerb2039ad2017-03-30 17:20:12 -070048 ASSERT_NE(Script(0), script);
Michael Butler2d4d6d92017-03-01 15:32:30 -080049
50 // arg tests
Michael Butler54fa8d42017-03-17 13:02:53 -070051 context->scriptSetVarI(script, mExportVarIdx_var_int, 100);
Michael Butler2d4d6d92017-03-01 15:32:30 -080052 int resultI = 0;
Michael Butler54fa8d42017-03-17 13:02:53 -070053 context->scriptGetVarV(script, mExportVarIdx_var_int, sizeof(int),
54 [&](const hidl_vec<uint8_t>& _data){ resultI = *((int*)_data.data()); });
Michael Butler2d4d6d92017-03-01 15:32:30 -080055 EXPECT_EQ(100, resultI);
56
Michael Butler54fa8d42017-03-17 13:02:53 -070057 context->scriptSetVarJ(script, mExportVarIdx_var_long, 101l);
Michael Butler2d4d6d92017-03-01 15:32:30 -080058 int resultJ = 0;
Michael Butler54fa8d42017-03-17 13:02:53 -070059 context->scriptGetVarV(script, mExportVarIdx_var_long, sizeof(long),
60 [&](const hidl_vec<uint8_t>& _data){
Michael Butler2d4d6d92017-03-01 15:32:30 -080061 resultJ = *((long*)_data.data()); });
Michael Butler54fa8d42017-03-17 13:02:53 -070062 EXPECT_EQ(101l, resultJ);
Michael Butler2d4d6d92017-03-01 15:32:30 -080063
Michael Butler54fa8d42017-03-17 13:02:53 -070064 context->scriptSetVarF(script, mExportVarIdx_var_float, 102.0f);
Michael Butler2d4d6d92017-03-01 15:32:30 -080065 int resultF = 0.0f;
Michael Butler54fa8d42017-03-17 13:02:53 -070066 context->scriptGetVarV(script, mExportVarIdx_var_float, sizeof(float),
67 [&](const hidl_vec<uint8_t>& _data){
Michael Butler2d4d6d92017-03-01 15:32:30 -080068 resultF = *((float*)_data.data()); });
69 EXPECT_EQ(102.0f, resultF);
70
Michael Butler54fa8d42017-03-17 13:02:53 -070071 context->scriptSetVarD(script, mExportVarIdx_var_double, 103.0);
Michael Butler2d4d6d92017-03-01 15:32:30 -080072 int resultD = 0.0;
Michael Butler54fa8d42017-03-17 13:02:53 -070073 context->scriptGetVarV(script, mExportVarIdx_var_double, sizeof(double),
74 [&](const hidl_vec<uint8_t>& _data){
Michael Butler2d4d6d92017-03-01 15:32:30 -080075 resultD = *((double*)_data.data()); });
76 EXPECT_EQ(103.0, resultD);
77
78 // float1
79 Element element = context->elementCreate(DataType::FLOAT_32, DataKind::USER, false, 1);
Michael Butlerb2039ad2017-03-30 17:20:12 -070080 ASSERT_NE(Element(0), element);
81
Michael Butler2d4d6d92017-03-01 15:32:30 -080082 // 128 x float1
83 Type type = context->typeCreate(element, 128, 0, 0, false, false, YuvFormat::YUV_NONE);
Michael Butlerb2039ad2017-03-30 17:20:12 -070084 ASSERT_NE(Type(0), type);
85
Michael Butler2d4d6d92017-03-01 15:32:30 -080086 // 128 x float1
87 Allocation allocationIn = context->allocationCreateTyped(type, AllocationMipmapControl::NONE,
88 (int)AllocationUsageType::SCRIPT,
89 (Ptr)nullptr);
Michael Butlerb2039ad2017-03-30 17:20:12 -070090 ASSERT_NE(Allocation(0), allocationIn);
91
Michael Butler2d4d6d92017-03-01 15:32:30 -080092 Allocation allocationOut = Allocation(0);
Michael Butler54fa8d42017-03-17 13:02:53 -070093 context->scriptSetVarObj(script, mExportVarIdx_var_allocation, (ObjectBase)allocationIn);
94 context->scriptGetVarV(script, mExportVarIdx_var_allocation, sizeof(ObjectBase),
95 [&](const hidl_vec<uint8_t>& _data){
Michael Butler2d4d6d92017-03-01 15:32:30 -080096 allocationOut = (Allocation) *((ObjectBase*)_data.data()); });
97 EXPECT_EQ(allocationOut, allocationIn);
98
Michael Butler54fa8d42017-03-17 13:02:53 -070099 uint32_t valueV = 104u;
100 hidl_vec<uint8_t> _dataV;
101 _dataV.setToExternal((uint8_t*)&valueV, sizeof(uint32_t));
102 context->scriptSetVarV(script, mExportVarIdx_var_uint32_t, _dataV);
103 uint32_t resultV = 0u;
104 context->scriptGetVarV(script, mExportVarIdx_var_uint32_t, sizeof(uint32_t),
105 [&](const hidl_vec<uint8_t>& _data){
106 resultV = *((uint32_t*)_data.data()); });
107 EXPECT_EQ(104u, resultV);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800108
109 std::vector<int> dataVE = {1000, 1001};
110 std::vector<uint32_t> dimsVE = {1};
111 std::vector<int> outVE(2);
112 hidl_vec<uint8_t> _dataVE;
113 hidl_vec<uint32_t> _dimsVE;
114 _dataVE.setToExternal((uint8_t*)dataVE.data(), dataVE.size()*sizeof(int));
115 _dimsVE.setToExternal((uint32_t*)dimsVE.data(), dimsVE.size());
Michael Butler54fa8d42017-03-17 13:02:53 -0700116 // intx2 to represent point2 which is {int, int}
Michael Butler2d4d6d92017-03-01 15:32:30 -0800117 Element elementVE = context->elementCreate(DataType::SIGNED_32, DataKind::USER, false, 2);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700118 ASSERT_NE(Element(0), elementVE);
119
Michael Butler54fa8d42017-03-17 13:02:53 -0700120 context->scriptSetVarVE(script, mExportVarIdx_var_point2, _dataVE, elementVE, _dimsVE);
121 context->scriptGetVarV(script, mExportVarIdx_var_point2, 2*sizeof(int),
122 [&](const hidl_vec<uint8_t>& _data){
123 outVE = std::vector<int>(
124 (int*)_data.data(), (int*)_data.data() + 2); });
Michael Butler2d4d6d92017-03-01 15:32:30 -0800125 EXPECT_EQ(1000, outVE[0]);
126 EXPECT_EQ(1001, outVE[1]);
127}
128
129/*
130 * Create a user script “struct_test”, and input and output Allocations.
131 * Verified the foreach launch correctly for the invoke kernel.
132 *
133 * Calls: scriptCCreate, scriptInvoke, scriptGetVarV, scriptInvokeV
134 */
135TEST_F(RenderscriptHidlTest, ScriptInvokeTest) {
136 hidl_vec<uint8_t> bitcode;
137 bitcode.setToExternal((uint8_t*)bitCode, bitCodeLength);
138 Script script = context->scriptCCreate("struct_test", "/data/local/tmp/", bitcode);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700139 ASSERT_NE(Script(0), script);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800140
141 // invoke test
Michael Butler54fa8d42017-03-17 13:02:53 -0700142 int resultI = 0;
143 long resultJ = 0l;
144 float resultF = 0.0f;
145 double resultD = 0.0;
146 uint32_t resultV = 0u;
147 std::vector<int> resultVE(2);
148 context->scriptInvoke(script, mExportFuncIdx_function);
149 context->scriptGetVarV(script, mExportVarIdx_var_int, sizeof(int),
150 [&](const hidl_vec<uint8_t>& _data){ resultI = *((int*)_data.data()); });
151 context->scriptGetVarV(script, mExportVarIdx_var_long, sizeof(long),
152 [&](const hidl_vec<uint8_t>& _data){
153 resultJ = *((long*)_data.data()); });
154 context->scriptGetVarV(script, mExportVarIdx_var_float, sizeof(float),
155 [&](const hidl_vec<uint8_t>& _data){
156 resultF = *((float*)_data.data()); });
157 context->scriptGetVarV(script, mExportVarIdx_var_double, sizeof(double),
158 [&](const hidl_vec<uint8_t>& _data){
159 resultD = *((double*)_data.data()); });
160 context->scriptGetVarV(script, mExportVarIdx_var_uint32_t, sizeof(uint32_t),
161 [&](const hidl_vec<uint8_t>& _data){
162 resultV = *((uint32_t*)_data.data()); });
163 context->scriptGetVarV(script, mExportVarIdx_var_point2, 2*sizeof(int),
164 [&](const hidl_vec<uint8_t>& _data){
165 resultVE = std::vector<int>(
166 (int*)_data.data(), (int*)_data.data() + 2); });
167 EXPECT_EQ(1, resultI);
168 EXPECT_EQ(2l, resultJ);
169 EXPECT_EQ(3.0f, resultF);
170 EXPECT_EQ(4.0, resultD);
171 EXPECT_EQ(5u, resultV);
172 EXPECT_EQ(6, resultVE[0]);
173 EXPECT_EQ(7, resultVE[1]);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800174
175 // invokeV test
176 int functionV_arg = 5;
177 int functionV_res = 0;
178 hidl_vec<uint8_t> functionV_data;
179 functionV_data.setToExternal((uint8_t*)&functionV_arg, sizeof(int));
Michael Butler54fa8d42017-03-17 13:02:53 -0700180 context->scriptInvokeV(script, mExportFuncIdx_functionV, functionV_data);
181 context->scriptGetVarV(script, mExportVarIdx_var_int, sizeof(int),
182 [&](const hidl_vec<uint8_t>& _data){
Michael Butler2d4d6d92017-03-01 15:32:30 -0800183 functionV_res = *((int*)_data.data()); });
184 EXPECT_EQ(5, functionV_res);
185}
186
187/*
188 * Create a user script “struct_test”, and input and output Allocations.
189 * Verified the foreach launch correctly for the foreach kernel.
190 *
191 * Calls: scriptCCreate, elementCreate, typeCreate, allocationCreateTyped,
192 * allocation1DWrite, scriptForEach, allocationRead
193 */
194TEST_F(RenderscriptHidlTest, ScriptForEachTest) {
195 hidl_vec<uint8_t> bitcode;
196 bitcode.setToExternal((uint8_t*)bitCode, bitCodeLength);
197 Script script = context->scriptCCreate("struct_test", "/data/local/tmp/", bitcode);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700198 ASSERT_NE(Script(0), script);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800199
200 // uint8_t
201 Element element = context->elementCreate(DataType::UNSIGNED_8, DataKind::USER, false, 1);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700202 ASSERT_NE(Element(0), element);
203
Michael Butler2d4d6d92017-03-01 15:32:30 -0800204 // 64 x uint8_t
205 Type type = context->typeCreate(element, 64, 0, 0, false, false, YuvFormat::YUV_NONE);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700206 ASSERT_NE(Type(0), type);
207
Michael Butler54fa8d42017-03-17 13:02:53 -0700208 std::vector<uint8_t> dataIn(64), dataOut(64), expected(64);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800209 std::generate(dataIn.begin(), dataIn.end(), [](){ static uint8_t val = 0; return val++; });
Michael Butler54fa8d42017-03-17 13:02:53 -0700210 std::generate(expected.begin(), expected.end(), [](){ static uint8_t val = 1; return val++; });
Michael Butler2d4d6d92017-03-01 15:32:30 -0800211 hidl_vec<uint8_t> _data;
212 _data.setToExternal((uint8_t*)dataIn.data(), dataIn.size());
213 // 64 x float1
214 Allocation allocation = context->allocationCreateTyped(type, AllocationMipmapControl::NONE,
215 (int)AllocationUsageType::SCRIPT,
216 (Ptr)nullptr);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700217 ASSERT_NE(Allocation(0), allocation);
218
Michael Butler2d4d6d92017-03-01 15:32:30 -0800219 Allocation vout = context->allocationCreateTyped(type, AllocationMipmapControl::NONE,
220 (int)AllocationUsageType::SCRIPT,
221 (Ptr)nullptr);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700222 ASSERT_NE(Allocation(0), vout);
223
Michael Butler2d4d6d92017-03-01 15:32:30 -0800224 context->allocation1DWrite(allocation, 0, 0, (Size)dataIn.size(), _data);
225 hidl_vec<Allocation> vains;
226 vains.setToExternal(&allocation, 1);
227 hidl_vec<uint8_t> params;
Michael Butler54fa8d42017-03-17 13:02:53 -0700228 context->scriptForEach(script, mExportForEachIdx_increment, vains, vout, params, nullptr);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800229 context->allocationRead(vout, (Ptr)dataOut.data(), (Size)dataOut.size()*sizeof(uint8_t));
Michael Butler54fa8d42017-03-17 13:02:53 -0700230 EXPECT_EQ(expected, dataOut);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800231}
232
233/*
234 * Create a user script “struct_test”, and input and output Allocations.
235 * Verified the foreach launch correctly for the reduction kernel.
236 *
237 * Calls: scriptCCreate, elementCreate, typeCreate, allocationCreateTyped,
238 * allocation1DWrite, scriptReduce, contextFinish, allocationRead
239 */
240TEST_F(RenderscriptHidlTest, ScriptReduceTest) {
241 hidl_vec<uint8_t> bitcode;
242 bitcode.setToExternal((uint8_t*)bitCode, bitCodeLength);
243 Script script = context->scriptCCreate("struct_test", "/data/local/tmp/", bitcode);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700244 ASSERT_NE(Script(0), script);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800245
246 // uint8_t
247 Element element = context->elementCreate(DataType::SIGNED_32, DataKind::USER, false, 1);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700248 ASSERT_NE(Element(0), element);
249
Michael Butler2d4d6d92017-03-01 15:32:30 -0800250 // 64 x uint8_t
251 Type type = context->typeCreate(element, 64, 0, 0, false, false, YuvFormat::YUV_NONE);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700252 ASSERT_NE(Type(0), type);
253
Michael Butler2d4d6d92017-03-01 15:32:30 -0800254 Type type2 = context->typeCreate(element, 1, 0, 0, false, false, YuvFormat::YUV_NONE);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700255 ASSERT_NE(Type(0), type2);
256
Michael Butler2d4d6d92017-03-01 15:32:30 -0800257 std::vector<int> dataIn(64), dataOut(1);
258 std::generate(dataIn.begin(), dataIn.end(), [](){ static int val = 0; return val++; });
259 hidl_vec<uint8_t> _data;
260 _data.setToExternal((uint8_t*)dataIn.data(), dataIn.size()*sizeof(int));
261 // 64 x float1
262 Allocation allocation = context->allocationCreateTyped(type, AllocationMipmapControl::NONE,
263 (int)AllocationUsageType::SCRIPT,
264 (Ptr)nullptr);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700265 ASSERT_NE(Allocation(0), allocation);
266
Michael Butler2d4d6d92017-03-01 15:32:30 -0800267 Allocation vaout = context->allocationCreateTyped(type2, AllocationMipmapControl::NONE,
268 (int)AllocationUsageType::SCRIPT,
269 (Ptr)nullptr);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700270 ASSERT_NE(Allocation(0), vaout);
271
Michael Butler2d4d6d92017-03-01 15:32:30 -0800272 context->allocation1DWrite(allocation, 0, 0, (Size)dataIn.size(), _data);
273 hidl_vec<Allocation> vains;
274 vains.setToExternal(&allocation, 1);
Michael Butler54fa8d42017-03-17 13:02:53 -0700275 context->scriptReduce(script, mExportReduceIdx_summation, vains, vaout, nullptr);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800276 context->contextFinish();
277 context->allocationRead(vaout, (Ptr)dataOut.data(), (Size)dataOut.size()*sizeof(int));
278 // sum of 0, 1, 2, ..., 62, 63
279 int sum = 63*64/2;
280 EXPECT_EQ(sum, dataOut[0]);
281}
282
283/*
284 * This test creates an allocation and binds it to a data segment in the
285 * RenderScript script, represented in the bitcode.
286 *
287 * Calls: scriptCCreate, elementCreate, typeCreate, allocationCreateTyped,
Michael Butlerb2039ad2017-03-30 17:20:12 -0700288 * allocation1DWrite, scriptBindAllocation, scriptSetVarV, scriptBindAllocation,
289 * allocationRead, scriptInvokeV, allocationRead
Michael Butler2d4d6d92017-03-01 15:32:30 -0800290 */
Michael Butler2d4d6d92017-03-01 15:32:30 -0800291TEST_F(RenderscriptHidlTest, ScriptBindTest) {
292 hidl_vec<uint8_t> bitcode;
293 bitcode.setToExternal((uint8_t*)bitCode, bitCodeLength);
294 Script script = context->scriptCCreate("struct_test", "/data/local/tmp/", bitcode);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700295 ASSERT_NE(Script(0), script);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800296
Michael Butler54fa8d42017-03-17 13:02:53 -0700297 // in32
Michael Butler2d4d6d92017-03-01 15:32:30 -0800298 Element element = context->elementCreate(DataType::SIGNED_32, DataKind::USER, false, 1);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700299 ASSERT_NE(Element(0), element);
300
Michael Butler54fa8d42017-03-17 13:02:53 -0700301 // 64 x int32
Michael Butler2d4d6d92017-03-01 15:32:30 -0800302 Type type = context->typeCreate(element, 64, 0, 0, false, false, YuvFormat::YUV_NONE);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700303 ASSERT_NE(Type(0), type);
304
Michael Butler54fa8d42017-03-17 13:02:53 -0700305 // 64 x int32
Michael Butler2d4d6d92017-03-01 15:32:30 -0800306 Allocation allocation = context->allocationCreateTyped(type, AllocationMipmapControl::NONE,
307 (int)AllocationUsageType::SCRIPT,
308 (Ptr)nullptr);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700309 ASSERT_NE(Allocation(0), allocation);
310
Michael Butler54fa8d42017-03-17 13:02:53 -0700311 std::vector<int> dataIn(64), dataOut(64), expected(64, 5);
312 hidl_vec<uint8_t> _data;
313 _data.setToExternal((uint8_t*)dataIn.data(), dataIn.size()*sizeof(int));
314 context->allocation1DWrite(allocation, 0, 0, (Size)dataIn.size(), _data);
315 context->scriptBindAllocation(script, allocation, mExportVarIdx_var_int_ptr);
316 int dim = 64;
317 hidl_vec<uint8_t> _dim;
318 _dim.setToExternal((uint8_t*)&dim, sizeof(int));
319 context->scriptInvokeV(script, mExportFuncIdx_setBuffer, _dim);
320 context->allocationRead(allocation, (Ptr)dataOut.data(), (Size)dataOut.size()*sizeof(int));
321 EXPECT_EQ(expected, dataOut);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800322}
Michael Butler2d4d6d92017-03-01 15:32:30 -0800323
324/*
325 * This test groups together two RenderScript intrinsic kernels to run one after
326 * the other asynchronously with respect to the client. The test configures YuvToRGB(A) and Blur,
327 * and links them together such that Blur will execute after YuvToRGB(A) and use its result. The
328 * test checks the data returned to make sure it was changed after passing through the entire
329 * ScriptGroup.
330 *
331 * Calls: elementCreate, typeCreate, allocationCreateTyped, allocation2DWrite,
Michael Butler54fa8d42017-03-17 13:02:53 -0700332 * scriptIntrinsicCreate, scriptKernelIDCreate, scriptFieldIDCreate,
Michael Butlerb2039ad2017-03-30 17:20:12 -0700333 * scriptGroupCreate, scriptSetVarObj, scriptGroupSetOutput, scriptGroupExecute,
334 * contextFinish, allocation2DRead
Michael Butler2d4d6d92017-03-01 15:32:30 -0800335 */
Michael Butler2d4d6d92017-03-01 15:32:30 -0800336TEST_F(RenderscriptHidlTest, ScriptGroupTest) {
Michael Butler54fa8d42017-03-17 13:02:53 -0700337 std::vector<uint8_t> dataIn(256*256*1, 128), dataOut(256*256*4, 0), zeros(256*256*4, 0);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800338 hidl_vec<uint8_t> _dataIn, _dataOut;
339 _dataIn.setToExternal(dataIn.data(), dataIn.size());
Michael Butler54fa8d42017-03-17 13:02:53 -0700340 _dataOut.setToExternal(dataOut.data(), dataOut.size());
Michael Butler2d4d6d92017-03-01 15:32:30 -0800341
342 // 256 x 256 YUV pixels
343 Element element1 = context->elementCreate(DataType::UNSIGNED_8, DataKind::PIXEL_YUV, true, 1);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700344 ASSERT_NE(Element(0), element1);
345
Michael Butler54fa8d42017-03-17 13:02:53 -0700346 Type type1 = context->typeCreate(element1, 256, 256, 0, false, false, YuvFormat::YUV_420_888);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700347 ASSERT_NE(Type(0), type1);
348
Michael Butler2d4d6d92017-03-01 15:32:30 -0800349 Allocation allocation1 = context->allocationCreateTyped(type1, AllocationMipmapControl::NONE,
350 (int)AllocationUsageType::SCRIPT,
351 (Ptr)nullptr);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700352 ASSERT_NE(Allocation(0), allocation1);
353
Michael Butler2d4d6d92017-03-01 15:32:30 -0800354 context->allocation2DWrite(allocation1, 0, 0, 0, AllocationCubemapFace::POSITIVE_X, 256, 256,
355 _dataIn, 0);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800356
Michael Butler54fa8d42017-03-17 13:02:53 -0700357 // 256 x 256 RGBA pixels
Michael Butler2d4d6d92017-03-01 15:32:30 -0800358 Element element2 = context->elementCreate(DataType::UNSIGNED_8, DataKind::PIXEL_RGBA, true, 4);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700359 ASSERT_NE(Element(0), element2);
360
Michael Butler2d4d6d92017-03-01 15:32:30 -0800361 Type type2 = context->typeCreate(element2, 256, 256, 0, false, false, YuvFormat::YUV_NONE);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700362 ASSERT_NE(Type(0), type2);
363
Michael Butler2d4d6d92017-03-01 15:32:30 -0800364 Allocation allocation2 = context->allocationCreateTyped(type2, AllocationMipmapControl::NONE,
365 (int)AllocationUsageType::SCRIPT,
366 (Ptr)nullptr);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700367 ASSERT_NE(Allocation(0), allocation2);
368
Michael Butler2d4d6d92017-03-01 15:32:30 -0800369 context->allocation2DWrite(allocation2, 0, 0, 0, AllocationCubemapFace::POSITIVE_X, 256, 256,
370 _dataOut, 0);
Michael Butler54fa8d42017-03-17 13:02:53 -0700371
372 // create scripts
373 Script yuv2rgb = context->scriptIntrinsicCreate(ScriptIntrinsicID::ID_YUV_TO_RGB, element1);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700374 ASSERT_NE(Script(0), yuv2rgb);
Michael Butler54fa8d42017-03-17 13:02:53 -0700375
376 ScriptKernelID yuv2rgbKID = context->scriptKernelIDCreate(yuv2rgb, 0, 2);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700377 ASSERT_NE(ScriptKernelID(0), yuv2rgbKID);
Michael Butler54fa8d42017-03-17 13:02:53 -0700378
Michael Butler2d4d6d92017-03-01 15:32:30 -0800379 Script blur = context->scriptIntrinsicCreate(ScriptIntrinsicID::ID_BLUR, element2);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700380 ASSERT_NE(Script(0), blur);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800381
382 ScriptKernelID blurKID = context->scriptKernelIDCreate(blur, 0, 2);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700383 ASSERT_NE(ScriptKernelID(0), blurKID);
384
Michael Butler54fa8d42017-03-17 13:02:53 -0700385 ScriptFieldID blurFID = context->scriptFieldIDCreate(blur, 1);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700386 ASSERT_NE(ScriptFieldID(0), blurFID);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800387
388 // ScriptGroup
389 hidl_vec<ScriptKernelID> kernels = {yuv2rgbKID, blurKID};
390 hidl_vec<ScriptKernelID> srcK = {yuv2rgbKID};
Michael Butler54fa8d42017-03-17 13:02:53 -0700391 hidl_vec<ScriptKernelID> dstK = {ScriptKernelID(0)};
392 hidl_vec<ScriptFieldID> dstF = {blurFID};
Michael Butler2d4d6d92017-03-01 15:32:30 -0800393 hidl_vec<Type> types = {type2};
394 ScriptGroup scriptGroup = context->scriptGroupCreate(kernels, srcK, dstK, dstF, types);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700395 ASSERT_NE(ScriptGroup(0), scriptGroup);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800396
Michael Butler54fa8d42017-03-17 13:02:53 -0700397 context->scriptSetVarObj(yuv2rgb, 0, (ObjectBase)allocation1);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800398 context->scriptGroupSetOutput(scriptGroup, blurKID, allocation2);
399 context->scriptGroupExecute(scriptGroup);
Michael Butler54fa8d42017-03-17 13:02:53 -0700400 context->contextFinish();
Michael Butler2d4d6d92017-03-01 15:32:30 -0800401
402 // verify contents were changed
403 context->allocation2DRead(allocation2, 0, 0, 0, AllocationCubemapFace::POSITIVE_X, 256, 256,
404 (Ptr)dataOut.data(), (Size)dataOut.size(), 0);
Michael Butler54fa8d42017-03-17 13:02:53 -0700405 EXPECT_NE(zeros, dataOut);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800406}
Michael Butler2d4d6d92017-03-01 15:32:30 -0800407
408/*
409 * Similar to the ScriptGroup test, this test verifies the execution flow of
410 * RenderScript kernels and invokables.
411 *
Michael Butlerb2039ad2017-03-30 17:20:12 -0700412 * Calls: scriptCCreate, elementCreate, typeCreate, allocationCreateTyped,
413 * allocation1DWrite, scriptFieldIDCreate, scriptInvokeIDCreate,
414 * invokeClosureCreate, closureCreate, closureSetGlobal, scriptGroup2Create,
415 * scriptGroupExecute, allocationRead
Michael Butler2d4d6d92017-03-01 15:32:30 -0800416 */
Michael Butler2d4d6d92017-03-01 15:32:30 -0800417TEST_F(RenderscriptHidlTest, ScriptGroup2Test) {
Michael Butler54fa8d42017-03-17 13:02:53 -0700418 hidl_vec<uint8_t> bitcode;
419 bitcode.setToExternal((uint8_t*)bitCode, bitCodeLength);
420 Script script = context->scriptCCreate("struct_test", "/data/local/tmp/", bitcode);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700421 ASSERT_NE(Script(0), script);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800422
Michael Butler54fa8d42017-03-17 13:02:53 -0700423 std::vector<uint8_t> dataIn(128, 128), dataOut(128, 0), expected(128, 7+1);
424 hidl_vec<uint8_t> _dataIn, _dataOut;
425 _dataIn.setToExternal(dataIn.data(), dataIn.size());
426
427 // 256 x 256 YUV pixels
428 Element element = context->elementCreate(DataType::UNSIGNED_8, DataKind::USER, false, 1);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700429 ASSERT_NE(Element(0), element);
430
Michael Butler54fa8d42017-03-17 13:02:53 -0700431 Type type = context->typeCreate(element, 128, 0, 0, false, false, YuvFormat::YUV_NONE);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700432 ASSERT_NE(Type(0), type);
433
Michael Butler54fa8d42017-03-17 13:02:53 -0700434 Allocation allocation = context->allocationCreateTyped(type, AllocationMipmapControl::NONE,
435 (int)AllocationUsageType::SCRIPT,
436 (Ptr)nullptr);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700437 ASSERT_NE(Allocation(0), allocation);
438
Michael Butler54fa8d42017-03-17 13:02:53 -0700439 context->allocation1DWrite(allocation, 0, 0, (Size)_dataIn.size(), _dataIn);
440
441 ScriptFieldID fieldID = context->scriptFieldIDCreate(script, mExportVarIdx_var_allocation);
Michael Butler54fa8d42017-03-17 13:02:53 -0700442 ASSERT_NE(ScriptFieldID(0), fieldID);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800443
Michael Butler54fa8d42017-03-17 13:02:53 -0700444 // invoke
445 ScriptInvokeID invokeID = context->scriptInvokeIDCreate(script, mExportFuncIdx_setAllocation);
Michael Butler54fa8d42017-03-17 13:02:53 -0700446 ASSERT_NE(ScriptInvokeID(0), invokeID);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800447
Michael Butler54fa8d42017-03-17 13:02:53 -0700448 int dim = 128;
449 hidl_vec<uint8_t> params;
450 params.setToExternal((uint8_t*)&dim, sizeof(dim));
451 hidl_vec<ScriptFieldID> fieldIDS1 = {fieldID};
452 hidl_vec<int64_t> values1 = {int64_t(0)};
453 hidl_vec<int32_t> sizes1 = {int32_t(0)};
454 Closure closure1 = context->invokeClosureCreate(invokeID, params, fieldIDS1, values1, sizes1);
Michael Butler54fa8d42017-03-17 13:02:53 -0700455 ASSERT_NE(Closure(0), closure1);
456
457 // kernel
458 ScriptKernelID kernelID = context->scriptKernelIDCreate(script, mExportForEachIdx_increment, 3);
Michael Butler54fa8d42017-03-17 13:02:53 -0700459 ASSERT_NE(ScriptKernelID(0), kernelID);
460
461 hidl_vec<ScriptFieldID> fieldIDS2 = {ScriptFieldID(0)};
462 hidl_vec<int64_t> values2 = {(int64_t)(intptr_t)allocation};
463 hidl_vec<int32_t> sizes2 = {-1 /* allocation */};
464 hidl_vec<Closure> depClosures2 = {closure1};
465 hidl_vec<ScriptFieldID> depFieldIDS2 = {fieldID};
466 Closure closure2 = context->closureCreate(kernelID, allocation /* returnValue */, fieldIDS2,
467 values2, sizes2, depClosures2, depFieldIDS2);
Michael Butler54fa8d42017-03-17 13:02:53 -0700468 ASSERT_NE(Closure(0), closure2);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800469
Michael Butler54fa8d42017-03-17 13:02:53 -0700470 // set argument
471 context->closureSetGlobal(closure1, fieldID, (int64_t)(intptr_t)allocation,
472 -1 /* allocation */);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800473
Michael Butler54fa8d42017-03-17 13:02:53 -0700474 // execute
Michael Butler2d4d6d92017-03-01 15:32:30 -0800475 hidl_string name = "script_group_2_test";
Michael Butler54fa8d42017-03-17 13:02:53 -0700476 hidl_string cacheDir = "/data/local/tmp";
477 hidl_vec<Closure> closures = {closure1, closure2};
Michael Butler2d4d6d92017-03-01 15:32:30 -0800478 ScriptGroup2 scriptGroup2 = context->scriptGroup2Create(name, cacheDir, closures);
Michael Butler54fa8d42017-03-17 13:02:53 -0700479 ASSERT_NE(ScriptGroup2(0), scriptGroup2);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800480
481 context->scriptGroupExecute(scriptGroup2);
Michael Butler54fa8d42017-03-17 13:02:53 -0700482 context->allocationRead(allocation, (Ptr)dataOut.data(), (Size)dataOut.size()*sizeof(uint8_t));
483 EXPECT_EQ(expected, dataOut);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800484}
Michael Butler54fa8d42017-03-17 13:02:53 -0700485
486/*
487 * Similar to the ScriptGroup test, this test verifies a single kernel can be
488 * called by ScriptGroup with an unbound allocation specified before launch
489 *
Michael Butlerb2039ad2017-03-30 17:20:12 -0700490 * Calls: scriptCCreate, elementCreate, typeCreate, allocationCreateTyped,
491 * allocation1DWrite, scriptKernelIDCreate, closureCreate, closureSetArg,
492 * scriptGroup2Create, scriptGroupExecute, allocationRead
Michael Butler54fa8d42017-03-17 13:02:53 -0700493 */
494TEST_F(RenderscriptHidlTest, ScriptGroup2KernelTest) {
495 hidl_vec<uint8_t> bitcode;
496 bitcode.setToExternal((uint8_t*)bitCode, bitCodeLength);
497 Script script = context->scriptCCreate("struct_test", "/data/local/tmp/", bitcode);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700498 ASSERT_NE(Script(0), script);
Michael Butler54fa8d42017-03-17 13:02:53 -0700499
500 std::vector<uint8_t> dataIn(128, 128), dataOut(128, 0), expected(128, 128 + 1);
501 hidl_vec<uint8_t> _dataIn, _dataOut;
502 _dataIn.setToExternal(dataIn.data(), dataIn.size());
503
504 // 256 x 256 YUV pixels
505 Element element = context->elementCreate(DataType::UNSIGNED_8, DataKind::USER, false, 1);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700506 ASSERT_NE(Element(0), element);
507
Michael Butler54fa8d42017-03-17 13:02:53 -0700508 Type type = context->typeCreate(element, 128, 0, 0, false, false, YuvFormat::YUV_NONE);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700509 ASSERT_NE(Type(0), type);
510
Michael Butler54fa8d42017-03-17 13:02:53 -0700511 Allocation allocation = context->allocationCreateTyped(type, AllocationMipmapControl::NONE,
512 (int)AllocationUsageType::SCRIPT,
513 (Ptr)nullptr);
Michael Butlerb2039ad2017-03-30 17:20:12 -0700514 ASSERT_NE(Allocation(0), allocation);
515
Michael Butler54fa8d42017-03-17 13:02:53 -0700516 context->allocation1DWrite(allocation, 0, 0, (Size)_dataIn.size(), _dataIn);
517
518 // kernel
519 ScriptKernelID kernelID = context->scriptKernelIDCreate(script, mExportForEachIdx_increment, 3);
Michael Butler54fa8d42017-03-17 13:02:53 -0700520 ASSERT_NE(ScriptKernelID(0), kernelID);
521
522 hidl_vec<ScriptFieldID> fieldIDS = {ScriptFieldID(0)};
523 hidl_vec<int64_t> values = {int64_t(0)};
524 hidl_vec<int32_t> sizes = {int32_t(0)};
525 hidl_vec<Closure> depClosures = {Closure(0)};
526 hidl_vec<ScriptFieldID> depFieldIDS = {ScriptFieldID(0)};
527 Closure closure = context->closureCreate(kernelID, allocation /* returnValue */, fieldIDS,
528 values, sizes, depClosures, depFieldIDS);
Michael Butler54fa8d42017-03-17 13:02:53 -0700529 ASSERT_NE(Closure(0), closure);
530
531 // set argument
532 context->closureSetArg(closure, 0 /* first argument */, (Ptr)allocation, -1);
533
534 // execute
535 hidl_string name = "script_group_2_test";
536 hidl_string cacheDir = "/data/local/tmp";
537 hidl_vec<Closure> closures = {closure};
538 ScriptGroup2 scriptGroup2 = context->scriptGroup2Create(name, cacheDir, closures);
Michael Butler54fa8d42017-03-17 13:02:53 -0700539 ASSERT_NE(ScriptGroup2(0), scriptGroup2);
540
541 context->scriptGroupExecute(scriptGroup2);
542 context->allocationRead(allocation, (Ptr)dataOut.data(), (Size)dataOut.size()*sizeof(uint8_t));
543 EXPECT_EQ(expected, dataOut);
544}