blob: d76c20965c9f673c08d2110d1dbb1ff59c5edfe3 [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
Stephen Hinesf257e512011-06-14 14:54:29 -070035 * alignment is determined by the most basic primitive type. i.e. a float4
36 * vector will be aligned to sizeof(float) and not sizeof(float4). The
Jason Samsa1b13ed2010-11-12 14:58:37 -080037 * 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;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070049 int[] mOffsetInBytes;
Jason Sams36e612a2009-07-31 16:26:13 -070050
Jason Sams718cd1f2009-12-23 14:35:29 -080051 DataType mType;
52 DataKind mKind;
53 boolean mNormalized;
54 int mVectorSize;
Jason Sams768bc022009-09-21 19:41:04 -070055
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070056 /**
57 * @hide
58 * @return element size in bytes
59 */
60 public int getSizeBytes() {return mSize;}
Jason Sams36e612a2009-07-31 16:26:13 -070061
Jason Samsa1b13ed2010-11-12 14:58:37 -080062
63 /**
64 * DataType represents the basic type information for a basic element. The
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -080065 * naming convention follows. For numeric types it is FLOAT,
66 * SIGNED, or UNSIGNED followed by the _BITS where BITS is the
67 * size of the data. BOOLEAN is a true / false (1,0)
68 * represented in an 8 bit container. The UNSIGNED variants
69 * with multiple bit definitions are for packed graphical data
70 * formats and represent vectors with per vector member sizes
71 * which are treated as a single unit for packing and alignment
72 * purposes.
Jason Samsa1b13ed2010-11-12 14:58:37 -080073 *
74 * MATRIX the three matrix types contain FLOAT_32 elements and are treated
75 * as 32 bits for alignment purposes.
76 *
77 * RS_* objects. 32 bit opaque handles.
78 */
Jason Sams36e612a2009-07-31 16:26:13 -070079 public enum DataType {
Jason Sams718cd1f2009-12-23 14:35:29 -080080 //FLOAT_16 (1, 2),
81 FLOAT_32 (2, 4),
Stephen Hines02f417052010-09-30 15:19:22 -070082 FLOAT_64 (3, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -080083 SIGNED_8 (4, 1),
84 SIGNED_16 (5, 2),
85 SIGNED_32 (6, 4),
Stephen Hinesef1dac22010-10-01 15:39:33 -070086 SIGNED_64 (7, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -080087 UNSIGNED_8 (8, 1),
88 UNSIGNED_16 (9, 2),
89 UNSIGNED_32 (10, 4),
Stephen Hines52d83632010-10-11 16:10:42 -070090 UNSIGNED_64 (11, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -080091
Jason Samsf110d4b2010-06-21 17:42:41 -070092 BOOLEAN(12, 1),
Jason Sams718cd1f2009-12-23 14:35:29 -080093
Jason Samsf110d4b2010-06-21 17:42:41 -070094 UNSIGNED_5_6_5 (13, 2),
95 UNSIGNED_5_5_5_1 (14, 2),
96 UNSIGNED_4_4_4_4 (15, 2),
97
Jason Sams1d45c472010-08-25 14:31:48 -070098 MATRIX_4X4 (16, 64),
99 MATRIX_3X3 (17, 36),
100 MATRIX_2X2 (18, 16),
101
102 RS_ELEMENT (1000, 4),
103 RS_TYPE (1001, 4),
104 RS_ALLOCATION (1002, 4),
105 RS_SAMPLER (1003, 4),
106 RS_SCRIPT (1004, 4),
107 RS_MESH (1005, 4),
108 RS_PROGRAM_FRAGMENT (1006, 4),
109 RS_PROGRAM_VERTEX (1007, 4),
110 RS_PROGRAM_RASTER (1008, 4),
111 RS_PROGRAM_STORE (1009, 4);
Jason Sams36e612a2009-07-31 16:26:13 -0700112
113 int mID;
Jason Sams718cd1f2009-12-23 14:35:29 -0800114 int mSize;
115 DataType(int id, int size) {
Jason Sams36e612a2009-07-31 16:26:13 -0700116 mID = id;
Jason Sams718cd1f2009-12-23 14:35:29 -0800117 mSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700118 }
119 }
120
Jason Samsa1b13ed2010-11-12 14:58:37 -0800121 /**
122 * The special interpretation of the data if required. This is primarly
123 * useful for graphical data. USER indicates no special interpretation is
124 * expected. PIXEL is used in conjunction with the standard data types for
125 * representing texture formats.
126 */
Jason Sams36e612a2009-07-31 16:26:13 -0700127 public enum DataKind {
128 USER (0),
Jason Sams718cd1f2009-12-23 14:35:29 -0800129
130 PIXEL_L (7),
131 PIXEL_A (8),
132 PIXEL_LA (9),
133 PIXEL_RGB (10),
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700134 PIXEL_RGBA (11),
135 PIXEL_DEPTH (12);
Jason Sams36e612a2009-07-31 16:26:13 -0700136
137 int mID;
138 DataKind(int id) {
139 mID = id;
140 }
141 }
142
Jason Samsa1b13ed2010-11-12 14:58:37 -0800143 /**
144 * Return if a element is too complex for use as a data source for a Mesh or
145 * a Program.
146 *
147 * @return boolean
148 */
Jason Samsc1d62102010-11-04 14:32:19 -0700149 public boolean isComplex() {
150 if (mElements == null) {
151 return false;
152 }
153 for (int ct=0; ct < mElements.length; ct++) {
154 if (mElements[ct].mElements != null) {
155 return true;
156 }
157 }
158 return false;
159 }
160
Jason Samsa1b13ed2010-11-12 14:58:37 -0800161 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700162 * @hide
163 * @return number of sub-elements in this element
164 */
165 public int getSubElementCount() {
166 if (mElements == null) {
167 return 0;
168 }
169 return mElements.length;
170 }
171
172 /**
173 * @hide
174 * @param index index of the sub-element to return
175 * @return sub-element in this element at given index
176 */
177 public Element getSubElement(int index) {
178 if (mElements == null) {
179 throw new RSIllegalArgumentException("Element contains no sub-elements");
180 }
181 if (index < 0 || index >= mElements.length) {
182 throw new RSIllegalArgumentException("Illegal sub-element index");
183 }
184 return mElements[index];
185 }
186
187 /**
188 * @hide
189 * @param index index of the sub-element
190 * @return sub-element in this element at given index
191 */
192 public String getSubElementName(int index) {
193 if (mElements == null) {
194 throw new RSIllegalArgumentException("Element contains no sub-elements");
195 }
196 if (index < 0 || index >= mElements.length) {
197 throw new RSIllegalArgumentException("Illegal sub-element index");
198 }
199 return mElementNames[index];
200 }
201
202 /**
203 * @hide
204 * @param index index of the sub-element
205 * @return array size of sub-element in this element at given index
206 */
207 public int getSubElementArraySize(int index) {
208 if (mElements == null) {
209 throw new RSIllegalArgumentException("Element contains no sub-elements");
210 }
211 if (index < 0 || index >= mElements.length) {
212 throw new RSIllegalArgumentException("Illegal sub-element index");
213 }
214 return mArraySizes[index];
215 }
216
217 /**
218 * @hide
219 * @param index index of the sub-element
220 * @return offset in bytes of sub-element in this element at given index
221 */
222 public int getSubElementOffsetBytes(int index) {
223 if (mElements == null) {
224 throw new RSIllegalArgumentException("Element contains no sub-elements");
225 }
226 if (index < 0 || index >= mElements.length) {
227 throw new RSIllegalArgumentException("Illegal sub-element index");
228 }
229 return mOffsetInBytes[index];
230 }
231
232 /**
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -0800233 * @hide
234 * @return element data type
235 */
236 public DataType getDataType() {
237 return mType;
238 }
239
240 /**
241 * @hide
242 * @return element data kind
243 */
244 public DataKind getDataKind() {
245 return mKind;
246 }
247
248 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800249 * Utility function for returning an Element containing a single Boolean.
250 *
251 * @param rs Context to which the element will belong.
252 *
253 * @return Element
254 */
Jason Samsf110d4b2010-06-21 17:42:41 -0700255 public static Element BOOLEAN(RenderScript rs) {
256 if(rs.mElement_BOOLEAN == null) {
257 rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN);
258 }
259 return rs.mElement_BOOLEAN;
260 }
261
Jason Samsa1b13ed2010-11-12 14:58:37 -0800262 /**
263 * Utility function for returning an Element containing a single UNSIGNED_8.
264 *
265 * @param rs Context to which the element will belong.
266 *
267 * @return Element
268 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700269 public static Element U8(RenderScript rs) {
270 if(rs.mElement_U8 == null) {
271 rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800272 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700273 return rs.mElement_U8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800274 }
275
Jason Samsa1b13ed2010-11-12 14:58:37 -0800276 /**
277 * Utility function for returning an Element containing a single SIGNED_8.
278 *
279 * @param rs Context to which the element will belong.
280 *
281 * @return Element
282 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700283 public static Element I8(RenderScript rs) {
284 if(rs.mElement_I8 == null) {
285 rs.mElement_I8 = createUser(rs, DataType.SIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800286 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700287 return rs.mElement_I8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800288 }
289
Jason Samse29f3e72010-06-08 15:40:48 -0700290 public static Element U16(RenderScript rs) {
291 if(rs.mElement_U16 == null) {
292 rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16);
293 }
294 return rs.mElement_U16;
295 }
296
297 public static Element I16(RenderScript rs) {
298 if(rs.mElement_I16 == null) {
299 rs.mElement_I16 = createUser(rs, DataType.SIGNED_16);
300 }
301 return rs.mElement_I16;
302 }
303
Jason Sams8cb39de2010-06-01 15:47:01 -0700304 public static Element U32(RenderScript rs) {
305 if(rs.mElement_U32 == null) {
306 rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800307 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700308 return rs.mElement_U32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800309 }
310
Jason Sams8cb39de2010-06-01 15:47:01 -0700311 public static Element I32(RenderScript rs) {
312 if(rs.mElement_I32 == null) {
313 rs.mElement_I32 = createUser(rs, DataType.SIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800314 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700315 return rs.mElement_I32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800316 }
317
Stephen Hines52d83632010-10-11 16:10:42 -0700318 public static Element U64(RenderScript rs) {
319 if(rs.mElement_U64 == null) {
320 rs.mElement_U64 = createUser(rs, DataType.UNSIGNED_64);
321 }
322 return rs.mElement_U64;
323 }
324
Stephen Hinesef1dac22010-10-01 15:39:33 -0700325 public static Element I64(RenderScript rs) {
326 if(rs.mElement_I64 == null) {
327 rs.mElement_I64 = createUser(rs, DataType.SIGNED_64);
328 }
329 return rs.mElement_I64;
330 }
331
Jason Sams8cb39de2010-06-01 15:47:01 -0700332 public static Element F32(RenderScript rs) {
333 if(rs.mElement_F32 == null) {
334 rs.mElement_F32 = createUser(rs, DataType.FLOAT_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800335 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700336 return rs.mElement_F32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800337 }
338
Stephen Hines02f417052010-09-30 15:19:22 -0700339 public static Element F64(RenderScript rs) {
340 if(rs.mElement_F64 == null) {
341 rs.mElement_F64 = createUser(rs, DataType.FLOAT_64);
342 }
343 return rs.mElement_F64;
344 }
345
Jason Sams8cb39de2010-06-01 15:47:01 -0700346 public static Element ELEMENT(RenderScript rs) {
347 if(rs.mElement_ELEMENT == null) {
348 rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700349 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700350 return rs.mElement_ELEMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700351 }
352
Jason Sams8cb39de2010-06-01 15:47:01 -0700353 public static Element TYPE(RenderScript rs) {
354 if(rs.mElement_TYPE == null) {
355 rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE);
Jason Samsa70f4162010-03-26 15:33:42 -0700356 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700357 return rs.mElement_TYPE;
Jason Samsa70f4162010-03-26 15:33:42 -0700358 }
359
Jason Sams8cb39de2010-06-01 15:47:01 -0700360 public static Element ALLOCATION(RenderScript rs) {
361 if(rs.mElement_ALLOCATION == null) {
362 rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION);
Jason Samsa70f4162010-03-26 15:33:42 -0700363 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700364 return rs.mElement_ALLOCATION;
Jason Samsa70f4162010-03-26 15:33:42 -0700365 }
366
Jason Sams8cb39de2010-06-01 15:47:01 -0700367 public static Element SAMPLER(RenderScript rs) {
368 if(rs.mElement_SAMPLER == null) {
369 rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER);
Jason Samsa70f4162010-03-26 15:33:42 -0700370 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700371 return rs.mElement_SAMPLER;
Jason Samsa70f4162010-03-26 15:33:42 -0700372 }
373
Jason Sams8cb39de2010-06-01 15:47:01 -0700374 public static Element SCRIPT(RenderScript rs) {
375 if(rs.mElement_SCRIPT == null) {
376 rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT);
Jason Samsa70f4162010-03-26 15:33:42 -0700377 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700378 return rs.mElement_SCRIPT;
Jason Samsa70f4162010-03-26 15:33:42 -0700379 }
380
Jason Sams8cb39de2010-06-01 15:47:01 -0700381 public static Element MESH(RenderScript rs) {
382 if(rs.mElement_MESH == null) {
383 rs.mElement_MESH = createUser(rs, DataType.RS_MESH);
Jason Samsa70f4162010-03-26 15:33:42 -0700384 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700385 return rs.mElement_MESH;
Jason Samsa70f4162010-03-26 15:33:42 -0700386 }
387
Jason Sams8cb39de2010-06-01 15:47:01 -0700388 public static Element PROGRAM_FRAGMENT(RenderScript rs) {
389 if(rs.mElement_PROGRAM_FRAGMENT == null) {
390 rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700391 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700392 return rs.mElement_PROGRAM_FRAGMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700393 }
394
Jason Sams8cb39de2010-06-01 15:47:01 -0700395 public static Element PROGRAM_VERTEX(RenderScript rs) {
396 if(rs.mElement_PROGRAM_VERTEX == null) {
397 rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX);
Jason Samsa70f4162010-03-26 15:33:42 -0700398 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700399 return rs.mElement_PROGRAM_VERTEX;
Jason Samsa70f4162010-03-26 15:33:42 -0700400 }
401
Jason Sams8cb39de2010-06-01 15:47:01 -0700402 public static Element PROGRAM_RASTER(RenderScript rs) {
403 if(rs.mElement_PROGRAM_RASTER == null) {
404 rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER);
Jason Samsa70f4162010-03-26 15:33:42 -0700405 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700406 return rs.mElement_PROGRAM_RASTER;
Jason Samsa70f4162010-03-26 15:33:42 -0700407 }
408
Jason Sams8cb39de2010-06-01 15:47:01 -0700409 public static Element PROGRAM_STORE(RenderScript rs) {
410 if(rs.mElement_PROGRAM_STORE == null) {
411 rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE);
Jason Samsa70f4162010-03-26 15:33:42 -0700412 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700413 return rs.mElement_PROGRAM_STORE;
Jason Samsa70f4162010-03-26 15:33:42 -0700414 }
415
416
Jason Sams718cd1f2009-12-23 14:35:29 -0800417 public static Element A_8(RenderScript rs) {
418 if(rs.mElement_A_8 == null) {
419 rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
420 }
421 return rs.mElement_A_8;
422 }
423
424 public static Element RGB_565(RenderScript rs) {
425 if(rs.mElement_RGB_565 == null) {
426 rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
427 }
428 return rs.mElement_RGB_565;
429 }
430
431 public static Element RGB_888(RenderScript rs) {
432 if(rs.mElement_RGB_888 == null) {
433 rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
434 }
435 return rs.mElement_RGB_888;
436 }
437
438 public static Element RGBA_5551(RenderScript rs) {
439 if(rs.mElement_RGBA_5551 == null) {
440 rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
441 }
442 return rs.mElement_RGBA_5551;
443 }
444
445 public static Element RGBA_4444(RenderScript rs) {
446 if(rs.mElement_RGBA_4444 == null) {
447 rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
448 }
449 return rs.mElement_RGBA_4444;
450 }
451
452 public static Element RGBA_8888(RenderScript rs) {
453 if(rs.mElement_RGBA_8888 == null) {
454 rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
455 }
456 return rs.mElement_RGBA_8888;
457 }
458
Jason Sams8cb39de2010-06-01 15:47:01 -0700459 public static Element F32_2(RenderScript rs) {
460 if(rs.mElement_FLOAT_2 == null) {
461 rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2);
Jason Sams718cd1f2009-12-23 14:35:29 -0800462 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700463 return rs.mElement_FLOAT_2;
Jason Sams718cd1f2009-12-23 14:35:29 -0800464 }
465
Jason Sams8cb39de2010-06-01 15:47:01 -0700466 public static Element F32_3(RenderScript rs) {
467 if(rs.mElement_FLOAT_3 == null) {
468 rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3);
Jason Sams718cd1f2009-12-23 14:35:29 -0800469 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700470 return rs.mElement_FLOAT_3;
Jason Sams718cd1f2009-12-23 14:35:29 -0800471 }
472
Jason Sams8cb39de2010-06-01 15:47:01 -0700473 public static Element F32_4(RenderScript rs) {
474 if(rs.mElement_FLOAT_4 == null) {
475 rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800476 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700477 return rs.mElement_FLOAT_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800478 }
479
Stephen Hines836c4a52011-06-01 14:38:10 -0700480 public static Element F64_2(RenderScript rs) {
481 if(rs.mElement_DOUBLE_2 == null) {
482 rs.mElement_DOUBLE_2 = createVector(rs, DataType.FLOAT_64, 2);
483 }
484 return rs.mElement_DOUBLE_2;
485 }
486
487 public static Element F64_3(RenderScript rs) {
488 if(rs.mElement_DOUBLE_3 == null) {
489 rs.mElement_DOUBLE_3 = createVector(rs, DataType.FLOAT_64, 3);
490 }
491 return rs.mElement_DOUBLE_3;
492 }
493
494 public static Element F64_4(RenderScript rs) {
495 if(rs.mElement_DOUBLE_4 == null) {
496 rs.mElement_DOUBLE_4 = createVector(rs, DataType.FLOAT_64, 4);
497 }
498 return rs.mElement_DOUBLE_4;
499 }
500
501 public static Element U8_2(RenderScript rs) {
502 if(rs.mElement_UCHAR_2 == null) {
503 rs.mElement_UCHAR_2 = createVector(rs, DataType.UNSIGNED_8, 2);
504 }
505 return rs.mElement_UCHAR_2;
506 }
507
508 public static Element U8_3(RenderScript rs) {
509 if(rs.mElement_UCHAR_3 == null) {
510 rs.mElement_UCHAR_3 = createVector(rs, DataType.UNSIGNED_8, 3);
511 }
512 return rs.mElement_UCHAR_3;
513 }
514
Jason Sams8cb39de2010-06-01 15:47:01 -0700515 public static Element U8_4(RenderScript rs) {
516 if(rs.mElement_UCHAR_4 == null) {
517 rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800518 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700519 return rs.mElement_UCHAR_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800520 }
521
Stephen Hines836c4a52011-06-01 14:38:10 -0700522 public static Element I8_2(RenderScript rs) {
523 if(rs.mElement_CHAR_2 == null) {
524 rs.mElement_CHAR_2 = createVector(rs, DataType.SIGNED_8, 2);
525 }
526 return rs.mElement_CHAR_2;
527 }
528
529 public static Element I8_3(RenderScript rs) {
530 if(rs.mElement_CHAR_3 == null) {
531 rs.mElement_CHAR_3 = createVector(rs, DataType.SIGNED_8, 3);
532 }
533 return rs.mElement_CHAR_3;
534 }
535
536 public static Element I8_4(RenderScript rs) {
537 if(rs.mElement_CHAR_4 == null) {
538 rs.mElement_CHAR_4 = createVector(rs, DataType.SIGNED_8, 4);
539 }
540 return rs.mElement_CHAR_4;
541 }
542
543 public static Element U16_2(RenderScript rs) {
544 if(rs.mElement_USHORT_2 == null) {
545 rs.mElement_USHORT_2 = createVector(rs, DataType.UNSIGNED_16, 2);
546 }
547 return rs.mElement_USHORT_2;
548 }
549
550 public static Element U16_3(RenderScript rs) {
551 if(rs.mElement_USHORT_3 == null) {
552 rs.mElement_USHORT_3 = createVector(rs, DataType.UNSIGNED_16, 3);
553 }
554 return rs.mElement_USHORT_3;
555 }
556
557 public static Element U16_4(RenderScript rs) {
558 if(rs.mElement_USHORT_4 == null) {
559 rs.mElement_USHORT_4 = createVector(rs, DataType.UNSIGNED_16, 4);
560 }
561 return rs.mElement_USHORT_4;
562 }
563
564 public static Element I16_2(RenderScript rs) {
565 if(rs.mElement_SHORT_2 == null) {
566 rs.mElement_SHORT_2 = createVector(rs, DataType.SIGNED_16, 2);
567 }
568 return rs.mElement_SHORT_2;
569 }
570
571 public static Element I16_3(RenderScript rs) {
572 if(rs.mElement_SHORT_3 == null) {
573 rs.mElement_SHORT_3 = createVector(rs, DataType.SIGNED_16, 3);
574 }
575 return rs.mElement_SHORT_3;
576 }
577
578 public static Element I16_4(RenderScript rs) {
579 if(rs.mElement_SHORT_4 == null) {
580 rs.mElement_SHORT_4 = createVector(rs, DataType.SIGNED_16, 4);
581 }
582 return rs.mElement_SHORT_4;
583 }
584
585 public static Element U32_2(RenderScript rs) {
586 if(rs.mElement_UINT_2 == null) {
587 rs.mElement_UINT_2 = createVector(rs, DataType.UNSIGNED_32, 2);
588 }
589 return rs.mElement_UINT_2;
590 }
591
592 public static Element U32_3(RenderScript rs) {
593 if(rs.mElement_UINT_3 == null) {
594 rs.mElement_UINT_3 = createVector(rs, DataType.UNSIGNED_32, 3);
595 }
596 return rs.mElement_UINT_3;
597 }
598
599 public static Element U32_4(RenderScript rs) {
600 if(rs.mElement_UINT_4 == null) {
601 rs.mElement_UINT_4 = createVector(rs, DataType.UNSIGNED_32, 4);
602 }
603 return rs.mElement_UINT_4;
604 }
605
606 public static Element I32_2(RenderScript rs) {
607 if(rs.mElement_INT_2 == null) {
608 rs.mElement_INT_2 = createVector(rs, DataType.SIGNED_32, 2);
609 }
610 return rs.mElement_INT_2;
611 }
612
613 public static Element I32_3(RenderScript rs) {
614 if(rs.mElement_INT_3 == null) {
615 rs.mElement_INT_3 = createVector(rs, DataType.SIGNED_32, 3);
616 }
617 return rs.mElement_INT_3;
618 }
619
620 public static Element I32_4(RenderScript rs) {
621 if(rs.mElement_INT_4 == null) {
622 rs.mElement_INT_4 = createVector(rs, DataType.SIGNED_32, 4);
623 }
624 return rs.mElement_INT_4;
625 }
626
627 public static Element U64_2(RenderScript rs) {
628 if(rs.mElement_ULONG_2 == null) {
629 rs.mElement_ULONG_2 = createVector(rs, DataType.UNSIGNED_64, 2);
630 }
631 return rs.mElement_ULONG_2;
632 }
633
634 public static Element U64_3(RenderScript rs) {
635 if(rs.mElement_ULONG_3 == null) {
636 rs.mElement_ULONG_3 = createVector(rs, DataType.UNSIGNED_64, 3);
637 }
638 return rs.mElement_ULONG_3;
639 }
640
641 public static Element U64_4(RenderScript rs) {
642 if(rs.mElement_ULONG_4 == null) {
643 rs.mElement_ULONG_4 = createVector(rs, DataType.UNSIGNED_64, 4);
644 }
645 return rs.mElement_ULONG_4;
646 }
647
648 public static Element I64_2(RenderScript rs) {
649 if(rs.mElement_LONG_2 == null) {
650 rs.mElement_LONG_2 = createVector(rs, DataType.SIGNED_64, 2);
651 }
652 return rs.mElement_LONG_2;
653 }
654
655 public static Element I64_3(RenderScript rs) {
656 if(rs.mElement_LONG_3 == null) {
657 rs.mElement_LONG_3 = createVector(rs, DataType.SIGNED_64, 3);
658 }
659 return rs.mElement_LONG_3;
660 }
661
662 public static Element I64_4(RenderScript rs) {
663 if(rs.mElement_LONG_4 == null) {
664 rs.mElement_LONG_4 = createVector(rs, DataType.SIGNED_64, 4);
665 }
666 return rs.mElement_LONG_4;
667 }
668
Jason Sams1d45c472010-08-25 14:31:48 -0700669 public static Element MATRIX_4X4(RenderScript rs) {
670 if(rs.mElement_MATRIX_4X4 == null) {
671 rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4);
672 }
673 return rs.mElement_MATRIX_4X4;
674 }
675 public static Element MATRIX4X4(RenderScript rs) {
676 return MATRIX_4X4(rs);
677 }
678
679 public static Element MATRIX_3X3(RenderScript rs) {
680 if(rs.mElement_MATRIX_3X3 == null) {
681 rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3);
682 }
Alex Sakhartchouk34769772011-02-28 16:01:28 -0800683 return rs.mElement_MATRIX_3X3;
Jason Sams1d45c472010-08-25 14:31:48 -0700684 }
685
686 public static Element MATRIX_2X2(RenderScript rs) {
687 if(rs.mElement_MATRIX_2X2 == null) {
688 rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2);
689 }
690 return rs.mElement_MATRIX_2X2;
691 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800692
Jason Sams70d4e502010-09-02 17:35:23 -0700693 Element(int id, RenderScript rs, Element[] e, String[] n, int[] as) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700694 super(id, rs);
Jason Samsea84a7c2009-09-04 14:42:41 -0700695 mSize = 0;
Jason Sams718cd1f2009-12-23 14:35:29 -0800696 mElements = e;
697 mElementNames = n;
Jason Sams70d4e502010-09-02 17:35:23 -0700698 mArraySizes = as;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700699 mOffsetInBytes = new int[mElements.length];
Jason Sams718cd1f2009-12-23 14:35:29 -0800700 for (int ct = 0; ct < mElements.length; ct++ ) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700701 mOffsetInBytes[ct] = mSize;
Alex Sakhartchouk9e401bc2010-10-13 14:22:02 -0700702 mSize += mElements[ct].mSize * mArraySizes[ct];
Jason Sams718cd1f2009-12-23 14:35:29 -0800703 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800704 }
705
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700706 Element(int id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
707 super(id, rs);
Jason Sams252c0782011-01-11 17:42:52 -0800708 if ((dt != DataType.UNSIGNED_5_6_5) &&
709 (dt != DataType.UNSIGNED_4_4_4_4) &&
710 (dt != DataType.UNSIGNED_5_5_5_1)) {
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800711 if (size == 3) {
712 mSize = dt.mSize * 4;
713 } else {
714 mSize = dt.mSize * size;
715 }
Jason Sams252c0782011-01-11 17:42:52 -0800716 } else {
717 mSize = dt.mSize;
718 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800719 mType = dt;
720 mKind = dk;
721 mNormalized = norm;
722 mVectorSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700723 }
724
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700725 Element(int id, RenderScript rs) {
726 super(id, rs);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700727 }
728
729 @Override
730 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800731 super.updateFromNative();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700732
733 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
734 int[] dataBuffer = new int[5];
Jason Sams06d69de2010-11-09 17:11:40 -0800735 mRS.nElementGetNativeData(getID(), dataBuffer);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700736
737 mNormalized = dataBuffer[2] == 1 ? true : false;
738 mVectorSize = dataBuffer[3];
739 mSize = 0;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700740 for (DataType dt: DataType.values()) {
741 if(dt.mID == dataBuffer[0]){
742 mType = dt;
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700743 mSize = mType.mSize * mVectorSize;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700744 }
745 }
746 for (DataKind dk: DataKind.values()) {
747 if(dk.mID == dataBuffer[1]){
748 mKind = dk;
749 }
750 }
751
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700752 int numSubElements = dataBuffer[4];
753 if(numSubElements > 0) {
754 mElements = new Element[numSubElements];
755 mElementNames = new String[numSubElements];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700756 mArraySizes = new int[numSubElements];
757 mOffsetInBytes = new int[numSubElements];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700758
759 int[] subElementIds = new int[numSubElements];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700760 mRS.nElementGetSubElements(getID(), subElementIds, mElementNames, mArraySizes);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700761 for(int i = 0; i < numSubElements; i ++) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700762 mElements[i] = new Element(subElementIds[i], mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700763 mElements[i].updateFromNative();
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700764 mOffsetInBytes[i] = mSize;
765 mSize += mElements[i].mSize * mArraySizes[i];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700766 }
767 }
768
769 }
770
Jason Samsa1b13ed2010-11-12 14:58:37 -0800771 /**
772 * Create a custom Element of the specified DataType. The DataKind will be
773 * set to USER and the vector size to 1 indicating non-vector.
774 *
775 * @param rs The context associated with the new Element.
776 * @param dt The DataType for the new element.
777 * @return Element
778 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800779 static Element createUser(RenderScript rs, DataType dt) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700780 DataKind dk = DataKind.USER;
781 boolean norm = false;
782 int vecSize = 1;
783 int id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize);
784 return new Element(id, rs, dt, dk, norm, vecSize);
Jason Sams718cd1f2009-12-23 14:35:29 -0800785 }
786
Jason Samsa1b13ed2010-11-12 14:58:37 -0800787 /**
788 * Create a custom vector element of the specified DataType and vector size.
789 * DataKind will be set to USER.
790 *
791 * @param rs The context associated with the new Element.
792 * @param dt The DataType for the new element.
793 * @param size Vector size for the new Element. Range 2-4 inclusive
794 * supported.
795 *
796 * @return Element
797 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800798 public static Element createVector(RenderScript rs, DataType dt, int size) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800799 if (size < 2 || size > 4) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800800 throw new RSIllegalArgumentException("Vector size out of range 2-4.");
Jason Samsea84a7c2009-09-04 14:42:41 -0700801 }
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700802 DataKind dk = DataKind.USER;
803 boolean norm = false;
804 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
805 return new Element(id, rs, dt, dk, norm, size);
Jason Samsea84a7c2009-09-04 14:42:41 -0700806 }
807
Jason Samsa1b13ed2010-11-12 14:58:37 -0800808 /**
809 * Create a new pixel Element type. A matching DataType and DataKind must
810 * be provided. The DataType and DataKind must contain the same number of
811 * components. Vector size will be set to 1.
812 *
813 * @param rs The context associated with the new Element.
814 * @param dt The DataType for the new element.
815 * @param dk The DataKind to specify the mapping of each component in the
816 * DataType.
817 *
818 * @return Element
819 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800820 public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800821 if (!(dk == DataKind.PIXEL_L ||
822 dk == DataKind.PIXEL_A ||
823 dk == DataKind.PIXEL_LA ||
824 dk == DataKind.PIXEL_RGB ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700825 dk == DataKind.PIXEL_RGBA ||
826 dk == DataKind.PIXEL_DEPTH)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700827 throw new RSIllegalArgumentException("Unsupported DataKind");
Jason Sams718cd1f2009-12-23 14:35:29 -0800828 }
829 if (!(dt == DataType.UNSIGNED_8 ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700830 dt == DataType.UNSIGNED_16 ||
Jason Sams718cd1f2009-12-23 14:35:29 -0800831 dt == DataType.UNSIGNED_5_6_5 ||
832 dt == DataType.UNSIGNED_4_4_4_4 ||
833 dt == DataType.UNSIGNED_5_5_5_1)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700834 throw new RSIllegalArgumentException("Unsupported DataType");
Jason Sams718cd1f2009-12-23 14:35:29 -0800835 }
836 if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
Jason Samsc1d62102010-11-04 14:32:19 -0700837 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800838 }
839 if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700840 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800841 }
842 if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700843 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800844 }
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700845 if (dt == DataType.UNSIGNED_16 &&
846 dk != DataKind.PIXEL_DEPTH) {
847 throw new RSIllegalArgumentException("Bad kind and type combo");
848 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800849
850 int size = 1;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700851 switch (dk) {
852 case PIXEL_LA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800853 size = 2;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700854 break;
855 case PIXEL_RGB:
Jason Sams718cd1f2009-12-23 14:35:29 -0800856 size = 3;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700857 break;
858 case PIXEL_RGBA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800859 size = 4;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700860 break;
861 case PIXEL_DEPTH:
862 size = 2;
863 break;
Jason Sams718cd1f2009-12-23 14:35:29 -0800864 }
865
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700866 boolean norm = true;
867 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
868 return new Element(id, rs, dt, dk, norm, size);
Jason Sams718cd1f2009-12-23 14:35:29 -0800869 }
Jason Sams36e612a2009-07-31 16:26:13 -0700870
Jason Samsa1b13ed2010-11-12 14:58:37 -0800871 /**
Stephen Hinesf257e512011-06-14 14:54:29 -0700872 * Check if the current Element is compatible with another Element.
873 * Primitive Elements are compatible if they share the same underlying
874 * size and type (i.e. U8 is compatible with A_8). User-defined Elements
875 * must be equal in order to be compatible. This requires strict name
876 * equivalence for all sub-Elements (in addition to structural equivalence).
877 *
878 * @param e The Element to check compatibility with.
879 *
880 * @return boolean true if the Elements are compatible, otherwise false.
881 */
882 public boolean isCompatible(Element e) {
883 // Try strict BaseObj equality to start with.
884 if (this.equals(e)) {
885 return true;
886 }
887
888 // Ignore mKind because it is allowed to be different (user vs. pixel).
889 // We also ignore mNormalized because it can be different. The mType
890 // field must be non-null since we require name equivalence for
891 // user-created Elements.
892 return ((mSize == e.mSize) &&
893 (mType != null) &&
894 (mType == e.mType) &&
895 (mVectorSize == e.mVectorSize));
896 }
897
898 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800899 * Builder class for producing complex elements with matching field and name
900 * pairs. The builder starts empty. The order in which elements are added
901 * is retained for the layout in memory.
902 *
903 */
Jason Sams36e612a2009-07-31 16:26:13 -0700904 public static class Builder {
905 RenderScript mRS;
Jason Sams718cd1f2009-12-23 14:35:29 -0800906 Element[] mElements;
907 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -0700908 int[] mArraySizes;
Jason Sams718cd1f2009-12-23 14:35:29 -0800909 int mCount;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800910 int mSkipPadding;
Jason Sams22534172009-08-04 16:58:20 -0700911
Jason Samsa1b13ed2010-11-12 14:58:37 -0800912 /**
913 * Create a builder object.
914 *
915 * @param rs
916 */
Jason Sams22534172009-08-04 16:58:20 -0700917 public Builder(RenderScript rs) {
Jason Sams36e612a2009-07-31 16:26:13 -0700918 mRS = rs;
Jason Sams718cd1f2009-12-23 14:35:29 -0800919 mCount = 0;
920 mElements = new Element[8];
921 mElementNames = new String[8];
Jason Sams70d4e502010-09-02 17:35:23 -0700922 mArraySizes = new int[8];
Jason Sams36e612a2009-07-31 16:26:13 -0700923 }
924
Jason Samsa1b13ed2010-11-12 14:58:37 -0800925 /**
926 * Add an array of elements to this element.
927 *
928 * @param element
929 * @param name
930 * @param arraySize
931 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800932 public Builder add(Element element, String name, int arraySize) {
Jason Sams70d4e502010-09-02 17:35:23 -0700933 if (arraySize < 1) {
Jason Samsc1d62102010-11-04 14:32:19 -0700934 throw new RSIllegalArgumentException("Array size cannot be less than 1.");
Jason Sams70d4e502010-09-02 17:35:23 -0700935 }
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800936
937 // Skip padding fields after a vector 3 type.
938 if (mSkipPadding != 0) {
939 if (name.startsWith("#padding_")) {
940 mSkipPadding = 0;
941 return this;
942 }
943 }
944
945 if (element.mVectorSize == 3) {
946 mSkipPadding = 1;
947 } else {
948 mSkipPadding = 0;
949 }
950
Jason Sams718cd1f2009-12-23 14:35:29 -0800951 if(mCount == mElements.length) {
952 Element[] e = new Element[mCount + 8];
953 String[] s = new String[mCount + 8];
Jason Sams70d4e502010-09-02 17:35:23 -0700954 int[] as = new int[mCount + 8];
Jason Sams718cd1f2009-12-23 14:35:29 -0800955 System.arraycopy(mElements, 0, e, 0, mCount);
956 System.arraycopy(mElementNames, 0, s, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -0700957 System.arraycopy(mArraySizes, 0, as, 0, mCount);
Jason Sams718cd1f2009-12-23 14:35:29 -0800958 mElements = e;
959 mElementNames = s;
Jason Sams70d4e502010-09-02 17:35:23 -0700960 mArraySizes = as;
Jason Sams36e612a2009-07-31 16:26:13 -0700961 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800962 mElements[mCount] = element;
963 mElementNames[mCount] = name;
Jason Sams70d4e502010-09-02 17:35:23 -0700964 mArraySizes[mCount] = arraySize;
Jason Sams718cd1f2009-12-23 14:35:29 -0800965 mCount++;
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800966 return this;
Jason Sams07ae4062009-08-27 20:23:34 -0700967 }
968
Jason Samsa1b13ed2010-11-12 14:58:37 -0800969 /**
970 * Add a single element to this Element.
971 *
972 * @param element
973 * @param name
974 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800975 public Builder add(Element element, String name) {
976 return add(element, name, 1);
Jason Sams70d4e502010-09-02 17:35:23 -0700977 }
978
Jason Samsa1b13ed2010-11-12 14:58:37 -0800979 /**
980 * Create the element from this builder.
981 *
982 *
983 * @return Element
984 */
Jason Sams22534172009-08-04 16:58:20 -0700985 public Element create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800986 mRS.validate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800987 Element[] ein = new Element[mCount];
988 String[] sin = new String[mCount];
Jason Sams70d4e502010-09-02 17:35:23 -0700989 int[] asin = new int[mCount];
Jason Sams718cd1f2009-12-23 14:35:29 -0800990 java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
991 java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -0700992 java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700993
994 int[] ids = new int[ein.length];
995 for (int ct = 0; ct < ein.length; ct++ ) {
Jason Sams06d69de2010-11-09 17:11:40 -0800996 ids[ct] = ein[ct].getID();
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700997 }
Jason Sams70d4e502010-09-02 17:35:23 -0700998 int id = mRS.nElementCreate2(ids, sin, asin);
999 return new Element(id, mRS, ein, sin, asin);
Jason Sams36e612a2009-07-31 16:26:13 -07001000 }
1001 }
Jason Sams36e612a2009-07-31 16:26:13 -07001002}
1003