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