1// This file is automatically generated from
2// frameworks/rs/tests/java_api/RSUnitTests/RSUnitTests.py
3/*
4 * Copyright (C) 2017 The Android Open Source Project
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *      http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#include "shared.rsh"
20
21int gDimX;
22int gDimY;
23int gDimZ;
24int gStart;
25static bool failed = false;
26
27
28// For each type, define 4 kernels, one per vector variant, that walk an
29// allocation and validate each cell.  The value in a cell must be gStart +
30// "index-of-the-cell-starting-from-zero".  For vector types, the 'x' field
31// must have this value.  The expected values for 'y', 'z' and 'w' follow the
32// 'x' value in increments of one.
33//
34// 'z' will be zero for 2D and 1D allocations.  'y' will be zero for 1D
35// allocations.
36
37// TODO When the requirement that kernels must return an output to be launched
38// using rsForEach is relaxed, make the kernel not return its input.
39#define VERIFY_KERNEL(CT)                                                      \
40    CT RS_KERNEL verify_##CT(CT in, int x, int y, int z) {                     \
41        int val = (gStart + x + y * gDimX + z * gDimY * gDimX);                \
42        _RS_ASSERT_EQU(in, (CT) val);                                          \
43        return in;                                                             \
44    }                                                                          \
45    CT##2 RS_KERNEL verify_##CT##2(CT##2 in, int x, int y, int z) {            \
46        int val = (gStart + x + y * gDimX + z * gDimY * gDimX);                \
47        _RS_ASSERT_EQU(in.x, (CT) val);                                        \
48        _RS_ASSERT_EQU(in.y, (CT) (val + 1));                                  \
49        return in;                                                             \
50    }                                                                          \
51    CT##3 RS_KERNEL verify_##CT##3(CT##3 in, int x, int y, int z) {            \
52        int val = (gStart + x + y * gDimX + z * gDimY * gDimX);                \
53        _RS_ASSERT_EQU(in.x, (CT) val);                                        \
54        _RS_ASSERT_EQU(in.y, (CT) (val + 1));                                  \
55        _RS_ASSERT_EQU(in.z, (CT) (val + 2));                                  \
56        return in;                                                             \
57    }                                                                          \
58    CT##4 RS_KERNEL verify_##CT##4(CT##4 in, int x, int y, int z) {            \
59        int val = (gStart + x + y * gDimX + z * gDimY * gDimX);                \
60        _RS_ASSERT_EQU(in.x, (CT) val);                                        \
61        _RS_ASSERT_EQU(in.y, (CT) (val + 1));                                  \
62        _RS_ASSERT_EQU(in.z, (CT) (val + 2));                                  \
63        _RS_ASSERT_EQU(in.w, (CT) (val + 3));                                  \
64        return in;                                                             \
65    }                                                                          \
66
67#ifndef RSTEST_COMPAT
68VERIFY_KERNEL(half)
69#endif
70VERIFY_KERNEL(float)
71VERIFY_KERNEL(double)
72VERIFY_KERNEL(char)
73VERIFY_KERNEL(short)
74VERIFY_KERNEL(int)
75VERIFY_KERNEL(long)
76VERIFY_KERNEL(uchar)
77VERIFY_KERNEL(ushort)
78VERIFY_KERNEL(uint)
79VERIFY_KERNEL(ulong)
80
81
82// Store to an allocation based on the vector size and dimensionality being
83// tested.  SETLEMENTAT, STORE_TO_ALLOC capture the following variables from
84// the context where they get instantiated:
85//     vecSize, gAlloc, val, x, y, z
86
87#define SETELEMENTAT(CT)                                                      \
88    if (gDimZ != 0) {                                                         \
89        rsSetElementAt_##CT(gAlloc, storeVal, x, y, z);                       \
90    }                                                                         \
91    else if (gDimY != 0) {                                                    \
92        rsSetElementAt_##CT(gAlloc, storeVal, x, y);                          \
93    }                                                                         \
94    else {                                                                    \
95        rsSetElementAt_##CT(gAlloc, storeVal, x);                             \
96    }
97
98#define STORE_TO_ALLOC(RST, CT)                                               \
99    case RST:                                                                 \
100        switch (vecSize) {                                                    \
101            case 1: {                                                         \
102                CT storeVal = (CT) val;                                       \
103                SETELEMENTAT(CT);                                             \
104                     }                                                        \
105                break;                                                        \
106            case 2: {                                                         \
107                CT##2 storeVal = {(CT) val, (CT) (val + 1)};                  \
108                SETELEMENTAT(CT##2);                                          \
109                    }                                                         \
110                break;                                                        \
111            case 3: {                                                         \
112                CT##3 storeVal = {(CT) val, (CT) (val + 1), (CT) (val + 2)};  \
113                SETELEMENTAT(CT##3);                                          \
114                    }                                                         \
115                break;                                                        \
116            case 4: {                                                         \
117                CT##4 storeVal = {(CT) val, (CT) (val + 1), (CT) (val + 2),   \
118                                  (CT) (val + 3)};                            \
119                SETELEMENTAT(CT##4);                                          \
120                    }                                                         \
121                break;                                                        \
122        }                                                                     \
123        break;                                                                \
124
125
126// Launch the verify_kernel based on the appropriate type and vector size.
127#define LAUNCH_VERIFY_KERNEL(RST, CT)                                         \
128    case RST:                                                                 \
129        if (vecSize == 1) rsForEach(verify_##CT, gAlloc, gAlloc);             \
130        else if (vecSize == 2) rsForEach(verify_##CT##2, gAlloc, gAlloc);     \
131        else if (vecSize == 3) rsForEach(verify_##CT##3, gAlloc, gAlloc);     \
132        else if (vecSize == 4) rsForEach(verify_##CT##4, gAlloc, gAlloc);     \
133        break;
134
135void CreateAndTestAlloc(int dataType, int vecSize) {
136    if (gDimZ != 0 && gDimY == 0) {
137        _RS_ASSERT(false); // Invalid test
138        return;
139    }
140    if (gDimX == 0) {
141        _RS_ASSERT(false); // Invalid test
142        return;
143    }
144
145    rs_data_type dt = (rs_data_type) dataType;
146
147    rs_element element;
148    rs_type type;
149    rs_allocation gAlloc;
150
151    // Create and validate the rs_element object
152    if (vecSize == 1)
153        element = rsCreateElement((rs_data_type) dt);
154    else
155        element = rsCreateVectorElement((rs_data_type) dt, vecSize);
156    _RS_ASSERT(rsIsObject(element));
157    if (!rsIsObject(element))
158        return;
159
160    // Create and validate the rs_type object
161    type = rsCreateType(element, gDimX, gDimY, gDimZ);
162    _RS_ASSERT(rsIsObject(type));
163    if (!rsIsObject(type))
164        return;
165
166    // Create and validate the rs_allocation object
167    gAlloc = rsCreateAllocation(type);
168    if (!rsIsObject(gAlloc))
169        return;
170
171    // Handle RenderScript's distinction between Y or Z dimension being absent
172    // and having a size of 1
173    int zEnd = (gDimZ != 0) ? gDimZ: 1;
174    int yEnd = (gDimY != 0) ? gDimY: 1;
175    for (int z = 0; z < zEnd; z ++) {
176        for (int y = 0; y < yEnd; y ++) {
177            for (int x = 0; x < gDimX; x ++) {
178                int val = gStart + (x + y * gDimX + z * gDimY * gDimX);
179
180                // Store to a cell based on the type, vector size and
181                // dimensionality
182                switch (dt) {
183#ifndef RSTEST_COMPAT
184                    STORE_TO_ALLOC(RS_TYPE_FLOAT_16, half);
185#else
186                    // support lib doesn't support f16, skip
187                    case RS_TYPE_FLOAT_16: break;
188#endif
189                    STORE_TO_ALLOC(RS_TYPE_FLOAT_32, float);
190                    STORE_TO_ALLOC(RS_TYPE_FLOAT_64, double);
191                    STORE_TO_ALLOC(RS_TYPE_SIGNED_8, char);
192                    STORE_TO_ALLOC(RS_TYPE_SIGNED_16, short);
193                    STORE_TO_ALLOC(RS_TYPE_SIGNED_32, int);
194                    STORE_TO_ALLOC(RS_TYPE_SIGNED_64, long);
195                    STORE_TO_ALLOC(RS_TYPE_UNSIGNED_8, uchar);
196                    STORE_TO_ALLOC(RS_TYPE_UNSIGNED_16, ushort);
197                    STORE_TO_ALLOC(RS_TYPE_UNSIGNED_32, uint);
198                    STORE_TO_ALLOC(RS_TYPE_UNSIGNED_64, ulong);
199                    default:
200                        // Invalid test
201                        _RS_ASSERT(false);
202                        break;
203                }
204            }
205        }
206    }
207
208    // Launch the appropriate verify_ kernel
209    switch (dt) {
210#ifndef RSTEST_COMPAT
211        LAUNCH_VERIFY_KERNEL(RS_TYPE_FLOAT_16, half);
212#else
213        // support lib doesn't support f16, skip
214        case RS_TYPE_FLOAT_16: break;
215#endif
216        LAUNCH_VERIFY_KERNEL(RS_TYPE_FLOAT_32, float);
217        LAUNCH_VERIFY_KERNEL(RS_TYPE_FLOAT_64, double);
218        LAUNCH_VERIFY_KERNEL(RS_TYPE_SIGNED_8, char);
219        LAUNCH_VERIFY_KERNEL(RS_TYPE_SIGNED_16, short);
220        LAUNCH_VERIFY_KERNEL(RS_TYPE_SIGNED_32, int);
221        LAUNCH_VERIFY_KERNEL(RS_TYPE_SIGNED_64, long);
222        LAUNCH_VERIFY_KERNEL(RS_TYPE_UNSIGNED_8, uchar);
223        LAUNCH_VERIFY_KERNEL(RS_TYPE_UNSIGNED_16, ushort);
224        LAUNCH_VERIFY_KERNEL(RS_TYPE_UNSIGNED_32, uint);
225        LAUNCH_VERIFY_KERNEL(RS_TYPE_UNSIGNED_64, ulong);
226
227        default:
228            // Invalid test
229            _RS_ASSERT(false);
230            break;
231    }
232}
233
234#define TEST_DATA_TYPE(dt, allowSimple, allowVector, allowPixel) {             \
235    if (allowSimple)                                                           \
236        _RS_ASSERT(rsIsObject(rsCreateElement(dt)));                           \
237    else                                                                       \
238        _RS_ASSERT(!rsIsObject(rsCreateElement(dt)));                          \
239    if (allowVector)                                                           \
240        _RS_ASSERT(rsIsObject(rsCreateVectorElement(dt, 3)));                  \
241    else                                                                       \
242        _RS_ASSERT(!rsIsObject(rsCreateVectorElement(dt, 3)));                 \
243    if (allowPixel)                                                            \
244        _RS_ASSERT(rsIsObject(rsCreatePixelElement(dt, RS_KIND_PIXEL_DEPTH))); \
245    else                                                                       \
246        _RS_ASSERT(!rsIsObject(rsCreatePixelElement(dt, RS_KIND_PIXEL_DEPTH)));\
247}
248
249#define TEST_SUPPORTED_PIXEL(dt, dk) {                                         \
250    _RS_ASSERT(rsIsObject(rsCreatePixelElement(dt, dk)));                      \
251}
252
253#define TEST_UNSUPPORTED_PIXEL(dt, dk) {                                       \
254    _RS_ASSERT(!rsIsObject(rsCreatePixelElement(dt, dk)));                     \
255}
256
257#define TEST_HELPER(suffix) {                                     \
258    _RS_ASSERT(rsIsObject(rsCreateAllocation_##suffix(3)));       \
259    _RS_ASSERT(rsIsObject(rsCreateAllocation_##suffix(3, 4)));    \
260    _RS_ASSERT(rsIsObject(rsCreateAllocation_##suffix(3, 4, 5))); \
261    }
262
263#define TEST_HELPERS(CT) { \
264    TEST_HELPER(CT);       \
265    TEST_HELPER(CT##2);    \
266    TEST_HELPER(CT##3);    \
267    TEST_HELPER(CT##4);    \
268}
269
270void TestAllCases() {
271    // vector_width must be at least 2
272    rs_element oneElt = rsCreateVectorElement(RS_TYPE_SIGNED_32, 1);
273    _RS_ASSERT(!rsIsObject(oneElt));
274
275    // vector_width must be at most 4
276    rs_element fiveElt = rsCreateVectorElement(RS_TYPE_SIGNED_32, 5);
277    _RS_ASSERT(!rsIsObject(fiveElt));
278
279    /////////////////////////////////////////////////////////////////
280    // Element validation
281    /////////////////////////////////////////////////////////////////
282    // These types are valid for scalar and vectors, but don't support pixel
283    TEST_DATA_TYPE(RS_TYPE_BOOLEAN,     true, true, false);
284    TEST_DATA_TYPE(RS_TYPE_FLOAT_32,    true, true, false);
285    TEST_DATA_TYPE(RS_TYPE_FLOAT_64,    true, true, false);
286    TEST_DATA_TYPE(RS_TYPE_SIGNED_8,    true, true, false);
287    TEST_DATA_TYPE(RS_TYPE_SIGNED_16,   true, true, false);
288    TEST_DATA_TYPE(RS_TYPE_SIGNED_32,   true, true, false);
289    TEST_DATA_TYPE(RS_TYPE_SIGNED_64,   true, true, false);
290    TEST_DATA_TYPE(RS_TYPE_UNSIGNED_32, true, true, false);
291    TEST_DATA_TYPE(RS_TYPE_UNSIGNED_64, true, true, false);
292
293    // These types are valid only for scalars
294    TEST_DATA_TYPE(RS_TYPE_MATRIX_4X4, true, false, false);
295    TEST_DATA_TYPE(RS_TYPE_MATRIX_3X3, true, false, false);
296    TEST_DATA_TYPE(RS_TYPE_MATRIX_2X2, true, false, false);
297    TEST_DATA_TYPE(RS_TYPE_ELEMENT,    true, false, false);
298    TEST_DATA_TYPE(RS_TYPE_TYPE,       true, false, false);
299    TEST_DATA_TYPE(RS_TYPE_ALLOCATION, true, false, false);
300    TEST_DATA_TYPE(RS_TYPE_SCRIPT,     true, false, false);
301
302    // U8, U16 are valid for scalar, vector and pixel
303    TEST_DATA_TYPE(RS_TYPE_UNSIGNED_8,  true, true, true);
304    TEST_DATA_TYPE(RS_TYPE_UNSIGNED_16, true, true, true);
305
306    // These data types are only for pixels and a particular data_kind
307    TEST_SUPPORTED_PIXEL  (RS_TYPE_UNSIGNED_5_6_5,   RS_KIND_PIXEL_RGB);
308    TEST_UNSUPPORTED_PIXEL(RS_TYPE_UNSIGNED_5_6_5,   RS_KIND_PIXEL_L);
309    TEST_SUPPORTED_PIXEL  (RS_TYPE_UNSIGNED_5_5_5_1, RS_KIND_PIXEL_RGBA);
310    TEST_UNSUPPORTED_PIXEL(RS_TYPE_UNSIGNED_5_5_5_1, RS_KIND_PIXEL_L);
311    TEST_SUPPORTED_PIXEL  (RS_TYPE_UNSIGNED_4_4_4_4, RS_KIND_PIXEL_RGBA);
312    TEST_UNSUPPORTED_PIXEL(RS_TYPE_UNSIGNED_4_4_4_4, RS_KIND_PIXEL_L);
313    TEST_SUPPORTED_PIXEL  (RS_TYPE_UNSIGNED_16,      RS_KIND_PIXEL_DEPTH);
314    TEST_UNSUPPORTED_PIXEL(RS_TYPE_UNSIGNED_4_4_4_4, RS_KIND_PIXEL_L);
315
316    // These data types are not supported from single-source
317    TEST_DATA_TYPE(RS_TYPE_NONE,             false, false, false);
318    TEST_DATA_TYPE(RS_TYPE_SAMPLER,          false, false, false);
319    TEST_DATA_TYPE(RS_TYPE_MESH,             false, false, false);
320    TEST_DATA_TYPE(RS_TYPE_PROGRAM_FRAGMENT, false, false, false);
321    TEST_DATA_TYPE(RS_TYPE_PROGRAM_VERTEX,   false, false, false);
322    TEST_DATA_TYPE(RS_TYPE_PROGRAM_RASTER,   false, false, false);
323    TEST_DATA_TYPE(RS_TYPE_PROGRAM_STORE,    false, false, false);
324    TEST_DATA_TYPE(RS_TYPE_FONT,             false, false, false);
325    TEST_DATA_TYPE(RS_TYPE_INVALID,          false, false, false);
326
327    /////////////////////////////////////////////////////////////////
328    // Test rs_type creation
329    /////////////////////////////////////////////////////////////////
330    rs_element I32_3 = rsCreateVectorElement(RS_TYPE_SIGNED_32, 3);
331
332    // Create 1D, 2D, 3D types
333    _RS_ASSERT(rsIsObject(rsCreateType(I32_3, 3)));
334    _RS_ASSERT(rsIsObject(rsCreateType(I32_3, 3, 4)));
335    _RS_ASSERT(rsIsObject(rsCreateType(I32_3, 3, 4, 5)));
336    _RS_ASSERT(rsIsObject(rsCreateType(I32_3, 0))); // x = 0 is allowed
337
338    // Invalid dimensionality
339    _RS_ASSERT(!rsIsObject(rsCreateType(I32_3, 0, 4))); // x is 0 but y isn't
340    _RS_ASSERT(!rsIsObject(rsCreateType(I32_3, 0, 4, 5))); // x is 0 but z isn't
341    _RS_ASSERT(!rsIsObject(rsCreateType(I32_3, 3, 0, 5))); // y is 0 but z isn't
342
343    // shape attributes
344    // Valid yuv_format
345    _RS_ASSERT(rsIsObject(rsCreateType(I32_3, 3, 4, 0, false, false,
346                    RS_YUV_NONE)));
347    _RS_ASSERT(rsIsObject(rsCreateType(I32_3, 3, 4, 0, false, false,
348                    RS_YUV_YV12)));
349    _RS_ASSERT(rsIsObject(rsCreateType(I32_3, 3, 4, 0, false, false,
350                    RS_YUV_NV21)));
351    _RS_ASSERT(rsIsObject(rsCreateType(I32_3, 3, 4, 0, false, false,
352                    RS_YUV_420_888)));
353
354    // Invalid yuv_format
355    _RS_ASSERT(!rsIsObject(rsCreateType(I32_3, 3, 4, 5, false, false, 1024)));
356    // yuv_format with 1D or 3D is invalid
357    _RS_ASSERT(!rsIsObject(rsCreateType(I32_3, 3, 0, 0, false, false,
358                    RS_YUV_YV12)));
359    _RS_ASSERT(!rsIsObject(rsCreateType(I32_3, 3, 4, 5, false, false,
360                    RS_YUV_YV12)));
361
362    // yuv_format with mipmaps or cubemap is invalid
363    _RS_ASSERT(!rsIsObject(rsCreateType(I32_3, 3, 4, 0, false, true,
364                    RS_YUV_YV12)));
365    _RS_ASSERT(!rsIsObject(rsCreateType(I32_3, 3, 4, 0, true, false,
366                    RS_YUV_YV12)));
367
368    // mipmaps with 1D or 3D is invalid
369    _RS_ASSERT(!rsIsObject(rsCreateType(I32_3, 3, 0, 0, true, false,
370                    RS_YUV_NONE)));
371    _RS_ASSERT(!rsIsObject(rsCreateType(I32_3, 3, 4, 5, true, false,
372                    RS_YUV_NONE)));
373
374    // cubemap with 1D or 3D is invalid
375    _RS_ASSERT(!rsIsObject(rsCreateType(I32_3, 3, 0, 0, false, true,
376                    RS_YUV_NONE)));
377    _RS_ASSERT(!rsIsObject(rsCreateType(I32_3, 3, 4, 5, false, true,
378                    RS_YUV_NONE)));
379
380    /////////////////////////////////////////////////////////////////
381    // Test rs_allocation creation
382    /////////////////////////////////////////////////////////////////
383    rs_type I32_3_2D = rsCreateType(I32_3, 3, 4);
384
385    // Valid uses
386    _RS_ASSERT(rsIsObject(rsCreateAllocation(I32_3_2D)));
387    _RS_ASSERT(rsIsObject(rsCreateAllocation(I32_3_2D,\
388                    (uint32_t) RS_ALLOCATION_USAGE_SCRIPT)));
389    _RS_ASSERT(rsIsObject(rsCreateAllocation(I32_3_2D,\
390                    (uint32_t) RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE)));
391    _RS_ASSERT(rsIsObject(rsCreateAllocation(I32_3_2D,
392                    (uint32_t) RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE |
393                               RS_ALLOCATION_USAGE_SCRIPT)));
394
395    // Invalid uses
396    _RS_ASSERT(!rsIsObject(rsCreateAllocation(I32_3_2D,
397                    (uint32_t) RS_ALLOCATION_USAGE_GRAPHICS_VERTEX)));
398    _RS_ASSERT(!rsIsObject(rsCreateAllocation(I32_3_2D,
399                    (uint32_t) RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS)));
400    _RS_ASSERT(!rsIsObject(rsCreateAllocation(I32_3_2D,
401                    (uint32_t) RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET)));
402    _RS_ASSERT(!rsIsObject(rsCreateAllocation(I32_3_2D,
403                    (uint32_t) RS_ALLOCATION_USAGE_IO_INPUT)));
404    _RS_ASSERT(!rsIsObject(rsCreateAllocation(I32_3_2D,
405                    (uint32_t) RS_ALLOCATION_USAGE_IO_OUTPUT)));
406    _RS_ASSERT(!rsIsObject(rsCreateAllocation(I32_3_2D,
407                    (uint32_t) RS_ALLOCATION_USAGE_SHARED)));
408
409#ifndef RSTEST_COMPAT
410    TEST_HELPER(half);
411#endif
412    TEST_HELPERS(float);
413    TEST_HELPERS(double);
414    TEST_HELPERS(char);
415    TEST_HELPERS(short);
416    TEST_HELPERS(int);
417    TEST_HELPERS(long);
418    TEST_HELPERS(uchar);
419    TEST_HELPERS(ushort);
420    TEST_HELPERS(uint);
421    TEST_HELPERS(ulong);
422}
423
424void single_source_alloc_test() {
425    if (failed) {
426        rsDebug("Single Source Alloc Test Failed", 0);
427        rsSendToClientBlocking(RS_MSG_TEST_FAILED);
428    }
429    else {
430        rsDebug("Single Source Alloc Test Passed", 0);
431        rsSendToClientBlocking(RS_MSG_TEST_PASSED);
432    }
433}
434