Search code examples
androidallocationbitmapdatarenderscript

Android Renderscript Allocation.USAGE_SHARED crash


I am getting a crash while running my app which uses renderscript. Unfortunately, the logcat does not give any specific details.

b = Bitmap.createBitmap(ib.getWidth(), ib.getHeight(),ib.getConfig());

Allocation mInAllocation = Allocation.createFromBitmap(mRS, inBitmap,
                Allocation.MipmapControl.MIPMAP_NONE,Allocation.USAGE_SHARED);

Allocation mOutAllocation2 = Allocation.createFromBitmap(mRS,
                 outBitmap, Allocation.MipmapControl.MIPMAP_NONE,
                 Allocation.USAGE_SHARED);

...execute an algorithm from .rs file and later do the below

mOutAllocation2.copyTo(outBitmap)`;

The same code sequence runs perfectly fine, when I used USAGE_SCRIPT flag instead of USAGE_SHARED for mOutAllocation2.

Any help on why this could happen?

I read in android docs that if the allocation is of the type USAGE_SHARED, then the copy operation from allocation to the bitmap (see above) is faster. Currently, I am seeing copies from allocation to bitmaps running into secs for decently large images (8MP and above)

I am using Nexus 10 (Android 4.3) currently.


Solution

  • First, you need to be using Allocation.USAGE_SCRIPT | Allocation.USAGE_SHARED. createFromBitmap(RenderScript, Bitmap) will set that for you when possible.

    Second, if your copy times are taking that long, you're probably seeing script execution as well. Script execution is asynchronous, so the wall clock time of copyTo(Bitmap) may include significantly more than just the copy.