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