blob: 0c1ad2a4b217069abf90103bd4820164273067d1 [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/**
Robert Ly11518ac2011-02-09 13:57:06 -080023 * <p>The most basic data type. An element represents one cell of a memory allocation.
Alex Sakhartchouk34769772011-02-28 16:01:28 -080024 * Element is the basic data type of Renderscript. An element can be of two forms: Basic elements or Complex forms.
Robert Ly11518ac2011-02-09 13:57:06 -080025 * Examples of basic elements are:</p>
26 * <ul>
27 * <li>Single float value</li>
28 * <li>4 element float vector</li>
29 * <li>single RGB-565 color</li>
30 * <li>single unsigned int 16</li>
31 * </ul>
Alex Sakhartchouk34769772011-02-28 16:01:28 -080032 * <p>Complex elements contain a list of sub-elements and names that
Robert Ly11518ac2011-02-09 13:57:06 -080033 * represents a structure of data. The fields can be accessed by name
34 * from a script or shader. The memory layout is defined and ordered. Data
35 * alignment is determinied by the most basic primitive type. i.e. a float4
Jason Samsa1b13ed2010-11-12 14:58:37 -080036 * vector will be alligned to sizeof(float) and not sizeof(float4). The
37 * ordering of elements in memory will be the order in which they were added
Robert Ly11518ac2011-02-09 13:57:06 -080038 * with each component aligned as necessary. No re-ordering will be done.</p>
Jason Samsa1b13ed2010-11-12 14:58:37 -080039 *
Robert Ly11518ac2011-02-09 13:57:06 -080040 * <p>The primary source of elements are from scripts. A script that exports a
41 * bind point for a data structure generates a Renderscript element to represent the
42 * data exported by the script. The other common source of elements is from bitmap formats.</p>
Jason Sams36e612a2009-07-31 16:26:13 -070043 **/
44public class Element extends BaseObj {
Jason Samsea84a7c2009-09-04 14:42:41 -070045 int mSize;
Jason Sams718cd1f2009-12-23 14:35:29 -080046 Element[] mElements;
47 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -070048 int[] mArraySizes;
Jason Sams36e612a2009-07-31 16:26:13 -070049
Jason Sams718cd1f2009-12-23 14:35:29 -080050 DataType mType;
51 DataKind mKind;
52 boolean mNormalized;
53 int mVectorSize;
Jason Sams768bc022009-09-21 19:41:04 -070054
Jason Sams718cd1f2009-12-23 14:35:29 -080055 int getSizeBytes() {return mSize;}
Jason Sams36e612a2009-07-31 16:26:13 -070056
Jason Samsa1b13ed2010-11-12 14:58:37 -080057
58 /**
59 * DataType represents the basic type information for a basic element. The
60 * naming convention follows. For numeric types its FLOAT, SIGNED, UNSIGNED
61 * followed by the _BITS where BITS is the size of the data. BOOLEAN is a
62 * true / false (1,0) represented in an 8 bit container. The UNSIGNED
63 * variants with multiple bit definitions are for packed graphical data
64 * formats and represents vectors with per vector member sizes which are
65 * treated as a single unit for packing and alignment purposes.
66 *
67 * MATRIX the three matrix types contain FLOAT_32 elements and are treated
68 * as 32 bits for alignment purposes.
69 *
70 * RS_* objects. 32 bit opaque handles.
71 */
Jason Sams36e612a2009-07-31 16:26:13 -070072 public enum DataType {
Jason Sams718cd1f2009-12-23 14:35:29 -080073 //FLOAT_16 (1, 2),
74 FLOAT_32 (2, 4),
Stephen Hines02f417052010-09-30 15:19:22 -070075 FLOAT_64 (3, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -080076 SIGNED_8 (4, 1),
77 SIGNED_16 (5, 2),
78 SIGNED_32 (6, 4),
Stephen Hinesef1dac22010-10-01 15:39:33 -070079 SIGNED_64 (7, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -080080 UNSIGNED_8 (8, 1),
81 UNSIGNED_16 (9, 2),
82 UNSIGNED_32 (10, 4),
Stephen Hines52d83632010-10-11 16:10:42 -070083 UNSIGNED_64 (11, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -080084
Jason Samsf110d4b2010-06-21 17:42:41 -070085 BOOLEAN(12, 1),
Jason Sams718cd1f2009-12-23 14:35:29 -080086
Jason Samsf110d4b2010-06-21 17:42:41 -070087 UNSIGNED_5_6_5 (13, 2),
88 UNSIGNED_5_5_5_1 (14, 2),
89 UNSIGNED_4_4_4_4 (15, 2),
90
Jason Sams1d45c472010-08-25 14:31:48 -070091 MATRIX_4X4 (16, 64),
92 MATRIX_3X3 (17, 36),
93 MATRIX_2X2 (18, 16),
94
95 RS_ELEMENT (1000, 4),
96 RS_TYPE (1001, 4),
97 RS_ALLOCATION (1002, 4),
98 RS_SAMPLER (1003, 4),
99 RS_SCRIPT (1004, 4),
100 RS_MESH (1005, 4),
101 RS_PROGRAM_FRAGMENT (1006, 4),
102 RS_PROGRAM_VERTEX (1007, 4),
103 RS_PROGRAM_RASTER (1008, 4),
104 RS_PROGRAM_STORE (1009, 4);
Jason Sams36e612a2009-07-31 16:26:13 -0700105
106 int mID;
Jason Sams718cd1f2009-12-23 14:35:29 -0800107 int mSize;
108 DataType(int id, int size) {
Jason Sams36e612a2009-07-31 16:26:13 -0700109 mID = id;
Jason Sams718cd1f2009-12-23 14:35:29 -0800110 mSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700111 }
112 }
113
Jason Samsa1b13ed2010-11-12 14:58:37 -0800114 /**
115 * The special interpretation of the data if required. This is primarly
116 * useful for graphical data. USER indicates no special interpretation is
117 * expected. PIXEL is used in conjunction with the standard data types for
118 * representing texture formats.
119 */
Jason Sams36e612a2009-07-31 16:26:13 -0700120 public enum DataKind {
121 USER (0),
Jason Sams718cd1f2009-12-23 14:35:29 -0800122
123 PIXEL_L (7),
124 PIXEL_A (8),
125 PIXEL_LA (9),
126 PIXEL_RGB (10),
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700127 PIXEL_RGBA (11),
128 PIXEL_DEPTH (12);
Jason Sams36e612a2009-07-31 16:26:13 -0700129
130 int mID;
131 DataKind(int id) {
132 mID = id;
133 }
134 }
135
Jason Samsa1b13ed2010-11-12 14:58:37 -0800136 /**
137 * Return if a element is too complex for use as a data source for a Mesh or
138 * a Program.
139 *
140 * @return boolean
141 */
Jason Samsc1d62102010-11-04 14:32:19 -0700142 public boolean isComplex() {
143 if (mElements == null) {
144 return false;
145 }
146 for (int ct=0; ct < mElements.length; ct++) {
147 if (mElements[ct].mElements != null) {
148 return true;
149 }
150 }
151 return false;
152 }
153
Jason Samsa1b13ed2010-11-12 14:58:37 -0800154 /**
155 * Utility function for returning an Element containing a single Boolean.
156 *
157 * @param rs Context to which the element will belong.
158 *
159 * @return Element
160 */
Jason Samsf110d4b2010-06-21 17:42:41 -0700161 public static Element BOOLEAN(RenderScript rs) {
162 if(rs.mElement_BOOLEAN == null) {
163 rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN);
164 }
165 return rs.mElement_BOOLEAN;
166 }
167
Jason Samsa1b13ed2010-11-12 14:58:37 -0800168 /**
169 * Utility function for returning an Element containing a single UNSIGNED_8.
170 *
171 * @param rs Context to which the element will belong.
172 *
173 * @return Element
174 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700175 public static Element U8(RenderScript rs) {
176 if(rs.mElement_U8 == null) {
177 rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800178 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700179 return rs.mElement_U8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800180 }
181
Jason Samsa1b13ed2010-11-12 14:58:37 -0800182 /**
183 * Utility function for returning an Element containing a single SIGNED_8.
184 *
185 * @param rs Context to which the element will belong.
186 *
187 * @return Element
188 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700189 public static Element I8(RenderScript rs) {
190 if(rs.mElement_I8 == null) {
191 rs.mElement_I8 = createUser(rs, DataType.SIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800192 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700193 return rs.mElement_I8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800194 }
195
Jason Samse29f3e72010-06-08 15:40:48 -0700196 public static Element U16(RenderScript rs) {
197 if(rs.mElement_U16 == null) {
198 rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16);
199 }
200 return rs.mElement_U16;
201 }
202
203 public static Element I16(RenderScript rs) {
204 if(rs.mElement_I16 == null) {
205 rs.mElement_I16 = createUser(rs, DataType.SIGNED_16);
206 }
207 return rs.mElement_I16;
208 }
209
Jason Sams8cb39de2010-06-01 15:47:01 -0700210 public static Element U32(RenderScript rs) {
211 if(rs.mElement_U32 == null) {
212 rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800213 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700214 return rs.mElement_U32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800215 }
216
Jason Sams8cb39de2010-06-01 15:47:01 -0700217 public static Element I32(RenderScript rs) {
218 if(rs.mElement_I32 == null) {
219 rs.mElement_I32 = createUser(rs, DataType.SIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800220 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700221 return rs.mElement_I32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800222 }
223
Stephen Hines52d83632010-10-11 16:10:42 -0700224 public static Element U64(RenderScript rs) {
225 if(rs.mElement_U64 == null) {
226 rs.mElement_U64 = createUser(rs, DataType.UNSIGNED_64);
227 }
228 return rs.mElement_U64;
229 }
230
Stephen Hinesef1dac22010-10-01 15:39:33 -0700231 public static Element I64(RenderScript rs) {
232 if(rs.mElement_I64 == null) {
233 rs.mElement_I64 = createUser(rs, DataType.SIGNED_64);
234 }
235 return rs.mElement_I64;
236 }
237
Jason Sams8cb39de2010-06-01 15:47:01 -0700238 public static Element F32(RenderScript rs) {
239 if(rs.mElement_F32 == null) {
240 rs.mElement_F32 = createUser(rs, DataType.FLOAT_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800241 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700242 return rs.mElement_F32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800243 }
244
Stephen Hines02f417052010-09-30 15:19:22 -0700245 public static Element F64(RenderScript rs) {
246 if(rs.mElement_F64 == null) {
247 rs.mElement_F64 = createUser(rs, DataType.FLOAT_64);
248 }
249 return rs.mElement_F64;
250 }
251
Jason Sams8cb39de2010-06-01 15:47:01 -0700252 public static Element ELEMENT(RenderScript rs) {
253 if(rs.mElement_ELEMENT == null) {
254 rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700255 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700256 return rs.mElement_ELEMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700257 }
258
Jason Sams8cb39de2010-06-01 15:47:01 -0700259 public static Element TYPE(RenderScript rs) {
260 if(rs.mElement_TYPE == null) {
261 rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE);
Jason Samsa70f4162010-03-26 15:33:42 -0700262 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700263 return rs.mElement_TYPE;
Jason Samsa70f4162010-03-26 15:33:42 -0700264 }
265
Jason Sams8cb39de2010-06-01 15:47:01 -0700266 public static Element ALLOCATION(RenderScript rs) {
267 if(rs.mElement_ALLOCATION == null) {
268 rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION);
Jason Samsa70f4162010-03-26 15:33:42 -0700269 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700270 return rs.mElement_ALLOCATION;
Jason Samsa70f4162010-03-26 15:33:42 -0700271 }
272
Jason Sams8cb39de2010-06-01 15:47:01 -0700273 public static Element SAMPLER(RenderScript rs) {
274 if(rs.mElement_SAMPLER == null) {
275 rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER);
Jason Samsa70f4162010-03-26 15:33:42 -0700276 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700277 return rs.mElement_SAMPLER;
Jason Samsa70f4162010-03-26 15:33:42 -0700278 }
279
Jason Sams8cb39de2010-06-01 15:47:01 -0700280 public static Element SCRIPT(RenderScript rs) {
281 if(rs.mElement_SCRIPT == null) {
282 rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT);
Jason Samsa70f4162010-03-26 15:33:42 -0700283 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700284 return rs.mElement_SCRIPT;
Jason Samsa70f4162010-03-26 15:33:42 -0700285 }
286
Jason Sams8cb39de2010-06-01 15:47:01 -0700287 public static Element MESH(RenderScript rs) {
288 if(rs.mElement_MESH == null) {
289 rs.mElement_MESH = createUser(rs, DataType.RS_MESH);
Jason Samsa70f4162010-03-26 15:33:42 -0700290 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700291 return rs.mElement_MESH;
Jason Samsa70f4162010-03-26 15:33:42 -0700292 }
293
Jason Sams8cb39de2010-06-01 15:47:01 -0700294 public static Element PROGRAM_FRAGMENT(RenderScript rs) {
295 if(rs.mElement_PROGRAM_FRAGMENT == null) {
296 rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700297 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700298 return rs.mElement_PROGRAM_FRAGMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700299 }
300
Jason Sams8cb39de2010-06-01 15:47:01 -0700301 public static Element PROGRAM_VERTEX(RenderScript rs) {
302 if(rs.mElement_PROGRAM_VERTEX == null) {
303 rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX);
Jason Samsa70f4162010-03-26 15:33:42 -0700304 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700305 return rs.mElement_PROGRAM_VERTEX;
Jason Samsa70f4162010-03-26 15:33:42 -0700306 }
307
Jason Sams8cb39de2010-06-01 15:47:01 -0700308 public static Element PROGRAM_RASTER(RenderScript rs) {
309 if(rs.mElement_PROGRAM_RASTER == null) {
310 rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER);
Jason Samsa70f4162010-03-26 15:33:42 -0700311 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700312 return rs.mElement_PROGRAM_RASTER;
Jason Samsa70f4162010-03-26 15:33:42 -0700313 }
314
Jason Sams8cb39de2010-06-01 15:47:01 -0700315 public static Element PROGRAM_STORE(RenderScript rs) {
316 if(rs.mElement_PROGRAM_STORE == null) {
317 rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE);
Jason Samsa70f4162010-03-26 15:33:42 -0700318 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700319 return rs.mElement_PROGRAM_STORE;
Jason Samsa70f4162010-03-26 15:33:42 -0700320 }
321
322
Jason Sams718cd1f2009-12-23 14:35:29 -0800323 public static Element A_8(RenderScript rs) {
324 if(rs.mElement_A_8 == null) {
325 rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
326 }
327 return rs.mElement_A_8;
328 }
329
330 public static Element RGB_565(RenderScript rs) {
331 if(rs.mElement_RGB_565 == null) {
332 rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
333 }
334 return rs.mElement_RGB_565;
335 }
336
337 public static Element RGB_888(RenderScript rs) {
338 if(rs.mElement_RGB_888 == null) {
339 rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
340 }
341 return rs.mElement_RGB_888;
342 }
343
344 public static Element RGBA_5551(RenderScript rs) {
345 if(rs.mElement_RGBA_5551 == null) {
346 rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
347 }
348 return rs.mElement_RGBA_5551;
349 }
350
351 public static Element RGBA_4444(RenderScript rs) {
352 if(rs.mElement_RGBA_4444 == null) {
353 rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
354 }
355 return rs.mElement_RGBA_4444;
356 }
357
358 public static Element RGBA_8888(RenderScript rs) {
359 if(rs.mElement_RGBA_8888 == null) {
360 rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
361 }
362 return rs.mElement_RGBA_8888;
363 }
364
Jason Sams8cb39de2010-06-01 15:47:01 -0700365 public static Element F32_2(RenderScript rs) {
366 if(rs.mElement_FLOAT_2 == null) {
367 rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2);
Jason Sams718cd1f2009-12-23 14:35:29 -0800368 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700369 return rs.mElement_FLOAT_2;
Jason Sams718cd1f2009-12-23 14:35:29 -0800370 }
371
Jason Sams8cb39de2010-06-01 15:47:01 -0700372 public static Element F32_3(RenderScript rs) {
373 if(rs.mElement_FLOAT_3 == null) {
374 rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3);
Jason Sams718cd1f2009-12-23 14:35:29 -0800375 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700376 return rs.mElement_FLOAT_3;
Jason Sams718cd1f2009-12-23 14:35:29 -0800377 }
378
Jason Sams8cb39de2010-06-01 15:47:01 -0700379 public static Element F32_4(RenderScript rs) {
380 if(rs.mElement_FLOAT_4 == null) {
381 rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800382 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700383 return rs.mElement_FLOAT_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800384 }
385
Jason Sams8cb39de2010-06-01 15:47:01 -0700386 public static Element U8_4(RenderScript rs) {
387 if(rs.mElement_UCHAR_4 == null) {
388 rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800389 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700390 return rs.mElement_UCHAR_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800391 }
392
Jason Sams1d45c472010-08-25 14:31:48 -0700393 public static Element MATRIX_4X4(RenderScript rs) {
394 if(rs.mElement_MATRIX_4X4 == null) {
395 rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4);
396 }
397 return rs.mElement_MATRIX_4X4;
398 }
399 public static Element MATRIX4X4(RenderScript rs) {
400 return MATRIX_4X4(rs);
401 }
402
403 public static Element MATRIX_3X3(RenderScript rs) {
404 if(rs.mElement_MATRIX_3X3 == null) {
405 rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3);
406 }
Alex Sakhartchouk34769772011-02-28 16:01:28 -0800407 return rs.mElement_MATRIX_3X3;
Jason Sams1d45c472010-08-25 14:31:48 -0700408 }
409
410 public static Element MATRIX_2X2(RenderScript rs) {
411 if(rs.mElement_MATRIX_2X2 == null) {
412 rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2);
413 }
414 return rs.mElement_MATRIX_2X2;
415 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800416
Jason Sams70d4e502010-09-02 17:35:23 -0700417 Element(int id, RenderScript rs, Element[] e, String[] n, int[] as) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700418 super(id, rs);
Jason Samsea84a7c2009-09-04 14:42:41 -0700419 mSize = 0;
Jason Sams718cd1f2009-12-23 14:35:29 -0800420 mElements = e;
421 mElementNames = n;
Jason Sams70d4e502010-09-02 17:35:23 -0700422 mArraySizes = as;
Jason Sams718cd1f2009-12-23 14:35:29 -0800423 for (int ct = 0; ct < mElements.length; ct++ ) {
Alex Sakhartchouk9e401bc2010-10-13 14:22:02 -0700424 mSize += mElements[ct].mSize * mArraySizes[ct];
Jason Sams718cd1f2009-12-23 14:35:29 -0800425 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800426 }
427
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700428 Element(int id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
429 super(id, rs);
Jason Sams252c0782011-01-11 17:42:52 -0800430 if ((dt != DataType.UNSIGNED_5_6_5) &&
431 (dt != DataType.UNSIGNED_4_4_4_4) &&
432 (dt != DataType.UNSIGNED_5_5_5_1)) {
433 mSize = dt.mSize * size;
434 } else {
435 mSize = dt.mSize;
436 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800437 mType = dt;
438 mKind = dk;
439 mNormalized = norm;
440 mVectorSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700441 }
442
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700443 Element(int id, RenderScript rs) {
444 super(id, rs);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700445 }
446
447 @Override
448 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800449 super.updateFromNative();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700450
451 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
452 int[] dataBuffer = new int[5];
Jason Sams06d69de2010-11-09 17:11:40 -0800453 mRS.nElementGetNativeData(getID(), dataBuffer);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700454
455 mNormalized = dataBuffer[2] == 1 ? true : false;
456 mVectorSize = dataBuffer[3];
457 mSize = 0;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700458 for (DataType dt: DataType.values()) {
459 if(dt.mID == dataBuffer[0]){
460 mType = dt;
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700461 mSize = mType.mSize * mVectorSize;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700462 }
463 }
464 for (DataKind dk: DataKind.values()) {
465 if(dk.mID == dataBuffer[1]){
466 mKind = dk;
467 }
468 }
469
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700470 int numSubElements = dataBuffer[4];
471 if(numSubElements > 0) {
472 mElements = new Element[numSubElements];
473 mElementNames = new String[numSubElements];
474
475 int[] subElementIds = new int[numSubElements];
Jason Sams06d69de2010-11-09 17:11:40 -0800476 mRS.nElementGetSubElements(getID(), subElementIds, mElementNames);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700477 for(int i = 0; i < numSubElements; i ++) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700478 mElements[i] = new Element(subElementIds[i], mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700479 mElements[i].updateFromNative();
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700480 mSize += mElements[i].mSize;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700481 }
482 }
483
484 }
485
Jason Samsa1b13ed2010-11-12 14:58:37 -0800486 /**
487 * Create a custom Element of the specified DataType. The DataKind will be
488 * set to USER and the vector size to 1 indicating non-vector.
489 *
490 * @param rs The context associated with the new Element.
491 * @param dt The DataType for the new element.
492 * @return Element
493 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800494 static Element createUser(RenderScript rs, DataType dt) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700495 DataKind dk = DataKind.USER;
496 boolean norm = false;
497 int vecSize = 1;
498 int id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize);
499 return new Element(id, rs, dt, dk, norm, vecSize);
Jason Sams718cd1f2009-12-23 14:35:29 -0800500 }
501
Jason Samsa1b13ed2010-11-12 14:58:37 -0800502 /**
503 * Create a custom vector element of the specified DataType and vector size.
504 * DataKind will be set to USER.
505 *
506 * @param rs The context associated with the new Element.
507 * @param dt The DataType for the new element.
508 * @param size Vector size for the new Element. Range 2-4 inclusive
509 * supported.
510 *
511 * @return Element
512 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800513 public static Element createVector(RenderScript rs, DataType dt, int size) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800514 if (size < 2 || size > 4) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800515 throw new RSIllegalArgumentException("Vector size out of range 2-4.");
Jason Samsea84a7c2009-09-04 14:42:41 -0700516 }
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700517 DataKind dk = DataKind.USER;
518 boolean norm = false;
519 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
520 return new Element(id, rs, dt, dk, norm, size);
Jason Samsea84a7c2009-09-04 14:42:41 -0700521 }
522
Jason Samsa1b13ed2010-11-12 14:58:37 -0800523 /**
524 * Create a new pixel Element type. A matching DataType and DataKind must
525 * be provided. The DataType and DataKind must contain the same number of
526 * components. Vector size will be set to 1.
527 *
528 * @param rs The context associated with the new Element.
529 * @param dt The DataType for the new element.
530 * @param dk The DataKind to specify the mapping of each component in the
531 * DataType.
532 *
533 * @return Element
534 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800535 public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800536 if (!(dk == DataKind.PIXEL_L ||
537 dk == DataKind.PIXEL_A ||
538 dk == DataKind.PIXEL_LA ||
539 dk == DataKind.PIXEL_RGB ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700540 dk == DataKind.PIXEL_RGBA ||
541 dk == DataKind.PIXEL_DEPTH)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700542 throw new RSIllegalArgumentException("Unsupported DataKind");
Jason Sams718cd1f2009-12-23 14:35:29 -0800543 }
544 if (!(dt == DataType.UNSIGNED_8 ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700545 dt == DataType.UNSIGNED_16 ||
Jason Sams718cd1f2009-12-23 14:35:29 -0800546 dt == DataType.UNSIGNED_5_6_5 ||
547 dt == DataType.UNSIGNED_4_4_4_4 ||
548 dt == DataType.UNSIGNED_5_5_5_1)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700549 throw new RSIllegalArgumentException("Unsupported DataType");
Jason Sams718cd1f2009-12-23 14:35:29 -0800550 }
551 if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
Jason Samsc1d62102010-11-04 14:32:19 -0700552 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800553 }
554 if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700555 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800556 }
557 if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700558 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800559 }
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700560 if (dt == DataType.UNSIGNED_16 &&
561 dk != DataKind.PIXEL_DEPTH) {
562 throw new RSIllegalArgumentException("Bad kind and type combo");
563 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800564
565 int size = 1;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700566 switch (dk) {
567 case PIXEL_LA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800568 size = 2;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700569 break;
570 case PIXEL_RGB:
Jason Sams718cd1f2009-12-23 14:35:29 -0800571 size = 3;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700572 break;
573 case PIXEL_RGBA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800574 size = 4;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700575 break;
576 case PIXEL_DEPTH:
577 size = 2;
578 break;
Jason Sams718cd1f2009-12-23 14:35:29 -0800579 }
580
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700581 boolean norm = true;
582 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
583 return new Element(id, rs, dt, dk, norm, size);
Jason Sams718cd1f2009-12-23 14:35:29 -0800584 }
Jason Sams36e612a2009-07-31 16:26:13 -0700585
Jason Samsa1b13ed2010-11-12 14:58:37 -0800586 /**
587 * Builder class for producing complex elements with matching field and name
588 * pairs. The builder starts empty. The order in which elements are added
589 * is retained for the layout in memory.
590 *
591 */
Jason Sams36e612a2009-07-31 16:26:13 -0700592 public static class Builder {
593 RenderScript mRS;
Jason Sams718cd1f2009-12-23 14:35:29 -0800594 Element[] mElements;
595 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -0700596 int[] mArraySizes;
Jason Sams718cd1f2009-12-23 14:35:29 -0800597 int mCount;
Jason Sams22534172009-08-04 16:58:20 -0700598
Jason Samsa1b13ed2010-11-12 14:58:37 -0800599 /**
600 * Create a builder object.
601 *
602 * @param rs
603 */
Jason Sams22534172009-08-04 16:58:20 -0700604 public Builder(RenderScript rs) {
Jason Sams36e612a2009-07-31 16:26:13 -0700605 mRS = rs;
Jason Sams718cd1f2009-12-23 14:35:29 -0800606 mCount = 0;
607 mElements = new Element[8];
608 mElementNames = new String[8];
Jason Sams70d4e502010-09-02 17:35:23 -0700609 mArraySizes = new int[8];
Jason Sams36e612a2009-07-31 16:26:13 -0700610 }
611
Jason Samsa1b13ed2010-11-12 14:58:37 -0800612 /**
613 * Add an array of elements to this element.
614 *
615 * @param element
616 * @param name
617 * @param arraySize
618 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800619 public Builder add(Element element, String name, int arraySize) {
Jason Sams70d4e502010-09-02 17:35:23 -0700620 if (arraySize < 1) {
Jason Samsc1d62102010-11-04 14:32:19 -0700621 throw new RSIllegalArgumentException("Array size cannot be less than 1.");
Jason Sams70d4e502010-09-02 17:35:23 -0700622 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800623 if(mCount == mElements.length) {
624 Element[] e = new Element[mCount + 8];
625 String[] s = new String[mCount + 8];
Jason Sams70d4e502010-09-02 17:35:23 -0700626 int[] as = new int[mCount + 8];
Jason Sams718cd1f2009-12-23 14:35:29 -0800627 System.arraycopy(mElements, 0, e, 0, mCount);
628 System.arraycopy(mElementNames, 0, s, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -0700629 System.arraycopy(mArraySizes, 0, as, 0, mCount);
Jason Sams718cd1f2009-12-23 14:35:29 -0800630 mElements = e;
631 mElementNames = s;
Jason Sams70d4e502010-09-02 17:35:23 -0700632 mArraySizes = as;
Jason Sams36e612a2009-07-31 16:26:13 -0700633 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800634 mElements[mCount] = element;
635 mElementNames[mCount] = name;
Jason Sams70d4e502010-09-02 17:35:23 -0700636 mArraySizes[mCount] = arraySize;
Jason Sams718cd1f2009-12-23 14:35:29 -0800637 mCount++;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800638 return this;
Jason Sams07ae4062009-08-27 20:23:34 -0700639 }
640
Jason Samsa1b13ed2010-11-12 14:58:37 -0800641 /**
642 * Add a single element to this Element.
643 *
644 * @param element
645 * @param name
646 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800647 public Builder add(Element element, String name) {
648 return add(element, name, 1);
Jason Sams70d4e502010-09-02 17:35:23 -0700649 }
650
Jason Samsa1b13ed2010-11-12 14:58:37 -0800651 /**
652 * Create the element from this builder.
653 *
654 *
655 * @return Element
656 */
Jason Sams22534172009-08-04 16:58:20 -0700657 public Element create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800658 mRS.validate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800659 Element[] ein = new Element[mCount];
660 String[] sin = new String[mCount];
Jason Sams70d4e502010-09-02 17:35:23 -0700661 int[] asin = new int[mCount];
Jason Sams718cd1f2009-12-23 14:35:29 -0800662 java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
663 java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -0700664 java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700665
666 int[] ids = new int[ein.length];
667 for (int ct = 0; ct < ein.length; ct++ ) {
Jason Sams06d69de2010-11-09 17:11:40 -0800668 ids[ct] = ein[ct].getID();
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700669 }
Jason Sams70d4e502010-09-02 17:35:23 -0700670 int id = mRS.nElementCreate2(ids, sin, asin);
671 return new Element(id, mRS, ein, sin, asin);
Jason Sams36e612a2009-07-31 16:26:13 -0700672 }
673 }
Jason Sams36e612a2009-07-31 16:26:13 -0700674}
675