blob: 10dc35b64955bed1e24e3719b97a7561d23a7b27 [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/**
Jason Samsa1b13ed2010-11-12 14:58:37 -080023 * Element is the basic data type of RenderScript. An element can be of 2
24 * forms. Basic elements contain a single component of data. This can be of
25 * any of the legal RS types. Examples of basic element types.
26 * Single float value
27 * 4 element float vector
28 * single RGB-565 color
29 * single unsigned int 16
30 *
31 * Complex elements will contain a list of sub-elements and names. This in
32 * effect represents a structure of data. The fields can be accessed by name
33 * from a script or shader. The memory layout is defined and ordered. Data
34 * alignment is determinied by the most basic primitive type. i.e. a float4
35 * vector will be alligned to sizeof(float) and not sizeof(float4). The
36 * ordering of elements in memory will be the order in which they were added
37 * with each component aligned as necessary. No re-ordering will be done.
38 *
39 * The primary source of elements will be from scripts. A script that exports a
40 * bind point for a data structure will generate a RS element to represent the
41 * data exported by the script.
Jason Sams36e612a2009-07-31 16:26:13 -070042 **/
43public class Element extends BaseObj {
Jason Samsea84a7c2009-09-04 14:42:41 -070044 int mSize;
Jason Sams718cd1f2009-12-23 14:35:29 -080045 Element[] mElements;
46 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -070047 int[] mArraySizes;
Jason Sams36e612a2009-07-31 16:26:13 -070048
Jason Sams718cd1f2009-12-23 14:35:29 -080049 DataType mType;
50 DataKind mKind;
51 boolean mNormalized;
52 int mVectorSize;
Jason Sams768bc022009-09-21 19:41:04 -070053
Jason Sams718cd1f2009-12-23 14:35:29 -080054 int getSizeBytes() {return mSize;}
Jason Sams36e612a2009-07-31 16:26:13 -070055
Jason Samsa1b13ed2010-11-12 14:58:37 -080056
57 /**
58 * DataType represents the basic type information for a basic element. The
59 * naming convention follows. For numeric types its FLOAT, SIGNED, UNSIGNED
60 * followed by the _BITS where BITS is the size of the data. BOOLEAN is a
61 * true / false (1,0) represented in an 8 bit container. The UNSIGNED
62 * variants with multiple bit definitions are for packed graphical data
63 * formats and represents vectors with per vector member sizes which are
64 * treated as a single unit for packing and alignment purposes.
65 *
66 * MATRIX the three matrix types contain FLOAT_32 elements and are treated
67 * as 32 bits for alignment purposes.
68 *
69 * RS_* objects. 32 bit opaque handles.
70 */
Jason Sams36e612a2009-07-31 16:26:13 -070071 public enum DataType {
Jason Sams718cd1f2009-12-23 14:35:29 -080072 //FLOAT_16 (1, 2),
73 FLOAT_32 (2, 4),
Stephen Hines02f417052010-09-30 15:19:22 -070074 FLOAT_64 (3, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -080075 SIGNED_8 (4, 1),
76 SIGNED_16 (5, 2),
77 SIGNED_32 (6, 4),
Stephen Hinesef1dac22010-10-01 15:39:33 -070078 SIGNED_64 (7, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -080079 UNSIGNED_8 (8, 1),
80 UNSIGNED_16 (9, 2),
81 UNSIGNED_32 (10, 4),
Stephen Hines52d83632010-10-11 16:10:42 -070082 UNSIGNED_64 (11, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -080083
Jason Samsf110d4b2010-06-21 17:42:41 -070084 BOOLEAN(12, 1),
Jason Sams718cd1f2009-12-23 14:35:29 -080085
Jason Samsf110d4b2010-06-21 17:42:41 -070086 UNSIGNED_5_6_5 (13, 2),
87 UNSIGNED_5_5_5_1 (14, 2),
88 UNSIGNED_4_4_4_4 (15, 2),
89
Jason Sams1d45c472010-08-25 14:31:48 -070090 MATRIX_4X4 (16, 64),
91 MATRIX_3X3 (17, 36),
92 MATRIX_2X2 (18, 16),
93
94 RS_ELEMENT (1000, 4),
95 RS_TYPE (1001, 4),
96 RS_ALLOCATION (1002, 4),
97 RS_SAMPLER (1003, 4),
98 RS_SCRIPT (1004, 4),
99 RS_MESH (1005, 4),
100 RS_PROGRAM_FRAGMENT (1006, 4),
101 RS_PROGRAM_VERTEX (1007, 4),
102 RS_PROGRAM_RASTER (1008, 4),
103 RS_PROGRAM_STORE (1009, 4);
Jason Sams36e612a2009-07-31 16:26:13 -0700104
105 int mID;
Jason Sams718cd1f2009-12-23 14:35:29 -0800106 int mSize;
107 DataType(int id, int size) {
Jason Sams36e612a2009-07-31 16:26:13 -0700108 mID = id;
Jason Sams718cd1f2009-12-23 14:35:29 -0800109 mSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700110 }
111 }
112
Jason Samsa1b13ed2010-11-12 14:58:37 -0800113 /**
114 * The special interpretation of the data if required. This is primarly
115 * useful for graphical data. USER indicates no special interpretation is
116 * expected. PIXEL is used in conjunction with the standard data types for
117 * representing texture formats.
118 */
Jason Sams36e612a2009-07-31 16:26:13 -0700119 public enum DataKind {
120 USER (0),
Jason Sams718cd1f2009-12-23 14:35:29 -0800121
122 PIXEL_L (7),
123 PIXEL_A (8),
124 PIXEL_LA (9),
125 PIXEL_RGB (10),
126 PIXEL_RGBA (11);
Jason Sams36e612a2009-07-31 16:26:13 -0700127
128 int mID;
129 DataKind(int id) {
130 mID = id;
131 }
132 }
133
Jason Samsa1b13ed2010-11-12 14:58:37 -0800134 /**
135 * Return if a element is too complex for use as a data source for a Mesh or
136 * a Program.
137 *
138 * @return boolean
139 */
Jason Samsc1d62102010-11-04 14:32:19 -0700140 public boolean isComplex() {
141 if (mElements == null) {
142 return false;
143 }
144 for (int ct=0; ct < mElements.length; ct++) {
145 if (mElements[ct].mElements != null) {
146 return true;
147 }
148 }
149 return false;
150 }
151
Jason Samsa1b13ed2010-11-12 14:58:37 -0800152 /**
153 * Utility function for returning an Element containing a single Boolean.
154 *
155 * @param rs Context to which the element will belong.
156 *
157 * @return Element
158 */
Jason Samsf110d4b2010-06-21 17:42:41 -0700159 public static Element BOOLEAN(RenderScript rs) {
160 if(rs.mElement_BOOLEAN == null) {
161 rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN);
162 }
163 return rs.mElement_BOOLEAN;
164 }
165
Jason Samsa1b13ed2010-11-12 14:58:37 -0800166 /**
167 * Utility function for returning an Element containing a single UNSIGNED_8.
168 *
169 * @param rs Context to which the element will belong.
170 *
171 * @return Element
172 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700173 public static Element U8(RenderScript rs) {
174 if(rs.mElement_U8 == null) {
175 rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800176 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700177 return rs.mElement_U8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800178 }
179
Jason Samsa1b13ed2010-11-12 14:58:37 -0800180 /**
181 * Utility function for returning an Element containing a single SIGNED_8.
182 *
183 * @param rs Context to which the element will belong.
184 *
185 * @return Element
186 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700187 public static Element I8(RenderScript rs) {
188 if(rs.mElement_I8 == null) {
189 rs.mElement_I8 = createUser(rs, DataType.SIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800190 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700191 return rs.mElement_I8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800192 }
193
Jason Samse29f3e72010-06-08 15:40:48 -0700194 public static Element U16(RenderScript rs) {
195 if(rs.mElement_U16 == null) {
196 rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16);
197 }
198 return rs.mElement_U16;
199 }
200
201 public static Element I16(RenderScript rs) {
202 if(rs.mElement_I16 == null) {
203 rs.mElement_I16 = createUser(rs, DataType.SIGNED_16);
204 }
205 return rs.mElement_I16;
206 }
207
Jason Sams8cb39de2010-06-01 15:47:01 -0700208 public static Element U32(RenderScript rs) {
209 if(rs.mElement_U32 == null) {
210 rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800211 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700212 return rs.mElement_U32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800213 }
214
Jason Sams8cb39de2010-06-01 15:47:01 -0700215 public static Element I32(RenderScript rs) {
216 if(rs.mElement_I32 == null) {
217 rs.mElement_I32 = createUser(rs, DataType.SIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800218 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700219 return rs.mElement_I32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800220 }
221
Stephen Hines52d83632010-10-11 16:10:42 -0700222 public static Element U64(RenderScript rs) {
223 if(rs.mElement_U64 == null) {
224 rs.mElement_U64 = createUser(rs, DataType.UNSIGNED_64);
225 }
226 return rs.mElement_U64;
227 }
228
Stephen Hinesef1dac22010-10-01 15:39:33 -0700229 public static Element I64(RenderScript rs) {
230 if(rs.mElement_I64 == null) {
231 rs.mElement_I64 = createUser(rs, DataType.SIGNED_64);
232 }
233 return rs.mElement_I64;
234 }
235
Jason Sams8cb39de2010-06-01 15:47:01 -0700236 public static Element F32(RenderScript rs) {
237 if(rs.mElement_F32 == null) {
238 rs.mElement_F32 = createUser(rs, DataType.FLOAT_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800239 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700240 return rs.mElement_F32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800241 }
242
Stephen Hines02f417052010-09-30 15:19:22 -0700243 public static Element F64(RenderScript rs) {
244 if(rs.mElement_F64 == null) {
245 rs.mElement_F64 = createUser(rs, DataType.FLOAT_64);
246 }
247 return rs.mElement_F64;
248 }
249
Jason Sams8cb39de2010-06-01 15:47:01 -0700250 public static Element ELEMENT(RenderScript rs) {
251 if(rs.mElement_ELEMENT == null) {
252 rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700253 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700254 return rs.mElement_ELEMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700255 }
256
Jason Sams8cb39de2010-06-01 15:47:01 -0700257 public static Element TYPE(RenderScript rs) {
258 if(rs.mElement_TYPE == null) {
259 rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE);
Jason Samsa70f4162010-03-26 15:33:42 -0700260 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700261 return rs.mElement_TYPE;
Jason Samsa70f4162010-03-26 15:33:42 -0700262 }
263
Jason Sams8cb39de2010-06-01 15:47:01 -0700264 public static Element ALLOCATION(RenderScript rs) {
265 if(rs.mElement_ALLOCATION == null) {
266 rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION);
Jason Samsa70f4162010-03-26 15:33:42 -0700267 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700268 return rs.mElement_ALLOCATION;
Jason Samsa70f4162010-03-26 15:33:42 -0700269 }
270
Jason Sams8cb39de2010-06-01 15:47:01 -0700271 public static Element SAMPLER(RenderScript rs) {
272 if(rs.mElement_SAMPLER == null) {
273 rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER);
Jason Samsa70f4162010-03-26 15:33:42 -0700274 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700275 return rs.mElement_SAMPLER;
Jason Samsa70f4162010-03-26 15:33:42 -0700276 }
277
Jason Sams8cb39de2010-06-01 15:47:01 -0700278 public static Element SCRIPT(RenderScript rs) {
279 if(rs.mElement_SCRIPT == null) {
280 rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT);
Jason Samsa70f4162010-03-26 15:33:42 -0700281 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700282 return rs.mElement_SCRIPT;
Jason Samsa70f4162010-03-26 15:33:42 -0700283 }
284
Jason Sams8cb39de2010-06-01 15:47:01 -0700285 public static Element MESH(RenderScript rs) {
286 if(rs.mElement_MESH == null) {
287 rs.mElement_MESH = createUser(rs, DataType.RS_MESH);
Jason Samsa70f4162010-03-26 15:33:42 -0700288 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700289 return rs.mElement_MESH;
Jason Samsa70f4162010-03-26 15:33:42 -0700290 }
291
Jason Sams8cb39de2010-06-01 15:47:01 -0700292 public static Element PROGRAM_FRAGMENT(RenderScript rs) {
293 if(rs.mElement_PROGRAM_FRAGMENT == null) {
294 rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700295 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700296 return rs.mElement_PROGRAM_FRAGMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700297 }
298
Jason Sams8cb39de2010-06-01 15:47:01 -0700299 public static Element PROGRAM_VERTEX(RenderScript rs) {
300 if(rs.mElement_PROGRAM_VERTEX == null) {
301 rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX);
Jason Samsa70f4162010-03-26 15:33:42 -0700302 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700303 return rs.mElement_PROGRAM_VERTEX;
Jason Samsa70f4162010-03-26 15:33:42 -0700304 }
305
Jason Sams8cb39de2010-06-01 15:47:01 -0700306 public static Element PROGRAM_RASTER(RenderScript rs) {
307 if(rs.mElement_PROGRAM_RASTER == null) {
308 rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER);
Jason Samsa70f4162010-03-26 15:33:42 -0700309 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700310 return rs.mElement_PROGRAM_RASTER;
Jason Samsa70f4162010-03-26 15:33:42 -0700311 }
312
Jason Sams8cb39de2010-06-01 15:47:01 -0700313 public static Element PROGRAM_STORE(RenderScript rs) {
314 if(rs.mElement_PROGRAM_STORE == null) {
315 rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE);
Jason Samsa70f4162010-03-26 15:33:42 -0700316 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700317 return rs.mElement_PROGRAM_STORE;
Jason Samsa70f4162010-03-26 15:33:42 -0700318 }
319
320
Jason Sams718cd1f2009-12-23 14:35:29 -0800321 public static Element A_8(RenderScript rs) {
322 if(rs.mElement_A_8 == null) {
323 rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
324 }
325 return rs.mElement_A_8;
326 }
327
328 public static Element RGB_565(RenderScript rs) {
329 if(rs.mElement_RGB_565 == null) {
330 rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
331 }
332 return rs.mElement_RGB_565;
333 }
334
335 public static Element RGB_888(RenderScript rs) {
336 if(rs.mElement_RGB_888 == null) {
337 rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
338 }
339 return rs.mElement_RGB_888;
340 }
341
342 public static Element RGBA_5551(RenderScript rs) {
343 if(rs.mElement_RGBA_5551 == null) {
344 rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
345 }
346 return rs.mElement_RGBA_5551;
347 }
348
349 public static Element RGBA_4444(RenderScript rs) {
350 if(rs.mElement_RGBA_4444 == null) {
351 rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
352 }
353 return rs.mElement_RGBA_4444;
354 }
355
356 public static Element RGBA_8888(RenderScript rs) {
357 if(rs.mElement_RGBA_8888 == null) {
358 rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
359 }
360 return rs.mElement_RGBA_8888;
361 }
362
Jason Sams8cb39de2010-06-01 15:47:01 -0700363 public static Element F32_2(RenderScript rs) {
364 if(rs.mElement_FLOAT_2 == null) {
365 rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2);
Jason Sams718cd1f2009-12-23 14:35:29 -0800366 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700367 return rs.mElement_FLOAT_2;
Jason Sams718cd1f2009-12-23 14:35:29 -0800368 }
369
Jason Sams8cb39de2010-06-01 15:47:01 -0700370 public static Element F32_3(RenderScript rs) {
371 if(rs.mElement_FLOAT_3 == null) {
372 rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3);
Jason Sams718cd1f2009-12-23 14:35:29 -0800373 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700374 return rs.mElement_FLOAT_3;
Jason Sams718cd1f2009-12-23 14:35:29 -0800375 }
376
Jason Sams8cb39de2010-06-01 15:47:01 -0700377 public static Element F32_4(RenderScript rs) {
378 if(rs.mElement_FLOAT_4 == null) {
379 rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800380 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700381 return rs.mElement_FLOAT_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800382 }
383
Jason Sams8cb39de2010-06-01 15:47:01 -0700384 public static Element U8_4(RenderScript rs) {
385 if(rs.mElement_UCHAR_4 == null) {
386 rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800387 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700388 return rs.mElement_UCHAR_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800389 }
390
Jason Sams1d45c472010-08-25 14:31:48 -0700391 public static Element MATRIX_4X4(RenderScript rs) {
392 if(rs.mElement_MATRIX_4X4 == null) {
393 rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4);
394 }
395 return rs.mElement_MATRIX_4X4;
396 }
397 public static Element MATRIX4X4(RenderScript rs) {
398 return MATRIX_4X4(rs);
399 }
400
401 public static Element MATRIX_3X3(RenderScript rs) {
402 if(rs.mElement_MATRIX_3X3 == null) {
403 rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3);
404 }
405 return rs.mElement_MATRIX_4X4;
406 }
407
408 public static Element MATRIX_2X2(RenderScript rs) {
409 if(rs.mElement_MATRIX_2X2 == null) {
410 rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2);
411 }
412 return rs.mElement_MATRIX_2X2;
413 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800414
Jason Sams70d4e502010-09-02 17:35:23 -0700415 Element(int id, RenderScript rs, Element[] e, String[] n, int[] as) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700416 super(id, rs);
Jason Samsea84a7c2009-09-04 14:42:41 -0700417 mSize = 0;
Jason Sams718cd1f2009-12-23 14:35:29 -0800418 mElements = e;
419 mElementNames = n;
Jason Sams70d4e502010-09-02 17:35:23 -0700420 mArraySizes = as;
Jason Sams718cd1f2009-12-23 14:35:29 -0800421 for (int ct = 0; ct < mElements.length; ct++ ) {
Alex Sakhartchouk9e401bc2010-10-13 14:22:02 -0700422 mSize += mElements[ct].mSize * mArraySizes[ct];
Jason Sams718cd1f2009-12-23 14:35:29 -0800423 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800424 }
425
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700426 Element(int id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
427 super(id, rs);
Jason Sams252c0782011-01-11 17:42:52 -0800428 if ((dt != DataType.UNSIGNED_5_6_5) &&
429 (dt != DataType.UNSIGNED_4_4_4_4) &&
430 (dt != DataType.UNSIGNED_5_5_5_1)) {
431 mSize = dt.mSize * size;
432 } else {
433 mSize = dt.mSize;
434 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800435 mType = dt;
436 mKind = dk;
437 mNormalized = norm;
438 mVectorSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700439 }
440
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700441 Element(int id, RenderScript rs) {
442 super(id, rs);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700443 }
444
445 @Override
446 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800447 super.updateFromNative();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700448
449 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
450 int[] dataBuffer = new int[5];
Jason Sams06d69de2010-11-09 17:11:40 -0800451 mRS.nElementGetNativeData(getID(), dataBuffer);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700452
453 mNormalized = dataBuffer[2] == 1 ? true : false;
454 mVectorSize = dataBuffer[3];
455 mSize = 0;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700456 for (DataType dt: DataType.values()) {
457 if(dt.mID == dataBuffer[0]){
458 mType = dt;
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700459 mSize = mType.mSize * mVectorSize;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700460 }
461 }
462 for (DataKind dk: DataKind.values()) {
463 if(dk.mID == dataBuffer[1]){
464 mKind = dk;
465 }
466 }
467
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700468 int numSubElements = dataBuffer[4];
469 if(numSubElements > 0) {
470 mElements = new Element[numSubElements];
471 mElementNames = new String[numSubElements];
472
473 int[] subElementIds = new int[numSubElements];
Jason Sams06d69de2010-11-09 17:11:40 -0800474 mRS.nElementGetSubElements(getID(), subElementIds, mElementNames);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700475 for(int i = 0; i < numSubElements; i ++) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700476 mElements[i] = new Element(subElementIds[i], mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700477 mElements[i].updateFromNative();
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700478 mSize += mElements[i].mSize;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700479 }
480 }
481
482 }
483
Jason Samsa1b13ed2010-11-12 14:58:37 -0800484 /**
485 * Create a custom Element of the specified DataType. The DataKind will be
486 * set to USER and the vector size to 1 indicating non-vector.
487 *
488 * @param rs The context associated with the new Element.
489 * @param dt The DataType for the new element.
490 * @return Element
491 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800492 static Element createUser(RenderScript rs, DataType dt) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700493 DataKind dk = DataKind.USER;
494 boolean norm = false;
495 int vecSize = 1;
496 int id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize);
497 return new Element(id, rs, dt, dk, norm, vecSize);
Jason Sams718cd1f2009-12-23 14:35:29 -0800498 }
499
Jason Samsa1b13ed2010-11-12 14:58:37 -0800500 /**
501 * Create a custom vector element of the specified DataType and vector size.
502 * DataKind will be set to USER.
503 *
504 * @param rs The context associated with the new Element.
505 * @param dt The DataType for the new element.
506 * @param size Vector size for the new Element. Range 2-4 inclusive
507 * supported.
508 *
509 * @return Element
510 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800511 public static Element createVector(RenderScript rs, DataType dt, int size) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800512 if (size < 2 || size > 4) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800513 throw new RSIllegalArgumentException("Vector size out of range 2-4.");
Jason Samsea84a7c2009-09-04 14:42:41 -0700514 }
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700515 DataKind dk = DataKind.USER;
516 boolean norm = false;
517 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
518 return new Element(id, rs, dt, dk, norm, size);
Jason Samsea84a7c2009-09-04 14:42:41 -0700519 }
520
Jason Samsa1b13ed2010-11-12 14:58:37 -0800521 /**
522 * Create a new pixel Element type. A matching DataType and DataKind must
523 * be provided. The DataType and DataKind must contain the same number of
524 * components. Vector size will be set to 1.
525 *
526 * @param rs The context associated with the new Element.
527 * @param dt The DataType for the new element.
528 * @param dk The DataKind to specify the mapping of each component in the
529 * DataType.
530 *
531 * @return Element
532 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800533 public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800534 if (!(dk == DataKind.PIXEL_L ||
535 dk == DataKind.PIXEL_A ||
536 dk == DataKind.PIXEL_LA ||
537 dk == DataKind.PIXEL_RGB ||
538 dk == DataKind.PIXEL_RGBA)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700539 throw new RSIllegalArgumentException("Unsupported DataKind");
Jason Sams718cd1f2009-12-23 14:35:29 -0800540 }
541 if (!(dt == DataType.UNSIGNED_8 ||
542 dt == DataType.UNSIGNED_5_6_5 ||
543 dt == DataType.UNSIGNED_4_4_4_4 ||
544 dt == DataType.UNSIGNED_5_5_5_1)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700545 throw new RSIllegalArgumentException("Unsupported DataType");
Jason Sams718cd1f2009-12-23 14:35:29 -0800546 }
547 if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
Jason Samsc1d62102010-11-04 14:32:19 -0700548 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800549 }
550 if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700551 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800552 }
553 if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700554 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800555 }
556
557 int size = 1;
558 if (dk == DataKind.PIXEL_LA) {
559 size = 2;
560 }
561 if (dk == DataKind.PIXEL_RGB) {
562 size = 3;
563 }
564 if (dk == DataKind.PIXEL_RGBA) {
565 size = 4;
566 }
567
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700568 boolean norm = true;
569 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
570 return new Element(id, rs, dt, dk, norm, size);
Jason Sams718cd1f2009-12-23 14:35:29 -0800571 }
Jason Sams36e612a2009-07-31 16:26:13 -0700572
Jason Samsa1b13ed2010-11-12 14:58:37 -0800573 /**
574 * Builder class for producing complex elements with matching field and name
575 * pairs. The builder starts empty. The order in which elements are added
576 * is retained for the layout in memory.
577 *
578 */
Jason Sams36e612a2009-07-31 16:26:13 -0700579 public static class Builder {
580 RenderScript mRS;
Jason Sams718cd1f2009-12-23 14:35:29 -0800581 Element[] mElements;
582 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -0700583 int[] mArraySizes;
Jason Sams718cd1f2009-12-23 14:35:29 -0800584 int mCount;
Jason Sams22534172009-08-04 16:58:20 -0700585
Jason Samsa1b13ed2010-11-12 14:58:37 -0800586 /**
587 * Create a builder object.
588 *
589 * @param rs
590 */
Jason Sams22534172009-08-04 16:58:20 -0700591 public Builder(RenderScript rs) {
Jason Sams36e612a2009-07-31 16:26:13 -0700592 mRS = rs;
Jason Sams718cd1f2009-12-23 14:35:29 -0800593 mCount = 0;
594 mElements = new Element[8];
595 mElementNames = new String[8];
Jason Sams70d4e502010-09-02 17:35:23 -0700596 mArraySizes = new int[8];
Jason Sams36e612a2009-07-31 16:26:13 -0700597 }
598
Jason Samsa1b13ed2010-11-12 14:58:37 -0800599 /**
600 * Add an array of elements to this element.
601 *
602 * @param element
603 * @param name
604 * @param arraySize
605 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800606 public Builder add(Element element, String name, int arraySize) {
Jason Sams70d4e502010-09-02 17:35:23 -0700607 if (arraySize < 1) {
Jason Samsc1d62102010-11-04 14:32:19 -0700608 throw new RSIllegalArgumentException("Array size cannot be less than 1.");
Jason Sams70d4e502010-09-02 17:35:23 -0700609 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800610 if(mCount == mElements.length) {
611 Element[] e = new Element[mCount + 8];
612 String[] s = new String[mCount + 8];
Jason Sams70d4e502010-09-02 17:35:23 -0700613 int[] as = new int[mCount + 8];
Jason Sams718cd1f2009-12-23 14:35:29 -0800614 System.arraycopy(mElements, 0, e, 0, mCount);
615 System.arraycopy(mElementNames, 0, s, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -0700616 System.arraycopy(mArraySizes, 0, as, 0, mCount);
Jason Sams718cd1f2009-12-23 14:35:29 -0800617 mElements = e;
618 mElementNames = s;
Jason Sams70d4e502010-09-02 17:35:23 -0700619 mArraySizes = as;
Jason Sams36e612a2009-07-31 16:26:13 -0700620 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800621 mElements[mCount] = element;
622 mElementNames[mCount] = name;
Jason Sams70d4e502010-09-02 17:35:23 -0700623 mArraySizes[mCount] = arraySize;
Jason Sams718cd1f2009-12-23 14:35:29 -0800624 mCount++;
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800625 return this;
Jason Sams07ae4062009-08-27 20:23:34 -0700626 }
627
Jason Samsa1b13ed2010-11-12 14:58:37 -0800628 /**
629 * Add a single element to this Element.
630 *
631 * @param element
632 * @param name
633 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800634 public Builder add(Element element, String name) {
635 return add(element, name, 1);
Jason Sams70d4e502010-09-02 17:35:23 -0700636 }
637
Jason Samsa1b13ed2010-11-12 14:58:37 -0800638 /**
639 * Create the element from this builder.
640 *
641 *
642 * @return Element
643 */
Jason Sams22534172009-08-04 16:58:20 -0700644 public Element create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800645 mRS.validate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800646 Element[] ein = new Element[mCount];
647 String[] sin = new String[mCount];
Jason Sams70d4e502010-09-02 17:35:23 -0700648 int[] asin = new int[mCount];
Jason Sams718cd1f2009-12-23 14:35:29 -0800649 java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
650 java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -0700651 java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700652
653 int[] ids = new int[ein.length];
654 for (int ct = 0; ct < ein.length; ct++ ) {
Jason Sams06d69de2010-11-09 17:11:40 -0800655 ids[ct] = ein[ct].getID();
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700656 }
Jason Sams70d4e502010-09-02 17:35:23 -0700657 int id = mRS.nElementCreate2(ids, sin, asin);
658 return new Element(id, mRS, ein, sin, asin);
Jason Sams36e612a2009-07-31 16:26:13 -0700659 }
660 }
Jason Sams36e612a2009-07-31 16:26:13 -0700661}
662