blob: 35f081dbefbf2398a91e661d29d8400db4a35dbd [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"
Jason Sams221a4b12012-02-22 15:22:41 -080021
Jason Sams69cccdf2012-04-02 19:11:49 -070022using namespace android;
Tim Murray9eb7f4b2012-11-16 14:02:18 -080023using namespace RSC;
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) {
Jason Sams221a4b12012-02-22 15:22:41 -080026 if (!mVisibleElementMap.size()) {
Jason Samsb2e3dc52012-02-23 17:14:39 -080027 mRS->throwError("Element contains no sub-elements");
Jason Sams221a4b12012-02-22 15:22:41 -080028 }
29 if (index >= mVisibleElementMap.size()) {
Jason Samsb2e3dc52012-02-23 17:14:39 -080030 mRS->throwError("Illegal sub-element index");
Jason Sams221a4b12012-02-22 15:22:41 -080031 }
32 return mElements[mVisibleElementMap[index]];
33}
34
35const char * Element::getSubElementName(uint32_t index) {
36 if (!mVisibleElementMap.size()) {
Jason Samsb2e3dc52012-02-23 17:14:39 -080037 mRS->throwError("Element contains no sub-elements");
Jason Sams221a4b12012-02-22 15:22:41 -080038 }
39 if (index >= mVisibleElementMap.size()) {
Jason Samsb2e3dc52012-02-23 17:14:39 -080040 mRS->throwError("Illegal sub-element index");
Jason Sams221a4b12012-02-22 15:22:41 -080041 }
Tim Murrayab716362013-08-12 12:37:18 -070042 return mElementNames[mVisibleElementMap[index]].c_str();
Jason Sams221a4b12012-02-22 15:22:41 -080043}
44
45size_t Element::getSubElementArraySize(uint32_t index) {
46 if (!mVisibleElementMap.size()) {
Jason Samsb2e3dc52012-02-23 17:14:39 -080047 mRS->throwError("Element contains no sub-elements");
Jason Sams221a4b12012-02-22 15:22:41 -080048 }
49 if (index >= mVisibleElementMap.size()) {
Jason Samsb2e3dc52012-02-23 17:14:39 -080050 mRS->throwError("Illegal sub-element index");
Jason Sams221a4b12012-02-22 15:22:41 -080051 }
52 return mArraySizes[mVisibleElementMap[index]];
53}
54
55uint32_t Element::getSubElementOffsetBytes(uint32_t index) {
56 if (mVisibleElementMap.size()) {
Jason Samsb2e3dc52012-02-23 17:14:39 -080057 mRS->throwError("Element contains no sub-elements");
Jason Sams221a4b12012-02-22 15:22:41 -080058 }
59 if (index >= mVisibleElementMap.size()) {
Jason Samsb2e3dc52012-02-23 17:14:39 -080060 mRS->throwError("Illegal sub-element index");
Jason Sams221a4b12012-02-22 15:22:41 -080061 }
62 return mOffsetInBytes[mVisibleElementMap[index]];
63}
64
65
Tim Murray89daad62013-07-29 14:30:02 -070066#define CREATE_USER(N, T) android::RSC::sp<const Element> Element::N(android::RSC::sp<RS> rs) { \
Tim Murray729b6fe2013-07-23 16:20:42 -070067 if (rs->mElements.N == NULL) { \
68 rs->mElements.N = (createUser(rs, RS_TYPE_##T)).get(); \
69 } \
70 return rs->mElements.N; \
71 }
72
Jason Sams221a4b12012-02-22 15:22:41 -080073CREATE_USER(BOOLEAN, BOOLEAN);
74CREATE_USER(U8, UNSIGNED_8);
75CREATE_USER(I8, SIGNED_8);
76CREATE_USER(U16, UNSIGNED_16);
77CREATE_USER(I16, SIGNED_16);
78CREATE_USER(U32, UNSIGNED_32);
79CREATE_USER(I32, SIGNED_32);
80CREATE_USER(U64, UNSIGNED_64);
81CREATE_USER(I64, SIGNED_64);
82CREATE_USER(F32, FLOAT_32);
83CREATE_USER(F64, FLOAT_64);
84CREATE_USER(ELEMENT, ELEMENT);
85CREATE_USER(TYPE, TYPE);
86CREATE_USER(ALLOCATION, ALLOCATION);
87CREATE_USER(SAMPLER, SAMPLER);
88CREATE_USER(SCRIPT, SCRIPT);
89CREATE_USER(MESH, MESH);
90CREATE_USER(PROGRAM_FRAGMENT, PROGRAM_FRAGMENT);
91CREATE_USER(PROGRAM_VERTEX, PROGRAM_VERTEX);
92CREATE_USER(PROGRAM_RASTER, PROGRAM_RASTER);
93CREATE_USER(PROGRAM_STORE, PROGRAM_STORE);
94CREATE_USER(MATRIX_4X4, MATRIX_4X4);
95CREATE_USER(MATRIX_3X3, MATRIX_3X3);
96CREATE_USER(MATRIX_2X2, MATRIX_2X2);
97
Tim Murray89daad62013-07-29 14:30:02 -070098#define CREATE_PIXEL(N, T, K) android::RSC::sp<const Element> Element::N(android::RSC::sp<RS> rs) { \
Jason Sams221a4b12012-02-22 15:22:41 -080099 return createPixel(rs, RS_TYPE_##T, RS_KIND_##K); \
100}
101CREATE_PIXEL(A_8, UNSIGNED_8, PIXEL_A);
102CREATE_PIXEL(RGB_565, UNSIGNED_5_6_5, PIXEL_RGB);
103CREATE_PIXEL(RGB_888, UNSIGNED_8, PIXEL_RGB);
104CREATE_PIXEL(RGBA_4444, UNSIGNED_4_4_4_4, PIXEL_RGBA);
105CREATE_PIXEL(RGBA_8888, UNSIGNED_8, PIXEL_RGBA);
106
Tim Murray89daad62013-07-29 14:30:02 -0700107#define CREATE_VECTOR(N, T) android::RSC::sp<const Element> Element::N##_2(android::RSC::sp<RS> rs) { \
Jason Sams221a4b12012-02-22 15:22:41 -0800108 return createVector(rs, RS_TYPE_##T, 2); \
109} \
Tim Murray89daad62013-07-29 14:30:02 -0700110android::RSC::sp<const Element> Element::N##_3(android::RSC::sp<RS> rs) { \
Jason Sams221a4b12012-02-22 15:22:41 -0800111 return createVector(rs, RS_TYPE_##T, 3); \
112} \
Tim Murray89daad62013-07-29 14:30:02 -0700113android::RSC::sp<const Element> Element::N##_4(android::RSC::sp<RS> rs) { \
Jason Sams221a4b12012-02-22 15:22:41 -0800114 return createVector(rs, RS_TYPE_##T, 4); \
115}
116CREATE_VECTOR(U8, UNSIGNED_8);
117CREATE_VECTOR(I8, SIGNED_8);
118CREATE_VECTOR(U16, UNSIGNED_16);
119CREATE_VECTOR(I16, SIGNED_16);
120CREATE_VECTOR(U32, UNSIGNED_32);
121CREATE_VECTOR(I32, SIGNED_32);
122CREATE_VECTOR(U64, UNSIGNED_64);
123CREATE_VECTOR(I64, SIGNED_64);
124CREATE_VECTOR(F32, FLOAT_32);
125CREATE_VECTOR(F64, FLOAT_64);
126
127
128void Element::updateVisibleSubElements() {
129 if (!mElements.size()) {
130 return;
131 }
132 mVisibleElementMap.clear();
133
134 int noPaddingFieldCount = 0;
135 size_t fieldCount = mElementNames.size();
136 // Find out how many elements are not padding
137 for (size_t ct = 0; ct < fieldCount; ct ++) {
Tim Murrayab716362013-08-12 12:37:18 -0700138 if (mElementNames[ct].c_str()[0] != '#') {
Jason Sams221a4b12012-02-22 15:22:41 -0800139 noPaddingFieldCount ++;
140 }
141 }
142
143 // Make a map that points us at non-padding elements
144 for (size_t ct = 0; ct < fieldCount; ct ++) {
Tim Murrayab716362013-08-12 12:37:18 -0700145 if (mElementNames[ct].c_str()[0] != '#') {
Tim Murray89daad62013-07-29 14:30:02 -0700146 mVisibleElementMap.push_back((uint32_t)ct);
Jason Sams221a4b12012-02-22 15:22:41 -0800147 }
148 }
149}
150
Tim Murray89daad62013-07-29 14:30:02 -0700151Element::Element(void *id, android::RSC::sp<RS> rs,
152 std::vector<android::RSC::sp<Element> > &elements,
Tim Murrayab716362013-08-12 12:37:18 -0700153 std::vector<std::string> &elementNames,
Tim Murray89daad62013-07-29 14:30:02 -0700154 std::vector<uint32_t> &arraySizes) : BaseObj(id, rs) {
Jason Sams221a4b12012-02-22 15:22:41 -0800155 mSizeBytes = 0;
156 mVectorSize = 1;
157 mElements = elements;
158 mArraySizes = arraySizes;
159 mElementNames = elementNames;
160
161 mType = RS_TYPE_NONE;
162 mKind = RS_KIND_USER;
163
164 for (size_t ct = 0; ct < mElements.size(); ct++ ) {
Tim Murray89daad62013-07-29 14:30:02 -0700165 mOffsetInBytes.push_back(mSizeBytes);
Jason Sams221a4b12012-02-22 15:22:41 -0800166 mSizeBytes += mElements[ct]->mSizeBytes * mArraySizes[ct];
167 }
168 updateVisibleSubElements();
169}
170
171
172static uint32_t GetSizeInBytesForType(RsDataType dt) {
173 switch(dt) {
174 case RS_TYPE_NONE:
175 return 0;
176 case RS_TYPE_SIGNED_8:
177 case RS_TYPE_UNSIGNED_8:
178 case RS_TYPE_BOOLEAN:
179 return 1;
180
181 case RS_TYPE_FLOAT_16:
182 case RS_TYPE_SIGNED_16:
183 case RS_TYPE_UNSIGNED_16:
184 case RS_TYPE_UNSIGNED_5_6_5:
185 case RS_TYPE_UNSIGNED_5_5_5_1:
186 case RS_TYPE_UNSIGNED_4_4_4_4:
187 return 2;
188
189 case RS_TYPE_FLOAT_32:
190 case RS_TYPE_SIGNED_32:
191 case RS_TYPE_UNSIGNED_32:
192 return 4;
193
194 case RS_TYPE_FLOAT_64:
195 case RS_TYPE_SIGNED_64:
196 case RS_TYPE_UNSIGNED_64:
197 return 8;
198
199 case RS_TYPE_MATRIX_4X4:
200 return 16 * 4;
201 case RS_TYPE_MATRIX_3X3:
202 return 9 * 4;
203 case RS_TYPE_MATRIX_2X2:
204 return 4 * 4;
205
206 case RS_TYPE_TYPE:
207 case RS_TYPE_ALLOCATION:
208 case RS_TYPE_SAMPLER:
209 case RS_TYPE_SCRIPT:
210 case RS_TYPE_MESH:
211 case RS_TYPE_PROGRAM_FRAGMENT:
212 case RS_TYPE_PROGRAM_VERTEX:
213 case RS_TYPE_PROGRAM_RASTER:
214 case RS_TYPE_PROGRAM_STORE:
215 return 4;
216
217 default:
218 break;
219 }
220
221 ALOGE("Missing type %i", dt);
222 return 0;
223}
224
Tim Murray89daad62013-07-29 14:30:02 -0700225Element::Element(void *id, android::RSC::sp<RS> rs,
Jason Sams221a4b12012-02-22 15:22:41 -0800226 RsDataType dt, RsDataKind dk, bool norm, uint32_t size) :
227 BaseObj(id, rs)
228{
229 uint32_t tsize = GetSizeInBytesForType(dt);
230 if ((dt != RS_TYPE_UNSIGNED_5_6_5) &&
231 (dt != RS_TYPE_UNSIGNED_4_4_4_4) &&
232 (dt != RS_TYPE_UNSIGNED_5_5_5_1)) {
233 if (size == 3) {
234 mSizeBytes = tsize * 4;
235 } else {
236 mSizeBytes = tsize * size;
237 }
238 } else {
239 mSizeBytes = tsize;
240 }
241 mType = dt;
242 mKind = dk;
243 mNormalized = norm;
244 mVectorSize = size;
245}
246
247Element::~Element() {
248}
249
Jason Sams221a4b12012-02-22 15:22:41 -0800250void Element::updateFromNative() {
251 BaseObj::updateFromNative();
Jason Sams221a4b12012-02-22 15:22:41 -0800252 updateVisibleSubElements();
253}
254
Tim Murray89daad62013-07-29 14:30:02 -0700255android::RSC::sp<const Element> Element::createUser(android::RSC::sp<RS> rs, RsDataType dt) {
Tim Murraya4230962013-07-17 16:50:10 -0700256 void * id = RS::dispatch->ElementCreate(rs->getContext(), dt, RS_KIND_USER, false, 1);
Jason Sams221a4b12012-02-22 15:22:41 -0800257 return new Element(id, rs, dt, RS_KIND_USER, false, 1);
258}
259
Tim Murray89daad62013-07-29 14:30:02 -0700260android::RSC::sp<const Element> Element::createVector(android::RSC::sp<RS> rs, RsDataType dt, uint32_t size) {
Jason Sams221a4b12012-02-22 15:22:41 -0800261 if (size < 2 || size > 4) {
Jason Samsb2e3dc52012-02-23 17:14:39 -0800262 rs->throwError("Vector size out of range 2-4.");
Jason Sams221a4b12012-02-22 15:22:41 -0800263 }
Tim Murraya4230962013-07-17 16:50:10 -0700264 void *id = RS::dispatch->ElementCreate(rs->getContext(), dt, RS_KIND_USER, false, size);
Jason Sams221a4b12012-02-22 15:22:41 -0800265 return new Element(id, rs, dt, RS_KIND_USER, false, size);
266}
267
Tim Murray89daad62013-07-29 14:30:02 -0700268android::RSC::sp<const Element> Element::createPixel(android::RSC::sp<RS> rs, RsDataType dt, RsDataKind dk) {
Jason Sams221a4b12012-02-22 15:22:41 -0800269 if (!(dk == RS_KIND_PIXEL_L ||
270 dk == RS_KIND_PIXEL_A ||
271 dk == RS_KIND_PIXEL_LA ||
272 dk == RS_KIND_PIXEL_RGB ||
273 dk == RS_KIND_PIXEL_RGBA ||
274 dk == RS_KIND_PIXEL_DEPTH)) {
Jason Samsb2e3dc52012-02-23 17:14:39 -0800275 rs->throwError("Unsupported DataKind");
Jason Sams221a4b12012-02-22 15:22:41 -0800276 }
277 if (!(dt == RS_TYPE_UNSIGNED_8 ||
278 dt == RS_TYPE_UNSIGNED_16 ||
279 dt == RS_TYPE_UNSIGNED_5_6_5 ||
280 dt == RS_TYPE_UNSIGNED_4_4_4_4 ||
281 dt == RS_TYPE_UNSIGNED_5_5_5_1)) {
Jason Samsb2e3dc52012-02-23 17:14:39 -0800282 rs->throwError("Unsupported DataType");
Jason Sams221a4b12012-02-22 15:22:41 -0800283 }
284 if (dt == RS_TYPE_UNSIGNED_5_6_5 && dk != RS_KIND_PIXEL_RGB) {
Jason Samsb2e3dc52012-02-23 17:14:39 -0800285 rs->throwError("Bad kind and type combo");
Jason Sams221a4b12012-02-22 15:22:41 -0800286 }
287 if (dt == RS_TYPE_UNSIGNED_5_5_5_1 && dk != RS_KIND_PIXEL_RGBA) {
Jason Samsb2e3dc52012-02-23 17:14:39 -0800288 rs->throwError("Bad kind and type combo");
Jason Sams221a4b12012-02-22 15:22:41 -0800289 }
290 if (dt == RS_TYPE_UNSIGNED_4_4_4_4 && dk != RS_KIND_PIXEL_RGBA) {
Jason Samsb2e3dc52012-02-23 17:14:39 -0800291 rs->throwError("Bad kind and type combo");
Jason Sams221a4b12012-02-22 15:22:41 -0800292 }
293 if (dt == RS_TYPE_UNSIGNED_16 && dk != RS_KIND_PIXEL_DEPTH) {
Jason Samsb2e3dc52012-02-23 17:14:39 -0800294 rs->throwError("Bad kind and type combo");
Jason Sams221a4b12012-02-22 15:22:41 -0800295 }
296
297 int size = 1;
298 switch (dk) {
299 case RS_KIND_PIXEL_LA:
300 size = 2;
301 break;
302 case RS_KIND_PIXEL_RGB:
303 size = 3;
304 break;
305 case RS_KIND_PIXEL_RGBA:
306 size = 4;
307 break;
308 case RS_KIND_PIXEL_DEPTH:
309 size = 2;
310 break;
311 default:
312 break;
313 }
314
Tim Murraya4230962013-07-17 16:50:10 -0700315 void * id = RS::dispatch->ElementCreate(rs->getContext(), dt, dk, true, size);
Jason Sams221a4b12012-02-22 15:22:41 -0800316 return new Element(id, rs, dt, dk, true, size);
317}
318
Tim Murray89daad62013-07-29 14:30:02 -0700319bool Element::isCompatible(android::RSC::sp<const Element>e) {
Jason Sams221a4b12012-02-22 15:22:41 -0800320 // Try strict BaseObj equality to start with.
Jason Sams69cccdf2012-04-02 19:11:49 -0700321 if (this == e.get()) {
Jason Sams221a4b12012-02-22 15:22:41 -0800322 return true;
323 }
324
325 // Ignore mKind because it is allowed to be different (user vs. pixel).
326 // We also ignore mNormalized because it can be different. The mType
327 // field must be non-null since we require name equivalence for
328 // user-created Elements.
329 return ((mSizeBytes == e->mSizeBytes) &&
Jason Sams69cccdf2012-04-02 19:11:49 -0700330 (mType != RS_TYPE_NONE) &&
Jason Sams221a4b12012-02-22 15:22:41 -0800331 (mType == e->mType) &&
332 (mVectorSize == e->mVectorSize));
333}
334
Tim Murray89daad62013-07-29 14:30:02 -0700335Element::Builder::Builder(android::RSC::sp<RS> rs) {
Jason Sams221a4b12012-02-22 15:22:41 -0800336 mRS = rs;
337 mSkipPadding = false;
338}
339
Tim Murrayab716362013-08-12 12:37:18 -0700340void Element::Builder::add(android::RSC::sp</*const*/ Element>e, std::string &name, uint32_t arraySize) {
Jason Sams221a4b12012-02-22 15:22:41 -0800341 // Skip padding fields after a vector 3 type.
342 if (mSkipPadding) {
343 const char *s1 = "#padding_";
Tim Murrayab716362013-08-12 12:37:18 -0700344 const char *s2 = name.c_str();
Jason Sams221a4b12012-02-22 15:22:41 -0800345 size_t len = strlen(s1);
346 if (strlen(s2) >= len) {
347 if (!memcmp(s1, s2, len)) {
348 mSkipPadding = false;
349 return;
350 }
351 }
352 }
353
354 if (e->mVectorSize == 3) {
355 mSkipPadding = true;
356 } else {
357 mSkipPadding = false;
358 }
359
Tim Murray89daad62013-07-29 14:30:02 -0700360 mElements.push_back(e);
361 mElementNames.push_back(name);
362 mArraySizes.push_back(arraySize);
Jason Sams221a4b12012-02-22 15:22:41 -0800363}
364
Tim Murray89daad62013-07-29 14:30:02 -0700365android::RSC::sp<const Element> Element::Builder::create() {
Jason Sams221a4b12012-02-22 15:22:41 -0800366 size_t fieldCount = mElements.size();
367 const char ** nameArray = (const char **)calloc(fieldCount, sizeof(char *));
Jason Sams69cccdf2012-04-02 19:11:49 -0700368 const Element ** elementArray = (const Element **)calloc(fieldCount, sizeof(Element *));
Jason Sams221a4b12012-02-22 15:22:41 -0800369 size_t* sizeArray = (size_t*)calloc(fieldCount, sizeof(size_t));
370
371 for (size_t ct = 0; ct < fieldCount; ct++) {
Tim Murrayab716362013-08-12 12:37:18 -0700372 nameArray[ct] = mElementNames[ct].c_str();
Jason Sams69cccdf2012-04-02 19:11:49 -0700373 elementArray[ct] = mElements[ct].get();
Jason Sams221a4b12012-02-22 15:22:41 -0800374 sizeArray[ct] = mElementNames[ct].length();
375 }
376
Tim Murraya4230962013-07-17 16:50:10 -0700377 void *id = RS::dispatch->ElementCreate2(mRS->getContext(),
Jason Sams69cccdf2012-04-02 19:11:49 -0700378 (RsElement *)elementArray, fieldCount,
Jason Sams221a4b12012-02-22 15:22:41 -0800379 nameArray, fieldCount * sizeof(size_t), sizeArray,
Tim Murray89daad62013-07-29 14:30:02 -0700380 (const uint32_t *)&mArraySizes[0], fieldCount);
Jason Sams221a4b12012-02-22 15:22:41 -0800381
382
383 free(nameArray);
384 free(sizeArray);
Jason Sams69cccdf2012-04-02 19:11:49 -0700385 free(elementArray);
386 return new Element(id, mRS, mElements, mElementNames, mArraySizes);
Jason Sams221a4b12012-02-22 15:22:41 -0800387}
388