| 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 |
| Stephen Hines | f257e51 | 2011-06-14 14:54:29 -0700 | [diff] [blame] | 35 | * alignment is determined by the most basic primitive type. i.e. a float4 |
| 36 | * vector will be aligned to sizeof(float) and not sizeof(float4). The |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 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; |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 49 | int[] mOffsetInBytes; |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 50 | |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 51 | DataType mType; |
| 52 | DataKind mKind; |
| 53 | boolean mNormalized; |
| 54 | int mVectorSize; |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 55 | |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 56 | /** |
| 57 | * @hide |
| 58 | * @return element size in bytes |
| 59 | */ |
| 60 | public int getSizeBytes() {return mSize;} |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 61 | |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * DataType represents the basic type information for a basic element. The |
| Alex Sakhartchouk | f5d8ac7 | 2011-12-16 09:44:26 -0800 | [diff] [blame^] | 65 | * naming convention follows. For numeric types it is FLOAT, |
| 66 | * SIGNED, or UNSIGNED followed by the _BITS where BITS is the |
| 67 | * size of the data. BOOLEAN is a true / false (1,0) |
| 68 | * represented in an 8 bit container. The UNSIGNED variants |
| 69 | * with multiple bit definitions are for packed graphical data |
| 70 | * formats and represent vectors with per vector member sizes |
| 71 | * which are treated as a single unit for packing and alignment |
| 72 | * purposes. |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 73 | * |
| 74 | * MATRIX the three matrix types contain FLOAT_32 elements and are treated |
| 75 | * as 32 bits for alignment purposes. |
| 76 | * |
| 77 | * RS_* objects. 32 bit opaque handles. |
| 78 | */ |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 79 | public enum DataType { |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 80 | //FLOAT_16 (1, 2), |
| 81 | FLOAT_32 (2, 4), |
| Stephen Hines | 02f41705 | 2010-09-30 15:19:22 -0700 | [diff] [blame] | 82 | FLOAT_64 (3, 8), |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 83 | SIGNED_8 (4, 1), |
| 84 | SIGNED_16 (5, 2), |
| 85 | SIGNED_32 (6, 4), |
| Stephen Hines | ef1dac2 | 2010-10-01 15:39:33 -0700 | [diff] [blame] | 86 | SIGNED_64 (7, 8), |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 87 | UNSIGNED_8 (8, 1), |
| 88 | UNSIGNED_16 (9, 2), |
| 89 | UNSIGNED_32 (10, 4), |
| Stephen Hines | 52d8363 | 2010-10-11 16:10:42 -0700 | [diff] [blame] | 90 | UNSIGNED_64 (11, 8), |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 91 | |
| Jason Sams | f110d4b | 2010-06-21 17:42:41 -0700 | [diff] [blame] | 92 | BOOLEAN(12, 1), |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 93 | |
| Jason Sams | f110d4b | 2010-06-21 17:42:41 -0700 | [diff] [blame] | 94 | UNSIGNED_5_6_5 (13, 2), |
| 95 | UNSIGNED_5_5_5_1 (14, 2), |
| 96 | UNSIGNED_4_4_4_4 (15, 2), |
| 97 | |
| Jason Sams | 1d45c47 | 2010-08-25 14:31:48 -0700 | [diff] [blame] | 98 | MATRIX_4X4 (16, 64), |
| 99 | MATRIX_3X3 (17, 36), |
| 100 | MATRIX_2X2 (18, 16), |
| 101 | |
| 102 | RS_ELEMENT (1000, 4), |
| 103 | RS_TYPE (1001, 4), |
| 104 | RS_ALLOCATION (1002, 4), |
| 105 | RS_SAMPLER (1003, 4), |
| 106 | RS_SCRIPT (1004, 4), |
| 107 | RS_MESH (1005, 4), |
| 108 | RS_PROGRAM_FRAGMENT (1006, 4), |
| 109 | RS_PROGRAM_VERTEX (1007, 4), |
| 110 | RS_PROGRAM_RASTER (1008, 4), |
| 111 | RS_PROGRAM_STORE (1009, 4); |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 112 | |
| 113 | int mID; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 114 | int mSize; |
| 115 | DataType(int id, int size) { |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 116 | mID = id; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 117 | mSize = size; |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 121 | /** |
| 122 | * The special interpretation of the data if required. This is primarly |
| 123 | * useful for graphical data. USER indicates no special interpretation is |
| 124 | * expected. PIXEL is used in conjunction with the standard data types for |
| 125 | * representing texture formats. |
| 126 | */ |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 127 | public enum DataKind { |
| 128 | USER (0), |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 129 | |
| 130 | PIXEL_L (7), |
| 131 | PIXEL_A (8), |
| 132 | PIXEL_LA (9), |
| 133 | PIXEL_RGB (10), |
| Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 134 | PIXEL_RGBA (11), |
| 135 | PIXEL_DEPTH (12); |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 136 | |
| 137 | int mID; |
| 138 | DataKind(int id) { |
| 139 | mID = id; |
| 140 | } |
| 141 | } |
| 142 | |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 143 | /** |
| 144 | * Return if a element is too complex for use as a data source for a Mesh or |
| 145 | * a Program. |
| 146 | * |
| 147 | * @return boolean |
| 148 | */ |
| Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 149 | public boolean isComplex() { |
| 150 | if (mElements == null) { |
| 151 | return false; |
| 152 | } |
| 153 | for (int ct=0; ct < mElements.length; ct++) { |
| 154 | if (mElements[ct].mElements != null) { |
| 155 | return true; |
| 156 | } |
| 157 | } |
| 158 | return false; |
| 159 | } |
| 160 | |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 161 | /** |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 162 | * @hide |
| 163 | * @return number of sub-elements in this element |
| 164 | */ |
| 165 | public int getSubElementCount() { |
| 166 | if (mElements == null) { |
| 167 | return 0; |
| 168 | } |
| 169 | return mElements.length; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * @hide |
| 174 | * @param index index of the sub-element to return |
| 175 | * @return sub-element in this element at given index |
| 176 | */ |
| 177 | public Element getSubElement(int index) { |
| 178 | if (mElements == null) { |
| 179 | throw new RSIllegalArgumentException("Element contains no sub-elements"); |
| 180 | } |
| 181 | if (index < 0 || index >= mElements.length) { |
| 182 | throw new RSIllegalArgumentException("Illegal sub-element index"); |
| 183 | } |
| 184 | return mElements[index]; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * @hide |
| 189 | * @param index index of the sub-element |
| 190 | * @return sub-element in this element at given index |
| 191 | */ |
| 192 | public String getSubElementName(int index) { |
| 193 | if (mElements == null) { |
| 194 | throw new RSIllegalArgumentException("Element contains no sub-elements"); |
| 195 | } |
| 196 | if (index < 0 || index >= mElements.length) { |
| 197 | throw new RSIllegalArgumentException("Illegal sub-element index"); |
| 198 | } |
| 199 | return mElementNames[index]; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * @hide |
| 204 | * @param index index of the sub-element |
| 205 | * @return array size of sub-element in this element at given index |
| 206 | */ |
| 207 | public int getSubElementArraySize(int index) { |
| 208 | if (mElements == null) { |
| 209 | throw new RSIllegalArgumentException("Element contains no sub-elements"); |
| 210 | } |
| 211 | if (index < 0 || index >= mElements.length) { |
| 212 | throw new RSIllegalArgumentException("Illegal sub-element index"); |
| 213 | } |
| 214 | return mArraySizes[index]; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * @hide |
| 219 | * @param index index of the sub-element |
| 220 | * @return offset in bytes of sub-element in this element at given index |
| 221 | */ |
| 222 | public int getSubElementOffsetBytes(int index) { |
| 223 | if (mElements == null) { |
| 224 | throw new RSIllegalArgumentException("Element contains no sub-elements"); |
| 225 | } |
| 226 | if (index < 0 || index >= mElements.length) { |
| 227 | throw new RSIllegalArgumentException("Illegal sub-element index"); |
| 228 | } |
| 229 | return mOffsetInBytes[index]; |
| 230 | } |
| 231 | |
| 232 | /** |
| Alex Sakhartchouk | f5d8ac7 | 2011-12-16 09:44:26 -0800 | [diff] [blame^] | 233 | * @hide |
| 234 | * @return element data type |
| 235 | */ |
| 236 | public DataType getDataType() { |
| 237 | return mType; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * @hide |
| 242 | * @return element data kind |
| 243 | */ |
| 244 | public DataKind getDataKind() { |
| 245 | return mKind; |
| 246 | } |
| 247 | |
| 248 | /** |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 249 | * Utility function for returning an Element containing a single Boolean. |
| 250 | * |
| 251 | * @param rs Context to which the element will belong. |
| 252 | * |
| 253 | * @return Element |
| 254 | */ |
| Jason Sams | f110d4b | 2010-06-21 17:42:41 -0700 | [diff] [blame] | 255 | public static Element BOOLEAN(RenderScript rs) { |
| 256 | if(rs.mElement_BOOLEAN == null) { |
| 257 | rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN); |
| 258 | } |
| 259 | return rs.mElement_BOOLEAN; |
| 260 | } |
| 261 | |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 262 | /** |
| 263 | * Utility function for returning an Element containing a single UNSIGNED_8. |
| 264 | * |
| 265 | * @param rs Context to which the element will belong. |
| 266 | * |
| 267 | * @return Element |
| 268 | */ |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 269 | public static Element U8(RenderScript rs) { |
| 270 | if(rs.mElement_U8 == null) { |
| 271 | rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 272 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 273 | return rs.mElement_U8; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 276 | /** |
| 277 | * Utility function for returning an Element containing a single SIGNED_8. |
| 278 | * |
| 279 | * @param rs Context to which the element will belong. |
| 280 | * |
| 281 | * @return Element |
| 282 | */ |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 283 | public static Element I8(RenderScript rs) { |
| 284 | if(rs.mElement_I8 == null) { |
| 285 | rs.mElement_I8 = createUser(rs, DataType.SIGNED_8); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 286 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 287 | return rs.mElement_I8; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 288 | } |
| 289 | |
| Jason Sams | e29f3e7 | 2010-06-08 15:40:48 -0700 | [diff] [blame] | 290 | public static Element U16(RenderScript rs) { |
| 291 | if(rs.mElement_U16 == null) { |
| 292 | rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16); |
| 293 | } |
| 294 | return rs.mElement_U16; |
| 295 | } |
| 296 | |
| 297 | public static Element I16(RenderScript rs) { |
| 298 | if(rs.mElement_I16 == null) { |
| 299 | rs.mElement_I16 = createUser(rs, DataType.SIGNED_16); |
| 300 | } |
| 301 | return rs.mElement_I16; |
| 302 | } |
| 303 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 304 | public static Element U32(RenderScript rs) { |
| 305 | if(rs.mElement_U32 == null) { |
| 306 | rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 307 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 308 | return rs.mElement_U32; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 309 | } |
| 310 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 311 | public static Element I32(RenderScript rs) { |
| 312 | if(rs.mElement_I32 == null) { |
| 313 | rs.mElement_I32 = createUser(rs, DataType.SIGNED_32); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 314 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 315 | return rs.mElement_I32; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| Stephen Hines | 52d8363 | 2010-10-11 16:10:42 -0700 | [diff] [blame] | 318 | public static Element U64(RenderScript rs) { |
| 319 | if(rs.mElement_U64 == null) { |
| 320 | rs.mElement_U64 = createUser(rs, DataType.UNSIGNED_64); |
| 321 | } |
| 322 | return rs.mElement_U64; |
| 323 | } |
| 324 | |
| Stephen Hines | ef1dac2 | 2010-10-01 15:39:33 -0700 | [diff] [blame] | 325 | public static Element I64(RenderScript rs) { |
| 326 | if(rs.mElement_I64 == null) { |
| 327 | rs.mElement_I64 = createUser(rs, DataType.SIGNED_64); |
| 328 | } |
| 329 | return rs.mElement_I64; |
| 330 | } |
| 331 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 332 | public static Element F32(RenderScript rs) { |
| 333 | if(rs.mElement_F32 == null) { |
| 334 | rs.mElement_F32 = createUser(rs, DataType.FLOAT_32); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 335 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 336 | return rs.mElement_F32; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 337 | } |
| 338 | |
| Stephen Hines | 02f41705 | 2010-09-30 15:19:22 -0700 | [diff] [blame] | 339 | public static Element F64(RenderScript rs) { |
| 340 | if(rs.mElement_F64 == null) { |
| 341 | rs.mElement_F64 = createUser(rs, DataType.FLOAT_64); |
| 342 | } |
| 343 | return rs.mElement_F64; |
| 344 | } |
| 345 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 346 | public static Element ELEMENT(RenderScript rs) { |
| 347 | if(rs.mElement_ELEMENT == null) { |
| 348 | rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 349 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 350 | return rs.mElement_ELEMENT; |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 353 | public static Element TYPE(RenderScript rs) { |
| 354 | if(rs.mElement_TYPE == null) { |
| 355 | rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 356 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 357 | return rs.mElement_TYPE; |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 360 | public static Element ALLOCATION(RenderScript rs) { |
| 361 | if(rs.mElement_ALLOCATION == null) { |
| 362 | rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 363 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 364 | return rs.mElement_ALLOCATION; |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 367 | public static Element SAMPLER(RenderScript rs) { |
| 368 | if(rs.mElement_SAMPLER == null) { |
| 369 | rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 370 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 371 | return rs.mElement_SAMPLER; |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 374 | public static Element SCRIPT(RenderScript rs) { |
| 375 | if(rs.mElement_SCRIPT == null) { |
| 376 | rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 377 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 378 | return rs.mElement_SCRIPT; |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 381 | public static Element MESH(RenderScript rs) { |
| 382 | if(rs.mElement_MESH == null) { |
| 383 | rs.mElement_MESH = createUser(rs, DataType.RS_MESH); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 384 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 385 | return rs.mElement_MESH; |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 388 | public static Element PROGRAM_FRAGMENT(RenderScript rs) { |
| 389 | if(rs.mElement_PROGRAM_FRAGMENT == null) { |
| 390 | rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 391 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 392 | return rs.mElement_PROGRAM_FRAGMENT; |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 395 | public static Element PROGRAM_VERTEX(RenderScript rs) { |
| 396 | if(rs.mElement_PROGRAM_VERTEX == null) { |
| 397 | rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 398 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 399 | return rs.mElement_PROGRAM_VERTEX; |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 402 | public static Element PROGRAM_RASTER(RenderScript rs) { |
| 403 | if(rs.mElement_PROGRAM_RASTER == null) { |
| 404 | rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 405 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 406 | return rs.mElement_PROGRAM_RASTER; |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 409 | public static Element PROGRAM_STORE(RenderScript rs) { |
| 410 | if(rs.mElement_PROGRAM_STORE == null) { |
| 411 | rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 412 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 413 | return rs.mElement_PROGRAM_STORE; |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 417 | public static Element A_8(RenderScript rs) { |
| 418 | if(rs.mElement_A_8 == null) { |
| 419 | rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A); |
| 420 | } |
| 421 | return rs.mElement_A_8; |
| 422 | } |
| 423 | |
| 424 | public static Element RGB_565(RenderScript rs) { |
| 425 | if(rs.mElement_RGB_565 == null) { |
| 426 | rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB); |
| 427 | } |
| 428 | return rs.mElement_RGB_565; |
| 429 | } |
| 430 | |
| 431 | public static Element RGB_888(RenderScript rs) { |
| 432 | if(rs.mElement_RGB_888 == null) { |
| 433 | rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB); |
| 434 | } |
| 435 | return rs.mElement_RGB_888; |
| 436 | } |
| 437 | |
| 438 | public static Element RGBA_5551(RenderScript rs) { |
| 439 | if(rs.mElement_RGBA_5551 == null) { |
| 440 | rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA); |
| 441 | } |
| 442 | return rs.mElement_RGBA_5551; |
| 443 | } |
| 444 | |
| 445 | public static Element RGBA_4444(RenderScript rs) { |
| 446 | if(rs.mElement_RGBA_4444 == null) { |
| 447 | rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA); |
| 448 | } |
| 449 | return rs.mElement_RGBA_4444; |
| 450 | } |
| 451 | |
| 452 | public static Element RGBA_8888(RenderScript rs) { |
| 453 | if(rs.mElement_RGBA_8888 == null) { |
| 454 | rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA); |
| 455 | } |
| 456 | return rs.mElement_RGBA_8888; |
| 457 | } |
| 458 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 459 | public static Element F32_2(RenderScript rs) { |
| 460 | if(rs.mElement_FLOAT_2 == null) { |
| 461 | rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 462 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 463 | return rs.mElement_FLOAT_2; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 464 | } |
| 465 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 466 | public static Element F32_3(RenderScript rs) { |
| 467 | if(rs.mElement_FLOAT_3 == null) { |
| 468 | rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 469 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 470 | return rs.mElement_FLOAT_3; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 471 | } |
| 472 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 473 | public static Element F32_4(RenderScript rs) { |
| 474 | if(rs.mElement_FLOAT_4 == null) { |
| 475 | rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 476 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 477 | return rs.mElement_FLOAT_4; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 478 | } |
| 479 | |
| Stephen Hines | 836c4a5 | 2011-06-01 14:38:10 -0700 | [diff] [blame] | 480 | public static Element F64_2(RenderScript rs) { |
| 481 | if(rs.mElement_DOUBLE_2 == null) { |
| 482 | rs.mElement_DOUBLE_2 = createVector(rs, DataType.FLOAT_64, 2); |
| 483 | } |
| 484 | return rs.mElement_DOUBLE_2; |
| 485 | } |
| 486 | |
| 487 | public static Element F64_3(RenderScript rs) { |
| 488 | if(rs.mElement_DOUBLE_3 == null) { |
| 489 | rs.mElement_DOUBLE_3 = createVector(rs, DataType.FLOAT_64, 3); |
| 490 | } |
| 491 | return rs.mElement_DOUBLE_3; |
| 492 | } |
| 493 | |
| 494 | public static Element F64_4(RenderScript rs) { |
| 495 | if(rs.mElement_DOUBLE_4 == null) { |
| 496 | rs.mElement_DOUBLE_4 = createVector(rs, DataType.FLOAT_64, 4); |
| 497 | } |
| 498 | return rs.mElement_DOUBLE_4; |
| 499 | } |
| 500 | |
| 501 | public static Element U8_2(RenderScript rs) { |
| 502 | if(rs.mElement_UCHAR_2 == null) { |
| 503 | rs.mElement_UCHAR_2 = createVector(rs, DataType.UNSIGNED_8, 2); |
| 504 | } |
| 505 | return rs.mElement_UCHAR_2; |
| 506 | } |
| 507 | |
| 508 | public static Element U8_3(RenderScript rs) { |
| 509 | if(rs.mElement_UCHAR_3 == null) { |
| 510 | rs.mElement_UCHAR_3 = createVector(rs, DataType.UNSIGNED_8, 3); |
| 511 | } |
| 512 | return rs.mElement_UCHAR_3; |
| 513 | } |
| 514 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 515 | public static Element U8_4(RenderScript rs) { |
| 516 | if(rs.mElement_UCHAR_4 == null) { |
| 517 | rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 518 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 519 | return rs.mElement_UCHAR_4; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 520 | } |
| 521 | |
| Stephen Hines | 836c4a5 | 2011-06-01 14:38:10 -0700 | [diff] [blame] | 522 | public static Element I8_2(RenderScript rs) { |
| 523 | if(rs.mElement_CHAR_2 == null) { |
| 524 | rs.mElement_CHAR_2 = createVector(rs, DataType.SIGNED_8, 2); |
| 525 | } |
| 526 | return rs.mElement_CHAR_2; |
| 527 | } |
| 528 | |
| 529 | public static Element I8_3(RenderScript rs) { |
| 530 | if(rs.mElement_CHAR_3 == null) { |
| 531 | rs.mElement_CHAR_3 = createVector(rs, DataType.SIGNED_8, 3); |
| 532 | } |
| 533 | return rs.mElement_CHAR_3; |
| 534 | } |
| 535 | |
| 536 | public static Element I8_4(RenderScript rs) { |
| 537 | if(rs.mElement_CHAR_4 == null) { |
| 538 | rs.mElement_CHAR_4 = createVector(rs, DataType.SIGNED_8, 4); |
| 539 | } |
| 540 | return rs.mElement_CHAR_4; |
| 541 | } |
| 542 | |
| 543 | public static Element U16_2(RenderScript rs) { |
| 544 | if(rs.mElement_USHORT_2 == null) { |
| 545 | rs.mElement_USHORT_2 = createVector(rs, DataType.UNSIGNED_16, 2); |
| 546 | } |
| 547 | return rs.mElement_USHORT_2; |
| 548 | } |
| 549 | |
| 550 | public static Element U16_3(RenderScript rs) { |
| 551 | if(rs.mElement_USHORT_3 == null) { |
| 552 | rs.mElement_USHORT_3 = createVector(rs, DataType.UNSIGNED_16, 3); |
| 553 | } |
| 554 | return rs.mElement_USHORT_3; |
| 555 | } |
| 556 | |
| 557 | public static Element U16_4(RenderScript rs) { |
| 558 | if(rs.mElement_USHORT_4 == null) { |
| 559 | rs.mElement_USHORT_4 = createVector(rs, DataType.UNSIGNED_16, 4); |
| 560 | } |
| 561 | return rs.mElement_USHORT_4; |
| 562 | } |
| 563 | |
| 564 | public static Element I16_2(RenderScript rs) { |
| 565 | if(rs.mElement_SHORT_2 == null) { |
| 566 | rs.mElement_SHORT_2 = createVector(rs, DataType.SIGNED_16, 2); |
| 567 | } |
| 568 | return rs.mElement_SHORT_2; |
| 569 | } |
| 570 | |
| 571 | public static Element I16_3(RenderScript rs) { |
| 572 | if(rs.mElement_SHORT_3 == null) { |
| 573 | rs.mElement_SHORT_3 = createVector(rs, DataType.SIGNED_16, 3); |
| 574 | } |
| 575 | return rs.mElement_SHORT_3; |
| 576 | } |
| 577 | |
| 578 | public static Element I16_4(RenderScript rs) { |
| 579 | if(rs.mElement_SHORT_4 == null) { |
| 580 | rs.mElement_SHORT_4 = createVector(rs, DataType.SIGNED_16, 4); |
| 581 | } |
| 582 | return rs.mElement_SHORT_4; |
| 583 | } |
| 584 | |
| 585 | public static Element U32_2(RenderScript rs) { |
| 586 | if(rs.mElement_UINT_2 == null) { |
| 587 | rs.mElement_UINT_2 = createVector(rs, DataType.UNSIGNED_32, 2); |
| 588 | } |
| 589 | return rs.mElement_UINT_2; |
| 590 | } |
| 591 | |
| 592 | public static Element U32_3(RenderScript rs) { |
| 593 | if(rs.mElement_UINT_3 == null) { |
| 594 | rs.mElement_UINT_3 = createVector(rs, DataType.UNSIGNED_32, 3); |
| 595 | } |
| 596 | return rs.mElement_UINT_3; |
| 597 | } |
| 598 | |
| 599 | public static Element U32_4(RenderScript rs) { |
| 600 | if(rs.mElement_UINT_4 == null) { |
| 601 | rs.mElement_UINT_4 = createVector(rs, DataType.UNSIGNED_32, 4); |
| 602 | } |
| 603 | return rs.mElement_UINT_4; |
| 604 | } |
| 605 | |
| 606 | public static Element I32_2(RenderScript rs) { |
| 607 | if(rs.mElement_INT_2 == null) { |
| 608 | rs.mElement_INT_2 = createVector(rs, DataType.SIGNED_32, 2); |
| 609 | } |
| 610 | return rs.mElement_INT_2; |
| 611 | } |
| 612 | |
| 613 | public static Element I32_3(RenderScript rs) { |
| 614 | if(rs.mElement_INT_3 == null) { |
| 615 | rs.mElement_INT_3 = createVector(rs, DataType.SIGNED_32, 3); |
| 616 | } |
| 617 | return rs.mElement_INT_3; |
| 618 | } |
| 619 | |
| 620 | public static Element I32_4(RenderScript rs) { |
| 621 | if(rs.mElement_INT_4 == null) { |
| 622 | rs.mElement_INT_4 = createVector(rs, DataType.SIGNED_32, 4); |
| 623 | } |
| 624 | return rs.mElement_INT_4; |
| 625 | } |
| 626 | |
| 627 | public static Element U64_2(RenderScript rs) { |
| 628 | if(rs.mElement_ULONG_2 == null) { |
| 629 | rs.mElement_ULONG_2 = createVector(rs, DataType.UNSIGNED_64, 2); |
| 630 | } |
| 631 | return rs.mElement_ULONG_2; |
| 632 | } |
| 633 | |
| 634 | public static Element U64_3(RenderScript rs) { |
| 635 | if(rs.mElement_ULONG_3 == null) { |
| 636 | rs.mElement_ULONG_3 = createVector(rs, DataType.UNSIGNED_64, 3); |
| 637 | } |
| 638 | return rs.mElement_ULONG_3; |
| 639 | } |
| 640 | |
| 641 | public static Element U64_4(RenderScript rs) { |
| 642 | if(rs.mElement_ULONG_4 == null) { |
| 643 | rs.mElement_ULONG_4 = createVector(rs, DataType.UNSIGNED_64, 4); |
| 644 | } |
| 645 | return rs.mElement_ULONG_4; |
| 646 | } |
| 647 | |
| 648 | public static Element I64_2(RenderScript rs) { |
| 649 | if(rs.mElement_LONG_2 == null) { |
| 650 | rs.mElement_LONG_2 = createVector(rs, DataType.SIGNED_64, 2); |
| 651 | } |
| 652 | return rs.mElement_LONG_2; |
| 653 | } |
| 654 | |
| 655 | public static Element I64_3(RenderScript rs) { |
| 656 | if(rs.mElement_LONG_3 == null) { |
| 657 | rs.mElement_LONG_3 = createVector(rs, DataType.SIGNED_64, 3); |
| 658 | } |
| 659 | return rs.mElement_LONG_3; |
| 660 | } |
| 661 | |
| 662 | public static Element I64_4(RenderScript rs) { |
| 663 | if(rs.mElement_LONG_4 == null) { |
| 664 | rs.mElement_LONG_4 = createVector(rs, DataType.SIGNED_64, 4); |
| 665 | } |
| 666 | return rs.mElement_LONG_4; |
| 667 | } |
| 668 | |
| Jason Sams | 1d45c47 | 2010-08-25 14:31:48 -0700 | [diff] [blame] | 669 | public static Element MATRIX_4X4(RenderScript rs) { |
| 670 | if(rs.mElement_MATRIX_4X4 == null) { |
| 671 | rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4); |
| 672 | } |
| 673 | return rs.mElement_MATRIX_4X4; |
| 674 | } |
| 675 | public static Element MATRIX4X4(RenderScript rs) { |
| 676 | return MATRIX_4X4(rs); |
| 677 | } |
| 678 | |
| 679 | public static Element MATRIX_3X3(RenderScript rs) { |
| 680 | if(rs.mElement_MATRIX_3X3 == null) { |
| 681 | rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3); |
| 682 | } |
| Alex Sakhartchouk | 3476977 | 2011-02-28 16:01:28 -0800 | [diff] [blame] | 683 | return rs.mElement_MATRIX_3X3; |
| Jason Sams | 1d45c47 | 2010-08-25 14:31:48 -0700 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | public static Element MATRIX_2X2(RenderScript rs) { |
| 687 | if(rs.mElement_MATRIX_2X2 == null) { |
| 688 | rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2); |
| 689 | } |
| 690 | return rs.mElement_MATRIX_2X2; |
| 691 | } |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 692 | |
| Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 693 | Element(int id, RenderScript rs, Element[] e, String[] n, int[] as) { |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 694 | super(id, rs); |
| Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 695 | mSize = 0; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 696 | mElements = e; |
| 697 | mElementNames = n; |
| Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 698 | mArraySizes = as; |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 699 | mOffsetInBytes = new int[mElements.length]; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 700 | for (int ct = 0; ct < mElements.length; ct++ ) { |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 701 | mOffsetInBytes[ct] = mSize; |
| Alex Sakhartchouk | 9e401bc | 2010-10-13 14:22:02 -0700 | [diff] [blame] | 702 | mSize += mElements[ct].mSize * mArraySizes[ct]; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 703 | } |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 704 | } |
| 705 | |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 706 | Element(int id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) { |
| 707 | super(id, rs); |
| Jason Sams | 252c078 | 2011-01-11 17:42:52 -0800 | [diff] [blame] | 708 | if ((dt != DataType.UNSIGNED_5_6_5) && |
| 709 | (dt != DataType.UNSIGNED_4_4_4_4) && |
| 710 | (dt != DataType.UNSIGNED_5_5_5_1)) { |
| Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 711 | if (size == 3) { |
| 712 | mSize = dt.mSize * 4; |
| 713 | } else { |
| 714 | mSize = dt.mSize * size; |
| 715 | } |
| Jason Sams | 252c078 | 2011-01-11 17:42:52 -0800 | [diff] [blame] | 716 | } else { |
| 717 | mSize = dt.mSize; |
| 718 | } |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 719 | mType = dt; |
| 720 | mKind = dk; |
| 721 | mNormalized = norm; |
| 722 | mVectorSize = size; |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 723 | } |
| 724 | |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 725 | Element(int id, RenderScript rs) { |
| 726 | super(id, rs); |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | @Override |
| 730 | void updateFromNative() { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 731 | super.updateFromNative(); |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 732 | |
| 733 | // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements |
| 734 | int[] dataBuffer = new int[5]; |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 735 | mRS.nElementGetNativeData(getID(), dataBuffer); |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 736 | |
| 737 | mNormalized = dataBuffer[2] == 1 ? true : false; |
| 738 | mVectorSize = dataBuffer[3]; |
| 739 | mSize = 0; |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 740 | for (DataType dt: DataType.values()) { |
| 741 | if(dt.mID == dataBuffer[0]){ |
| 742 | mType = dt; |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 743 | mSize = mType.mSize * mVectorSize; |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 744 | } |
| 745 | } |
| 746 | for (DataKind dk: DataKind.values()) { |
| 747 | if(dk.mID == dataBuffer[1]){ |
| 748 | mKind = dk; |
| 749 | } |
| 750 | } |
| 751 | |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 752 | int numSubElements = dataBuffer[4]; |
| 753 | if(numSubElements > 0) { |
| 754 | mElements = new Element[numSubElements]; |
| 755 | mElementNames = new String[numSubElements]; |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 756 | mArraySizes = new int[numSubElements]; |
| 757 | mOffsetInBytes = new int[numSubElements]; |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 758 | |
| 759 | int[] subElementIds = new int[numSubElements]; |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 760 | mRS.nElementGetSubElements(getID(), subElementIds, mElementNames, mArraySizes); |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 761 | for(int i = 0; i < numSubElements; i ++) { |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 762 | mElements[i] = new Element(subElementIds[i], mRS); |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 763 | mElements[i].updateFromNative(); |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 764 | mOffsetInBytes[i] = mSize; |
| 765 | mSize += mElements[i].mSize * mArraySizes[i]; |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 766 | } |
| 767 | } |
| 768 | |
| 769 | } |
| 770 | |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 771 | /** |
| 772 | * Create a custom Element of the specified DataType. The DataKind will be |
| 773 | * set to USER and the vector size to 1 indicating non-vector. |
| 774 | * |
| 775 | * @param rs The context associated with the new Element. |
| 776 | * @param dt The DataType for the new element. |
| 777 | * @return Element |
| 778 | */ |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 779 | static Element createUser(RenderScript rs, DataType dt) { |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 780 | DataKind dk = DataKind.USER; |
| 781 | boolean norm = false; |
| 782 | int vecSize = 1; |
| 783 | int id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize); |
| 784 | return new Element(id, rs, dt, dk, norm, vecSize); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 785 | } |
| 786 | |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 787 | /** |
| 788 | * Create a custom vector element of the specified DataType and vector size. |
| 789 | * DataKind will be set to USER. |
| 790 | * |
| 791 | * @param rs The context associated with the new Element. |
| 792 | * @param dt The DataType for the new element. |
| 793 | * @param size Vector size for the new Element. Range 2-4 inclusive |
| 794 | * supported. |
| 795 | * |
| 796 | * @return Element |
| 797 | */ |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 798 | public static Element createVector(RenderScript rs, DataType dt, int size) { |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 799 | if (size < 2 || size > 4) { |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 800 | throw new RSIllegalArgumentException("Vector size out of range 2-4."); |
| Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 801 | } |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 802 | DataKind dk = DataKind.USER; |
| 803 | boolean norm = false; |
| 804 | int id = rs.nElementCreate(dt.mID, dk.mID, norm, size); |
| 805 | return new Element(id, rs, dt, dk, norm, size); |
| Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 806 | } |
| 807 | |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 808 | /** |
| 809 | * Create a new pixel Element type. A matching DataType and DataKind must |
| 810 | * be provided. The DataType and DataKind must contain the same number of |
| 811 | * components. Vector size will be set to 1. |
| 812 | * |
| 813 | * @param rs The context associated with the new Element. |
| 814 | * @param dt The DataType for the new element. |
| 815 | * @param dk The DataKind to specify the mapping of each component in the |
| 816 | * DataType. |
| 817 | * |
| 818 | * @return Element |
| 819 | */ |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 820 | public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) { |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 821 | if (!(dk == DataKind.PIXEL_L || |
| 822 | dk == DataKind.PIXEL_A || |
| 823 | dk == DataKind.PIXEL_LA || |
| 824 | dk == DataKind.PIXEL_RGB || |
| Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 825 | dk == DataKind.PIXEL_RGBA || |
| 826 | dk == DataKind.PIXEL_DEPTH)) { |
| Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 827 | throw new RSIllegalArgumentException("Unsupported DataKind"); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 828 | } |
| 829 | if (!(dt == DataType.UNSIGNED_8 || |
| Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 830 | dt == DataType.UNSIGNED_16 || |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 831 | dt == DataType.UNSIGNED_5_6_5 || |
| 832 | dt == DataType.UNSIGNED_4_4_4_4 || |
| 833 | dt == DataType.UNSIGNED_5_5_5_1)) { |
| Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 834 | throw new RSIllegalArgumentException("Unsupported DataType"); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 835 | } |
| 836 | if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) { |
| Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 837 | throw new RSIllegalArgumentException("Bad kind and type combo"); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 838 | } |
| 839 | if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) { |
| Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 840 | throw new RSIllegalArgumentException("Bad kind and type combo"); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 841 | } |
| 842 | if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) { |
| Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 843 | throw new RSIllegalArgumentException("Bad kind and type combo"); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 844 | } |
| Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 845 | if (dt == DataType.UNSIGNED_16 && |
| 846 | dk != DataKind.PIXEL_DEPTH) { |
| 847 | throw new RSIllegalArgumentException("Bad kind and type combo"); |
| 848 | } |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 849 | |
| 850 | int size = 1; |
| Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 851 | switch (dk) { |
| 852 | case PIXEL_LA: |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 853 | size = 2; |
| Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 854 | break; |
| 855 | case PIXEL_RGB: |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 856 | size = 3; |
| Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 857 | break; |
| 858 | case PIXEL_RGBA: |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 859 | size = 4; |
| Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 860 | break; |
| 861 | case PIXEL_DEPTH: |
| 862 | size = 2; |
| 863 | break; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 864 | } |
| 865 | |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 866 | boolean norm = true; |
| 867 | int id = rs.nElementCreate(dt.mID, dk.mID, norm, size); |
| 868 | return new Element(id, rs, dt, dk, norm, size); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 869 | } |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 870 | |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 871 | /** |
| Stephen Hines | f257e51 | 2011-06-14 14:54:29 -0700 | [diff] [blame] | 872 | * Check if the current Element is compatible with another Element. |
| 873 | * Primitive Elements are compatible if they share the same underlying |
| 874 | * size and type (i.e. U8 is compatible with A_8). User-defined Elements |
| 875 | * must be equal in order to be compatible. This requires strict name |
| 876 | * equivalence for all sub-Elements (in addition to structural equivalence). |
| 877 | * |
| 878 | * @param e The Element to check compatibility with. |
| 879 | * |
| 880 | * @return boolean true if the Elements are compatible, otherwise false. |
| 881 | */ |
| 882 | public boolean isCompatible(Element e) { |
| 883 | // Try strict BaseObj equality to start with. |
| 884 | if (this.equals(e)) { |
| 885 | return true; |
| 886 | } |
| 887 | |
| 888 | // Ignore mKind because it is allowed to be different (user vs. pixel). |
| 889 | // We also ignore mNormalized because it can be different. The mType |
| 890 | // field must be non-null since we require name equivalence for |
| 891 | // user-created Elements. |
| 892 | return ((mSize == e.mSize) && |
| 893 | (mType != null) && |
| 894 | (mType == e.mType) && |
| 895 | (mVectorSize == e.mVectorSize)); |
| 896 | } |
| 897 | |
| 898 | /** |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 899 | * Builder class for producing complex elements with matching field and name |
| 900 | * pairs. The builder starts empty. The order in which elements are added |
| 901 | * is retained for the layout in memory. |
| 902 | * |
| 903 | */ |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 904 | public static class Builder { |
| 905 | RenderScript mRS; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 906 | Element[] mElements; |
| 907 | String[] mElementNames; |
| Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 908 | int[] mArraySizes; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 909 | int mCount; |
| Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 910 | int mSkipPadding; |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 911 | |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 912 | /** |
| 913 | * Create a builder object. |
| 914 | * |
| 915 | * @param rs |
| 916 | */ |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 917 | public Builder(RenderScript rs) { |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 918 | mRS = rs; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 919 | mCount = 0; |
| 920 | mElements = new Element[8]; |
| 921 | mElementNames = new String[8]; |
| Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 922 | mArraySizes = new int[8]; |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 923 | } |
| 924 | |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 925 | /** |
| 926 | * Add an array of elements to this element. |
| 927 | * |
| 928 | * @param element |
| 929 | * @param name |
| 930 | * @param arraySize |
| 931 | */ |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 932 | public Builder add(Element element, String name, int arraySize) { |
| Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 933 | if (arraySize < 1) { |
| Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 934 | throw new RSIllegalArgumentException("Array size cannot be less than 1."); |
| Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 935 | } |
| Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 936 | |
| 937 | // Skip padding fields after a vector 3 type. |
| 938 | if (mSkipPadding != 0) { |
| 939 | if (name.startsWith("#padding_")) { |
| 940 | mSkipPadding = 0; |
| 941 | return this; |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | if (element.mVectorSize == 3) { |
| 946 | mSkipPadding = 1; |
| 947 | } else { |
| 948 | mSkipPadding = 0; |
| 949 | } |
| 950 | |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 951 | if(mCount == mElements.length) { |
| 952 | Element[] e = new Element[mCount + 8]; |
| 953 | String[] s = new String[mCount + 8]; |
| Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 954 | int[] as = new int[mCount + 8]; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 955 | System.arraycopy(mElements, 0, e, 0, mCount); |
| 956 | System.arraycopy(mElementNames, 0, s, 0, mCount); |
| Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 957 | System.arraycopy(mArraySizes, 0, as, 0, mCount); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 958 | mElements = e; |
| 959 | mElementNames = s; |
| Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 960 | mArraySizes = as; |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 961 | } |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 962 | mElements[mCount] = element; |
| 963 | mElementNames[mCount] = name; |
| Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 964 | mArraySizes[mCount] = arraySize; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 965 | mCount++; |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 966 | return this; |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 967 | } |
| 968 | |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 969 | /** |
| 970 | * Add a single element to this Element. |
| 971 | * |
| 972 | * @param element |
| 973 | * @param name |
| 974 | */ |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 975 | public Builder add(Element element, String name) { |
| 976 | return add(element, name, 1); |
| Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 977 | } |
| 978 | |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 979 | /** |
| 980 | * Create the element from this builder. |
| 981 | * |
| 982 | * |
| 983 | * @return Element |
| 984 | */ |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 985 | public Element create() { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 986 | mRS.validate(); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 987 | Element[] ein = new Element[mCount]; |
| 988 | String[] sin = new String[mCount]; |
| Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 989 | int[] asin = new int[mCount]; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 990 | java.lang.System.arraycopy(mElements, 0, ein, 0, mCount); |
| 991 | java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount); |
| Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 992 | java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount); |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 993 | |
| 994 | int[] ids = new int[ein.length]; |
| 995 | for (int ct = 0; ct < ein.length; ct++ ) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 996 | ids[ct] = ein[ct].getID(); |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 997 | } |
| Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 998 | int id = mRS.nElementCreate2(ids, sin, asin); |
| 999 | return new Element(id, mRS, ein, sin, asin); |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 1000 | } |
| 1001 | } |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |