Home | History | Annotate | Download | only in cppbasic-getpointer
      1 
      2 #include "RenderScript.h"
      3 
      4 #include "ScriptC_mono.h"
      5 
      6 #include <stdlib.h>
      7 
      8 using namespace android;
      9 using namespace RSC;
     10 
     11 const uint32_t DIMX = 128;
     12 const uint32_t DIMY = 128;
     13 
     14 int test_compute()
     15 {
     16     bool failed = false;
     17 
     18     sp<RS> rs = new RS();
     19     printf("New RS %p\n", rs.get());
     20 
     21     // only legitimate because this is a standalone executable
     22     bool r = rs->init("/system/bin");
     23     printf("Init returned %i\n", r);
     24 
     25     sp<const Element> e = Element::U32(rs);
     26     printf("Element %p\n", e.get());
     27 
     28     Type::Builder tb(rs, e);
     29     tb.setX(DIMX);
     30     tb.setY(DIMY);
     31     sp<const Type> t = tb.create();
     32     printf("Type %p\n", t.get());
     33 
     34 
     35     sp<Allocation> a1 = Allocation::createSized(rs, e, 1000);
     36     printf("Allocation %p\n", a1.get());
     37 
     38     sp<Allocation> ain = Allocation::createTyped(rs, t, RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_SHARED);
     39     sp<Allocation> aout = Allocation::createTyped(rs, t, RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_SHARED);
     40     printf("Allocation %p %p\n", ain.get(), aout.get());
     41 
     42     size_t inputStride, outputStride;
     43 
     44     uint32_t *input = (uint32_t*)ain->getPointer(&inputStride);
     45     uint32_t *output = (uint32_t*)aout->getPointer(&outputStride);
     46 
     47     printf("Input pointer: %p\n", input);
     48     printf("Input stride: %u\n", inputStride);
     49     printf("Output pointer: %p\n", output);
     50     printf("Output stride: %u\n", outputStride);
     51 
     52     inputStride /= sizeof(uint32_t);
     53     outputStride /= sizeof(uint32_t);
     54 
     55     for (size_t i = 0; i < DIMY; i++) {
     56         for (size_t j = 0; j < DIMX; j++) {
     57             input[i * inputStride + j] = rand();
     58             output[i * inputStride + j] = 0;
     59         }
     60     }
     61 
     62     ain->syncAll(RS_ALLOCATION_USAGE_SHARED);
     63     aout->syncAll(RS_ALLOCATION_USAGE_SHARED);
     64 
     65     printf("Launching script\n");
     66 
     67     sp<ScriptC_mono> sc = new ScriptC_mono(rs);
     68     sc->forEach_copyAndNot(ain, aout);
     69     rs->finish();
     70 
     71     printf("Script completed\n");
     72 
     73     ain->syncAll(RS_ALLOCATION_USAGE_SCRIPT);
     74     aout->syncAll(RS_ALLOCATION_USAGE_SCRIPT);
     75 
     76     for (size_t i = 0; i < DIMY; i++) {
     77         for (size_t j = 0; j < DIMX; j++) {
     78             if (input[i * inputStride + j] != ~(output[i * inputStride + j])) {
     79                 printf("Mismatch at location %u, %u\n", j, i);
     80                 failed = true;
     81                 return failed;
     82             }
     83         }
     84     }
     85 
     86 
     87     return failed;
     88 }
     89 
     90 int main() {
     91     bool failed = test_compute();
     92 
     93     if (failed) {
     94         printf("TEST FAILED!\n");
     95     } else {
     96         printf("TEST PASSED!\n");
     97     }
     98 
     99     return failed;
    100 }
    101