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