blob: 99b106906914f791461f3e0993632b4be8184a6e [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;
23using namespace renderscriptCpp;
Jason Sams221a4b12012-02-22 15:22:41 -080024
Jason Sams69cccdf2012-04-02 19:11:49 -070025sp<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 }
42 return mElementNames[mVisibleElementMap[index]];
43}
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 Murray84bf2b82012-10-31 16:03:16 -070066#define CREATE_USER(N, T) sp<const Element> Element::N(sp<RS> rs) { \
Jason Sams221a4b12012-02-22 15:22:41 -080067 return createUser(rs, RS_TYPE_##T); \
68}
69CREATE_USER(BOOLEAN, BOOLEAN);
70CREATE_USER(U8, UNSIGNED_8);
71CREATE_USER(I8, SIGNED_8);
72CREATE_USER(U16, UNSIGNED_16);
73CREATE_USER(I16, SIGNED_16);
74CREATE_USER(U32, UNSIGNED_32);
75CREATE_USER(I32, SIGNED_32);
76CREATE_USER(U64, UNSIGNED_64);
77CREATE_USER(I64, SIGNED_64);
78CREATE_USER(F32, FLOAT_32);
79CREATE_USER(F64, FLOAT_64);
80CREATE_USER(ELEMENT, ELEMENT);
81CREATE_USER(TYPE, TYPE);
82CREATE_USER(ALLOCATION, ALLOCATION);
83CREATE_USER(SAMPLER, SAMPLER);
84CREATE_USER(SCRIPT, SCRIPT);
85CREATE_USER(MESH, MESH);
86CREATE_USER(PROGRAM_FRAGMENT, PROGRAM_FRAGMENT);
87CREATE_USER(PROGRAM_VERTEX, PROGRAM_VERTEX);
88CREATE_USER(PROGRAM_RASTER, PROGRAM_RASTER);
89CREATE_USER(PROGRAM_STORE, PROGRAM_STORE);
90CREATE_USER(MATRIX_4X4, MATRIX_4X4);
91CREATE_USER(MATRIX_3X3, MATRIX_3X3);
92CREATE_USER(MATRIX_2X2, MATRIX_2X2);
93
Tim Murray84bf2b82012-10-31 16:03:16 -070094#define CREATE_PIXEL(N, T, K) sp<const Element> Element::N(sp<RS> rs) { \
Jason Sams221a4b12012-02-22 15:22:41 -080095 return createPixel(rs, RS_TYPE_##T, RS_KIND_##K); \
96}
97CREATE_PIXEL(A_8, UNSIGNED_8, PIXEL_A);
98CREATE_PIXEL(RGB_565, UNSIGNED_5_6_5, PIXEL_RGB);
99CREATE_PIXEL(RGB_888, UNSIGNED_8, PIXEL_RGB);
100CREATE_PIXEL(RGBA_4444, UNSIGNED_4_4_4_4, PIXEL_RGBA);
101CREATE_PIXEL(RGBA_8888, UNSIGNED_8, PIXEL_RGBA);
102
Tim Murray84bf2b82012-10-31 16:03:16 -0700103#define CREATE_VECTOR(N, T) sp<const Element> Element::N##_2(sp<RS> rs) { \
Jason Sams221a4b12012-02-22 15:22:41 -0800104 return createVector(rs, RS_TYPE_##T, 2); \
105} \
Tim Murray84bf2b82012-10-31 16:03:16 -0700106sp<const Element> Element::N##_3(sp<RS> rs) { \
Jason Sams221a4b12012-02-22 15:22:41 -0800107 return createVector(rs, RS_TYPE_##T, 3); \
108} \
Tim Murray84bf2b82012-10-31 16:03:16 -0700109sp<const Element> Element::N##_4(sp<RS> rs) { \
Jason Sams221a4b12012-02-22 15:22:41 -0800110 return createVector(rs, RS_TYPE_##T, 4); \
111}
112CREATE_VECTOR(U8, UNSIGNED_8);
113CREATE_VECTOR(I8, SIGNED_8);
114CREATE_VECTOR(U16, UNSIGNED_16);
115CREATE_VECTOR(I16, SIGNED_16);
116CREATE_VECTOR(U32, UNSIGNED_32);
117CREATE_VECTOR(I32, SIGNED_32);
118CREATE_VECTOR(U64, UNSIGNED_64);
119CREATE_VECTOR(I64, SIGNED_64);
120CREATE_VECTOR(F32, FLOAT_32);
121CREATE_VECTOR(F64, FLOAT_64);
122
123
124void Element::updateVisibleSubElements() {
125 if (!mElements.size()) {
126 return;
127 }
128 mVisibleElementMap.clear();
129
130 int noPaddingFieldCount = 0;
131 size_t fieldCount = mElementNames.size();
132 // Find out how many elements are not padding
133 for (size_t ct = 0; ct < fieldCount; ct ++) {
134 if (mElementNames[ct].string()[0] != '#') {
135 noPaddingFieldCount ++;
136 }
137 }
138
139 // Make a map that points us at non-padding elements
140 for (size_t ct = 0; ct < fieldCount; ct ++) {
141 if (mElementNames[ct].string()[0] != '#') {
142 mVisibleElementMap.push((uint32_t)ct);
143 }
144 }
145}
146
Tim Murray84bf2b82012-10-31 16:03:16 -0700147Element::Element(void *id, sp<RS> rs,
148 android::Vector<sp<Element> > &elements,
Jason Sams221a4b12012-02-22 15:22:41 -0800149 android::Vector<android::String8> &elementNames,
150 android::Vector<uint32_t> &arraySizes) : BaseObj(id, rs) {
151 mSizeBytes = 0;
152 mVectorSize = 1;
153 mElements = elements;
154 mArraySizes = arraySizes;
155 mElementNames = elementNames;
156
157 mType = RS_TYPE_NONE;
158 mKind = RS_KIND_USER;
159
160 for (size_t ct = 0; ct < mElements.size(); ct++ ) {
161 mOffsetInBytes.push(mSizeBytes);
162 mSizeBytes += mElements[ct]->mSizeBytes * mArraySizes[ct];
163 }
164 updateVisibleSubElements();
165}
166
167
168static uint32_t GetSizeInBytesForType(RsDataType dt) {
169 switch(dt) {
170 case RS_TYPE_NONE:
171 return 0;
172 case RS_TYPE_SIGNED_8:
173 case RS_TYPE_UNSIGNED_8:
174 case RS_TYPE_BOOLEAN:
175 return 1;
176
177 case RS_TYPE_FLOAT_16:
178 case RS_TYPE_SIGNED_16:
179 case RS_TYPE_UNSIGNED_16:
180 case RS_TYPE_UNSIGNED_5_6_5:
181 case RS_TYPE_UNSIGNED_5_5_5_1:
182 case RS_TYPE_UNSIGNED_4_4_4_4:
183 return 2;
184
185 case RS_TYPE_FLOAT_32:
186 case RS_TYPE_SIGNED_32:
187 case RS_TYPE_UNSIGNED_32:
188 return 4;
189
190 case RS_TYPE_FLOAT_64:
191 case RS_TYPE_SIGNED_64:
192 case RS_TYPE_UNSIGNED_64:
193 return 8;
194
195 case RS_TYPE_MATRIX_4X4:
196 return 16 * 4;
197 case RS_TYPE_MATRIX_3X3:
198 return 9 * 4;
199 case RS_TYPE_MATRIX_2X2:
200 return 4 * 4;
201
202 case RS_TYPE_TYPE:
203 case RS_TYPE_ALLOCATION:
204 case RS_TYPE_SAMPLER:
205 case RS_TYPE_SCRIPT:
206 case RS_TYPE_MESH:
207 case RS_TYPE_PROGRAM_FRAGMENT:
208 case RS_TYPE_PROGRAM_VERTEX:
209 case RS_TYPE_PROGRAM_RASTER:
210 case RS_TYPE_PROGRAM_STORE:
211 return 4;
212
213 default:
214 break;
215 }
216
217 ALOGE("Missing type %i", dt);
218 return 0;
219}
220
Tim Murray84bf2b82012-10-31 16:03:16 -0700221Element::Element(void *id, sp<RS> rs,
Jason Sams221a4b12012-02-22 15:22:41 -0800222 RsDataType dt, RsDataKind dk, bool norm, uint32_t size) :
223 BaseObj(id, rs)
224{
225 uint32_t tsize = GetSizeInBytesForType(dt);
226 if ((dt != RS_TYPE_UNSIGNED_5_6_5) &&
227 (dt != RS_TYPE_UNSIGNED_4_4_4_4) &&
228 (dt != RS_TYPE_UNSIGNED_5_5_5_1)) {
229 if (size == 3) {
230 mSizeBytes = tsize * 4;
231 } else {
232 mSizeBytes = tsize * size;
233 }
234 } else {
235 mSizeBytes = tsize;
236 }
237 mType = dt;
238 mKind = dk;
239 mNormalized = norm;
240 mVectorSize = size;
241}
242
243Element::~Element() {
244}
245
Jason Sams221a4b12012-02-22 15:22:41 -0800246void Element::updateFromNative() {
247 BaseObj::updateFromNative();
Jason Sams221a4b12012-02-22 15:22:41 -0800248 updateVisibleSubElements();
249}
250
Tim Murray84bf2b82012-10-31 16:03:16 -0700251sp<const Element> Element::createUser(sp<RS> rs, RsDataType dt) {
252 void * id = rsElementCreate(rs->getContext(), dt, RS_KIND_USER, false, 1);
Jason Sams221a4b12012-02-22 15:22:41 -0800253 return new Element(id, rs, dt, RS_KIND_USER, false, 1);
254}
255
Tim Murray84bf2b82012-10-31 16:03:16 -0700256sp<const Element> Element::createVector(sp<RS> rs, RsDataType dt, uint32_t size) {
Jason Sams221a4b12012-02-22 15:22:41 -0800257 if (size < 2 || size > 4) {
Jason Samsb2e3dc52012-02-23 17:14:39 -0800258 rs->throwError("Vector size out of range 2-4.");
Jason Sams221a4b12012-02-22 15:22:41 -0800259 }
Tim Murray84bf2b82012-10-31 16:03:16 -0700260 void *id = rsElementCreate(rs->getContext(), dt, RS_KIND_USER, false, size);
Jason Sams221a4b12012-02-22 15:22:41 -0800261 return new Element(id, rs, dt, RS_KIND_USER, false, size);
262}
263
Tim Murray84bf2b82012-10-31 16:03:16 -0700264sp<const Element> Element::createPixel(sp<RS> rs, RsDataType dt, RsDataKind dk) {
Jason Sams221a4b12012-02-22 15:22:41 -0800265 if (!(dk == RS_KIND_PIXEL_L ||
266 dk == RS_KIND_PIXEL_A ||
267 dk == RS_KIND_PIXEL_LA ||
268 dk == RS_KIND_PIXEL_RGB ||
269 dk == RS_KIND_PIXEL_RGBA ||
270 dk == RS_KIND_PIXEL_DEPTH)) {
Jason Samsb2e3dc52012-02-23 17:14:39 -0800271 rs->throwError("Unsupported DataKind");
Jason Sams221a4b12012-02-22 15:22:41 -0800272 }
273 if (!(dt == RS_TYPE_UNSIGNED_8 ||
274 dt == RS_TYPE_UNSIGNED_16 ||
275 dt == RS_TYPE_UNSIGNED_5_6_5 ||
276 dt == RS_TYPE_UNSIGNED_4_4_4_4 ||
277 dt == RS_TYPE_UNSIGNED_5_5_5_1)) {
Jason Samsb2e3dc52012-02-23 17:14:39 -0800278 rs->throwError("Unsupported DataType");
Jason Sams221a4b12012-02-22 15:22:41 -0800279 }
280 if (dt == RS_TYPE_UNSIGNED_5_6_5 && dk != RS_KIND_PIXEL_RGB) {
Jason Samsb2e3dc52012-02-23 17:14:39 -0800281 rs->throwError("Bad kind and type combo");
Jason Sams221a4b12012-02-22 15:22:41 -0800282 }
283 if (dt == RS_TYPE_UNSIGNED_5_5_5_1 && dk != RS_KIND_PIXEL_RGBA) {
Jason Samsb2e3dc52012-02-23 17:14:39 -0800284 rs->throwError("Bad kind and type combo");
Jason Sams221a4b12012-02-22 15:22:41 -0800285 }
286 if (dt == RS_TYPE_UNSIGNED_4_4_4_4 && dk != RS_KIND_PIXEL_RGBA) {
Jason Samsb2e3dc52012-02-23 17:14:39 -0800287 rs->throwError("Bad kind and type combo");
Jason Sams221a4b12012-02-22 15:22:41 -0800288 }
289 if (dt == RS_TYPE_UNSIGNED_16 && dk != RS_KIND_PIXEL_DEPTH) {
Jason Samsb2e3dc52012-02-23 17:14:39 -0800290 rs->throwError("Bad kind and type combo");
Jason Sams221a4b12012-02-22 15:22:41 -0800291 }
292
293 int size = 1;
294 switch (dk) {
295 case RS_KIND_PIXEL_LA:
296 size = 2;
297 break;
298 case RS_KIND_PIXEL_RGB:
299 size = 3;
300 break;
301 case RS_KIND_PIXEL_RGBA:
302 size = 4;
303 break;
304 case RS_KIND_PIXEL_DEPTH:
305 size = 2;
306 break;
307 default:
308 break;
309 }
310
Tim Murray84bf2b82012-10-31 16:03:16 -0700311 void * id = rsElementCreate(rs->getContext(), dt, dk, true, size);
Jason Sams221a4b12012-02-22 15:22:41 -0800312 return new Element(id, rs, dt, dk, true, size);
313}
314
Jason Sams69cccdf2012-04-02 19:11:49 -0700315bool Element::isCompatible(sp<const Element>e) {
Jason Sams221a4b12012-02-22 15:22:41 -0800316 // Try strict BaseObj equality to start with.
Jason Sams69cccdf2012-04-02 19:11:49 -0700317 if (this == e.get()) {
Jason Sams221a4b12012-02-22 15:22:41 -0800318 return true;
319 }
320
321 // Ignore mKind because it is allowed to be different (user vs. pixel).
322 // We also ignore mNormalized because it can be different. The mType
323 // field must be non-null since we require name equivalence for
324 // user-created Elements.
325 return ((mSizeBytes == e->mSizeBytes) &&
Jason Sams69cccdf2012-04-02 19:11:49 -0700326 (mType != RS_TYPE_NONE) &&
Jason Sams221a4b12012-02-22 15:22:41 -0800327 (mType == e->mType) &&
328 (mVectorSize == e->mVectorSize));
329}
330
Tim Murray84bf2b82012-10-31 16:03:16 -0700331Element::Builder::Builder(sp<RS> rs) {
Jason Sams221a4b12012-02-22 15:22:41 -0800332 mRS = rs;
333 mSkipPadding = false;
334}
335
Jason Sams69cccdf2012-04-02 19:11:49 -0700336void Element::Builder::add(sp</*const*/ Element>e, android::String8 &name, uint32_t arraySize) {
Jason Sams221a4b12012-02-22 15:22:41 -0800337 // Skip padding fields after a vector 3 type.
338 if (mSkipPadding) {
339 const char *s1 = "#padding_";
340 const char *s2 = name;
341 size_t len = strlen(s1);
342 if (strlen(s2) >= len) {
343 if (!memcmp(s1, s2, len)) {
344 mSkipPadding = false;
345 return;
346 }
347 }
348 }
349
350 if (e->mVectorSize == 3) {
351 mSkipPadding = true;
352 } else {
353 mSkipPadding = false;
354 }
355
356 mElements.add(e);
357 mElementNames.add(name);
358 mArraySizes.add(arraySize);
359}
360
Jason Sams69cccdf2012-04-02 19:11:49 -0700361sp<const Element> Element::Builder::create() {
Jason Sams221a4b12012-02-22 15:22:41 -0800362 size_t fieldCount = mElements.size();
363 const char ** nameArray = (const char **)calloc(fieldCount, sizeof(char *));
Jason Sams69cccdf2012-04-02 19:11:49 -0700364 const Element ** elementArray = (const Element **)calloc(fieldCount, sizeof(Element *));
Jason Sams221a4b12012-02-22 15:22:41 -0800365 size_t* sizeArray = (size_t*)calloc(fieldCount, sizeof(size_t));
366
367 for (size_t ct = 0; ct < fieldCount; ct++) {
368 nameArray[ct] = mElementNames[ct].string();
Jason Sams69cccdf2012-04-02 19:11:49 -0700369 elementArray[ct] = mElements[ct].get();
Jason Sams221a4b12012-02-22 15:22:41 -0800370 sizeArray[ct] = mElementNames[ct].length();
371 }
372
Tim Murray84bf2b82012-10-31 16:03:16 -0700373 void *id = rsElementCreate2(mRS->getContext(),
Jason Sams69cccdf2012-04-02 19:11:49 -0700374 (RsElement *)elementArray, fieldCount,
Jason Sams221a4b12012-02-22 15:22:41 -0800375 nameArray, fieldCount * sizeof(size_t), sizeArray,
376 (const uint32_t *)mArraySizes.array(), fieldCount);
377
378
379 free(nameArray);
380 free(sizeArray);
Jason Sams69cccdf2012-04-02 19:11:49 -0700381 free(elementArray);
382 return new Element(id, mRS, mElements, mElementNames, mArraySizes);
Jason Sams221a4b12012-02-22 15:22:41 -0800383}
384