Home | History | Annotate | Download | only in cpp
      1 /*
      2  * Copyright (C) 2012 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 #define LOG_TAG "libRS_cpp"
     18 
     19 #include <utils/Log.h>
     20 #include <malloc.h>
     21 #include <string.h>
     22 
     23 #include "RenderScript.h"
     24 #include "Element.h"
     25 
     26 using namespace android;
     27 using namespace renderscriptCpp;
     28 
     29 sp<const Element> Element::getSubElement(uint32_t index) {
     30     if (!mVisibleElementMap.size()) {
     31         mRS->throwError("Element contains no sub-elements");
     32     }
     33     if (index >= mVisibleElementMap.size()) {
     34         mRS->throwError("Illegal sub-element index");
     35     }
     36     return mElements[mVisibleElementMap[index]];
     37 }
     38 
     39 const char * Element::getSubElementName(uint32_t index) {
     40     if (!mVisibleElementMap.size()) {
     41         mRS->throwError("Element contains no sub-elements");
     42     }
     43     if (index >= mVisibleElementMap.size()) {
     44         mRS->throwError("Illegal sub-element index");
     45     }
     46     return mElementNames[mVisibleElementMap[index]];
     47 }
     48 
     49 size_t Element::getSubElementArraySize(uint32_t index) {
     50     if (!mVisibleElementMap.size()) {
     51         mRS->throwError("Element contains no sub-elements");
     52     }
     53     if (index >= mVisibleElementMap.size()) {
     54         mRS->throwError("Illegal sub-element index");
     55     }
     56     return mArraySizes[mVisibleElementMap[index]];
     57 }
     58 
     59 uint32_t Element::getSubElementOffsetBytes(uint32_t index) {
     60     if (mVisibleElementMap.size()) {
     61         mRS->throwError("Element contains no sub-elements");
     62     }
     63     if (index >= mVisibleElementMap.size()) {
     64         mRS->throwError("Illegal sub-element index");
     65     }
     66     return mOffsetInBytes[mVisibleElementMap[index]];
     67 }
     68 
     69 
     70 #define CREATE_USER(N, T) sp<const Element> Element::N(RenderScript *rs) { \
     71     return createUser(rs, RS_TYPE_##T); \
     72 }
     73 CREATE_USER(BOOLEAN, BOOLEAN);
     74 CREATE_USER(U8, UNSIGNED_8);
     75 CREATE_USER(I8, SIGNED_8);
     76 CREATE_USER(U16, UNSIGNED_16);
     77 CREATE_USER(I16, SIGNED_16);
     78 CREATE_USER(U32, UNSIGNED_32);
     79 CREATE_USER(I32, SIGNED_32);
     80 CREATE_USER(U64, UNSIGNED_64);
     81 CREATE_USER(I64, SIGNED_64);
     82 CREATE_USER(F32, FLOAT_32);
     83 CREATE_USER(F64, FLOAT_64);
     84 CREATE_USER(ELEMENT, ELEMENT);
     85 CREATE_USER(TYPE, TYPE);
     86 CREATE_USER(ALLOCATION, ALLOCATION);
     87 CREATE_USER(SAMPLER, SAMPLER);
     88 CREATE_USER(SCRIPT, SCRIPT);
     89 CREATE_USER(MESH, MESH);
     90 CREATE_USER(PROGRAM_FRAGMENT, PROGRAM_FRAGMENT);
     91 CREATE_USER(PROGRAM_VERTEX, PROGRAM_VERTEX);
     92 CREATE_USER(PROGRAM_RASTER, PROGRAM_RASTER);
     93 CREATE_USER(PROGRAM_STORE, PROGRAM_STORE);
     94 CREATE_USER(MATRIX_4X4, MATRIX_4X4);
     95 CREATE_USER(MATRIX_3X3, MATRIX_3X3);
     96 CREATE_USER(MATRIX_2X2, MATRIX_2X2);
     97 
     98 #define CREATE_PIXEL(N, T, K) sp<const Element> Element::N(RenderScript *rs) { \
     99     return createPixel(rs, RS_TYPE_##T, RS_KIND_##K); \
    100 }
    101 CREATE_PIXEL(A_8, UNSIGNED_8, PIXEL_A);
    102 CREATE_PIXEL(RGB_565, UNSIGNED_5_6_5, PIXEL_RGB);
    103 CREATE_PIXEL(RGB_888, UNSIGNED_8, PIXEL_RGB);
    104 CREATE_PIXEL(RGBA_4444, UNSIGNED_4_4_4_4, PIXEL_RGBA);
    105 CREATE_PIXEL(RGBA_8888, UNSIGNED_8, PIXEL_RGBA);
    106 
    107 #define CREATE_VECTOR(N, T) sp<const Element> Element::N##_2(RenderScript *rs) { \
    108     return createVector(rs, RS_TYPE_##T, 2); \
    109 } \
    110 sp<const Element> Element::N##_3(RenderScript *rs) { \
    111     return createVector(rs, RS_TYPE_##T, 3); \
    112 } \
    113 sp<const Element> Element::N##_4(RenderScript *rs) { \
    114     return createVector(rs, RS_TYPE_##T, 4); \
    115 }
    116 CREATE_VECTOR(U8, UNSIGNED_8);
    117 CREATE_VECTOR(I8, SIGNED_8);
    118 CREATE_VECTOR(U16, UNSIGNED_16);
    119 CREATE_VECTOR(I16, SIGNED_16);
    120 CREATE_VECTOR(U32, UNSIGNED_32);
    121 CREATE_VECTOR(I32, SIGNED_32);
    122 CREATE_VECTOR(U64, UNSIGNED_64);
    123 CREATE_VECTOR(I64, SIGNED_64);
    124 CREATE_VECTOR(F32, FLOAT_32);
    125 CREATE_VECTOR(F64, FLOAT_64);
    126 
    127 
    128 void Element::updateVisibleSubElements() {
    129     if (!mElements.size()) {
    130         return;
    131     }
    132     mVisibleElementMap.clear();
    133 
    134     int noPaddingFieldCount = 0;
    135     size_t fieldCount = mElementNames.size();
    136     // Find out how many elements are not padding
    137     for (size_t ct = 0; ct < fieldCount; ct ++) {
    138         if (mElementNames[ct].string()[0] != '#') {
    139             noPaddingFieldCount ++;
    140         }
    141     }
    142 
    143     // Make a map that points us at non-padding elements
    144     for (size_t ct = 0; ct < fieldCount; ct ++) {
    145         if (mElementNames[ct].string()[0] != '#') {
    146             mVisibleElementMap.push((uint32_t)ct);
    147         }
    148     }
    149 }
    150 
    151 Element::Element(void *id, RenderScript *rs,
    152                  android::Vector<sp</*const*/ Element> > &elements,
    153                  android::Vector<android::String8> &elementNames,
    154                  android::Vector<uint32_t> &arraySizes) : BaseObj(id, rs) {
    155     mSizeBytes = 0;
    156     mVectorSize = 1;
    157     mElements = elements;
    158     mArraySizes = arraySizes;
    159     mElementNames = elementNames;
    160 
    161     mType = RS_TYPE_NONE;
    162     mKind = RS_KIND_USER;
    163 
    164     for (size_t ct = 0; ct < mElements.size(); ct++ ) {
    165         mOffsetInBytes.push(mSizeBytes);
    166         mSizeBytes += mElements[ct]->mSizeBytes * mArraySizes[ct];
    167     }
    168     updateVisibleSubElements();
    169 }
    170 
    171 
    172 static uint32_t GetSizeInBytesForType(RsDataType dt) {
    173     switch(dt) {
    174     case RS_TYPE_NONE:
    175         return 0;
    176     case RS_TYPE_SIGNED_8:
    177     case RS_TYPE_UNSIGNED_8:
    178     case RS_TYPE_BOOLEAN:
    179         return 1;
    180 
    181     case RS_TYPE_FLOAT_16:
    182     case RS_TYPE_SIGNED_16:
    183     case RS_TYPE_UNSIGNED_16:
    184     case RS_TYPE_UNSIGNED_5_6_5:
    185     case RS_TYPE_UNSIGNED_5_5_5_1:
    186     case RS_TYPE_UNSIGNED_4_4_4_4:
    187         return 2;
    188 
    189     case RS_TYPE_FLOAT_32:
    190     case RS_TYPE_SIGNED_32:
    191     case RS_TYPE_UNSIGNED_32:
    192         return 4;
    193 
    194     case RS_TYPE_FLOAT_64:
    195     case RS_TYPE_SIGNED_64:
    196     case RS_TYPE_UNSIGNED_64:
    197         return 8;
    198 
    199     case RS_TYPE_MATRIX_4X4:
    200         return 16 * 4;
    201     case RS_TYPE_MATRIX_3X3:
    202         return 9 * 4;
    203     case RS_TYPE_MATRIX_2X2:
    204         return 4 * 4;
    205 
    206     case RS_TYPE_TYPE:
    207     case RS_TYPE_ALLOCATION:
    208     case RS_TYPE_SAMPLER:
    209     case RS_TYPE_SCRIPT:
    210     case RS_TYPE_MESH:
    211     case RS_TYPE_PROGRAM_FRAGMENT:
    212     case RS_TYPE_PROGRAM_VERTEX:
    213     case RS_TYPE_PROGRAM_RASTER:
    214     case RS_TYPE_PROGRAM_STORE:
    215         return 4;
    216 
    217     default:
    218         break;
    219     }
    220 
    221     ALOGE("Missing type %i", dt);
    222     return 0;
    223 }
    224 
    225 Element::Element(void *id, RenderScript *rs,
    226                  RsDataType dt, RsDataKind dk, bool norm, uint32_t size) :
    227     BaseObj(id, rs)
    228 {
    229     uint32_t tsize = GetSizeInBytesForType(dt);
    230     if ((dt != RS_TYPE_UNSIGNED_5_6_5) &&
    231         (dt != RS_TYPE_UNSIGNED_4_4_4_4) &&
    232         (dt != RS_TYPE_UNSIGNED_5_5_5_1)) {
    233         if (size == 3) {
    234             mSizeBytes = tsize * 4;
    235         } else {
    236             mSizeBytes = tsize * size;
    237         }
    238     } else {
    239         mSizeBytes = tsize;
    240     }
    241     mType = dt;
    242     mKind = dk;
    243     mNormalized = norm;
    244     mVectorSize = size;
    245 }
    246 
    247 Element::~Element() {
    248 }
    249 
    250    /*
    251     Element(int id, RenderScript rs) {
    252         super(id, rs);
    253     }
    254     */
    255 
    256 void Element::updateFromNative() {
    257     BaseObj::updateFromNative();
    258 /*
    259     // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
    260     int[] dataBuffer = new int[5];
    261     mRS.nElementGetNativeData(getID(), dataBuffer);
    262 
    263     mNormalized = dataBuffer[2] == 1 ? true : false;
    264     mVectorSize = dataBuffer[3];
    265     mSize = 0;
    266     for (DataType dt: DataType.values()) {
    267         if(dt.mID == dataBuffer[0]){
    268             mType = dt;
    269             mSize = mType.mSize * mVectorSize;
    270         }
    271     }
    272     for (DataKind dk: DataKind.values()) {
    273         if(dk.mID == dataBuffer[1]){
    274             mKind = dk;
    275         }
    276     }
    277 
    278     int numSubElements = dataBuffer[4];
    279     if(numSubElements > 0) {
    280         mElements = new Element[numSubElements];
    281         mElementNames = new String[numSubElements];
    282         mArraySizes = new int[numSubElements];
    283         mOffsetInBytes = new int[numSubElements];
    284 
    285         int[] subElementIds = new int[numSubElements];
    286         mRS.nElementGetSubElements(getID(), subElementIds, mElementNames, mArraySizes);
    287         for(int i = 0; i < numSubElements; i ++) {
    288             mElements[i] = new Element(subElementIds[i], mRS);
    289             mElements[i].updateFromNative();
    290             mOffsetInBytes[i] = mSize;
    291             mSize += mElements[i].mSize * mArraySizes[i];
    292         }
    293     }
    294     */
    295     updateVisibleSubElements();
    296 }
    297 
    298 sp<const Element> Element::createUser(RenderScript *rs, RsDataType dt) {
    299     void * id = rsElementCreate(rs->mContext, dt, RS_KIND_USER, false, 1);
    300     return new Element(id, rs, dt, RS_KIND_USER, false, 1);
    301 }
    302 
    303 sp<const Element> Element::createVector(RenderScript *rs, RsDataType dt, uint32_t size) {
    304     if (size < 2 || size > 4) {
    305         rs->throwError("Vector size out of range 2-4.");
    306     }
    307     void *id = rsElementCreate(rs->mContext, dt, RS_KIND_USER, false, size);
    308     return new Element(id, rs, dt, RS_KIND_USER, false, size);
    309 }
    310 
    311 sp<const Element> Element::createPixel(RenderScript *rs, RsDataType dt, RsDataKind dk) {
    312     if (!(dk == RS_KIND_PIXEL_L ||
    313           dk == RS_KIND_PIXEL_A ||
    314           dk == RS_KIND_PIXEL_LA ||
    315           dk == RS_KIND_PIXEL_RGB ||
    316           dk == RS_KIND_PIXEL_RGBA ||
    317           dk == RS_KIND_PIXEL_DEPTH)) {
    318         rs->throwError("Unsupported DataKind");
    319     }
    320     if (!(dt == RS_TYPE_UNSIGNED_8 ||
    321           dt == RS_TYPE_UNSIGNED_16 ||
    322           dt == RS_TYPE_UNSIGNED_5_6_5 ||
    323           dt == RS_TYPE_UNSIGNED_4_4_4_4 ||
    324           dt == RS_TYPE_UNSIGNED_5_5_5_1)) {
    325         rs->throwError("Unsupported DataType");
    326     }
    327     if (dt == RS_TYPE_UNSIGNED_5_6_5 && dk != RS_KIND_PIXEL_RGB) {
    328         rs->throwError("Bad kind and type combo");
    329     }
    330     if (dt == RS_TYPE_UNSIGNED_5_5_5_1 && dk != RS_KIND_PIXEL_RGBA) {
    331         rs->throwError("Bad kind and type combo");
    332     }
    333     if (dt == RS_TYPE_UNSIGNED_4_4_4_4 && dk != RS_KIND_PIXEL_RGBA) {
    334         rs->throwError("Bad kind and type combo");
    335     }
    336     if (dt == RS_TYPE_UNSIGNED_16 && dk != RS_KIND_PIXEL_DEPTH) {
    337         rs->throwError("Bad kind and type combo");
    338     }
    339 
    340     int size = 1;
    341     switch (dk) {
    342     case RS_KIND_PIXEL_LA:
    343         size = 2;
    344         break;
    345     case RS_KIND_PIXEL_RGB:
    346         size = 3;
    347         break;
    348     case RS_KIND_PIXEL_RGBA:
    349         size = 4;
    350         break;
    351     case RS_KIND_PIXEL_DEPTH:
    352         size = 2;
    353         break;
    354     default:
    355         break;
    356     }
    357 
    358     void * id = rsElementCreate(rs->mContext, dt, dk, true, size);
    359     return new Element(id, rs, dt, dk, true, size);
    360 }
    361 
    362 bool Element::isCompatible(sp<const Element>e) {
    363     // Try strict BaseObj equality to start with.
    364     if (this == e.get()) {
    365         return true;
    366     }
    367 
    368     // Ignore mKind because it is allowed to be different (user vs. pixel).
    369     // We also ignore mNormalized because it can be different. The mType
    370     // field must be non-null since we require name equivalence for
    371     // user-created Elements.
    372     return ((mSizeBytes == e->mSizeBytes) &&
    373             (mType != RS_TYPE_NONE) &&
    374             (mType == e->mType) &&
    375             (mVectorSize == e->mVectorSize));
    376 }
    377 
    378 Element::Builder::Builder(RenderScript *rs) {
    379     mRS = rs;
    380     mSkipPadding = false;
    381 }
    382 
    383 void Element::Builder::add(sp</*const*/ Element>e, android::String8 &name, uint32_t arraySize) {
    384     // Skip padding fields after a vector 3 type.
    385     if (mSkipPadding) {
    386         const char *s1 = "#padding_";
    387         const char *s2 = name;
    388         size_t len = strlen(s1);
    389         if (strlen(s2) >= len) {
    390             if (!memcmp(s1, s2, len)) {
    391                 mSkipPadding = false;
    392                 return;
    393             }
    394         }
    395     }
    396 
    397     if (e->mVectorSize == 3) {
    398         mSkipPadding = true;
    399     } else {
    400         mSkipPadding = false;
    401     }
    402 
    403     mElements.add(e);
    404     mElementNames.add(name);
    405     mArraySizes.add(arraySize);
    406 }
    407 
    408 sp<const Element> Element::Builder::create() {
    409     size_t fieldCount = mElements.size();
    410     const char ** nameArray = (const char **)calloc(fieldCount, sizeof(char *));
    411     const Element ** elementArray = (const Element **)calloc(fieldCount, sizeof(Element *));
    412     size_t* sizeArray = (size_t*)calloc(fieldCount, sizeof(size_t));
    413 
    414     for (size_t ct = 0; ct < fieldCount; ct++) {
    415         nameArray[ct] = mElementNames[ct].string();
    416         elementArray[ct] = mElements[ct].get();
    417         sizeArray[ct] = mElementNames[ct].length();
    418     }
    419 
    420     void *id = rsElementCreate2(mRS->mContext,
    421                                 (RsElement *)elementArray, fieldCount,
    422                                 nameArray, fieldCount * sizeof(size_t),  sizeArray,
    423                                 (const uint32_t *)mArraySizes.array(), fieldCount);
    424 
    425 
    426     free(nameArray);
    427     free(sizeArray);
    428     free(elementArray);
    429     return new Element(id, mRS, mElements, mElementNames, mArraySizes);
    430 }
    431 
    432