Allocation.java revision 3c0618be2fdad66f8d2249bd8b83a436b8aadec4
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.io.IOException; 20import java.io.InputStream; 21import android.content.res.Resources; 22import android.content.res.AssetManager; 23import android.graphics.Bitmap; 24import android.graphics.BitmapFactory; 25import android.util.Log; 26import android.util.TypedValue; 27 28/** 29 * <p> 30 * Memory allocation class for renderscript. An allocation combines a 31 * {@link android.renderscript.Type} with the memory to provide storage for user data and objects. 32 * This implies that all memory in Renderscript is typed. 33 * </p> 34 * 35 * <p>Allocations are the primary way data moves into and out of scripts. Memory is user 36 * synchronized and it's possible for allocations to exist in multiple memory spaces 37 * concurrently. Currently those spaces are:</p> 38 * <ul> 39 * <li>Script: accessable by RS scripts.</li> 40 * <li>Graphics Texture: accessable as a graphics texture.</li> 41 * <li>Graphics Vertex: accessable as graphical vertex data.</li> 42 * <li>Graphics Constants: Accessable as constants in user shaders</li> 43 * </ul> 44 * </p> 45 * <p> 46 * For example, when creating a allocation for a texture, the user can 47 * specify its memory spaces as both script and textures. This means that it can both 48 * be used as script binding and as a GPU texture for rendering. To maintain 49 * synchronization if a script modifies an allocation used by other targets it must 50 * call a synchronizing function to push the updates to the memory, otherwise the results 51 * are undefined. 52 * </p> 53 * <p>By default, Android system side updates are always applied to the script accessable 54 * memory. If this is not present, they are then applied to the various HW 55 * memory types. A {@link android.renderscript.Allocation#syncAll syncAll()} 56 * call is necessary after the script data is updated to 57 * keep the other memory spaces in sync.</p> 58 * 59 * <p>Allocation data is uploaded in one of two primary ways. For simple 60 * arrays there are copyFrom() functions that take an array from the control code and 61 * copy it to the slave memory store. Both type checked and unchecked copies are provided. 62 * The unchecked variants exist to allow apps to copy over arrays of structures from a 63 * control language that does not support structures.</p> 64 * 65 **/ 66public class Allocation extends BaseObj { 67 Type mType; 68 Bitmap mBitmap; 69 int mUsage; 70 71 /** 72 * The usage of the allocation. These signal to renderscript 73 * where to place the allocation in memory. 74 * 75 * SCRIPT The allocation will be bound to and accessed by 76 * scripts. 77 */ 78 public static final int USAGE_SCRIPT = 0x0001; 79 80 /** 81 * GRAPHICS_TEXTURE The allcation will be used as a texture 82 * source by one or more graphics programs. 83 * 84 */ 85 public static final int USAGE_GRAPHICS_TEXTURE = 0x0002; 86 87 /** 88 * GRAPHICS_VERTEX The allocation will be used as a graphics 89 * mesh. 90 * 91 */ 92 public static final int USAGE_GRAPHICS_VERTEX = 0x0004; 93 94 95 /** 96 * GRAPHICS_CONSTANTS The allocation will be used as the source 97 * of shader constants by one or more programs. 98 * 99 */ 100 public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008; 101 102 /** 103 * USAGE_GRAPHICS_RENDER_TARGET The allcation will be used as a 104 * target for offscreen rendering 105 * 106 */ 107 public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010; 108 109 110 /** 111 * Controls mipmap behavior when using the bitmap creation and 112 * update functions. 113 */ 114 public enum MipmapControl { 115 /** 116 * No mipmaps will be generated and the type generated from the 117 * incoming bitmap will not contain additional LODs. 118 */ 119 MIPMAP_NONE(0), 120 121 /** 122 * A Full mipmap chain will be created in script memory. The 123 * type of the allocation will contain a full mipmap chain. On 124 * upload to graphics the full chain will be transfered. 125 */ 126 MIPMAP_FULL(1), 127 128 /** 129 * The type of the allocation will be the same as MIPMAP_NONE. 130 * It will not contain mipmaps. On upload to graphics the 131 * graphics copy of the allocation data will contain a full 132 * mipmap chain generated from the top level in script memory. 133 */ 134 MIPMAP_ON_SYNC_TO_TEXTURE(2); 135 136 int mID; 137 MipmapControl(int id) { 138 mID = id; 139 } 140 } 141 142 Allocation(int id, RenderScript rs, Type t, int usage) { 143 super(id, rs); 144 if ((usage & ~(USAGE_SCRIPT | 145 USAGE_GRAPHICS_TEXTURE | 146 USAGE_GRAPHICS_VERTEX | 147 USAGE_GRAPHICS_CONSTANTS | 148 USAGE_GRAPHICS_RENDER_TARGET)) != 0) { 149 throw new RSIllegalArgumentException("Unknown usage specified."); 150 } 151 mType = t; 152 } 153 154 private void validateIsInt32() { 155 if ((mType.mElement.mType == Element.DataType.SIGNED_32) || 156 (mType.mElement.mType == Element.DataType.UNSIGNED_32)) { 157 return; 158 } 159 throw new RSIllegalArgumentException( 160 "32 bit integer source does not match allocation type " + mType.mElement.mType); 161 } 162 163 private void validateIsInt16() { 164 if ((mType.mElement.mType == Element.DataType.SIGNED_16) || 165 (mType.mElement.mType == Element.DataType.UNSIGNED_16)) { 166 return; 167 } 168 throw new RSIllegalArgumentException( 169 "16 bit integer source does not match allocation type " + mType.mElement.mType); 170 } 171 172 private void validateIsInt8() { 173 if ((mType.mElement.mType == Element.DataType.SIGNED_8) || 174 (mType.mElement.mType == Element.DataType.UNSIGNED_8)) { 175 return; 176 } 177 throw new RSIllegalArgumentException( 178 "8 bit integer source does not match allocation type " + mType.mElement.mType); 179 } 180 181 private void validateIsFloat32() { 182 if (mType.mElement.mType == Element.DataType.FLOAT_32) { 183 return; 184 } 185 throw new RSIllegalArgumentException( 186 "32 bit float source does not match allocation type " + mType.mElement.mType); 187 } 188 189 private void validateIsObject() { 190 if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) || 191 (mType.mElement.mType == Element.DataType.RS_TYPE) || 192 (mType.mElement.mType == Element.DataType.RS_ALLOCATION) || 193 (mType.mElement.mType == Element.DataType.RS_SAMPLER) || 194 (mType.mElement.mType == Element.DataType.RS_SCRIPT) || 195 (mType.mElement.mType == Element.DataType.RS_MESH) || 196 (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) || 197 (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) || 198 (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) || 199 (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) { 200 return; 201 } 202 throw new RSIllegalArgumentException( 203 "Object source does not match allocation type " + mType.mElement.mType); 204 } 205 206 @Override 207 void updateFromNative() { 208 super.updateFromNative(); 209 int typeID = mRS.nAllocationGetType(getID()); 210 if(typeID != 0) { 211 mType = new Type(typeID, mRS); 212 mType.updateFromNative(); 213 } 214 } 215 216 public Type getType() { 217 return mType; 218 } 219 220 public void syncAll(int srcLocation) { 221 switch (srcLocation) { 222 case USAGE_SCRIPT: 223 case USAGE_GRAPHICS_CONSTANTS: 224 case USAGE_GRAPHICS_TEXTURE: 225 case USAGE_GRAPHICS_VERTEX: 226 break; 227 default: 228 throw new RSIllegalArgumentException("Source must be exactly one usage type."); 229 } 230 mRS.validate(); 231 mRS.nAllocationSyncAll(getID(), srcLocation); 232 } 233 234 public void copyFrom(BaseObj[] d) { 235 mRS.validate(); 236 validateIsObject(); 237 if (d.length != mType.getCount()) { 238 throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " + 239 mType.getCount() + ", array length = " + d.length); 240 } 241 int i[] = new int[d.length]; 242 for (int ct=0; ct < d.length; ct++) { 243 i[ct] = d[ct].getID(); 244 } 245 copy1DRangeFromUnchecked(0, mType.getCount(), i); 246 } 247 248 private void validateBitmapFormat(Bitmap b) { 249 Bitmap.Config bc = b.getConfig(); 250 switch (bc) { 251 case ALPHA_8: 252 if (mType.getElement().mKind != Element.DataKind.PIXEL_A) { 253 throw new RSIllegalArgumentException("Allocation kind is " + 254 mType.getElement().mKind + ", type " + 255 mType.getElement().mType + 256 " of " + mType.getElement().getSizeBytes() + 257 " bytes, passed bitmap was " + bc); 258 } 259 break; 260 case ARGB_8888: 261 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) || 262 (mType.getElement().getSizeBytes() != 4)) { 263 throw new RSIllegalArgumentException("Allocation kind is " + 264 mType.getElement().mKind + ", type " + 265 mType.getElement().mType + 266 " of " + mType.getElement().getSizeBytes() + 267 " bytes, passed bitmap was " + bc); 268 } 269 break; 270 case RGB_565: 271 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) || 272 (mType.getElement().getSizeBytes() != 2)) { 273 throw new RSIllegalArgumentException("Allocation kind is " + 274 mType.getElement().mKind + ", type " + 275 mType.getElement().mType + 276 " of " + mType.getElement().getSizeBytes() + 277 " bytes, passed bitmap was " + bc); 278 } 279 break; 280 case ARGB_4444: 281 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) || 282 (mType.getElement().getSizeBytes() != 2)) { 283 throw new RSIllegalArgumentException("Allocation kind is " + 284 mType.getElement().mKind + ", type " + 285 mType.getElement().mType + 286 " of " + mType.getElement().getSizeBytes() + 287 " bytes, passed bitmap was " + bc); 288 } 289 break; 290 291 } 292 } 293 294 private void validateBitmapSize(Bitmap b) { 295 if(mType.getX() != b.getWidth() || 296 mType.getY() != b.getHeight()) { 297 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch"); 298 } 299 } 300 301 /** 302 * Copy an allocation from an array. This variant is not type 303 * checked which allows an application to fill in structured 304 * data from an array. 305 * 306 * @param d the source data array 307 */ 308 public void copyFromUnchecked(int[] d) { 309 mRS.validate(); 310 copy1DRangeFromUnchecked(0, mType.getCount(), d); 311 } 312 /** 313 * Copy an allocation from an array. This variant is not type 314 * checked which allows an application to fill in structured 315 * data from an array. 316 * 317 * @param d the source data array 318 */ 319 public void copyFromUnchecked(short[] d) { 320 mRS.validate(); 321 copy1DRangeFromUnchecked(0, mType.getCount(), d); 322 } 323 /** 324 * Copy an allocation from an array. This variant is not type 325 * checked which allows an application to fill in structured 326 * data from an array. 327 * 328 * @param d the source data array 329 */ 330 public void copyFromUnchecked(byte[] d) { 331 mRS.validate(); 332 copy1DRangeFromUnchecked(0, mType.getCount(), d); 333 } 334 /** 335 * Copy an allocation from an array. This variant is not type 336 * checked which allows an application to fill in structured 337 * data from an array. 338 * 339 * @param d the source data array 340 */ 341 public void copyFromUnchecked(float[] d) { 342 mRS.validate(); 343 copy1DRangeFromUnchecked(0, mType.getCount(), d); 344 } 345 346 /** 347 * Copy an allocation from an array. This variant is type 348 * checked and will generate exceptions if the Allocation type 349 * is not a 32 bit integer type. 350 * 351 * @param d the source data array 352 */ 353 public void copyFrom(int[] d) { 354 mRS.validate(); 355 copy1DRangeFrom(0, mType.getCount(), d); 356 } 357 358 /** 359 * Copy an allocation from an array. This variant is type 360 * checked and will generate exceptions if the Allocation type 361 * is not a 16 bit integer type. 362 * 363 * @param d the source data array 364 */ 365 public void copyFrom(short[] d) { 366 mRS.validate(); 367 copy1DRangeFrom(0, mType.getCount(), d); 368 } 369 370 /** 371 * Copy an allocation from an array. This variant is type 372 * checked and will generate exceptions if the Allocation type 373 * is not a 8 bit integer type. 374 * 375 * @param d the source data array 376 */ 377 public void copyFrom(byte[] d) { 378 mRS.validate(); 379 copy1DRangeFrom(0, mType.getCount(), d); 380 } 381 382 /** 383 * Copy an allocation from an array. This variant is type 384 * checked and will generate exceptions if the Allocation type 385 * is not a 32 bit float type. 386 * 387 * @param d the source data array 388 */ 389 public void copyFrom(float[] d) { 390 mRS.validate(); 391 copy1DRangeFrom(0, mType.getCount(), d); 392 } 393 394 /** 395 * Copy an allocation from a bitmap. The height, width, and 396 * format of the bitmap must match the existing allocation. 397 * 398 * @param b the source bitmap 399 */ 400 public void copyFrom(Bitmap b) { 401 mRS.validate(); 402 validateBitmapSize(b); 403 validateBitmapFormat(b); 404 mRS.nAllocationCopyFromBitmap(getID(), b); 405 } 406 407 /** 408 * This is only intended to be used by auto-generate code reflected from the 409 * renderscript script files. 410 * 411 * @param xoff 412 * @param fp 413 */ 414 public void setFromFieldPacker(int xoff, FieldPacker fp) { 415 int eSize = mType.mElement.getSizeBytes(); 416 final byte[] data = fp.getData(); 417 418 int count = data.length / eSize; 419 if ((eSize * count) != data.length) { 420 throw new RSIllegalArgumentException("Field packer length " + data.length + 421 " not divisible by element size " + eSize + "."); 422 } 423 data1DChecks(xoff, count, data.length, data.length); 424 mRS.nAllocationData1D(getID(), xoff, 0, count, data, data.length); 425 } 426 427 /** 428 * This is only intended to be used by auto-generate code reflected from the 429 * renderscript script files. 430 * 431 * @param xoff 432 * @param component_number 433 * @param fp 434 */ 435 public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) { 436 if (component_number >= mType.mElement.mElements.length) { 437 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range."); 438 } 439 if(xoff < 0) { 440 throw new RSIllegalArgumentException("Offset must be >= 0."); 441 } 442 443 final byte[] data = fp.getData(); 444 int eSize = mType.mElement.mElements[component_number].getSizeBytes(); 445 446 if (data.length != eSize) { 447 throw new RSIllegalArgumentException("Field packer sizelength " + data.length + 448 " does not match component size " + eSize + "."); 449 } 450 451 mRS.nAllocationElementData1D(getID(), xoff, 0, component_number, data, data.length); 452 } 453 454 private void data1DChecks(int off, int count, int len, int dataSize) { 455 mRS.validate(); 456 if(off < 0) { 457 throw new RSIllegalArgumentException("Offset must be >= 0."); 458 } 459 if(count < 1) { 460 throw new RSIllegalArgumentException("Count must be >= 1."); 461 } 462 if((off + count) > mType.getCount()) { 463 throw new RSIllegalArgumentException("Overflow, Available count " + mType.getCount() + 464 ", got " + count + " at offset " + off + "."); 465 } 466 if((len) < dataSize) { 467 throw new RSIllegalArgumentException("Array too small for allocation type."); 468 } 469 } 470 471 /** 472 * Generate a mipmap chain. Requires the type of the allocation 473 * include mipmaps. 474 * 475 * This function will generate a complete set of mipmaps from 476 * the top level lod and place them into the script memoryspace. 477 * 478 * If the allocation is also using other memory spaces a 479 * followup sync will be required. 480 */ 481 public void generateMipmaps() { 482 mRS.nAllocationGenerateMipmaps(getID()); 483 } 484 485 /** 486 * Copy part of an allocation from an array. This variant is 487 * not type checked which allows an application to fill in 488 * structured data from an array. 489 * 490 * @param off The offset of the first element to be copied. 491 * @param count The number of elements to be copied. 492 * @param d the source data array 493 */ 494 public void copy1DRangeFromUnchecked(int off, int count, int[] d) { 495 int dataSize = mType.mElement.getSizeBytes() * count; 496 data1DChecks(off, count, d.length * 4, dataSize); 497 mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize); 498 } 499 /** 500 * Copy part of an allocation from an array. This variant is 501 * not type checked which allows an application to fill in 502 * structured data from an array. 503 * 504 * @param off The offset of the first element to be copied. 505 * @param count The number of elements to be copied. 506 * @param d the source data array 507 */ 508 public void copy1DRangeFromUnchecked(int off, int count, short[] d) { 509 int dataSize = mType.mElement.getSizeBytes() * count; 510 data1DChecks(off, count, d.length * 2, dataSize); 511 mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize); 512 } 513 /** 514 * Copy part of an allocation from an array. This variant is 515 * not type checked which allows an application to fill in 516 * structured data from an array. 517 * 518 * @param off The offset of the first element to be copied. 519 * @param count The number of elements to be copied. 520 * @param d the source data array 521 */ 522 public void copy1DRangeFromUnchecked(int off, int count, byte[] d) { 523 int dataSize = mType.mElement.getSizeBytes() * count; 524 data1DChecks(off, count, d.length, dataSize); 525 mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize); 526 } 527 /** 528 * Copy part of an allocation from an array. This variant is 529 * not type checked which allows an application to fill in 530 * structured data from an array. 531 * 532 * @param off The offset of the first element to be copied. 533 * @param count The number of elements to be copied. 534 * @param d the source data array 535 */ 536 public void copy1DRangeFromUnchecked(int off, int count, float[] d) { 537 int dataSize = mType.mElement.getSizeBytes() * count; 538 data1DChecks(off, count, d.length * 4, dataSize); 539 mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize); 540 } 541 542 /** 543 * Copy part of an allocation from an array. This variant is 544 * type checked and will generate exceptions if the Allocation 545 * type is not a 32 bit integer type. 546 * 547 * @param off The offset of the first element to be copied. 548 * @param count The number of elements to be copied. 549 * @param d the source data array 550 */ 551 public void copy1DRangeFrom(int off, int count, int[] d) { 552 validateIsInt32(); 553 copy1DRangeFromUnchecked(off, count, d); 554 } 555 556 /** 557 * Copy part of an allocation from an array. This variant is 558 * type checked and will generate exceptions if the Allocation 559 * type is not a 16 bit integer type. 560 * 561 * @param off The offset of the first element to be copied. 562 * @param count The number of elements to be copied. 563 * @param d the source data array 564 */ 565 public void copy1DRangeFrom(int off, int count, short[] d) { 566 validateIsInt16(); 567 copy1DRangeFromUnchecked(off, count, d); 568 } 569 570 /** 571 * Copy part of an allocation from an array. This variant is 572 * type checked and will generate exceptions if the Allocation 573 * type is not a 8 bit integer type. 574 * 575 * @param off The offset of the first element to be copied. 576 * @param count The number of elements to be copied. 577 * @param d the source data array 578 */ 579 public void copy1DRangeFrom(int off, int count, byte[] d) { 580 validateIsInt8(); 581 copy1DRangeFromUnchecked(off, count, d); 582 } 583 584 /** 585 * Copy part of an allocation from an array. This variant is 586 * type checked and will generate exceptions if the Allocation 587 * type is not a 32 bit float type. 588 * 589 * @param off The offset of the first element to be copied. 590 * @param count The number of elements to be copied. 591 * @param d the source data array. 592 */ 593 public void copy1DRangeFrom(int off, int count, float[] d) { 594 validateIsFloat32(); 595 copy1DRangeFromUnchecked(off, count, d); 596 } 597 598 /** 599 * Copy part of an allocation from another allocation. 600 * 601 * @param off The offset of the first element to be copied. 602 * @param count The number of elements to be copied. 603 * @param data the source data allocation. 604 * @param dataOff off The offset of the first element in data to 605 * be copied. 606 */ 607 public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) { 608 mRS.nAllocationData2D(getID(), off, 0, 609 0, Type.CubemapFace.POSITIVE_X.mID, 610 count, 1, data.getID(), dataOff, 0, 611 0, Type.CubemapFace.POSITIVE_X.mID); 612 } 613 614 private void validate2DRange(int xoff, int yoff, int w, int h) { 615 if (xoff < 0 || yoff < 0) { 616 throw new RSIllegalArgumentException("Offset cannot be negative."); 617 } 618 if (h < 0 || w < 0) { 619 throw new RSIllegalArgumentException("Height or width cannot be negative."); 620 } 621 if ((xoff + w) > mType.mDimX || 622 (yoff + h) > mType.mDimY) { 623 throw new RSIllegalArgumentException("Updated region larger than allocation."); 624 } 625 } 626 627 /** 628 * Copy a rectangular region from the array into the allocation. 629 * The incoming array is assumed to be tightly packed. 630 * 631 * @param xoff X offset of the region to update 632 * @param yoff Y offset of the region to update 633 * @param w Width of the incoming region to update 634 * @param h Height of the incoming region to update 635 * @param data to be placed into the allocation 636 */ 637 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) { 638 mRS.validate(); 639 validate2DRange(xoff, yoff, w, h); 640 mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, data, data.length); 641 } 642 643 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) { 644 mRS.validate(); 645 validate2DRange(xoff, yoff, w, h); 646 mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, data, data.length * 2); 647 } 648 649 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) { 650 mRS.validate(); 651 validate2DRange(xoff, yoff, w, h); 652 mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, data, data.length * 4); 653 } 654 655 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) { 656 mRS.validate(); 657 validate2DRange(xoff, yoff, w, h); 658 mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, data, data.length * 4); 659 } 660 661 /** 662 * Copy a rectangular region into the allocation from another 663 * allocation. 664 * 665 * @param xoff X offset of the region to update. 666 * @param yoff Y offset of the region to update. 667 * @param w Width of the incoming region to update. 668 * @param h Height of the incoming region to update. 669 * @param data source allocation. 670 * @param dataXoff X offset in data of the region to update. 671 * @param dataYoff Y offset in data of the region to update. 672 */ 673 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, 674 Allocation data, int dataXoff, int dataYoff) { 675 mRS.validate(); 676 validate2DRange(xoff, yoff, w, h); 677 mRS.nAllocationData2D(getID(), xoff, yoff, 678 0, Type.CubemapFace.POSITIVE_X.mID, 679 w, h, data.getID(), dataXoff, dataYoff, 680 0, Type.CubemapFace.POSITIVE_X.mID); 681 } 682 683 /** 684 * Copy a bitmap into an allocation. The height and width of 685 * the update will use the height and width of the incoming 686 * bitmap. 687 * 688 * @param xoff X offset of the region to update 689 * @param yoff Y offset of the region to update 690 * @param data the bitmap to be copied 691 */ 692 public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) { 693 mRS.validate(); 694 validateBitmapFormat(data); 695 validate2DRange(xoff, yoff, data.getWidth(), data.getHeight()); 696 mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, data); 697 } 698 699 700 public void copyTo(Bitmap b) { 701 mRS.validate(); 702 validateBitmapFormat(b); 703 validateBitmapSize(b); 704 mRS.nAllocationCopyToBitmap(getID(), b); 705 } 706 707 public void copyTo(byte[] d) { 708 validateIsInt8(); 709 mRS.validate(); 710 mRS.nAllocationRead(getID(), d); 711 } 712 713 public void copyTo(short[] d) { 714 validateIsInt16(); 715 mRS.validate(); 716 mRS.nAllocationRead(getID(), d); 717 } 718 719 public void copyTo(int[] d) { 720 validateIsInt32(); 721 mRS.validate(); 722 mRS.nAllocationRead(getID(), d); 723 } 724 725 public void copyTo(float[] d) { 726 validateIsFloat32(); 727 mRS.validate(); 728 mRS.nAllocationRead(getID(), d); 729 } 730 731 /** 732 * Resize a 1D allocation. The contents of the allocation are 733 * preserved. If new elements are allocated objects are created 734 * with null contents and the new region is otherwise undefined. 735 * 736 * If the new region is smaller the references of any objects 737 * outside the new region will be released. 738 * 739 * A new type will be created with the new dimension. 740 * 741 * @param dimX The new size of the allocation. 742 */ 743 public synchronized void resize(int dimX) { 744 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) { 745 throw new RSInvalidStateException("Resize only support for 1D allocations at this time."); 746 } 747 mRS.nAllocationResize1D(getID(), dimX); 748 mRS.finish(); // Necessary because resize is fifoed and update is async. 749 750 int typeID = mRS.nAllocationGetType(getID()); 751 mType = new Type(typeID, mRS); 752 mType.updateFromNative(); 753 } 754 755 /* 756 public void resize(int dimX, int dimY) { 757 if ((mType.getZ() > 0) || mType.getFaces() || mType.getLOD()) { 758 throw new RSIllegalStateException("Resize only support for 2D allocations at this time."); 759 } 760 if (mType.getY() == 0) { 761 throw new RSIllegalStateException("Resize only support for 2D allocations at this time."); 762 } 763 mRS.nAllocationResize2D(getID(), dimX, dimY); 764 } 765 */ 766 767 768 769 // creation 770 771 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options(); 772 static { 773 mBitmapOptions.inScaled = false; 774 } 775 776 /** 777 * 778 * @param type renderscript type describing data layout 779 * @param mips specifies desired mipmap behaviour for the 780 * allocation 781 * @param usage bit field specifying how the allocation is 782 * utilized 783 */ 784 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) { 785 rs.validate(); 786 if (type.getID() == 0) { 787 throw new RSInvalidStateException("Bad Type"); 788 } 789 int id = rs.nAllocationCreateTyped(type.getID(), mips.mID, usage); 790 if (id == 0) { 791 throw new RSRuntimeException("Allocation creation failed."); 792 } 793 return new Allocation(id, rs, type, usage); 794 } 795 796 /** 797 * Creates a renderscript allocation with the size specified by 798 * the type and no mipmaps generated by default 799 * 800 * @param rs Context to which the allocation will belong. 801 * @param type renderscript type describing data layout 802 * @param usage bit field specifying how the allocation is 803 * utilized 804 * 805 * @return allocation 806 */ 807 static public Allocation createTyped(RenderScript rs, Type type, int usage) { 808 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage); 809 } 810 811 /** 812 * Creates a renderscript allocation for use by the script with 813 * the size specified by the type and no mipmaps generated by 814 * default 815 * 816 * @param rs Context to which the allocation will belong. 817 * @param type renderscript type describing data layout 818 * 819 * @return allocation 820 */ 821 static public Allocation createTyped(RenderScript rs, Type type) { 822 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT); 823 } 824 825 /** 826 * Creates a renderscript allocation with a specified number of 827 * given elements 828 * 829 * @param rs Context to which the allocation will belong. 830 * @param e describes what each element of an allocation is 831 * @param count specifies the number of element in the allocation 832 * @param usage bit field specifying how the allocation is 833 * utilized 834 * 835 * @return allocation 836 */ 837 static public Allocation createSized(RenderScript rs, Element e, 838 int count, int usage) { 839 rs.validate(); 840 Type.Builder b = new Type.Builder(rs, e); 841 b.setX(count); 842 Type t = b.create(); 843 844 int id = rs.nAllocationCreateTyped(t.getID(), MipmapControl.MIPMAP_NONE.mID, usage); 845 if (id == 0) { 846 throw new RSRuntimeException("Allocation creation failed."); 847 } 848 return new Allocation(id, rs, t, usage); 849 } 850 851 /** 852 * Creates a renderscript allocation with a specified number of 853 * given elements 854 * 855 * @param rs Context to which the allocation will belong. 856 * @param e describes what each element of an allocation is 857 * @param count specifies the number of element in the allocation 858 * 859 * @return allocation 860 */ 861 static public Allocation createSized(RenderScript rs, Element e, int count) { 862 return createSized(rs, e, count, USAGE_SCRIPT); 863 } 864 865 static Element elementFromBitmap(RenderScript rs, Bitmap b) { 866 final Bitmap.Config bc = b.getConfig(); 867 if (bc == Bitmap.Config.ALPHA_8) { 868 return Element.A_8(rs); 869 } 870 if (bc == Bitmap.Config.ARGB_4444) { 871 return Element.RGBA_4444(rs); 872 } 873 if (bc == Bitmap.Config.ARGB_8888) { 874 return Element.RGBA_8888(rs); 875 } 876 if (bc == Bitmap.Config.RGB_565) { 877 return Element.RGB_565(rs); 878 } 879 throw new RSInvalidStateException("Bad bitmap type: " + bc); 880 } 881 882 static Type typeFromBitmap(RenderScript rs, Bitmap b, 883 MipmapControl mip) { 884 Element e = elementFromBitmap(rs, b); 885 Type.Builder tb = new Type.Builder(rs, e); 886 tb.setX(b.getWidth()); 887 tb.setY(b.getHeight()); 888 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL); 889 return tb.create(); 890 } 891 892 /** 893 * Creates a renderscript allocation from a bitmap 894 * 895 * @param rs Context to which the allocation will belong. 896 * @param b bitmap source for the allocation data 897 * @param mips specifies desired mipmap behaviour for the 898 * allocation 899 * @param usage bit field specifying how the allocation is 900 * utilized 901 * 902 * @return renderscript allocation containing bitmap data 903 * 904 */ 905 static public Allocation createFromBitmap(RenderScript rs, Bitmap b, 906 MipmapControl mips, 907 int usage) { 908 rs.validate(); 909 Type t = typeFromBitmap(rs, b, mips); 910 911 int id = rs.nAllocationCreateFromBitmap(t.getID(), mips.mID, b, usage); 912 if (id == 0) { 913 throw new RSRuntimeException("Load failed."); 914 } 915 return new Allocation(id, rs, t, usage); 916 } 917 918 /** 919 * Creates a non-mipmapped renderscript allocation to use as a 920 * graphics texture 921 * 922 * @param rs Context to which the allocation will belong. 923 * @param b bitmap source for the allocation data 924 * 925 * @return renderscript allocation containing bitmap data 926 * 927 */ 928 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) { 929 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE, 930 USAGE_GRAPHICS_TEXTURE); 931 } 932 933 /** 934 * Creates a cubemap allocation from a bitmap containing the 935 * horizontal list of cube faces. Each individual face must be 936 * the same size and power of 2 937 * 938 * @param rs Context to which the allocation will belong. 939 * @param b bitmap with cubemap faces layed out in the following 940 * format: right, left, top, bottom, front, back 941 * @param mips specifies desired mipmap behaviour for the cubemap 942 * @param usage bit field specifying how the cubemap is utilized 943 * 944 * @return allocation containing cubemap data 945 * 946 */ 947 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b, 948 MipmapControl mips, 949 int usage) { 950 rs.validate(); 951 952 int height = b.getHeight(); 953 int width = b.getWidth(); 954 955 if (width % 6 != 0) { 956 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6"); 957 } 958 if (width / 6 != height) { 959 throw new RSIllegalArgumentException("Only square cube map faces supported"); 960 } 961 boolean isPow2 = (height & (height - 1)) == 0; 962 if (!isPow2) { 963 throw new RSIllegalArgumentException("Only power of 2 cube faces supported"); 964 } 965 966 Element e = elementFromBitmap(rs, b); 967 Type.Builder tb = new Type.Builder(rs, e); 968 tb.setX(height); 969 tb.setY(height); 970 tb.setFaces(true); 971 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL); 972 Type t = tb.create(); 973 974 int id = rs.nAllocationCubeCreateFromBitmap(t.getID(), mips.mID, b, usage); 975 if(id == 0) { 976 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e); 977 } 978 return new Allocation(id, rs, t, usage); 979 } 980 981 /** 982 * Creates a non-mipmapped cubemap allocation for use as a 983 * graphics texture from a bitmap containing the horizontal list 984 * of cube faces. Each individual face must be the same size and 985 * power of 2 986 * 987 * @param rs Context to which the allocation will belong. 988 * @param b bitmap with cubemap faces layed out in the following 989 * format: right, left, top, bottom, front, back 990 * 991 * @return allocation containing cubemap data 992 * 993 */ 994 static public Allocation createCubemapFromBitmap(RenderScript rs, 995 Bitmap b) { 996 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE, 997 USAGE_GRAPHICS_TEXTURE); 998 } 999 1000 /** 1001 * Creates a cubemap allocation from 6 bitmaps containing 1002 * the cube faces. All the faces must be the same size and 1003 * power of 2 1004 * 1005 * @param rs Context to which the allocation will belong. 1006 * @param xpos cubemap face in the positive x direction 1007 * @param xneg cubemap face in the negative x direction 1008 * @param ypos cubemap face in the positive y direction 1009 * @param yneg cubemap face in the negative y direction 1010 * @param zpos cubemap face in the positive z direction 1011 * @param zneg cubemap face in the negative z direction 1012 * @param mips specifies desired mipmap behaviour for the cubemap 1013 * @param usage bit field specifying how the cubemap is utilized 1014 * 1015 * @return allocation containing cubemap data 1016 * 1017 */ 1018 static public Allocation createCubemapFromCubeFaces(RenderScript rs, 1019 Bitmap xpos, 1020 Bitmap xneg, 1021 Bitmap ypos, 1022 Bitmap yneg, 1023 Bitmap zpos, 1024 Bitmap zneg, 1025 MipmapControl mips, 1026 int usage) { 1027 int height = xpos.getHeight(); 1028 if (xpos.getWidth() != height || 1029 xneg.getWidth() != height || xneg.getHeight() != height || 1030 ypos.getWidth() != height || ypos.getHeight() != height || 1031 yneg.getWidth() != height || yneg.getHeight() != height || 1032 zpos.getWidth() != height || zpos.getHeight() != height || 1033 zneg.getWidth() != height || zneg.getHeight() != height) { 1034 throw new RSIllegalArgumentException("Only square cube map faces supported"); 1035 } 1036 boolean isPow2 = (height & (height - 1)) == 0; 1037 if (!isPow2) { 1038 throw new RSIllegalArgumentException("Only power of 2 cube faces supported"); 1039 } 1040 1041 Element e = elementFromBitmap(rs, xpos); 1042 Type.Builder tb = new Type.Builder(rs, e); 1043 tb.setX(height); 1044 tb.setY(height); 1045 tb.setFaces(true); 1046 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL); 1047 Type t = tb.create(); 1048 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage); 1049 1050 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap); 1051 adapter.setFace(Type.CubemapFace.POSITIVE_X); 1052 adapter.copyFrom(xpos); 1053 adapter.setFace(Type.CubemapFace.NEGATIVE_X); 1054 adapter.copyFrom(xneg); 1055 adapter.setFace(Type.CubemapFace.POSITIVE_Y); 1056 adapter.copyFrom(ypos); 1057 adapter.setFace(Type.CubemapFace.NEGATIVE_Y); 1058 adapter.copyFrom(yneg); 1059 adapter.setFace(Type.CubemapFace.POSITIVE_Z); 1060 adapter.copyFrom(zpos); 1061 adapter.setFace(Type.CubemapFace.NEGATIVE_Z); 1062 adapter.copyFrom(zneg); 1063 1064 return cubemap; 1065 } 1066 1067 /** 1068 * Creates a non-mipmapped cubemap allocation for use as a 1069 * graphics texture from 6 bitmaps containing 1070 * the cube faces. All the faces must be the same size and 1071 * power of 2 1072 * 1073 * @param rs Context to which the allocation will belong. 1074 * @param xpos cubemap face in the positive x direction 1075 * @param xneg cubemap face in the negative x direction 1076 * @param ypos cubemap face in the positive y direction 1077 * @param yneg cubemap face in the negative y direction 1078 * @param zpos cubemap face in the positive z direction 1079 * @param zneg cubemap face in the negative z direction 1080 * 1081 * @return allocation containing cubemap data 1082 * 1083 */ 1084 static public Allocation createCubemapFromCubeFaces(RenderScript rs, 1085 Bitmap xpos, 1086 Bitmap xneg, 1087 Bitmap ypos, 1088 Bitmap yneg, 1089 Bitmap zpos, 1090 Bitmap zneg) { 1091 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg, 1092 zpos, zneg, MipmapControl.MIPMAP_NONE, 1093 USAGE_GRAPHICS_TEXTURE); 1094 } 1095 1096 /** 1097 * Creates a renderscript allocation from the bitmap referenced 1098 * by resource id 1099 * 1100 * @param rs Context to which the allocation will belong. 1101 * @param res application resources 1102 * @param id resource id to load the data from 1103 * @param mips specifies desired mipmap behaviour for the 1104 * allocation 1105 * @param usage bit field specifying how the allocation is 1106 * utilized 1107 * 1108 * @return renderscript allocation containing resource data 1109 * 1110 */ 1111 static public Allocation createFromBitmapResource(RenderScript rs, 1112 Resources res, 1113 int id, 1114 MipmapControl mips, 1115 int usage) { 1116 1117 rs.validate(); 1118 Bitmap b = BitmapFactory.decodeResource(res, id); 1119 Allocation alloc = createFromBitmap(rs, b, mips, usage); 1120 b.recycle(); 1121 return alloc; 1122 } 1123 1124 /** 1125 * Creates a non-mipmapped renderscript allocation to use as a 1126 * graphics texture from the bitmap referenced by resource id 1127 * 1128 * @param rs Context to which the allocation will belong. 1129 * @param res application resources 1130 * @param id resource id to load the data from 1131 * 1132 * @return renderscript allocation containing resource data 1133 * 1134 */ 1135 static public Allocation createFromBitmapResource(RenderScript rs, 1136 Resources res, 1137 int id) { 1138 return createFromBitmapResource(rs, res, id, 1139 MipmapControl.MIPMAP_NONE, 1140 USAGE_GRAPHICS_TEXTURE); 1141 } 1142 1143 /** 1144 * Creates a renderscript allocation containing string data 1145 * encoded in UTF-8 format 1146 * 1147 * @param rs Context to which the allocation will belong. 1148 * @param str string to create the allocation from 1149 * @param usage bit field specifying how the allocaiton is 1150 * utilized 1151 * 1152 */ 1153 static public Allocation createFromString(RenderScript rs, 1154 String str, 1155 int usage) { 1156 rs.validate(); 1157 byte[] allocArray = null; 1158 try { 1159 allocArray = str.getBytes("UTF-8"); 1160 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage); 1161 alloc.copyFrom(allocArray); 1162 return alloc; 1163 } 1164 catch (Exception e) { 1165 throw new RSRuntimeException("Could not convert string to utf-8."); 1166 } 1167 } 1168} 1169 1170 1171