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 | /** |
| 23 | * @hide |
| 24 | * |
| 25 | **/ |
| 26 | public class Element extends BaseObj { |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 27 | int mSize; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 28 | Element[] mElements; |
| 29 | String[] mElementNames; |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 30 | int[] mArraySizes; |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 31 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 32 | DataType mType; |
| 33 | DataKind mKind; |
| 34 | boolean mNormalized; |
| 35 | int mVectorSize; |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 36 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 37 | int getSizeBytes() {return mSize;} |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 38 | |
| 39 | public enum DataType { |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 40 | //FLOAT_16 (1, 2), |
| 41 | FLOAT_32 (2, 4), |
Stephen Hines | 02f41705 | 2010-09-30 15:19:22 -0700 | [diff] [blame] | 42 | FLOAT_64 (3, 8), |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 43 | SIGNED_8 (4, 1), |
| 44 | SIGNED_16 (5, 2), |
| 45 | SIGNED_32 (6, 4), |
Stephen Hines | ef1dac2 | 2010-10-01 15:39:33 -0700 | [diff] [blame] | 46 | SIGNED_64 (7, 8), |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 47 | UNSIGNED_8 (8, 1), |
| 48 | UNSIGNED_16 (9, 2), |
| 49 | UNSIGNED_32 (10, 4), |
Stephen Hines | 52d8363 | 2010-10-11 16:10:42 -0700 | [diff] [blame] | 50 | UNSIGNED_64 (11, 8), |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 51 | |
Jason Sams | f110d4b | 2010-06-21 17:42:41 -0700 | [diff] [blame] | 52 | BOOLEAN(12, 1), |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 53 | |
Jason Sams | f110d4b | 2010-06-21 17:42:41 -0700 | [diff] [blame] | 54 | UNSIGNED_5_6_5 (13, 2), |
| 55 | UNSIGNED_5_5_5_1 (14, 2), |
| 56 | UNSIGNED_4_4_4_4 (15, 2), |
| 57 | |
Jason Sams | 1d45c47 | 2010-08-25 14:31:48 -0700 | [diff] [blame] | 58 | MATRIX_4X4 (16, 64), |
| 59 | MATRIX_3X3 (17, 36), |
| 60 | MATRIX_2X2 (18, 16), |
| 61 | |
| 62 | RS_ELEMENT (1000, 4), |
| 63 | RS_TYPE (1001, 4), |
| 64 | RS_ALLOCATION (1002, 4), |
| 65 | RS_SAMPLER (1003, 4), |
| 66 | RS_SCRIPT (1004, 4), |
| 67 | RS_MESH (1005, 4), |
| 68 | RS_PROGRAM_FRAGMENT (1006, 4), |
| 69 | RS_PROGRAM_VERTEX (1007, 4), |
| 70 | RS_PROGRAM_RASTER (1008, 4), |
| 71 | RS_PROGRAM_STORE (1009, 4); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 72 | |
| 73 | int mID; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 74 | int mSize; |
| 75 | DataType(int id, int size) { |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 76 | mID = id; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 77 | mSize = size; |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | public enum DataKind { |
| 82 | USER (0), |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 83 | |
| 84 | PIXEL_L (7), |
| 85 | PIXEL_A (8), |
| 86 | PIXEL_LA (9), |
| 87 | PIXEL_RGB (10), |
| 88 | PIXEL_RGBA (11); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 89 | |
| 90 | int mID; |
| 91 | DataKind(int id) { |
| 92 | mID = id; |
| 93 | } |
| 94 | } |
| 95 | |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 96 | public boolean isComplex() { |
| 97 | if (mElements == null) { |
| 98 | return false; |
| 99 | } |
| 100 | for (int ct=0; ct < mElements.length; ct++) { |
| 101 | if (mElements[ct].mElements != null) { |
| 102 | return true; |
| 103 | } |
| 104 | } |
| 105 | return false; |
| 106 | } |
| 107 | |
Jason Sams | f110d4b | 2010-06-21 17:42:41 -0700 | [diff] [blame] | 108 | public static Element BOOLEAN(RenderScript rs) { |
| 109 | if(rs.mElement_BOOLEAN == null) { |
| 110 | rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN); |
| 111 | } |
| 112 | return rs.mElement_BOOLEAN; |
| 113 | } |
| 114 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 115 | public static Element U8(RenderScript rs) { |
| 116 | if(rs.mElement_U8 == null) { |
| 117 | rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 118 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 119 | return rs.mElement_U8; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 122 | public static Element I8(RenderScript rs) { |
| 123 | if(rs.mElement_I8 == null) { |
| 124 | rs.mElement_I8 = createUser(rs, DataType.SIGNED_8); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 125 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 126 | return rs.mElement_I8; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Jason Sams | e29f3e7 | 2010-06-08 15:40:48 -0700 | [diff] [blame] | 129 | public static Element U16(RenderScript rs) { |
| 130 | if(rs.mElement_U16 == null) { |
| 131 | rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16); |
| 132 | } |
| 133 | return rs.mElement_U16; |
| 134 | } |
| 135 | |
| 136 | public static Element I16(RenderScript rs) { |
| 137 | if(rs.mElement_I16 == null) { |
| 138 | rs.mElement_I16 = createUser(rs, DataType.SIGNED_16); |
| 139 | } |
| 140 | return rs.mElement_I16; |
| 141 | } |
| 142 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 143 | public static Element U32(RenderScript rs) { |
| 144 | if(rs.mElement_U32 == null) { |
| 145 | rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 146 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 147 | return rs.mElement_U32; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 150 | public static Element I32(RenderScript rs) { |
| 151 | if(rs.mElement_I32 == null) { |
| 152 | rs.mElement_I32 = createUser(rs, DataType.SIGNED_32); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 153 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 154 | return rs.mElement_I32; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Stephen Hines | 52d8363 | 2010-10-11 16:10:42 -0700 | [diff] [blame] | 157 | public static Element U64(RenderScript rs) { |
| 158 | if(rs.mElement_U64 == null) { |
| 159 | rs.mElement_U64 = createUser(rs, DataType.UNSIGNED_64); |
| 160 | } |
| 161 | return rs.mElement_U64; |
| 162 | } |
| 163 | |
Stephen Hines | ef1dac2 | 2010-10-01 15:39:33 -0700 | [diff] [blame] | 164 | public static Element I64(RenderScript rs) { |
| 165 | if(rs.mElement_I64 == null) { |
| 166 | rs.mElement_I64 = createUser(rs, DataType.SIGNED_64); |
| 167 | } |
| 168 | return rs.mElement_I64; |
| 169 | } |
| 170 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 171 | public static Element F32(RenderScript rs) { |
| 172 | if(rs.mElement_F32 == null) { |
| 173 | rs.mElement_F32 = createUser(rs, DataType.FLOAT_32); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 174 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 175 | return rs.mElement_F32; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Stephen Hines | 02f41705 | 2010-09-30 15:19:22 -0700 | [diff] [blame] | 178 | public static Element F64(RenderScript rs) { |
| 179 | if(rs.mElement_F64 == null) { |
| 180 | rs.mElement_F64 = createUser(rs, DataType.FLOAT_64); |
| 181 | } |
| 182 | return rs.mElement_F64; |
| 183 | } |
| 184 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 185 | public static Element ELEMENT(RenderScript rs) { |
| 186 | if(rs.mElement_ELEMENT == null) { |
| 187 | rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 188 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 189 | return rs.mElement_ELEMENT; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 192 | public static Element TYPE(RenderScript rs) { |
| 193 | if(rs.mElement_TYPE == null) { |
| 194 | rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 195 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 196 | return rs.mElement_TYPE; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 199 | public static Element ALLOCATION(RenderScript rs) { |
| 200 | if(rs.mElement_ALLOCATION == null) { |
| 201 | rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 202 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 203 | return rs.mElement_ALLOCATION; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 206 | public static Element SAMPLER(RenderScript rs) { |
| 207 | if(rs.mElement_SAMPLER == null) { |
| 208 | rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 209 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 210 | return rs.mElement_SAMPLER; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 213 | public static Element SCRIPT(RenderScript rs) { |
| 214 | if(rs.mElement_SCRIPT == null) { |
| 215 | rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 216 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 217 | return rs.mElement_SCRIPT; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 220 | public static Element MESH(RenderScript rs) { |
| 221 | if(rs.mElement_MESH == null) { |
| 222 | rs.mElement_MESH = createUser(rs, DataType.RS_MESH); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 223 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 224 | return rs.mElement_MESH; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 227 | public static Element PROGRAM_FRAGMENT(RenderScript rs) { |
| 228 | if(rs.mElement_PROGRAM_FRAGMENT == null) { |
| 229 | rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 230 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 231 | return rs.mElement_PROGRAM_FRAGMENT; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 234 | public static Element PROGRAM_VERTEX(RenderScript rs) { |
| 235 | if(rs.mElement_PROGRAM_VERTEX == null) { |
| 236 | rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 237 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 238 | return rs.mElement_PROGRAM_VERTEX; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 241 | public static Element PROGRAM_RASTER(RenderScript rs) { |
| 242 | if(rs.mElement_PROGRAM_RASTER == null) { |
| 243 | rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 244 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 245 | return rs.mElement_PROGRAM_RASTER; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 248 | public static Element PROGRAM_STORE(RenderScript rs) { |
| 249 | if(rs.mElement_PROGRAM_STORE == null) { |
| 250 | rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 251 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 252 | return rs.mElement_PROGRAM_STORE; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 256 | public static Element A_8(RenderScript rs) { |
| 257 | if(rs.mElement_A_8 == null) { |
| 258 | rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A); |
| 259 | } |
| 260 | return rs.mElement_A_8; |
| 261 | } |
| 262 | |
| 263 | public static Element RGB_565(RenderScript rs) { |
| 264 | if(rs.mElement_RGB_565 == null) { |
| 265 | rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB); |
| 266 | } |
| 267 | return rs.mElement_RGB_565; |
| 268 | } |
| 269 | |
| 270 | public static Element RGB_888(RenderScript rs) { |
| 271 | if(rs.mElement_RGB_888 == null) { |
| 272 | rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB); |
| 273 | } |
| 274 | return rs.mElement_RGB_888; |
| 275 | } |
| 276 | |
| 277 | public static Element RGBA_5551(RenderScript rs) { |
| 278 | if(rs.mElement_RGBA_5551 == null) { |
| 279 | rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA); |
| 280 | } |
| 281 | return rs.mElement_RGBA_5551; |
| 282 | } |
| 283 | |
| 284 | public static Element RGBA_4444(RenderScript rs) { |
| 285 | if(rs.mElement_RGBA_4444 == null) { |
| 286 | rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA); |
| 287 | } |
| 288 | return rs.mElement_RGBA_4444; |
| 289 | } |
| 290 | |
| 291 | public static Element RGBA_8888(RenderScript rs) { |
| 292 | if(rs.mElement_RGBA_8888 == null) { |
| 293 | rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA); |
| 294 | } |
| 295 | return rs.mElement_RGBA_8888; |
| 296 | } |
| 297 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 298 | public static Element F32_2(RenderScript rs) { |
| 299 | if(rs.mElement_FLOAT_2 == null) { |
| 300 | rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 301 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 302 | return rs.mElement_FLOAT_2; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 303 | } |
| 304 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 305 | public static Element F32_3(RenderScript rs) { |
| 306 | if(rs.mElement_FLOAT_3 == null) { |
| 307 | rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 308 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 309 | return rs.mElement_FLOAT_3; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 312 | public static Element F32_4(RenderScript rs) { |
| 313 | if(rs.mElement_FLOAT_4 == null) { |
| 314 | rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 315 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 316 | return rs.mElement_FLOAT_4; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 317 | } |
| 318 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 319 | public static Element U8_4(RenderScript rs) { |
| 320 | if(rs.mElement_UCHAR_4 == null) { |
| 321 | rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 322 | } |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 323 | return rs.mElement_UCHAR_4; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Jason Sams | 1d45c47 | 2010-08-25 14:31:48 -0700 | [diff] [blame] | 326 | public static Element MATRIX_4X4(RenderScript rs) { |
| 327 | if(rs.mElement_MATRIX_4X4 == null) { |
| 328 | rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4); |
| 329 | } |
| 330 | return rs.mElement_MATRIX_4X4; |
| 331 | } |
| 332 | public static Element MATRIX4X4(RenderScript rs) { |
| 333 | return MATRIX_4X4(rs); |
| 334 | } |
| 335 | |
| 336 | public static Element MATRIX_3X3(RenderScript rs) { |
| 337 | if(rs.mElement_MATRIX_3X3 == null) { |
| 338 | rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3); |
| 339 | } |
| 340 | return rs.mElement_MATRIX_4X4; |
| 341 | } |
| 342 | |
| 343 | public static Element MATRIX_2X2(RenderScript rs) { |
| 344 | if(rs.mElement_MATRIX_2X2 == null) { |
| 345 | rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2); |
| 346 | } |
| 347 | return rs.mElement_MATRIX_2X2; |
| 348 | } |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 349 | |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 350 | Element(int id, RenderScript rs, Element[] e, String[] n, int[] as) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 351 | super(id, rs); |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 352 | mSize = 0; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 353 | mElements = e; |
| 354 | mElementNames = n; |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 355 | mArraySizes = as; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 356 | for (int ct = 0; ct < mElements.length; ct++ ) { |
Alex Sakhartchouk | 9e401bc | 2010-10-13 14:22:02 -0700 | [diff] [blame] | 357 | mSize += mElements[ct].mSize * mArraySizes[ct]; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 358 | } |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 361 | Element(int id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) { |
| 362 | super(id, rs); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 363 | mSize = dt.mSize * size; |
| 364 | mType = dt; |
| 365 | mKind = dk; |
| 366 | mNormalized = norm; |
| 367 | mVectorSize = size; |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 370 | Element(int id, RenderScript rs) { |
| 371 | super(id, rs); |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | @Override |
| 375 | void updateFromNative() { |
| 376 | |
| 377 | // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements |
| 378 | int[] dataBuffer = new int[5]; |
| 379 | mRS.nElementGetNativeData(mID, dataBuffer); |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 380 | |
| 381 | mNormalized = dataBuffer[2] == 1 ? true : false; |
| 382 | mVectorSize = dataBuffer[3]; |
| 383 | mSize = 0; |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 384 | for (DataType dt: DataType.values()) { |
| 385 | if(dt.mID == dataBuffer[0]){ |
| 386 | mType = dt; |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 387 | mSize = mType.mSize * mVectorSize; |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | for (DataKind dk: DataKind.values()) { |
| 391 | if(dk.mID == dataBuffer[1]){ |
| 392 | mKind = dk; |
| 393 | } |
| 394 | } |
| 395 | |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 396 | int numSubElements = dataBuffer[4]; |
| 397 | if(numSubElements > 0) { |
| 398 | mElements = new Element[numSubElements]; |
| 399 | mElementNames = new String[numSubElements]; |
| 400 | |
| 401 | int[] subElementIds = new int[numSubElements]; |
| 402 | mRS.nElementGetSubElements(mID, subElementIds, mElementNames); |
| 403 | for(int i = 0; i < numSubElements; i ++) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 404 | mElements[i] = new Element(subElementIds[i], mRS); |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 405 | mElements[i].updateFromNative(); |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 406 | mSize += mElements[i].mSize; |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
| 410 | } |
| 411 | |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 412 | public void destroy() { |
Jason Sams | 7ce033d | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 413 | super.destroy(); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 416 | ///////////////////////////////////////// |
| 417 | public static Element createUser(RenderScript rs, DataType dt) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 418 | DataKind dk = DataKind.USER; |
| 419 | boolean norm = false; |
| 420 | int vecSize = 1; |
| 421 | int id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize); |
| 422 | return new Element(id, rs, dt, dk, norm, vecSize); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | public static Element createVector(RenderScript rs, DataType dt, int size) { |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 426 | if (size < 2 || size > 4) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 427 | throw new RSIllegalArgumentException("Vector size out of rance 2-4."); |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 428 | } |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 429 | DataKind dk = DataKind.USER; |
| 430 | boolean norm = false; |
| 431 | int id = rs.nElementCreate(dt.mID, dk.mID, norm, size); |
| 432 | return new Element(id, rs, dt, dk, norm, size); |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 435 | public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) { |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 436 | if (!(dk == DataKind.PIXEL_L || |
| 437 | dk == DataKind.PIXEL_A || |
| 438 | dk == DataKind.PIXEL_LA || |
| 439 | dk == DataKind.PIXEL_RGB || |
| 440 | dk == DataKind.PIXEL_RGBA)) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 441 | throw new RSIllegalArgumentException("Unsupported DataKind"); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 442 | } |
| 443 | if (!(dt == DataType.UNSIGNED_8 || |
| 444 | dt == DataType.UNSIGNED_5_6_5 || |
| 445 | dt == DataType.UNSIGNED_4_4_4_4 || |
| 446 | dt == DataType.UNSIGNED_5_5_5_1)) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 447 | throw new RSIllegalArgumentException("Unsupported DataType"); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 448 | } |
| 449 | if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 450 | throw new RSIllegalArgumentException("Bad kind and type combo"); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 451 | } |
| 452 | if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 453 | throw new RSIllegalArgumentException("Bad kind and type combo"); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 454 | } |
| 455 | if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 456 | throw new RSIllegalArgumentException("Bad kind and type combo"); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | int size = 1; |
| 460 | if (dk == DataKind.PIXEL_LA) { |
| 461 | size = 2; |
| 462 | } |
| 463 | if (dk == DataKind.PIXEL_RGB) { |
| 464 | size = 3; |
| 465 | } |
| 466 | if (dk == DataKind.PIXEL_RGBA) { |
| 467 | size = 4; |
| 468 | } |
| 469 | |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 470 | boolean norm = true; |
| 471 | int id = rs.nElementCreate(dt.mID, dk.mID, norm, size); |
| 472 | return new Element(id, rs, dt, dk, norm, size); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 473 | } |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 474 | |
| 475 | public static class Builder { |
| 476 | RenderScript mRS; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 477 | Element[] mElements; |
| 478 | String[] mElementNames; |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 479 | int[] mArraySizes; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 480 | int mCount; |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 481 | |
| 482 | public Builder(RenderScript rs) { |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 483 | mRS = rs; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 484 | mCount = 0; |
| 485 | mElements = new Element[8]; |
| 486 | mElementNames = new String[8]; |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 487 | mArraySizes = new int[8]; |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 490 | public void add(Element element, String name, int arraySize) { |
| 491 | if (arraySize < 1) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 492 | throw new RSIllegalArgumentException("Array size cannot be less than 1."); |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 493 | } |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 494 | if(mCount == mElements.length) { |
| 495 | Element[] e = new Element[mCount + 8]; |
| 496 | String[] s = new String[mCount + 8]; |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 497 | int[] as = new int[mCount + 8]; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 498 | System.arraycopy(mElements, 0, e, 0, mCount); |
| 499 | System.arraycopy(mElementNames, 0, s, 0, mCount); |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 500 | System.arraycopy(mArraySizes, 0, as, 0, mCount); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 501 | mElements = e; |
| 502 | mElementNames = s; |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 503 | mArraySizes = as; |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 504 | } |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 505 | mElements[mCount] = element; |
| 506 | mElementNames[mCount] = name; |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 507 | mArraySizes[mCount] = arraySize; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 508 | mCount++; |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 511 | public void add(Element element, String name) { |
| 512 | add(element, name, 1); |
| 513 | } |
| 514 | |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 515 | public Element create() { |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 516 | mRS.validate(); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 517 | Element[] ein = new Element[mCount]; |
| 518 | String[] sin = new String[mCount]; |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 519 | int[] asin = new int[mCount]; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 520 | java.lang.System.arraycopy(mElements, 0, ein, 0, mCount); |
| 521 | java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount); |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 522 | java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount); |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 523 | |
| 524 | int[] ids = new int[ein.length]; |
| 525 | for (int ct = 0; ct < ein.length; ct++ ) { |
| 526 | ids[ct] = ein[ct].mID; |
| 527 | } |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 528 | int id = mRS.nElementCreate2(ids, sin, asin); |
| 529 | return new Element(id, mRS, ein, sin, asin); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 533 | static void initPredefined(RenderScript rs) { |
| 534 | int a8 = rs.nElementCreate(DataType.UNSIGNED_8.mID, |
| 535 | DataKind.PIXEL_A.mID, true, 1); |
| 536 | int rgba4444 = rs.nElementCreate(DataType.UNSIGNED_4_4_4_4.mID, |
| 537 | DataKind.PIXEL_RGBA.mID, true, 4); |
| 538 | int rgba8888 = rs.nElementCreate(DataType.UNSIGNED_8.mID, |
| 539 | DataKind.PIXEL_RGBA.mID, true, 4); |
| 540 | int rgb565 = rs.nElementCreate(DataType.UNSIGNED_5_6_5.mID, |
| 541 | DataKind.PIXEL_RGB.mID, true, 3); |
| 542 | rs.nInitElements(a8, rgba4444, rgba8888, rgb565); |
| 543 | } |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 544 | } |
| 545 | |