Android Renderscript Allocation.USAGE_SHARED crash

半城伤御伤魂 提交于 2019-12-08 08:25:04

问题


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.


回答1:


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.




回答2:


I was facing the same problem and I resolved it, this issue was happening because my bitmap configuration was not Bitmap.Config.ARGB_8888, we should convert it to ARGB_8888 before applying the blur.

    Bitmap U8_4Bitmap;
    if (yourBitmap.getConfig() == Bitmap.Config.ARGB_8888) {
        U8_4Bitmap = yourBitmap;
    } else {
        U8_4Bitmap = yourBitmap.copy(Bitmap.Config.ARGB_8888, true);
    }


来源:https://stackoverflow.com/questions/18709301/android-renderscript-allocation-usage-shared-crash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!