Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 1 | |
| 2 | #include "RenderScript.h" |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 3 | |
Jason Sams | f1e6d22 | 2012-02-24 14:24:56 -0800 | [diff] [blame] | 4 | #include "ScriptC_mono.h" |
| 5 | |
Jason Sams | 69cccdf | 2012-04-02 19:11:49 -0700 | [diff] [blame] | 6 | using namespace android; |
Tim Murray | 9eb7f4b | 2012-11-16 14:02:18 -0800 | [diff] [blame] | 7 | using namespace RSC; |
Jason Sams | 69cccdf | 2012-04-02 19:11:49 -0700 | [diff] [blame] | 8 | |
Stephen Hines | 8a588bd | 2013-11-26 15:38:31 -0800 | [diff] [blame] | 9 | int test_compute() |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 10 | { |
Stephen Hines | 43514cd | 2012-11-16 14:33:47 -0800 | [diff] [blame] | 11 | bool failed = false; |
Stephen Hines | 43514cd | 2012-11-16 14:33:47 -0800 | [diff] [blame] | 12 | |
Stephen Hines | 43514cd | 2012-11-16 14:33:47 -0800 | [diff] [blame] | 13 | { |
Tim Murray | caf4126 | 2013-12-13 12:54:37 -0800 | [diff] [blame] | 14 | sp<RS> rs = new RS(); |
| 15 | printf("New RS %p\n", rs.get()); |
Stephen Hines | 43514cd | 2012-11-16 14:33:47 -0800 | [diff] [blame] | 16 | |
Tim Murray | caf4126 | 2013-12-13 12:54:37 -0800 | [diff] [blame] | 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(NULL); |
| 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()]; |
Stephen Hines | 43514cd | 2012-11-16 14:33:47 -0800 | [diff] [blame] | 60 | for (uint32_t ct=0; ct < t->getCount(); ct++) { |
Tim Murray | caf4126 | 2013-12-13 12:54:37 -0800 | [diff] [blame] | 61 | buf[ct] = ct | (ct << 16); |
Stephen Hines | 43514cd | 2012-11-16 14:33:47 -0800 | [diff] [blame] | 62 | } |
Tim Murray | caf4126 | 2013-12-13 12:54:37 -0800 | [diff] [blame] | 63 | ain->copy1DRangeFrom(0, t->getCount(), buf); |
Stephen Hines | 43514cd | 2012-11-16 14:33:47 -0800 | [diff] [blame] | 64 | delete [] buf; |
| 65 | |
Tim Murray | caf4126 | 2013-12-13 12:54:37 -0800 | [diff] [blame] | 66 | sc->forEach_root(ain, aout); |
Stephen Hines | 43514cd | 2012-11-16 14:33:47 -0800 | [diff] [blame] | 67 | |
Tim Murray | caf4126 | 2013-12-13 12:54:37 -0800 | [diff] [blame] | 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 | } |
Stephen Hines | 43514cd | 2012-11-16 14:33:47 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Stephen Hines | 8a588bd | 2013-11-26 15:38:31 -0800 | [diff] [blame] | 103 | return failed; |
| 104 | } |
| 105 | |
| 106 | int main() { |
| 107 | bool failed = test_compute(); |
Stephen Hines | 43514cd | 2012-11-16 14:33:47 -0800 | [diff] [blame] | 108 | |
| 109 | if (failed) { |
| 110 | printf("TEST FAILED!\n"); |
| 111 | } else { |
| 112 | printf("TEST PASSED!\n"); |
| 113 | } |
| 114 | |
| 115 | return failed; |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 116 | } |