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