1 /*
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 // This file is based on frameworks/rs/tests/cpp_api/cppallocation/compute.cpp.
18
19 #include "RenderScript.h"
20
21 #include "ScriptC_multiply.h"
22
23 sp<RS> rs;
24 sp<const Element> e;
25 sp<const Type> t;
26 sp<Allocation> ain;
27 sp<Allocation> aout;
28 sp<ScriptC_multiply> sc;
29
main(int argc,char ** argv)30 int main(int argc, char** argv)
31 {
32 uint32_t numElems = 1024;
33
34 if (argc >= 2) {
35 int tempNumElems = atoi(argv[1]);
36 if (tempNumElems < 1) {
37 printf("numElems must be greater than 0\n");
38 return 1;
39 }
40 numElems = (uint32_t) tempNumElems;
41 }
42
43 rs = new RS();
44
45 if (!rs->init("/system/bin")) {
46 printf("Could not initialize RenderScript\n");
47 return 1;
48 }
49
50 e = Element::U32(rs);
51
52 Type::Builder tb(rs, e);
53 tb.setX(numElems);
54 t = tb.create();
55
56 ain = Allocation::createTyped(rs, t);
57 aout = Allocation::createTyped(rs, t);
58
59 sc = new ScriptC_multiply(rs);
60
61 uint32_t* buf = new uint32_t[numElems];
62 for (uint32_t ct=0; ct < numElems; ct++) {
63 buf[ct] = (uint32_t)ct;
64 }
65
66 ain->copy1DRangeFrom(0, numElems, buf);
67
68 sc->forEach_multiply(ain, aout);
69
70 aout->copy1DRangeTo(0, numElems, buf);
71
72 for (uint32_t ct=0; ct < numElems; ct++) {
73 if (buf[ct] != ct * 2) {
74 printf("Mismatch at location %d: %u\n", ct, buf[ct]);
75 return 1;
76 }
77 }
78
79 printf("Test successful with %u elems!\n", numElems);
80 }
81