Element.java revision a5835a2e5fdb98fdf03a223ddf7e082ba1020807
1/* 2 * Copyright (C) 2013 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 19/** 20 * <p>An Element represents one item within an {@link 21 * android.renderscript.Allocation}. An Element is roughly equivalent to a C 22 * type in a RenderScript kernel. Elements may be basic or complex. Some basic 23 * elements are</p> <ul> <li>A single float value (equivalent to a float in a 24 * kernel)</li> <li>A four-element float vector (equivalent to a float4 in a 25 * kernel)</li> <li>An unsigned 32-bit integer (equivalent to an unsigned int in 26 * a kernel)</li> <li>A single signed 8-bit integer (equivalent to a char in a 27 * kernel)</li> </ul> <p>A complex element is roughly equivalent to a C struct 28 * and contains a number of basic or complex Elements. From Java code, a complex 29 * element contains a list of sub-elements and names that represents a 30 * particular data structure. Structs used in RS scripts are available to Java 31 * code by using the {@code ScriptField_structname} class that is reflected from 32 * a particular script.</p> 33 * 34 * <p>Basic Elements are comprised of a {@link 35 * android.renderscript.Element.DataType} and a {@link 36 * android.renderscript.Element.DataKind}. The DataType encodes C type 37 * information of an Element, while the DataKind encodes how that Element should 38 * be interpreted by a {@link android.renderscript.Sampler}. Note that {@link 39 * android.renderscript.Allocation} objects with DataKind {@link 40 * android.renderscript.Element.DataKind#USER} cannot be used as input for a 41 * {@link android.renderscript.Sampler}. In general, {@link 42 * android.renderscript.Allocation} objects that are intended for use with a 43 * {@link android.renderscript.Sampler} should use bitmap-derived Elements such 44 * as {@link android.renderscript.Element#RGBA_8888} or {@link 45 * android.renderscript#Element.A_8}.</p> 46 * 47 * <div class="special reference"> 48 * <h3>Developer Guides</h3> 49 * <p>For more information about creating an application that uses RenderScript, read the 50 * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p> 51 * </div> 52 **/ 53public class Element extends BaseObj { 54 int mSize; 55 Element[] mElements; 56 String[] mElementNames; 57 int[] mArraySizes; 58 int[] mOffsetInBytes; 59 60 int[] mVisibleElementMap; 61 62 DataType mType; 63 DataKind mKind; 64 boolean mNormalized; 65 int mVectorSize; 66 67 private void updateVisibleSubElements() { 68 if (mElements == null) { 69 return; 70 } 71 72 int noPaddingFieldCount = 0; 73 int fieldCount = mElementNames.length; 74 // Find out how many elements are not padding 75 for (int ct = 0; ct < fieldCount; ct ++) { 76 if (mElementNames[ct].charAt(0) != '#') { 77 noPaddingFieldCount ++; 78 } 79 } 80 mVisibleElementMap = new int[noPaddingFieldCount]; 81 82 // Make a map that points us at non-padding elements 83 for (int ct = 0, ctNoPadding = 0; ct < fieldCount; ct ++) { 84 if (mElementNames[ct].charAt(0) != '#') { 85 mVisibleElementMap[ctNoPadding ++] = ct; 86 } 87 } 88 } 89 90 /** 91 * @return element size in bytes 92 */ 93 public int getBytesSize() {return mSize;} 94 95 /** 96 * Returns the number of vector components. 2 for float2, 4 for 97 * float4, etc. 98 * @return element vector size 99 */ 100 public int getVectorSize() {return mVectorSize;} 101 102 103 /** 104 * DataType represents the basic type information for a basic element. The 105 * naming convention follows. For numeric types it is FLOAT, 106 * SIGNED, or UNSIGNED followed by the _BITS where BITS is the 107 * size of the data. BOOLEAN is a true / false (1,0) 108 * represented in an 8 bit container. The UNSIGNED variants 109 * with multiple bit definitions are for packed graphical data 110 * formats and represent vectors with per vector member sizes 111 * which are treated as a single unit for packing and alignment 112 * purposes. 113 * 114 * MATRIX the three matrix types contain FLOAT_32 elements and are treated 115 * as 32 bits for alignment purposes. 116 * 117 * RS_* objects. 32 bit opaque handles. 118 */ 119 public enum DataType { 120 NONE (0, 0), 121 /** 122 * @hide 123 */ 124 FLOAT_16 (1, 2), 125 FLOAT_32 (2, 4), 126 FLOAT_64 (3, 8), 127 SIGNED_8 (4, 1), 128 SIGNED_16 (5, 2), 129 SIGNED_32 (6, 4), 130 SIGNED_64 (7, 8), 131 UNSIGNED_8 (8, 1), 132 UNSIGNED_16 (9, 2), 133 UNSIGNED_32 (10, 4), 134 UNSIGNED_64 (11, 8), 135 136 BOOLEAN(12, 1), 137 138 UNSIGNED_5_6_5 (13, 2), 139 UNSIGNED_5_5_5_1 (14, 2), 140 UNSIGNED_4_4_4_4 (15, 2), 141 142 MATRIX_4X4 (16, 64), 143 MATRIX_3X3 (17, 36), 144 MATRIX_2X2 (18, 16), 145 146 RS_ELEMENT (1000), 147 RS_TYPE (1001), 148 RS_ALLOCATION (1002), 149 RS_SAMPLER (1003), 150 RS_SCRIPT (1004), 151 RS_MESH (1005), 152 RS_PROGRAM_FRAGMENT (1006), 153 RS_PROGRAM_VERTEX (1007), 154 RS_PROGRAM_RASTER (1008), 155 RS_PROGRAM_STORE (1009), 156 RS_FONT (1010); 157 158 int mID; 159 int mSize; 160 DataType(int id, int size) { 161 mID = id; 162 mSize = size; 163 } 164 165 DataType(int id) { 166 mID = id; 167 mSize = 4; 168 if (RenderScript.sPointerSize == 8) { 169 mSize = 32; 170 } 171 } 172 } 173 174 /** 175 * The special interpretation of the data if required. This is primarly 176 * useful for graphical data. USER indicates no special interpretation is 177 * expected. PIXEL is used in conjunction with the standard data types for 178 * representing texture formats. 179 */ 180 public enum DataKind { 181 USER (0), 182 183 PIXEL_L (7), 184 PIXEL_A (8), 185 PIXEL_LA (9), 186 PIXEL_RGB (10), 187 PIXEL_RGBA (11), 188 PIXEL_DEPTH (12), 189 PIXEL_YUV(13); 190 191 int mID; 192 DataKind(int id) { 193 mID = id; 194 } 195 } 196 197 /** 198 * Return if a element is too complex for use as a data source for a Mesh or 199 * a Program. 200 * 201 * @return boolean 202 */ 203 public boolean isComplex() { 204 if (mElements == null) { 205 return false; 206 } 207 for (int ct=0; ct < mElements.length; ct++) { 208 if (mElements[ct].mElements != null) { 209 return true; 210 } 211 } 212 return false; 213 } 214 215 /** 216 * Elements could be simple, such as an int or a float, or a 217 * structure with multiple sub elements, such as a collection of 218 * floats, float2, float4. This function returns zero for simple 219 * elements or the number of sub-elements otherwise. 220 * @return number of sub-elements in this element 221 */ 222 public int getSubElementCount() { 223 if (mVisibleElementMap == null) { 224 return 0; 225 } 226 return mVisibleElementMap.length; 227 } 228 229 /** 230 * For complex elements, this function will return the 231 * sub-element at index 232 * @param index index of the sub-element to return 233 * @return sub-element in this element at given index 234 */ 235 public Element getSubElement(int index) { 236 if (mVisibleElementMap == null) { 237 throw new RSIllegalArgumentException("Element contains no sub-elements"); 238 } 239 if (index < 0 || index >= mVisibleElementMap.length) { 240 throw new RSIllegalArgumentException("Illegal sub-element index"); 241 } 242 return mElements[mVisibleElementMap[index]]; 243 } 244 245 /** 246 * For complex elements, this function will return the 247 * sub-element name at index 248 * @param index index of the sub-element 249 * @return sub-element in this element at given index 250 */ 251 public String getSubElementName(int index) { 252 if (mVisibleElementMap == null) { 253 throw new RSIllegalArgumentException("Element contains no sub-elements"); 254 } 255 if (index < 0 || index >= mVisibleElementMap.length) { 256 throw new RSIllegalArgumentException("Illegal sub-element index"); 257 } 258 return mElementNames[mVisibleElementMap[index]]; 259 } 260 261 /** 262 * For complex elements, some sub-elements could be statically 263 * sized arrays. This function will return the array size for 264 * sub-element at index 265 * @param index index of the sub-element 266 * @return array size of sub-element in this element at given index 267 */ 268 public int getSubElementArraySize(int index) { 269 if (mVisibleElementMap == null) { 270 throw new RSIllegalArgumentException("Element contains no sub-elements"); 271 } 272 if (index < 0 || index >= mVisibleElementMap.length) { 273 throw new RSIllegalArgumentException("Illegal sub-element index"); 274 } 275 return mArraySizes[mVisibleElementMap[index]]; 276 } 277 278 /** 279 * This function specifies the location of a sub-element within 280 * the element 281 * @param index index of the sub-element 282 * @return offset in bytes of sub-element in this element at given index 283 */ 284 public int getSubElementOffsetBytes(int index) { 285 if (mVisibleElementMap == null) { 286 throw new RSIllegalArgumentException("Element contains no sub-elements"); 287 } 288 if (index < 0 || index >= mVisibleElementMap.length) { 289 throw new RSIllegalArgumentException("Illegal sub-element index"); 290 } 291 return mOffsetInBytes[mVisibleElementMap[index]]; 292 } 293 294 /** 295 * @return element data type 296 */ 297 public DataType getDataType() { 298 return mType; 299 } 300 301 /** 302 * @return element data kind 303 */ 304 public DataKind getDataKind() { 305 return mKind; 306 } 307 308 /** 309 * Utility function for returning an Element containing a single Boolean. 310 * 311 * @param rs Context to which the element will belong. 312 * 313 * @return Element 314 */ 315 public static Element BOOLEAN(RenderScript rs) { 316 if(rs.mElement_BOOLEAN == null) { 317 rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN); 318 } 319 return rs.mElement_BOOLEAN; 320 } 321 322 /** 323 * Utility function for returning an Element containing a single UNSIGNED_8. 324 * 325 * @param rs Context to which the element will belong. 326 * 327 * @return Element 328 */ 329 public static Element U8(RenderScript rs) { 330 if(rs.mElement_U8 == null) { 331 rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8); 332 } 333 return rs.mElement_U8; 334 } 335 336 /** 337 * Utility function for returning an Element containing a single SIGNED_8. 338 * 339 * @param rs Context to which the element will belong. 340 * 341 * @return Element 342 */ 343 public static Element I8(RenderScript rs) { 344 if(rs.mElement_I8 == null) { 345 rs.mElement_I8 = createUser(rs, DataType.SIGNED_8); 346 } 347 return rs.mElement_I8; 348 } 349 350 public static Element U16(RenderScript rs) { 351 if(rs.mElement_U16 == null) { 352 rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16); 353 } 354 return rs.mElement_U16; 355 } 356 357 public static Element I16(RenderScript rs) { 358 if(rs.mElement_I16 == null) { 359 rs.mElement_I16 = createUser(rs, DataType.SIGNED_16); 360 } 361 return rs.mElement_I16; 362 } 363 364 public static Element U32(RenderScript rs) { 365 if(rs.mElement_U32 == null) { 366 rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32); 367 } 368 return rs.mElement_U32; 369 } 370 371 public static Element I32(RenderScript rs) { 372 if(rs.mElement_I32 == null) { 373 rs.mElement_I32 = createUser(rs, DataType.SIGNED_32); 374 } 375 return rs.mElement_I32; 376 } 377 378 public static Element U64(RenderScript rs) { 379 if(rs.mElement_U64 == null) { 380 rs.mElement_U64 = createUser(rs, DataType.UNSIGNED_64); 381 } 382 return rs.mElement_U64; 383 } 384 385 public static Element I64(RenderScript rs) { 386 if(rs.mElement_I64 == null) { 387 rs.mElement_I64 = createUser(rs, DataType.SIGNED_64); 388 } 389 return rs.mElement_I64; 390 } 391 392 /** 393 * @hide 394 */ 395 public static Element F16(RenderScript rs) { 396 if(rs.mElement_F16 == null) { 397 rs.mElement_F16 = createUser(rs, DataType.FLOAT_16); 398 } 399 return rs.mElement_F16; 400 } 401 402 public static Element F32(RenderScript rs) { 403 if(rs.mElement_F32 == null) { 404 rs.mElement_F32 = createUser(rs, DataType.FLOAT_32); 405 } 406 return rs.mElement_F32; 407 } 408 409 public static Element F64(RenderScript rs) { 410 if(rs.mElement_F64 == null) { 411 rs.mElement_F64 = createUser(rs, DataType.FLOAT_64); 412 } 413 return rs.mElement_F64; 414 } 415 416 public static Element ELEMENT(RenderScript rs) { 417 if(rs.mElement_ELEMENT == null) { 418 rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT); 419 } 420 return rs.mElement_ELEMENT; 421 } 422 423 public static Element TYPE(RenderScript rs) { 424 if(rs.mElement_TYPE == null) { 425 rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE); 426 } 427 return rs.mElement_TYPE; 428 } 429 430 public static Element ALLOCATION(RenderScript rs) { 431 if(rs.mElement_ALLOCATION == null) { 432 rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION); 433 } 434 return rs.mElement_ALLOCATION; 435 } 436 437 public static Element SAMPLER(RenderScript rs) { 438 if(rs.mElement_SAMPLER == null) { 439 rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER); 440 } 441 return rs.mElement_SAMPLER; 442 } 443 444 public static Element SCRIPT(RenderScript rs) { 445 if(rs.mElement_SCRIPT == null) { 446 rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT); 447 } 448 return rs.mElement_SCRIPT; 449 } 450 451 public static Element MESH(RenderScript rs) { 452 if(rs.mElement_MESH == null) { 453 rs.mElement_MESH = createUser(rs, DataType.RS_MESH); 454 } 455 return rs.mElement_MESH; 456 } 457 458 public static Element PROGRAM_FRAGMENT(RenderScript rs) { 459 if(rs.mElement_PROGRAM_FRAGMENT == null) { 460 rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT); 461 } 462 return rs.mElement_PROGRAM_FRAGMENT; 463 } 464 465 public static Element PROGRAM_VERTEX(RenderScript rs) { 466 if(rs.mElement_PROGRAM_VERTEX == null) { 467 rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX); 468 } 469 return rs.mElement_PROGRAM_VERTEX; 470 } 471 472 public static Element PROGRAM_RASTER(RenderScript rs) { 473 if(rs.mElement_PROGRAM_RASTER == null) { 474 rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER); 475 } 476 return rs.mElement_PROGRAM_RASTER; 477 } 478 479 public static Element PROGRAM_STORE(RenderScript rs) { 480 if(rs.mElement_PROGRAM_STORE == null) { 481 rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE); 482 } 483 return rs.mElement_PROGRAM_STORE; 484 } 485 486 public static Element FONT(RenderScript rs) { 487 if(rs.mElement_FONT == null) { 488 rs.mElement_FONT = createUser(rs, DataType.RS_FONT); 489 } 490 return rs.mElement_FONT; 491 } 492 493 494 public static Element A_8(RenderScript rs) { 495 if(rs.mElement_A_8 == null) { 496 rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A); 497 } 498 return rs.mElement_A_8; 499 } 500 501 public static Element RGB_565(RenderScript rs) { 502 if(rs.mElement_RGB_565 == null) { 503 rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB); 504 } 505 return rs.mElement_RGB_565; 506 } 507 508 public static Element RGB_888(RenderScript rs) { 509 if(rs.mElement_RGB_888 == null) { 510 rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB); 511 } 512 return rs.mElement_RGB_888; 513 } 514 515 public static Element RGBA_5551(RenderScript rs) { 516 if(rs.mElement_RGBA_5551 == null) { 517 rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA); 518 } 519 return rs.mElement_RGBA_5551; 520 } 521 522 public static Element RGBA_4444(RenderScript rs) { 523 if(rs.mElement_RGBA_4444 == null) { 524 rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA); 525 } 526 return rs.mElement_RGBA_4444; 527 } 528 529 public static Element RGBA_8888(RenderScript rs) { 530 if(rs.mElement_RGBA_8888 == null) { 531 rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA); 532 } 533 return rs.mElement_RGBA_8888; 534 } 535 536 /** 537 * @hide 538 */ 539 public static Element F16_2(RenderScript rs) { 540 if(rs.mElement_HALF_2 == null) { 541 rs.mElement_HALF_2 = createVector(rs, DataType.FLOAT_16, 2); 542 } 543 return rs.mElement_HALF_2; 544 } 545 546 /** 547 * @hide 548 */ 549 public static Element F16_3(RenderScript rs) { 550 if(rs.mElement_FLOAT_3 == null) { 551 rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_16, 3); 552 } 553 return rs.mElement_HALF_3; 554 } 555 556 /** 557 * @hide 558 */ 559 public static Element F16_4(RenderScript rs) { 560 if(rs.mElement_HALF_4 == null) { 561 rs.mElement_HALF_4 = createVector(rs, DataType.FLOAT_16, 4); 562 } 563 return rs.mElement_HALF_4; 564 } 565 566 public static Element F32_2(RenderScript rs) { 567 if(rs.mElement_FLOAT_2 == null) { 568 rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2); 569 } 570 return rs.mElement_FLOAT_2; 571 } 572 573 public static Element F32_3(RenderScript rs) { 574 if(rs.mElement_FLOAT_3 == null) { 575 rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3); 576 } 577 return rs.mElement_FLOAT_3; 578 } 579 580 public static Element F32_4(RenderScript rs) { 581 if(rs.mElement_FLOAT_4 == null) { 582 rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4); 583 } 584 return rs.mElement_FLOAT_4; 585 } 586 587 public static Element F64_2(RenderScript rs) { 588 if(rs.mElement_DOUBLE_2 == null) { 589 rs.mElement_DOUBLE_2 = createVector(rs, DataType.FLOAT_64, 2); 590 } 591 return rs.mElement_DOUBLE_2; 592 } 593 594 public static Element F64_3(RenderScript rs) { 595 if(rs.mElement_DOUBLE_3 == null) { 596 rs.mElement_DOUBLE_3 = createVector(rs, DataType.FLOAT_64, 3); 597 } 598 return rs.mElement_DOUBLE_3; 599 } 600 601 public static Element F64_4(RenderScript rs) { 602 if(rs.mElement_DOUBLE_4 == null) { 603 rs.mElement_DOUBLE_4 = createVector(rs, DataType.FLOAT_64, 4); 604 } 605 return rs.mElement_DOUBLE_4; 606 } 607 608 public static Element U8_2(RenderScript rs) { 609 if(rs.mElement_UCHAR_2 == null) { 610 rs.mElement_UCHAR_2 = createVector(rs, DataType.UNSIGNED_8, 2); 611 } 612 return rs.mElement_UCHAR_2; 613 } 614 615 public static Element U8_3(RenderScript rs) { 616 if(rs.mElement_UCHAR_3 == null) { 617 rs.mElement_UCHAR_3 = createVector(rs, DataType.UNSIGNED_8, 3); 618 } 619 return rs.mElement_UCHAR_3; 620 } 621 622 public static Element U8_4(RenderScript rs) { 623 if(rs.mElement_UCHAR_4 == null) { 624 rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4); 625 } 626 return rs.mElement_UCHAR_4; 627 } 628 629 public static Element I8_2(RenderScript rs) { 630 if(rs.mElement_CHAR_2 == null) { 631 rs.mElement_CHAR_2 = createVector(rs, DataType.SIGNED_8, 2); 632 } 633 return rs.mElement_CHAR_2; 634 } 635 636 public static Element I8_3(RenderScript rs) { 637 if(rs.mElement_CHAR_3 == null) { 638 rs.mElement_CHAR_3 = createVector(rs, DataType.SIGNED_8, 3); 639 } 640 return rs.mElement_CHAR_3; 641 } 642 643 public static Element I8_4(RenderScript rs) { 644 if(rs.mElement_CHAR_4 == null) { 645 rs.mElement_CHAR_4 = createVector(rs, DataType.SIGNED_8, 4); 646 } 647 return rs.mElement_CHAR_4; 648 } 649 650 public static Element U16_2(RenderScript rs) { 651 if(rs.mElement_USHORT_2 == null) { 652 rs.mElement_USHORT_2 = createVector(rs, DataType.UNSIGNED_16, 2); 653 } 654 return rs.mElement_USHORT_2; 655 } 656 657 public static Element U16_3(RenderScript rs) { 658 if(rs.mElement_USHORT_3 == null) { 659 rs.mElement_USHORT_3 = createVector(rs, DataType.UNSIGNED_16, 3); 660 } 661 return rs.mElement_USHORT_3; 662 } 663 664 public static Element U16_4(RenderScript rs) { 665 if(rs.mElement_USHORT_4 == null) { 666 rs.mElement_USHORT_4 = createVector(rs, DataType.UNSIGNED_16, 4); 667 } 668 return rs.mElement_USHORT_4; 669 } 670 671 public static Element I16_2(RenderScript rs) { 672 if(rs.mElement_SHORT_2 == null) { 673 rs.mElement_SHORT_2 = createVector(rs, DataType.SIGNED_16, 2); 674 } 675 return rs.mElement_SHORT_2; 676 } 677 678 public static Element I16_3(RenderScript rs) { 679 if(rs.mElement_SHORT_3 == null) { 680 rs.mElement_SHORT_3 = createVector(rs, DataType.SIGNED_16, 3); 681 } 682 return rs.mElement_SHORT_3; 683 } 684 685 public static Element I16_4(RenderScript rs) { 686 if(rs.mElement_SHORT_4 == null) { 687 rs.mElement_SHORT_4 = createVector(rs, DataType.SIGNED_16, 4); 688 } 689 return rs.mElement_SHORT_4; 690 } 691 692 public static Element U32_2(RenderScript rs) { 693 if(rs.mElement_UINT_2 == null) { 694 rs.mElement_UINT_2 = createVector(rs, DataType.UNSIGNED_32, 2); 695 } 696 return rs.mElement_UINT_2; 697 } 698 699 public static Element U32_3(RenderScript rs) { 700 if(rs.mElement_UINT_3 == null) { 701 rs.mElement_UINT_3 = createVector(rs, DataType.UNSIGNED_32, 3); 702 } 703 return rs.mElement_UINT_3; 704 } 705 706 public static Element U32_4(RenderScript rs) { 707 if(rs.mElement_UINT_4 == null) { 708 rs.mElement_UINT_4 = createVector(rs, DataType.UNSIGNED_32, 4); 709 } 710 return rs.mElement_UINT_4; 711 } 712 713 public static Element I32_2(RenderScript rs) { 714 if(rs.mElement_INT_2 == null) { 715 rs.mElement_INT_2 = createVector(rs, DataType.SIGNED_32, 2); 716 } 717 return rs.mElement_INT_2; 718 } 719 720 public static Element I32_3(RenderScript rs) { 721 if(rs.mElement_INT_3 == null) { 722 rs.mElement_INT_3 = createVector(rs, DataType.SIGNED_32, 3); 723 } 724 return rs.mElement_INT_3; 725 } 726 727 public static Element I32_4(RenderScript rs) { 728 if(rs.mElement_INT_4 == null) { 729 rs.mElement_INT_4 = createVector(rs, DataType.SIGNED_32, 4); 730 } 731 return rs.mElement_INT_4; 732 } 733 734 public static Element U64_2(RenderScript rs) { 735 if(rs.mElement_ULONG_2 == null) { 736 rs.mElement_ULONG_2 = createVector(rs, DataType.UNSIGNED_64, 2); 737 } 738 return rs.mElement_ULONG_2; 739 } 740 741 public static Element U64_3(RenderScript rs) { 742 if(rs.mElement_ULONG_3 == null) { 743 rs.mElement_ULONG_3 = createVector(rs, DataType.UNSIGNED_64, 3); 744 } 745 return rs.mElement_ULONG_3; 746 } 747 748 public static Element U64_4(RenderScript rs) { 749 if(rs.mElement_ULONG_4 == null) { 750 rs.mElement_ULONG_4 = createVector(rs, DataType.UNSIGNED_64, 4); 751 } 752 return rs.mElement_ULONG_4; 753 } 754 755 public static Element I64_2(RenderScript rs) { 756 if(rs.mElement_LONG_2 == null) { 757 rs.mElement_LONG_2 = createVector(rs, DataType.SIGNED_64, 2); 758 } 759 return rs.mElement_LONG_2; 760 } 761 762 public static Element I64_3(RenderScript rs) { 763 if(rs.mElement_LONG_3 == null) { 764 rs.mElement_LONG_3 = createVector(rs, DataType.SIGNED_64, 3); 765 } 766 return rs.mElement_LONG_3; 767 } 768 769 public static Element I64_4(RenderScript rs) { 770 if(rs.mElement_LONG_4 == null) { 771 rs.mElement_LONG_4 = createVector(rs, DataType.SIGNED_64, 4); 772 } 773 return rs.mElement_LONG_4; 774 } 775 776 public static Element YUV(RenderScript rs) { 777 if (rs.mElement_YUV == null) { 778 rs.mElement_YUV = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_YUV); 779 } 780 return rs.mElement_YUV; 781 } 782 783 public static Element MATRIX_4X4(RenderScript rs) { 784 if(rs.mElement_MATRIX_4X4 == null) { 785 rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4); 786 } 787 return rs.mElement_MATRIX_4X4; 788 } 789 790 /** @deprecated use MATRIX_4X4 791 */ 792 public static Element MATRIX4X4(RenderScript rs) { 793 return MATRIX_4X4(rs); 794 } 795 796 public static Element MATRIX_3X3(RenderScript rs) { 797 if(rs.mElement_MATRIX_3X3 == null) { 798 rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3); 799 } 800 return rs.mElement_MATRIX_3X3; 801 } 802 803 public static Element MATRIX_2X2(RenderScript rs) { 804 if(rs.mElement_MATRIX_2X2 == null) { 805 rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2); 806 } 807 return rs.mElement_MATRIX_2X2; 808 } 809 810 Element(long id, RenderScript rs, Element[] e, String[] n, int[] as) { 811 super(id, rs); 812 mSize = 0; 813 mVectorSize = 1; 814 mElements = e; 815 mElementNames = n; 816 mArraySizes = as; 817 mType = DataType.NONE; 818 mKind = DataKind.USER; 819 mOffsetInBytes = new int[mElements.length]; 820 for (int ct = 0; ct < mElements.length; ct++ ) { 821 mOffsetInBytes[ct] = mSize; 822 mSize += mElements[ct].mSize * mArraySizes[ct]; 823 } 824 updateVisibleSubElements(); 825 } 826 827 Element(long id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) { 828 super(id, rs); 829 if ((dt != DataType.UNSIGNED_5_6_5) && 830 (dt != DataType.UNSIGNED_4_4_4_4) && 831 (dt != DataType.UNSIGNED_5_5_5_1)) { 832 if (size == 3) { 833 mSize = dt.mSize * 4; 834 } else { 835 mSize = dt.mSize * size; 836 } 837 } else { 838 mSize = dt.mSize; 839 } 840 mType = dt; 841 mKind = dk; 842 mNormalized = norm; 843 mVectorSize = size; 844 } 845 846 Element(long id, RenderScript rs) { 847 super(id, rs); 848 } 849 850 @Override 851 void updateFromNative() { 852 super.updateFromNative(); 853 854 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements 855 int[] dataBuffer = new int[5]; 856 mRS.nElementGetNativeData(getID(mRS), dataBuffer); 857 858 mNormalized = dataBuffer[2] == 1 ? true : false; 859 mVectorSize = dataBuffer[3]; 860 mSize = 0; 861 for (DataType dt: DataType.values()) { 862 if(dt.mID == dataBuffer[0]){ 863 mType = dt; 864 mSize = mType.mSize * mVectorSize; 865 } 866 } 867 for (DataKind dk: DataKind.values()) { 868 if(dk.mID == dataBuffer[1]){ 869 mKind = dk; 870 } 871 } 872 873 int numSubElements = dataBuffer[4]; 874 if(numSubElements > 0) { 875 mElements = new Element[numSubElements]; 876 mElementNames = new String[numSubElements]; 877 mArraySizes = new int[numSubElements]; 878 mOffsetInBytes = new int[numSubElements]; 879 880 long[] subElementIds = new long[numSubElements]; 881 mRS.nElementGetSubElements(getID(mRS), subElementIds, mElementNames, mArraySizes); 882 for(int i = 0; i < numSubElements; i ++) { 883 mElements[i] = new Element(subElementIds[i], mRS); 884 mElements[i].updateFromNative(); 885 mOffsetInBytes[i] = mSize; 886 mSize += mElements[i].mSize * mArraySizes[i]; 887 } 888 } 889 updateVisibleSubElements(); 890 } 891 892 /** 893 * Create a custom Element of the specified DataType. The DataKind will be 894 * set to USER and the vector size to 1 indicating non-vector. 895 * 896 * @param rs The context associated with the new Element. 897 * @param dt The DataType for the new element. 898 * @return Element 899 */ 900 static Element createUser(RenderScript rs, DataType dt) { 901 DataKind dk = DataKind.USER; 902 boolean norm = false; 903 int vecSize = 1; 904 long id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize); 905 return new Element(id, rs, dt, dk, norm, vecSize); 906 } 907 908 /** 909 * Create a custom vector element of the specified DataType and vector size. 910 * DataKind will be set to USER. Only primitive types (FLOAT_32, FLOAT_64, 911 * SIGNED_8, SIGNED_16, SIGNED_32, SIGNED_64, UNSIGNED_8, UNSIGNED_16, 912 * UNSIGNED_32, UNSIGNED_64, BOOLEAN) are supported. 913 * 914 * @param rs The context associated with the new Element. 915 * @param dt The DataType for the new Element. 916 * @param size Vector size for the new Element. Range 2-4 inclusive 917 * supported. 918 * 919 * @return Element 920 */ 921 public static Element createVector(RenderScript rs, DataType dt, int size) { 922 if (size < 2 || size > 4) { 923 throw new RSIllegalArgumentException("Vector size out of range 2-4."); 924 } 925 926 switch (dt) { 927 // Support only primitive integer/float/boolean types as vectors. 928 case FLOAT_32: 929 case FLOAT_64: 930 case SIGNED_8: 931 case SIGNED_16: 932 case SIGNED_32: 933 case SIGNED_64: 934 case UNSIGNED_8: 935 case UNSIGNED_16: 936 case UNSIGNED_32: 937 case UNSIGNED_64: 938 case BOOLEAN: { 939 DataKind dk = DataKind.USER; 940 boolean norm = false; 941 long id = rs.nElementCreate(dt.mID, dk.mID, norm, size); 942 return new Element(id, rs, dt, dk, norm, size); 943 } 944 945 default: { 946 throw new RSIllegalArgumentException("Cannot create vector of " + 947 "non-primitive type."); 948 } 949 } 950 } 951 952 /** 953 * Create a new pixel Element type. A matching DataType and DataKind must 954 * be provided. The DataType and DataKind must contain the same number of 955 * components. Vector size will be set to 1. 956 * 957 * @param rs The context associated with the new Element. 958 * @param dt The DataType for the new element. 959 * @param dk The DataKind to specify the mapping of each component in the 960 * DataType. 961 * 962 * @return Element 963 */ 964 public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) { 965 if (!(dk == DataKind.PIXEL_L || 966 dk == DataKind.PIXEL_A || 967 dk == DataKind.PIXEL_LA || 968 dk == DataKind.PIXEL_RGB || 969 dk == DataKind.PIXEL_RGBA || 970 dk == DataKind.PIXEL_DEPTH || 971 dk == DataKind.PIXEL_YUV)) { 972 throw new RSIllegalArgumentException("Unsupported DataKind"); 973 } 974 if (!(dt == DataType.UNSIGNED_8 || 975 dt == DataType.UNSIGNED_16 || 976 dt == DataType.UNSIGNED_5_6_5 || 977 dt == DataType.UNSIGNED_4_4_4_4 || 978 dt == DataType.UNSIGNED_5_5_5_1)) { 979 throw new RSIllegalArgumentException("Unsupported DataType"); 980 } 981 if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) { 982 throw new RSIllegalArgumentException("Bad kind and type combo"); 983 } 984 if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) { 985 throw new RSIllegalArgumentException("Bad kind and type combo"); 986 } 987 if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) { 988 throw new RSIllegalArgumentException("Bad kind and type combo"); 989 } 990 if (dt == DataType.UNSIGNED_16 && 991 dk != DataKind.PIXEL_DEPTH) { 992 throw new RSIllegalArgumentException("Bad kind and type combo"); 993 } 994 995 int size = 1; 996 switch (dk) { 997 case PIXEL_LA: 998 size = 2; 999 break; 1000 case PIXEL_RGB: 1001 size = 3; 1002 break; 1003 case PIXEL_RGBA: 1004 size = 4; 1005 break; 1006 case PIXEL_DEPTH: 1007 size = 2; 1008 break; 1009 } 1010 1011 boolean norm = true; 1012 long id = rs.nElementCreate(dt.mID, dk.mID, norm, size); 1013 return new Element(id, rs, dt, dk, norm, size); 1014 } 1015 1016 /** 1017 * Check if the current Element is compatible with another Element. 1018 * Primitive Elements are compatible if they share the same underlying 1019 * size and type (i.e. U8 is compatible with A_8). User-defined Elements 1020 * must be equal in order to be compatible. This requires strict name 1021 * equivalence for all sub-Elements (in addition to structural equivalence). 1022 * 1023 * @param e The Element to check compatibility with. 1024 * 1025 * @return boolean true if the Elements are compatible, otherwise false. 1026 */ 1027 public boolean isCompatible(Element e) { 1028 // Try strict BaseObj equality to start with. 1029 if (this.equals(e)) { 1030 return true; 1031 } 1032 1033 // Ignore mKind because it is allowed to be different (user vs. pixel). 1034 // We also ignore mNormalized because it can be different. The mType 1035 // field must not be NONE since we require name equivalence for 1036 // all user-created Elements. 1037 return ((mSize == e.mSize) && 1038 (mType != DataType.NONE) && 1039 (mType == e.mType) && 1040 (mVectorSize == e.mVectorSize)); 1041 } 1042 1043 /** 1044 * Builder class for producing complex elements with matching field and name 1045 * pairs. The builder starts empty. The order in which elements are added 1046 * is retained for the layout in memory. 1047 * 1048 */ 1049 public static class Builder { 1050 RenderScript mRS; 1051 Element[] mElements; 1052 String[] mElementNames; 1053 int[] mArraySizes; 1054 int mCount; 1055 int mSkipPadding; 1056 1057 /** 1058 * Create a builder object. 1059 * 1060 * @param rs 1061 */ 1062 public Builder(RenderScript rs) { 1063 mRS = rs; 1064 mCount = 0; 1065 mElements = new Element[8]; 1066 mElementNames = new String[8]; 1067 mArraySizes = new int[8]; 1068 } 1069 1070 /** 1071 * Add an array of elements to this element. 1072 * 1073 * @param element 1074 * @param name 1075 * @param arraySize 1076 */ 1077 public Builder add(Element element, String name, int arraySize) { 1078 if (arraySize < 1) { 1079 throw new RSIllegalArgumentException("Array size cannot be less than 1."); 1080 } 1081 1082 // Skip padding fields after a vector 3 type. 1083 if (mSkipPadding != 0) { 1084 if (name.startsWith("#padding_")) { 1085 mSkipPadding = 0; 1086 return this; 1087 } 1088 } 1089 1090 if (element.mVectorSize == 3) { 1091 mSkipPadding = 1; 1092 } else { 1093 mSkipPadding = 0; 1094 } 1095 1096 if(mCount == mElements.length) { 1097 Element[] e = new Element[mCount + 8]; 1098 String[] s = new String[mCount + 8]; 1099 int[] as = new int[mCount + 8]; 1100 System.arraycopy(mElements, 0, e, 0, mCount); 1101 System.arraycopy(mElementNames, 0, s, 0, mCount); 1102 System.arraycopy(mArraySizes, 0, as, 0, mCount); 1103 mElements = e; 1104 mElementNames = s; 1105 mArraySizes = as; 1106 } 1107 mElements[mCount] = element; 1108 mElementNames[mCount] = name; 1109 mArraySizes[mCount] = arraySize; 1110 mCount++; 1111 return this; 1112 } 1113 1114 /** 1115 * Add a single element to this Element. 1116 * 1117 * @param element 1118 * @param name 1119 */ 1120 public Builder add(Element element, String name) { 1121 return add(element, name, 1); 1122 } 1123 1124 /** 1125 * Create the element from this builder. 1126 * 1127 * 1128 * @return Element 1129 */ 1130 public Element create() { 1131 mRS.validate(); 1132 Element[] ein = new Element[mCount]; 1133 String[] sin = new String[mCount]; 1134 int[] asin = new int[mCount]; 1135 java.lang.System.arraycopy(mElements, 0, ein, 0, mCount); 1136 java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount); 1137 java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount); 1138 1139 long[] ids = new long[ein.length]; 1140 for (int ct = 0; ct < ein.length; ct++ ) { 1141 ids[ct] = ein[ct].getID(mRS); 1142 } 1143 long id = mRS.nElementCreate2(ids, sin, asin); 1144 return new Element(id, mRS, ein, sin, asin); 1145 } 1146 } 1147} 1148 1149