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