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