blob: 29306e4b52f58543bcc4e0c7144f49d6efc57c97 [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
65 * naming convention follows. For numeric types its FLOAT, SIGNED, UNSIGNED
66 * followed by the _BITS where BITS is the size of the data. BOOLEAN is a
67 * true / false (1,0) represented in an 8 bit container. The UNSIGNED
68 * variants with multiple bit definitions are for packed graphical data
69 * formats and represents vectors with per vector member sizes which are
70 * treated as a single unit for packing and alignment purposes.
71 *
72 * MATRIX the three matrix types contain FLOAT_32 elements and are treated
73 * as 32 bits for alignment purposes.
74 *
75 * RS_* objects. 32 bit opaque handles.
76 */
Jason Sams36e612a2009-07-31 16:26:13 -070077 public enum DataType {
Jason Sams718cd1f2009-12-23 14:35:29 -080078 //FLOAT_16 (1, 2),
79 FLOAT_32 (2, 4),
Stephen Hines02f417052010-09-30 15:19:22 -070080 FLOAT_64 (3, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -080081 SIGNED_8 (4, 1),
82 SIGNED_16 (5, 2),
83 SIGNED_32 (6, 4),
Stephen Hinesef1dac22010-10-01 15:39:33 -070084 SIGNED_64 (7, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -080085 UNSIGNED_8 (8, 1),
86 UNSIGNED_16 (9, 2),
87 UNSIGNED_32 (10, 4),
Stephen Hines52d83632010-10-11 16:10:42 -070088 UNSIGNED_64 (11, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -080089
Jason Samsf110d4b2010-06-21 17:42:41 -070090 BOOLEAN(12, 1),
Jason Sams718cd1f2009-12-23 14:35:29 -080091
Jason Samsf110d4b2010-06-21 17:42:41 -070092 UNSIGNED_5_6_5 (13, 2),
93 UNSIGNED_5_5_5_1 (14, 2),
94 UNSIGNED_4_4_4_4 (15, 2),
95
Jason Sams1d45c472010-08-25 14:31:48 -070096 MATRIX_4X4 (16, 64),
97 MATRIX_3X3 (17, 36),
98 MATRIX_2X2 (18, 16),
99
100 RS_ELEMENT (1000, 4),
101 RS_TYPE (1001, 4),
102 RS_ALLOCATION (1002, 4),
103 RS_SAMPLER (1003, 4),
104 RS_SCRIPT (1004, 4),
105 RS_MESH (1005, 4),
106 RS_PROGRAM_FRAGMENT (1006, 4),
107 RS_PROGRAM_VERTEX (1007, 4),
108 RS_PROGRAM_RASTER (1008, 4),
109 RS_PROGRAM_STORE (1009, 4);
Jason Sams36e612a2009-07-31 16:26:13 -0700110
111 int mID;
Jason Sams718cd1f2009-12-23 14:35:29 -0800112 int mSize;
113 DataType(int id, int size) {
Jason Sams36e612a2009-07-31 16:26:13 -0700114 mID = id;
Jason Sams718cd1f2009-12-23 14:35:29 -0800115 mSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700116 }
117 }
118
Jason Samsa1b13ed2010-11-12 14:58:37 -0800119 /**
120 * The special interpretation of the data if required. This is primarly
121 * useful for graphical data. USER indicates no special interpretation is
122 * expected. PIXEL is used in conjunction with the standard data types for
123 * representing texture formats.
124 */
Jason Sams36e612a2009-07-31 16:26:13 -0700125 public enum DataKind {
126 USER (0),
Jason Sams718cd1f2009-12-23 14:35:29 -0800127
128 PIXEL_L (7),
129 PIXEL_A (8),
130 PIXEL_LA (9),
131 PIXEL_RGB (10),
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700132 PIXEL_RGBA (11),
133 PIXEL_DEPTH (12);
Jason Sams36e612a2009-07-31 16:26:13 -0700134
135 int mID;
136 DataKind(int id) {
137 mID = id;
138 }
139 }
140
Jason Samsa1b13ed2010-11-12 14:58:37 -0800141 /**
142 * Return if a element is too complex for use as a data source for a Mesh or
143 * a Program.
144 *
145 * @return boolean
146 */
Jason Samsc1d62102010-11-04 14:32:19 -0700147 public boolean isComplex() {
148 if (mElements == null) {
149 return false;
150 }
151 for (int ct=0; ct < mElements.length; ct++) {
152 if (mElements[ct].mElements != null) {
153 return true;
154 }
155 }
156 return false;
157 }
158
Jason Samsa1b13ed2010-11-12 14:58:37 -0800159 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700160 * @hide
161 * @return number of sub-elements in this element
162 */
163 public int getSubElementCount() {
164 if (mElements == null) {
165 return 0;
166 }
167 return mElements.length;
168 }
169
170 /**
171 * @hide
172 * @param index index of the sub-element to return
173 * @return sub-element in this element at given index
174 */
175 public Element getSubElement(int index) {
176 if (mElements == null) {
177 throw new RSIllegalArgumentException("Element contains no sub-elements");
178 }
179 if (index < 0 || index >= mElements.length) {
180 throw new RSIllegalArgumentException("Illegal sub-element index");
181 }
182 return mElements[index];
183 }
184
185 /**
186 * @hide
187 * @param index index of the sub-element
188 * @return sub-element in this element at given index
189 */
190 public String getSubElementName(int index) {
191 if (mElements == null) {
192 throw new RSIllegalArgumentException("Element contains no sub-elements");
193 }
194 if (index < 0 || index >= mElements.length) {
195 throw new RSIllegalArgumentException("Illegal sub-element index");
196 }
197 return mElementNames[index];
198 }
199
200 /**
201 * @hide
202 * @param index index of the sub-element
203 * @return array size of sub-element in this element at given index
204 */
205 public int getSubElementArraySize(int index) {
206 if (mElements == null) {
207 throw new RSIllegalArgumentException("Element contains no sub-elements");
208 }
209 if (index < 0 || index >= mElements.length) {
210 throw new RSIllegalArgumentException("Illegal sub-element index");
211 }
212 return mArraySizes[index];
213 }
214
215 /**
216 * @hide
217 * @param index index of the sub-element
218 * @return offset in bytes of sub-element in this element at given index
219 */
220 public int getSubElementOffsetBytes(int index) {
221 if (mElements == null) {
222 throw new RSIllegalArgumentException("Element contains no sub-elements");
223 }
224 if (index < 0 || index >= mElements.length) {
225 throw new RSIllegalArgumentException("Illegal sub-element index");
226 }
227 return mOffsetInBytes[index];
228 }
229
230 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800231 * Utility function for returning an Element containing a single Boolean.
232 *
233 * @param rs Context to which the element will belong.
234 *
235 * @return Element
236 */
Jason Samsf110d4b2010-06-21 17:42:41 -0700237 public static Element BOOLEAN(RenderScript rs) {
238 if(rs.mElement_BOOLEAN == null) {
239 rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN);
240 }
241 return rs.mElement_BOOLEAN;
242 }
243
Jason Samsa1b13ed2010-11-12 14:58:37 -0800244 /**
245 * Utility function for returning an Element containing a single UNSIGNED_8.
246 *
247 * @param rs Context to which the element will belong.
248 *
249 * @return Element
250 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700251 public static Element U8(RenderScript rs) {
252 if(rs.mElement_U8 == null) {
253 rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800254 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700255 return rs.mElement_U8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800256 }
257
Jason Samsa1b13ed2010-11-12 14:58:37 -0800258 /**
259 * Utility function for returning an Element containing a single SIGNED_8.
260 *
261 * @param rs Context to which the element will belong.
262 *
263 * @return Element
264 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700265 public static Element I8(RenderScript rs) {
266 if(rs.mElement_I8 == null) {
267 rs.mElement_I8 = createUser(rs, DataType.SIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800268 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700269 return rs.mElement_I8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800270 }
271
Jason Samse29f3e72010-06-08 15:40:48 -0700272 public static Element U16(RenderScript rs) {
273 if(rs.mElement_U16 == null) {
274 rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16);
275 }
276 return rs.mElement_U16;
277 }
278
279 public static Element I16(RenderScript rs) {
280 if(rs.mElement_I16 == null) {
281 rs.mElement_I16 = createUser(rs, DataType.SIGNED_16);
282 }
283 return rs.mElement_I16;
284 }
285
Jason Sams8cb39de2010-06-01 15:47:01 -0700286 public static Element U32(RenderScript rs) {
287 if(rs.mElement_U32 == null) {
288 rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800289 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700290 return rs.mElement_U32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800291 }
292
Jason Sams8cb39de2010-06-01 15:47:01 -0700293 public static Element I32(RenderScript rs) {
294 if(rs.mElement_I32 == null) {
295 rs.mElement_I32 = createUser(rs, DataType.SIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800296 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700297 return rs.mElement_I32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800298 }
299
Stephen Hines52d83632010-10-11 16:10:42 -0700300 public static Element U64(RenderScript rs) {
301 if(rs.mElement_U64 == null) {
302 rs.mElement_U64 = createUser(rs, DataType.UNSIGNED_64);
303 }
304 return rs.mElement_U64;
305 }
306
Stephen Hinesef1dac22010-10-01 15:39:33 -0700307 public static Element I64(RenderScript rs) {
308 if(rs.mElement_I64 == null) {
309 rs.mElement_I64 = createUser(rs, DataType.SIGNED_64);
310 }
311 return rs.mElement_I64;
312 }
313
Jason Sams8cb39de2010-06-01 15:47:01 -0700314 public static Element F32(RenderScript rs) {
315 if(rs.mElement_F32 == null) {
316 rs.mElement_F32 = createUser(rs, DataType.FLOAT_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800317 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700318 return rs.mElement_F32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800319 }
320
Stephen Hines02f417052010-09-30 15:19:22 -0700321 public static Element F64(RenderScript rs) {
322 if(rs.mElement_F64 == null) {
323 rs.mElement_F64 = createUser(rs, DataType.FLOAT_64);
324 }
325 return rs.mElement_F64;
326 }
327
Jason Sams8cb39de2010-06-01 15:47:01 -0700328 public static Element ELEMENT(RenderScript rs) {
329 if(rs.mElement_ELEMENT == null) {
330 rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700331 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700332 return rs.mElement_ELEMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700333 }
334
Jason Sams8cb39de2010-06-01 15:47:01 -0700335 public static Element TYPE(RenderScript rs) {
336 if(rs.mElement_TYPE == null) {
337 rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE);
Jason Samsa70f4162010-03-26 15:33:42 -0700338 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700339 return rs.mElement_TYPE;
Jason Samsa70f4162010-03-26 15:33:42 -0700340 }
341
Jason Sams8cb39de2010-06-01 15:47:01 -0700342 public static Element ALLOCATION(RenderScript rs) {
343 if(rs.mElement_ALLOCATION == null) {
344 rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION);
Jason Samsa70f4162010-03-26 15:33:42 -0700345 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700346 return rs.mElement_ALLOCATION;
Jason Samsa70f4162010-03-26 15:33:42 -0700347 }
348
Jason Sams8cb39de2010-06-01 15:47:01 -0700349 public static Element SAMPLER(RenderScript rs) {
350 if(rs.mElement_SAMPLER == null) {
351 rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER);
Jason Samsa70f4162010-03-26 15:33:42 -0700352 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700353 return rs.mElement_SAMPLER;
Jason Samsa70f4162010-03-26 15:33:42 -0700354 }
355
Jason Sams8cb39de2010-06-01 15:47:01 -0700356 public static Element SCRIPT(RenderScript rs) {
357 if(rs.mElement_SCRIPT == null) {
358 rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT);
Jason Samsa70f4162010-03-26 15:33:42 -0700359 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700360 return rs.mElement_SCRIPT;
Jason Samsa70f4162010-03-26 15:33:42 -0700361 }
362
Jason Sams8cb39de2010-06-01 15:47:01 -0700363 public static Element MESH(RenderScript rs) {
364 if(rs.mElement_MESH == null) {
365 rs.mElement_MESH = createUser(rs, DataType.RS_MESH);
Jason Samsa70f4162010-03-26 15:33:42 -0700366 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700367 return rs.mElement_MESH;
Jason Samsa70f4162010-03-26 15:33:42 -0700368 }
369
Jason Sams8cb39de2010-06-01 15:47:01 -0700370 public static Element PROGRAM_FRAGMENT(RenderScript rs) {
371 if(rs.mElement_PROGRAM_FRAGMENT == null) {
372 rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700373 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700374 return rs.mElement_PROGRAM_FRAGMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700375 }
376
Jason Sams8cb39de2010-06-01 15:47:01 -0700377 public static Element PROGRAM_VERTEX(RenderScript rs) {
378 if(rs.mElement_PROGRAM_VERTEX == null) {
379 rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX);
Jason Samsa70f4162010-03-26 15:33:42 -0700380 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700381 return rs.mElement_PROGRAM_VERTEX;
Jason Samsa70f4162010-03-26 15:33:42 -0700382 }
383
Jason Sams8cb39de2010-06-01 15:47:01 -0700384 public static Element PROGRAM_RASTER(RenderScript rs) {
385 if(rs.mElement_PROGRAM_RASTER == null) {
386 rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER);
Jason Samsa70f4162010-03-26 15:33:42 -0700387 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700388 return rs.mElement_PROGRAM_RASTER;
Jason Samsa70f4162010-03-26 15:33:42 -0700389 }
390
Jason Sams8cb39de2010-06-01 15:47:01 -0700391 public static Element PROGRAM_STORE(RenderScript rs) {
392 if(rs.mElement_PROGRAM_STORE == null) {
393 rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE);
Jason Samsa70f4162010-03-26 15:33:42 -0700394 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700395 return rs.mElement_PROGRAM_STORE;
Jason Samsa70f4162010-03-26 15:33:42 -0700396 }
397
398
Jason Sams718cd1f2009-12-23 14:35:29 -0800399 public static Element A_8(RenderScript rs) {
400 if(rs.mElement_A_8 == null) {
401 rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
402 }
403 return rs.mElement_A_8;
404 }
405
406 public static Element RGB_565(RenderScript rs) {
407 if(rs.mElement_RGB_565 == null) {
408 rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
409 }
410 return rs.mElement_RGB_565;
411 }
412
413 public static Element RGB_888(RenderScript rs) {
414 if(rs.mElement_RGB_888 == null) {
415 rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
416 }
417 return rs.mElement_RGB_888;
418 }
419
420 public static Element RGBA_5551(RenderScript rs) {
421 if(rs.mElement_RGBA_5551 == null) {
422 rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
423 }
424 return rs.mElement_RGBA_5551;
425 }
426
427 public static Element RGBA_4444(RenderScript rs) {
428 if(rs.mElement_RGBA_4444 == null) {
429 rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
430 }
431 return rs.mElement_RGBA_4444;
432 }
433
434 public static Element RGBA_8888(RenderScript rs) {
435 if(rs.mElement_RGBA_8888 == null) {
436 rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
437 }
438 return rs.mElement_RGBA_8888;
439 }
440
Jason Sams8cb39de2010-06-01 15:47:01 -0700441 public static Element F32_2(RenderScript rs) {
442 if(rs.mElement_FLOAT_2 == null) {
443 rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2);
Jason Sams718cd1f2009-12-23 14:35:29 -0800444 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700445 return rs.mElement_FLOAT_2;
Jason Sams718cd1f2009-12-23 14:35:29 -0800446 }
447
Jason Sams8cb39de2010-06-01 15:47:01 -0700448 public static Element F32_3(RenderScript rs) {
449 if(rs.mElement_FLOAT_3 == null) {
450 rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3);
Jason Sams718cd1f2009-12-23 14:35:29 -0800451 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700452 return rs.mElement_FLOAT_3;
Jason Sams718cd1f2009-12-23 14:35:29 -0800453 }
454
Jason Sams8cb39de2010-06-01 15:47:01 -0700455 public static Element F32_4(RenderScript rs) {
456 if(rs.mElement_FLOAT_4 == null) {
457 rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800458 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700459 return rs.mElement_FLOAT_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800460 }
461
Stephen Hines836c4a52011-06-01 14:38:10 -0700462 public static Element F64_2(RenderScript rs) {
463 if(rs.mElement_DOUBLE_2 == null) {
464 rs.mElement_DOUBLE_2 = createVector(rs, DataType.FLOAT_64, 2);
465 }
466 return rs.mElement_DOUBLE_2;
467 }
468
469 public static Element F64_3(RenderScript rs) {
470 if(rs.mElement_DOUBLE_3 == null) {
471 rs.mElement_DOUBLE_3 = createVector(rs, DataType.FLOAT_64, 3);
472 }
473 return rs.mElement_DOUBLE_3;
474 }
475
476 public static Element F64_4(RenderScript rs) {
477 if(rs.mElement_DOUBLE_4 == null) {
478 rs.mElement_DOUBLE_4 = createVector(rs, DataType.FLOAT_64, 4);
479 }
480 return rs.mElement_DOUBLE_4;
481 }
482
483 public static Element U8_2(RenderScript rs) {
484 if(rs.mElement_UCHAR_2 == null) {
485 rs.mElement_UCHAR_2 = createVector(rs, DataType.UNSIGNED_8, 2);
486 }
487 return rs.mElement_UCHAR_2;
488 }
489
490 public static Element U8_3(RenderScript rs) {
491 if(rs.mElement_UCHAR_3 == null) {
492 rs.mElement_UCHAR_3 = createVector(rs, DataType.UNSIGNED_8, 3);
493 }
494 return rs.mElement_UCHAR_3;
495 }
496
Jason Sams8cb39de2010-06-01 15:47:01 -0700497 public static Element U8_4(RenderScript rs) {
498 if(rs.mElement_UCHAR_4 == null) {
499 rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800500 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700501 return rs.mElement_UCHAR_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800502 }
503
Stephen Hines836c4a52011-06-01 14:38:10 -0700504 public static Element I8_2(RenderScript rs) {
505 if(rs.mElement_CHAR_2 == null) {
506 rs.mElement_CHAR_2 = createVector(rs, DataType.SIGNED_8, 2);
507 }
508 return rs.mElement_CHAR_2;
509 }
510
511 public static Element I8_3(RenderScript rs) {
512 if(rs.mElement_CHAR_3 == null) {
513 rs.mElement_CHAR_3 = createVector(rs, DataType.SIGNED_8, 3);
514 }
515 return rs.mElement_CHAR_3;
516 }
517
518 public static Element I8_4(RenderScript rs) {
519 if(rs.mElement_CHAR_4 == null) {
520 rs.mElement_CHAR_4 = createVector(rs, DataType.SIGNED_8, 4);
521 }
522 return rs.mElement_CHAR_4;
523 }
524
525 public static Element U16_2(RenderScript rs) {
526 if(rs.mElement_USHORT_2 == null) {
527 rs.mElement_USHORT_2 = createVector(rs, DataType.UNSIGNED_16, 2);
528 }
529 return rs.mElement_USHORT_2;
530 }
531
532 public static Element U16_3(RenderScript rs) {
533 if(rs.mElement_USHORT_3 == null) {
534 rs.mElement_USHORT_3 = createVector(rs, DataType.UNSIGNED_16, 3);
535 }
536 return rs.mElement_USHORT_3;
537 }
538
539 public static Element U16_4(RenderScript rs) {
540 if(rs.mElement_USHORT_4 == null) {
541 rs.mElement_USHORT_4 = createVector(rs, DataType.UNSIGNED_16, 4);
542 }
543 return rs.mElement_USHORT_4;
544 }
545
546 public static Element I16_2(RenderScript rs) {
547 if(rs.mElement_SHORT_2 == null) {
548 rs.mElement_SHORT_2 = createVector(rs, DataType.SIGNED_16, 2);
549 }
550 return rs.mElement_SHORT_2;
551 }
552
553 public static Element I16_3(RenderScript rs) {
554 if(rs.mElement_SHORT_3 == null) {
555 rs.mElement_SHORT_3 = createVector(rs, DataType.SIGNED_16, 3);
556 }
557 return rs.mElement_SHORT_3;
558 }
559
560 public static Element I16_4(RenderScript rs) {
561 if(rs.mElement_SHORT_4 == null) {
562 rs.mElement_SHORT_4 = createVector(rs, DataType.SIGNED_16, 4);
563 }
564 return rs.mElement_SHORT_4;
565 }
566
567 public static Element U32_2(RenderScript rs) {
568 if(rs.mElement_UINT_2 == null) {
569 rs.mElement_UINT_2 = createVector(rs, DataType.UNSIGNED_32, 2);
570 }
571 return rs.mElement_UINT_2;
572 }
573
574 public static Element U32_3(RenderScript rs) {
575 if(rs.mElement_UINT_3 == null) {
576 rs.mElement_UINT_3 = createVector(rs, DataType.UNSIGNED_32, 3);
577 }
578 return rs.mElement_UINT_3;
579 }
580
581 public static Element U32_4(RenderScript rs) {
582 if(rs.mElement_UINT_4 == null) {
583 rs.mElement_UINT_4 = createVector(rs, DataType.UNSIGNED_32, 4);
584 }
585 return rs.mElement_UINT_4;
586 }
587
588 public static Element I32_2(RenderScript rs) {
589 if(rs.mElement_INT_2 == null) {
590 rs.mElement_INT_2 = createVector(rs, DataType.SIGNED_32, 2);
591 }
592 return rs.mElement_INT_2;
593 }
594
595 public static Element I32_3(RenderScript rs) {
596 if(rs.mElement_INT_3 == null) {
597 rs.mElement_INT_3 = createVector(rs, DataType.SIGNED_32, 3);
598 }
599 return rs.mElement_INT_3;
600 }
601
602 public static Element I32_4(RenderScript rs) {
603 if(rs.mElement_INT_4 == null) {
604 rs.mElement_INT_4 = createVector(rs, DataType.SIGNED_32, 4);
605 }
606 return rs.mElement_INT_4;
607 }
608
609 public static Element U64_2(RenderScript rs) {
610 if(rs.mElement_ULONG_2 == null) {
611 rs.mElement_ULONG_2 = createVector(rs, DataType.UNSIGNED_64, 2);
612 }
613 return rs.mElement_ULONG_2;
614 }
615
616 public static Element U64_3(RenderScript rs) {
617 if(rs.mElement_ULONG_3 == null) {
618 rs.mElement_ULONG_3 = createVector(rs, DataType.UNSIGNED_64, 3);
619 }
620 return rs.mElement_ULONG_3;
621 }
622
623 public static Element U64_4(RenderScript rs) {
624 if(rs.mElement_ULONG_4 == null) {
625 rs.mElement_ULONG_4 = createVector(rs, DataType.UNSIGNED_64, 4);
626 }
627 return rs.mElement_ULONG_4;
628 }
629
630 public static Element I64_2(RenderScript rs) {
631 if(rs.mElement_LONG_2 == null) {
632 rs.mElement_LONG_2 = createVector(rs, DataType.SIGNED_64, 2);
633 }
634 return rs.mElement_LONG_2;
635 }
636
637 public static Element I64_3(RenderScript rs) {
638 if(rs.mElement_LONG_3 == null) {
639 rs.mElement_LONG_3 = createVector(rs, DataType.SIGNED_64, 3);
640 }
641 return rs.mElement_LONG_3;
642 }
643
644 public static Element I64_4(RenderScript rs) {
645 if(rs.mElement_LONG_4 == null) {
646 rs.mElement_LONG_4 = createVector(rs, DataType.SIGNED_64, 4);
647 }
648 return rs.mElement_LONG_4;
649 }
650
Jason Sams1d45c472010-08-25 14:31:48 -0700651 public static Element MATRIX_4X4(RenderScript rs) {
652 if(rs.mElement_MATRIX_4X4 == null) {
653 rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4);
654 }
655 return rs.mElement_MATRIX_4X4;
656 }
657 public static Element MATRIX4X4(RenderScript rs) {
658 return MATRIX_4X4(rs);
659 }
660
661 public static Element MATRIX_3X3(RenderScript rs) {
662 if(rs.mElement_MATRIX_3X3 == null) {
663 rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3);
664 }
Alex Sakhartchouk34769772011-02-28 16:01:28 -0800665 return rs.mElement_MATRIX_3X3;
Jason Sams1d45c472010-08-25 14:31:48 -0700666 }
667
668 public static Element MATRIX_2X2(RenderScript rs) {
669 if(rs.mElement_MATRIX_2X2 == null) {
670 rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2);
671 }
672 return rs.mElement_MATRIX_2X2;
673 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800674
Jason Sams70d4e502010-09-02 17:35:23 -0700675 Element(int id, RenderScript rs, Element[] e, String[] n, int[] as) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700676 super(id, rs);
Jason Samsea84a7c2009-09-04 14:42:41 -0700677 mSize = 0;
Jason Sams718cd1f2009-12-23 14:35:29 -0800678 mElements = e;
679 mElementNames = n;
Jason Sams70d4e502010-09-02 17:35:23 -0700680 mArraySizes = as;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700681 mOffsetInBytes = new int[mElements.length];
Jason Sams718cd1f2009-12-23 14:35:29 -0800682 for (int ct = 0; ct < mElements.length; ct++ ) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700683 mOffsetInBytes[ct] = mSize;
Alex Sakhartchouk9e401bc2010-10-13 14:22:02 -0700684 mSize += mElements[ct].mSize * mArraySizes[ct];
Jason Sams718cd1f2009-12-23 14:35:29 -0800685 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800686 }
687
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700688 Element(int id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
689 super(id, rs);
Jason Sams252c0782011-01-11 17:42:52 -0800690 if ((dt != DataType.UNSIGNED_5_6_5) &&
691 (dt != DataType.UNSIGNED_4_4_4_4) &&
692 (dt != DataType.UNSIGNED_5_5_5_1)) {
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800693 if (size == 3) {
694 mSize = dt.mSize * 4;
695 } else {
696 mSize = dt.mSize * size;
697 }
Jason Sams252c0782011-01-11 17:42:52 -0800698 } else {
699 mSize = dt.mSize;
700 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800701 mType = dt;
702 mKind = dk;
703 mNormalized = norm;
704 mVectorSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700705 }
706
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700707 Element(int id, RenderScript rs) {
708 super(id, rs);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700709 }
710
711 @Override
712 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800713 super.updateFromNative();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700714
715 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
716 int[] dataBuffer = new int[5];
Jason Sams06d69de2010-11-09 17:11:40 -0800717 mRS.nElementGetNativeData(getID(), dataBuffer);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700718
719 mNormalized = dataBuffer[2] == 1 ? true : false;
720 mVectorSize = dataBuffer[3];
721 mSize = 0;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700722 for (DataType dt: DataType.values()) {
723 if(dt.mID == dataBuffer[0]){
724 mType = dt;
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700725 mSize = mType.mSize * mVectorSize;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700726 }
727 }
728 for (DataKind dk: DataKind.values()) {
729 if(dk.mID == dataBuffer[1]){
730 mKind = dk;
731 }
732 }
733
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700734 int numSubElements = dataBuffer[4];
735 if(numSubElements > 0) {
736 mElements = new Element[numSubElements];
737 mElementNames = new String[numSubElements];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700738 mArraySizes = new int[numSubElements];
739 mOffsetInBytes = new int[numSubElements];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700740
741 int[] subElementIds = new int[numSubElements];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700742 mRS.nElementGetSubElements(getID(), subElementIds, mElementNames, mArraySizes);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700743 for(int i = 0; i < numSubElements; i ++) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700744 mElements[i] = new Element(subElementIds[i], mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700745 mElements[i].updateFromNative();
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700746 mOffsetInBytes[i] = mSize;
747 mSize += mElements[i].mSize * mArraySizes[i];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700748 }
749 }
750
751 }
752
Jason Samsa1b13ed2010-11-12 14:58:37 -0800753 /**
754 * Create a custom Element of the specified DataType. The DataKind will be
755 * set to USER and the vector size to 1 indicating non-vector.
756 *
757 * @param rs The context associated with the new Element.
758 * @param dt The DataType for the new element.
759 * @return Element
760 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800761 static Element createUser(RenderScript rs, DataType dt) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700762 DataKind dk = DataKind.USER;
763 boolean norm = false;
764 int vecSize = 1;
765 int id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize);
766 return new Element(id, rs, dt, dk, norm, vecSize);
Jason Sams718cd1f2009-12-23 14:35:29 -0800767 }
768
Jason Samsa1b13ed2010-11-12 14:58:37 -0800769 /**
770 * Create a custom vector element of the specified DataType and vector size.
771 * DataKind will be set to USER.
772 *
773 * @param rs The context associated with the new Element.
774 * @param dt The DataType for the new element.
775 * @param size Vector size for the new Element. Range 2-4 inclusive
776 * supported.
777 *
778 * @return Element
779 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800780 public static Element createVector(RenderScript rs, DataType dt, int size) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800781 if (size < 2 || size > 4) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800782 throw new RSIllegalArgumentException("Vector size out of range 2-4.");
Jason Samsea84a7c2009-09-04 14:42:41 -0700783 }
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700784 DataKind dk = DataKind.USER;
785 boolean norm = false;
786 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
787 return new Element(id, rs, dt, dk, norm, size);
Jason Samsea84a7c2009-09-04 14:42:41 -0700788 }
789
Jason Samsa1b13ed2010-11-12 14:58:37 -0800790 /**
791 * Create a new pixel Element type. A matching DataType and DataKind must
792 * be provided. The DataType and DataKind must contain the same number of
793 * components. Vector size will be set to 1.
794 *
795 * @param rs The context associated with the new Element.
796 * @param dt The DataType for the new element.
797 * @param dk The DataKind to specify the mapping of each component in the
798 * DataType.
799 *
800 * @return Element
801 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800802 public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800803 if (!(dk == DataKind.PIXEL_L ||
804 dk == DataKind.PIXEL_A ||
805 dk == DataKind.PIXEL_LA ||
806 dk == DataKind.PIXEL_RGB ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700807 dk == DataKind.PIXEL_RGBA ||
808 dk == DataKind.PIXEL_DEPTH)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700809 throw new RSIllegalArgumentException("Unsupported DataKind");
Jason Sams718cd1f2009-12-23 14:35:29 -0800810 }
811 if (!(dt == DataType.UNSIGNED_8 ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700812 dt == DataType.UNSIGNED_16 ||
Jason Sams718cd1f2009-12-23 14:35:29 -0800813 dt == DataType.UNSIGNED_5_6_5 ||
814 dt == DataType.UNSIGNED_4_4_4_4 ||
815 dt == DataType.UNSIGNED_5_5_5_1)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700816 throw new RSIllegalArgumentException("Unsupported DataType");
Jason Sams718cd1f2009-12-23 14:35:29 -0800817 }
818 if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
Jason Samsc1d62102010-11-04 14:32:19 -0700819 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800820 }
821 if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700822 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800823 }
824 if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700825 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800826 }
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700827 if (dt == DataType.UNSIGNED_16 &&
828 dk != DataKind.PIXEL_DEPTH) {
829 throw new RSIllegalArgumentException("Bad kind and type combo");
830 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800831
832 int size = 1;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700833 switch (dk) {
834 case PIXEL_LA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800835 size = 2;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700836 break;
837 case PIXEL_RGB:
Jason Sams718cd1f2009-12-23 14:35:29 -0800838 size = 3;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700839 break;
840 case PIXEL_RGBA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800841 size = 4;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700842 break;
843 case PIXEL_DEPTH:
844 size = 2;
845 break;
Jason Sams718cd1f2009-12-23 14:35:29 -0800846 }
847
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700848 boolean norm = true;
849 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
850 return new Element(id, rs, dt, dk, norm, size);
Jason Sams718cd1f2009-12-23 14:35:29 -0800851 }
Jason Sams36e612a2009-07-31 16:26:13 -0700852
Jason Samsa1b13ed2010-11-12 14:58:37 -0800853 /**
Stephen Hinesf257e512011-06-14 14:54:29 -0700854 * Check if the current Element is compatible with another Element.
855 * Primitive Elements are compatible if they share the same underlying
856 * size and type (i.e. U8 is compatible with A_8). User-defined Elements
857 * must be equal in order to be compatible. This requires strict name
858 * equivalence for all sub-Elements (in addition to structural equivalence).
859 *
860 * @param e The Element to check compatibility with.
861 *
862 * @return boolean true if the Elements are compatible, otherwise false.
863 */
864 public boolean isCompatible(Element e) {
865 // Try strict BaseObj equality to start with.
866 if (this.equals(e)) {
867 return true;
868 }
869
870 // Ignore mKind because it is allowed to be different (user vs. pixel).
871 // We also ignore mNormalized because it can be different. The mType
872 // field must be non-null since we require name equivalence for
873 // user-created Elements.
874 return ((mSize == e.mSize) &&
875 (mType != null) &&
876 (mType == e.mType) &&
877 (mVectorSize == e.mVectorSize));
878 }
879
880 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800881 * Builder class for producing complex elements with matching field and name
882 * pairs. The builder starts empty. The order in which elements are added
883 * is retained for the layout in memory.
884 *
885 */
Jason Sams36e612a2009-07-31 16:26:13 -0700886 public static class Builder {
887 RenderScript mRS;
Jason Sams718cd1f2009-12-23 14:35:29 -0800888 Element[] mElements;
889 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -0700890 int[] mArraySizes;
Jason Sams718cd1f2009-12-23 14:35:29 -0800891 int mCount;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800892 int mSkipPadding;
Jason Sams22534172009-08-04 16:58:20 -0700893
Jason Samsa1b13ed2010-11-12 14:58:37 -0800894 /**
895 * Create a builder object.
896 *
897 * @param rs
898 */
Jason Sams22534172009-08-04 16:58:20 -0700899 public Builder(RenderScript rs) {
Jason Sams36e612a2009-07-31 16:26:13 -0700900 mRS = rs;
Jason Sams718cd1f2009-12-23 14:35:29 -0800901 mCount = 0;
902 mElements = new Element[8];
903 mElementNames = new String[8];
Jason Sams70d4e502010-09-02 17:35:23 -0700904 mArraySizes = new int[8];
Jason Sams36e612a2009-07-31 16:26:13 -0700905 }
906
Jason Samsa1b13ed2010-11-12 14:58:37 -0800907 /**
908 * Add an array of elements to this element.
909 *
910 * @param element
911 * @param name
912 * @param arraySize
913 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800914 public Builder add(Element element, String name, int arraySize) {
Jason Sams70d4e502010-09-02 17:35:23 -0700915 if (arraySize < 1) {
Jason Samsc1d62102010-11-04 14:32:19 -0700916 throw new RSIllegalArgumentException("Array size cannot be less than 1.");
Jason Sams70d4e502010-09-02 17:35:23 -0700917 }
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800918
919 // Skip padding fields after a vector 3 type.
920 if (mSkipPadding != 0) {
921 if (name.startsWith("#padding_")) {
922 mSkipPadding = 0;
923 return this;
924 }
925 }
926
927 if (element.mVectorSize == 3) {
928 mSkipPadding = 1;
929 } else {
930 mSkipPadding = 0;
931 }
932
Jason Sams718cd1f2009-12-23 14:35:29 -0800933 if(mCount == mElements.length) {
934 Element[] e = new Element[mCount + 8];
935 String[] s = new String[mCount + 8];
Jason Sams70d4e502010-09-02 17:35:23 -0700936 int[] as = new int[mCount + 8];
Jason Sams718cd1f2009-12-23 14:35:29 -0800937 System.arraycopy(mElements, 0, e, 0, mCount);
938 System.arraycopy(mElementNames, 0, s, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -0700939 System.arraycopy(mArraySizes, 0, as, 0, mCount);
Jason Sams718cd1f2009-12-23 14:35:29 -0800940 mElements = e;
941 mElementNames = s;
Jason Sams70d4e502010-09-02 17:35:23 -0700942 mArraySizes = as;
Jason Sams36e612a2009-07-31 16:26:13 -0700943 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800944 mElements[mCount] = element;
945 mElementNames[mCount] = name;
Jason Sams70d4e502010-09-02 17:35:23 -0700946 mArraySizes[mCount] = arraySize;
Jason Sams718cd1f2009-12-23 14:35:29 -0800947 mCount++;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800948 return this;
Jason Sams07ae4062009-08-27 20:23:34 -0700949 }
950
Jason Samsa1b13ed2010-11-12 14:58:37 -0800951 /**
952 * Add a single element to this Element.
953 *
954 * @param element
955 * @param name
956 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800957 public Builder add(Element element, String name) {
958 return add(element, name, 1);
Jason Sams70d4e502010-09-02 17:35:23 -0700959 }
960
Jason Samsa1b13ed2010-11-12 14:58:37 -0800961 /**
962 * Create the element from this builder.
963 *
964 *
965 * @return Element
966 */
Jason Sams22534172009-08-04 16:58:20 -0700967 public Element create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800968 mRS.validate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800969 Element[] ein = new Element[mCount];
970 String[] sin = new String[mCount];
Jason Sams70d4e502010-09-02 17:35:23 -0700971 int[] asin = new int[mCount];
Jason Sams718cd1f2009-12-23 14:35:29 -0800972 java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
973 java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -0700974 java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700975
976 int[] ids = new int[ein.length];
977 for (int ct = 0; ct < ein.length; ct++ ) {
Jason Sams06d69de2010-11-09 17:11:40 -0800978 ids[ct] = ein[ct].getID();
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700979 }
Jason Sams70d4e502010-09-02 17:35:23 -0700980 int id = mRS.nElementCreate2(ids, sin, asin);
981 return new Element(id, mRS, ein, sin, asin);
Jason Sams36e612a2009-07-31 16:26:13 -0700982 }
983 }
Jason Sams36e612a2009-07-31 16:26:13 -0700984}
985