Element.java revision a23d4e792cb13090c540edfdd5cee03799bb9d48
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; 20import android.util.Log; 21 22/** 23 * Element is the basic data type of RenderScript. An element can be of 2 24 * forms. Basic elements contain a single component of data. This can be of 25 * any of the legal RS types. Examples of basic element types. 26 * Single float value 27 * 4 element float vector 28 * single RGB-565 color 29 * single unsigned int 16 30 * 31 * Complex elements will contain a list of sub-elements and names. This in 32 * effect represents a structure of data. The fields can be accessed by name 33 * from a script or shader. The memory layout is defined and ordered. Data 34 * alignment is determinied by the most basic primitive type. i.e. a float4 35 * vector will be alligned to sizeof(float) and not sizeof(float4). The 36 * ordering of elements in memory will be the order in which they were added 37 * with each component aligned as necessary. No re-ordering will be done. 38 * 39 * The primary source of elements will be from scripts. A script that exports a 40 * bind point for a data structure will generate a RS element to represent the 41 * data exported by the script. 42 **/ 43public class Element extends BaseObj { 44 int mSize; 45 Element[] mElements; 46 String[] mElementNames; 47 int[] mArraySizes; 48 49 DataType mType; 50 DataKind mKind; 51 boolean mNormalized; 52 int mVectorSize; 53 54 int getSizeBytes() {return mSize;} 55 56 57 /** 58 * DataType represents the basic type information for a basic element. The 59 * naming convention follows. For numeric types its FLOAT, SIGNED, UNSIGNED 60 * followed by the _BITS where BITS is the size of the data. BOOLEAN is a 61 * true / false (1,0) represented in an 8 bit container. The UNSIGNED 62 * variants with multiple bit definitions are for packed graphical data 63 * formats and represents vectors with per vector member sizes which are 64 * treated as a single unit for packing and alignment purposes. 65 * 66 * MATRIX the three matrix types contain FLOAT_32 elements and are treated 67 * as 32 bits for alignment purposes. 68 * 69 * RS_* objects. 32 bit opaque handles. 70 */ 71 public enum DataType { 72 //FLOAT_16 (1, 2), 73 FLOAT_32 (2, 4), 74 FLOAT_64 (3, 8), 75 SIGNED_8 (4, 1), 76 SIGNED_16 (5, 2), 77 SIGNED_32 (6, 4), 78 SIGNED_64 (7, 8), 79 UNSIGNED_8 (8, 1), 80 UNSIGNED_16 (9, 2), 81 UNSIGNED_32 (10, 4), 82 UNSIGNED_64 (11, 8), 83 84 BOOLEAN(12, 1), 85 86 UNSIGNED_5_6_5 (13, 2), 87 UNSIGNED_5_5_5_1 (14, 2), 88 UNSIGNED_4_4_4_4 (15, 2), 89 90 MATRIX_4X4 (16, 64), 91 MATRIX_3X3 (17, 36), 92 MATRIX_2X2 (18, 16), 93 94 RS_ELEMENT (1000, 4), 95 RS_TYPE (1001, 4), 96 RS_ALLOCATION (1002, 4), 97 RS_SAMPLER (1003, 4), 98 RS_SCRIPT (1004, 4), 99 RS_MESH (1005, 4), 100 RS_PROGRAM_FRAGMENT (1006, 4), 101 RS_PROGRAM_VERTEX (1007, 4), 102 RS_PROGRAM_RASTER (1008, 4), 103 RS_PROGRAM_STORE (1009, 4); 104 105 int mID; 106 int mSize; 107 DataType(int id, int size) { 108 mID = id; 109 mSize = size; 110 } 111 } 112 113 /** 114 * The special interpretation of the data if required. This is primarly 115 * useful for graphical data. USER indicates no special interpretation is 116 * expected. PIXEL is used in conjunction with the standard data types for 117 * representing texture formats. 118 */ 119 public enum DataKind { 120 USER (0), 121 122 PIXEL_L (7), 123 PIXEL_A (8), 124 PIXEL_LA (9), 125 PIXEL_RGB (10), 126 PIXEL_RGBA (11); 127 128 int mID; 129 DataKind(int id) { 130 mID = id; 131 } 132 } 133 134 /** 135 * Return if a element is too complex for use as a data source for a Mesh or 136 * a Program. 137 * 138 * @return boolean 139 */ 140 public boolean isComplex() { 141 if (mElements == null) { 142 return false; 143 } 144 for (int ct=0; ct < mElements.length; ct++) { 145 if (mElements[ct].mElements != null) { 146 return true; 147 } 148 } 149 return false; 150 } 151 152 /** 153 * Utility function for returning an Element containing a single Boolean. 154 * 155 * @param rs Context to which the element will belong. 156 * 157 * @return Element 158 */ 159 public static Element BOOLEAN(RenderScript rs) { 160 if(rs.mElement_BOOLEAN == null) { 161 rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN); 162 } 163 return rs.mElement_BOOLEAN; 164 } 165 166 /** 167 * Utility function for returning an Element containing a single UNSIGNED_8. 168 * 169 * @param rs Context to which the element will belong. 170 * 171 * @return Element 172 */ 173 public static Element U8(RenderScript rs) { 174 if(rs.mElement_U8 == null) { 175 rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8); 176 } 177 return rs.mElement_U8; 178 } 179 180 /** 181 * Utility function for returning an Element containing a single SIGNED_8. 182 * 183 * @param rs Context to which the element will belong. 184 * 185 * @return Element 186 */ 187 public static Element I8(RenderScript rs) { 188 if(rs.mElement_I8 == null) { 189 rs.mElement_I8 = createUser(rs, DataType.SIGNED_8); 190 } 191 return rs.mElement_I8; 192 } 193 194 public static Element U16(RenderScript rs) { 195 if(rs.mElement_U16 == null) { 196 rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16); 197 } 198 return rs.mElement_U16; 199 } 200 201 public static Element I16(RenderScript rs) { 202 if(rs.mElement_I16 == null) { 203 rs.mElement_I16 = createUser(rs, DataType.SIGNED_16); 204 } 205 return rs.mElement_I16; 206 } 207 208 public static Element U32(RenderScript rs) { 209 if(rs.mElement_U32 == null) { 210 rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32); 211 } 212 return rs.mElement_U32; 213 } 214 215 public static Element I32(RenderScript rs) { 216 if(rs.mElement_I32 == null) { 217 rs.mElement_I32 = createUser(rs, DataType.SIGNED_32); 218 } 219 return rs.mElement_I32; 220 } 221 222 public static Element U64(RenderScript rs) { 223 if(rs.mElement_U64 == null) { 224 rs.mElement_U64 = createUser(rs, DataType.UNSIGNED_64); 225 } 226 return rs.mElement_U64; 227 } 228 229 public static Element I64(RenderScript rs) { 230 if(rs.mElement_I64 == null) { 231 rs.mElement_I64 = createUser(rs, DataType.SIGNED_64); 232 } 233 return rs.mElement_I64; 234 } 235 236 public static Element F32(RenderScript rs) { 237 if(rs.mElement_F32 == null) { 238 rs.mElement_F32 = createUser(rs, DataType.FLOAT_32); 239 } 240 return rs.mElement_F32; 241 } 242 243 public static Element F64(RenderScript rs) { 244 if(rs.mElement_F64 == null) { 245 rs.mElement_F64 = createUser(rs, DataType.FLOAT_64); 246 } 247 return rs.mElement_F64; 248 } 249 250 public static Element ELEMENT(RenderScript rs) { 251 if(rs.mElement_ELEMENT == null) { 252 rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT); 253 } 254 return rs.mElement_ELEMENT; 255 } 256 257 public static Element TYPE(RenderScript rs) { 258 if(rs.mElement_TYPE == null) { 259 rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE); 260 } 261 return rs.mElement_TYPE; 262 } 263 264 public static Element ALLOCATION(RenderScript rs) { 265 if(rs.mElement_ALLOCATION == null) { 266 rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION); 267 } 268 return rs.mElement_ALLOCATION; 269 } 270 271 public static Element SAMPLER(RenderScript rs) { 272 if(rs.mElement_SAMPLER == null) { 273 rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER); 274 } 275 return rs.mElement_SAMPLER; 276 } 277 278 public static Element SCRIPT(RenderScript rs) { 279 if(rs.mElement_SCRIPT == null) { 280 rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT); 281 } 282 return rs.mElement_SCRIPT; 283 } 284 285 public static Element MESH(RenderScript rs) { 286 if(rs.mElement_MESH == null) { 287 rs.mElement_MESH = createUser(rs, DataType.RS_MESH); 288 } 289 return rs.mElement_MESH; 290 } 291 292 public static Element PROGRAM_FRAGMENT(RenderScript rs) { 293 if(rs.mElement_PROGRAM_FRAGMENT == null) { 294 rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT); 295 } 296 return rs.mElement_PROGRAM_FRAGMENT; 297 } 298 299 public static Element PROGRAM_VERTEX(RenderScript rs) { 300 if(rs.mElement_PROGRAM_VERTEX == null) { 301 rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX); 302 } 303 return rs.mElement_PROGRAM_VERTEX; 304 } 305 306 public static Element PROGRAM_RASTER(RenderScript rs) { 307 if(rs.mElement_PROGRAM_RASTER == null) { 308 rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER); 309 } 310 return rs.mElement_PROGRAM_RASTER; 311 } 312 313 public static Element PROGRAM_STORE(RenderScript rs) { 314 if(rs.mElement_PROGRAM_STORE == null) { 315 rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE); 316 } 317 return rs.mElement_PROGRAM_STORE; 318 } 319 320 321 public static Element A_8(RenderScript rs) { 322 if(rs.mElement_A_8 == null) { 323 rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A); 324 } 325 return rs.mElement_A_8; 326 } 327 328 public static Element RGB_565(RenderScript rs) { 329 if(rs.mElement_RGB_565 == null) { 330 rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB); 331 } 332 return rs.mElement_RGB_565; 333 } 334 335 public static Element RGB_888(RenderScript rs) { 336 if(rs.mElement_RGB_888 == null) { 337 rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB); 338 } 339 return rs.mElement_RGB_888; 340 } 341 342 public static Element RGBA_5551(RenderScript rs) { 343 if(rs.mElement_RGBA_5551 == null) { 344 rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA); 345 } 346 return rs.mElement_RGBA_5551; 347 } 348 349 public static Element RGBA_4444(RenderScript rs) { 350 if(rs.mElement_RGBA_4444 == null) { 351 rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA); 352 } 353 return rs.mElement_RGBA_4444; 354 } 355 356 public static Element RGBA_8888(RenderScript rs) { 357 if(rs.mElement_RGBA_8888 == null) { 358 rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA); 359 } 360 return rs.mElement_RGBA_8888; 361 } 362 363 public static Element F32_2(RenderScript rs) { 364 if(rs.mElement_FLOAT_2 == null) { 365 rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2); 366 } 367 return rs.mElement_FLOAT_2; 368 } 369 370 public static Element F32_3(RenderScript rs) { 371 if(rs.mElement_FLOAT_3 == null) { 372 rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3); 373 } 374 return rs.mElement_FLOAT_3; 375 } 376 377 public static Element F32_4(RenderScript rs) { 378 if(rs.mElement_FLOAT_4 == null) { 379 rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4); 380 } 381 return rs.mElement_FLOAT_4; 382 } 383 384 public static Element U8_4(RenderScript rs) { 385 if(rs.mElement_UCHAR_4 == null) { 386 rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4); 387 } 388 return rs.mElement_UCHAR_4; 389 } 390 391 public static Element MATRIX_4X4(RenderScript rs) { 392 if(rs.mElement_MATRIX_4X4 == null) { 393 rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4); 394 } 395 return rs.mElement_MATRIX_4X4; 396 } 397 public static Element MATRIX4X4(RenderScript rs) { 398 return MATRIX_4X4(rs); 399 } 400 401 public static Element MATRIX_3X3(RenderScript rs) { 402 if(rs.mElement_MATRIX_3X3 == null) { 403 rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3); 404 } 405 return rs.mElement_MATRIX_4X4; 406 } 407 408 public static Element MATRIX_2X2(RenderScript rs) { 409 if(rs.mElement_MATRIX_2X2 == null) { 410 rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2); 411 } 412 return rs.mElement_MATRIX_2X2; 413 } 414 415 Element(int id, RenderScript rs, Element[] e, String[] n, int[] as) { 416 super(id, rs); 417 mSize = 0; 418 mElements = e; 419 mElementNames = n; 420 mArraySizes = as; 421 for (int ct = 0; ct < mElements.length; ct++ ) { 422 mSize += mElements[ct].mSize * mArraySizes[ct]; 423 } 424 } 425 426 Element(int id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) { 427 super(id, rs); 428 mSize = dt.mSize * size; 429 mType = dt; 430 mKind = dk; 431 mNormalized = norm; 432 mVectorSize = size; 433 } 434 435 Element(int id, RenderScript rs) { 436 super(id, rs); 437 } 438 439 @Override 440 void updateFromNative() { 441 super.updateFromNative(); 442 443 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements 444 int[] dataBuffer = new int[5]; 445 mRS.nElementGetNativeData(getID(), dataBuffer); 446 447 mNormalized = dataBuffer[2] == 1 ? true : false; 448 mVectorSize = dataBuffer[3]; 449 mSize = 0; 450 for (DataType dt: DataType.values()) { 451 if(dt.mID == dataBuffer[0]){ 452 mType = dt; 453 mSize = mType.mSize * mVectorSize; 454 } 455 } 456 for (DataKind dk: DataKind.values()) { 457 if(dk.mID == dataBuffer[1]){ 458 mKind = dk; 459 } 460 } 461 462 int numSubElements = dataBuffer[4]; 463 if(numSubElements > 0) { 464 mElements = new Element[numSubElements]; 465 mElementNames = new String[numSubElements]; 466 467 int[] subElementIds = new int[numSubElements]; 468 mRS.nElementGetSubElements(getID(), subElementIds, mElementNames); 469 for(int i = 0; i < numSubElements; i ++) { 470 mElements[i] = new Element(subElementIds[i], mRS); 471 mElements[i].updateFromNative(); 472 mSize += mElements[i].mSize; 473 } 474 } 475 476 } 477 478 /** 479 * Create a custom Element of the specified DataType. The DataKind will be 480 * set to USER and the vector size to 1 indicating non-vector. 481 * 482 * @param rs The context associated with the new Element. 483 * @param dt The DataType for the new element. 484 * @return Element 485 */ 486 static Element createUser(RenderScript rs, DataType dt) { 487 DataKind dk = DataKind.USER; 488 boolean norm = false; 489 int vecSize = 1; 490 int id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize); 491 return new Element(id, rs, dt, dk, norm, vecSize); 492 } 493 494 /** 495 * Create a custom vector element of the specified DataType and vector size. 496 * DataKind will be set to USER. 497 * 498 * @param rs The context associated with the new Element. 499 * @param dt The DataType for the new element. 500 * @param size Vector size for the new Element. Range 2-4 inclusive 501 * supported. 502 * 503 * @return Element 504 */ 505 public static Element createVector(RenderScript rs, DataType dt, int size) { 506 if (size < 2 || size > 4) { 507 throw new RSIllegalArgumentException("Vector size out of range 2-4."); 508 } 509 DataKind dk = DataKind.USER; 510 boolean norm = false; 511 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size); 512 return new Element(id, rs, dt, dk, norm, size); 513 } 514 515 /** 516 * Create a new pixel Element type. A matching DataType and DataKind must 517 * be provided. The DataType and DataKind must contain the same number of 518 * components. Vector size will be set to 1. 519 * 520 * @param rs The context associated with the new Element. 521 * @param dt The DataType for the new element. 522 * @param dk The DataKind to specify the mapping of each component in the 523 * DataType. 524 * 525 * @return Element 526 */ 527 public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) { 528 if (!(dk == DataKind.PIXEL_L || 529 dk == DataKind.PIXEL_A || 530 dk == DataKind.PIXEL_LA || 531 dk == DataKind.PIXEL_RGB || 532 dk == DataKind.PIXEL_RGBA)) { 533 throw new RSIllegalArgumentException("Unsupported DataKind"); 534 } 535 if (!(dt == DataType.UNSIGNED_8 || 536 dt == DataType.UNSIGNED_5_6_5 || 537 dt == DataType.UNSIGNED_4_4_4_4 || 538 dt == DataType.UNSIGNED_5_5_5_1)) { 539 throw new RSIllegalArgumentException("Unsupported DataType"); 540 } 541 if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) { 542 throw new RSIllegalArgumentException("Bad kind and type combo"); 543 } 544 if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) { 545 throw new RSIllegalArgumentException("Bad kind and type combo"); 546 } 547 if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) { 548 throw new RSIllegalArgumentException("Bad kind and type combo"); 549 } 550 551 int size = 1; 552 if (dk == DataKind.PIXEL_LA) { 553 size = 2; 554 } 555 if (dk == DataKind.PIXEL_RGB) { 556 size = 3; 557 } 558 if (dk == DataKind.PIXEL_RGBA) { 559 size = 4; 560 } 561 562 boolean norm = true; 563 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size); 564 return new Element(id, rs, dt, dk, norm, size); 565 } 566 567 /** 568 * Builder class for producing complex elements with matching field and name 569 * pairs. The builder starts empty. The order in which elements are added 570 * is retained for the layout in memory. 571 * 572 */ 573 public static class Builder { 574 RenderScript mRS; 575 Element[] mElements; 576 String[] mElementNames; 577 int[] mArraySizes; 578 int mCount; 579 580 /** 581 * Create a builder object. 582 * 583 * @param rs 584 */ 585 public Builder(RenderScript rs) { 586 mRS = rs; 587 mCount = 0; 588 mElements = new Element[8]; 589 mElementNames = new String[8]; 590 mArraySizes = new int[8]; 591 } 592 593 /** 594 * Add an array of elements to this element. 595 * 596 * @param element 597 * @param name 598 * @param arraySize 599 */ 600 public Builder add(Element element, String name, int arraySize) { 601 if (arraySize < 1) { 602 throw new RSIllegalArgumentException("Array size cannot be less than 1."); 603 } 604 if(mCount == mElements.length) { 605 Element[] e = new Element[mCount + 8]; 606 String[] s = new String[mCount + 8]; 607 int[] as = new int[mCount + 8]; 608 System.arraycopy(mElements, 0, e, 0, mCount); 609 System.arraycopy(mElementNames, 0, s, 0, mCount); 610 System.arraycopy(mArraySizes, 0, as, 0, mCount); 611 mElements = e; 612 mElementNames = s; 613 mArraySizes = as; 614 } 615 mElements[mCount] = element; 616 mElementNames[mCount] = name; 617 mArraySizes[mCount] = arraySize; 618 mCount++; 619 return this; 620 } 621 622 /** 623 * Add a single element to this Element. 624 * 625 * @param element 626 * @param name 627 */ 628 public Builder add(Element element, String name) { 629 return add(element, name, 1); 630 } 631 632 /** 633 * Create the element from this builder. 634 * 635 * 636 * @return Element 637 */ 638 public Element create() { 639 mRS.validate(); 640 Element[] ein = new Element[mCount]; 641 String[] sin = new String[mCount]; 642 int[] asin = new int[mCount]; 643 java.lang.System.arraycopy(mElements, 0, ein, 0, mCount); 644 java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount); 645 java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount); 646 647 int[] ids = new int[ein.length]; 648 for (int ct = 0; ct < ein.length; ct++ ) { 649 ids[ct] = ein[ct].getID(); 650 } 651 int id = mRS.nElementCreate2(ids, sin, asin); 652 return new Element(id, mRS, ein, sin, asin); 653 } 654 } 655} 656 657