Search code examples
androidrenderscript

Using generic Allocations with ScriptIntrinsics


I have been trying to use Renderscript ScriptIntrinsics with generic Allocations not created from bitmaps, such as the following code:

byte[] zeroedArray = new byte[bitmap.getWidth() * bitmap.getHeight()];
Allocation inputAllocation = Allocation.createSized(mRSContext,
                Element.U8(mRSContext),
                bitmap.getWidth() * bitmap.getHeight(),
                Allocation.USAGE_SCRIPT);
inputAllocation.copyFrom(zeroedArray);

final Allocation output = Allocation.createTyped(mRSContext, inputAllocation.getType());

ScriptIntrinsicBlur scriptBlur =
                ScriptIntrinsicBlur.create(mRSContext, Element.U8(mRSContext));

scriptBlur.setRadius(25f);
scriptBlur.setInput(inputAllocation);
scriptBlur.forEach(output);
output.copyTo(zeroedArray);

Running this code will crash the app and throw exceptions:

10-01 02:47:59.601    8705-8726/com.example A/libc﹕ Fatal signal 11 (SIGSEGV), code 1, fault addr 0xa4892700 in tid 8726 (rsexample)
10-01 02:47:59.601    8705-8729/com.example A/libc﹕ Fatal signal 11 (SIGSEGV), code 1, fault addr 0xa4892700 in tid 8729 (rsexample)
10-01 02:47:59.601    8705-8727/com.example A/libc﹕ Fatal signal 11 (SIGSEGV), code 1, fault addr 0xa4892700 in tid 8727 (rsexample)
10-01 02:47:59.601    8705-8728/com.example A/libc﹕ Fatal signal 11 (SIGSEGV), code 1, fault addr 0xa4892700 in tid 8728 (rsexample)

However if I create the allocations from bitmaps like the following it works (also using the correct element type when instantiating the script):

inputAllocation = Allocation.createFromBitmap(mRSContext,
                bitmap,
                Allocation.MipmapControl.MIPMAP_NONE,
                Allocation.USAGE_SCRIPT);
(...)
ScriptIntrinsicBlur scriptBlur =
                ScriptIntrinsicBlur.create(mRSContext,
                        Element.U8_4(mRSContext));

Reading from the documentation I find no requirement that the input Allocation should be created from a bitmap. It only mentions that it should be of Element U8 types. Am I missing something to make it work with generic Allocations or is it intended to only work with bitmaps?


Solution

  • Ah, I think there is a bug here (and I will file it internally tomorrow at work). Blur expects a 2D input, and you created only a 1 dimensional Allocation above (with createSized()). I believe that if you create a 2D Allocation, it will work even for non-Bitmap data.