patrickfav / Dali

Dali is an image blur library for Android. It contains several modules for static blurring, live blurring and animations.
https://favr.dev/opensource/dali
Apache License 2.0
1.05k stars 89 forks source link

Crash when using HARDWARE bitmap #20

Open diegor2 opened 4 years ago

diegor2 commented 4 years ago
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        GlobalScope.launch(Dispatchers.IO) {
            val url = "https://upload.wikimedia.org/wikipedia/commons/a/ad/Gretag-Macbeth_ColorChecker.jpg"
            val stream = URL(url).openStream()
            val bitmap = BitmapFactory.decodeStream(stream)
                .copy(Bitmap.Config.HARDWARE, false)

            Dali.create(this@MainActivity)
                .load(bitmap)
                .skipCache()
                .blurRadius(12)
                .into(image);
        }
    }
}
2020-06-12 20:31:48.532 23211-23211/com.example.myapplication E/BlurBuilder: Could not set into imageview
    at.favre.lib.dali.builder.exception.BlurWorkerException: androidx.renderscript.RSInvalidStateException: Bad bitmap type: HARDWARE
        at at.favre.lib.dali.builder.blur.BlurWorker.process(BlurWorker.java:153)
        at at.favre.lib.dali.builder.blur.BlurWorker.call(BlurWorker.java:48)
        at at.favre.lib.dali.builder.blur.BlurWorker.call(BlurWorker.java:27)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:798)
     Caused by: androidx.renderscript.RSInvalidStateException: Bad bitmap type: HARDWARE
        at androidx.renderscript.Allocation.elementFromBitmap(Allocation.java:2654)
        at androidx.renderscript.Allocation.typeFromBitmap(Allocation.java:2659)
        at androidx.renderscript.Allocation.createFromBitmap(Allocation.java:2696)
        at androidx.renderscript.Allocation.createFromBitmap(Allocation.java:2749)
        at at.favre.lib.dali.blur.algorithms.RenderScriptGaussianBlur.blur(RenderScriptGaussianBlur.java:24)
        at at.favre.lib.dali.builder.blur.BlurWorker.process(BlurWorker.java:129)
        at at.favre.lib.dali.builder.blur.BlurWorker.call(BlurWorker.java:48) 
        at at.favre.lib.dali.builder.blur.BlurWorker.call(BlurWorker.java:27) 
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
        at java.lang.Thread.run(Thread.java:798) 
patrickfav commented 4 years ago

My only guess is, that renderscript is not compatible with the new HARDWARE flag? Again, I think this is a bug that has to be fixed on Google's side.

diegor2 commented 4 years ago

Yes, looks like renderscript doesn't support hardware bitmaps. As a workaround for #19 and #20 I'm copying the bitmap to a new one with ARGB_8888 config.

patrickfav commented 4 years ago

Do you have a suggestion how to continue with this issue?

diegor2 commented 4 years ago

I think Dali could internally check if the bitmap is hardware type and create a software copy.