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()) { |
Jason Sams | b2e3dc5 | 2012-02-23 17:14:39 -0800 | [diff] [blame] | 27 | mRS->throwError("Element contains no sub-elements"); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 28 | } |
| 29 | if (index >= mVisibleElementMap.size()) { |
Jason Sams | b2e3dc5 | 2012-02-23 17:14:39 -0800 | [diff] [blame] | 30 | mRS->throwError("Illegal sub-element index"); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 31 | } |
| 32 | return mElements[mVisibleElementMap[index]]; |
| 33 | } |
| 34 | |
| 35 | const char * Element::getSubElementName(uint32_t index) { |
| 36 | if (!mVisibleElementMap.size()) { |
Jason Sams | b2e3dc5 | 2012-02-23 17:14:39 -0800 | [diff] [blame] | 37 | mRS->throwError("Element contains no sub-elements"); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 38 | } |
| 39 | if (index >= mVisibleElementMap.size()) { |
Jason Sams | b2e3dc5 | 2012-02-23 17:14:39 -0800 | [diff] [blame] | 40 | mRS->throwError("Illegal sub-element index"); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 41 | } |
Tim Murray | ab71636 | 2013-08-12 12:37:18 -0700 | [diff] [blame^] | 42 | return mElementNames[mVisibleElementMap[index]].c_str(); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | size_t Element::getSubElementArraySize(uint32_t index) { |
| 46 | if (!mVisibleElementMap.size()) { |
Jason Sams | b2e3dc5 | 2012-02-23 17:14:39 -0800 | [diff] [blame] | 47 | mRS->throwError("Element contains no sub-elements"); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 48 | } |
| 49 | if (index >= mVisibleElementMap.size()) { |
Jason Sams | b2e3dc5 | 2012-02-23 17:14:39 -0800 | [diff] [blame] | 50 | mRS->throwError("Illegal sub-element index"); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 51 | } |
| 52 | return mArraySizes[mVisibleElementMap[index]]; |
| 53 | } |
| 54 | |
| 55 | uint32_t Element::getSubElementOffsetBytes(uint32_t index) { |
| 56 | if (mVisibleElementMap.size()) { |
Jason Sams | b2e3dc5 | 2012-02-23 17:14:39 -0800 | [diff] [blame] | 57 | mRS->throwError("Element contains no sub-elements"); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 58 | } |
| 59 | if (index >= mVisibleElementMap.size()) { |
Jason Sams | b2e3dc5 | 2012-02-23 17:14:39 -0800 | [diff] [blame] | 60 | mRS->throwError("Illegal sub-element index"); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 61 | } |
| 62 | return mOffsetInBytes[mVisibleElementMap[index]]; |
| 63 | } |
| 64 | |
| 65 | |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 66 | #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] | 67 | if (rs->mElements.N == NULL) { \ |
| 68 | rs->mElements.N = (createUser(rs, RS_TYPE_##T)).get(); \ |
| 69 | } \ |
| 70 | return rs->mElements.N; \ |
| 71 | } |
| 72 | |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 73 | CREATE_USER(BOOLEAN, BOOLEAN); |
| 74 | CREATE_USER(U8, UNSIGNED_8); |
| 75 | CREATE_USER(I8, SIGNED_8); |
| 76 | CREATE_USER(U16, UNSIGNED_16); |
| 77 | CREATE_USER(I16, SIGNED_16); |
| 78 | CREATE_USER(U32, UNSIGNED_32); |
| 79 | CREATE_USER(I32, SIGNED_32); |
| 80 | CREATE_USER(U64, UNSIGNED_64); |
| 81 | CREATE_USER(I64, SIGNED_64); |
| 82 | CREATE_USER(F32, FLOAT_32); |
| 83 | CREATE_USER(F64, FLOAT_64); |
| 84 | CREATE_USER(ELEMENT, ELEMENT); |
| 85 | CREATE_USER(TYPE, TYPE); |
| 86 | CREATE_USER(ALLOCATION, ALLOCATION); |
| 87 | CREATE_USER(SAMPLER, SAMPLER); |
| 88 | CREATE_USER(SCRIPT, SCRIPT); |
| 89 | CREATE_USER(MESH, MESH); |
| 90 | CREATE_USER(PROGRAM_FRAGMENT, PROGRAM_FRAGMENT); |
| 91 | CREATE_USER(PROGRAM_VERTEX, PROGRAM_VERTEX); |
| 92 | CREATE_USER(PROGRAM_RASTER, PROGRAM_RASTER); |
| 93 | CREATE_USER(PROGRAM_STORE, PROGRAM_STORE); |
| 94 | CREATE_USER(MATRIX_4X4, MATRIX_4X4); |
| 95 | CREATE_USER(MATRIX_3X3, MATRIX_3X3); |
| 96 | CREATE_USER(MATRIX_2X2, MATRIX_2X2); |
| 97 | |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 98 | #define CREATE_PIXEL(N, T, K) android::RSC::sp<const Element> Element::N(android::RSC::sp<RS> rs) { \ |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 99 | return createPixel(rs, RS_TYPE_##T, RS_KIND_##K); \ |
| 100 | } |
| 101 | CREATE_PIXEL(A_8, UNSIGNED_8, PIXEL_A); |
| 102 | CREATE_PIXEL(RGB_565, UNSIGNED_5_6_5, PIXEL_RGB); |
| 103 | CREATE_PIXEL(RGB_888, UNSIGNED_8, PIXEL_RGB); |
| 104 | CREATE_PIXEL(RGBA_4444, UNSIGNED_4_4_4_4, PIXEL_RGBA); |
| 105 | CREATE_PIXEL(RGBA_8888, UNSIGNED_8, PIXEL_RGBA); |
| 106 | |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 107 | #define CREATE_VECTOR(N, T) android::RSC::sp<const Element> Element::N##_2(android::RSC::sp<RS> rs) { \ |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 108 | return createVector(rs, RS_TYPE_##T, 2); \ |
| 109 | } \ |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 110 | android::RSC::sp<const Element> Element::N##_3(android::RSC::sp<RS> rs) { \ |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 111 | return createVector(rs, RS_TYPE_##T, 3); \ |
| 112 | } \ |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 113 | android::RSC::sp<const Element> Element::N##_4(android::RSC::sp<RS> rs) { \ |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 114 | return createVector(rs, RS_TYPE_##T, 4); \ |
| 115 | } |
| 116 | CREATE_VECTOR(U8, UNSIGNED_8); |
| 117 | CREATE_VECTOR(I8, SIGNED_8); |
| 118 | CREATE_VECTOR(U16, UNSIGNED_16); |
| 119 | CREATE_VECTOR(I16, SIGNED_16); |
| 120 | CREATE_VECTOR(U32, UNSIGNED_32); |
| 121 | CREATE_VECTOR(I32, SIGNED_32); |
| 122 | CREATE_VECTOR(U64, UNSIGNED_64); |
| 123 | CREATE_VECTOR(I64, SIGNED_64); |
| 124 | CREATE_VECTOR(F32, FLOAT_32); |
| 125 | CREATE_VECTOR(F64, FLOAT_64); |
| 126 | |
| 127 | |
| 128 | void 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 ++) { |
Tim Murray | ab71636 | 2013-08-12 12:37:18 -0700 | [diff] [blame^] | 138 | if (mElementNames[ct].c_str()[0] != '#') { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 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 ++) { |
Tim Murray | ab71636 | 2013-08-12 12:37:18 -0700 | [diff] [blame^] | 145 | if (mElementNames[ct].c_str()[0] != '#') { |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 146 | mVisibleElementMap.push_back((uint32_t)ct); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 151 | Element::Element(void *id, android::RSC::sp<RS> rs, |
| 152 | std::vector<android::RSC::sp<Element> > &elements, |
Tim Murray | ab71636 | 2013-08-12 12:37:18 -0700 | [diff] [blame^] | 153 | std::vector<std::string> &elementNames, |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 154 | std::vector<uint32_t> &arraySizes) : BaseObj(id, rs) { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 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++ ) { |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 165 | mOffsetInBytes.push_back(mSizeBytes); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 166 | mSizeBytes += mElements[ct]->mSizeBytes * mArraySizes[ct]; |
| 167 | } |
| 168 | updateVisibleSubElements(); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | static 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 | |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 225 | Element::Element(void *id, android::RSC::sp<RS> rs, |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 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 | |
| 247 | Element::~Element() { |
| 248 | } |
| 249 | |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 250 | void Element::updateFromNative() { |
| 251 | BaseObj::updateFromNative(); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 252 | updateVisibleSubElements(); |
| 253 | } |
| 254 | |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 255 | 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] | 256 | 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] | 257 | return new Element(id, rs, dt, RS_KIND_USER, false, 1); |
| 258 | } |
| 259 | |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 260 | 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] | 261 | if (size < 2 || size > 4) { |
Jason Sams | b2e3dc5 | 2012-02-23 17:14:39 -0800 | [diff] [blame] | 262 | rs->throwError("Vector size out of range 2-4."); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 263 | } |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 264 | 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] | 265 | return new Element(id, rs, dt, RS_KIND_USER, false, size); |
| 266 | } |
| 267 | |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 268 | 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] | 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)) { |
Jason Sams | b2e3dc5 | 2012-02-23 17:14:39 -0800 | [diff] [blame] | 275 | rs->throwError("Unsupported DataKind"); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 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)) { |
Jason Sams | b2e3dc5 | 2012-02-23 17:14:39 -0800 | [diff] [blame] | 282 | rs->throwError("Unsupported DataType"); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 283 | } |
| 284 | if (dt == RS_TYPE_UNSIGNED_5_6_5 && dk != RS_KIND_PIXEL_RGB) { |
Jason Sams | b2e3dc5 | 2012-02-23 17:14:39 -0800 | [diff] [blame] | 285 | rs->throwError("Bad kind and type combo"); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 286 | } |
| 287 | if (dt == RS_TYPE_UNSIGNED_5_5_5_1 && dk != RS_KIND_PIXEL_RGBA) { |
Jason Sams | b2e3dc5 | 2012-02-23 17:14:39 -0800 | [diff] [blame] | 288 | rs->throwError("Bad kind and type combo"); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 289 | } |
| 290 | if (dt == RS_TYPE_UNSIGNED_4_4_4_4 && dk != RS_KIND_PIXEL_RGBA) { |
Jason Sams | b2e3dc5 | 2012-02-23 17:14:39 -0800 | [diff] [blame] | 291 | rs->throwError("Bad kind and type combo"); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 292 | } |
| 293 | if (dt == RS_TYPE_UNSIGNED_16 && dk != RS_KIND_PIXEL_DEPTH) { |
Jason Sams | b2e3dc5 | 2012-02-23 17:14:39 -0800 | [diff] [blame] | 294 | rs->throwError("Bad kind and type combo"); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 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 | |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 315 | void * id = RS::dispatch->ElementCreate(rs->getContext(), dt, dk, true, size); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 316 | return new Element(id, rs, dt, dk, true, size); |
| 317 | } |
| 318 | |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 319 | bool Element::isCompatible(android::RSC::sp<const Element>e) { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 320 | // Try strict BaseObj equality to start with. |
Jason Sams | 69cccdf | 2012-04-02 19:11:49 -0700 | [diff] [blame] | 321 | if (this == e.get()) { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 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) && |
Jason Sams | 69cccdf | 2012-04-02 19:11:49 -0700 | [diff] [blame] | 330 | (mType != RS_TYPE_NONE) && |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 331 | (mType == e->mType) && |
| 332 | (mVectorSize == e->mVectorSize)); |
| 333 | } |
| 334 | |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 335 | Element::Builder::Builder(android::RSC::sp<RS> rs) { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 336 | mRS = rs; |
| 337 | mSkipPadding = false; |
| 338 | } |
| 339 | |
Tim Murray | ab71636 | 2013-08-12 12:37:18 -0700 | [diff] [blame^] | 340 | 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] | 341 | // Skip padding fields after a vector 3 type. |
| 342 | if (mSkipPadding) { |
| 343 | const char *s1 = "#padding_"; |
Tim Murray | ab71636 | 2013-08-12 12:37:18 -0700 | [diff] [blame^] | 344 | const char *s2 = name.c_str(); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 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 | |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 360 | mElements.push_back(e); |
| 361 | mElementNames.push_back(name); |
| 362 | mArraySizes.push_back(arraySize); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 363 | } |
| 364 | |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 365 | android::RSC::sp<const Element> Element::Builder::create() { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 366 | size_t fieldCount = mElements.size(); |
| 367 | const char ** nameArray = (const char **)calloc(fieldCount, sizeof(char *)); |
Jason Sams | 69cccdf | 2012-04-02 19:11:49 -0700 | [diff] [blame] | 368 | const Element ** elementArray = (const Element **)calloc(fieldCount, sizeof(Element *)); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 369 | size_t* sizeArray = (size_t*)calloc(fieldCount, sizeof(size_t)); |
| 370 | |
| 371 | for (size_t ct = 0; ct < fieldCount; ct++) { |
Tim Murray | ab71636 | 2013-08-12 12:37:18 -0700 | [diff] [blame^] | 372 | nameArray[ct] = mElementNames[ct].c_str(); |
Jason Sams | 69cccdf | 2012-04-02 19:11:49 -0700 | [diff] [blame] | 373 | elementArray[ct] = mElements[ct].get(); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 374 | sizeArray[ct] = mElementNames[ct].length(); |
| 375 | } |
| 376 | |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 377 | void *id = RS::dispatch->ElementCreate2(mRS->getContext(), |
Jason Sams | 69cccdf | 2012-04-02 19:11:49 -0700 | [diff] [blame] | 378 | (RsElement *)elementArray, fieldCount, |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 379 | nameArray, fieldCount * sizeof(size_t), sizeArray, |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 380 | (const uint32_t *)&mArraySizes[0], fieldCount); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 381 | |
| 382 | |
| 383 | free(nameArray); |
| 384 | free(sizeArray); |
Jason Sams | 69cccdf | 2012-04-02 19:11:49 -0700 | [diff] [blame] | 385 | free(elementArray); |
| 386 | return new Element(id, mRS, mElements, mElementNames, mArraySizes); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 387 | } |
| 388 | |