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