1
2#include "RenderScript.h"
3
4#include "ScriptC_mono.h"
5
6using namespace android;
7using namespace RSC;
8
9int test_compute()
10{
11    bool failed = false;
12
13    {
14        sp<RS> rs = new RS();
15        printf("New RS %p\n", rs.get());
16
17        // only legitimate because this is a standalone executable
18        bool r = rs->init("/system/bin");
19        printf("Init returned %i\n", r);
20
21        sp<const Element> e = Element::RGBA_8888(rs);
22        printf("Element %p\n", e.get());
23
24        Type::Builder tb(rs, e);
25        tb.setX(128);
26        tb.setY(128);
27        sp<const Type> t = tb.create();
28        printf("Type %p\n", t.get());
29
30
31        sp<Allocation> a1 = Allocation::createSized(rs, e, 1000);
32        printf("Allocation %p\n", a1.get());
33
34        sp<Allocation> ain = Allocation::createTyped(rs, t);
35        sp<Allocation> aout = Allocation::createTyped(rs, t);
36        printf("Allocation %p %p\n", ain.get(), aout.get());
37
38        sp<ScriptC_mono> sc = new ScriptC_mono(rs);
39        printf("new script\n");
40
41        sc->set_alloc(a1);
42        sc->set_elem(e);
43        sc->set_type(t);
44        sc->set_script(sc);
45        sc->set_script(nullptr);
46        sp<const Sampler> samp = Sampler::CLAMP_NEAREST(rs);
47        sc->set_sampler(samp);
48
49        // We read back the status from the script-side via a "failed" allocation.
50        sp<const Element> failed_e = Element::BOOLEAN(rs);
51        Type::Builder failed_tb(rs, failed_e);
52        failed_tb.setX(1);
53        sp<const Type> failed_t = failed_tb.create();
54        sp<Allocation> failed_alloc = Allocation::createTyped(rs, failed_t);
55
56        failed_alloc->copy1DRangeFrom(0, failed_t->getCount(), &failed);
57        sc->bind_failed(failed_alloc);
58
59        uint32_t *buf = new uint32_t[t->getCount()];
60        for (uint32_t ct=0; ct < t->getCount(); ct++) {
61            buf[ct] = ct | (ct << 16);
62        }
63        ain->copy1DRangeFrom(0, t->getCount(), buf);
64        delete [] buf;
65
66        sc->forEach_root(ain, aout);
67
68        sc->invoke_foo(99, 3.1f);
69        sc->set_g_f(39.9f);
70        sc->set_g_i(-14);
71        sc->invoke_foo(99, 3.1f);
72        printf("for each done\n");
73
74        sc->invoke_bar(47, -3, 'c', -7, 14, -8);
75
76        // Verify a simple kernel.
77        {
78            static const uint32_t xDim = 7;
79            static const uint32_t yDim = 7;
80            sp<const Element> e = Element::I32(rs);
81            Type::Builder tb(rs, e);
82            tb.setX(xDim);
83            tb.setY(yDim);
84            sp<const Type> t = tb.create();
85            sp<Allocation> kern1_in = Allocation::createTyped(rs, t);
86            sp<Allocation> kern1_out = Allocation::createTyped(rs, t);
87
88            int *buf = new int[t->getCount()];
89            for (uint32_t ct=0; ct < t->getCount(); ct++) {
90                buf[ct] = 5;
91            }
92            kern1_in->copy2DRangeFrom(0, 0, xDim, yDim, buf);
93            delete [] buf;
94
95            sc->forEach_kern1(kern1_in, kern1_out);
96            sc->forEach_verify_kern1(kern1_out);
97
98            rs->finish();
99            failed_alloc->copy1DTo(&failed);
100        }
101    }
102
103    return failed;
104}
105
106int main() {
107    bool failed = test_compute();
108
109    if (failed) {
110        printf("TEST FAILED!\n");
111    } else {
112        printf("TEST PASSED!\n");
113    }
114
115    return failed;
116}
117