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