Element.java revision f110d4b787b91dabe968a812e76e5c1f8d953487
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 17package android.renderscript; 18 19import java.lang.reflect.Field; 20 21/** 22 * @hide 23 * 24 **/ 25public class Element extends BaseObj { 26 int mSize; 27 Element[] mElements; 28 String[] mElementNames; 29 30 DataType mType; 31 DataKind mKind; 32 boolean mNormalized; 33 int mVectorSize; 34 35 int getSizeBytes() {return mSize;} 36 37 public enum DataType { 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 BOOLEAN(12, 1), 51 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); 66 67 int mID; 68 int mSize; 69 DataType(int id, int size) { 70 mID = id; 71 mSize = size; 72 } 73 } 74 75 public enum DataKind { 76 USER (0), 77 78 PIXEL_L (7), 79 PIXEL_A (8), 80 PIXEL_LA (9), 81 PIXEL_RGB (10), 82 PIXEL_RGBA (11); 83 84 int mID; 85 DataKind(int id) { 86 mID = id; 87 } 88 } 89 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 97 public static Element U8(RenderScript rs) { 98 if(rs.mElement_U8 == null) { 99 rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8); 100 } 101 return rs.mElement_U8; 102 } 103 104 public static Element I8(RenderScript rs) { 105 if(rs.mElement_I8 == null) { 106 rs.mElement_I8 = createUser(rs, DataType.SIGNED_8); 107 } 108 return rs.mElement_I8; 109 } 110 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 125 public static Element U32(RenderScript rs) { 126 if(rs.mElement_U32 == null) { 127 rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32); 128 } 129 return rs.mElement_U32; 130 } 131 132 public static Element I32(RenderScript rs) { 133 if(rs.mElement_I32 == null) { 134 rs.mElement_I32 = createUser(rs, DataType.SIGNED_32); 135 } 136 return rs.mElement_I32; 137 } 138 139 public static Element F32(RenderScript rs) { 140 if(rs.mElement_F32 == null) { 141 rs.mElement_F32 = createUser(rs, DataType.FLOAT_32); 142 } 143 return rs.mElement_F32; 144 } 145 146 public static Element ELEMENT(RenderScript rs) { 147 if(rs.mElement_ELEMENT == null) { 148 rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT); 149 } 150 return rs.mElement_ELEMENT; 151 } 152 153 public static Element TYPE(RenderScript rs) { 154 if(rs.mElement_TYPE == null) { 155 rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE); 156 } 157 return rs.mElement_TYPE; 158 } 159 160 public static Element ALLOCATION(RenderScript rs) { 161 if(rs.mElement_ALLOCATION == null) { 162 rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION); 163 } 164 return rs.mElement_ALLOCATION; 165 } 166 167 public static Element SAMPLER(RenderScript rs) { 168 if(rs.mElement_SAMPLER == null) { 169 rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER); 170 } 171 return rs.mElement_SAMPLER; 172 } 173 174 public static Element SCRIPT(RenderScript rs) { 175 if(rs.mElement_SCRIPT == null) { 176 rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT); 177 } 178 return rs.mElement_SCRIPT; 179 } 180 181 public static Element MESH(RenderScript rs) { 182 if(rs.mElement_MESH == null) { 183 rs.mElement_MESH = createUser(rs, DataType.RS_MESH); 184 } 185 return rs.mElement_MESH; 186 } 187 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); 191 } 192 return rs.mElement_PROGRAM_FRAGMENT; 193 } 194 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); 198 } 199 return rs.mElement_PROGRAM_VERTEX; 200 } 201 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); 205 } 206 return rs.mElement_PROGRAM_RASTER; 207 } 208 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); 212 } 213 return rs.mElement_PROGRAM_STORE; 214 } 215 216 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 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); 262 } 263 return rs.mElement_FLOAT_2; 264 } 265 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); 269 } 270 return rs.mElement_FLOAT_3; 271 } 272 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); 276 } 277 return rs.mElement_FLOAT_4; 278 } 279 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); 283 } 284 return rs.mElement_UCHAR_4; 285 } 286 287 288 Element(RenderScript rs, Element[] e, String[] n) { 289 super(rs); 290 mSize = 0; 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); 309 } 310 311 public void destroy() throws IllegalStateException { 312 super.destroy(); 313 } 314 315 ///////////////////////////////////////// 316 public static Element createUser(RenderScript rs, DataType dt) { 317 return new Element(rs, dt, DataKind.USER, false, 1); 318 } 319 320 public static Element createVector(RenderScript rs, DataType dt, int size) { 321 if (size < 2 || size > 4) { 322 throw new IllegalArgumentException("Bad size"); 323 } 324 return new Element(rs, dt, DataKind.USER, false, size); 325 } 326 327 public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) { 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 } 364 365 public static class Builder { 366 RenderScript mRS; 367 Element[] mElements; 368 String[] mElementNames; 369 int mCount; 370 371 public Builder(RenderScript rs) { 372 mRS = rs; 373 mCount = 0; 374 mElements = new Element[8]; 375 mElementNames = new String[8]; 376 } 377 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; 386 } 387 mElements[mCount] = element; 388 mElementNames[mCount] = name; 389 mCount++; 390 } 391 392 public Element create() { 393 mRS.validate(); 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); 399 } 400 } 401 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 } 413} 414 415