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