Element.cpp revision 89daad6bae798779e57f252e9da4fe4e62337124
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#include <malloc.h> 18#include <string.h> 19 20#include "RenderScript.h" 21 22using namespace android; 23using namespace RSC; 24 25android::RSC::sp<const Element> Element::getSubElement(uint32_t index) { 26 if (!mVisibleElementMap.size()) { 27 mRS->throwError("Element contains no sub-elements"); 28 } 29 if (index >= mVisibleElementMap.size()) { 30 mRS->throwError("Illegal sub-element index"); 31 } 32 return mElements[mVisibleElementMap[index]]; 33} 34 35const char * Element::getSubElementName(uint32_t index) { 36 if (!mVisibleElementMap.size()) { 37 mRS->throwError("Element contains no sub-elements"); 38 } 39 if (index >= mVisibleElementMap.size()) { 40 mRS->throwError("Illegal sub-element index"); 41 } 42 return mElementNames[mVisibleElementMap[index]].string(); 43} 44 45size_t Element::getSubElementArraySize(uint32_t index) { 46 if (!mVisibleElementMap.size()) { 47 mRS->throwError("Element contains no sub-elements"); 48 } 49 if (index >= mVisibleElementMap.size()) { 50 mRS->throwError("Illegal sub-element index"); 51 } 52 return mArraySizes[mVisibleElementMap[index]]; 53} 54 55uint32_t Element::getSubElementOffsetBytes(uint32_t index) { 56 if (mVisibleElementMap.size()) { 57 mRS->throwError("Element contains no sub-elements"); 58 } 59 if (index >= mVisibleElementMap.size()) { 60 mRS->throwError("Illegal sub-element index"); 61 } 62 return mOffsetInBytes[mVisibleElementMap[index]]; 63} 64 65 66#define CREATE_USER(N, T) android::RSC::sp<const Element> Element::N(android::RSC::sp<RS> rs) { \ 67 if (rs->mElements.N == NULL) { \ 68 rs->mElements.N = (createUser(rs, RS_TYPE_##T)).get(); \ 69 } \ 70 return rs->mElements.N; \ 71 } 72 73CREATE_USER(BOOLEAN, BOOLEAN); 74CREATE_USER(U8, UNSIGNED_8); 75CREATE_USER(I8, SIGNED_8); 76CREATE_USER(U16, UNSIGNED_16); 77CREATE_USER(I16, SIGNED_16); 78CREATE_USER(U32, UNSIGNED_32); 79CREATE_USER(I32, SIGNED_32); 80CREATE_USER(U64, UNSIGNED_64); 81CREATE_USER(I64, SIGNED_64); 82CREATE_USER(F32, FLOAT_32); 83CREATE_USER(F64, FLOAT_64); 84CREATE_USER(ELEMENT, ELEMENT); 85CREATE_USER(TYPE, TYPE); 86CREATE_USER(ALLOCATION, ALLOCATION); 87CREATE_USER(SAMPLER, SAMPLER); 88CREATE_USER(SCRIPT, SCRIPT); 89CREATE_USER(MESH, MESH); 90CREATE_USER(PROGRAM_FRAGMENT, PROGRAM_FRAGMENT); 91CREATE_USER(PROGRAM_VERTEX, PROGRAM_VERTEX); 92CREATE_USER(PROGRAM_RASTER, PROGRAM_RASTER); 93CREATE_USER(PROGRAM_STORE, PROGRAM_STORE); 94CREATE_USER(MATRIX_4X4, MATRIX_4X4); 95CREATE_USER(MATRIX_3X3, MATRIX_3X3); 96CREATE_USER(MATRIX_2X2, MATRIX_2X2); 97 98#define CREATE_PIXEL(N, T, K) android::RSC::sp<const Element> Element::N(android::RSC::sp<RS> rs) { \ 99 return createPixel(rs, RS_TYPE_##T, RS_KIND_##K); \ 100} 101CREATE_PIXEL(A_8, UNSIGNED_8, PIXEL_A); 102CREATE_PIXEL(RGB_565, UNSIGNED_5_6_5, PIXEL_RGB); 103CREATE_PIXEL(RGB_888, UNSIGNED_8, PIXEL_RGB); 104CREATE_PIXEL(RGBA_4444, UNSIGNED_4_4_4_4, PIXEL_RGBA); 105CREATE_PIXEL(RGBA_8888, UNSIGNED_8, PIXEL_RGBA); 106 107#define CREATE_VECTOR(N, T) android::RSC::sp<const Element> Element::N##_2(android::RSC::sp<RS> rs) { \ 108 return createVector(rs, RS_TYPE_##T, 2); \ 109} \ 110android::RSC::sp<const Element> Element::N##_3(android::RSC::sp<RS> rs) { \ 111 return createVector(rs, RS_TYPE_##T, 3); \ 112} \ 113android::RSC::sp<const Element> Element::N##_4(android::RSC::sp<RS> rs) { \ 114 return createVector(rs, RS_TYPE_##T, 4); \ 115} 116CREATE_VECTOR(U8, UNSIGNED_8); 117CREATE_VECTOR(I8, SIGNED_8); 118CREATE_VECTOR(U16, UNSIGNED_16); 119CREATE_VECTOR(I16, SIGNED_16); 120CREATE_VECTOR(U32, UNSIGNED_32); 121CREATE_VECTOR(I32, SIGNED_32); 122CREATE_VECTOR(U64, UNSIGNED_64); 123CREATE_VECTOR(I64, SIGNED_64); 124CREATE_VECTOR(F32, FLOAT_32); 125CREATE_VECTOR(F64, FLOAT_64); 126 127 128void 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_back((uint32_t)ct); 147 } 148 } 149} 150 151Element::Element(void *id, android::RSC::sp<RS> rs, 152 std::vector<android::RSC::sp<Element> > &elements, 153 std::vector<android::String8> &elementNames, 154 std::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_back(mSizeBytes); 166 mSizeBytes += mElements[ct]->mSizeBytes * mArraySizes[ct]; 167 } 168 updateVisibleSubElements(); 169} 170 171 172static 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 225Element::Element(void *id, android::RSC::sp<RS> 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 247Element::~Element() { 248} 249 250void Element::updateFromNative() { 251 BaseObj::updateFromNative(); 252 updateVisibleSubElements(); 253} 254 255android::RSC::sp<const Element> Element::createUser(android::RSC::sp<RS> rs, RsDataType dt) { 256 void * id = RS::dispatch->ElementCreate(rs->getContext(), dt, RS_KIND_USER, false, 1); 257 return new Element(id, rs, dt, RS_KIND_USER, false, 1); 258} 259 260android::RSC::sp<const Element> Element::createVector(android::RSC::sp<RS> rs, RsDataType dt, uint32_t size) { 261 if (size < 2 || size > 4) { 262 rs->throwError("Vector size out of range 2-4."); 263 } 264 void *id = RS::dispatch->ElementCreate(rs->getContext(), dt, RS_KIND_USER, false, size); 265 return new Element(id, rs, dt, RS_KIND_USER, false, size); 266} 267 268android::RSC::sp<const Element> Element::createPixel(android::RSC::sp<RS> rs, RsDataType dt, RsDataKind dk) { 269 if (!(dk == RS_KIND_PIXEL_L || 270 dk == RS_KIND_PIXEL_A || 271 dk == RS_KIND_PIXEL_LA || 272 dk == RS_KIND_PIXEL_RGB || 273 dk == RS_KIND_PIXEL_RGBA || 274 dk == RS_KIND_PIXEL_DEPTH)) { 275 rs->throwError("Unsupported DataKind"); 276 } 277 if (!(dt == RS_TYPE_UNSIGNED_8 || 278 dt == RS_TYPE_UNSIGNED_16 || 279 dt == RS_TYPE_UNSIGNED_5_6_5 || 280 dt == RS_TYPE_UNSIGNED_4_4_4_4 || 281 dt == RS_TYPE_UNSIGNED_5_5_5_1)) { 282 rs->throwError("Unsupported DataType"); 283 } 284 if (dt == RS_TYPE_UNSIGNED_5_6_5 && dk != RS_KIND_PIXEL_RGB) { 285 rs->throwError("Bad kind and type combo"); 286 } 287 if (dt == RS_TYPE_UNSIGNED_5_5_5_1 && dk != RS_KIND_PIXEL_RGBA) { 288 rs->throwError("Bad kind and type combo"); 289 } 290 if (dt == RS_TYPE_UNSIGNED_4_4_4_4 && dk != RS_KIND_PIXEL_RGBA) { 291 rs->throwError("Bad kind and type combo"); 292 } 293 if (dt == RS_TYPE_UNSIGNED_16 && dk != RS_KIND_PIXEL_DEPTH) { 294 rs->throwError("Bad kind and type combo"); 295 } 296 297 int size = 1; 298 switch (dk) { 299 case RS_KIND_PIXEL_LA: 300 size = 2; 301 break; 302 case RS_KIND_PIXEL_RGB: 303 size = 3; 304 break; 305 case RS_KIND_PIXEL_RGBA: 306 size = 4; 307 break; 308 case RS_KIND_PIXEL_DEPTH: 309 size = 2; 310 break; 311 default: 312 break; 313 } 314 315 void * id = RS::dispatch->ElementCreate(rs->getContext(), dt, dk, true, size); 316 return new Element(id, rs, dt, dk, true, size); 317} 318 319bool Element::isCompatible(android::RSC::sp<const Element>e) { 320 // Try strict BaseObj equality to start with. 321 if (this == e.get()) { 322 return true; 323 } 324 325 // Ignore mKind because it is allowed to be different (user vs. pixel). 326 // We also ignore mNormalized because it can be different. The mType 327 // field must be non-null since we require name equivalence for 328 // user-created Elements. 329 return ((mSizeBytes == e->mSizeBytes) && 330 (mType != RS_TYPE_NONE) && 331 (mType == e->mType) && 332 (mVectorSize == e->mVectorSize)); 333} 334 335Element::Builder::Builder(android::RSC::sp<RS> rs) { 336 mRS = rs; 337 mSkipPadding = false; 338} 339 340void Element::Builder::add(android::RSC::sp</*const*/ Element>e, android::String8 &name, uint32_t arraySize) { 341 // Skip padding fields after a vector 3 type. 342 if (mSkipPadding) { 343 const char *s1 = "#padding_"; 344 const char *s2 = name.string(); 345 size_t len = strlen(s1); 346 if (strlen(s2) >= len) { 347 if (!memcmp(s1, s2, len)) { 348 mSkipPadding = false; 349 return; 350 } 351 } 352 } 353 354 if (e->mVectorSize == 3) { 355 mSkipPadding = true; 356 } else { 357 mSkipPadding = false; 358 } 359 360 mElements.push_back(e); 361 mElementNames.push_back(name); 362 mArraySizes.push_back(arraySize); 363} 364 365android::RSC::sp<const Element> Element::Builder::create() { 366 size_t fieldCount = mElements.size(); 367 const char ** nameArray = (const char **)calloc(fieldCount, sizeof(char *)); 368 const Element ** elementArray = (const Element **)calloc(fieldCount, sizeof(Element *)); 369 size_t* sizeArray = (size_t*)calloc(fieldCount, sizeof(size_t)); 370 371 for (size_t ct = 0; ct < fieldCount; ct++) { 372 nameArray[ct] = mElementNames[ct].string(); 373 elementArray[ct] = mElements[ct].get(); 374 sizeArray[ct] = mElementNames[ct].length(); 375 } 376 377 void *id = RS::dispatch->ElementCreate2(mRS->getContext(), 378 (RsElement *)elementArray, fieldCount, 379 nameArray, fieldCount * sizeof(size_t), sizeArray, 380 (const uint32_t *)&mArraySizes[0], fieldCount); 381 382 383 free(nameArray); 384 free(sizeArray); 385 free(elementArray); 386 return new Element(id, mRS, mElements, mElementNames, mArraySizes); 387} 388 389