Home | History | Annotate | Download | only in librscpptest
      1 /*
      2  * Copyright (C) 2013 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 <jni.h>
     18 #include <android/log.h>
     19 
     20 #include <stdio.h>
     21 #include <stdlib.h>
     22 #include <math.h>
     23 
     24 #include <RenderScript.h>
     25 
     26 #define  LOG_TAG    "rscpptest"
     27 #define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
     28 #define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
     29 
     30 using namespace android::RSC;
     31 
     32 /*returns an addr aligned to the byte boundary specified by align*/
     33 #define align_addr(addr,align) (void *)(((size_t)(addr) + ((align) - 1)) & (size_t) - (align))
     34 #define ADDRESS_STORAGE_SIZE sizeof(size_t)
     35 
     36 void * aligned_alloc(size_t align, size_t size) {
     37     void * addr, * x = NULL;
     38     addr = malloc(size + align - 1 + ADDRESS_STORAGE_SIZE);
     39     if (addr) {
     40         x = align_addr((unsigned char *) addr + ADDRESS_STORAGE_SIZE, (int) align);
     41         /* save the actual malloc address */
     42         ((size_t *) x)[-1] = (size_t) addr;
     43     }
     44     return x;
     45 }
     46 
     47 void aligned_free(void * memblk) {
     48     if (memblk) {
     49         void * addr = (void *) (((size_t *) memblk)[-1]);
     50         free(addr);
     51     }
     52 }
     53 
     54 extern "C" JNIEXPORT jboolean JNICALL Java_android_cts_rscpp_RSInitTest_initTest(JNIEnv * env,
     55                                                                                  jclass obj,
     56                                                                                  jstring pathObj)
     57 {
     58     const char * path = env->GetStringUTFChars(pathObj, NULL);
     59     bool r = true;
     60     for (int i = 0; i < 1000; i++) {
     61         sp<RS> rs = new RS();
     62         r &= rs->init(path);
     63         LOGE("Native iteration %i, returned %i", i, (int)r);
     64     }
     65     env->ReleaseStringUTFChars(pathObj, path);
     66     return r;
     67 }
     68 
     69 extern "C" JNIEXPORT jboolean JNICALL Java_android_cts_rscpp_RSBlurTest_blurTest(JNIEnv * env,
     70                                                                                  jclass obj,
     71                                                                                  jstring pathObj,
     72                                                                                  jint X,
     73                                                                                  jint Y,
     74                                                                                  jbyteArray inputByteArray,
     75                                                                                  jbyteArray outputByteArray,
     76                                                                                  jboolean singleChannel)
     77 {
     78     const char * path = env->GetStringUTFChars(pathObj, NULL);
     79     jbyte * input = (jbyte *) env->GetPrimitiveArrayCritical(inputByteArray, 0);
     80     jbyte * output = (jbyte *) env->GetPrimitiveArrayCritical(outputByteArray, 0);
     81 
     82     sp<RS> rs = new RS();
     83     rs->init(path);
     84 
     85     sp<const Element> e;
     86     if (singleChannel) {
     87         e = Element::A_8(rs);
     88     } else {
     89         e = Element::RGBA_8888(rs);
     90     }
     91 
     92     sp<Allocation> inputAlloc = Allocation::createSized2D(rs, e, X, Y);
     93     sp<Allocation> outputAlloc = Allocation::createSized2D(rs, e, X, Y);
     94     sp<ScriptIntrinsicBlur> blur = ScriptIntrinsicBlur::create(rs, e);
     95 
     96     inputAlloc->copy2DRangeFrom(0, 0, X, Y, input);
     97 
     98     blur->setRadius(15);
     99     blur->setInput(inputAlloc);
    100     blur->forEach(outputAlloc);
    101     outputAlloc->copy2DRangeTo(0, 0, X, Y, output);
    102 
    103     env->ReleasePrimitiveArrayCritical(inputByteArray, input, 0);
    104     env->ReleasePrimitiveArrayCritical(outputByteArray, output, 0);
    105     env->ReleaseStringUTFChars(pathObj, path);
    106     return (rs->getError() == RS_SUCCESS);
    107 
    108 }
    109 
    110 extern "C" JNIEXPORT jboolean JNICALL
    111 Java_android_cts_rscpp_RSConvolveTest_convolveTest(JNIEnv * env, jclass obj, jstring pathObj,
    112                                                    jint X, jint Y, jbyteArray inputByteArray,
    113                                                    jbyteArray outputByteArray,
    114                                                    jfloatArray coeffArray,
    115                                                    jboolean is3x3)
    116 {
    117     const char * path = env->GetStringUTFChars(pathObj, NULL);
    118     jfloat * coeffs = env->GetFloatArrayElements(coeffArray, NULL);
    119     jbyte * input = (jbyte *) env->GetPrimitiveArrayCritical(inputByteArray, 0);
    120     jbyte * output = (jbyte *) env->GetPrimitiveArrayCritical(outputByteArray, 0);
    121 
    122 
    123     sp<RS> rs = new RS();
    124     rs->init(path);
    125 
    126     sp<const Element> e = Element::A_8(rs);
    127 
    128     sp<Allocation> inputAlloc = Allocation::createSized2D(rs, e, X, Y);
    129     sp<Allocation> outputAlloc = Allocation::createSized2D(rs, e, X, Y);
    130 
    131     inputAlloc->copy2DRangeFrom(0, 0, X, Y, input);
    132 
    133 
    134     if (is3x3) {
    135         sp<ScriptIntrinsicConvolve3x3> convolve = ScriptIntrinsicConvolve3x3::create(rs, e);
    136         convolve->setInput(inputAlloc);
    137         convolve->setCoefficients(coeffs);
    138         convolve->forEach(outputAlloc);
    139     } else {
    140         sp<ScriptIntrinsicConvolve5x5> convolve = ScriptIntrinsicConvolve5x5::create(rs, e);
    141         convolve->setInput(inputAlloc);
    142         convolve->setCoefficients(coeffs);
    143         convolve->forEach(outputAlloc);
    144     }
    145 
    146     outputAlloc->copy2DRangeTo(0, 0, X, Y, output);
    147 
    148     env->ReleasePrimitiveArrayCritical(inputByteArray, input, 0);
    149     env->ReleasePrimitiveArrayCritical(outputByteArray, output, 0);
    150     env->ReleaseFloatArrayElements(coeffArray, coeffs, JNI_ABORT);
    151     env->ReleaseStringUTFChars(pathObj, path);
    152     return (rs->getError() == RS_SUCCESS);
    153 
    154 }
    155 
    156 extern "C" JNIEXPORT jboolean JNICALL Java_android_cts_rscpp_RSLUTTest_lutTest(JNIEnv * env,
    157                                                                                jclass obj,
    158                                                                                jstring pathObj,
    159                                                                                jint X,
    160                                                                                jint Y,
    161                                                                                jbyteArray inputByteArray,
    162                                                                                jbyteArray outputByteArray)
    163 {
    164     const char * path = env->GetStringUTFChars(pathObj, NULL);
    165     jbyte * input = (jbyte *) env->GetPrimitiveArrayCritical(inputByteArray, 0);
    166     jbyte * output = (jbyte *) env->GetPrimitiveArrayCritical(outputByteArray, 0);
    167 
    168     sp<RS> rs = new RS();
    169     rs->init(path);
    170 
    171     sp<const Element> e = Element::RGBA_8888(rs);
    172 
    173     sp<Allocation> inputAlloc = Allocation::createSized2D(rs, e, X, Y);
    174     sp<Allocation> outputAlloc = Allocation::createSized2D(rs, e, X, Y);
    175     sp<ScriptIntrinsicLUT> lut = ScriptIntrinsicLUT::create(rs, e);
    176 
    177     inputAlloc->copy2DRangeFrom(0, 0, X, Y, input);
    178     unsigned char lutValues[256];
    179     for (int i = 0; i < 256; i++) {
    180         lutValues[i] = 255-i;
    181     }
    182     lut->setRed(0, 256, lutValues);
    183     lut->setGreen(0, 256, lutValues);
    184     lut->setBlue(0, 256, lutValues);
    185 
    186     lut->forEach(inputAlloc,outputAlloc);
    187     outputAlloc->copy2DRangeTo(0, 0, X, Y, output);
    188 
    189     env->ReleasePrimitiveArrayCritical(inputByteArray, input, 0);
    190     env->ReleasePrimitiveArrayCritical(outputByteArray, output, 0);
    191     env->ReleaseStringUTFChars(pathObj, path);
    192     return (rs->getError() == RS_SUCCESS);
    193 
    194 }
    195 
    196 extern "C" JNIEXPORT jboolean JNICALL Java_android_cts_rscpp_RS3DLUTTest_lutTest(JNIEnv * env,
    197                                                                                  jclass obj,
    198                                                                                  jstring pathObj,
    199                                                                                  jint X,
    200                                                                                  jint Y,
    201                                                                                  jint lutSize,
    202                                                                                  jbyteArray inputByteArray,
    203                                                                                  jbyteArray inputByteArray2,
    204                                                                                  jbyteArray outputByteArray)
    205 {
    206     const char * path = env->GetStringUTFChars(pathObj, NULL);
    207     jbyte * input = (jbyte *) env->GetPrimitiveArrayCritical(inputByteArray, 0);
    208     jbyte * input2 = (jbyte *) env->GetPrimitiveArrayCritical(inputByteArray2, 0);
    209     jbyte * output = (jbyte *) env->GetPrimitiveArrayCritical(outputByteArray, 0);
    210 
    211     sp<RS> rs = new RS();
    212     rs->init(path);
    213 
    214     sp<const Element> e = Element::RGBA_8888(rs);
    215 
    216     Type::Builder builder(rs, e);
    217 
    218     builder.setX(lutSize);
    219     builder.setY(lutSize);
    220     builder.setZ(lutSize);
    221 
    222     sp<Allocation> inputAlloc = Allocation::createSized2D(rs, e, X, Y);
    223     sp<Allocation> colorCube = Allocation::createTyped(rs, builder.create());
    224     sp<Allocation> outputAlloc = Allocation::createSized2D(rs, e, X, Y);
    225     sp<ScriptIntrinsic3DLUT> lut = ScriptIntrinsic3DLUT::create(rs, e);
    226 
    227     inputAlloc->copy2DRangeFrom(0, 0, X, Y, input);
    228     colorCube->copy3DRangeFrom(0, 0, 0, lutSize, lutSize, lutSize, input2);
    229 
    230     lut->setLUT(colorCube);
    231     lut->forEach(inputAlloc,outputAlloc);
    232 
    233     outputAlloc->copy2DRangeTo(0, 0, X, Y, output);
    234 
    235     env->ReleasePrimitiveArrayCritical(inputByteArray, input, 0);
    236     env->ReleasePrimitiveArrayCritical(inputByteArray2, input2, 0);
    237     env->ReleasePrimitiveArrayCritical(outputByteArray, output, 0);
    238     env->ReleaseStringUTFChars(pathObj, path);
    239     return (rs->getError() == RS_SUCCESS);
    240 
    241 }
    242 
    243 extern "C" JNIEXPORT jboolean JNICALL
    244 Java_android_cts_rscpp_RSColorMatrixTest_colorMatrixTest(JNIEnv * env, jclass obj, jstring pathObj,
    245                                                          jint X, jint Y, jbyteArray inputByteArray,
    246                                                          jbyteArray outputByteArray,
    247                                                          jfloatArray coeffArray,
    248                                                          jint optionFlag)
    249 {
    250     const char * path = env->GetStringUTFChars(pathObj, NULL);
    251     jfloat * coeffs = env->GetFloatArrayElements(coeffArray, NULL);
    252     jbyte * input = (jbyte *) env->GetPrimitiveArrayCritical(inputByteArray, 0);
    253     jbyte * output = (jbyte *) env->GetPrimitiveArrayCritical(outputByteArray, 0);
    254 
    255     sp<RS> rs = new RS();
    256     rs->init(path);
    257 
    258     sp<const Element> e = Element::RGBA_8888(rs);
    259 
    260     sp<Allocation> inputAlloc = Allocation::createSized2D(rs, e, X, Y);
    261     sp<Allocation> outputAlloc = Allocation::createSized2D(rs, e, X, Y);
    262 
    263     inputAlloc->copy2DRangeFrom(0, 0, X, Y, input);
    264 
    265     sp<ScriptIntrinsicColorMatrix> cm = ScriptIntrinsicColorMatrix::create(rs);
    266     if (optionFlag == 0) {
    267         cm->setColorMatrix3(coeffs);
    268     } else if (optionFlag == 1) {
    269         cm->setGreyscale();
    270     } else if (optionFlag == 2) {
    271         cm->setColorMatrix4(coeffs);
    272     } else if (optionFlag == 3) {
    273         cm->setYUVtoRGB();
    274     } else if (optionFlag == 4) {
    275         cm->setRGBtoYUV();
    276     } else if (optionFlag == 5) {
    277         cm->setColorMatrix4(coeffs);
    278         float add[4] = {5.3f, 2.1f, 0.3f, 4.4f};
    279         cm->setAdd(add);
    280     }
    281     cm->forEach(inputAlloc, outputAlloc);
    282 
    283     outputAlloc->copy2DRangeTo(0, 0, X, Y, output);
    284 
    285     env->ReleasePrimitiveArrayCritical(inputByteArray, input, 0);
    286     env->ReleasePrimitiveArrayCritical(outputByteArray, output, 0);
    287     env->ReleaseFloatArrayElements(coeffArray, coeffs, JNI_ABORT);
    288     env->ReleaseStringUTFChars(pathObj, path);
    289     return (rs->getError() == RS_SUCCESS);
    290 
    291 }
    292 
    293 extern "C" JNIEXPORT jboolean JNICALL
    294 Java_android_cts_rscpp_RSBlendTest_blendTest(JNIEnv * env, jclass obj, jstring pathObj,
    295                                              jint X, jint Y, jbyteArray inputByteArray,
    296                                              jbyteArray outputByteArray,
    297                                              jint optionFlag)
    298 {
    299     const char * path = env->GetStringUTFChars(pathObj, NULL);
    300     jbyte * input = (jbyte *) env->GetPrimitiveArrayCritical(inputByteArray, 0);
    301     jbyte * output = (jbyte *) env->GetPrimitiveArrayCritical(outputByteArray, 0);
    302 
    303     sp<RS> rs = new RS();
    304     rs->init(path);
    305 
    306     sp<const Element> e = Element::RGBA_8888(rs);
    307 
    308     sp<Allocation> inputAlloc = Allocation::createSized2D(rs, e, X, Y);
    309     sp<Allocation> outputAlloc = Allocation::createSized2D(rs, e, X, Y);
    310 
    311     inputAlloc->copy2DRangeFrom(0, 0, X, Y, input);
    312     outputAlloc->copy2DRangeFrom(0, 0, X, Y, output);
    313 
    314     sp<ScriptIntrinsicBlend> blend = ScriptIntrinsicBlend::create(rs, e);
    315     switch(optionFlag) {
    316     case 0:
    317         blend->forEachAdd(inputAlloc, outputAlloc);
    318         break;
    319     case 1:
    320         blend->forEachClear(inputAlloc, outputAlloc);
    321         break;
    322     case 2:
    323         blend->forEachDst(inputAlloc, outputAlloc);
    324         break;
    325     case 3:
    326         blend->forEachDstAtop(inputAlloc, outputAlloc);
    327         break;
    328     case 4:
    329         blend->forEachDstIn(inputAlloc, outputAlloc);
    330         break;
    331     case 5:
    332         blend->forEachDstOut(inputAlloc, outputAlloc);
    333         break;
    334     case 6:
    335         blend->forEachDstOver(inputAlloc, outputAlloc);
    336         break;
    337     case 7:
    338         blend->forEachMultiply(inputAlloc, outputAlloc);
    339         break;
    340     case 8:
    341         blend->forEachSrc(inputAlloc, outputAlloc);
    342         break;
    343     case 9:
    344         blend->forEachSrcAtop(inputAlloc, outputAlloc);
    345         break;
    346     case 10:
    347         blend->forEachSrcIn(inputAlloc, outputAlloc);
    348         break;
    349     case 11:
    350         blend->forEachSrcOut(inputAlloc, outputAlloc);
    351         break;
    352     case 12:
    353         blend->forEachSrcOver(inputAlloc, outputAlloc);
    354         break;
    355     case 13:
    356         blend->forEachSubtract(inputAlloc, outputAlloc);
    357         break;
    358     case 14:
    359         blend->forEachXor(inputAlloc, outputAlloc);
    360         break;
    361     default:
    362         break;
    363     }
    364 
    365     outputAlloc->copy2DRangeTo(0, 0, X, Y, output);
    366 
    367     env->ReleasePrimitiveArrayCritical(inputByteArray, input, 0);
    368     env->ReleasePrimitiveArrayCritical(outputByteArray, output, 0);
    369     env->ReleaseStringUTFChars(pathObj, path);
    370     return (rs->getError() == RS_SUCCESS);
    371 
    372 }
    373 
    374 
    375