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