blob: d3fb29a28a27b25bbd9f2cdfa3bcddcd7a41281d [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
Jason Sams69cccdf2012-04-02 19:11:49 -070023using namespace android;
Tim Murray9eb7f4b2012-11-16 14:02:18 -080024using namespace RSC;
Jason Sams221a4b12012-02-22 15:22:41 -080025
Tim Murray89daad62013-07-29 14:30:02 -070026android::RSC::sp<const Element> Element::getSubElement(uint32_t index) {
Jason Sams221a4b12012-02-22 15:22:41 -080027 if (!mVisibleElementMap.size()) {
Tim Murray21fa7a02013-08-15 16:25:03 -070028 mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Element contains no sub-elements");
29 return NULL;
Jason Sams221a4b12012-02-22 15:22:41 -080030 }
31 if (index >= mVisibleElementMap.size()) {
Tim Murray21fa7a02013-08-15 16:25:03 -070032 mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Illegal sub-element index");
33 return NULL;
Jason Sams221a4b12012-02-22 15:22:41 -080034 }
35 return mElements[mVisibleElementMap[index]];
36}
37
38const char * Element::getSubElementName(uint32_t index) {
39 if (!mVisibleElementMap.size()) {
Tim Murray21fa7a02013-08-15 16:25:03 -070040 mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Element contains no sub-elements");
41 return NULL;
Jason Sams221a4b12012-02-22 15:22:41 -080042 }
43 if (index >= mVisibleElementMap.size()) {
Tim Murray21fa7a02013-08-15 16:25:03 -070044 mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Illegal sub-element index");
45 return NULL;
Jason Sams221a4b12012-02-22 15:22:41 -080046 }
Tim Murrayab716362013-08-12 12:37:18 -070047 return mElementNames[mVisibleElementMap[index]].c_str();
Jason Sams221a4b12012-02-22 15:22:41 -080048}
49
50size_t Element::getSubElementArraySize(uint32_t index) {
51 if (!mVisibleElementMap.size()) {
Tim Murray21fa7a02013-08-15 16:25:03 -070052 mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Element contains no sub-elements");
53 return 0;
Jason Sams221a4b12012-02-22 15:22:41 -080054 }
55 if (index >= mVisibleElementMap.size()) {
Tim Murray21fa7a02013-08-15 16:25:03 -070056 mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Illegal sub-element index");
57 return 0;
Jason Sams221a4b12012-02-22 15:22:41 -080058 }
59 return mArraySizes[mVisibleElementMap[index]];
60}
61
62uint32_t Element::getSubElementOffsetBytes(uint32_t index) {
63 if (mVisibleElementMap.size()) {
Tim Murray21fa7a02013-08-15 16:25:03 -070064 mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Element contains no sub-elements");
65 return 0;
Jason Sams221a4b12012-02-22 15:22:41 -080066 }
67 if (index >= mVisibleElementMap.size()) {
Tim Murray21fa7a02013-08-15 16:25:03 -070068 mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Illegal sub-element index");
69 return 0;
Jason Sams221a4b12012-02-22 15:22:41 -080070 }
71 return mOffsetInBytes[mVisibleElementMap[index]];
72}
73
74
Tim Murray89daad62013-07-29 14:30:02 -070075#define CREATE_USER(N, T) android::RSC::sp<const Element> Element::N(android::RSC::sp<RS> rs) { \
Tim Murray729b6fe2013-07-23 16:20:42 -070076 if (rs->mElements.N == NULL) { \
Tim Murrayeb4426d2013-08-27 15:30:16 -070077 rs->mElements.N = (createUser(rs, RS_TYPE_##T)); \
Tim Murray729b6fe2013-07-23 16:20:42 -070078 } \
79 return rs->mElements.N; \
80 }
81
Jason Sams221a4b12012-02-22 15:22:41 -080082CREATE_USER(BOOLEAN, BOOLEAN);
83CREATE_USER(U8, UNSIGNED_8);
84CREATE_USER(I8, SIGNED_8);
85CREATE_USER(U16, UNSIGNED_16);
86CREATE_USER(I16, SIGNED_16);
87CREATE_USER(U32, UNSIGNED_32);
88CREATE_USER(I32, SIGNED_32);
89CREATE_USER(U64, UNSIGNED_64);
90CREATE_USER(I64, SIGNED_64);
91CREATE_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
Tim Murray89daad62013-07-29 14:30:02 -0700102#define CREATE_PIXEL(N, T, K) android::RSC::sp<const Element> Element::N(android::RSC::sp<RS> rs) { \
Tim Murrayeb4426d2013-08-27 15:30:16 -0700103 if (rs->mElements.N == NULL) { \
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);
Jason Sams221a4b12012-02-22 15:22:41 -0800115
Tim Murray89daad62013-07-29 14:30:02 -0700116#define CREATE_VECTOR(N, T) android::RSC::sp<const Element> Element::N##_2(android::RSC::sp<RS> rs) { \
Tim Murrayeb4426d2013-08-27 15:30:16 -0700117 if (rs->mElements.N##_2 == NULL) { \
118 rs->mElements.N##_2 = createVector(rs, RS_TYPE_##T, 2); \
119 } \
120 return rs->mElements.N##_2; \
121} \
Tim Murray89daad62013-07-29 14:30:02 -0700122android::RSC::sp<const Element> Element::N##_3(android::RSC::sp<RS> rs) { \
Tim Murrayeb4426d2013-08-27 15:30:16 -0700123 if (rs->mElements.N##_3 == NULL) { \
124 rs->mElements.N##_3 = createVector(rs, RS_TYPE_##T, 3); \
125 } \
126 return rs->mElements.N##_3; \
Jason Sams221a4b12012-02-22 15:22:41 -0800127} \
Tim Murray89daad62013-07-29 14:30:02 -0700128android::RSC::sp<const Element> Element::N##_4(android::RSC::sp<RS> rs) { \
Tim Murrayeb4426d2013-08-27 15:30:16 -0700129 if (rs->mElements.N##_4 == NULL) { \
130 rs->mElements.N##_4 = createVector(rs, RS_TYPE_##T, 4); \
131 } \
132 return rs->mElements.N##_4; \
Jason Sams221a4b12012-02-22 15:22:41 -0800133}
134CREATE_VECTOR(U8, UNSIGNED_8);
135CREATE_VECTOR(I8, SIGNED_8);
136CREATE_VECTOR(U16, UNSIGNED_16);
137CREATE_VECTOR(I16, SIGNED_16);
138CREATE_VECTOR(U32, UNSIGNED_32);
139CREATE_VECTOR(I32, SIGNED_32);
140CREATE_VECTOR(U64, UNSIGNED_64);
141CREATE_VECTOR(I64, SIGNED_64);
142CREATE_VECTOR(F32, FLOAT_32);
143CREATE_VECTOR(F64, FLOAT_64);
144
145
146void Element::updateVisibleSubElements() {
147 if (!mElements.size()) {
148 return;
149 }
150 mVisibleElementMap.clear();
151
152 int noPaddingFieldCount = 0;
153 size_t fieldCount = mElementNames.size();
154 // Find out how many elements are not padding
155 for (size_t ct = 0; ct < fieldCount; ct ++) {
Tim Murrayab716362013-08-12 12:37:18 -0700156 if (mElementNames[ct].c_str()[0] != '#') {
Jason Sams221a4b12012-02-22 15:22:41 -0800157 noPaddingFieldCount ++;
158 }
159 }
160
161 // Make a map that points us at non-padding elements
162 for (size_t ct = 0; ct < fieldCount; ct ++) {
Tim Murrayab716362013-08-12 12:37:18 -0700163 if (mElementNames[ct].c_str()[0] != '#') {
Tim Murray89daad62013-07-29 14:30:02 -0700164 mVisibleElementMap.push_back((uint32_t)ct);
Jason Sams221a4b12012-02-22 15:22:41 -0800165 }
166 }
167}
168
Tim Murray89daad62013-07-29 14:30:02 -0700169Element::Element(void *id, android::RSC::sp<RS> rs,
170 std::vector<android::RSC::sp<Element> > &elements,
Tim Murrayab716362013-08-12 12:37:18 -0700171 std::vector<std::string> &elementNames,
Tim Murray89daad62013-07-29 14:30:02 -0700172 std::vector<uint32_t> &arraySizes) : BaseObj(id, rs) {
Jason Sams221a4b12012-02-22 15:22:41 -0800173 mSizeBytes = 0;
174 mVectorSize = 1;
175 mElements = elements;
176 mArraySizes = arraySizes;
177 mElementNames = elementNames;
178
179 mType = RS_TYPE_NONE;
180 mKind = RS_KIND_USER;
181
182 for (size_t ct = 0; ct < mElements.size(); ct++ ) {
Tim Murray89daad62013-07-29 14:30:02 -0700183 mOffsetInBytes.push_back(mSizeBytes);
Jason Sams221a4b12012-02-22 15:22:41 -0800184 mSizeBytes += mElements[ct]->mSizeBytes * mArraySizes[ct];
185 }
186 updateVisibleSubElements();
187}
188
189
190static uint32_t GetSizeInBytesForType(RsDataType dt) {
191 switch(dt) {
192 case RS_TYPE_NONE:
193 return 0;
194 case RS_TYPE_SIGNED_8:
195 case RS_TYPE_UNSIGNED_8:
196 case RS_TYPE_BOOLEAN:
197 return 1;
198
199 case RS_TYPE_FLOAT_16:
200 case RS_TYPE_SIGNED_16:
201 case RS_TYPE_UNSIGNED_16:
202 case RS_TYPE_UNSIGNED_5_6_5:
203 case RS_TYPE_UNSIGNED_5_5_5_1:
204 case RS_TYPE_UNSIGNED_4_4_4_4:
205 return 2;
206
207 case RS_TYPE_FLOAT_32:
208 case RS_TYPE_SIGNED_32:
209 case RS_TYPE_UNSIGNED_32:
210 return 4;
211
212 case RS_TYPE_FLOAT_64:
213 case RS_TYPE_SIGNED_64:
214 case RS_TYPE_UNSIGNED_64:
215 return 8;
216
217 case RS_TYPE_MATRIX_4X4:
218 return 16 * 4;
219 case RS_TYPE_MATRIX_3X3:
220 return 9 * 4;
221 case RS_TYPE_MATRIX_2X2:
222 return 4 * 4;
223
224 case RS_TYPE_TYPE:
225 case RS_TYPE_ALLOCATION:
226 case RS_TYPE_SAMPLER:
227 case RS_TYPE_SCRIPT:
228 case RS_TYPE_MESH:
229 case RS_TYPE_PROGRAM_FRAGMENT:
230 case RS_TYPE_PROGRAM_VERTEX:
231 case RS_TYPE_PROGRAM_RASTER:
232 case RS_TYPE_PROGRAM_STORE:
233 return 4;
234
235 default:
236 break;
237 }
238
239 ALOGE("Missing type %i", dt);
240 return 0;
241}
242
Tim Murray89daad62013-07-29 14:30:02 -0700243Element::Element(void *id, android::RSC::sp<RS> rs,
Jason Sams221a4b12012-02-22 15:22:41 -0800244 RsDataType dt, RsDataKind dk, bool norm, uint32_t size) :
245 BaseObj(id, rs)
246{
247 uint32_t tsize = GetSizeInBytesForType(dt);
248 if ((dt != RS_TYPE_UNSIGNED_5_6_5) &&
249 (dt != RS_TYPE_UNSIGNED_4_4_4_4) &&
250 (dt != RS_TYPE_UNSIGNED_5_5_5_1)) {
251 if (size == 3) {
252 mSizeBytes = tsize * 4;
253 } else {
254 mSizeBytes = tsize * size;
255 }
256 } else {
257 mSizeBytes = tsize;
258 }
259 mType = dt;
260 mKind = dk;
261 mNormalized = norm;
262 mVectorSize = size;
263}
264
265Element::~Element() {
266}
267
Jason Sams221a4b12012-02-22 15:22:41 -0800268void Element::updateFromNative() {
269 BaseObj::updateFromNative();
Jason Sams221a4b12012-02-22 15:22:41 -0800270 updateVisibleSubElements();
271}
272
Tim Murray89daad62013-07-29 14:30:02 -0700273android::RSC::sp<const Element> Element::createUser(android::RSC::sp<RS> rs, RsDataType dt) {
Tim Murraya4230962013-07-17 16:50:10 -0700274 void * id = RS::dispatch->ElementCreate(rs->getContext(), dt, RS_KIND_USER, false, 1);
Jason Sams221a4b12012-02-22 15:22:41 -0800275 return new Element(id, rs, dt, RS_KIND_USER, false, 1);
276}
277
Tim Murray89daad62013-07-29 14:30:02 -0700278android::RSC::sp<const Element> Element::createVector(android::RSC::sp<RS> rs, RsDataType dt, uint32_t size) {
Jason Sams221a4b12012-02-22 15:22:41 -0800279 if (size < 2 || size > 4) {
Tim Murray21fa7a02013-08-15 16:25:03 -0700280 rs->throwError(RS_ERROR_INVALID_PARAMETER, "Vector size out of range 2-4.");
281 return NULL;
Jason Sams221a4b12012-02-22 15:22:41 -0800282 }
Tim Murraya4230962013-07-17 16:50:10 -0700283 void *id = RS::dispatch->ElementCreate(rs->getContext(), dt, RS_KIND_USER, false, size);
Jason Sams221a4b12012-02-22 15:22:41 -0800284 return new Element(id, rs, dt, RS_KIND_USER, false, size);
285}
286
Tim Murray89daad62013-07-29 14:30:02 -0700287android::RSC::sp<const Element> Element::createPixel(android::RSC::sp<RS> rs, RsDataType dt, RsDataKind dk) {
Jason Sams221a4b12012-02-22 15:22:41 -0800288 if (!(dk == RS_KIND_PIXEL_L ||
289 dk == RS_KIND_PIXEL_A ||
290 dk == RS_KIND_PIXEL_LA ||
291 dk == RS_KIND_PIXEL_RGB ||
292 dk == RS_KIND_PIXEL_RGBA ||
293 dk == RS_KIND_PIXEL_DEPTH)) {
Tim Murray21fa7a02013-08-15 16:25:03 -0700294 rs->throwError(RS_ERROR_INVALID_PARAMETER, "Unsupported DataKind");
295 return NULL;
Jason Sams221a4b12012-02-22 15:22:41 -0800296 }
297 if (!(dt == RS_TYPE_UNSIGNED_8 ||
298 dt == RS_TYPE_UNSIGNED_16 ||
299 dt == RS_TYPE_UNSIGNED_5_6_5 ||
300 dt == RS_TYPE_UNSIGNED_4_4_4_4 ||
301 dt == RS_TYPE_UNSIGNED_5_5_5_1)) {
Tim Murray21fa7a02013-08-15 16:25:03 -0700302 rs->throwError(RS_ERROR_INVALID_PARAMETER, "Unsupported DataType");
303 return NULL;
Jason Sams221a4b12012-02-22 15:22:41 -0800304 }
305 if (dt == RS_TYPE_UNSIGNED_5_6_5 && dk != RS_KIND_PIXEL_RGB) {
Tim Murray21fa7a02013-08-15 16:25:03 -0700306 rs->throwError(RS_ERROR_INVALID_PARAMETER, "Bad kind and type combo");
307 return NULL;
Jason Sams221a4b12012-02-22 15:22:41 -0800308 }
309 if (dt == RS_TYPE_UNSIGNED_5_5_5_1 && dk != RS_KIND_PIXEL_RGBA) {
Tim Murray21fa7a02013-08-15 16:25:03 -0700310 rs->throwError(RS_ERROR_INVALID_PARAMETER, "Bad kind and type combo");
311 return NULL;
Jason Sams221a4b12012-02-22 15:22:41 -0800312 }
313 if (dt == RS_TYPE_UNSIGNED_4_4_4_4 && dk != RS_KIND_PIXEL_RGBA) {
Tim Murray21fa7a02013-08-15 16:25:03 -0700314 rs->throwError(RS_ERROR_INVALID_PARAMETER, "Bad kind and type combo");
315 return NULL;
Jason Sams221a4b12012-02-22 15:22:41 -0800316 }
317 if (dt == RS_TYPE_UNSIGNED_16 && dk != RS_KIND_PIXEL_DEPTH) {
Tim Murray21fa7a02013-08-15 16:25:03 -0700318 rs->throwError(RS_ERROR_INVALID_PARAMETER, "Bad kind and type combo");
319 return NULL;
Jason Sams221a4b12012-02-22 15:22:41 -0800320 }
321
322 int size = 1;
323 switch (dk) {
324 case RS_KIND_PIXEL_LA:
325 size = 2;
326 break;
327 case RS_KIND_PIXEL_RGB:
328 size = 3;
329 break;
330 case RS_KIND_PIXEL_RGBA:
331 size = 4;
332 break;
333 case RS_KIND_PIXEL_DEPTH:
334 size = 2;
335 break;
336 default:
337 break;
338 }
339
Tim Murraya4230962013-07-17 16:50:10 -0700340 void * id = RS::dispatch->ElementCreate(rs->getContext(), dt, dk, true, size);
Jason Sams221a4b12012-02-22 15:22:41 -0800341 return new Element(id, rs, dt, dk, true, size);
342}
343
Tim Murray10913a52013-08-20 17:19:47 -0700344bool Element::isCompatible(android::RSC::sp<const Element>e) const {
Jason Sams221a4b12012-02-22 15:22:41 -0800345 // Try strict BaseObj equality to start with.
Jason Sams69cccdf2012-04-02 19:11:49 -0700346 if (this == e.get()) {
Jason Sams221a4b12012-02-22 15:22:41 -0800347 return true;
348 }
349
350 // Ignore mKind because it is allowed to be different (user vs. pixel).
351 // We also ignore mNormalized because it can be different. The mType
352 // field must be non-null since we require name equivalence for
353 // user-created Elements.
354 return ((mSizeBytes == e->mSizeBytes) &&
Jason Sams69cccdf2012-04-02 19:11:49 -0700355 (mType != RS_TYPE_NONE) &&
Jason Sams221a4b12012-02-22 15:22:41 -0800356 (mType == e->mType) &&
357 (mVectorSize == e->mVectorSize));
358}
359
Tim Murray89daad62013-07-29 14:30:02 -0700360Element::Builder::Builder(android::RSC::sp<RS> rs) {
Jason Sams221a4b12012-02-22 15:22:41 -0800361 mRS = rs;
362 mSkipPadding = false;
363}
364
Tim Murrayab716362013-08-12 12:37:18 -0700365void Element::Builder::add(android::RSC::sp</*const*/ Element>e, std::string &name, uint32_t arraySize) {
Jason Sams221a4b12012-02-22 15:22:41 -0800366 // Skip padding fields after a vector 3 type.
367 if (mSkipPadding) {
368 const char *s1 = "#padding_";
Tim Murrayab716362013-08-12 12:37:18 -0700369 const char *s2 = name.c_str();
Jason Sams221a4b12012-02-22 15:22:41 -0800370 size_t len = strlen(s1);
371 if (strlen(s2) >= len) {
372 if (!memcmp(s1, s2, len)) {
373 mSkipPadding = false;
374 return;
375 }
376 }
377 }
378
379 if (e->mVectorSize == 3) {
380 mSkipPadding = true;
381 } else {
382 mSkipPadding = false;
383 }
384
Tim Murray89daad62013-07-29 14:30:02 -0700385 mElements.push_back(e);
386 mElementNames.push_back(name);
387 mArraySizes.push_back(arraySize);
Jason Sams221a4b12012-02-22 15:22:41 -0800388}
389
Tim Murray89daad62013-07-29 14:30:02 -0700390android::RSC::sp<const Element> Element::Builder::create() {
Jason Sams221a4b12012-02-22 15:22:41 -0800391 size_t fieldCount = mElements.size();
392 const char ** nameArray = (const char **)calloc(fieldCount, sizeof(char *));
Jason Sams69cccdf2012-04-02 19:11:49 -0700393 const Element ** elementArray = (const Element **)calloc(fieldCount, sizeof(Element *));
Jason Sams221a4b12012-02-22 15:22:41 -0800394 size_t* sizeArray = (size_t*)calloc(fieldCount, sizeof(size_t));
395
396 for (size_t ct = 0; ct < fieldCount; ct++) {
Tim Murrayab716362013-08-12 12:37:18 -0700397 nameArray[ct] = mElementNames[ct].c_str();
Jason Sams69cccdf2012-04-02 19:11:49 -0700398 elementArray[ct] = mElements[ct].get();
Jason Sams221a4b12012-02-22 15:22:41 -0800399 sizeArray[ct] = mElementNames[ct].length();
400 }
401
Tim Murraya4230962013-07-17 16:50:10 -0700402 void *id = RS::dispatch->ElementCreate2(mRS->getContext(),
Jason Sams69cccdf2012-04-02 19:11:49 -0700403 (RsElement *)elementArray, fieldCount,
Jason Sams221a4b12012-02-22 15:22:41 -0800404 nameArray, fieldCount * sizeof(size_t), sizeArray,
Tim Murray89daad62013-07-29 14:30:02 -0700405 (const uint32_t *)&mArraySizes[0], fieldCount);
Jason Sams221a4b12012-02-22 15:22:41 -0800406
407
408 free(nameArray);
409 free(sizeArray);
Jason Sams69cccdf2012-04-02 19:11:49 -0700410 free(elementArray);
411 return new Element(id, mRS, mElements, mElementNames, mArraySizes);
Jason Sams221a4b12012-02-22 15:22:41 -0800412}
413