blob: 1289c6ec033598dfb3a016dd12d59817bcc3630f [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 Murrayb206ace2013-02-13 14:52:20 -080021#include <rs.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
Jason Sams69cccdf2012-04-02 19:11:49 -070026sp<const Element> Element::getSubElement(uint32_t index) {
Jason Sams221a4b12012-02-22 15:22:41 -080027 if (!mVisibleElementMap.size()) {
Jason Samsb2e3dc52012-02-23 17:14:39 -080028 mRS->throwError("Element contains no sub-elements");
Jason Sams221a4b12012-02-22 15:22:41 -080029 }
30 if (index >= mVisibleElementMap.size()) {
Jason Samsb2e3dc52012-02-23 17:14:39 -080031 mRS->throwError("Illegal sub-element index");
Jason Sams221a4b12012-02-22 15:22:41 -080032 }
33 return mElements[mVisibleElementMap[index]];
34}
35
36const char * Element::getSubElementName(uint32_t index) {
37 if (!mVisibleElementMap.size()) {
Jason Samsb2e3dc52012-02-23 17:14:39 -080038 mRS->throwError("Element contains no sub-elements");
Jason Sams221a4b12012-02-22 15:22:41 -080039 }
40 if (index >= mVisibleElementMap.size()) {
Jason Samsb2e3dc52012-02-23 17:14:39 -080041 mRS->throwError("Illegal sub-element index");
Jason Sams221a4b12012-02-22 15:22:41 -080042 }
Tim Murray0b575de2013-03-15 15:56:43 -070043 return mElementNames[mVisibleElementMap[index]].string();
Jason Sams221a4b12012-02-22 15:22:41 -080044}
45
46size_t Element::getSubElementArraySize(uint32_t index) {
47 if (!mVisibleElementMap.size()) {
Jason Samsb2e3dc52012-02-23 17:14:39 -080048 mRS->throwError("Element contains no sub-elements");
Jason Sams221a4b12012-02-22 15:22:41 -080049 }
50 if (index >= mVisibleElementMap.size()) {
Jason Samsb2e3dc52012-02-23 17:14:39 -080051 mRS->throwError("Illegal sub-element index");
Jason Sams221a4b12012-02-22 15:22:41 -080052 }
53 return mArraySizes[mVisibleElementMap[index]];
54}
55
56uint32_t Element::getSubElementOffsetBytes(uint32_t index) {
57 if (mVisibleElementMap.size()) {
Jason Samsb2e3dc52012-02-23 17:14:39 -080058 mRS->throwError("Element contains no sub-elements");
Jason Sams221a4b12012-02-22 15:22:41 -080059 }
60 if (index >= mVisibleElementMap.size()) {
Jason Samsb2e3dc52012-02-23 17:14:39 -080061 mRS->throwError("Illegal sub-element index");
Jason Sams221a4b12012-02-22 15:22:41 -080062 }
63 return mOffsetInBytes[mVisibleElementMap[index]];
64}
65
66
Tim Murray84bf2b82012-10-31 16:03:16 -070067#define CREATE_USER(N, T) sp<const Element> Element::N(sp<RS> rs) { \
Tim Murray729b6fe2013-07-23 16:20:42 -070068 if (rs->mElements.N == NULL) { \
69 rs->mElements.N = (createUser(rs, RS_TYPE_##T)).get(); \
70 } \
71 return rs->mElements.N; \
72 }
73
Jason Sams221a4b12012-02-22 15:22:41 -080074CREATE_USER(BOOLEAN, BOOLEAN);
75CREATE_USER(U8, UNSIGNED_8);
76CREATE_USER(I8, SIGNED_8);
77CREATE_USER(U16, UNSIGNED_16);
78CREATE_USER(I16, SIGNED_16);
79CREATE_USER(U32, UNSIGNED_32);
80CREATE_USER(I32, SIGNED_32);
81CREATE_USER(U64, UNSIGNED_64);
82CREATE_USER(I64, SIGNED_64);
83CREATE_USER(F32, FLOAT_32);
84CREATE_USER(F64, FLOAT_64);
85CREATE_USER(ELEMENT, ELEMENT);
86CREATE_USER(TYPE, TYPE);
87CREATE_USER(ALLOCATION, ALLOCATION);
88CREATE_USER(SAMPLER, SAMPLER);
89CREATE_USER(SCRIPT, SCRIPT);
90CREATE_USER(MESH, MESH);
91CREATE_USER(PROGRAM_FRAGMENT, PROGRAM_FRAGMENT);
92CREATE_USER(PROGRAM_VERTEX, PROGRAM_VERTEX);
93CREATE_USER(PROGRAM_RASTER, PROGRAM_RASTER);
94CREATE_USER(PROGRAM_STORE, PROGRAM_STORE);
95CREATE_USER(MATRIX_4X4, MATRIX_4X4);
96CREATE_USER(MATRIX_3X3, MATRIX_3X3);
97CREATE_USER(MATRIX_2X2, MATRIX_2X2);
98
Tim Murray84bf2b82012-10-31 16:03:16 -070099#define CREATE_PIXEL(N, T, K) sp<const Element> Element::N(sp<RS> rs) { \
Jason Sams221a4b12012-02-22 15:22:41 -0800100 return createPixel(rs, RS_TYPE_##T, RS_KIND_##K); \
101}
102CREATE_PIXEL(A_8, UNSIGNED_8, PIXEL_A);
103CREATE_PIXEL(RGB_565, UNSIGNED_5_6_5, PIXEL_RGB);
104CREATE_PIXEL(RGB_888, UNSIGNED_8, PIXEL_RGB);
105CREATE_PIXEL(RGBA_4444, UNSIGNED_4_4_4_4, PIXEL_RGBA);
106CREATE_PIXEL(RGBA_8888, UNSIGNED_8, PIXEL_RGBA);
107
Tim Murray84bf2b82012-10-31 16:03:16 -0700108#define CREATE_VECTOR(N, T) sp<const Element> Element::N##_2(sp<RS> rs) { \
Jason Sams221a4b12012-02-22 15:22:41 -0800109 return createVector(rs, RS_TYPE_##T, 2); \
110} \
Tim Murray84bf2b82012-10-31 16:03:16 -0700111sp<const Element> Element::N##_3(sp<RS> rs) { \
Jason Sams221a4b12012-02-22 15:22:41 -0800112 return createVector(rs, RS_TYPE_##T, 3); \
113} \
Tim Murray84bf2b82012-10-31 16:03:16 -0700114sp<const Element> Element::N##_4(sp<RS> rs) { \
Jason Sams221a4b12012-02-22 15:22:41 -0800115 return createVector(rs, RS_TYPE_##T, 4); \
116}
117CREATE_VECTOR(U8, UNSIGNED_8);
118CREATE_VECTOR(I8, SIGNED_8);
119CREATE_VECTOR(U16, UNSIGNED_16);
120CREATE_VECTOR(I16, SIGNED_16);
121CREATE_VECTOR(U32, UNSIGNED_32);
122CREATE_VECTOR(I32, SIGNED_32);
123CREATE_VECTOR(U64, UNSIGNED_64);
124CREATE_VECTOR(I64, SIGNED_64);
125CREATE_VECTOR(F32, FLOAT_32);
126CREATE_VECTOR(F64, FLOAT_64);
127
128
129void Element::updateVisibleSubElements() {
130 if (!mElements.size()) {
131 return;
132 }
133 mVisibleElementMap.clear();
134
135 int noPaddingFieldCount = 0;
136 size_t fieldCount = mElementNames.size();
137 // Find out how many elements are not padding
138 for (size_t ct = 0; ct < fieldCount; ct ++) {
139 if (mElementNames[ct].string()[0] != '#') {
140 noPaddingFieldCount ++;
141 }
142 }
143
144 // Make a map that points us at non-padding elements
145 for (size_t ct = 0; ct < fieldCount; ct ++) {
146 if (mElementNames[ct].string()[0] != '#') {
147 mVisibleElementMap.push((uint32_t)ct);
148 }
149 }
150}
151
Tim Murray84bf2b82012-10-31 16:03:16 -0700152Element::Element(void *id, sp<RS> rs,
153 android::Vector<sp<Element> > &elements,
Jason Sams221a4b12012-02-22 15:22:41 -0800154 android::Vector<android::String8> &elementNames,
155 android::Vector<uint32_t> &arraySizes) : BaseObj(id, rs) {
156 mSizeBytes = 0;
157 mVectorSize = 1;
158 mElements = elements;
159 mArraySizes = arraySizes;
160 mElementNames = elementNames;
161
162 mType = RS_TYPE_NONE;
163 mKind = RS_KIND_USER;
164
165 for (size_t ct = 0; ct < mElements.size(); ct++ ) {
166 mOffsetInBytes.push(mSizeBytes);
167 mSizeBytes += mElements[ct]->mSizeBytes * mArraySizes[ct];
168 }
169 updateVisibleSubElements();
170}
171
172
173static uint32_t GetSizeInBytesForType(RsDataType dt) {
174 switch(dt) {
175 case RS_TYPE_NONE:
176 return 0;
177 case RS_TYPE_SIGNED_8:
178 case RS_TYPE_UNSIGNED_8:
179 case RS_TYPE_BOOLEAN:
180 return 1;
181
182 case RS_TYPE_FLOAT_16:
183 case RS_TYPE_SIGNED_16:
184 case RS_TYPE_UNSIGNED_16:
185 case RS_TYPE_UNSIGNED_5_6_5:
186 case RS_TYPE_UNSIGNED_5_5_5_1:
187 case RS_TYPE_UNSIGNED_4_4_4_4:
188 return 2;
189
190 case RS_TYPE_FLOAT_32:
191 case RS_TYPE_SIGNED_32:
192 case RS_TYPE_UNSIGNED_32:
193 return 4;
194
195 case RS_TYPE_FLOAT_64:
196 case RS_TYPE_SIGNED_64:
197 case RS_TYPE_UNSIGNED_64:
198 return 8;
199
200 case RS_TYPE_MATRIX_4X4:
201 return 16 * 4;
202 case RS_TYPE_MATRIX_3X3:
203 return 9 * 4;
204 case RS_TYPE_MATRIX_2X2:
205 return 4 * 4;
206
207 case RS_TYPE_TYPE:
208 case RS_TYPE_ALLOCATION:
209 case RS_TYPE_SAMPLER:
210 case RS_TYPE_SCRIPT:
211 case RS_TYPE_MESH:
212 case RS_TYPE_PROGRAM_FRAGMENT:
213 case RS_TYPE_PROGRAM_VERTEX:
214 case RS_TYPE_PROGRAM_RASTER:
215 case RS_TYPE_PROGRAM_STORE:
216 return 4;
217
218 default:
219 break;
220 }
221
222 ALOGE("Missing type %i", dt);
223 return 0;
224}
225
Tim Murray84bf2b82012-10-31 16:03:16 -0700226Element::Element(void *id, sp<RS> rs,
Jason Sams221a4b12012-02-22 15:22:41 -0800227 RsDataType dt, RsDataKind dk, bool norm, uint32_t size) :
228 BaseObj(id, rs)
229{
230 uint32_t tsize = GetSizeInBytesForType(dt);
231 if ((dt != RS_TYPE_UNSIGNED_5_6_5) &&
232 (dt != RS_TYPE_UNSIGNED_4_4_4_4) &&
233 (dt != RS_TYPE_UNSIGNED_5_5_5_1)) {
234 if (size == 3) {
235 mSizeBytes = tsize * 4;
236 } else {
237 mSizeBytes = tsize * size;
238 }
239 } else {
240 mSizeBytes = tsize;
241 }
242 mType = dt;
243 mKind = dk;
244 mNormalized = norm;
245 mVectorSize = size;
246}
247
248Element::~Element() {
249}
250
Jason Sams221a4b12012-02-22 15:22:41 -0800251void Element::updateFromNative() {
252 BaseObj::updateFromNative();
Jason Sams221a4b12012-02-22 15:22:41 -0800253 updateVisibleSubElements();
254}
255
Tim Murray84bf2b82012-10-31 16:03:16 -0700256sp<const Element> Element::createUser(sp<RS> rs, RsDataType dt) {
Tim Murraya4230962013-07-17 16:50:10 -0700257 void * id = RS::dispatch->ElementCreate(rs->getContext(), dt, RS_KIND_USER, false, 1);
Jason Sams221a4b12012-02-22 15:22:41 -0800258 return new Element(id, rs, dt, RS_KIND_USER, false, 1);
259}
260
Tim Murray84bf2b82012-10-31 16:03:16 -0700261sp<const Element> Element::createVector(sp<RS> rs, RsDataType dt, uint32_t size) {
Jason Sams221a4b12012-02-22 15:22:41 -0800262 if (size < 2 || size > 4) {
Jason Samsb2e3dc52012-02-23 17:14:39 -0800263 rs->throwError("Vector size out of range 2-4.");
Jason Sams221a4b12012-02-22 15:22:41 -0800264 }
Tim Murraya4230962013-07-17 16:50:10 -0700265 void *id = RS::dispatch->ElementCreate(rs->getContext(), dt, RS_KIND_USER, false, size);
Jason Sams221a4b12012-02-22 15:22:41 -0800266 return new Element(id, rs, dt, RS_KIND_USER, false, size);
267}
268
Tim Murray84bf2b82012-10-31 16:03:16 -0700269sp<const Element> Element::createPixel(sp<RS> rs, RsDataType dt, RsDataKind dk) {
Jason Sams221a4b12012-02-22 15:22:41 -0800270 if (!(dk == RS_KIND_PIXEL_L ||
271 dk == RS_KIND_PIXEL_A ||
272 dk == RS_KIND_PIXEL_LA ||
273 dk == RS_KIND_PIXEL_RGB ||
274 dk == RS_KIND_PIXEL_RGBA ||
275 dk == RS_KIND_PIXEL_DEPTH)) {
Jason Samsb2e3dc52012-02-23 17:14:39 -0800276 rs->throwError("Unsupported DataKind");
Jason Sams221a4b12012-02-22 15:22:41 -0800277 }
278 if (!(dt == RS_TYPE_UNSIGNED_8 ||
279 dt == RS_TYPE_UNSIGNED_16 ||
280 dt == RS_TYPE_UNSIGNED_5_6_5 ||
281 dt == RS_TYPE_UNSIGNED_4_4_4_4 ||
282 dt == RS_TYPE_UNSIGNED_5_5_5_1)) {
Jason Samsb2e3dc52012-02-23 17:14:39 -0800283 rs->throwError("Unsupported DataType");
Jason Sams221a4b12012-02-22 15:22:41 -0800284 }
285 if (dt == RS_TYPE_UNSIGNED_5_6_5 && dk != RS_KIND_PIXEL_RGB) {
Jason Samsb2e3dc52012-02-23 17:14:39 -0800286 rs->throwError("Bad kind and type combo");
Jason Sams221a4b12012-02-22 15:22:41 -0800287 }
288 if (dt == RS_TYPE_UNSIGNED_5_5_5_1 && dk != RS_KIND_PIXEL_RGBA) {
Jason Samsb2e3dc52012-02-23 17:14:39 -0800289 rs->throwError("Bad kind and type combo");
Jason Sams221a4b12012-02-22 15:22:41 -0800290 }
291 if (dt == RS_TYPE_UNSIGNED_4_4_4_4 && dk != RS_KIND_PIXEL_RGBA) {
Jason Samsb2e3dc52012-02-23 17:14:39 -0800292 rs->throwError("Bad kind and type combo");
Jason Sams221a4b12012-02-22 15:22:41 -0800293 }
294 if (dt == RS_TYPE_UNSIGNED_16 && dk != RS_KIND_PIXEL_DEPTH) {
Jason Samsb2e3dc52012-02-23 17:14:39 -0800295 rs->throwError("Bad kind and type combo");
Jason Sams221a4b12012-02-22 15:22:41 -0800296 }
297
298 int size = 1;
299 switch (dk) {
300 case RS_KIND_PIXEL_LA:
301 size = 2;
302 break;
303 case RS_KIND_PIXEL_RGB:
304 size = 3;
305 break;
306 case RS_KIND_PIXEL_RGBA:
307 size = 4;
308 break;
309 case RS_KIND_PIXEL_DEPTH:
310 size = 2;
311 break;
312 default:
313 break;
314 }
315
Tim Murraya4230962013-07-17 16:50:10 -0700316 void * id = RS::dispatch->ElementCreate(rs->getContext(), dt, dk, true, size);
Jason Sams221a4b12012-02-22 15:22:41 -0800317 return new Element(id, rs, dt, dk, true, size);
318}
319
Jason Sams69cccdf2012-04-02 19:11:49 -0700320bool Element::isCompatible(sp<const Element>e) {
Jason Sams221a4b12012-02-22 15:22:41 -0800321 // Try strict BaseObj equality to start with.
Jason Sams69cccdf2012-04-02 19:11:49 -0700322 if (this == e.get()) {
Jason Sams221a4b12012-02-22 15:22:41 -0800323 return true;
324 }
325
326 // Ignore mKind because it is allowed to be different (user vs. pixel).
327 // We also ignore mNormalized because it can be different. The mType
328 // field must be non-null since we require name equivalence for
329 // user-created Elements.
330 return ((mSizeBytes == e->mSizeBytes) &&
Jason Sams69cccdf2012-04-02 19:11:49 -0700331 (mType != RS_TYPE_NONE) &&
Jason Sams221a4b12012-02-22 15:22:41 -0800332 (mType == e->mType) &&
333 (mVectorSize == e->mVectorSize));
334}
335
Tim Murray84bf2b82012-10-31 16:03:16 -0700336Element::Builder::Builder(sp<RS> rs) {
Jason Sams221a4b12012-02-22 15:22:41 -0800337 mRS = rs;
338 mSkipPadding = false;
339}
340
Jason Sams69cccdf2012-04-02 19:11:49 -0700341void Element::Builder::add(sp</*const*/ Element>e, android::String8 &name, uint32_t arraySize) {
Jason Sams221a4b12012-02-22 15:22:41 -0800342 // Skip padding fields after a vector 3 type.
343 if (mSkipPadding) {
344 const char *s1 = "#padding_";
Tim Murray0b575de2013-03-15 15:56:43 -0700345 const char *s2 = name.string();
Jason Sams221a4b12012-02-22 15:22:41 -0800346 size_t len = strlen(s1);
347 if (strlen(s2) >= len) {
348 if (!memcmp(s1, s2, len)) {
349 mSkipPadding = false;
350 return;
351 }
352 }
353 }
354
355 if (e->mVectorSize == 3) {
356 mSkipPadding = true;
357 } else {
358 mSkipPadding = false;
359 }
360
361 mElements.add(e);
362 mElementNames.add(name);
363 mArraySizes.add(arraySize);
364}
365
Jason Sams69cccdf2012-04-02 19:11:49 -0700366sp<const Element> Element::Builder::create() {
Jason Sams221a4b12012-02-22 15:22:41 -0800367 size_t fieldCount = mElements.size();
368 const char ** nameArray = (const char **)calloc(fieldCount, sizeof(char *));
Jason Sams69cccdf2012-04-02 19:11:49 -0700369 const Element ** elementArray = (const Element **)calloc(fieldCount, sizeof(Element *));
Jason Sams221a4b12012-02-22 15:22:41 -0800370 size_t* sizeArray = (size_t*)calloc(fieldCount, sizeof(size_t));
371
372 for (size_t ct = 0; ct < fieldCount; ct++) {
373 nameArray[ct] = mElementNames[ct].string();
Jason Sams69cccdf2012-04-02 19:11:49 -0700374 elementArray[ct] = mElements[ct].get();
Jason Sams221a4b12012-02-22 15:22:41 -0800375 sizeArray[ct] = mElementNames[ct].length();
376 }
377
Tim Murraya4230962013-07-17 16:50:10 -0700378 void *id = RS::dispatch->ElementCreate2(mRS->getContext(),
Jason Sams69cccdf2012-04-02 19:11:49 -0700379 (RsElement *)elementArray, fieldCount,
Jason Sams221a4b12012-02-22 15:22:41 -0800380 nameArray, fieldCount * sizeof(size_t), sizeArray,
381 (const uint32_t *)mArraySizes.array(), fieldCount);
382
383
384 free(nameArray);
385 free(sizeArray);
Jason Sams69cccdf2012-04-02 19:11:49 -0700386 free(elementArray);
387 return new Element(id, mRS, mElements, mElementNames, mArraySizes);
Jason Sams221a4b12012-02-22 15:22:41 -0800388}
389