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