Home | History | Annotate | Download | only in jnikernelvariables
      1 /*
      2 * Copyright (C) 2016 The Android Open Source Project
      3 *
      4 * Licensed under the Apache License, Version 2.0 (the "License");
      5 * you may not use this file except in compliance with the License.
      6 * You may obtain a copy of the License at
      7 *
      8 *      http://www.apache.org/licenses/LICENSE-2.0
      9 *
     10 * Unless required by applicable law or agreed to in writing, software
     11 * distributed under the License is distributed on an "AS IS" BASIS,
     12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 * See the License for the specific language governing permissions and
     14 * limitations under the License.
     15 */
     16 
     17 #include <memory>
     18 
     19 #include <jni.h>
     20 #include <RenderScript.h>
     21 
     22 #include "ScriptC_simple.h"
     23 
     24 extern "C" void JNICALL
     25 Java_com_android_rs_jnikernelvariables_MainActivity_nativeRS(
     26     JNIEnv * env,
     27     jclass,
     28     jstring pathObj)
     29 {
     30     static const int size = 64;
     31     sp<RS> rs = new RS();
     32 
     33     const char * path = env->GetStringUTFChars(pathObj, nullptr);
     34     rs->init(path, RS_INIT_LOW_LATENCY | RS_INIT_WAIT_FOR_ATTACH);
     35     env->ReleaseStringUTFChars(pathObj, path);
     36 
     37     auto e = Element::RGBA_8888(rs);
     38     Type::Builder tb(rs, e);
     39     tb.setX(size);
     40     tb.setY(size);
     41     auto t = tb.create();
     42 
     43     auto a = Allocation::createTyped(rs, t);
     44     auto b = Allocation::createTyped(rs, t);
     45 
     46     sp<ScriptC_simple> s = new ScriptC_simple(rs);
     47 
     48     static const int buffer_int[] = {1, 2, 3, 4};
     49     sp<Allocation> int_allocation = Allocation::createSized(rs, Element::I32(rs), 4);
     50     int_allocation->copy1DRangeFrom(0, 4, buffer_int);
     51     s->set_allocation_1D_global(int_allocation);
     52 
     53     static const int buffer_int2[] = {5, 6, 7, 8};
     54 
     55     Type::Builder typeI32Builder2D(rs, Element::I32(rs));
     56     typeI32Builder2D.setX(2);
     57     typeI32Builder2D.setY(2);
     58 
     59     sp<Allocation> int_allocation2 = Allocation::createTyped(rs, typeI32Builder2D.create());
     60     int_allocation2->copy2DRangeFrom(0, 0, 2, 2, buffer_int2);
     61     s->set_allocation_1D_global2(int_allocation2);
     62 
     63     s->set_allocation_2D_global(a);
     64     s->set_allocation_2D_global2(b);
     65 
     66     static const int buffer_int3[] = {9, 10, 11, 12, 13, 14, 15, 16};
     67 
     68     Type::Builder typeI32Builder3D(rs, Element::I32(rs));
     69     typeI32Builder3D.setX(2);
     70     typeI32Builder3D.setY(2);
     71     typeI32Builder3D.setZ(2);
     72 
     73     sp<Allocation> int_allocation3 = Allocation::createTyped(rs, typeI32Builder3D.create());
     74     int_allocation3->copy3DRangeFrom(0, 0, 0, 2, 2, 2, buffer_int3);
     75     s->set_allocation_3D_global(int_allocation3);
     76 
     77     Type::Builder yuvTypeBuilder(rs, Element::YUV(rs));
     78     yuvTypeBuilder.setX(4);
     79     yuvTypeBuilder.setY(4);
     80     yuvTypeBuilder.setYuvFormat(RS_YUV_YV12);
     81 
     82     sp<Allocation> yuv_allocation = Allocation::createTyped(rs, yuvTypeBuilder.create());
     83     s->set_allocation_YUV_2D_global(yuv_allocation);
     84 
     85     s->set_sampler_global(Sampler::CLAMP_LINEAR(rs));
     86 
     87     // Script is executed once, then the data is copied back when finished
     88     s->forEach_kernel(a, b);
     89     rs->finish();
     90     uint32_t * output = new uint32_t[size*size];
     91     b->copy2DRangeTo(0, 0, size, size, output);
     92     delete [] output;
     93 }
     94 
     95