blob: 05e5b685c553513e10d4d9c1384b08e59244f6d0 [file] [log] [blame]
Jason Sams221a4b12012-02-22 15:22:41 -08001/*
Jason Sams69cccdf2012-04-02 19:11:49 -07002 * Copyright (C) 2012 The Android Open Source Project
Jason Sams221a4b12012-02-22 15:22:41 -08003 *
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 Sams221a4b12012-02-22 15:22:41 -080017#include <malloc.h>
18#include <string.h>
19
20#include "RenderScript.h"
Tim Murrayeeaf7142013-09-09 15:03:50 -070021#include "rsCppInternal.h"
Jason Sams221a4b12012-02-22 15:22:41 -080022
Chih-Hung Hsiehdfcfabf2016-11-04 15:55:18 -070023using android::RSC::Element;
Jason Sams221a4b12012-02-22 15:22:41 -080024
Tim Murray89daad62013-07-29 14:30:02 -070025android::RSC::sp<const Element> Element::getSubElement(uint32_t index) {
Miao Wangbc10dff2015-04-03 17:44:55 -070026 if (!mVisibleElementMapSize) {
Tim Murray21fa7a02013-08-15 16:25:03 -070027 mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Element contains no sub-elements");
Chris Wailes44bef6f2014-08-12 13:51:10 -070028 return nullptr;
Jason Sams221a4b12012-02-22 15:22:41 -080029 }
Miao Wangbc10dff2015-04-03 17:44:55 -070030 if (index >= mVisibleElementMapSize) {
Tim Murray21fa7a02013-08-15 16:25:03 -070031 mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Illegal sub-element index");
Chris Wailes44bef6f2014-08-12 13:51:10 -070032 return nullptr;
Jason Sams221a4b12012-02-22 15:22:41 -080033 }
34 return mElements[mVisibleElementMap[index]];
35}
36
37const char * Element::getSubElementName(uint32_t index) {
Miao Wangbc10dff2015-04-03 17:44:55 -070038 if (!mVisibleElementMapSize) {
Tim Murray21fa7a02013-08-15 16:25:03 -070039 mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Element contains no sub-elements");
Chris Wailes44bef6f2014-08-12 13:51:10 -070040 return nullptr;
Jason Sams221a4b12012-02-22 15:22:41 -080041 }
Miao Wangbc10dff2015-04-03 17:44:55 -070042 if (index >= mVisibleElementMapSize) {
Tim Murray21fa7a02013-08-15 16:25:03 -070043 mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Illegal sub-element index");
Chris Wailes44bef6f2014-08-12 13:51:10 -070044 return nullptr;
Jason Sams221a4b12012-02-22 15:22:41 -080045 }
Miao Wangbc10dff2015-04-03 17:44:55 -070046 return mElementNames[mVisibleElementMap[index]];
Jason Sams221a4b12012-02-22 15:22:41 -080047}
48
49size_t Element::getSubElementArraySize(uint32_t index) {
Miao Wangbc10dff2015-04-03 17:44:55 -070050 if (!mVisibleElementMapSize) {
Tim Murray21fa7a02013-08-15 16:25:03 -070051 mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Element contains no sub-elements");
52 return 0;
Jason Sams221a4b12012-02-22 15:22:41 -080053 }
Miao Wangbc10dff2015-04-03 17:44:55 -070054 if (index >= mVisibleElementMapSize) {
Tim Murray21fa7a02013-08-15 16:25:03 -070055 mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Illegal sub-element index");
56 return 0;
Jason Sams221a4b12012-02-22 15:22:41 -080057 }
58 return mArraySizes[mVisibleElementMap[index]];
59}
60
61uint32_t Element::getSubElementOffsetBytes(uint32_t index) {
Miao Wangbc10dff2015-04-03 17:44:55 -070062 if (!mVisibleElementMapSize) {
Tim Murray21fa7a02013-08-15 16:25:03 -070063 mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Element contains no sub-elements");
64 return 0;
Jason Sams221a4b12012-02-22 15:22:41 -080065 }
Miao Wangbc10dff2015-04-03 17:44:55 -070066 if (index >= mVisibleElementMapSize) {
Tim Murray21fa7a02013-08-15 16:25:03 -070067 mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Illegal sub-element index");
68 return 0;
Jason Sams221a4b12012-02-22 15:22:41 -080069 }
70 return mOffsetInBytes[mVisibleElementMap[index]];
71}
72
73
Chih-Hung Hsieh45768e12016-08-10 12:32:11 -070074#define CREATE_USER(N, T) android::RSC::sp<const Element> Element::N(const android::RSC::sp<RS>& rs) { \
Chris Wailes44bef6f2014-08-12 13:51:10 -070075 if (rs->mElements.N == nullptr) { \
Tim Murrayeb4426d2013-08-27 15:30:16 -070076 rs->mElements.N = (createUser(rs, RS_TYPE_##T)); \
Tim Murray729b6fe2013-07-23 16:20:42 -070077 } \
78 return rs->mElements.N; \
79 }
80
Jason Sams221a4b12012-02-22 15:22:41 -080081CREATE_USER(BOOLEAN, BOOLEAN);
82CREATE_USER(U8, UNSIGNED_8);
83CREATE_USER(I8, SIGNED_8);
84CREATE_USER(U16, UNSIGNED_16);
85CREATE_USER(I16, SIGNED_16);
86CREATE_USER(U32, UNSIGNED_32);
87CREATE_USER(I32, SIGNED_32);
88CREATE_USER(U64, UNSIGNED_64);
89CREATE_USER(I64, SIGNED_64);
Pirama Arumuga Nainar56616842015-12-02 11:40:54 -080090CREATE_USER(F16, FLOAT_16);
Jason Sams221a4b12012-02-22 15:22:41 -080091CREATE_USER(F32, FLOAT_32);
92CREATE_USER(F64, FLOAT_64);
93CREATE_USER(ELEMENT, ELEMENT);
94CREATE_USER(TYPE, TYPE);
95CREATE_USER(ALLOCATION, ALLOCATION);
96CREATE_USER(SAMPLER, SAMPLER);
97CREATE_USER(SCRIPT, SCRIPT);
Jason Sams221a4b12012-02-22 15:22:41 -080098CREATE_USER(MATRIX_4X4, MATRIX_4X4);
99CREATE_USER(MATRIX_3X3, MATRIX_3X3);
100CREATE_USER(MATRIX_2X2, MATRIX_2X2);
101
Chih-Hung Hsieh45768e12016-08-10 12:32:11 -0700102#define CREATE_PIXEL(N, T, K) android::RSC::sp<const Element> Element::N(const android::RSC::sp<RS> &rs) { \
Chris Wailes44bef6f2014-08-12 13:51:10 -0700103 if (rs->mElements.N == nullptr) { \
104 rs->mElements.N = createPixel(rs, RS_TYPE_##T, RS_KIND_##K); \
105 } \
106 return rs->mElements.N; \
Jason Sams221a4b12012-02-22 15:22:41 -0800107}
Tim Murrayeb4426d2013-08-27 15:30:16 -0700108
Jason Sams221a4b12012-02-22 15:22:41 -0800109CREATE_PIXEL(A_8, UNSIGNED_8, PIXEL_A);
110CREATE_PIXEL(RGB_565, UNSIGNED_5_6_5, PIXEL_RGB);
111CREATE_PIXEL(RGB_888, UNSIGNED_8, PIXEL_RGB);
112CREATE_PIXEL(RGBA_4444, UNSIGNED_4_4_4_4, PIXEL_RGBA);
113CREATE_PIXEL(RGBA_8888, UNSIGNED_8, PIXEL_RGBA);
Tim Murrayeb4426d2013-08-27 15:30:16 -0700114CREATE_PIXEL(YUV, UNSIGNED_8, PIXEL_YUV);
Tim Murray071aee62013-12-09 11:59:48 -0800115CREATE_PIXEL(RGBA_5551, UNSIGNED_5_5_5_1, PIXEL_RGBA);
Jason Sams221a4b12012-02-22 15:22:41 -0800116
Chih-Hung Hsieh45768e12016-08-10 12:32:11 -0700117#define CREATE_VECTOR(N, T) android::RSC::sp<const Element> Element::N##_2(const android::RSC::sp<RS> &rs) { \
Chris Wailes44bef6f2014-08-12 13:51:10 -0700118 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 Hsieh45768e12016-08-10 12:32:11 -0700123android::RSC::sp<const Element> Element::N##_3(const android::RSC::sp<RS> &rs) { \
Chris Wailes44bef6f2014-08-12 13:51:10 -0700124 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 Sams221a4b12012-02-22 15:22:41 -0800128} \
Chih-Hung Hsieh45768e12016-08-10 12:32:11 -0700129android::RSC::sp<const Element> Element::N##_4(const android::RSC::sp<RS> &rs) { \
Chris Wailes44bef6f2014-08-12 13:51:10 -0700130 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 Sams221a4b12012-02-22 15:22:41 -0800134}
135CREATE_VECTOR(U8, UNSIGNED_8);
136CREATE_VECTOR(I8, SIGNED_8);
137CREATE_VECTOR(U16, UNSIGNED_16);
138CREATE_VECTOR(I16, SIGNED_16);
139CREATE_VECTOR(U32, UNSIGNED_32);
140CREATE_VECTOR(I32, SIGNED_32);
141CREATE_VECTOR(U64, UNSIGNED_64);
142CREATE_VECTOR(I64, SIGNED_64);
Pirama Arumuga Nainar56616842015-12-02 11:40:54 -0800143CREATE_VECTOR(F16, FLOAT_16);
Jason Sams221a4b12012-02-22 15:22:41 -0800144CREATE_VECTOR(F32, FLOAT_32);
145CREATE_VECTOR(F64, FLOAT_64);
146
147
148void Element::updateVisibleSubElements() {
Miao Wangbc10dff2015-04-03 17:44:55 -0700149 if (!mElementsCount) {
Jason Sams221a4b12012-02-22 15:22:41 -0800150 return;
151 }
Miao Wangbc10dff2015-04-03 17:44:55 -0700152 if (mVisibleElementMapSize) {
153 free(mVisibleElementMap);
154 mVisibleElementMapSize = 0;
155 }
156 mVisibleElementMap = (uint32_t*)calloc(mElementsCount, sizeof(uint32_t));
Jason Sams221a4b12012-02-22 15:22:41 -0800157
158 int noPaddingFieldCount = 0;
Miao Wangbc10dff2015-04-03 17:44:55 -0700159 size_t fieldCount = mElementsCount;
Miao Wangcf067b82015-09-14 18:27:17 -0700160 // Find out how many elements are not padding.
Jason Sams221a4b12012-02-22 15:22:41 -0800161 for (size_t ct = 0; ct < fieldCount; ct ++) {
Miao Wangbc10dff2015-04-03 17:44:55 -0700162 if (mElementNames[ct][0] != '#') {
Jason Sams221a4b12012-02-22 15:22:41 -0800163 noPaddingFieldCount ++;
164 }
165 }
166
Miao Wangcf067b82015-09-14 18:27:17 -0700167 // Make a map that points us at non-padding elements.
Miao Wangbc10dff2015-04-03 17:44:55 -0700168 size_t i = 0;
Jason Sams221a4b12012-02-22 15:22:41 -0800169 for (size_t ct = 0; ct < fieldCount; ct ++) {
Miao Wangbc10dff2015-04-03 17:44:55 -0700170 if (mElementNames[ct][0] != '#') {
171 mVisibleElementMap[i++] = (uint32_t)ct;
Jason Sams221a4b12012-02-22 15:22:41 -0800172 }
173 }
Miao Wangbc10dff2015-04-03 17:44:55 -0700174 mVisibleElementMapSize = i;
Jason Sams221a4b12012-02-22 15:22:41 -0800175}
176
Tim Murray89daad62013-07-29 14:30:02 -0700177Element::Element(void *id, android::RSC::sp<RS> rs,
Miao Wangbc10dff2015-04-03 17:44:55 -0700178 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 Sams221a4b12012-02-22 15:22:41 -0800183 mSizeBytes = 0;
184 mVectorSize = 1;
Miao Wangbc10dff2015-04-03 17:44:55 -0700185 mElementsCount = elementCount;
186 mVisibleElementMap = nullptr;
187 mVisibleElementMapSize = 0;
188
Rahul Chaudhry9ab11992017-02-07 16:33:46 -0800189 mElements = (android::RSC::sp<const Element> *)calloc(mElementsCount, sizeof(android::RSC::sp<const Element>));
Miao Wangbc10dff2015-04-03 17:44:55 -0700190 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 Wangcf067b82015-09-14 18:27:17 -0700198 // Copy strings (char array).
Miao Wangbc10dff2015-04-03 17:44:55 -0700199 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 Sams221a4b12012-02-22 15:22:41 -0800205
206 mType = RS_TYPE_NONE;
207 mKind = RS_KIND_USER;
208
Miao Wangbc10dff2015-04-03 17:44:55 -0700209 for (size_t ct = 0; ct < mElementsCount; ct++ ) {
210 mOffsetInBytes[ct] = mSizeBytes;
Jason Sams221a4b12012-02-22 15:22:41 -0800211 mSizeBytes += mElements[ct]->mSizeBytes * mArraySizes[ct];
212 }
213 updateVisibleSubElements();
214}
215
Miao Wang70d89952015-09-14 15:05:41 -0700216Element::Element(void *id, android::RSC::sp<RS> rs) :
217 BaseObj(id, rs) {
218}
Jason Sams221a4b12012-02-22 15:22:41 -0800219
220static 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 Murray89daad62013-07-29 14:30:02 -0700273Element::Element(void *id, android::RSC::sp<RS> rs,
Jason Sams221a4b12012-02-22 15:22:41 -0800274 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 Wangbc10dff2015-04-03 17:44:55 -0700293 mElementsCount = 0;
294 mVisibleElementMap = 0;
Jason Sams221a4b12012-02-22 15:22:41 -0800295}
296
297Element::~Element() {
Miao Wangbc10dff2015-04-03 17:44:55 -0700298 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 Sams221a4b12012-02-22 15:22:41 -0800311}
312
Jason Sams221a4b12012-02-22 15:22:41 -0800313void Element::updateFromNative() {
314 BaseObj::updateFromNative();
Jason Sams221a4b12012-02-22 15:22:41 -0800315 updateVisibleSubElements();
316}
317
Chih-Hung Hsieh45768e12016-08-10 12:32:11 -0700318android::RSC::sp<const Element> Element::createUser(const android::RSC::sp<RS>& rs, RsDataType dt) {
Tim Murraya4230962013-07-17 16:50:10 -0700319 void * id = RS::dispatch->ElementCreate(rs->getContext(), dt, RS_KIND_USER, false, 1);
Jason Sams221a4b12012-02-22 15:22:41 -0800320 return new Element(id, rs, dt, RS_KIND_USER, false, 1);
321}
322
Chih-Hung Hsieh45768e12016-08-10 12:32:11 -0700323android::RSC::sp<const Element> Element::createVector(const android::RSC::sp<RS>& rs, RsDataType dt, uint32_t size) {
Jason Sams221a4b12012-02-22 15:22:41 -0800324 if (size < 2 || size > 4) {
Tim Murray21fa7a02013-08-15 16:25:03 -0700325 rs->throwError(RS_ERROR_INVALID_PARAMETER, "Vector size out of range 2-4.");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700326 return nullptr;
Jason Sams221a4b12012-02-22 15:22:41 -0800327 }
Tim Murraya4230962013-07-17 16:50:10 -0700328 void *id = RS::dispatch->ElementCreate(rs->getContext(), dt, RS_KIND_USER, false, size);
Jason Sams221a4b12012-02-22 15:22:41 -0800329 return new Element(id, rs, dt, RS_KIND_USER, false, size);
330}
331
Chih-Hung Hsieh45768e12016-08-10 12:32:11 -0700332android::RSC::sp<const Element> Element::createPixel(const android::RSC::sp<RS>& rs, RsDataType dt, RsDataKind dk) {
Jason Sams221a4b12012-02-22 15:22:41 -0800333 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 Wang43a6cba2015-04-12 13:39:56 -0700338 dk == RS_KIND_PIXEL_DEPTH ||
339 dk == RS_KIND_PIXEL_YUV)) {
Tim Murray21fa7a02013-08-15 16:25:03 -0700340 rs->throwError(RS_ERROR_INVALID_PARAMETER, "Unsupported DataKind");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700341 return nullptr;
Jason Sams221a4b12012-02-22 15:22:41 -0800342 }
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 Murray21fa7a02013-08-15 16:25:03 -0700348 rs->throwError(RS_ERROR_INVALID_PARAMETER, "Unsupported DataType");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700349 return nullptr;
Jason Sams221a4b12012-02-22 15:22:41 -0800350 }
351 if (dt == RS_TYPE_UNSIGNED_5_6_5 && dk != RS_KIND_PIXEL_RGB) {
Tim Murray21fa7a02013-08-15 16:25:03 -0700352 rs->throwError(RS_ERROR_INVALID_PARAMETER, "Bad kind and type combo");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700353 return nullptr;
Jason Sams221a4b12012-02-22 15:22:41 -0800354 }
355 if (dt == RS_TYPE_UNSIGNED_5_5_5_1 && dk != RS_KIND_PIXEL_RGBA) {
Tim Murray21fa7a02013-08-15 16:25:03 -0700356 rs->throwError(RS_ERROR_INVALID_PARAMETER, "Bad kind and type combo");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700357 return nullptr;
Jason Sams221a4b12012-02-22 15:22:41 -0800358 }
359 if (dt == RS_TYPE_UNSIGNED_4_4_4_4 && dk != RS_KIND_PIXEL_RGBA) {
Tim Murray21fa7a02013-08-15 16:25:03 -0700360 rs->throwError(RS_ERROR_INVALID_PARAMETER, "Bad kind and type combo");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700361 return nullptr;
Jason Sams221a4b12012-02-22 15:22:41 -0800362 }
363 if (dt == RS_TYPE_UNSIGNED_16 && dk != RS_KIND_PIXEL_DEPTH) {
Tim Murray21fa7a02013-08-15 16:25:03 -0700364 rs->throwError(RS_ERROR_INVALID_PARAMETER, "Bad kind and type combo");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700365 return nullptr;
Jason Sams221a4b12012-02-22 15:22:41 -0800366 }
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 Murraya4230962013-07-17 16:50:10 -0700386 void * id = RS::dispatch->ElementCreate(rs->getContext(), dt, dk, true, size);
Jason Sams221a4b12012-02-22 15:22:41 -0800387 return new Element(id, rs, dt, dk, true, size);
388}
389
Chih-Hung Hsieh45768e12016-08-10 12:32:11 -0700390bool Element::isCompatible(const android::RSC::sp<const Element>&e) const {
Jason Sams221a4b12012-02-22 15:22:41 -0800391 // Try strict BaseObj equality to start with.
Jason Sams69cccdf2012-04-02 19:11:49 -0700392 if (this == e.get()) {
Jason Sams221a4b12012-02-22 15:22:41 -0800393 return true;
394 }
395
Miao Wangcf067b82015-09-14 18:27:17 -0700396 /*
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 Sams221a4b12012-02-22 15:22:41 -0800402 return ((mSizeBytes == e->mSizeBytes) &&
Jason Sams69cccdf2012-04-02 19:11:49 -0700403 (mType != RS_TYPE_NONE) &&
Jason Sams221a4b12012-02-22 15:22:41 -0800404 (mType == e->mType) &&
405 (mVectorSize == e->mVectorSize));
406}
407
Tim Murray89daad62013-07-29 14:30:02 -0700408Element::Builder::Builder(android::RSC::sp<RS> rs) {
Tim Murray35609072013-12-03 11:36:03 -0800409 mRS = rs.get();
Jason Sams221a4b12012-02-22 15:22:41 -0800410 mSkipPadding = false;
Miao Wangbc10dff2015-04-03 17:44:55 -0700411 mElementsVecSize = 8;
412 mElementsCount = 0;
Miao Wangcf067b82015-09-14 18:27:17 -0700413 // Initialize space.
Rahul Chaudhry9ab11992017-02-07 16:33:46 -0800414 mElements = (android::RSC::sp<const Element> *)calloc(mElementsVecSize, sizeof(android::RSC::sp<const Element>));
Miao Wangbc10dff2015-04-03 17:44:55 -0700415 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 Sams221a4b12012-02-22 15:22:41 -0800418}
419
Miao Wangbc10dff2015-04-03 17:44:55 -0700420Element::Builder::~Builder() {
Miao Wangcf067b82015-09-14 18:27:17 -0700421 // Free allocated space.
Miao Wangbc10dff2015-04-03 17:44:55 -0700422 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 Hsieh45768e12016-08-10 12:32:11 -0700431void Element::Builder::add(const android::RSC::sp<const Element>&e, const char * name, uint32_t arraySize) {
Jason Sams221a4b12012-02-22 15:22:41 -0800432 // Skip padding fields after a vector 3 type.
433 if (mSkipPadding) {
434 const char *s1 = "#padding_";
Miao Wangbc10dff2015-04-03 17:44:55 -0700435 const char *s2 = name;
Jason Sams221a4b12012-02-22 15:22:41 -0800436 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 Wangbc10dff2015-04-03 17:44:55 -0700451 if (mElementsCount >= mElementsVecSize) {
Miao Wangcf067b82015-09-14 18:27:17 -0700452 // If pre-allocated space is full, allocate a larger one.
Miao Wangbc10dff2015-04-03 17:44:55 -0700453 mElementsVecSize += 8;
454
Rahul Chaudhry9ab11992017-02-07 16:33:46 -0800455 android::RSC::sp<const Element> * newElements = (android::RSC::sp<const Element> *)calloc(mElementsVecSize, sizeof(android::RSC::sp<const Element>));
Miao Wangbc10dff2015-04-03 17:44:55 -0700456 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 Wangcf067b82015-09-14 18:27:17 -0700465 // Free the old arrays.
Miao Wangbc10dff2015-04-03 17:44:55 -0700466 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 Sams221a4b12012-02-22 15:22:41 -0800486}
487
Tim Murray89daad62013-07-29 14:30:02 -0700488android::RSC::sp<const Element> Element::Builder::create() {
Miao Wangbc10dff2015-04-03 17:44:55 -0700489 size_t fieldCount = mElementsCount;
490 void ** elementArray = (void **)calloc(fieldCount, sizeof(void *));
Jason Sams221a4b12012-02-22 15:22:41 -0800491
492 for (size_t ct = 0; ct < fieldCount; ct++) {
Miao Wangbc10dff2015-04-03 17:44:55 -0700493 elementArray[ct] = mElements[ct]->getID();
Jason Sams221a4b12012-02-22 15:22:41 -0800494 }
495
Tim Murraya4230962013-07-17 16:50:10 -0700496 void *id = RS::dispatch->ElementCreate2(mRS->getContext(),
Miao Wangbc10dff2015-04-03 17:44:55 -0700497 (RsElement *)elementArray, fieldCount,
498 (const char **)mElementNames, fieldCount, mElementNameLengths,
499 mArraySizes, fieldCount);
Jason Sams69cccdf2012-04-02 19:11:49 -0700500 free(elementArray);
Miao Wangbc10dff2015-04-03 17:44:55 -0700501 return new Element(id, mRS, mElements, mElementsCount, (const char **)mElementNames, mElementNameLengths, mArraySizes);
Jason Sams221a4b12012-02-22 15:22:41 -0800502}