From 1bf8c2dee4e135a7b3f5206db57e69ba216de483 Mon Sep 17 00:00:00 2001 From: skydoves Date: Thu, 2 Jan 2025 12:37:35 +0900 Subject: [PATCH] Use deferred for launching iterative blur --- .../main/kotlin/com/skydoves/cloudy/CloudyModifierNode.kt | 2 +- .../cloudy/internals/render/RenderScriptToolkit.kt | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/cloudy/src/main/kotlin/com/skydoves/cloudy/CloudyModifierNode.kt b/cloudy/src/main/kotlin/com/skydoves/cloudy/CloudyModifierNode.kt index a36a464..c7e4fa4 100644 --- a/cloudy/src/main/kotlin/com/skydoves/cloudy/CloudyModifierNode.kt +++ b/cloudy/src/main/kotlin/com/skydoves/cloudy/CloudyModifierNode.kt @@ -119,7 +119,7 @@ private class CloudyModifierNode( val blurredBitmap = iterativeBlur( androidBitmap = targetBitmap, radius = radius - )?.apply { + ).await()?.apply { drawImage(this.asImageBitmap()) } ?: throw RuntimeException("Couldn't capture a bitmap from the composable tree") diff --git a/cloudy/src/main/kotlin/com/skydoves/cloudy/internals/render/RenderScriptToolkit.kt b/cloudy/src/main/kotlin/com/skydoves/cloudy/internals/render/RenderScriptToolkit.kt index f89a57d..946cb70 100644 --- a/cloudy/src/main/kotlin/com/skydoves/cloudy/internals/render/RenderScriptToolkit.kt +++ b/cloudy/src/main/kotlin/com/skydoves/cloudy/internals/render/RenderScriptToolkit.kt @@ -18,7 +18,9 @@ package com.skydoves.cloudy.internals.render import android.graphics.Bitmap -import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Deferred +import kotlinx.coroutines.async // This string is used for error messages. private const val externalName = "RenderScript Toolkit" @@ -316,10 +318,10 @@ internal fun vectorSize(bitmap: Bitmap): Int { } } -internal suspend fun iterativeBlur( +internal fun CoroutineScope.iterativeBlur( androidBitmap: Bitmap, radius: Int -): Bitmap? = coroutineScope { +): Deferred = async { val iterate = (radius + 1) / 25 var bitmap: Bitmap? = RenderScriptToolkit.blur( inputBitmap = androidBitmap,