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), |
Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 127 | PIXEL_RGBA (11), |
| 128 | PIXEL_DEPTH (12); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 129 | |
| 130 | int mID; |
| 131 | DataKind(int id) { |
| 132 | mID = id; |
| 133 | } |
| 134 | } |
| 135 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 136 | /** |
| 137 | * Return if a element is too complex for use as a data source for a Mesh or |
| 138 | * a Program. |
| 139 | * |
| 140 | * @return boolean |
| 141 | */ |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 142 | public boolean isComplex() { |
| 143 | if (mElements == null) { |
| 144 | return false; |
| 145 | } |
| 146 | for (int ct=0; ct < mElements.length; ct++) { |
| 147 | if (mElements[ct].mElements != null) { |
| 148 | return true; |
| 149 | } |
| 150 | } |
| 151 | return false; |
| 152 | } |
| 153 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 154 | /** |
| 155 | * Utility function for returning an Element containing a single Boolean. |
| 156 | * |
| 157 | * @param rs Context to which the element will belong. |
| 158 | * |
| 159 | * @return Element |
| 160 | */ |
Jason Sams | f110d4b | 2010-06-21 17:42:41 -0700 | [diff] [blame] | 161 | public static Element BOOLEAN(RenderScript rs) { |
| 162 | if(rs.mElement_BOOLEAN == null) { |
| 163 | rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN); |
| 164 | } |
| 165 | return rs.mElement_BOOLEAN; |
| 166 | } |
| 167 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 168 | /** |
| 169 | * Utility function for returning an Element containing a single UNSIGNED_8. |
| 170 | * |
| 171 | * @param rs Context to which the element will belong. |
| 172 | * |
| 173 | * @return Element |
| 174 | */ |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 175 | public static Element U8(RenderScript rs) { |
| 176 | if(rs.mElement_U8 == null) { |
| 177 | rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 178 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 179 | return rs.mElement_U8; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 182 | /** |
| 183 | * Utility function for returning an Element containing a single SIGNED_8. |
| 184 | * |
| 185 | * @param rs Context to which the element will belong. |
| 186 | * |
| 187 | * @return Element |
| 188 | */ |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 189 | public static Element I8(RenderScript rs) { |
| 190 | if(rs.mElement_I8 == null) { |
| 191 | rs.mElement_I8 = createUser(rs, DataType.SIGNED_8); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 192 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 193 | return rs.mElement_I8; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Jason Sams | e29f3e7 | 2010-06-08 15:40:48 -0700 | [diff] [blame] | 196 | public static Element U16(RenderScript rs) { |
| 197 | if(rs.mElement_U16 == null) { |
| 198 | rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16); |
| 199 | } |
| 200 | return rs.mElement_U16; |
| 201 | } |
| 202 | |
| 203 | public static Element I16(RenderScript rs) { |
| 204 | if(rs.mElement_I16 == null) { |
| 205 | rs.mElement_I16 = createUser(rs, DataType.SIGNED_16); |
| 206 | } |
| 207 | return rs.mElement_I16; |
| 208 | } |
| 209 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 210 | public static Element U32(RenderScript rs) { |
| 211 | if(rs.mElement_U32 == null) { |
| 212 | rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 213 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 214 | return rs.mElement_U32; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 217 | public static Element I32(RenderScript rs) { |
| 218 | if(rs.mElement_I32 == null) { |
| 219 | rs.mElement_I32 = createUser(rs, DataType.SIGNED_32); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 220 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 221 | return rs.mElement_I32; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Stephen Hines | 52d8363 | 2010-10-11 16:10:42 -0700 | [diff] [blame] | 224 | public static Element U64(RenderScript rs) { |
| 225 | if(rs.mElement_U64 == null) { |
| 226 | rs.mElement_U64 = createUser(rs, DataType.UNSIGNED_64); |
| 227 | } |
| 228 | return rs.mElement_U64; |
| 229 | } |
| 230 | |
Stephen Hines | ef1dac2 | 2010-10-01 15:39:33 -0700 | [diff] [blame] | 231 | public static Element I64(RenderScript rs) { |
| 232 | if(rs.mElement_I64 == null) { |
| 233 | rs.mElement_I64 = createUser(rs, DataType.SIGNED_64); |
| 234 | } |
| 235 | return rs.mElement_I64; |
| 236 | } |
| 237 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 238 | public static Element F32(RenderScript rs) { |
| 239 | if(rs.mElement_F32 == null) { |
| 240 | rs.mElement_F32 = createUser(rs, DataType.FLOAT_32); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 241 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 242 | return rs.mElement_F32; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Stephen Hines | 02f41705 | 2010-09-30 15:19:22 -0700 | [diff] [blame] | 245 | public static Element F64(RenderScript rs) { |
| 246 | if(rs.mElement_F64 == null) { |
| 247 | rs.mElement_F64 = createUser(rs, DataType.FLOAT_64); |
| 248 | } |
| 249 | return rs.mElement_F64; |
| 250 | } |
| 251 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 252 | public static Element ELEMENT(RenderScript rs) { |
| 253 | if(rs.mElement_ELEMENT == null) { |
| 254 | rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 255 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 256 | return rs.mElement_ELEMENT; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 259 | public static Element TYPE(RenderScript rs) { |
| 260 | if(rs.mElement_TYPE == null) { |
| 261 | rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 262 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 263 | return rs.mElement_TYPE; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 266 | public static Element ALLOCATION(RenderScript rs) { |
| 267 | if(rs.mElement_ALLOCATION == null) { |
| 268 | rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 269 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 270 | return rs.mElement_ALLOCATION; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 273 | public static Element SAMPLER(RenderScript rs) { |
| 274 | if(rs.mElement_SAMPLER == null) { |
| 275 | rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 276 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 277 | return rs.mElement_SAMPLER; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 280 | public static Element SCRIPT(RenderScript rs) { |
| 281 | if(rs.mElement_SCRIPT == null) { |
| 282 | rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 283 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 284 | return rs.mElement_SCRIPT; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 287 | public static Element MESH(RenderScript rs) { |
| 288 | if(rs.mElement_MESH == null) { |
| 289 | rs.mElement_MESH = createUser(rs, DataType.RS_MESH); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 290 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 291 | return rs.mElement_MESH; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 294 | public static Element PROGRAM_FRAGMENT(RenderScript rs) { |
| 295 | if(rs.mElement_PROGRAM_FRAGMENT == null) { |
| 296 | rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 297 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 298 | return rs.mElement_PROGRAM_FRAGMENT; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 301 | public static Element PROGRAM_VERTEX(RenderScript rs) { |
| 302 | if(rs.mElement_PROGRAM_VERTEX == null) { |
| 303 | rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 304 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 305 | return rs.mElement_PROGRAM_VERTEX; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 308 | public static Element PROGRAM_RASTER(RenderScript rs) { |
| 309 | if(rs.mElement_PROGRAM_RASTER == null) { |
| 310 | rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 311 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 312 | return rs.mElement_PROGRAM_RASTER; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 315 | public static Element PROGRAM_STORE(RenderScript rs) { |
| 316 | if(rs.mElement_PROGRAM_STORE == null) { |
| 317 | rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 318 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 319 | return rs.mElement_PROGRAM_STORE; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 323 | public static Element A_8(RenderScript rs) { |
| 324 | if(rs.mElement_A_8 == null) { |
| 325 | rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A); |
| 326 | } |
| 327 | return rs.mElement_A_8; |
| 328 | } |
| 329 | |
| 330 | public static Element RGB_565(RenderScript rs) { |
| 331 | if(rs.mElement_RGB_565 == null) { |
| 332 | rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB); |
| 333 | } |
| 334 | return rs.mElement_RGB_565; |
| 335 | } |
| 336 | |
| 337 | public static Element RGB_888(RenderScript rs) { |
| 338 | if(rs.mElement_RGB_888 == null) { |
| 339 | rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB); |
| 340 | } |
| 341 | return rs.mElement_RGB_888; |
| 342 | } |
| 343 | |
| 344 | public static Element RGBA_5551(RenderScript rs) { |
| 345 | if(rs.mElement_RGBA_5551 == null) { |
| 346 | rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA); |
| 347 | } |
| 348 | return rs.mElement_RGBA_5551; |
| 349 | } |
| 350 | |
| 351 | public static Element RGBA_4444(RenderScript rs) { |
| 352 | if(rs.mElement_RGBA_4444 == null) { |
| 353 | rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA); |
| 354 | } |
| 355 | return rs.mElement_RGBA_4444; |
| 356 | } |
| 357 | |
| 358 | public static Element RGBA_8888(RenderScript rs) { |
| 359 | if(rs.mElement_RGBA_8888 == null) { |
| 360 | rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA); |
| 361 | } |
| 362 | return rs.mElement_RGBA_8888; |
| 363 | } |
| 364 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 365 | public static Element F32_2(RenderScript rs) { |
| 366 | if(rs.mElement_FLOAT_2 == null) { |
| 367 | rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 368 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 369 | return rs.mElement_FLOAT_2; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 372 | public static Element F32_3(RenderScript rs) { |
| 373 | if(rs.mElement_FLOAT_3 == null) { |
| 374 | rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 375 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 376 | return rs.mElement_FLOAT_3; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 377 | } |
| 378 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 379 | public static Element F32_4(RenderScript rs) { |
| 380 | if(rs.mElement_FLOAT_4 == null) { |
| 381 | rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 382 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 383 | return rs.mElement_FLOAT_4; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 386 | public static Element U8_4(RenderScript rs) { |
| 387 | if(rs.mElement_UCHAR_4 == null) { |
| 388 | rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 389 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 390 | return rs.mElement_UCHAR_4; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 391 | } |
| 392 | |
Jason Sams | 1d45c47 | 2010-08-25 14:31:48 -0700 | [diff] [blame] | 393 | public static Element MATRIX_4X4(RenderScript rs) { |
| 394 | if(rs.mElement_MATRIX_4X4 == null) { |
| 395 | rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4); |
| 396 | } |
| 397 | return rs.mElement_MATRIX_4X4; |
| 398 | } |
| 399 | public static Element MATRIX4X4(RenderScript rs) { |
| 400 | return MATRIX_4X4(rs); |
| 401 | } |
| 402 | |
| 403 | public static Element MATRIX_3X3(RenderScript rs) { |
| 404 | if(rs.mElement_MATRIX_3X3 == null) { |
| 405 | rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3); |
| 406 | } |
Alex Sakhartchouk | 3476977 | 2011-02-28 16:01:28 -0800 | [diff] [blame] | 407 | return rs.mElement_MATRIX_3X3; |
Jason Sams | 1d45c47 | 2010-08-25 14:31:48 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | public static Element MATRIX_2X2(RenderScript rs) { |
| 411 | if(rs.mElement_MATRIX_2X2 == null) { |
| 412 | rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2); |
| 413 | } |
| 414 | return rs.mElement_MATRIX_2X2; |
| 415 | } |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 416 | |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 417 | Element(int id, RenderScript rs, Element[] e, String[] n, int[] as) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 418 | super(id, rs); |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 419 | mSize = 0; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 420 | mElements = e; |
| 421 | mElementNames = n; |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 422 | mArraySizes = as; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 423 | for (int ct = 0; ct < mElements.length; ct++ ) { |
Alex Sakhartchouk | 9e401bc | 2010-10-13 14:22:02 -0700 | [diff] [blame] | 424 | mSize += mElements[ct].mSize * mArraySizes[ct]; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 425 | } |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 426 | } |
| 427 | |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 428 | Element(int id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) { |
| 429 | super(id, rs); |
Jason Sams | 252c078 | 2011-01-11 17:42:52 -0800 | [diff] [blame] | 430 | if ((dt != DataType.UNSIGNED_5_6_5) && |
| 431 | (dt != DataType.UNSIGNED_4_4_4_4) && |
| 432 | (dt != DataType.UNSIGNED_5_5_5_1)) { |
| 433 | mSize = dt.mSize * size; |
| 434 | } else { |
| 435 | mSize = dt.mSize; |
| 436 | } |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 437 | mType = dt; |
| 438 | mKind = dk; |
| 439 | mNormalized = norm; |
| 440 | mVectorSize = size; |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 443 | Element(int id, RenderScript rs) { |
| 444 | super(id, rs); |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | @Override |
| 448 | void updateFromNative() { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 449 | super.updateFromNative(); |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 450 | |
| 451 | // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements |
| 452 | int[] dataBuffer = new int[5]; |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 453 | mRS.nElementGetNativeData(getID(), dataBuffer); |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 454 | |
| 455 | mNormalized = dataBuffer[2] == 1 ? true : false; |
| 456 | mVectorSize = dataBuffer[3]; |
| 457 | mSize = 0; |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 458 | for (DataType dt: DataType.values()) { |
| 459 | if(dt.mID == dataBuffer[0]){ |
| 460 | mType = dt; |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 461 | mSize = mType.mSize * mVectorSize; |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | for (DataKind dk: DataKind.values()) { |
| 465 | if(dk.mID == dataBuffer[1]){ |
| 466 | mKind = dk; |
| 467 | } |
| 468 | } |
| 469 | |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 470 | int numSubElements = dataBuffer[4]; |
| 471 | if(numSubElements > 0) { |
| 472 | mElements = new Element[numSubElements]; |
| 473 | mElementNames = new String[numSubElements]; |
| 474 | |
| 475 | int[] subElementIds = new int[numSubElements]; |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 476 | mRS.nElementGetSubElements(getID(), subElementIds, mElementNames); |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 477 | for(int i = 0; i < numSubElements; i ++) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 478 | mElements[i] = new Element(subElementIds[i], mRS); |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 479 | mElements[i].updateFromNative(); |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 480 | mSize += mElements[i].mSize; |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | |
| 484 | } |
| 485 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 486 | /** |
| 487 | * Create a custom Element of the specified DataType. The DataKind will be |
| 488 | * set to USER and the vector size to 1 indicating non-vector. |
| 489 | * |
| 490 | * @param rs The context associated with the new Element. |
| 491 | * @param dt The DataType for the new element. |
| 492 | * @return Element |
| 493 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 494 | static Element createUser(RenderScript rs, DataType dt) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 495 | DataKind dk = DataKind.USER; |
| 496 | boolean norm = false; |
| 497 | int vecSize = 1; |
| 498 | int id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize); |
| 499 | return new Element(id, rs, dt, dk, norm, vecSize); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 500 | } |
| 501 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 502 | /** |
| 503 | * Create a custom vector element of the specified DataType and vector size. |
| 504 | * DataKind will be set to USER. |
| 505 | * |
| 506 | * @param rs The context associated with the new Element. |
| 507 | * @param dt The DataType for the new element. |
| 508 | * @param size Vector size for the new Element. Range 2-4 inclusive |
| 509 | * supported. |
| 510 | * |
| 511 | * @return Element |
| 512 | */ |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 513 | public static Element createVector(RenderScript rs, DataType dt, int size) { |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 514 | if (size < 2 || size > 4) { |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 515 | throw new RSIllegalArgumentException("Vector size out of range 2-4."); |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 516 | } |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 517 | DataKind dk = DataKind.USER; |
| 518 | boolean norm = false; |
| 519 | int id = rs.nElementCreate(dt.mID, dk.mID, norm, size); |
| 520 | return new Element(id, rs, dt, dk, norm, size); |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 523 | /** |
| 524 | * Create a new pixel Element type. A matching DataType and DataKind must |
| 525 | * be provided. The DataType and DataKind must contain the same number of |
| 526 | * components. Vector size will be set to 1. |
| 527 | * |
| 528 | * @param rs The context associated with the new Element. |
| 529 | * @param dt The DataType for the new element. |
| 530 | * @param dk The DataKind to specify the mapping of each component in the |
| 531 | * DataType. |
| 532 | * |
| 533 | * @return Element |
| 534 | */ |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 535 | public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) { |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 536 | if (!(dk == DataKind.PIXEL_L || |
| 537 | dk == DataKind.PIXEL_A || |
| 538 | dk == DataKind.PIXEL_LA || |
| 539 | dk == DataKind.PIXEL_RGB || |
Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 540 | dk == DataKind.PIXEL_RGBA || |
| 541 | dk == DataKind.PIXEL_DEPTH)) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 542 | throw new RSIllegalArgumentException("Unsupported DataKind"); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 543 | } |
| 544 | if (!(dt == DataType.UNSIGNED_8 || |
Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 545 | dt == DataType.UNSIGNED_16 || |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 546 | dt == DataType.UNSIGNED_5_6_5 || |
| 547 | dt == DataType.UNSIGNED_4_4_4_4 || |
| 548 | dt == DataType.UNSIGNED_5_5_5_1)) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 549 | throw new RSIllegalArgumentException("Unsupported DataType"); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 550 | } |
| 551 | if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) { |
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_5_5_5_1 && 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 | if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 558 | throw new RSIllegalArgumentException("Bad kind and type combo"); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 559 | } |
Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 560 | if (dt == DataType.UNSIGNED_16 && |
| 561 | dk != DataKind.PIXEL_DEPTH) { |
| 562 | throw new RSIllegalArgumentException("Bad kind and type combo"); |
| 563 | } |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 564 | |
| 565 | int size = 1; |
Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 566 | switch (dk) { |
| 567 | case PIXEL_LA: |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 568 | size = 2; |
Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 569 | break; |
| 570 | case PIXEL_RGB: |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 571 | size = 3; |
Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 572 | break; |
| 573 | case PIXEL_RGBA: |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 574 | size = 4; |
Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 575 | break; |
| 576 | case PIXEL_DEPTH: |
| 577 | size = 2; |
| 578 | break; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 579 | } |
| 580 | |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 581 | boolean norm = true; |
| 582 | int id = rs.nElementCreate(dt.mID, dk.mID, norm, size); |
| 583 | return new Element(id, rs, dt, dk, norm, size); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 584 | } |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 585 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 586 | /** |
| 587 | * Builder class for producing complex elements with matching field and name |
| 588 | * pairs. The builder starts empty. The order in which elements are added |
| 589 | * is retained for the layout in memory. |
| 590 | * |
| 591 | */ |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 592 | public static class Builder { |
| 593 | RenderScript mRS; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 594 | Element[] mElements; |
| 595 | String[] mElementNames; |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 596 | int[] mArraySizes; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 597 | int mCount; |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 598 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 599 | /** |
| 600 | * Create a builder object. |
| 601 | * |
| 602 | * @param rs |
| 603 | */ |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 604 | public Builder(RenderScript rs) { |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 605 | mRS = rs; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 606 | mCount = 0; |
| 607 | mElements = new Element[8]; |
| 608 | mElementNames = new String[8]; |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 609 | mArraySizes = new int[8]; |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 610 | } |
| 611 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 612 | /** |
| 613 | * Add an array of elements to this element. |
| 614 | * |
| 615 | * @param element |
| 616 | * @param name |
| 617 | * @param arraySize |
| 618 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 619 | public Builder add(Element element, String name, int arraySize) { |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 620 | if (arraySize < 1) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 621 | throw new RSIllegalArgumentException("Array size cannot be less than 1."); |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 622 | } |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 623 | if(mCount == mElements.length) { |
| 624 | Element[] e = new Element[mCount + 8]; |
| 625 | String[] s = new String[mCount + 8]; |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 626 | int[] as = new int[mCount + 8]; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 627 | System.arraycopy(mElements, 0, e, 0, mCount); |
| 628 | System.arraycopy(mElementNames, 0, s, 0, mCount); |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 629 | System.arraycopy(mArraySizes, 0, as, 0, mCount); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 630 | mElements = e; |
| 631 | mElementNames = s; |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 632 | mArraySizes = as; |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 633 | } |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 634 | mElements[mCount] = element; |
| 635 | mElementNames[mCount] = name; |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 636 | mArraySizes[mCount] = arraySize; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 637 | mCount++; |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 638 | return this; |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 639 | } |
| 640 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 641 | /** |
| 642 | * Add a single element to this Element. |
| 643 | * |
| 644 | * @param element |
| 645 | * @param name |
| 646 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 647 | public Builder add(Element element, String name) { |
| 648 | return add(element, name, 1); |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 651 | /** |
| 652 | * Create the element from this builder. |
| 653 | * |
| 654 | * |
| 655 | * @return Element |
| 656 | */ |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 657 | public Element create() { |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 658 | mRS.validate(); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 659 | Element[] ein = new Element[mCount]; |
| 660 | String[] sin = new String[mCount]; |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 661 | int[] asin = new int[mCount]; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 662 | java.lang.System.arraycopy(mElements, 0, ein, 0, mCount); |
| 663 | java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount); |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 664 | java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount); |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 665 | |
| 666 | int[] ids = new int[ein.length]; |
| 667 | for (int ct = 0; ct < ein.length; ct++ ) { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 668 | ids[ct] = ein[ct].getID(); |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 669 | } |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 670 | int id = mRS.nElementCreate2(ids, sin, asin); |
| 671 | return new Element(id, mRS, ein, sin, asin); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 672 | } |
| 673 | } |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 674 | } |
| 675 | |