blob: d193892fcf140c872872078f97893a2f73de0c4b [file] [log] [blame]
Jason Samsf70b0fc82012-02-22 15:22:41 -08001/*
2 * Copyright (C) 2008-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 <utils/Log.h>
18#include <malloc.h>
19#include <string.h>
20
21#include "RenderScript.h"
22#include "Element.h"
23
24
25const Element * Element::getSubElement(uint32_t index) {
26 if (!mVisibleElementMap.size()) {
Jason Sams170dc842012-02-23 17:14:39 -080027 mRS->throwError("Element contains no sub-elements");
Jason Samsf70b0fc82012-02-22 15:22:41 -080028 }
29 if (index >= mVisibleElementMap.size()) {
Jason Sams170dc842012-02-23 17:14:39 -080030 mRS->throwError("Illegal sub-element index");
Jason Samsf70b0fc82012-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 Sams170dc842012-02-23 17:14:39 -080037 mRS->throwError("Element contains no sub-elements");
Jason Samsf70b0fc82012-02-22 15:22:41 -080038 }
39 if (index >= mVisibleElementMap.size()) {
Jason Sams170dc842012-02-23 17:14:39 -080040 mRS->throwError("Illegal sub-element index");
Jason Samsf70b0fc82012-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 Sams170dc842012-02-23 17:14:39 -080047 mRS->throwError("Element contains no sub-elements");
Jason Samsf70b0fc82012-02-22 15:22:41 -080048 }
49 if (index >= mVisibleElementMap.size()) {
Jason Sams170dc842012-02-23 17:14:39 -080050 mRS->throwError("Illegal sub-element index");
Jason Samsf70b0fc82012-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 Sams170dc842012-02-23 17:14:39 -080057 mRS->throwError("Element contains no sub-elements");
Jason Samsf70b0fc82012-02-22 15:22:41 -080058 }
59 if (index >= mVisibleElementMap.size()) {
Jason Sams170dc842012-02-23 17:14:39 -080060 mRS->throwError("Illegal sub-element index");
Jason Samsf70b0fc82012-02-22 15:22:41 -080061 }
62 return mOffsetInBytes[mVisibleElementMap[index]];
63}
64
65
66#define CREATE_USER(N, T) const Element * Element::N(RenderScript *rs) { \
67 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
94#define CREATE_PIXEL(N, T, K) const Element * Element::N(RenderScript *rs) { \
95 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
103#define CREATE_VECTOR(N, T) const Element * Element::N##_2(RenderScript *rs) { \
104 return createVector(rs, RS_TYPE_##T, 2); \
105} \
106const Element * Element::N##_3(RenderScript *rs) { \
107 return createVector(rs, RS_TYPE_##T, 3); \
108} \
109const Element * Element::N##_4(RenderScript *rs) { \
110 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
147Element::Element(void *id, RenderScript *rs,
148 android::Vector<const Element *> &elements,
149 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
221Element::Element(void *id, RenderScript *rs,
222 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
246 /*
247 Element(int id, RenderScript rs) {
248 super(id, rs);
249 }
250 */
251
252void Element::updateFromNative() {
253 BaseObj::updateFromNative();
254/*
255 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
256 int[] dataBuffer = new int[5];
257 mRS.nElementGetNativeData(getID(), dataBuffer);
258
259 mNormalized = dataBuffer[2] == 1 ? true : false;
260 mVectorSize = dataBuffer[3];
261 mSize = 0;
262 for (DataType dt: DataType.values()) {
263 if(dt.mID == dataBuffer[0]){
264 mType = dt;
265 mSize = mType.mSize * mVectorSize;
266 }
267 }
268 for (DataKind dk: DataKind.values()) {
269 if(dk.mID == dataBuffer[1]){
270 mKind = dk;
271 }
272 }
273
274 int numSubElements = dataBuffer[4];
275 if(numSubElements > 0) {
276 mElements = new Element[numSubElements];
277 mElementNames = new String[numSubElements];
278 mArraySizes = new int[numSubElements];
279 mOffsetInBytes = new int[numSubElements];
280
281 int[] subElementIds = new int[numSubElements];
282 mRS.nElementGetSubElements(getID(), subElementIds, mElementNames, mArraySizes);
283 for(int i = 0; i < numSubElements; i ++) {
284 mElements[i] = new Element(subElementIds[i], mRS);
285 mElements[i].updateFromNative();
286 mOffsetInBytes[i] = mSize;
287 mSize += mElements[i].mSize * mArraySizes[i];
288 }
289 }
290 */
291 updateVisibleSubElements();
292}
293
294const Element * Element::createUser(RenderScript *rs, RsDataType dt) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800295 void * id = rsElementCreate(rs->mContext, dt, RS_KIND_USER, false, 1);
296 return new Element(id, rs, dt, RS_KIND_USER, false, 1);
297}
298
299const Element * Element::createVector(RenderScript *rs, RsDataType dt, uint32_t size) {
300 if (size < 2 || size > 4) {
Jason Sams170dc842012-02-23 17:14:39 -0800301 rs->throwError("Vector size out of range 2-4.");
Jason Samsf70b0fc82012-02-22 15:22:41 -0800302 }
303 void *id = rsElementCreate(rs->mContext, dt, RS_KIND_USER, false, size);
304 return new Element(id, rs, dt, RS_KIND_USER, false, size);
305}
306
307const Element * Element::createPixel(RenderScript *rs, RsDataType dt, RsDataKind dk) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800308 if (!(dk == RS_KIND_PIXEL_L ||
309 dk == RS_KIND_PIXEL_A ||
310 dk == RS_KIND_PIXEL_LA ||
311 dk == RS_KIND_PIXEL_RGB ||
312 dk == RS_KIND_PIXEL_RGBA ||
313 dk == RS_KIND_PIXEL_DEPTH)) {
Jason Sams170dc842012-02-23 17:14:39 -0800314 rs->throwError("Unsupported DataKind");
Jason Samsf70b0fc82012-02-22 15:22:41 -0800315 }
316 if (!(dt == RS_TYPE_UNSIGNED_8 ||
317 dt == RS_TYPE_UNSIGNED_16 ||
318 dt == RS_TYPE_UNSIGNED_5_6_5 ||
319 dt == RS_TYPE_UNSIGNED_4_4_4_4 ||
320 dt == RS_TYPE_UNSIGNED_5_5_5_1)) {
Jason Sams170dc842012-02-23 17:14:39 -0800321 rs->throwError("Unsupported DataType");
Jason Samsf70b0fc82012-02-22 15:22:41 -0800322 }
323 if (dt == RS_TYPE_UNSIGNED_5_6_5 && dk != RS_KIND_PIXEL_RGB) {
Jason Sams170dc842012-02-23 17:14:39 -0800324 rs->throwError("Bad kind and type combo");
Jason Samsf70b0fc82012-02-22 15:22:41 -0800325 }
326 if (dt == RS_TYPE_UNSIGNED_5_5_5_1 && dk != RS_KIND_PIXEL_RGBA) {
Jason Sams170dc842012-02-23 17:14:39 -0800327 rs->throwError("Bad kind and type combo");
Jason Samsf70b0fc82012-02-22 15:22:41 -0800328 }
329 if (dt == RS_TYPE_UNSIGNED_4_4_4_4 && dk != RS_KIND_PIXEL_RGBA) {
Jason Sams170dc842012-02-23 17:14:39 -0800330 rs->throwError("Bad kind and type combo");
Jason Samsf70b0fc82012-02-22 15:22:41 -0800331 }
332 if (dt == RS_TYPE_UNSIGNED_16 && dk != RS_KIND_PIXEL_DEPTH) {
Jason Sams170dc842012-02-23 17:14:39 -0800333 rs->throwError("Bad kind and type combo");
Jason Samsf70b0fc82012-02-22 15:22:41 -0800334 }
335
336 int size = 1;
337 switch (dk) {
338 case RS_KIND_PIXEL_LA:
339 size = 2;
340 break;
341 case RS_KIND_PIXEL_RGB:
342 size = 3;
343 break;
344 case RS_KIND_PIXEL_RGBA:
345 size = 4;
346 break;
347 case RS_KIND_PIXEL_DEPTH:
348 size = 2;
349 break;
350 default:
351 break;
352 }
353
354 void * id = rsElementCreate(rs->mContext, dt, dk, true, size);
355 return new Element(id, rs, dt, dk, true, size);
356}
357
358bool Element::isCompatible(const Element *e) {
359 // Try strict BaseObj equality to start with.
360 if (this == e) {
361 return true;
362 }
363
364 // Ignore mKind because it is allowed to be different (user vs. pixel).
365 // We also ignore mNormalized because it can be different. The mType
366 // field must be non-null since we require name equivalence for
367 // user-created Elements.
368 return ((mSizeBytes == e->mSizeBytes) &&
369 (mType != NULL) &&
370 (mType == e->mType) &&
371 (mVectorSize == e->mVectorSize));
372}
373
374Element::Builder::Builder(RenderScript *rs) {
375 mRS = rs;
376 mSkipPadding = false;
377}
378
379void Element::Builder::add(const Element *e, android::String8 &name, uint32_t arraySize) {
380 // Skip padding fields after a vector 3 type.
381 if (mSkipPadding) {
382 const char *s1 = "#padding_";
383 const char *s2 = name;
384 size_t len = strlen(s1);
385 if (strlen(s2) >= len) {
386 if (!memcmp(s1, s2, len)) {
387 mSkipPadding = false;
388 return;
389 }
390 }
391 }
392
393 if (e->mVectorSize == 3) {
394 mSkipPadding = true;
395 } else {
396 mSkipPadding = false;
397 }
398
399 mElements.add(e);
400 mElementNames.add(name);
401 mArraySizes.add(arraySize);
402}
403
404const Element * Element::Builder::create() {
405 size_t fieldCount = mElements.size();
406 const char ** nameArray = (const char **)calloc(fieldCount, sizeof(char *));
407 size_t* sizeArray = (size_t*)calloc(fieldCount, sizeof(size_t));
408
409 for (size_t ct = 0; ct < fieldCount; ct++) {
410 nameArray[ct] = mElementNames[ct].string();
411 sizeArray[ct] = mElementNames[ct].length();
412 }
413
414 void *id = rsElementCreate2(mRS->mContext,
415 (RsElement *)mElements.array(), fieldCount,
416 nameArray, fieldCount * sizeof(size_t), sizeArray,
417 (const uint32_t *)mArraySizes.array(), fieldCount);
418
419
420 free(nameArray);
421 free(sizeArray);
422
423 Element *e = new Element(id, mRS, mElements, mElementNames, mArraySizes);
424 return e;
425}
426