blob: 8907cd2fb2632de4b2b91952614d4045f6e5e56c [file] [log] [blame]
Jason Sams36e612a2009-07-31 16:26:13 -07001/*
2 * Copyright (C) 2008 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
17package android.renderscript;
18
Jason Sams43ee06852009-08-12 17:54:11 -070019import java.lang.reflect.Field;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070020import android.util.Log;
Jason Sams36e612a2009-07-31 16:26:13 -070021
22/**
23 * @hide
24 *
25 **/
26public class Element extends BaseObj {
Jason Samsea84a7c2009-09-04 14:42:41 -070027 int mSize;
Jason Sams718cd1f2009-12-23 14:35:29 -080028 Element[] mElements;
29 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -070030 int[] mArraySizes;
Jason Sams36e612a2009-07-31 16:26:13 -070031
Jason Sams718cd1f2009-12-23 14:35:29 -080032 DataType mType;
33 DataKind mKind;
34 boolean mNormalized;
35 int mVectorSize;
Jason Sams768bc022009-09-21 19:41:04 -070036
Jason Sams718cd1f2009-12-23 14:35:29 -080037 int getSizeBytes() {return mSize;}
Jason Sams36e612a2009-07-31 16:26:13 -070038
39 public enum DataType {
Jason Sams718cd1f2009-12-23 14:35:29 -080040 //FLOAT_16 (1, 2),
41 FLOAT_32 (2, 4),
Stephen Hines02f417052010-09-30 15:19:22 -070042 FLOAT_64 (3, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -080043 SIGNED_8 (4, 1),
44 SIGNED_16 (5, 2),
45 SIGNED_32 (6, 4),
Stephen Hinesef1dac22010-10-01 15:39:33 -070046 SIGNED_64 (7, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -080047 UNSIGNED_8 (8, 1),
48 UNSIGNED_16 (9, 2),
49 UNSIGNED_32 (10, 4),
Stephen Hines52d83632010-10-11 16:10:42 -070050 UNSIGNED_64 (11, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -080051
Jason Samsf110d4b2010-06-21 17:42:41 -070052 BOOLEAN(12, 1),
Jason Sams718cd1f2009-12-23 14:35:29 -080053
Jason Samsf110d4b2010-06-21 17:42:41 -070054 UNSIGNED_5_6_5 (13, 2),
55 UNSIGNED_5_5_5_1 (14, 2),
56 UNSIGNED_4_4_4_4 (15, 2),
57
Jason Sams1d45c472010-08-25 14:31:48 -070058 MATRIX_4X4 (16, 64),
59 MATRIX_3X3 (17, 36),
60 MATRIX_2X2 (18, 16),
61
62 RS_ELEMENT (1000, 4),
63 RS_TYPE (1001, 4),
64 RS_ALLOCATION (1002, 4),
65 RS_SAMPLER (1003, 4),
66 RS_SCRIPT (1004, 4),
67 RS_MESH (1005, 4),
68 RS_PROGRAM_FRAGMENT (1006, 4),
69 RS_PROGRAM_VERTEX (1007, 4),
70 RS_PROGRAM_RASTER (1008, 4),
71 RS_PROGRAM_STORE (1009, 4);
Jason Sams36e612a2009-07-31 16:26:13 -070072
73 int mID;
Jason Sams718cd1f2009-12-23 14:35:29 -080074 int mSize;
75 DataType(int id, int size) {
Jason Sams36e612a2009-07-31 16:26:13 -070076 mID = id;
Jason Sams718cd1f2009-12-23 14:35:29 -080077 mSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -070078 }
79 }
80
81 public enum DataKind {
82 USER (0),
Jason Sams718cd1f2009-12-23 14:35:29 -080083
84 PIXEL_L (7),
85 PIXEL_A (8),
86 PIXEL_LA (9),
87 PIXEL_RGB (10),
88 PIXEL_RGBA (11);
Jason Sams36e612a2009-07-31 16:26:13 -070089
90 int mID;
91 DataKind(int id) {
92 mID = id;
93 }
94 }
95
Jason Samsc1d62102010-11-04 14:32:19 -070096 public boolean isComplex() {
97 if (mElements == null) {
98 return false;
99 }
100 for (int ct=0; ct < mElements.length; ct++) {
101 if (mElements[ct].mElements != null) {
102 return true;
103 }
104 }
105 return false;
106 }
107
Jason Samsf110d4b2010-06-21 17:42:41 -0700108 public static Element BOOLEAN(RenderScript rs) {
109 if(rs.mElement_BOOLEAN == null) {
110 rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN);
111 }
112 return rs.mElement_BOOLEAN;
113 }
114
Jason Sams8cb39de2010-06-01 15:47:01 -0700115 public static Element U8(RenderScript rs) {
116 if(rs.mElement_U8 == null) {
117 rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800118 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700119 return rs.mElement_U8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800120 }
121
Jason Sams8cb39de2010-06-01 15:47:01 -0700122 public static Element I8(RenderScript rs) {
123 if(rs.mElement_I8 == null) {
124 rs.mElement_I8 = createUser(rs, DataType.SIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800125 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700126 return rs.mElement_I8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800127 }
128
Jason Samse29f3e72010-06-08 15:40:48 -0700129 public static Element U16(RenderScript rs) {
130 if(rs.mElement_U16 == null) {
131 rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16);
132 }
133 return rs.mElement_U16;
134 }
135
136 public static Element I16(RenderScript rs) {
137 if(rs.mElement_I16 == null) {
138 rs.mElement_I16 = createUser(rs, DataType.SIGNED_16);
139 }
140 return rs.mElement_I16;
141 }
142
Jason Sams8cb39de2010-06-01 15:47:01 -0700143 public static Element U32(RenderScript rs) {
144 if(rs.mElement_U32 == null) {
145 rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800146 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700147 return rs.mElement_U32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800148 }
149
Jason Sams8cb39de2010-06-01 15:47:01 -0700150 public static Element I32(RenderScript rs) {
151 if(rs.mElement_I32 == null) {
152 rs.mElement_I32 = createUser(rs, DataType.SIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800153 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700154 return rs.mElement_I32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800155 }
156
Stephen Hines52d83632010-10-11 16:10:42 -0700157 public static Element U64(RenderScript rs) {
158 if(rs.mElement_U64 == null) {
159 rs.mElement_U64 = createUser(rs, DataType.UNSIGNED_64);
160 }
161 return rs.mElement_U64;
162 }
163
Stephen Hinesef1dac22010-10-01 15:39:33 -0700164 public static Element I64(RenderScript rs) {
165 if(rs.mElement_I64 == null) {
166 rs.mElement_I64 = createUser(rs, DataType.SIGNED_64);
167 }
168 return rs.mElement_I64;
169 }
170
Jason Sams8cb39de2010-06-01 15:47:01 -0700171 public static Element F32(RenderScript rs) {
172 if(rs.mElement_F32 == null) {
173 rs.mElement_F32 = createUser(rs, DataType.FLOAT_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800174 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700175 return rs.mElement_F32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800176 }
177
Stephen Hines02f417052010-09-30 15:19:22 -0700178 public static Element F64(RenderScript rs) {
179 if(rs.mElement_F64 == null) {
180 rs.mElement_F64 = createUser(rs, DataType.FLOAT_64);
181 }
182 return rs.mElement_F64;
183 }
184
Jason Sams8cb39de2010-06-01 15:47:01 -0700185 public static Element ELEMENT(RenderScript rs) {
186 if(rs.mElement_ELEMENT == null) {
187 rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700188 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700189 return rs.mElement_ELEMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700190 }
191
Jason Sams8cb39de2010-06-01 15:47:01 -0700192 public static Element TYPE(RenderScript rs) {
193 if(rs.mElement_TYPE == null) {
194 rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE);
Jason Samsa70f4162010-03-26 15:33:42 -0700195 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700196 return rs.mElement_TYPE;
Jason Samsa70f4162010-03-26 15:33:42 -0700197 }
198
Jason Sams8cb39de2010-06-01 15:47:01 -0700199 public static Element ALLOCATION(RenderScript rs) {
200 if(rs.mElement_ALLOCATION == null) {
201 rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION);
Jason Samsa70f4162010-03-26 15:33:42 -0700202 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700203 return rs.mElement_ALLOCATION;
Jason Samsa70f4162010-03-26 15:33:42 -0700204 }
205
Jason Sams8cb39de2010-06-01 15:47:01 -0700206 public static Element SAMPLER(RenderScript rs) {
207 if(rs.mElement_SAMPLER == null) {
208 rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER);
Jason Samsa70f4162010-03-26 15:33:42 -0700209 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700210 return rs.mElement_SAMPLER;
Jason Samsa70f4162010-03-26 15:33:42 -0700211 }
212
Jason Sams8cb39de2010-06-01 15:47:01 -0700213 public static Element SCRIPT(RenderScript rs) {
214 if(rs.mElement_SCRIPT == null) {
215 rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT);
Jason Samsa70f4162010-03-26 15:33:42 -0700216 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700217 return rs.mElement_SCRIPT;
Jason Samsa70f4162010-03-26 15:33:42 -0700218 }
219
Jason Sams8cb39de2010-06-01 15:47:01 -0700220 public static Element MESH(RenderScript rs) {
221 if(rs.mElement_MESH == null) {
222 rs.mElement_MESH = createUser(rs, DataType.RS_MESH);
Jason Samsa70f4162010-03-26 15:33:42 -0700223 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700224 return rs.mElement_MESH;
Jason Samsa70f4162010-03-26 15:33:42 -0700225 }
226
Jason Sams8cb39de2010-06-01 15:47:01 -0700227 public static Element PROGRAM_FRAGMENT(RenderScript rs) {
228 if(rs.mElement_PROGRAM_FRAGMENT == null) {
229 rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700230 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700231 return rs.mElement_PROGRAM_FRAGMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700232 }
233
Jason Sams8cb39de2010-06-01 15:47:01 -0700234 public static Element PROGRAM_VERTEX(RenderScript rs) {
235 if(rs.mElement_PROGRAM_VERTEX == null) {
236 rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX);
Jason Samsa70f4162010-03-26 15:33:42 -0700237 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700238 return rs.mElement_PROGRAM_VERTEX;
Jason Samsa70f4162010-03-26 15:33:42 -0700239 }
240
Jason Sams8cb39de2010-06-01 15:47:01 -0700241 public static Element PROGRAM_RASTER(RenderScript rs) {
242 if(rs.mElement_PROGRAM_RASTER == null) {
243 rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER);
Jason Samsa70f4162010-03-26 15:33:42 -0700244 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700245 return rs.mElement_PROGRAM_RASTER;
Jason Samsa70f4162010-03-26 15:33:42 -0700246 }
247
Jason Sams8cb39de2010-06-01 15:47:01 -0700248 public static Element PROGRAM_STORE(RenderScript rs) {
249 if(rs.mElement_PROGRAM_STORE == null) {
250 rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE);
Jason Samsa70f4162010-03-26 15:33:42 -0700251 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700252 return rs.mElement_PROGRAM_STORE;
Jason Samsa70f4162010-03-26 15:33:42 -0700253 }
254
255
Jason Sams718cd1f2009-12-23 14:35:29 -0800256 public static Element A_8(RenderScript rs) {
257 if(rs.mElement_A_8 == null) {
258 rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
259 }
260 return rs.mElement_A_8;
261 }
262
263 public static Element RGB_565(RenderScript rs) {
264 if(rs.mElement_RGB_565 == null) {
265 rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
266 }
267 return rs.mElement_RGB_565;
268 }
269
270 public static Element RGB_888(RenderScript rs) {
271 if(rs.mElement_RGB_888 == null) {
272 rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
273 }
274 return rs.mElement_RGB_888;
275 }
276
277 public static Element RGBA_5551(RenderScript rs) {
278 if(rs.mElement_RGBA_5551 == null) {
279 rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
280 }
281 return rs.mElement_RGBA_5551;
282 }
283
284 public static Element RGBA_4444(RenderScript rs) {
285 if(rs.mElement_RGBA_4444 == null) {
286 rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
287 }
288 return rs.mElement_RGBA_4444;
289 }
290
291 public static Element RGBA_8888(RenderScript rs) {
292 if(rs.mElement_RGBA_8888 == null) {
293 rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
294 }
295 return rs.mElement_RGBA_8888;
296 }
297
Jason Sams8cb39de2010-06-01 15:47:01 -0700298 public static Element F32_2(RenderScript rs) {
299 if(rs.mElement_FLOAT_2 == null) {
300 rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2);
Jason Sams718cd1f2009-12-23 14:35:29 -0800301 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700302 return rs.mElement_FLOAT_2;
Jason Sams718cd1f2009-12-23 14:35:29 -0800303 }
304
Jason Sams8cb39de2010-06-01 15:47:01 -0700305 public static Element F32_3(RenderScript rs) {
306 if(rs.mElement_FLOAT_3 == null) {
307 rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3);
Jason Sams718cd1f2009-12-23 14:35:29 -0800308 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700309 return rs.mElement_FLOAT_3;
Jason Sams718cd1f2009-12-23 14:35:29 -0800310 }
311
Jason Sams8cb39de2010-06-01 15:47:01 -0700312 public static Element F32_4(RenderScript rs) {
313 if(rs.mElement_FLOAT_4 == null) {
314 rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800315 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700316 return rs.mElement_FLOAT_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800317 }
318
Jason Sams8cb39de2010-06-01 15:47:01 -0700319 public static Element U8_4(RenderScript rs) {
320 if(rs.mElement_UCHAR_4 == null) {
321 rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800322 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700323 return rs.mElement_UCHAR_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800324 }
325
Jason Sams1d45c472010-08-25 14:31:48 -0700326 public static Element MATRIX_4X4(RenderScript rs) {
327 if(rs.mElement_MATRIX_4X4 == null) {
328 rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4);
329 }
330 return rs.mElement_MATRIX_4X4;
331 }
332 public static Element MATRIX4X4(RenderScript rs) {
333 return MATRIX_4X4(rs);
334 }
335
336 public static Element MATRIX_3X3(RenderScript rs) {
337 if(rs.mElement_MATRIX_3X3 == null) {
338 rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3);
339 }
340 return rs.mElement_MATRIX_4X4;
341 }
342
343 public static Element MATRIX_2X2(RenderScript rs) {
344 if(rs.mElement_MATRIX_2X2 == null) {
345 rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2);
346 }
347 return rs.mElement_MATRIX_2X2;
348 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800349
Jason Sams70d4e502010-09-02 17:35:23 -0700350 Element(int id, RenderScript rs, Element[] e, String[] n, int[] as) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700351 super(id, rs);
Jason Samsea84a7c2009-09-04 14:42:41 -0700352 mSize = 0;
Jason Sams718cd1f2009-12-23 14:35:29 -0800353 mElements = e;
354 mElementNames = n;
Jason Sams70d4e502010-09-02 17:35:23 -0700355 mArraySizes = as;
Jason Sams718cd1f2009-12-23 14:35:29 -0800356 for (int ct = 0; ct < mElements.length; ct++ ) {
Alex Sakhartchouk9e401bc2010-10-13 14:22:02 -0700357 mSize += mElements[ct].mSize * mArraySizes[ct];
Jason Sams718cd1f2009-12-23 14:35:29 -0800358 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800359 }
360
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700361 Element(int id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
362 super(id, rs);
Jason Sams718cd1f2009-12-23 14:35:29 -0800363 mSize = dt.mSize * size;
364 mType = dt;
365 mKind = dk;
366 mNormalized = norm;
367 mVectorSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700368 }
369
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700370 Element(int id, RenderScript rs) {
371 super(id, rs);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700372 }
373
374 @Override
375 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800376 super.updateFromNative();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700377
378 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
379 int[] dataBuffer = new int[5];
Jason Sams06d69de2010-11-09 17:11:40 -0800380 mRS.nElementGetNativeData(getID(), dataBuffer);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700381
382 mNormalized = dataBuffer[2] == 1 ? true : false;
383 mVectorSize = dataBuffer[3];
384 mSize = 0;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700385 for (DataType dt: DataType.values()) {
386 if(dt.mID == dataBuffer[0]){
387 mType = dt;
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700388 mSize = mType.mSize * mVectorSize;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700389 }
390 }
391 for (DataKind dk: DataKind.values()) {
392 if(dk.mID == dataBuffer[1]){
393 mKind = dk;
394 }
395 }
396
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700397 int numSubElements = dataBuffer[4];
398 if(numSubElements > 0) {
399 mElements = new Element[numSubElements];
400 mElementNames = new String[numSubElements];
401
402 int[] subElementIds = new int[numSubElements];
Jason Sams06d69de2010-11-09 17:11:40 -0800403 mRS.nElementGetSubElements(getID(), subElementIds, mElementNames);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700404 for(int i = 0; i < numSubElements; i ++) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700405 mElements[i] = new Element(subElementIds[i], mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700406 mElements[i].updateFromNative();
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700407 mSize += mElements[i].mSize;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700408 }
409 }
410
411 }
412
Jason Samsc1d62102010-11-04 14:32:19 -0700413 public void destroy() {
Jason Sams7ce033d2009-08-18 14:14:24 -0700414 super.destroy();
Jason Sams36e612a2009-07-31 16:26:13 -0700415 }
416
Jason Sams718cd1f2009-12-23 14:35:29 -0800417 /////////////////////////////////////////
418 public static Element createUser(RenderScript rs, DataType dt) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700419 DataKind dk = DataKind.USER;
420 boolean norm = false;
421 int vecSize = 1;
422 int id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize);
423 return new Element(id, rs, dt, dk, norm, vecSize);
Jason Sams718cd1f2009-12-23 14:35:29 -0800424 }
425
426 public static Element createVector(RenderScript rs, DataType dt, int size) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800427 if (size < 2 || size > 4) {
Jason Samsc1d62102010-11-04 14:32:19 -0700428 throw new RSIllegalArgumentException("Vector size out of rance 2-4.");
Jason Samsea84a7c2009-09-04 14:42:41 -0700429 }
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700430 DataKind dk = DataKind.USER;
431 boolean norm = false;
432 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
433 return new Element(id, rs, dt, dk, norm, size);
Jason Samsea84a7c2009-09-04 14:42:41 -0700434 }
435
Jason Sams718cd1f2009-12-23 14:35:29 -0800436 public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800437 if (!(dk == DataKind.PIXEL_L ||
438 dk == DataKind.PIXEL_A ||
439 dk == DataKind.PIXEL_LA ||
440 dk == DataKind.PIXEL_RGB ||
441 dk == DataKind.PIXEL_RGBA)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700442 throw new RSIllegalArgumentException("Unsupported DataKind");
Jason Sams718cd1f2009-12-23 14:35:29 -0800443 }
444 if (!(dt == DataType.UNSIGNED_8 ||
445 dt == DataType.UNSIGNED_5_6_5 ||
446 dt == DataType.UNSIGNED_4_4_4_4 ||
447 dt == DataType.UNSIGNED_5_5_5_1)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700448 throw new RSIllegalArgumentException("Unsupported DataType");
Jason Sams718cd1f2009-12-23 14:35:29 -0800449 }
450 if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
Jason Samsc1d62102010-11-04 14:32:19 -0700451 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800452 }
453 if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700454 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800455 }
456 if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700457 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800458 }
459
460 int size = 1;
461 if (dk == DataKind.PIXEL_LA) {
462 size = 2;
463 }
464 if (dk == DataKind.PIXEL_RGB) {
465 size = 3;
466 }
467 if (dk == DataKind.PIXEL_RGBA) {
468 size = 4;
469 }
470
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700471 boolean norm = true;
472 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
473 return new Element(id, rs, dt, dk, norm, size);
Jason Sams718cd1f2009-12-23 14:35:29 -0800474 }
Jason Sams36e612a2009-07-31 16:26:13 -0700475
476 public static class Builder {
477 RenderScript mRS;
Jason Sams718cd1f2009-12-23 14:35:29 -0800478 Element[] mElements;
479 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -0700480 int[] mArraySizes;
Jason Sams718cd1f2009-12-23 14:35:29 -0800481 int mCount;
Jason Sams22534172009-08-04 16:58:20 -0700482
483 public Builder(RenderScript rs) {
Jason Sams36e612a2009-07-31 16:26:13 -0700484 mRS = rs;
Jason Sams718cd1f2009-12-23 14:35:29 -0800485 mCount = 0;
486 mElements = new Element[8];
487 mElementNames = new String[8];
Jason Sams70d4e502010-09-02 17:35:23 -0700488 mArraySizes = new int[8];
Jason Sams36e612a2009-07-31 16:26:13 -0700489 }
490
Jason Sams70d4e502010-09-02 17:35:23 -0700491 public void add(Element element, String name, int arraySize) {
492 if (arraySize < 1) {
Jason Samsc1d62102010-11-04 14:32:19 -0700493 throw new RSIllegalArgumentException("Array size cannot be less than 1.");
Jason Sams70d4e502010-09-02 17:35:23 -0700494 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800495 if(mCount == mElements.length) {
496 Element[] e = new Element[mCount + 8];
497 String[] s = new String[mCount + 8];
Jason Sams70d4e502010-09-02 17:35:23 -0700498 int[] as = new int[mCount + 8];
Jason Sams718cd1f2009-12-23 14:35:29 -0800499 System.arraycopy(mElements, 0, e, 0, mCount);
500 System.arraycopy(mElementNames, 0, s, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -0700501 System.arraycopy(mArraySizes, 0, as, 0, mCount);
Jason Sams718cd1f2009-12-23 14:35:29 -0800502 mElements = e;
503 mElementNames = s;
Jason Sams70d4e502010-09-02 17:35:23 -0700504 mArraySizes = as;
Jason Sams36e612a2009-07-31 16:26:13 -0700505 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800506 mElements[mCount] = element;
507 mElementNames[mCount] = name;
Jason Sams70d4e502010-09-02 17:35:23 -0700508 mArraySizes[mCount] = arraySize;
Jason Sams718cd1f2009-12-23 14:35:29 -0800509 mCount++;
Jason Sams07ae4062009-08-27 20:23:34 -0700510 }
511
Jason Sams70d4e502010-09-02 17:35:23 -0700512 public void add(Element element, String name) {
513 add(element, name, 1);
514 }
515
Jason Sams22534172009-08-04 16:58:20 -0700516 public Element create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800517 mRS.validate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800518 Element[] ein = new Element[mCount];
519 String[] sin = new String[mCount];
Jason Sams70d4e502010-09-02 17:35:23 -0700520 int[] asin = new int[mCount];
Jason Sams718cd1f2009-12-23 14:35:29 -0800521 java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
522 java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -0700523 java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700524
525 int[] ids = new int[ein.length];
526 for (int ct = 0; ct < ein.length; ct++ ) {
Jason Sams06d69de2010-11-09 17:11:40 -0800527 ids[ct] = ein[ct].getID();
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700528 }
Jason Sams70d4e502010-09-02 17:35:23 -0700529 int id = mRS.nElementCreate2(ids, sin, asin);
530 return new Element(id, mRS, ein, sin, asin);
Jason Sams36e612a2009-07-31 16:26:13 -0700531 }
532 }
533
Jason Sams718cd1f2009-12-23 14:35:29 -0800534 static void initPredefined(RenderScript rs) {
535 int a8 = rs.nElementCreate(DataType.UNSIGNED_8.mID,
536 DataKind.PIXEL_A.mID, true, 1);
537 int rgba4444 = rs.nElementCreate(DataType.UNSIGNED_4_4_4_4.mID,
538 DataKind.PIXEL_RGBA.mID, true, 4);
539 int rgba8888 = rs.nElementCreate(DataType.UNSIGNED_8.mID,
540 DataKind.PIXEL_RGBA.mID, true, 4);
541 int rgb565 = rs.nElementCreate(DataType.UNSIGNED_5_6_5.mID,
542 DataKind.PIXEL_RGB.mID, true, 3);
543 rs.nInitElements(a8, rgba4444, rgba8888, rgb565);
544 }
Jason Sams36e612a2009-07-31 16:26:13 -0700545}
546