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