blob: 8268dcce58db71dda87dcf7a6470afc4d24b4f16 [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 Butler7c1ef5b2017-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 Butler7c1ef5b2017-03-30 17:20:12 -070048 ASSERT_NE(Script(0), script);
Michael Butler2d4d6d92017-03-01 15:32:30 -080049
50 // arg tests
Michael Butlerda8c2c12017-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 Butlerda8c2c12017-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 Butlerda8c2c12017-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 Butlerda8c2c12017-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 Butlerda8c2c12017-03-17 13:02:53 -070062 EXPECT_EQ(101l, resultJ);
Michael Butler2d4d6d92017-03-01 15:32:30 -080063
Michael Butlerda8c2c12017-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 Butlerda8c2c12017-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 Butlerda8c2c12017-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 Butlerda8c2c12017-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 Butler7c1ef5b2017-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 Butler7c1ef5b2017-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 Butler7c1ef5b2017-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 Butlerda8c2c12017-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 Butlerda8c2c12017-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 Butlerda8c2c12017-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 Butler7c1ef5b2017-03-30 17:20:12 -0700118 ASSERT_NE(Element(0), elementVE);
119
Michael Butlerda8c2c12017-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 Butler7c1ef5b2017-03-30 17:20:12 -0700139 ASSERT_NE(Script(0), script);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800140
141 // invoke test
Michael Butlerda8c2c12017-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 Butlerda8c2c12017-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 Butler7c1ef5b2017-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 Butler7c1ef5b2017-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 Butler7c1ef5b2017-03-30 17:20:12 -0700206 ASSERT_NE(Type(0), type);
207
Michael Butlerda8c2c12017-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 Butlerda8c2c12017-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 Butler7c1ef5b2017-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 Butler7c1ef5b2017-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 Butlerda8c2c12017-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 Butlerda8c2c12017-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 Butler7c1ef5b2017-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 Butler7c1ef5b2017-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 Butler7c1ef5b2017-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 Butler7c1ef5b2017-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 Butler7c1ef5b2017-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 Butler7c1ef5b2017-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 Butlerda8c2c12017-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 Butler7c1ef5b2017-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 Butler7c1ef5b2017-03-30 17:20:12 -0700295 ASSERT_NE(Script(0), script);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800296
Michael Butlerda8c2c12017-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 Butler7c1ef5b2017-03-30 17:20:12 -0700299 ASSERT_NE(Element(0), element);
300
Michael Butlerda8c2c12017-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 Butler7c1ef5b2017-03-30 17:20:12 -0700303 ASSERT_NE(Type(0), type);
304
Michael Butlerda8c2c12017-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 Butler7c1ef5b2017-03-30 17:20:12 -0700309 ASSERT_NE(Allocation(0), allocation);
310
Michael Butlerda8c2c12017-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
Michael Butler3f986a02017-04-13 17:42:07 -0700326 * the other asynchronously with respect to the client. The test configures
327 * Blend and Blur, and links them together such that Blur will execute after
328 * Blend and use its result. The test checks the data returned to make sure it
329 * was changed after passing through the entire ScriptGroup.
Michael Butler2d4d6d92017-03-01 15:32:30 -0800330 *
331 * Calls: elementCreate, typeCreate, allocationCreateTyped, allocation2DWrite,
Michael Butlerda8c2c12017-03-17 13:02:53 -0700332 * scriptIntrinsicCreate, scriptKernelIDCreate, scriptFieldIDCreate,
Michael Butler3f986a02017-04-13 17:42:07 -0700333 * scriptGroupCreate, scriptGroupSetInput, scriptGroupSetOutput,
334 * scriptGroupExecute, contextFinish, allocation2DRead
Michael Butler2d4d6d92017-03-01 15:32:30 -0800335 */
Michael Butler2d4d6d92017-03-01 15:32:30 -0800336TEST_F(RenderscriptHidlTest, ScriptGroupTest) {
Michael Butler3f986a02017-04-13 17:42:07 -0700337 std::vector<uint8_t> dataIn(256 * 256 * 4, 128), dataOut(256 * 256 * 4, 0),
338 zeros(256 * 256 * 4, 0);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800339 hidl_vec<uint8_t> _dataIn, _dataOut;
340 _dataIn.setToExternal(dataIn.data(), dataIn.size());
Michael Butlerda8c2c12017-03-17 13:02:53 -0700341 _dataOut.setToExternal(dataOut.data(), dataOut.size());
Michael Butler2d4d6d92017-03-01 15:32:30 -0800342
343 // 256 x 256 YUV pixels
Michael Butler3f986a02017-04-13 17:42:07 -0700344 Element element1 = context->elementCreate(DataType::UNSIGNED_8,
345 DataKind::PIXEL_RGBA, true, 4);
Michael Butler7c1ef5b2017-03-30 17:20:12 -0700346 ASSERT_NE(Element(0), element1);
347
Michael Butler3f986a02017-04-13 17:42:07 -0700348 Type type1 = context->typeCreate(element1, 256, 256, 0, false, false,
349 YuvFormat::YUV_NONE);
Michael Butler7c1ef5b2017-03-30 17:20:12 -0700350 ASSERT_NE(Type(0), type1);
351
Michael Butler2d4d6d92017-03-01 15:32:30 -0800352 Allocation allocation1 = context->allocationCreateTyped(type1, AllocationMipmapControl::NONE,
353 (int)AllocationUsageType::SCRIPT,
354 (Ptr)nullptr);
Michael Butler7c1ef5b2017-03-30 17:20:12 -0700355 ASSERT_NE(Allocation(0), allocation1);
356
Michael Butler2d4d6d92017-03-01 15:32:30 -0800357 context->allocation2DWrite(allocation1, 0, 0, 0, AllocationCubemapFace::POSITIVE_X, 256, 256,
358 _dataIn, 0);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800359
Michael Butlerda8c2c12017-03-17 13:02:53 -0700360 // 256 x 256 RGBA pixels
Michael Butler2d4d6d92017-03-01 15:32:30 -0800361 Element element2 = context->elementCreate(DataType::UNSIGNED_8, DataKind::PIXEL_RGBA, true, 4);
Michael Butler7c1ef5b2017-03-30 17:20:12 -0700362 ASSERT_NE(Element(0), element2);
363
Michael Butler2d4d6d92017-03-01 15:32:30 -0800364 Type type2 = context->typeCreate(element2, 256, 256, 0, false, false, YuvFormat::YUV_NONE);
Michael Butler7c1ef5b2017-03-30 17:20:12 -0700365 ASSERT_NE(Type(0), type2);
366
Michael Butler2d4d6d92017-03-01 15:32:30 -0800367 Allocation allocation2 = context->allocationCreateTyped(type2, AllocationMipmapControl::NONE,
368 (int)AllocationUsageType::SCRIPT,
369 (Ptr)nullptr);
Michael Butler7c1ef5b2017-03-30 17:20:12 -0700370 ASSERT_NE(Allocation(0), allocation2);
371
Michael Butler2d4d6d92017-03-01 15:32:30 -0800372 context->allocation2DWrite(allocation2, 0, 0, 0, AllocationCubemapFace::POSITIVE_X, 256, 256,
373 _dataOut, 0);
Michael Butlerda8c2c12017-03-17 13:02:53 -0700374
375 // create scripts
Michael Butler3f986a02017-04-13 17:42:07 -0700376 Script blend =
377 context->scriptIntrinsicCreate(ScriptIntrinsicID::ID_BLEND, element1);
378 ASSERT_NE(Script(0), blend);
Michael Butlerda8c2c12017-03-17 13:02:53 -0700379
Michael Butler3f986a02017-04-13 17:42:07 -0700380 ScriptKernelID blendKID = context->scriptKernelIDCreate(blend, 1, 3);
381 ASSERT_NE(ScriptKernelID(0), blendKID);
Michael Butlerda8c2c12017-03-17 13:02:53 -0700382
Michael Butler2d4d6d92017-03-01 15:32:30 -0800383 Script blur = context->scriptIntrinsicCreate(ScriptIntrinsicID::ID_BLUR, element2);
Michael Butler7c1ef5b2017-03-30 17:20:12 -0700384 ASSERT_NE(Script(0), blur);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800385
386 ScriptKernelID blurKID = context->scriptKernelIDCreate(blur, 0, 2);
Michael Butler7c1ef5b2017-03-30 17:20:12 -0700387 ASSERT_NE(ScriptKernelID(0), blurKID);
388
Michael Butlerda8c2c12017-03-17 13:02:53 -0700389 ScriptFieldID blurFID = context->scriptFieldIDCreate(blur, 1);
Michael Butler7c1ef5b2017-03-30 17:20:12 -0700390 ASSERT_NE(ScriptFieldID(0), blurFID);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800391
392 // ScriptGroup
Michael Butler3f986a02017-04-13 17:42:07 -0700393 hidl_vec<ScriptKernelID> kernels = {blendKID, blurKID};
394 hidl_vec<ScriptKernelID> srcK = {blendKID};
Michael Butlerda8c2c12017-03-17 13:02:53 -0700395 hidl_vec<ScriptKernelID> dstK = {ScriptKernelID(0)};
396 hidl_vec<ScriptFieldID> dstF = {blurFID};
Michael Butler2d4d6d92017-03-01 15:32:30 -0800397 hidl_vec<Type> types = {type2};
398 ScriptGroup scriptGroup = context->scriptGroupCreate(kernels, srcK, dstK, dstF, types);
Michael Butler7c1ef5b2017-03-30 17:20:12 -0700399 ASSERT_NE(ScriptGroup(0), scriptGroup);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800400
Michael Butler3f986a02017-04-13 17:42:07 -0700401 context->scriptGroupSetInput(scriptGroup, blendKID, allocation1);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800402 context->scriptGroupSetOutput(scriptGroup, blurKID, allocation2);
403 context->scriptGroupExecute(scriptGroup);
Michael Butlerda8c2c12017-03-17 13:02:53 -0700404 context->contextFinish();
Michael Butler2d4d6d92017-03-01 15:32:30 -0800405
406 // verify contents were changed
407 context->allocation2DRead(allocation2, 0, 0, 0, AllocationCubemapFace::POSITIVE_X, 256, 256,
408 (Ptr)dataOut.data(), (Size)dataOut.size(), 0);
Michael Butlerda8c2c12017-03-17 13:02:53 -0700409 EXPECT_NE(zeros, dataOut);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800410}
Michael Butler2d4d6d92017-03-01 15:32:30 -0800411
412/*
413 * Similar to the ScriptGroup test, this test verifies the execution flow of
414 * RenderScript kernels and invokables.
415 *
Michael Butler7c1ef5b2017-03-30 17:20:12 -0700416 * Calls: scriptCCreate, elementCreate, typeCreate, allocationCreateTyped,
417 * allocation1DWrite, scriptFieldIDCreate, scriptInvokeIDCreate,
418 * invokeClosureCreate, closureCreate, closureSetGlobal, scriptGroup2Create,
419 * scriptGroupExecute, allocationRead
Michael Butler2d4d6d92017-03-01 15:32:30 -0800420 */
Michael Butler2d4d6d92017-03-01 15:32:30 -0800421TEST_F(RenderscriptHidlTest, ScriptGroup2Test) {
Michael Butlerda8c2c12017-03-17 13:02:53 -0700422 hidl_vec<uint8_t> bitcode;
423 bitcode.setToExternal((uint8_t*)bitCode, bitCodeLength);
424 Script script = context->scriptCCreate("struct_test", "/data/local/tmp/", bitcode);
Michael Butler7c1ef5b2017-03-30 17:20:12 -0700425 ASSERT_NE(Script(0), script);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800426
Michael Butlerda8c2c12017-03-17 13:02:53 -0700427 std::vector<uint8_t> dataIn(128, 128), dataOut(128, 0), expected(128, 7+1);
428 hidl_vec<uint8_t> _dataIn, _dataOut;
429 _dataIn.setToExternal(dataIn.data(), dataIn.size());
430
431 // 256 x 256 YUV pixels
432 Element element = context->elementCreate(DataType::UNSIGNED_8, DataKind::USER, false, 1);
Michael Butler7c1ef5b2017-03-30 17:20:12 -0700433 ASSERT_NE(Element(0), element);
434
Michael Butlerda8c2c12017-03-17 13:02:53 -0700435 Type type = context->typeCreate(element, 128, 0, 0, false, false, YuvFormat::YUV_NONE);
Michael Butler7c1ef5b2017-03-30 17:20:12 -0700436 ASSERT_NE(Type(0), type);
437
Michael Butlerda8c2c12017-03-17 13:02:53 -0700438 Allocation allocation = context->allocationCreateTyped(type, AllocationMipmapControl::NONE,
439 (int)AllocationUsageType::SCRIPT,
440 (Ptr)nullptr);
Michael Butler7c1ef5b2017-03-30 17:20:12 -0700441 ASSERT_NE(Allocation(0), allocation);
442
Michael Butlerda8c2c12017-03-17 13:02:53 -0700443 context->allocation1DWrite(allocation, 0, 0, (Size)_dataIn.size(), _dataIn);
444
445 ScriptFieldID fieldID = context->scriptFieldIDCreate(script, mExportVarIdx_var_allocation);
Michael Butlerda8c2c12017-03-17 13:02:53 -0700446 ASSERT_NE(ScriptFieldID(0), fieldID);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800447
Michael Butlerda8c2c12017-03-17 13:02:53 -0700448 // invoke
449 ScriptInvokeID invokeID = context->scriptInvokeIDCreate(script, mExportFuncIdx_setAllocation);
Michael Butlerda8c2c12017-03-17 13:02:53 -0700450 ASSERT_NE(ScriptInvokeID(0), invokeID);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800451
Michael Butlerda8c2c12017-03-17 13:02:53 -0700452 int dim = 128;
453 hidl_vec<uint8_t> params;
454 params.setToExternal((uint8_t*)&dim, sizeof(dim));
455 hidl_vec<ScriptFieldID> fieldIDS1 = {fieldID};
456 hidl_vec<int64_t> values1 = {int64_t(0)};
457 hidl_vec<int32_t> sizes1 = {int32_t(0)};
458 Closure closure1 = context->invokeClosureCreate(invokeID, params, fieldIDS1, values1, sizes1);
Michael Butlerda8c2c12017-03-17 13:02:53 -0700459 ASSERT_NE(Closure(0), closure1);
460
461 // kernel
462 ScriptKernelID kernelID = context->scriptKernelIDCreate(script, mExportForEachIdx_increment, 3);
Michael Butlerda8c2c12017-03-17 13:02:53 -0700463 ASSERT_NE(ScriptKernelID(0), kernelID);
464
465 hidl_vec<ScriptFieldID> fieldIDS2 = {ScriptFieldID(0)};
466 hidl_vec<int64_t> values2 = {(int64_t)(intptr_t)allocation};
467 hidl_vec<int32_t> sizes2 = {-1 /* allocation */};
468 hidl_vec<Closure> depClosures2 = {closure1};
469 hidl_vec<ScriptFieldID> depFieldIDS2 = {fieldID};
470 Closure closure2 = context->closureCreate(kernelID, allocation /* returnValue */, fieldIDS2,
471 values2, sizes2, depClosures2, depFieldIDS2);
Michael Butlerda8c2c12017-03-17 13:02:53 -0700472 ASSERT_NE(Closure(0), closure2);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800473
Michael Butlerda8c2c12017-03-17 13:02:53 -0700474 // set argument
475 context->closureSetGlobal(closure1, fieldID, (int64_t)(intptr_t)allocation,
476 -1 /* allocation */);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800477
Michael Butlerda8c2c12017-03-17 13:02:53 -0700478 // execute
Michael Butler2d4d6d92017-03-01 15:32:30 -0800479 hidl_string name = "script_group_2_test";
Michael Butlerda8c2c12017-03-17 13:02:53 -0700480 hidl_string cacheDir = "/data/local/tmp";
481 hidl_vec<Closure> closures = {closure1, closure2};
Michael Butler2d4d6d92017-03-01 15:32:30 -0800482 ScriptGroup2 scriptGroup2 = context->scriptGroup2Create(name, cacheDir, closures);
Michael Butlerda8c2c12017-03-17 13:02:53 -0700483 ASSERT_NE(ScriptGroup2(0), scriptGroup2);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800484
485 context->scriptGroupExecute(scriptGroup2);
Michael Butlerda8c2c12017-03-17 13:02:53 -0700486 context->allocationRead(allocation, (Ptr)dataOut.data(), (Size)dataOut.size()*sizeof(uint8_t));
487 EXPECT_EQ(expected, dataOut);
Michael Butler2d4d6d92017-03-01 15:32:30 -0800488}
Michael Butlerda8c2c12017-03-17 13:02:53 -0700489
490/*
491 * Similar to the ScriptGroup test, this test verifies a single kernel can be
492 * called by ScriptGroup with an unbound allocation specified before launch
493 *
Michael Butler7c1ef5b2017-03-30 17:20:12 -0700494 * Calls: scriptCCreate, elementCreate, typeCreate, allocationCreateTyped,
495 * allocation1DWrite, scriptKernelIDCreate, closureCreate, closureSetArg,
496 * scriptGroup2Create, scriptGroupExecute, allocationRead
Michael Butlerda8c2c12017-03-17 13:02:53 -0700497 */
498TEST_F(RenderscriptHidlTest, ScriptGroup2KernelTest) {
499 hidl_vec<uint8_t> bitcode;
500 bitcode.setToExternal((uint8_t*)bitCode, bitCodeLength);
501 Script script = context->scriptCCreate("struct_test", "/data/local/tmp/", bitcode);
Michael Butler7c1ef5b2017-03-30 17:20:12 -0700502 ASSERT_NE(Script(0), script);
Michael Butlerda8c2c12017-03-17 13:02:53 -0700503
504 std::vector<uint8_t> dataIn(128, 128), dataOut(128, 0), expected(128, 128 + 1);
505 hidl_vec<uint8_t> _dataIn, _dataOut;
506 _dataIn.setToExternal(dataIn.data(), dataIn.size());
507
508 // 256 x 256 YUV pixels
509 Element element = context->elementCreate(DataType::UNSIGNED_8, DataKind::USER, false, 1);
Michael Butler7c1ef5b2017-03-30 17:20:12 -0700510 ASSERT_NE(Element(0), element);
511
Michael Butlerda8c2c12017-03-17 13:02:53 -0700512 Type type = context->typeCreate(element, 128, 0, 0, false, false, YuvFormat::YUV_NONE);
Michael Butler7c1ef5b2017-03-30 17:20:12 -0700513 ASSERT_NE(Type(0), type);
514
Michael Butlerda8c2c12017-03-17 13:02:53 -0700515 Allocation allocation = context->allocationCreateTyped(type, AllocationMipmapControl::NONE,
516 (int)AllocationUsageType::SCRIPT,
517 (Ptr)nullptr);
Michael Butler7c1ef5b2017-03-30 17:20:12 -0700518 ASSERT_NE(Allocation(0), allocation);
519
Michael Butlerda8c2c12017-03-17 13:02:53 -0700520 context->allocation1DWrite(allocation, 0, 0, (Size)_dataIn.size(), _dataIn);
521
522 // kernel
523 ScriptKernelID kernelID = context->scriptKernelIDCreate(script, mExportForEachIdx_increment, 3);
Michael Butlerda8c2c12017-03-17 13:02:53 -0700524 ASSERT_NE(ScriptKernelID(0), kernelID);
525
526 hidl_vec<ScriptFieldID> fieldIDS = {ScriptFieldID(0)};
527 hidl_vec<int64_t> values = {int64_t(0)};
528 hidl_vec<int32_t> sizes = {int32_t(0)};
529 hidl_vec<Closure> depClosures = {Closure(0)};
530 hidl_vec<ScriptFieldID> depFieldIDS = {ScriptFieldID(0)};
531 Closure closure = context->closureCreate(kernelID, allocation /* returnValue */, fieldIDS,
532 values, sizes, depClosures, depFieldIDS);
Michael Butlerda8c2c12017-03-17 13:02:53 -0700533 ASSERT_NE(Closure(0), closure);
534
535 // set argument
536 context->closureSetArg(closure, 0 /* first argument */, (Ptr)allocation, -1);
537
538 // execute
539 hidl_string name = "script_group_2_test";
540 hidl_string cacheDir = "/data/local/tmp";
541 hidl_vec<Closure> closures = {closure};
542 ScriptGroup2 scriptGroup2 = context->scriptGroup2Create(name, cacheDir, closures);
Michael Butlerda8c2c12017-03-17 13:02:53 -0700543 ASSERT_NE(ScriptGroup2(0), scriptGroup2);
544
545 context->scriptGroupExecute(scriptGroup2);
546 context->allocationRead(allocation, (Ptr)dataOut.data(), (Size)dataOut.size()*sizeof(uint8_t));
547 EXPECT_EQ(expected, dataOut);
548}