1 /*
2 * Copyright (C) 2016 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 <RenderScript.h>
18
19 #include "ScriptC_simple.h"
20
main()21 int main()
22 {
23 static const int size = 64;
24 sp<RS> rs = new RS();
25
26 rs->init("/data/rscache", RS_INIT_LOW_LATENCY | RS_INIT_WAIT_FOR_ATTACH);
27
28 auto e = Element::RGBA_8888(rs);
29 Type::Builder tb(rs, e);
30 tb.setX(size);
31 tb.setY(size);
32 auto t = tb.create();
33
34 auto a = Allocation::createTyped(rs, t);
35 auto b = Allocation::createTyped(rs, t);
36
37 sp<ScriptC_simple> s = new ScriptC_simple(rs);
38
39 static const int buffer_int[] = {1, 2, 3, 4};
40 sp<Allocation> int_allocation = Allocation::createSized(rs, Element::I32(rs), 4);
41 int_allocation->copy1DRangeFrom(0, 4, buffer_int);
42 s->set_allocation_1D_global(int_allocation);
43
44 static const int buffer_int2[] = {5, 6, 7, 8};
45
46 Type::Builder typeI32Builder2D(rs, Element::I32(rs));
47 typeI32Builder2D.setX(2);
48 typeI32Builder2D.setY(2);
49
50 sp<Allocation> int_allocation2 = Allocation::createTyped(rs, typeI32Builder2D.create());
51 int_allocation2->copy2DRangeFrom(0, 0, 2, 2, buffer_int2);
52 s->set_allocation_1D_global2(int_allocation2);
53
54 s->set_allocation_2D_global(a);
55 s->set_allocation_2D_global2(b);
56
57 static const int buffer_int3[] = {9, 10, 11, 12, 13, 14, 15, 16};
58
59 Type::Builder typeI32Builder3D(rs, Element::I32(rs));
60 typeI32Builder3D.setX(2);
61 typeI32Builder3D.setY(2);
62 typeI32Builder3D.setZ(2);
63
64 sp<Allocation> int_allocation3 = Allocation::createTyped(rs, typeI32Builder3D.create());
65 int_allocation3->copy3DRangeFrom(0, 0, 0, 2, 2, 2, buffer_int3);
66 s->set_allocation_3D_global(int_allocation3);
67
68 Type::Builder yuvTypeBuilder(rs, Element::YUV(rs));
69 yuvTypeBuilder.setX(4);
70 yuvTypeBuilder.setY(4);
71 yuvTypeBuilder.setYuvFormat(RS_YUV_YV12);
72
73 sp<Allocation> yuv_allocation = Allocation::createTyped(rs, yuvTypeBuilder.create());
74 s->set_allocation_YUV_2D_global(yuv_allocation);
75
76 s->set_sampler_global(Sampler::CLAMP_LINEAR(rs));
77
78 // Script is executed once, then the data is copied back when finished
79 s->forEach_kernel(a, b);
80 rs->finish();
81 uint32_t * output = new uint32_t[size*size];
82 b->copy2DRangeTo(0, 0, size, size, output);
83 delete [] output;
84
85 return 0;
86 }
87
88