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