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