• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
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 
17 #include <malloc.h>
18 #include <string.h>
19 
20 #include "RenderScript.h"
21 #include "rsCppInternal.h"
22 
23 using android::RSC::Element;
24 
getSubElement(uint32_t index)25 android::RSC::sp<const Element> Element::getSubElement(uint32_t index) {
26     if (!mVisibleElementMapSize) {
27         mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Element contains no sub-elements");
28         return nullptr;
29     }
30     if (index >= mVisibleElementMapSize) {
31         mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Illegal sub-element index");
32         return nullptr;
33     }
34     return mElements[mVisibleElementMap[index]];
35 }
36 
getSubElementName(uint32_t index)37 const char * Element::getSubElementName(uint32_t index) {
38     if (!mVisibleElementMapSize) {
39         mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Element contains no sub-elements");
40         return nullptr;
41     }
42     if (index >= mVisibleElementMapSize) {
43         mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Illegal sub-element index");
44         return nullptr;
45     }
46     return mElementNames[mVisibleElementMap[index]];
47 }
48 
getSubElementArraySize(uint32_t index)49 size_t Element::getSubElementArraySize(uint32_t index) {
50     if (!mVisibleElementMapSize) {
51         mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Element contains no sub-elements");
52         return 0;
53     }
54     if (index >= mVisibleElementMapSize) {
55         mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Illegal sub-element index");
56         return 0;
57     }
58     return mArraySizes[mVisibleElementMap[index]];
59 }
60 
getSubElementOffsetBytes(uint32_t index)61 uint32_t Element::getSubElementOffsetBytes(uint32_t index) {
62     if (!mVisibleElementMapSize) {
63         mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Element contains no sub-elements");
64         return 0;
65     }
66     if (index >= mVisibleElementMapSize) {
67         mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Illegal sub-element index");
68         return 0;
69     }
70     return mOffsetInBytes[mVisibleElementMap[index]];
71 }
72 
73 
74 #define CREATE_USER(N, T) android::RSC::sp<const Element> Element::N(const android::RSC::sp<RS>& rs) { \
75     if (rs->mElements.N == nullptr) {                               \
76         rs->mElements.N = (createUser(rs, RS_TYPE_##T));            \
77     }                                                               \
78     return rs->mElements.N;                                         \
79     }
80 
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);
90 CREATE_USER(F16, FLOAT_16);
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);
98 CREATE_USER(MATRIX_4X4, MATRIX_4X4);
99 CREATE_USER(MATRIX_3X3, MATRIX_3X3);
100 CREATE_USER(MATRIX_2X2, MATRIX_2X2);
101 
102 #define CREATE_PIXEL(N, T, K) android::RSC::sp<const Element> Element::N(const android::RSC::sp<RS> &rs) { \
103     if (rs->mElements.N == nullptr) {                                \
104         rs->mElements.N = createPixel(rs, RS_TYPE_##T, RS_KIND_##K); \
105     }                                                                \
106     return rs->mElements.N;                                          \
107 }
108 
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);
114 CREATE_PIXEL(YUV, UNSIGNED_8, PIXEL_YUV);
115 CREATE_PIXEL(RGBA_5551, UNSIGNED_5_5_5_1, PIXEL_RGBA);
116 
117 #define CREATE_VECTOR(N, T) android::RSC::sp<const Element> Element::N##_2(const android::RSC::sp<RS> &rs) { \
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 }                                                                         \
123 android::RSC::sp<const Element> Element::N##_3(const android::RSC::sp<RS> &rs) { \
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;                                           \
128 } \
129 android::RSC::sp<const Element> Element::N##_4(const android::RSC::sp<RS> &rs) { \
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;                                           \
134 }
135 CREATE_VECTOR(U8, UNSIGNED_8);
136 CREATE_VECTOR(I8, SIGNED_8);
137 CREATE_VECTOR(U16, UNSIGNED_16);
138 CREATE_VECTOR(I16, SIGNED_16);
139 CREATE_VECTOR(U32, UNSIGNED_32);
140 CREATE_VECTOR(I32, SIGNED_32);
141 CREATE_VECTOR(U64, UNSIGNED_64);
142 CREATE_VECTOR(I64, SIGNED_64);
143 CREATE_VECTOR(F16, FLOAT_16);
144 CREATE_VECTOR(F32, FLOAT_32);
145 CREATE_VECTOR(F64, FLOAT_64);
146 
147 
updateVisibleSubElements()148 void Element::updateVisibleSubElements() {
149     if (!mElementsCount) {
150         return;
151     }
152     if (mVisibleElementMapSize) {
153         free(mVisibleElementMap);
154         mVisibleElementMapSize = 0;
155     }
156     mVisibleElementMap = (uint32_t*)calloc(mElementsCount, sizeof(uint32_t));
157 
158     int noPaddingFieldCount = 0;
159     size_t fieldCount = mElementsCount;
160     // Find out how many elements are not padding.
161     for (size_t ct = 0; ct < fieldCount; ct ++) {
162         if (mElementNames[ct][0] != '#') {
163             noPaddingFieldCount ++;
164         }
165     }
166 
167     // Make a map that points us at non-padding elements.
168     size_t i = 0;
169     for (size_t ct = 0; ct < fieldCount; ct ++) {
170         if (mElementNames[ct][0] != '#') {
171             mVisibleElementMap[i++] = (uint32_t)ct;
172         }
173     }
174     mVisibleElementMapSize = i;
175 }
176 
Element(void * id,android::RSC::sp<RS> rs,android::RSC::sp<const Element> * elements,size_t elementCount,const char ** elementNames,size_t * elementNameLengths,uint32_t * arraySizes)177 Element::Element(void *id, android::RSC::sp<RS> rs,
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) {
183     mSizeBytes = 0;
184     mVectorSize = 1;
185     mElementsCount = elementCount;
186     mVisibleElementMap = nullptr;
187     mVisibleElementMapSize = 0;
188 
189     mElements = (android::RSC::sp<const Element> *)calloc(mElementsCount, sizeof(android::RSC::sp<const Element>));
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 
198     // Copy strings (char array).
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     }
205 
206     mType = RS_TYPE_NONE;
207     mKind = RS_KIND_USER;
208 
209     for (size_t ct = 0; ct < mElementsCount; ct++ ) {
210         mOffsetInBytes[ct] = mSizeBytes;
211         mSizeBytes += mElements[ct]->mSizeBytes * mArraySizes[ct];
212     }
213     updateVisibleSubElements();
214 }
215 
Element(void * id,android::RSC::sp<RS> rs)216 Element::Element(void *id, android::RSC::sp<RS> rs) :
217     BaseObj(id, rs) {
218 }
219 
GetSizeInBytesForType(RsDataType dt)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 
Element(void * id,android::RSC::sp<RS> rs,RsDataType dt,RsDataKind dk,bool norm,uint32_t size)273 Element::Element(void *id, android::RSC::sp<RS> rs,
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;
293     mElementsCount = 0;
294     mVisibleElementMap = 0;
295 }
296 
~Element()297 Element::~Element() {
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     }
311 }
312 
updateFromNative()313 void Element::updateFromNative() {
314     BaseObj::updateFromNative();
315     updateVisibleSubElements();
316 }
317 
createUser(const android::RSC::sp<RS> & rs,RsDataType dt)318 android::RSC::sp<const Element> Element::createUser(const android::RSC::sp<RS>& rs, RsDataType dt) {
319     void * id = RS::dispatch->ElementCreate(rs->getContext(), dt, RS_KIND_USER, false, 1);
320     return new Element(id, rs, dt, RS_KIND_USER, false, 1);
321 }
322 
createVector(const android::RSC::sp<RS> & rs,RsDataType dt,uint32_t size)323 android::RSC::sp<const Element> Element::createVector(const android::RSC::sp<RS>& rs, RsDataType dt, uint32_t size) {
324     if (size < 2 || size > 4) {
325         rs->throwError(RS_ERROR_INVALID_PARAMETER, "Vector size out of range 2-4.");
326         return nullptr;
327     }
328     void *id = RS::dispatch->ElementCreate(rs->getContext(), dt, RS_KIND_USER, false, size);
329     return new Element(id, rs, dt, RS_KIND_USER, false, size);
330 }
331 
createPixel(const android::RSC::sp<RS> & rs,RsDataType dt,RsDataKind dk)332 android::RSC::sp<const Element> Element::createPixel(const android::RSC::sp<RS>& rs, RsDataType dt, RsDataKind dk) {
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 ||
338           dk == RS_KIND_PIXEL_DEPTH ||
339           dk == RS_KIND_PIXEL_YUV)) {
340         rs->throwError(RS_ERROR_INVALID_PARAMETER, "Unsupported DataKind");
341         return nullptr;
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)) {
348         rs->throwError(RS_ERROR_INVALID_PARAMETER, "Unsupported DataType");
349         return nullptr;
350     }
351     if (dt == RS_TYPE_UNSIGNED_5_6_5 && dk != RS_KIND_PIXEL_RGB) {
352         rs->throwError(RS_ERROR_INVALID_PARAMETER, "Bad kind and type combo");
353         return nullptr;
354     }
355     if (dt == RS_TYPE_UNSIGNED_5_5_5_1 && dk != RS_KIND_PIXEL_RGBA) {
356         rs->throwError(RS_ERROR_INVALID_PARAMETER, "Bad kind and type combo");
357         return nullptr;
358     }
359     if (dt == RS_TYPE_UNSIGNED_4_4_4_4 && dk != RS_KIND_PIXEL_RGBA) {
360         rs->throwError(RS_ERROR_INVALID_PARAMETER, "Bad kind and type combo");
361         return nullptr;
362     }
363     if (dt == RS_TYPE_UNSIGNED_16 && dk != RS_KIND_PIXEL_DEPTH) {
364         rs->throwError(RS_ERROR_INVALID_PARAMETER, "Bad kind and type combo");
365         return nullptr;
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 
386     void * id = RS::dispatch->ElementCreate(rs->getContext(), dt, dk, true, size);
387     return new Element(id, rs, dt, dk, true, size);
388 }
389 
isCompatible(const android::RSC::sp<const Element> & e) const390 bool Element::isCompatible(const android::RSC::sp<const Element>&e) const {
391     // Try strict BaseObj equality to start with.
392     if (this == e.get()) {
393         return true;
394     }
395 
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      */
402     return ((mSizeBytes == e->mSizeBytes) &&
403             (mType != RS_TYPE_NONE) &&
404             (mType == e->mType) &&
405             (mVectorSize == e->mVectorSize));
406 }
407 
Builder(android::RSC::sp<RS> rs)408 Element::Builder::Builder(android::RSC::sp<RS> rs) {
409     mRS = rs.get();
410     mSkipPadding = false;
411     mElementsVecSize = 8;
412     mElementsCount = 0;
413     // Initialize space.
414     mElements = (android::RSC::sp<const Element> *)calloc(mElementsVecSize, sizeof(android::RSC::sp<const Element>));
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));
418 }
419 
~Builder()420 Element::Builder::~Builder() {
421     // Free allocated space.
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 
add(const android::RSC::sp<const Element> & e,const char * name,uint32_t arraySize)431 void Element::Builder::add(const android::RSC::sp<const Element>&e, const char * name, uint32_t arraySize) {
432     // Skip padding fields after a vector 3 type.
433     if (mSkipPadding) {
434         const char *s1 = "#padding_";
435         const char *s2 = name;
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 
451     if (mElementsCount >= mElementsVecSize) {
452         // If pre-allocated space is full, allocate a larger one.
453         mElementsVecSize += 8;
454 
455         android::RSC::sp<const Element> * newElements = (android::RSC::sp<const Element> *)calloc(mElementsVecSize, sizeof(android::RSC::sp<const Element>));
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 
465         // Free the old arrays.
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++;
486 }
487 
create()488 android::RSC::sp<const Element> Element::Builder::create() {
489     size_t fieldCount = mElementsCount;
490     void ** elementArray = (void **)calloc(fieldCount, sizeof(void *));
491 
492     for (size_t ct = 0; ct < fieldCount; ct++) {
493         elementArray[ct] = mElements[ct]->getID();
494     }
495 
496     void *id = RS::dispatch->ElementCreate2(mRS->getContext(),
497                                             (RsElement *)elementArray, fieldCount,
498                                             (const char **)mElementNames, fieldCount, mElementNameLengths,
499                                             mArraySizes, fieldCount);
500     free(elementArray);
501     return new Element(id, mRS, mElements, mElementsCount, (const char **)mElementNames, mElementNameLengths, mArraySizes);
502 }
503