blob: 8a9ca855099241c17cbb5746b0e3414489a093e4 [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)) {
693 mSize = dt.mSize * size;
694 } else {
695 mSize = dt.mSize;
696 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800697 mType = dt;
698 mKind = dk;
699 mNormalized = norm;
700 mVectorSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700701 }
702
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700703 Element(int id, RenderScript rs) {
704 super(id, rs);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700705 }
706
707 @Override
708 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800709 super.updateFromNative();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700710
711 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
712 int[] dataBuffer = new int[5];
Jason Sams06d69de2010-11-09 17:11:40 -0800713 mRS.nElementGetNativeData(getID(), dataBuffer);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700714
715 mNormalized = dataBuffer[2] == 1 ? true : false;
716 mVectorSize = dataBuffer[3];
717 mSize = 0;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700718 for (DataType dt: DataType.values()) {
719 if(dt.mID == dataBuffer[0]){
720 mType = dt;
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700721 mSize = mType.mSize * mVectorSize;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700722 }
723 }
724 for (DataKind dk: DataKind.values()) {
725 if(dk.mID == dataBuffer[1]){
726 mKind = dk;
727 }
728 }
729
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700730 int numSubElements = dataBuffer[4];
731 if(numSubElements > 0) {
732 mElements = new Element[numSubElements];
733 mElementNames = new String[numSubElements];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700734 mArraySizes = new int[numSubElements];
735 mOffsetInBytes = new int[numSubElements];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700736
737 int[] subElementIds = new int[numSubElements];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700738 mRS.nElementGetSubElements(getID(), subElementIds, mElementNames, mArraySizes);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700739 for(int i = 0; i < numSubElements; i ++) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700740 mElements[i] = new Element(subElementIds[i], mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700741 mElements[i].updateFromNative();
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700742 mOffsetInBytes[i] = mSize;
743 mSize += mElements[i].mSize * mArraySizes[i];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700744 }
745 }
746
747 }
748
Jason Samsa1b13ed2010-11-12 14:58:37 -0800749 /**
750 * Create a custom Element of the specified DataType. The DataKind will be
751 * set to USER and the vector size to 1 indicating non-vector.
752 *
753 * @param rs The context associated with the new Element.
754 * @param dt The DataType for the new element.
755 * @return Element
756 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800757 static Element createUser(RenderScript rs, DataType dt) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700758 DataKind dk = DataKind.USER;
759 boolean norm = false;
760 int vecSize = 1;
761 int id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize);
762 return new Element(id, rs, dt, dk, norm, vecSize);
Jason Sams718cd1f2009-12-23 14:35:29 -0800763 }
764
Jason Samsa1b13ed2010-11-12 14:58:37 -0800765 /**
766 * Create a custom vector element of the specified DataType and vector size.
767 * DataKind will be set to USER.
768 *
769 * @param rs The context associated with the new Element.
770 * @param dt The DataType for the new element.
771 * @param size Vector size for the new Element. Range 2-4 inclusive
772 * supported.
773 *
774 * @return Element
775 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800776 public static Element createVector(RenderScript rs, DataType dt, int size) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800777 if (size < 2 || size > 4) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800778 throw new RSIllegalArgumentException("Vector size out of range 2-4.");
Jason Samsea84a7c2009-09-04 14:42:41 -0700779 }
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700780 DataKind dk = DataKind.USER;
781 boolean norm = false;
782 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
783 return new Element(id, rs, dt, dk, norm, size);
Jason Samsea84a7c2009-09-04 14:42:41 -0700784 }
785
Jason Samsa1b13ed2010-11-12 14:58:37 -0800786 /**
787 * Create a new pixel Element type. A matching DataType and DataKind must
788 * be provided. The DataType and DataKind must contain the same number of
789 * components. Vector size will be set to 1.
790 *
791 * @param rs The context associated with the new Element.
792 * @param dt The DataType for the new element.
793 * @param dk The DataKind to specify the mapping of each component in the
794 * DataType.
795 *
796 * @return Element
797 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800798 public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800799 if (!(dk == DataKind.PIXEL_L ||
800 dk == DataKind.PIXEL_A ||
801 dk == DataKind.PIXEL_LA ||
802 dk == DataKind.PIXEL_RGB ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700803 dk == DataKind.PIXEL_RGBA ||
804 dk == DataKind.PIXEL_DEPTH)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700805 throw new RSIllegalArgumentException("Unsupported DataKind");
Jason Sams718cd1f2009-12-23 14:35:29 -0800806 }
807 if (!(dt == DataType.UNSIGNED_8 ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700808 dt == DataType.UNSIGNED_16 ||
Jason Sams718cd1f2009-12-23 14:35:29 -0800809 dt == DataType.UNSIGNED_5_6_5 ||
810 dt == DataType.UNSIGNED_4_4_4_4 ||
811 dt == DataType.UNSIGNED_5_5_5_1)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700812 throw new RSIllegalArgumentException("Unsupported DataType");
Jason Sams718cd1f2009-12-23 14:35:29 -0800813 }
814 if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
Jason Samsc1d62102010-11-04 14:32:19 -0700815 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800816 }
817 if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700818 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800819 }
820 if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700821 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800822 }
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700823 if (dt == DataType.UNSIGNED_16 &&
824 dk != DataKind.PIXEL_DEPTH) {
825 throw new RSIllegalArgumentException("Bad kind and type combo");
826 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800827
828 int size = 1;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700829 switch (dk) {
830 case PIXEL_LA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800831 size = 2;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700832 break;
833 case PIXEL_RGB:
Jason Sams718cd1f2009-12-23 14:35:29 -0800834 size = 3;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700835 break;
836 case PIXEL_RGBA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800837 size = 4;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700838 break;
839 case PIXEL_DEPTH:
840 size = 2;
841 break;
Jason Sams718cd1f2009-12-23 14:35:29 -0800842 }
843
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700844 boolean norm = true;
845 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
846 return new Element(id, rs, dt, dk, norm, size);
Jason Sams718cd1f2009-12-23 14:35:29 -0800847 }
Jason Sams36e612a2009-07-31 16:26:13 -0700848
Jason Samsa1b13ed2010-11-12 14:58:37 -0800849 /**
Stephen Hinesf257e512011-06-14 14:54:29 -0700850 * Check if the current Element is compatible with another Element.
851 * Primitive Elements are compatible if they share the same underlying
852 * size and type (i.e. U8 is compatible with A_8). User-defined Elements
853 * must be equal in order to be compatible. This requires strict name
854 * equivalence for all sub-Elements (in addition to structural equivalence).
855 *
856 * @param e The Element to check compatibility with.
857 *
858 * @return boolean true if the Elements are compatible, otherwise false.
859 */
860 public boolean isCompatible(Element e) {
861 // Try strict BaseObj equality to start with.
862 if (this.equals(e)) {
863 return true;
864 }
865
866 // Ignore mKind because it is allowed to be different (user vs. pixel).
867 // We also ignore mNormalized because it can be different. The mType
868 // field must be non-null since we require name equivalence for
869 // user-created Elements.
870 return ((mSize == e.mSize) &&
871 (mType != null) &&
872 (mType == e.mType) &&
873 (mVectorSize == e.mVectorSize));
874 }
875
876 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800877 * Builder class for producing complex elements with matching field and name
878 * pairs. The builder starts empty. The order in which elements are added
879 * is retained for the layout in memory.
880 *
881 */
Jason Sams36e612a2009-07-31 16:26:13 -0700882 public static class Builder {
883 RenderScript mRS;
Jason Sams718cd1f2009-12-23 14:35:29 -0800884 Element[] mElements;
885 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -0700886 int[] mArraySizes;
Jason Sams718cd1f2009-12-23 14:35:29 -0800887 int mCount;
Jason Sams22534172009-08-04 16:58:20 -0700888
Jason Samsa1b13ed2010-11-12 14:58:37 -0800889 /**
890 * Create a builder object.
891 *
892 * @param rs
893 */
Jason Sams22534172009-08-04 16:58:20 -0700894 public Builder(RenderScript rs) {
Jason Sams36e612a2009-07-31 16:26:13 -0700895 mRS = rs;
Jason Sams718cd1f2009-12-23 14:35:29 -0800896 mCount = 0;
897 mElements = new Element[8];
898 mElementNames = new String[8];
Jason Sams70d4e502010-09-02 17:35:23 -0700899 mArraySizes = new int[8];
Jason Sams36e612a2009-07-31 16:26:13 -0700900 }
901
Jason Samsa1b13ed2010-11-12 14:58:37 -0800902 /**
903 * Add an array of elements to this element.
904 *
905 * @param element
906 * @param name
907 * @param arraySize
908 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800909 public Builder add(Element element, String name, int arraySize) {
Jason Sams70d4e502010-09-02 17:35:23 -0700910 if (arraySize < 1) {
Jason Samsc1d62102010-11-04 14:32:19 -0700911 throw new RSIllegalArgumentException("Array size cannot be less than 1.");
Jason Sams70d4e502010-09-02 17:35:23 -0700912 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800913 if(mCount == mElements.length) {
914 Element[] e = new Element[mCount + 8];
915 String[] s = new String[mCount + 8];
Jason Sams70d4e502010-09-02 17:35:23 -0700916 int[] as = new int[mCount + 8];
Jason Sams718cd1f2009-12-23 14:35:29 -0800917 System.arraycopy(mElements, 0, e, 0, mCount);
918 System.arraycopy(mElementNames, 0, s, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -0700919 System.arraycopy(mArraySizes, 0, as, 0, mCount);
Jason Sams718cd1f2009-12-23 14:35:29 -0800920 mElements = e;
921 mElementNames = s;
Jason Sams70d4e502010-09-02 17:35:23 -0700922 mArraySizes = as;
Jason Sams36e612a2009-07-31 16:26:13 -0700923 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800924 mElements[mCount] = element;
925 mElementNames[mCount] = name;
Jason Sams70d4e502010-09-02 17:35:23 -0700926 mArraySizes[mCount] = arraySize;
Jason Sams718cd1f2009-12-23 14:35:29 -0800927 mCount++;
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800928 return this;
Jason Sams07ae4062009-08-27 20:23:34 -0700929 }
930
Jason Samsa1b13ed2010-11-12 14:58:37 -0800931 /**
932 * Add a single element to this Element.
933 *
934 * @param element
935 * @param name
936 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800937 public Builder add(Element element, String name) {
938 return add(element, name, 1);
Jason Sams70d4e502010-09-02 17:35:23 -0700939 }
940
Jason Samsa1b13ed2010-11-12 14:58:37 -0800941 /**
942 * Create the element from this builder.
943 *
944 *
945 * @return Element
946 */
Jason Sams22534172009-08-04 16:58:20 -0700947 public Element create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800948 mRS.validate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800949 Element[] ein = new Element[mCount];
950 String[] sin = new String[mCount];
Jason Sams70d4e502010-09-02 17:35:23 -0700951 int[] asin = new int[mCount];
Jason Sams718cd1f2009-12-23 14:35:29 -0800952 java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
953 java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -0700954 java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700955
956 int[] ids = new int[ein.length];
957 for (int ct = 0; ct < ein.length; ct++ ) {
Jason Sams06d69de2010-11-09 17:11:40 -0800958 ids[ct] = ein[ct].getID();
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700959 }
Jason Sams70d4e502010-09-02 17:35:23 -0700960 int id = mRS.nElementCreate2(ids, sin, asin);
961 return new Element(id, mRS, ein, sin, asin);
Jason Sams36e612a2009-07-31 16:26:13 -0700962 }
963 }
Jason Sams36e612a2009-07-31 16:26:13 -0700964}
965