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