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