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 | |
Chih-Hung Hsieh | dfcfabf | 2016-11-04 15:55:18 -0700 | [diff] [blame] | 23 | using android::RSC::Element; |
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) { |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 26 | if (!mVisibleElementMapSize) { |
Tim Murray | 21fa7a0 | 2013-08-15 16:25:03 -0700 | [diff] [blame] | 27 | mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Element contains no sub-elements"); |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 28 | return nullptr; |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 29 | } |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 30 | if (index >= mVisibleElementMapSize) { |
Tim Murray | 21fa7a0 | 2013-08-15 16:25:03 -0700 | [diff] [blame] | 31 | mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Illegal sub-element index"); |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 32 | return nullptr; |
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) { |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 38 | if (!mVisibleElementMapSize) { |
Tim Murray | 21fa7a0 | 2013-08-15 16:25:03 -0700 | [diff] [blame] | 39 | mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Element contains no sub-elements"); |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 40 | return nullptr; |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 41 | } |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 42 | if (index >= mVisibleElementMapSize) { |
Tim Murray | 21fa7a0 | 2013-08-15 16:25:03 -0700 | [diff] [blame] | 43 | mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Illegal sub-element index"); |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 44 | return nullptr; |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 45 | } |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 46 | return mElementNames[mVisibleElementMap[index]]; |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | size_t Element::getSubElementArraySize(uint32_t index) { |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 50 | if (!mVisibleElementMapSize) { |
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 | } |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 54 | if (index >= mVisibleElementMapSize) { |
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) { |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 62 | if (!mVisibleElementMapSize) { |
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 | } |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 66 | if (index >= mVisibleElementMapSize) { |
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 | |
Chih-Hung Hsieh | 45768e1 | 2016-08-10 12:32:11 -0700 | [diff] [blame] | 74 | #define CREATE_USER(N, T) android::RSC::sp<const Element> Element::N(const android::RSC::sp<RS>& rs) { \ |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 75 | if (rs->mElements.N == nullptr) { \ |
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); |
Pirama Arumuga Nainar | 5661684 | 2015-12-02 11:40:54 -0800 | [diff] [blame] | 90 | CREATE_USER(F16, FLOAT_16); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 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 | |
Chih-Hung Hsieh | 45768e1 | 2016-08-10 12:32:11 -0700 | [diff] [blame] | 102 | #define CREATE_PIXEL(N, T, K) android::RSC::sp<const Element> Element::N(const android::RSC::sp<RS> &rs) { \ |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 103 | if (rs->mElements.N == nullptr) { \ |
| 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 | |
Chih-Hung Hsieh | 45768e1 | 2016-08-10 12:32:11 -0700 | [diff] [blame] | 117 | #define CREATE_VECTOR(N, T) android::RSC::sp<const Element> Element::N##_2(const android::RSC::sp<RS> &rs) { \ |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 118 | if (rs->mElements.N##_2 == nullptr) { \ |
| 119 | rs->mElements.N##_2 = createVector(rs, RS_TYPE_##T, 2); \ |
| 120 | } \ |
| 121 | return rs->mElements.N##_2; \ |
| 122 | } \ |
Chih-Hung Hsieh | 45768e1 | 2016-08-10 12:32:11 -0700 | [diff] [blame] | 123 | android::RSC::sp<const Element> Element::N##_3(const android::RSC::sp<RS> &rs) { \ |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 124 | if (rs->mElements.N##_3 == nullptr) { \ |
| 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 | } \ |
Chih-Hung Hsieh | 45768e1 | 2016-08-10 12:32:11 -0700 | [diff] [blame] | 129 | android::RSC::sp<const Element> Element::N##_4(const android::RSC::sp<RS> &rs) { \ |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 130 | if (rs->mElements.N##_4 == nullptr) { \ |
| 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); |
Pirama Arumuga Nainar | 5661684 | 2015-12-02 11:40:54 -0800 | [diff] [blame] | 143 | CREATE_VECTOR(F16, FLOAT_16); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 144 | CREATE_VECTOR(F32, FLOAT_32); |
| 145 | CREATE_VECTOR(F64, FLOAT_64); |
| 146 | |
| 147 | |
| 148 | void Element::updateVisibleSubElements() { |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 149 | if (!mElementsCount) { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 150 | return; |
| 151 | } |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 152 | if (mVisibleElementMapSize) { |
| 153 | free(mVisibleElementMap); |
| 154 | mVisibleElementMapSize = 0; |
| 155 | } |
| 156 | mVisibleElementMap = (uint32_t*)calloc(mElementsCount, sizeof(uint32_t)); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 157 | |
| 158 | int noPaddingFieldCount = 0; |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 159 | size_t fieldCount = mElementsCount; |
Miao Wang | cf067b8 | 2015-09-14 18:27:17 -0700 | [diff] [blame] | 160 | // Find out how many elements are not padding. |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 161 | for (size_t ct = 0; ct < fieldCount; ct ++) { |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 162 | if (mElementNames[ct][0] != '#') { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 163 | noPaddingFieldCount ++; |
| 164 | } |
| 165 | } |
| 166 | |
Miao Wang | cf067b8 | 2015-09-14 18:27:17 -0700 | [diff] [blame] | 167 | // Make a map that points us at non-padding elements. |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 168 | size_t i = 0; |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 169 | for (size_t ct = 0; ct < fieldCount; ct ++) { |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 170 | if (mElementNames[ct][0] != '#') { |
| 171 | mVisibleElementMap[i++] = (uint32_t)ct; |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 174 | mVisibleElementMapSize = i; |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 177 | Element::Element(void *id, android::RSC::sp<RS> rs, |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 178 | android::RSC::sp<const Element> * elements, |
| 179 | size_t elementCount, |
| 180 | const char ** elementNames, |
| 181 | size_t * elementNameLengths, |
| 182 | uint32_t * arraySizes) : BaseObj(id, rs) { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 183 | mSizeBytes = 0; |
| 184 | mVectorSize = 1; |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 185 | mElementsCount = elementCount; |
| 186 | mVisibleElementMap = nullptr; |
| 187 | mVisibleElementMapSize = 0; |
| 188 | |
Rahul Chaudhry | 9ab1199 | 2017-02-07 16:33:46 -0800 | [diff] [blame] | 189 | mElements = (android::RSC::sp<const Element> *)calloc(mElementsCount, sizeof(android::RSC::sp<const Element>)); |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 190 | mElementNames = (char **)calloc(mElementsCount, sizeof(char *)); |
| 191 | mElementNameLengths = (size_t*)calloc(mElementsCount, sizeof(size_t)); |
| 192 | mArraySizes = (uint32_t*)calloc(mElementsCount, sizeof(uint32_t)); |
| 193 | mOffsetInBytes = (uint32_t*)calloc(mElementsCount, sizeof(uint32_t)); |
| 194 | |
| 195 | memcpy(mElements, elements, mElementsCount * sizeof(android::RSC::sp<Element>)); |
| 196 | memcpy(mArraySizes, arraySizes, mElementsCount * sizeof(uint32_t)); |
| 197 | |
Miao Wang | cf067b8 | 2015-09-14 18:27:17 -0700 | [diff] [blame] | 198 | // Copy strings (char array). |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 199 | memcpy(mElementNameLengths, elementNameLengths, mElementsCount * sizeof(size_t)); |
| 200 | for (size_t ct = 0; ct < mElementsCount; ct++ ) { |
| 201 | size_t elemNameLen = mElementNameLengths[ct]; |
| 202 | mElementNames[ct] = (char *)calloc(elemNameLen, sizeof(char)); |
| 203 | memcpy(mElementNames[ct], elementNames[ct], elemNameLen); |
| 204 | } |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 205 | |
| 206 | mType = RS_TYPE_NONE; |
| 207 | mKind = RS_KIND_USER; |
| 208 | |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 209 | for (size_t ct = 0; ct < mElementsCount; ct++ ) { |
| 210 | mOffsetInBytes[ct] = mSizeBytes; |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 211 | mSizeBytes += mElements[ct]->mSizeBytes * mArraySizes[ct]; |
| 212 | } |
| 213 | updateVisibleSubElements(); |
| 214 | } |
| 215 | |
Miao Wang | 70d8995 | 2015-09-14 15:05:41 -0700 | [diff] [blame] | 216 | Element::Element(void *id, android::RSC::sp<RS> rs) : |
| 217 | BaseObj(id, rs) { |
| 218 | } |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 219 | |
| 220 | static uint32_t GetSizeInBytesForType(RsDataType dt) { |
| 221 | switch(dt) { |
| 222 | case RS_TYPE_NONE: |
| 223 | return 0; |
| 224 | case RS_TYPE_SIGNED_8: |
| 225 | case RS_TYPE_UNSIGNED_8: |
| 226 | case RS_TYPE_BOOLEAN: |
| 227 | return 1; |
| 228 | |
| 229 | case RS_TYPE_FLOAT_16: |
| 230 | case RS_TYPE_SIGNED_16: |
| 231 | case RS_TYPE_UNSIGNED_16: |
| 232 | case RS_TYPE_UNSIGNED_5_6_5: |
| 233 | case RS_TYPE_UNSIGNED_5_5_5_1: |
| 234 | case RS_TYPE_UNSIGNED_4_4_4_4: |
| 235 | return 2; |
| 236 | |
| 237 | case RS_TYPE_FLOAT_32: |
| 238 | case RS_TYPE_SIGNED_32: |
| 239 | case RS_TYPE_UNSIGNED_32: |
| 240 | return 4; |
| 241 | |
| 242 | case RS_TYPE_FLOAT_64: |
| 243 | case RS_TYPE_SIGNED_64: |
| 244 | case RS_TYPE_UNSIGNED_64: |
| 245 | return 8; |
| 246 | |
| 247 | case RS_TYPE_MATRIX_4X4: |
| 248 | return 16 * 4; |
| 249 | case RS_TYPE_MATRIX_3X3: |
| 250 | return 9 * 4; |
| 251 | case RS_TYPE_MATRIX_2X2: |
| 252 | return 4 * 4; |
| 253 | |
| 254 | case RS_TYPE_TYPE: |
| 255 | case RS_TYPE_ALLOCATION: |
| 256 | case RS_TYPE_SAMPLER: |
| 257 | case RS_TYPE_SCRIPT: |
| 258 | case RS_TYPE_MESH: |
| 259 | case RS_TYPE_PROGRAM_FRAGMENT: |
| 260 | case RS_TYPE_PROGRAM_VERTEX: |
| 261 | case RS_TYPE_PROGRAM_RASTER: |
| 262 | case RS_TYPE_PROGRAM_STORE: |
| 263 | return 4; |
| 264 | |
| 265 | default: |
| 266 | break; |
| 267 | } |
| 268 | |
| 269 | ALOGE("Missing type %i", dt); |
| 270 | return 0; |
| 271 | } |
| 272 | |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 273 | Element::Element(void *id, android::RSC::sp<RS> rs, |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 274 | RsDataType dt, RsDataKind dk, bool norm, uint32_t size) : |
| 275 | BaseObj(id, rs) |
| 276 | { |
| 277 | uint32_t tsize = GetSizeInBytesForType(dt); |
| 278 | if ((dt != RS_TYPE_UNSIGNED_5_6_5) && |
| 279 | (dt != RS_TYPE_UNSIGNED_4_4_4_4) && |
| 280 | (dt != RS_TYPE_UNSIGNED_5_5_5_1)) { |
| 281 | if (size == 3) { |
| 282 | mSizeBytes = tsize * 4; |
| 283 | } else { |
| 284 | mSizeBytes = tsize * size; |
| 285 | } |
| 286 | } else { |
| 287 | mSizeBytes = tsize; |
| 288 | } |
| 289 | mType = dt; |
| 290 | mKind = dk; |
| 291 | mNormalized = norm; |
| 292 | mVectorSize = size; |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 293 | mElementsCount = 0; |
| 294 | mVisibleElementMap = 0; |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | Element::~Element() { |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 298 | if (mElementsCount) { |
| 299 | free(mElements); |
| 300 | for (size_t ct = 0; ct < mElementsCount; ct++ ) { |
| 301 | free(mElementNames[ct]); |
| 302 | } |
| 303 | free(mElementNames); |
| 304 | free(mElementNameLengths); |
| 305 | free(mArraySizes); |
| 306 | free(mOffsetInBytes); |
| 307 | } |
| 308 | if (mVisibleElementMapSize) { |
| 309 | free(mVisibleElementMap); |
| 310 | } |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 313 | void Element::updateFromNative() { |
| 314 | BaseObj::updateFromNative(); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 315 | updateVisibleSubElements(); |
| 316 | } |
| 317 | |
Chih-Hung Hsieh | 45768e1 | 2016-08-10 12:32:11 -0700 | [diff] [blame] | 318 | android::RSC::sp<const Element> Element::createUser(const android::RSC::sp<RS>& rs, RsDataType dt) { |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 319 | 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] | 320 | return new Element(id, rs, dt, RS_KIND_USER, false, 1); |
| 321 | } |
| 322 | |
Chih-Hung Hsieh | 45768e1 | 2016-08-10 12:32:11 -0700 | [diff] [blame] | 323 | android::RSC::sp<const Element> Element::createVector(const android::RSC::sp<RS>& rs, RsDataType dt, uint32_t size) { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 324 | if (size < 2 || size > 4) { |
Tim Murray | 21fa7a0 | 2013-08-15 16:25:03 -0700 | [diff] [blame] | 325 | rs->throwError(RS_ERROR_INVALID_PARAMETER, "Vector size out of range 2-4."); |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 326 | return nullptr; |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 327 | } |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 328 | 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] | 329 | return new Element(id, rs, dt, RS_KIND_USER, false, size); |
| 330 | } |
| 331 | |
Chih-Hung Hsieh | 45768e1 | 2016-08-10 12:32:11 -0700 | [diff] [blame] | 332 | android::RSC::sp<const Element> Element::createPixel(const android::RSC::sp<RS>& rs, RsDataType dt, RsDataKind dk) { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 333 | if (!(dk == RS_KIND_PIXEL_L || |
| 334 | dk == RS_KIND_PIXEL_A || |
| 335 | dk == RS_KIND_PIXEL_LA || |
| 336 | dk == RS_KIND_PIXEL_RGB || |
| 337 | dk == RS_KIND_PIXEL_RGBA || |
Miao Wang | 43a6cba | 2015-04-12 13:39:56 -0700 | [diff] [blame] | 338 | dk == RS_KIND_PIXEL_DEPTH || |
| 339 | dk == RS_KIND_PIXEL_YUV)) { |
Tim Murray | 21fa7a0 | 2013-08-15 16:25:03 -0700 | [diff] [blame] | 340 | rs->throwError(RS_ERROR_INVALID_PARAMETER, "Unsupported DataKind"); |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 341 | return nullptr; |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 342 | } |
| 343 | if (!(dt == RS_TYPE_UNSIGNED_8 || |
| 344 | dt == RS_TYPE_UNSIGNED_16 || |
| 345 | dt == RS_TYPE_UNSIGNED_5_6_5 || |
| 346 | dt == RS_TYPE_UNSIGNED_4_4_4_4 || |
| 347 | dt == RS_TYPE_UNSIGNED_5_5_5_1)) { |
Tim Murray | 21fa7a0 | 2013-08-15 16:25:03 -0700 | [diff] [blame] | 348 | rs->throwError(RS_ERROR_INVALID_PARAMETER, "Unsupported DataType"); |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 349 | return nullptr; |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 350 | } |
| 351 | 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] | 352 | rs->throwError(RS_ERROR_INVALID_PARAMETER, "Bad kind and type combo"); |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 353 | return nullptr; |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 354 | } |
| 355 | 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] | 356 | rs->throwError(RS_ERROR_INVALID_PARAMETER, "Bad kind and type combo"); |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 357 | return nullptr; |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 358 | } |
| 359 | 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] | 360 | rs->throwError(RS_ERROR_INVALID_PARAMETER, "Bad kind and type combo"); |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 361 | return nullptr; |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 362 | } |
| 363 | if (dt == RS_TYPE_UNSIGNED_16 && dk != RS_KIND_PIXEL_DEPTH) { |
Tim Murray | 21fa7a0 | 2013-08-15 16:25:03 -0700 | [diff] [blame] | 364 | rs->throwError(RS_ERROR_INVALID_PARAMETER, "Bad kind and type combo"); |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 365 | return nullptr; |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | int size = 1; |
| 369 | switch (dk) { |
| 370 | case RS_KIND_PIXEL_LA: |
| 371 | size = 2; |
| 372 | break; |
| 373 | case RS_KIND_PIXEL_RGB: |
| 374 | size = 3; |
| 375 | break; |
| 376 | case RS_KIND_PIXEL_RGBA: |
| 377 | size = 4; |
| 378 | break; |
| 379 | case RS_KIND_PIXEL_DEPTH: |
| 380 | size = 2; |
| 381 | break; |
| 382 | default: |
| 383 | break; |
| 384 | } |
| 385 | |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 386 | void * id = RS::dispatch->ElementCreate(rs->getContext(), dt, dk, true, size); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 387 | return new Element(id, rs, dt, dk, true, size); |
| 388 | } |
| 389 | |
Chih-Hung Hsieh | 45768e1 | 2016-08-10 12:32:11 -0700 | [diff] [blame] | 390 | bool Element::isCompatible(const android::RSC::sp<const Element>&e) const { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 391 | // Try strict BaseObj equality to start with. |
Jason Sams | 69cccdf | 2012-04-02 19:11:49 -0700 | [diff] [blame] | 392 | if (this == e.get()) { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 393 | return true; |
| 394 | } |
| 395 | |
Miao Wang | cf067b8 | 2015-09-14 18:27:17 -0700 | [diff] [blame] | 396 | /* |
| 397 | * Ignore mKind because it is allowed to be different (user vs. pixel). |
| 398 | * We also ignore mNormalized because it can be different. The mType |
| 399 | * field must be non-null since we require name equivalence for |
| 400 | * user-created Elements. |
| 401 | */ |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 402 | return ((mSizeBytes == e->mSizeBytes) && |
Jason Sams | 69cccdf | 2012-04-02 19:11:49 -0700 | [diff] [blame] | 403 | (mType != RS_TYPE_NONE) && |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 404 | (mType == e->mType) && |
| 405 | (mVectorSize == e->mVectorSize)); |
| 406 | } |
| 407 | |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 408 | Element::Builder::Builder(android::RSC::sp<RS> rs) { |
Tim Murray | 3560907 | 2013-12-03 11:36:03 -0800 | [diff] [blame] | 409 | mRS = rs.get(); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 410 | mSkipPadding = false; |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 411 | mElementsVecSize = 8; |
| 412 | mElementsCount = 0; |
Miao Wang | cf067b8 | 2015-09-14 18:27:17 -0700 | [diff] [blame] | 413 | // Initialize space. |
Rahul Chaudhry | 9ab1199 | 2017-02-07 16:33:46 -0800 | [diff] [blame] | 414 | mElements = (android::RSC::sp<const Element> *)calloc(mElementsVecSize, sizeof(android::RSC::sp<const Element>)); |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 415 | mElementNames = (char **)calloc(mElementsVecSize, sizeof(char *)); |
| 416 | mElementNameLengths = (size_t*)calloc(mElementsVecSize, sizeof(size_t)); |
| 417 | mArraySizes = (uint32_t*)calloc(mElementsVecSize, sizeof(uint32_t)); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 418 | } |
| 419 | |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 420 | Element::Builder::~Builder() { |
Miao Wang | cf067b8 | 2015-09-14 18:27:17 -0700 | [diff] [blame] | 421 | // Free allocated space. |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 422 | free(mElements); |
| 423 | for (size_t ct = 0; ct < mElementsCount; ct++ ) { |
| 424 | free(mElementNames[ct]); |
| 425 | } |
| 426 | free(mElementNameLengths); |
| 427 | free(mElementNames); |
| 428 | free(mArraySizes); |
| 429 | } |
| 430 | |
Chih-Hung Hsieh | 45768e1 | 2016-08-10 12:32:11 -0700 | [diff] [blame] | 431 | void Element::Builder::add(const android::RSC::sp<const Element>&e, const char * name, uint32_t arraySize) { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 432 | // Skip padding fields after a vector 3 type. |
| 433 | if (mSkipPadding) { |
| 434 | const char *s1 = "#padding_"; |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 435 | const char *s2 = name; |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 436 | size_t len = strlen(s1); |
| 437 | if (strlen(s2) >= len) { |
| 438 | if (!memcmp(s1, s2, len)) { |
| 439 | mSkipPadding = false; |
| 440 | return; |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | if (e->mVectorSize == 3) { |
| 446 | mSkipPadding = true; |
| 447 | } else { |
| 448 | mSkipPadding = false; |
| 449 | } |
| 450 | |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 451 | if (mElementsCount >= mElementsVecSize) { |
Miao Wang | cf067b8 | 2015-09-14 18:27:17 -0700 | [diff] [blame] | 452 | // If pre-allocated space is full, allocate a larger one. |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 453 | mElementsVecSize += 8; |
| 454 | |
Rahul Chaudhry | 9ab1199 | 2017-02-07 16:33:46 -0800 | [diff] [blame] | 455 | android::RSC::sp<const Element> * newElements = (android::RSC::sp<const Element> *)calloc(mElementsVecSize, sizeof(android::RSC::sp<const Element>)); |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 456 | char ** newElementNames = (char **)calloc(mElementsVecSize, sizeof(char *)); |
| 457 | size_t * newElementNameLengths = (size_t*)calloc(mElementsVecSize, sizeof(size_t)); |
| 458 | uint32_t * newArraySizes = (uint32_t*)calloc(mElementsVecSize, sizeof(uint32_t)); |
| 459 | |
| 460 | memcpy(newElements, mElements, mElementsCount * sizeof(android::RSC::sp<Element>)); |
| 461 | memcpy(newElementNames, mElementNames, mElementsCount * sizeof(char *)); |
| 462 | memcpy(newElementNameLengths, mElementNameLengths, mElementsCount * sizeof(size_t)); |
| 463 | memcpy(newArraySizes, mArraySizes, mElementsCount * sizeof(uint32_t)); |
| 464 | |
Miao Wang | cf067b8 | 2015-09-14 18:27:17 -0700 | [diff] [blame] | 465 | // Free the old arrays. |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 466 | free(mElements); |
| 467 | free(mElementNames); |
| 468 | free(mArraySizes); |
| 469 | free(mElementNameLengths); |
| 470 | |
| 471 | mElements = newElements; |
| 472 | mElementNames = newElementNames; |
| 473 | mArraySizes = newArraySizes; |
| 474 | mElementNameLengths = newElementNameLengths; |
| 475 | } |
| 476 | mElements[mElementsCount] = e; |
| 477 | mArraySizes[mElementsCount] = arraySize; |
| 478 | |
| 479 | size_t nameLen = strlen(name); |
| 480 | mElementNameLengths[mElementsCount] = nameLen + 1; |
| 481 | mElementNames[mElementsCount] = (char *)calloc(nameLen + 1, sizeof(char)); |
| 482 | memcpy(mElementNames[mElementsCount], name, nameLen); |
| 483 | mElementNames[mElementsCount][nameLen] = 0; |
| 484 | |
| 485 | mElementsCount++; |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 486 | } |
| 487 | |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 488 | android::RSC::sp<const Element> Element::Builder::create() { |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 489 | size_t fieldCount = mElementsCount; |
| 490 | void ** elementArray = (void **)calloc(fieldCount, sizeof(void *)); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 491 | |
| 492 | for (size_t ct = 0; ct < fieldCount; ct++) { |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 493 | elementArray[ct] = mElements[ct]->getID(); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 494 | } |
| 495 | |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 496 | void *id = RS::dispatch->ElementCreate2(mRS->getContext(), |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 497 | (RsElement *)elementArray, fieldCount, |
| 498 | (const char **)mElementNames, fieldCount, mElementNameLengths, |
| 499 | mArraySizes, fieldCount); |
Jason Sams | 69cccdf | 2012-04-02 19:11:49 -0700 | [diff] [blame] | 500 | free(elementArray); |
Miao Wang | bc10dff | 2015-04-03 17:44:55 -0700 | [diff] [blame] | 501 | return new Element(id, mRS, mElements, mElementsCount, (const char **)mElementNames, mElementNameLengths, mArraySizes); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 502 | } |