Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.renderscript; |
| 18 | |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 19 | import java.lang.reflect.Field; |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 20 | import android.util.Log; |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 21 | |
| 22 | /** |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 23 | * <p>The most basic data type. An element represents one cell of a memory allocation. |
Alex Sakhartchouk | 3476977 | 2011-02-28 16:01:28 -0800 | [diff] [blame] | 24 | * Element is the basic data type of Renderscript. An element can be of two forms: Basic elements or Complex forms. |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 25 | * 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 Sakhartchouk | 3476977 | 2011-02-28 16:01:28 -0800 | [diff] [blame] | 32 | * <p>Complex elements contain a list of sub-elements and names that |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 33 | * represents a structure of data. The fields can be accessed by name |
| 34 | * from a script or shader. The memory layout is defined and ordered. Data |
| 35 | * alignment is determinied by the most basic primitive type. i.e. a float4 |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 36 | * vector will be alligned to sizeof(float) and not sizeof(float4). The |
| 37 | * ordering of elements in memory will be the order in which they were added |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 38 | * with each component aligned as necessary. No re-ordering will be done.</p> |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 39 | * |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 40 | * <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 Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 43 | **/ |
| 44 | public class Element extends BaseObj { |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 45 | int mSize; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 46 | Element[] mElements; |
| 47 | String[] mElementNames; |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 48 | int[] mArraySizes; |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 49 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 50 | DataType mType; |
| 51 | DataKind mKind; |
| 52 | boolean mNormalized; |
| 53 | int mVectorSize; |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 54 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 55 | int getSizeBytes() {return mSize;} |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 56 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * DataType represents the basic type information for a basic element. The |
| 60 | * naming convention follows. For numeric types its FLOAT, SIGNED, UNSIGNED |
| 61 | * followed by the _BITS where BITS is the size of the data. BOOLEAN is a |
| 62 | * true / false (1,0) represented in an 8 bit container. The UNSIGNED |
| 63 | * variants with multiple bit definitions are for packed graphical data |
| 64 | * formats and represents vectors with per vector member sizes which are |
| 65 | * treated as a single unit for packing and alignment purposes. |
| 66 | * |
| 67 | * MATRIX the three matrix types contain FLOAT_32 elements and are treated |
| 68 | * as 32 bits for alignment purposes. |
| 69 | * |
| 70 | * RS_* objects. 32 bit opaque handles. |
| 71 | */ |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 72 | public enum DataType { |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 73 | //FLOAT_16 (1, 2), |
| 74 | FLOAT_32 (2, 4), |
Stephen Hines | 02f41705 | 2010-09-30 15:19:22 -0700 | [diff] [blame] | 75 | FLOAT_64 (3, 8), |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 76 | SIGNED_8 (4, 1), |
| 77 | SIGNED_16 (5, 2), |
| 78 | SIGNED_32 (6, 4), |
Stephen Hines | ef1dac2 | 2010-10-01 15:39:33 -0700 | [diff] [blame] | 79 | SIGNED_64 (7, 8), |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 80 | UNSIGNED_8 (8, 1), |
| 81 | UNSIGNED_16 (9, 2), |
| 82 | UNSIGNED_32 (10, 4), |
Stephen Hines | 52d8363 | 2010-10-11 16:10:42 -0700 | [diff] [blame] | 83 | UNSIGNED_64 (11, 8), |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 84 | |
Jason Sams | f110d4b | 2010-06-21 17:42:41 -0700 | [diff] [blame] | 85 | BOOLEAN(12, 1), |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 86 | |
Jason Sams | f110d4b | 2010-06-21 17:42:41 -0700 | [diff] [blame] | 87 | UNSIGNED_5_6_5 (13, 2), |
| 88 | UNSIGNED_5_5_5_1 (14, 2), |
| 89 | UNSIGNED_4_4_4_4 (15, 2), |
| 90 | |
Jason Sams | 1d45c47 | 2010-08-25 14:31:48 -0700 | [diff] [blame] | 91 | MATRIX_4X4 (16, 64), |
| 92 | MATRIX_3X3 (17, 36), |
| 93 | MATRIX_2X2 (18, 16), |
| 94 | |
| 95 | RS_ELEMENT (1000, 4), |
| 96 | RS_TYPE (1001, 4), |
| 97 | RS_ALLOCATION (1002, 4), |
| 98 | RS_SAMPLER (1003, 4), |
| 99 | RS_SCRIPT (1004, 4), |
| 100 | RS_MESH (1005, 4), |
| 101 | RS_PROGRAM_FRAGMENT (1006, 4), |
| 102 | RS_PROGRAM_VERTEX (1007, 4), |
| 103 | RS_PROGRAM_RASTER (1008, 4), |
| 104 | RS_PROGRAM_STORE (1009, 4); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 105 | |
| 106 | int mID; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 107 | int mSize; |
| 108 | DataType(int id, int size) { |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 109 | mID = id; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 110 | mSize = size; |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 114 | /** |
| 115 | * The special interpretation of the data if required. This is primarly |
| 116 | * useful for graphical data. USER indicates no special interpretation is |
| 117 | * expected. PIXEL is used in conjunction with the standard data types for |
| 118 | * representing texture formats. |
| 119 | */ |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 120 | public enum DataKind { |
| 121 | USER (0), |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 122 | |
| 123 | PIXEL_L (7), |
| 124 | PIXEL_A (8), |
| 125 | PIXEL_LA (9), |
| 126 | PIXEL_RGB (10), |
| 127 | PIXEL_RGBA (11); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 128 | |
| 129 | int mID; |
| 130 | DataKind(int id) { |
| 131 | mID = id; |
| 132 | } |
| 133 | } |
| 134 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 135 | /** |
| 136 | * Return if a element is too complex for use as a data source for a Mesh or |
| 137 | * a Program. |
| 138 | * |
| 139 | * @return boolean |
| 140 | */ |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 141 | public boolean isComplex() { |
| 142 | if (mElements == null) { |
| 143 | return false; |
| 144 | } |
| 145 | for (int ct=0; ct < mElements.length; ct++) { |
| 146 | if (mElements[ct].mElements != null) { |
| 147 | return true; |
| 148 | } |
| 149 | } |
| 150 | return false; |
| 151 | } |
| 152 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 153 | /** |
| 154 | * Utility function for returning an Element containing a single Boolean. |
| 155 | * |
| 156 | * @param rs Context to which the element will belong. |
| 157 | * |
| 158 | * @return Element |
| 159 | */ |
Jason Sams | f110d4b | 2010-06-21 17:42:41 -0700 | [diff] [blame] | 160 | public static Element BOOLEAN(RenderScript rs) { |
| 161 | if(rs.mElement_BOOLEAN == null) { |
| 162 | rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN); |
| 163 | } |
| 164 | return rs.mElement_BOOLEAN; |
| 165 | } |
| 166 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 167 | /** |
| 168 | * Utility function for returning an Element containing a single UNSIGNED_8. |
| 169 | * |
| 170 | * @param rs Context to which the element will belong. |
| 171 | * |
| 172 | * @return Element |
| 173 | */ |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 174 | public static Element U8(RenderScript rs) { |
| 175 | if(rs.mElement_U8 == null) { |
| 176 | rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 177 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 178 | return rs.mElement_U8; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 181 | /** |
| 182 | * Utility function for returning an Element containing a single SIGNED_8. |
| 183 | * |
| 184 | * @param rs Context to which the element will belong. |
| 185 | * |
| 186 | * @return Element |
| 187 | */ |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 188 | public static Element I8(RenderScript rs) { |
| 189 | if(rs.mElement_I8 == null) { |
| 190 | rs.mElement_I8 = createUser(rs, DataType.SIGNED_8); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 191 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 192 | return rs.mElement_I8; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Jason Sams | e29f3e7 | 2010-06-08 15:40:48 -0700 | [diff] [blame] | 195 | public static Element U16(RenderScript rs) { |
| 196 | if(rs.mElement_U16 == null) { |
| 197 | rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16); |
| 198 | } |
| 199 | return rs.mElement_U16; |
| 200 | } |
| 201 | |
| 202 | public static Element I16(RenderScript rs) { |
| 203 | if(rs.mElement_I16 == null) { |
| 204 | rs.mElement_I16 = createUser(rs, DataType.SIGNED_16); |
| 205 | } |
| 206 | return rs.mElement_I16; |
| 207 | } |
| 208 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 209 | public static Element U32(RenderScript rs) { |
| 210 | if(rs.mElement_U32 == null) { |
| 211 | rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 212 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 213 | return rs.mElement_U32; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 216 | public static Element I32(RenderScript rs) { |
| 217 | if(rs.mElement_I32 == null) { |
| 218 | rs.mElement_I32 = createUser(rs, DataType.SIGNED_32); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 219 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 220 | return rs.mElement_I32; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Stephen Hines | 52d8363 | 2010-10-11 16:10:42 -0700 | [diff] [blame] | 223 | public static Element U64(RenderScript rs) { |
| 224 | if(rs.mElement_U64 == null) { |
| 225 | rs.mElement_U64 = createUser(rs, DataType.UNSIGNED_64); |
| 226 | } |
| 227 | return rs.mElement_U64; |
| 228 | } |
| 229 | |
Stephen Hines | ef1dac2 | 2010-10-01 15:39:33 -0700 | [diff] [blame] | 230 | public static Element I64(RenderScript rs) { |
| 231 | if(rs.mElement_I64 == null) { |
| 232 | rs.mElement_I64 = createUser(rs, DataType.SIGNED_64); |
| 233 | } |
| 234 | return rs.mElement_I64; |
| 235 | } |
| 236 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 237 | public static Element F32(RenderScript rs) { |
| 238 | if(rs.mElement_F32 == null) { |
| 239 | rs.mElement_F32 = createUser(rs, DataType.FLOAT_32); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 240 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 241 | return rs.mElement_F32; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Stephen Hines | 02f41705 | 2010-09-30 15:19:22 -0700 | [diff] [blame] | 244 | public static Element F64(RenderScript rs) { |
| 245 | if(rs.mElement_F64 == null) { |
| 246 | rs.mElement_F64 = createUser(rs, DataType.FLOAT_64); |
| 247 | } |
| 248 | return rs.mElement_F64; |
| 249 | } |
| 250 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 251 | public static Element ELEMENT(RenderScript rs) { |
| 252 | if(rs.mElement_ELEMENT == null) { |
| 253 | rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 254 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 255 | return rs.mElement_ELEMENT; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 258 | public static Element TYPE(RenderScript rs) { |
| 259 | if(rs.mElement_TYPE == null) { |
| 260 | rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 261 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 262 | return rs.mElement_TYPE; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 265 | public static Element ALLOCATION(RenderScript rs) { |
| 266 | if(rs.mElement_ALLOCATION == null) { |
| 267 | rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 268 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 269 | return rs.mElement_ALLOCATION; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 272 | public static Element SAMPLER(RenderScript rs) { |
| 273 | if(rs.mElement_SAMPLER == null) { |
| 274 | rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 275 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 276 | return rs.mElement_SAMPLER; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 279 | public static Element SCRIPT(RenderScript rs) { |
| 280 | if(rs.mElement_SCRIPT == null) { |
| 281 | rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 282 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 283 | return rs.mElement_SCRIPT; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 286 | public static Element MESH(RenderScript rs) { |
| 287 | if(rs.mElement_MESH == null) { |
| 288 | rs.mElement_MESH = createUser(rs, DataType.RS_MESH); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 289 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 290 | return rs.mElement_MESH; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 293 | public static Element PROGRAM_FRAGMENT(RenderScript rs) { |
| 294 | if(rs.mElement_PROGRAM_FRAGMENT == null) { |
| 295 | rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 296 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 297 | return rs.mElement_PROGRAM_FRAGMENT; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 300 | public static Element PROGRAM_VERTEX(RenderScript rs) { |
| 301 | if(rs.mElement_PROGRAM_VERTEX == null) { |
| 302 | rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 303 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 304 | return rs.mElement_PROGRAM_VERTEX; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 307 | public static Element PROGRAM_RASTER(RenderScript rs) { |
| 308 | if(rs.mElement_PROGRAM_RASTER == null) { |
| 309 | rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 310 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 311 | return rs.mElement_PROGRAM_RASTER; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 314 | public static Element PROGRAM_STORE(RenderScript rs) { |
| 315 | if(rs.mElement_PROGRAM_STORE == null) { |
| 316 | rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 317 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 318 | return rs.mElement_PROGRAM_STORE; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 322 | public static Element A_8(RenderScript rs) { |
| 323 | if(rs.mElement_A_8 == null) { |
| 324 | rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A); |
| 325 | } |
| 326 | return rs.mElement_A_8; |
| 327 | } |
| 328 | |
| 329 | public static Element RGB_565(RenderScript rs) { |
| 330 | if(rs.mElement_RGB_565 == null) { |
| 331 | rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB); |
| 332 | } |
| 333 | return rs.mElement_RGB_565; |
| 334 | } |
| 335 | |
| 336 | public static Element RGB_888(RenderScript rs) { |
| 337 | if(rs.mElement_RGB_888 == null) { |
| 338 | rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB); |
| 339 | } |
| 340 | return rs.mElement_RGB_888; |
| 341 | } |
| 342 | |
| 343 | public static Element RGBA_5551(RenderScript rs) { |
| 344 | if(rs.mElement_RGBA_5551 == null) { |
| 345 | rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA); |
| 346 | } |
| 347 | return rs.mElement_RGBA_5551; |
| 348 | } |
| 349 | |
| 350 | public static Element RGBA_4444(RenderScript rs) { |
| 351 | if(rs.mElement_RGBA_4444 == null) { |
| 352 | rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA); |
| 353 | } |
| 354 | return rs.mElement_RGBA_4444; |
| 355 | } |
| 356 | |
| 357 | public static Element RGBA_8888(RenderScript rs) { |
| 358 | if(rs.mElement_RGBA_8888 == null) { |
| 359 | rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA); |
| 360 | } |
| 361 | return rs.mElement_RGBA_8888; |
| 362 | } |
| 363 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 364 | public static Element F32_2(RenderScript rs) { |
| 365 | if(rs.mElement_FLOAT_2 == null) { |
| 366 | rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 367 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 368 | return rs.mElement_FLOAT_2; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 369 | } |
| 370 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 371 | public static Element F32_3(RenderScript rs) { |
| 372 | if(rs.mElement_FLOAT_3 == null) { |
| 373 | rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 374 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 375 | return rs.mElement_FLOAT_3; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 376 | } |
| 377 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 378 | public static Element F32_4(RenderScript rs) { |
| 379 | if(rs.mElement_FLOAT_4 == null) { |
| 380 | rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 381 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 382 | return rs.mElement_FLOAT_4; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 383 | } |
| 384 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 385 | public static Element U8_4(RenderScript rs) { |
| 386 | if(rs.mElement_UCHAR_4 == null) { |
| 387 | rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 388 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 389 | return rs.mElement_UCHAR_4; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 390 | } |
| 391 | |
Jason Sams | 1d45c47 | 2010-08-25 14:31:48 -0700 | [diff] [blame] | 392 | public static Element MATRIX_4X4(RenderScript rs) { |
| 393 | if(rs.mElement_MATRIX_4X4 == null) { |
| 394 | rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4); |
| 395 | } |
| 396 | return rs.mElement_MATRIX_4X4; |
| 397 | } |
| 398 | public static Element MATRIX4X4(RenderScript rs) { |
| 399 | return MATRIX_4X4(rs); |
| 400 | } |
| 401 | |
| 402 | public static Element MATRIX_3X3(RenderScript rs) { |
| 403 | if(rs.mElement_MATRIX_3X3 == null) { |
| 404 | rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3); |
| 405 | } |
Alex Sakhartchouk | 3476977 | 2011-02-28 16:01:28 -0800 | [diff] [blame] | 406 | return rs.mElement_MATRIX_3X3; |
Jason Sams | 1d45c47 | 2010-08-25 14:31:48 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | public static Element MATRIX_2X2(RenderScript rs) { |
| 410 | if(rs.mElement_MATRIX_2X2 == null) { |
| 411 | rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2); |
| 412 | } |
| 413 | return rs.mElement_MATRIX_2X2; |
| 414 | } |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 415 | |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 416 | Element(int id, RenderScript rs, Element[] e, String[] n, int[] as) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 417 | super(id, rs); |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 418 | mSize = 0; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 419 | mElements = e; |
| 420 | mElementNames = n; |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 421 | mArraySizes = as; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 422 | for (int ct = 0; ct < mElements.length; ct++ ) { |
Alex Sakhartchouk | 9e401bc | 2010-10-13 14:22:02 -0700 | [diff] [blame] | 423 | mSize += mElements[ct].mSize * mArraySizes[ct]; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 424 | } |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 425 | } |
| 426 | |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 427 | Element(int id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) { |
| 428 | super(id, rs); |
Jason Sams | 252c078 | 2011-01-11 17:42:52 -0800 | [diff] [blame] | 429 | if ((dt != DataType.UNSIGNED_5_6_5) && |
| 430 | (dt != DataType.UNSIGNED_4_4_4_4) && |
| 431 | (dt != DataType.UNSIGNED_5_5_5_1)) { |
| 432 | mSize = dt.mSize * size; |
| 433 | } else { |
| 434 | mSize = dt.mSize; |
| 435 | } |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 436 | mType = dt; |
| 437 | mKind = dk; |
| 438 | mNormalized = norm; |
| 439 | mVectorSize = size; |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 442 | Element(int id, RenderScript rs) { |
| 443 | super(id, rs); |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | @Override |
| 447 | void updateFromNative() { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 448 | super.updateFromNative(); |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 449 | |
| 450 | // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements |
| 451 | int[] dataBuffer = new int[5]; |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 452 | mRS.nElementGetNativeData(getID(), dataBuffer); |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 453 | |
| 454 | mNormalized = dataBuffer[2] == 1 ? true : false; |
| 455 | mVectorSize = dataBuffer[3]; |
| 456 | mSize = 0; |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 457 | for (DataType dt: DataType.values()) { |
| 458 | if(dt.mID == dataBuffer[0]){ |
| 459 | mType = dt; |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 460 | mSize = mType.mSize * mVectorSize; |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | for (DataKind dk: DataKind.values()) { |
| 464 | if(dk.mID == dataBuffer[1]){ |
| 465 | mKind = dk; |
| 466 | } |
| 467 | } |
| 468 | |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 469 | int numSubElements = dataBuffer[4]; |
| 470 | if(numSubElements > 0) { |
| 471 | mElements = new Element[numSubElements]; |
| 472 | mElementNames = new String[numSubElements]; |
| 473 | |
| 474 | int[] subElementIds = new int[numSubElements]; |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 475 | mRS.nElementGetSubElements(getID(), subElementIds, mElementNames); |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 476 | for(int i = 0; i < numSubElements; i ++) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 477 | mElements[i] = new Element(subElementIds[i], mRS); |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 478 | mElements[i].updateFromNative(); |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 479 | mSize += mElements[i].mSize; |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | |
| 483 | } |
| 484 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 485 | /** |
| 486 | * Create a custom Element of the specified DataType. The DataKind will be |
| 487 | * set to USER and the vector size to 1 indicating non-vector. |
| 488 | * |
| 489 | * @param rs The context associated with the new Element. |
| 490 | * @param dt The DataType for the new element. |
| 491 | * @return Element |
| 492 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 493 | static Element createUser(RenderScript rs, DataType dt) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 494 | DataKind dk = DataKind.USER; |
| 495 | boolean norm = false; |
| 496 | int vecSize = 1; |
| 497 | int id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize); |
| 498 | return new Element(id, rs, dt, dk, norm, vecSize); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 499 | } |
| 500 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 501 | /** |
| 502 | * Create a custom vector element of the specified DataType and vector size. |
| 503 | * DataKind will be set to USER. |
| 504 | * |
| 505 | * @param rs The context associated with the new Element. |
| 506 | * @param dt The DataType for the new element. |
| 507 | * @param size Vector size for the new Element. Range 2-4 inclusive |
| 508 | * supported. |
| 509 | * |
| 510 | * @return Element |
| 511 | */ |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 512 | public static Element createVector(RenderScript rs, DataType dt, int size) { |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 513 | if (size < 2 || size > 4) { |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 514 | throw new RSIllegalArgumentException("Vector size out of range 2-4."); |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 515 | } |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 516 | DataKind dk = DataKind.USER; |
| 517 | boolean norm = false; |
| 518 | int id = rs.nElementCreate(dt.mID, dk.mID, norm, size); |
| 519 | return new Element(id, rs, dt, dk, norm, size); |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 522 | /** |
| 523 | * Create a new pixel Element type. A matching DataType and DataKind must |
| 524 | * be provided. The DataType and DataKind must contain the same number of |
| 525 | * components. Vector size will be set to 1. |
| 526 | * |
| 527 | * @param rs The context associated with the new Element. |
| 528 | * @param dt The DataType for the new element. |
| 529 | * @param dk The DataKind to specify the mapping of each component in the |
| 530 | * DataType. |
| 531 | * |
| 532 | * @return Element |
| 533 | */ |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 534 | public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) { |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 535 | if (!(dk == DataKind.PIXEL_L || |
| 536 | dk == DataKind.PIXEL_A || |
| 537 | dk == DataKind.PIXEL_LA || |
| 538 | dk == DataKind.PIXEL_RGB || |
| 539 | dk == DataKind.PIXEL_RGBA)) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 540 | throw new RSIllegalArgumentException("Unsupported DataKind"); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 541 | } |
| 542 | if (!(dt == DataType.UNSIGNED_8 || |
| 543 | dt == DataType.UNSIGNED_5_6_5 || |
| 544 | dt == DataType.UNSIGNED_4_4_4_4 || |
| 545 | dt == DataType.UNSIGNED_5_5_5_1)) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 546 | throw new RSIllegalArgumentException("Unsupported DataType"); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 547 | } |
| 548 | if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 549 | throw new RSIllegalArgumentException("Bad kind and type combo"); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 550 | } |
| 551 | if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 552 | throw new RSIllegalArgumentException("Bad kind and type combo"); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 553 | } |
| 554 | if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 555 | throw new RSIllegalArgumentException("Bad kind and type combo"); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | int size = 1; |
| 559 | if (dk == DataKind.PIXEL_LA) { |
| 560 | size = 2; |
| 561 | } |
| 562 | if (dk == DataKind.PIXEL_RGB) { |
| 563 | size = 3; |
| 564 | } |
| 565 | if (dk == DataKind.PIXEL_RGBA) { |
| 566 | size = 4; |
| 567 | } |
| 568 | |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 569 | boolean norm = true; |
| 570 | int id = rs.nElementCreate(dt.mID, dk.mID, norm, size); |
| 571 | return new Element(id, rs, dt, dk, norm, size); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 572 | } |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 573 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 574 | /** |
| 575 | * Builder class for producing complex elements with matching field and name |
| 576 | * pairs. The builder starts empty. The order in which elements are added |
| 577 | * is retained for the layout in memory. |
| 578 | * |
| 579 | */ |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 580 | public static class Builder { |
| 581 | RenderScript mRS; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 582 | Element[] mElements; |
| 583 | String[] mElementNames; |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 584 | int[] mArraySizes; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 585 | int mCount; |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 586 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 587 | /** |
| 588 | * Create a builder object. |
| 589 | * |
| 590 | * @param rs |
| 591 | */ |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 592 | public Builder(RenderScript rs) { |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 593 | mRS = rs; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 594 | mCount = 0; |
| 595 | mElements = new Element[8]; |
| 596 | mElementNames = new String[8]; |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 597 | mArraySizes = new int[8]; |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 600 | /** |
| 601 | * Add an array of elements to this element. |
| 602 | * |
| 603 | * @param element |
| 604 | * @param name |
| 605 | * @param arraySize |
| 606 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 607 | public Builder add(Element element, String name, int arraySize) { |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 608 | if (arraySize < 1) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 609 | throw new RSIllegalArgumentException("Array size cannot be less than 1."); |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 610 | } |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 611 | if(mCount == mElements.length) { |
| 612 | Element[] e = new Element[mCount + 8]; |
| 613 | String[] s = new String[mCount + 8]; |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 614 | int[] as = new int[mCount + 8]; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 615 | System.arraycopy(mElements, 0, e, 0, mCount); |
| 616 | System.arraycopy(mElementNames, 0, s, 0, mCount); |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 617 | System.arraycopy(mArraySizes, 0, as, 0, mCount); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 618 | mElements = e; |
| 619 | mElementNames = s; |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 620 | mArraySizes = as; |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 621 | } |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 622 | mElements[mCount] = element; |
| 623 | mElementNames[mCount] = name; |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 624 | mArraySizes[mCount] = arraySize; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 625 | mCount++; |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 626 | return this; |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 629 | /** |
| 630 | * Add a single element to this Element. |
| 631 | * |
| 632 | * @param element |
| 633 | * @param name |
| 634 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 635 | public Builder add(Element element, String name) { |
| 636 | return add(element, name, 1); |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 637 | } |
| 638 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 639 | /** |
| 640 | * Create the element from this builder. |
| 641 | * |
| 642 | * |
| 643 | * @return Element |
| 644 | */ |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 645 | public Element create() { |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 646 | mRS.validate(); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 647 | Element[] ein = new Element[mCount]; |
| 648 | String[] sin = new String[mCount]; |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 649 | int[] asin = new int[mCount]; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 650 | java.lang.System.arraycopy(mElements, 0, ein, 0, mCount); |
| 651 | java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount); |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 652 | java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount); |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 653 | |
| 654 | int[] ids = new int[ein.length]; |
| 655 | for (int ct = 0; ct < ein.length; ct++ ) { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 656 | ids[ct] = ein[ct].getID(); |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 657 | } |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 658 | int id = mRS.nElementCreate2(ids, sin, asin); |
| 659 | return new Element(id, mRS, ein, sin, asin); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 660 | } |
| 661 | } |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 662 | } |
| 663 | |