blob: 22ef7bb08f47aae1c0bb5d1f9de5d7b5dabac804 [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>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080043 *
44 * <div class="special reference">
45 * <h3>Developer Guides</h3>
46 * <p>For more information about creating an application that uses Renderscript, read the
47 * <a href="{@docRoot}guide/topics/graphics/renderscript.html">Renderscript</a> developer guide.</p>
48 * </div>
Jason Sams36e612a2009-07-31 16:26:13 -070049 **/
50public class Element extends BaseObj {
Jason Samsea84a7c2009-09-04 14:42:41 -070051 int mSize;
Jason Sams718cd1f2009-12-23 14:35:29 -080052 Element[] mElements;
53 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -070054 int[] mArraySizes;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070055 int[] mOffsetInBytes;
Jason Sams36e612a2009-07-31 16:26:13 -070056
Jason Sams718cd1f2009-12-23 14:35:29 -080057 DataType mType;
58 DataKind mKind;
59 boolean mNormalized;
60 int mVectorSize;
Jason Sams768bc022009-09-21 19:41:04 -070061
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070062 /**
63 * @hide
64 * @return element size in bytes
65 */
66 public int getSizeBytes() {return mSize;}
Jason Sams36e612a2009-07-31 16:26:13 -070067
Jason Samsa1b13ed2010-11-12 14:58:37 -080068
69 /**
70 * DataType represents the basic type information for a basic element. The
71 * naming convention follows. For numeric types its FLOAT, SIGNED, UNSIGNED
72 * followed by the _BITS where BITS is the size of the data. BOOLEAN is a
73 * true / false (1,0) represented in an 8 bit container. The UNSIGNED
74 * variants with multiple bit definitions are for packed graphical data
75 * formats and represents vectors with per vector member sizes which are
76 * treated as a single unit for packing and alignment purposes.
77 *
78 * MATRIX the three matrix types contain FLOAT_32 elements and are treated
79 * as 32 bits for alignment purposes.
80 *
81 * RS_* objects. 32 bit opaque handles.
82 */
Jason Sams36e612a2009-07-31 16:26:13 -070083 public enum DataType {
Jason Sams718cd1f2009-12-23 14:35:29 -080084 //FLOAT_16 (1, 2),
85 FLOAT_32 (2, 4),
Stephen Hines02f417052010-09-30 15:19:22 -070086 FLOAT_64 (3, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -080087 SIGNED_8 (4, 1),
88 SIGNED_16 (5, 2),
89 SIGNED_32 (6, 4),
Stephen Hinesef1dac22010-10-01 15:39:33 -070090 SIGNED_64 (7, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -080091 UNSIGNED_8 (8, 1),
92 UNSIGNED_16 (9, 2),
93 UNSIGNED_32 (10, 4),
Stephen Hines52d83632010-10-11 16:10:42 -070094 UNSIGNED_64 (11, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -080095
Jason Samsf110d4b2010-06-21 17:42:41 -070096 BOOLEAN(12, 1),
Jason Sams718cd1f2009-12-23 14:35:29 -080097
Jason Samsf110d4b2010-06-21 17:42:41 -070098 UNSIGNED_5_6_5 (13, 2),
99 UNSIGNED_5_5_5_1 (14, 2),
100 UNSIGNED_4_4_4_4 (15, 2),
101
Jason Sams1d45c472010-08-25 14:31:48 -0700102 MATRIX_4X4 (16, 64),
103 MATRIX_3X3 (17, 36),
104 MATRIX_2X2 (18, 16),
105
106 RS_ELEMENT (1000, 4),
107 RS_TYPE (1001, 4),
108 RS_ALLOCATION (1002, 4),
109 RS_SAMPLER (1003, 4),
110 RS_SCRIPT (1004, 4),
111 RS_MESH (1005, 4),
112 RS_PROGRAM_FRAGMENT (1006, 4),
113 RS_PROGRAM_VERTEX (1007, 4),
114 RS_PROGRAM_RASTER (1008, 4),
115 RS_PROGRAM_STORE (1009, 4);
Jason Sams36e612a2009-07-31 16:26:13 -0700116
117 int mID;
Jason Sams718cd1f2009-12-23 14:35:29 -0800118 int mSize;
119 DataType(int id, int size) {
Jason Sams36e612a2009-07-31 16:26:13 -0700120 mID = id;
Jason Sams718cd1f2009-12-23 14:35:29 -0800121 mSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700122 }
123 }
124
Jason Samsa1b13ed2010-11-12 14:58:37 -0800125 /**
126 * The special interpretation of the data if required. This is primarly
127 * useful for graphical data. USER indicates no special interpretation is
128 * expected. PIXEL is used in conjunction with the standard data types for
129 * representing texture formats.
130 */
Jason Sams36e612a2009-07-31 16:26:13 -0700131 public enum DataKind {
132 USER (0),
Jason Sams718cd1f2009-12-23 14:35:29 -0800133
134 PIXEL_L (7),
135 PIXEL_A (8),
136 PIXEL_LA (9),
137 PIXEL_RGB (10),
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700138 PIXEL_RGBA (11),
139 PIXEL_DEPTH (12);
Jason Sams36e612a2009-07-31 16:26:13 -0700140
141 int mID;
142 DataKind(int id) {
143 mID = id;
144 }
145 }
146
Jason Samsa1b13ed2010-11-12 14:58:37 -0800147 /**
148 * Return if a element is too complex for use as a data source for a Mesh or
149 * a Program.
150 *
151 * @return boolean
152 */
Jason Samsc1d62102010-11-04 14:32:19 -0700153 public boolean isComplex() {
154 if (mElements == null) {
155 return false;
156 }
157 for (int ct=0; ct < mElements.length; ct++) {
158 if (mElements[ct].mElements != null) {
159 return true;
160 }
161 }
162 return false;
163 }
164
Jason Samsa1b13ed2010-11-12 14:58:37 -0800165 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700166 * @hide
167 * @return number of sub-elements in this element
168 */
169 public int getSubElementCount() {
170 if (mElements == null) {
171 return 0;
172 }
173 return mElements.length;
174 }
175
176 /**
177 * @hide
178 * @param index index of the sub-element to return
179 * @return sub-element in this element at given index
180 */
181 public Element getSubElement(int index) {
182 if (mElements == null) {
183 throw new RSIllegalArgumentException("Element contains no sub-elements");
184 }
185 if (index < 0 || index >= mElements.length) {
186 throw new RSIllegalArgumentException("Illegal sub-element index");
187 }
188 return mElements[index];
189 }
190
191 /**
192 * @hide
193 * @param index index of the sub-element
194 * @return sub-element in this element at given index
195 */
196 public String getSubElementName(int index) {
197 if (mElements == null) {
198 throw new RSIllegalArgumentException("Element contains no sub-elements");
199 }
200 if (index < 0 || index >= mElements.length) {
201 throw new RSIllegalArgumentException("Illegal sub-element index");
202 }
203 return mElementNames[index];
204 }
205
206 /**
207 * @hide
208 * @param index index of the sub-element
209 * @return array size of sub-element in this element at given index
210 */
211 public int getSubElementArraySize(int index) {
212 if (mElements == null) {
213 throw new RSIllegalArgumentException("Element contains no sub-elements");
214 }
215 if (index < 0 || index >= mElements.length) {
216 throw new RSIllegalArgumentException("Illegal sub-element index");
217 }
218 return mArraySizes[index];
219 }
220
221 /**
222 * @hide
223 * @param index index of the sub-element
224 * @return offset in bytes of sub-element in this element at given index
225 */
226 public int getSubElementOffsetBytes(int index) {
227 if (mElements == null) {
228 throw new RSIllegalArgumentException("Element contains no sub-elements");
229 }
230 if (index < 0 || index >= mElements.length) {
231 throw new RSIllegalArgumentException("Illegal sub-element index");
232 }
233 return mOffsetInBytes[index];
234 }
235
236 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800237 * Utility function for returning an Element containing a single Boolean.
238 *
239 * @param rs Context to which the element will belong.
240 *
241 * @return Element
242 */
Jason Samsf110d4b2010-06-21 17:42:41 -0700243 public static Element BOOLEAN(RenderScript rs) {
244 if(rs.mElement_BOOLEAN == null) {
245 rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN);
246 }
247 return rs.mElement_BOOLEAN;
248 }
249
Jason Samsa1b13ed2010-11-12 14:58:37 -0800250 /**
251 * Utility function for returning an Element containing a single UNSIGNED_8.
252 *
253 * @param rs Context to which the element will belong.
254 *
255 * @return Element
256 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700257 public static Element U8(RenderScript rs) {
258 if(rs.mElement_U8 == null) {
259 rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800260 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700261 return rs.mElement_U8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800262 }
263
Jason Samsa1b13ed2010-11-12 14:58:37 -0800264 /**
265 * Utility function for returning an Element containing a single SIGNED_8.
266 *
267 * @param rs Context to which the element will belong.
268 *
269 * @return Element
270 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700271 public static Element I8(RenderScript rs) {
272 if(rs.mElement_I8 == null) {
273 rs.mElement_I8 = createUser(rs, DataType.SIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800274 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700275 return rs.mElement_I8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800276 }
277
Jason Samse29f3e72010-06-08 15:40:48 -0700278 public static Element U16(RenderScript rs) {
279 if(rs.mElement_U16 == null) {
280 rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16);
281 }
282 return rs.mElement_U16;
283 }
284
285 public static Element I16(RenderScript rs) {
286 if(rs.mElement_I16 == null) {
287 rs.mElement_I16 = createUser(rs, DataType.SIGNED_16);
288 }
289 return rs.mElement_I16;
290 }
291
Jason Sams8cb39de2010-06-01 15:47:01 -0700292 public static Element U32(RenderScript rs) {
293 if(rs.mElement_U32 == null) {
294 rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800295 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700296 return rs.mElement_U32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800297 }
298
Jason Sams8cb39de2010-06-01 15:47:01 -0700299 public static Element I32(RenderScript rs) {
300 if(rs.mElement_I32 == null) {
301 rs.mElement_I32 = createUser(rs, DataType.SIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800302 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700303 return rs.mElement_I32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800304 }
305
Stephen Hines52d83632010-10-11 16:10:42 -0700306 public static Element U64(RenderScript rs) {
307 if(rs.mElement_U64 == null) {
308 rs.mElement_U64 = createUser(rs, DataType.UNSIGNED_64);
309 }
310 return rs.mElement_U64;
311 }
312
Stephen Hinesef1dac22010-10-01 15:39:33 -0700313 public static Element I64(RenderScript rs) {
314 if(rs.mElement_I64 == null) {
315 rs.mElement_I64 = createUser(rs, DataType.SIGNED_64);
316 }
317 return rs.mElement_I64;
318 }
319
Jason Sams8cb39de2010-06-01 15:47:01 -0700320 public static Element F32(RenderScript rs) {
321 if(rs.mElement_F32 == null) {
322 rs.mElement_F32 = createUser(rs, DataType.FLOAT_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800323 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700324 return rs.mElement_F32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800325 }
326
Stephen Hines02f417052010-09-30 15:19:22 -0700327 public static Element F64(RenderScript rs) {
328 if(rs.mElement_F64 == null) {
329 rs.mElement_F64 = createUser(rs, DataType.FLOAT_64);
330 }
331 return rs.mElement_F64;
332 }
333
Jason Sams8cb39de2010-06-01 15:47:01 -0700334 public static Element ELEMENT(RenderScript rs) {
335 if(rs.mElement_ELEMENT == null) {
336 rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700337 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700338 return rs.mElement_ELEMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700339 }
340
Jason Sams8cb39de2010-06-01 15:47:01 -0700341 public static Element TYPE(RenderScript rs) {
342 if(rs.mElement_TYPE == null) {
343 rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE);
Jason Samsa70f4162010-03-26 15:33:42 -0700344 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700345 return rs.mElement_TYPE;
Jason Samsa70f4162010-03-26 15:33:42 -0700346 }
347
Jason Sams8cb39de2010-06-01 15:47:01 -0700348 public static Element ALLOCATION(RenderScript rs) {
349 if(rs.mElement_ALLOCATION == null) {
350 rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION);
Jason Samsa70f4162010-03-26 15:33:42 -0700351 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700352 return rs.mElement_ALLOCATION;
Jason Samsa70f4162010-03-26 15:33:42 -0700353 }
354
Jason Sams8cb39de2010-06-01 15:47:01 -0700355 public static Element SAMPLER(RenderScript rs) {
356 if(rs.mElement_SAMPLER == null) {
357 rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER);
Jason Samsa70f4162010-03-26 15:33:42 -0700358 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700359 return rs.mElement_SAMPLER;
Jason Samsa70f4162010-03-26 15:33:42 -0700360 }
361
Jason Sams8cb39de2010-06-01 15:47:01 -0700362 public static Element SCRIPT(RenderScript rs) {
363 if(rs.mElement_SCRIPT == null) {
364 rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT);
Jason Samsa70f4162010-03-26 15:33:42 -0700365 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700366 return rs.mElement_SCRIPT;
Jason Samsa70f4162010-03-26 15:33:42 -0700367 }
368
Jason Sams8cb39de2010-06-01 15:47:01 -0700369 public static Element MESH(RenderScript rs) {
370 if(rs.mElement_MESH == null) {
371 rs.mElement_MESH = createUser(rs, DataType.RS_MESH);
Jason Samsa70f4162010-03-26 15:33:42 -0700372 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700373 return rs.mElement_MESH;
Jason Samsa70f4162010-03-26 15:33:42 -0700374 }
375
Jason Sams8cb39de2010-06-01 15:47:01 -0700376 public static Element PROGRAM_FRAGMENT(RenderScript rs) {
377 if(rs.mElement_PROGRAM_FRAGMENT == null) {
378 rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700379 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700380 return rs.mElement_PROGRAM_FRAGMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700381 }
382
Jason Sams8cb39de2010-06-01 15:47:01 -0700383 public static Element PROGRAM_VERTEX(RenderScript rs) {
384 if(rs.mElement_PROGRAM_VERTEX == null) {
385 rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX);
Jason Samsa70f4162010-03-26 15:33:42 -0700386 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700387 return rs.mElement_PROGRAM_VERTEX;
Jason Samsa70f4162010-03-26 15:33:42 -0700388 }
389
Jason Sams8cb39de2010-06-01 15:47:01 -0700390 public static Element PROGRAM_RASTER(RenderScript rs) {
391 if(rs.mElement_PROGRAM_RASTER == null) {
392 rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER);
Jason Samsa70f4162010-03-26 15:33:42 -0700393 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700394 return rs.mElement_PROGRAM_RASTER;
Jason Samsa70f4162010-03-26 15:33:42 -0700395 }
396
Jason Sams8cb39de2010-06-01 15:47:01 -0700397 public static Element PROGRAM_STORE(RenderScript rs) {
398 if(rs.mElement_PROGRAM_STORE == null) {
399 rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE);
Jason Samsa70f4162010-03-26 15:33:42 -0700400 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700401 return rs.mElement_PROGRAM_STORE;
Jason Samsa70f4162010-03-26 15:33:42 -0700402 }
403
404
Jason Sams718cd1f2009-12-23 14:35:29 -0800405 public static Element A_8(RenderScript rs) {
406 if(rs.mElement_A_8 == null) {
407 rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
408 }
409 return rs.mElement_A_8;
410 }
411
412 public static Element RGB_565(RenderScript rs) {
413 if(rs.mElement_RGB_565 == null) {
414 rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
415 }
416 return rs.mElement_RGB_565;
417 }
418
419 public static Element RGB_888(RenderScript rs) {
420 if(rs.mElement_RGB_888 == null) {
421 rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
422 }
423 return rs.mElement_RGB_888;
424 }
425
426 public static Element RGBA_5551(RenderScript rs) {
427 if(rs.mElement_RGBA_5551 == null) {
428 rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
429 }
430 return rs.mElement_RGBA_5551;
431 }
432
433 public static Element RGBA_4444(RenderScript rs) {
434 if(rs.mElement_RGBA_4444 == null) {
435 rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
436 }
437 return rs.mElement_RGBA_4444;
438 }
439
440 public static Element RGBA_8888(RenderScript rs) {
441 if(rs.mElement_RGBA_8888 == null) {
442 rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
443 }
444 return rs.mElement_RGBA_8888;
445 }
446
Jason Sams8cb39de2010-06-01 15:47:01 -0700447 public static Element F32_2(RenderScript rs) {
448 if(rs.mElement_FLOAT_2 == null) {
449 rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2);
Jason Sams718cd1f2009-12-23 14:35:29 -0800450 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700451 return rs.mElement_FLOAT_2;
Jason Sams718cd1f2009-12-23 14:35:29 -0800452 }
453
Jason Sams8cb39de2010-06-01 15:47:01 -0700454 public static Element F32_3(RenderScript rs) {
455 if(rs.mElement_FLOAT_3 == null) {
456 rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3);
Jason Sams718cd1f2009-12-23 14:35:29 -0800457 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700458 return rs.mElement_FLOAT_3;
Jason Sams718cd1f2009-12-23 14:35:29 -0800459 }
460
Jason Sams8cb39de2010-06-01 15:47:01 -0700461 public static Element F32_4(RenderScript rs) {
462 if(rs.mElement_FLOAT_4 == null) {
463 rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800464 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700465 return rs.mElement_FLOAT_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800466 }
467
Stephen Hines836c4a52011-06-01 14:38:10 -0700468 public static Element F64_2(RenderScript rs) {
469 if(rs.mElement_DOUBLE_2 == null) {
470 rs.mElement_DOUBLE_2 = createVector(rs, DataType.FLOAT_64, 2);
471 }
472 return rs.mElement_DOUBLE_2;
473 }
474
475 public static Element F64_3(RenderScript rs) {
476 if(rs.mElement_DOUBLE_3 == null) {
477 rs.mElement_DOUBLE_3 = createVector(rs, DataType.FLOAT_64, 3);
478 }
479 return rs.mElement_DOUBLE_3;
480 }
481
482 public static Element F64_4(RenderScript rs) {
483 if(rs.mElement_DOUBLE_4 == null) {
484 rs.mElement_DOUBLE_4 = createVector(rs, DataType.FLOAT_64, 4);
485 }
486 return rs.mElement_DOUBLE_4;
487 }
488
489 public static Element U8_2(RenderScript rs) {
490 if(rs.mElement_UCHAR_2 == null) {
491 rs.mElement_UCHAR_2 = createVector(rs, DataType.UNSIGNED_8, 2);
492 }
493 return rs.mElement_UCHAR_2;
494 }
495
496 public static Element U8_3(RenderScript rs) {
497 if(rs.mElement_UCHAR_3 == null) {
498 rs.mElement_UCHAR_3 = createVector(rs, DataType.UNSIGNED_8, 3);
499 }
500 return rs.mElement_UCHAR_3;
501 }
502
Jason Sams8cb39de2010-06-01 15:47:01 -0700503 public static Element U8_4(RenderScript rs) {
504 if(rs.mElement_UCHAR_4 == null) {
505 rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800506 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700507 return rs.mElement_UCHAR_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800508 }
509
Stephen Hines836c4a52011-06-01 14:38:10 -0700510 public static Element I8_2(RenderScript rs) {
511 if(rs.mElement_CHAR_2 == null) {
512 rs.mElement_CHAR_2 = createVector(rs, DataType.SIGNED_8, 2);
513 }
514 return rs.mElement_CHAR_2;
515 }
516
517 public static Element I8_3(RenderScript rs) {
518 if(rs.mElement_CHAR_3 == null) {
519 rs.mElement_CHAR_3 = createVector(rs, DataType.SIGNED_8, 3);
520 }
521 return rs.mElement_CHAR_3;
522 }
523
524 public static Element I8_4(RenderScript rs) {
525 if(rs.mElement_CHAR_4 == null) {
526 rs.mElement_CHAR_4 = createVector(rs, DataType.SIGNED_8, 4);
527 }
528 return rs.mElement_CHAR_4;
529 }
530
531 public static Element U16_2(RenderScript rs) {
532 if(rs.mElement_USHORT_2 == null) {
533 rs.mElement_USHORT_2 = createVector(rs, DataType.UNSIGNED_16, 2);
534 }
535 return rs.mElement_USHORT_2;
536 }
537
538 public static Element U16_3(RenderScript rs) {
539 if(rs.mElement_USHORT_3 == null) {
540 rs.mElement_USHORT_3 = createVector(rs, DataType.UNSIGNED_16, 3);
541 }
542 return rs.mElement_USHORT_3;
543 }
544
545 public static Element U16_4(RenderScript rs) {
546 if(rs.mElement_USHORT_4 == null) {
547 rs.mElement_USHORT_4 = createVector(rs, DataType.UNSIGNED_16, 4);
548 }
549 return rs.mElement_USHORT_4;
550 }
551
552 public static Element I16_2(RenderScript rs) {
553 if(rs.mElement_SHORT_2 == null) {
554 rs.mElement_SHORT_2 = createVector(rs, DataType.SIGNED_16, 2);
555 }
556 return rs.mElement_SHORT_2;
557 }
558
559 public static Element I16_3(RenderScript rs) {
560 if(rs.mElement_SHORT_3 == null) {
561 rs.mElement_SHORT_3 = createVector(rs, DataType.SIGNED_16, 3);
562 }
563 return rs.mElement_SHORT_3;
564 }
565
566 public static Element I16_4(RenderScript rs) {
567 if(rs.mElement_SHORT_4 == null) {
568 rs.mElement_SHORT_4 = createVector(rs, DataType.SIGNED_16, 4);
569 }
570 return rs.mElement_SHORT_4;
571 }
572
573 public static Element U32_2(RenderScript rs) {
574 if(rs.mElement_UINT_2 == null) {
575 rs.mElement_UINT_2 = createVector(rs, DataType.UNSIGNED_32, 2);
576 }
577 return rs.mElement_UINT_2;
578 }
579
580 public static Element U32_3(RenderScript rs) {
581 if(rs.mElement_UINT_3 == null) {
582 rs.mElement_UINT_3 = createVector(rs, DataType.UNSIGNED_32, 3);
583 }
584 return rs.mElement_UINT_3;
585 }
586
587 public static Element U32_4(RenderScript rs) {
588 if(rs.mElement_UINT_4 == null) {
589 rs.mElement_UINT_4 = createVector(rs, DataType.UNSIGNED_32, 4);
590 }
591 return rs.mElement_UINT_4;
592 }
593
594 public static Element I32_2(RenderScript rs) {
595 if(rs.mElement_INT_2 == null) {
596 rs.mElement_INT_2 = createVector(rs, DataType.SIGNED_32, 2);
597 }
598 return rs.mElement_INT_2;
599 }
600
601 public static Element I32_3(RenderScript rs) {
602 if(rs.mElement_INT_3 == null) {
603 rs.mElement_INT_3 = createVector(rs, DataType.SIGNED_32, 3);
604 }
605 return rs.mElement_INT_3;
606 }
607
608 public static Element I32_4(RenderScript rs) {
609 if(rs.mElement_INT_4 == null) {
610 rs.mElement_INT_4 = createVector(rs, DataType.SIGNED_32, 4);
611 }
612 return rs.mElement_INT_4;
613 }
614
615 public static Element U64_2(RenderScript rs) {
616 if(rs.mElement_ULONG_2 == null) {
617 rs.mElement_ULONG_2 = createVector(rs, DataType.UNSIGNED_64, 2);
618 }
619 return rs.mElement_ULONG_2;
620 }
621
622 public static Element U64_3(RenderScript rs) {
623 if(rs.mElement_ULONG_3 == null) {
624 rs.mElement_ULONG_3 = createVector(rs, DataType.UNSIGNED_64, 3);
625 }
626 return rs.mElement_ULONG_3;
627 }
628
629 public static Element U64_4(RenderScript rs) {
630 if(rs.mElement_ULONG_4 == null) {
631 rs.mElement_ULONG_4 = createVector(rs, DataType.UNSIGNED_64, 4);
632 }
633 return rs.mElement_ULONG_4;
634 }
635
636 public static Element I64_2(RenderScript rs) {
637 if(rs.mElement_LONG_2 == null) {
638 rs.mElement_LONG_2 = createVector(rs, DataType.SIGNED_64, 2);
639 }
640 return rs.mElement_LONG_2;
641 }
642
643 public static Element I64_3(RenderScript rs) {
644 if(rs.mElement_LONG_3 == null) {
645 rs.mElement_LONG_3 = createVector(rs, DataType.SIGNED_64, 3);
646 }
647 return rs.mElement_LONG_3;
648 }
649
650 public static Element I64_4(RenderScript rs) {
651 if(rs.mElement_LONG_4 == null) {
652 rs.mElement_LONG_4 = createVector(rs, DataType.SIGNED_64, 4);
653 }
654 return rs.mElement_LONG_4;
655 }
656
Jason Sams1d45c472010-08-25 14:31:48 -0700657 public static Element MATRIX_4X4(RenderScript rs) {
658 if(rs.mElement_MATRIX_4X4 == null) {
659 rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4);
660 }
661 return rs.mElement_MATRIX_4X4;
662 }
663 public static Element MATRIX4X4(RenderScript rs) {
664 return MATRIX_4X4(rs);
665 }
666
667 public static Element MATRIX_3X3(RenderScript rs) {
668 if(rs.mElement_MATRIX_3X3 == null) {
669 rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3);
670 }
Alex Sakhartchouk34769772011-02-28 16:01:28 -0800671 return rs.mElement_MATRIX_3X3;
Jason Sams1d45c472010-08-25 14:31:48 -0700672 }
673
674 public static Element MATRIX_2X2(RenderScript rs) {
675 if(rs.mElement_MATRIX_2X2 == null) {
676 rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2);
677 }
678 return rs.mElement_MATRIX_2X2;
679 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800680
Jason Sams70d4e502010-09-02 17:35:23 -0700681 Element(int id, RenderScript rs, Element[] e, String[] n, int[] as) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700682 super(id, rs);
Jason Samsea84a7c2009-09-04 14:42:41 -0700683 mSize = 0;
Jason Sams718cd1f2009-12-23 14:35:29 -0800684 mElements = e;
685 mElementNames = n;
Jason Sams70d4e502010-09-02 17:35:23 -0700686 mArraySizes = as;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700687 mOffsetInBytes = new int[mElements.length];
Jason Sams718cd1f2009-12-23 14:35:29 -0800688 for (int ct = 0; ct < mElements.length; ct++ ) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700689 mOffsetInBytes[ct] = mSize;
Alex Sakhartchouk9e401bc2010-10-13 14:22:02 -0700690 mSize += mElements[ct].mSize * mArraySizes[ct];
Jason Sams718cd1f2009-12-23 14:35:29 -0800691 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800692 }
693
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700694 Element(int id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
695 super(id, rs);
Jason Sams252c0782011-01-11 17:42:52 -0800696 if ((dt != DataType.UNSIGNED_5_6_5) &&
697 (dt != DataType.UNSIGNED_4_4_4_4) &&
698 (dt != DataType.UNSIGNED_5_5_5_1)) {
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800699 if (size == 3) {
700 mSize = dt.mSize * 4;
701 } else {
702 mSize = dt.mSize * size;
703 }
Jason Sams252c0782011-01-11 17:42:52 -0800704 } else {
705 mSize = dt.mSize;
706 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800707 mType = dt;
708 mKind = dk;
709 mNormalized = norm;
710 mVectorSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700711 }
712
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700713 Element(int id, RenderScript rs) {
714 super(id, rs);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700715 }
716
717 @Override
718 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800719 super.updateFromNative();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700720
721 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
722 int[] dataBuffer = new int[5];
Jason Sams06d69de2010-11-09 17:11:40 -0800723 mRS.nElementGetNativeData(getID(), dataBuffer);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700724
725 mNormalized = dataBuffer[2] == 1 ? true : false;
726 mVectorSize = dataBuffer[3];
727 mSize = 0;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700728 for (DataType dt: DataType.values()) {
729 if(dt.mID == dataBuffer[0]){
730 mType = dt;
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700731 mSize = mType.mSize * mVectorSize;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700732 }
733 }
734 for (DataKind dk: DataKind.values()) {
735 if(dk.mID == dataBuffer[1]){
736 mKind = dk;
737 }
738 }
739
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700740 int numSubElements = dataBuffer[4];
741 if(numSubElements > 0) {
742 mElements = new Element[numSubElements];
743 mElementNames = new String[numSubElements];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700744 mArraySizes = new int[numSubElements];
745 mOffsetInBytes = new int[numSubElements];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700746
747 int[] subElementIds = new int[numSubElements];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700748 mRS.nElementGetSubElements(getID(), subElementIds, mElementNames, mArraySizes);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700749 for(int i = 0; i < numSubElements; i ++) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700750 mElements[i] = new Element(subElementIds[i], mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700751 mElements[i].updateFromNative();
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700752 mOffsetInBytes[i] = mSize;
753 mSize += mElements[i].mSize * mArraySizes[i];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700754 }
755 }
756
757 }
758
Jason Samsa1b13ed2010-11-12 14:58:37 -0800759 /**
760 * Create a custom Element of the specified DataType. The DataKind will be
761 * set to USER and the vector size to 1 indicating non-vector.
762 *
763 * @param rs The context associated with the new Element.
764 * @param dt The DataType for the new element.
765 * @return Element
766 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800767 static Element createUser(RenderScript rs, DataType dt) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700768 DataKind dk = DataKind.USER;
769 boolean norm = false;
770 int vecSize = 1;
771 int id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize);
772 return new Element(id, rs, dt, dk, norm, vecSize);
Jason Sams718cd1f2009-12-23 14:35:29 -0800773 }
774
Jason Samsa1b13ed2010-11-12 14:58:37 -0800775 /**
776 * Create a custom vector element of the specified DataType and vector size.
777 * DataKind will be set to USER.
778 *
779 * @param rs The context associated with the new Element.
780 * @param dt The DataType for the new element.
781 * @param size Vector size for the new Element. Range 2-4 inclusive
782 * supported.
783 *
784 * @return Element
785 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800786 public static Element createVector(RenderScript rs, DataType dt, int size) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800787 if (size < 2 || size > 4) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800788 throw new RSIllegalArgumentException("Vector size out of range 2-4.");
Jason Samsea84a7c2009-09-04 14:42:41 -0700789 }
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700790 DataKind dk = DataKind.USER;
791 boolean norm = false;
792 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
793 return new Element(id, rs, dt, dk, norm, size);
Jason Samsea84a7c2009-09-04 14:42:41 -0700794 }
795
Jason Samsa1b13ed2010-11-12 14:58:37 -0800796 /**
797 * Create a new pixel Element type. A matching DataType and DataKind must
798 * be provided. The DataType and DataKind must contain the same number of
799 * components. Vector size will be set to 1.
800 *
801 * @param rs The context associated with the new Element.
802 * @param dt The DataType for the new element.
803 * @param dk The DataKind to specify the mapping of each component in the
804 * DataType.
805 *
806 * @return Element
807 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800808 public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800809 if (!(dk == DataKind.PIXEL_L ||
810 dk == DataKind.PIXEL_A ||
811 dk == DataKind.PIXEL_LA ||
812 dk == DataKind.PIXEL_RGB ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700813 dk == DataKind.PIXEL_RGBA ||
814 dk == DataKind.PIXEL_DEPTH)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700815 throw new RSIllegalArgumentException("Unsupported DataKind");
Jason Sams718cd1f2009-12-23 14:35:29 -0800816 }
817 if (!(dt == DataType.UNSIGNED_8 ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700818 dt == DataType.UNSIGNED_16 ||
Jason Sams718cd1f2009-12-23 14:35:29 -0800819 dt == DataType.UNSIGNED_5_6_5 ||
820 dt == DataType.UNSIGNED_4_4_4_4 ||
821 dt == DataType.UNSIGNED_5_5_5_1)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700822 throw new RSIllegalArgumentException("Unsupported DataType");
Jason Sams718cd1f2009-12-23 14:35:29 -0800823 }
824 if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
Jason Samsc1d62102010-11-04 14:32:19 -0700825 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800826 }
827 if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700828 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800829 }
830 if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700831 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800832 }
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700833 if (dt == DataType.UNSIGNED_16 &&
834 dk != DataKind.PIXEL_DEPTH) {
835 throw new RSIllegalArgumentException("Bad kind and type combo");
836 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800837
838 int size = 1;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700839 switch (dk) {
840 case PIXEL_LA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800841 size = 2;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700842 break;
843 case PIXEL_RGB:
Jason Sams718cd1f2009-12-23 14:35:29 -0800844 size = 3;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700845 break;
846 case PIXEL_RGBA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800847 size = 4;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700848 break;
849 case PIXEL_DEPTH:
850 size = 2;
851 break;
Jason Sams718cd1f2009-12-23 14:35:29 -0800852 }
853
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700854 boolean norm = true;
855 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
856 return new Element(id, rs, dt, dk, norm, size);
Jason Sams718cd1f2009-12-23 14:35:29 -0800857 }
Jason Sams36e612a2009-07-31 16:26:13 -0700858
Jason Samsa1b13ed2010-11-12 14:58:37 -0800859 /**
Stephen Hinesf257e512011-06-14 14:54:29 -0700860 * Check if the current Element is compatible with another Element.
861 * Primitive Elements are compatible if they share the same underlying
862 * size and type (i.e. U8 is compatible with A_8). User-defined Elements
863 * must be equal in order to be compatible. This requires strict name
864 * equivalence for all sub-Elements (in addition to structural equivalence).
865 *
866 * @param e The Element to check compatibility with.
867 *
868 * @return boolean true if the Elements are compatible, otherwise false.
869 */
870 public boolean isCompatible(Element e) {
871 // Try strict BaseObj equality to start with.
872 if (this.equals(e)) {
873 return true;
874 }
875
876 // Ignore mKind because it is allowed to be different (user vs. pixel).
877 // We also ignore mNormalized because it can be different. The mType
878 // field must be non-null since we require name equivalence for
879 // user-created Elements.
880 return ((mSize == e.mSize) &&
881 (mType != null) &&
882 (mType == e.mType) &&
883 (mVectorSize == e.mVectorSize));
884 }
885
886 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800887 * Builder class for producing complex elements with matching field and name
888 * pairs. The builder starts empty. The order in which elements are added
889 * is retained for the layout in memory.
890 *
891 */
Jason Sams36e612a2009-07-31 16:26:13 -0700892 public static class Builder {
893 RenderScript mRS;
Jason Sams718cd1f2009-12-23 14:35:29 -0800894 Element[] mElements;
895 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -0700896 int[] mArraySizes;
Jason Sams718cd1f2009-12-23 14:35:29 -0800897 int mCount;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800898 int mSkipPadding;
Jason Sams22534172009-08-04 16:58:20 -0700899
Jason Samsa1b13ed2010-11-12 14:58:37 -0800900 /**
901 * Create a builder object.
902 *
903 * @param rs
904 */
Jason Sams22534172009-08-04 16:58:20 -0700905 public Builder(RenderScript rs) {
Jason Sams36e612a2009-07-31 16:26:13 -0700906 mRS = rs;
Jason Sams718cd1f2009-12-23 14:35:29 -0800907 mCount = 0;
908 mElements = new Element[8];
909 mElementNames = new String[8];
Jason Sams70d4e502010-09-02 17:35:23 -0700910 mArraySizes = new int[8];
Jason Sams36e612a2009-07-31 16:26:13 -0700911 }
912
Jason Samsa1b13ed2010-11-12 14:58:37 -0800913 /**
914 * Add an array of elements to this element.
915 *
916 * @param element
917 * @param name
918 * @param arraySize
919 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800920 public Builder add(Element element, String name, int arraySize) {
Jason Sams70d4e502010-09-02 17:35:23 -0700921 if (arraySize < 1) {
Jason Samsc1d62102010-11-04 14:32:19 -0700922 throw new RSIllegalArgumentException("Array size cannot be less than 1.");
Jason Sams70d4e502010-09-02 17:35:23 -0700923 }
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800924
925 // Skip padding fields after a vector 3 type.
926 if (mSkipPadding != 0) {
927 if (name.startsWith("#padding_")) {
928 mSkipPadding = 0;
929 return this;
930 }
931 }
932
933 if (element.mVectorSize == 3) {
934 mSkipPadding = 1;
935 } else {
936 mSkipPadding = 0;
937 }
938
Jason Sams718cd1f2009-12-23 14:35:29 -0800939 if(mCount == mElements.length) {
940 Element[] e = new Element[mCount + 8];
941 String[] s = new String[mCount + 8];
Jason Sams70d4e502010-09-02 17:35:23 -0700942 int[] as = new int[mCount + 8];
Jason Sams718cd1f2009-12-23 14:35:29 -0800943 System.arraycopy(mElements, 0, e, 0, mCount);
944 System.arraycopy(mElementNames, 0, s, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -0700945 System.arraycopy(mArraySizes, 0, as, 0, mCount);
Jason Sams718cd1f2009-12-23 14:35:29 -0800946 mElements = e;
947 mElementNames = s;
Jason Sams70d4e502010-09-02 17:35:23 -0700948 mArraySizes = as;
Jason Sams36e612a2009-07-31 16:26:13 -0700949 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800950 mElements[mCount] = element;
951 mElementNames[mCount] = name;
Jason Sams70d4e502010-09-02 17:35:23 -0700952 mArraySizes[mCount] = arraySize;
Jason Sams718cd1f2009-12-23 14:35:29 -0800953 mCount++;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800954 return this;
Jason Sams07ae4062009-08-27 20:23:34 -0700955 }
956
Jason Samsa1b13ed2010-11-12 14:58:37 -0800957 /**
958 * Add a single element to this Element.
959 *
960 * @param element
961 * @param name
962 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800963 public Builder add(Element element, String name) {
964 return add(element, name, 1);
Jason Sams70d4e502010-09-02 17:35:23 -0700965 }
966
Jason Samsa1b13ed2010-11-12 14:58:37 -0800967 /**
968 * Create the element from this builder.
969 *
970 *
971 * @return Element
972 */
Jason Sams22534172009-08-04 16:58:20 -0700973 public Element create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800974 mRS.validate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800975 Element[] ein = new Element[mCount];
976 String[] sin = new String[mCount];
Jason Sams70d4e502010-09-02 17:35:23 -0700977 int[] asin = new int[mCount];
Jason Sams718cd1f2009-12-23 14:35:29 -0800978 java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
979 java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -0700980 java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700981
982 int[] ids = new int[ein.length];
983 for (int ct = 0; ct < ein.length; ct++ ) {
Jason Sams06d69de2010-11-09 17:11:40 -0800984 ids[ct] = ein[ct].getID();
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700985 }
Jason Sams70d4e502010-09-02 17:35:23 -0700986 int id = mRS.nElementCreate2(ids, sin, asin);
987 return new Element(id, mRS, ein, sin, asin);
Jason Sams36e612a2009-07-31 16:26:13 -0700988 }
989 }
Jason Sams36e612a2009-07-31 16:26:13 -0700990}
991