Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use internal modifier's coroutine scope instead of using runblocking #48

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cloudy/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,5 @@ kotlin {
dependencies {
implementation(libs.androidx.compose.ui)
implementation(libs.androidx.compose.runtime)
implementation(libs.kotlinx.coroutines.android)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import androidx.compose.ui.graphics.asAndroidBitmap
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.drawscope.ContentDrawScope
import androidx.compose.ui.graphics.layer.GraphicsLayer
import androidx.compose.ui.graphics.layer.drawLayer
import androidx.compose.ui.graphics.rememberGraphicsLayer
import androidx.compose.ui.layout.Measurable
import androidx.compose.ui.layout.MeasureResult
Expand All @@ -38,7 +39,7 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.offset
import com.skydoves.cloudy.internals.render.iterativeBlur
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.launch

/**
* `Modifier.cloudy()` is a replacement of the [blur] modifier (compatible with under Android 12),
Expand Down Expand Up @@ -121,24 +122,26 @@ private class CloudyModifierNode(
[email protected]()
}

drawLayer(graphicsLayer)

onStateChanged.invoke(CloudyState.Loading)

try {
val blurredBitmap: Bitmap = runBlocking(Dispatchers.IO) {
coroutineScope.launch(Dispatchers.Main.immediate) {
try {
val targetBitmap: Bitmap = graphicsLayer.toImageBitmap().asAndroidBitmap()
.copy(Bitmap.Config.ARGB_8888, true)

iterativeBlur(
val blurredBitmap = iterativeBlur(
androidBitmap = targetBitmap,
radius = radius
)?.apply {
drawImage(this.asImageBitmap())
}
} ?: throw RuntimeException("Couldn't capture a bitmap from the composable tree")
} ?: throw RuntimeException("Couldn't capture a bitmap from the composable tree")

onStateChanged.invoke(CloudyState.Success(blurredBitmap))
} catch (e: Exception) {
onStateChanged.invoke(CloudyState.Error(e))
onStateChanged.invoke(CloudyState.Success(blurredBitmap))
} catch (e: Exception) {
onStateChanged.invoke(CloudyState.Error(e))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.skydoves.cloudy.internals.render

import android.graphics.Bitmap
import kotlinx.coroutines.coroutineScope

// This string is used for error messages.
private const val externalName = "RenderScript Toolkit"
Expand Down Expand Up @@ -315,10 +316,10 @@ internal fun vectorSize(bitmap: Bitmap): Int {
}
}

internal fun iterativeBlur(
internal suspend fun iterativeBlur(
androidBitmap: Bitmap,
radius: Int
): Bitmap? {
): Bitmap? = coroutineScope {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsolicited review:

coroutineScope { } is redundant. You are not launching any other coroutines there. suspend is enough.

Maybe if you have launched each blur region in separate coroutine then it would have been justified. Like

coroutineScope  { 
  for (i in 0 until iterate) {
     launch { 
          bitmap = RenderScriptToolkit.blur(
            inputBitmap = bitmap,
            radius = 25
          )
       }
  }
  
  }

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing out of it! I was testing on the bitmap transformation and forgetting to remove the scope before merging it. Will fix it soon later 👍

val iterate = (radius + 1) / 25
var bitmap: Bitmap? = RenderScriptToolkit.blur(
inputBitmap = androidBitmap,
Expand All @@ -332,5 +333,5 @@ internal fun iterativeBlur(
)
}

return bitmap
bitmap
}
14 changes: 8 additions & 6 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
[versions]
agp = "8.7.2"
kotlin = "2.0.21"
dokka = "1.9.20"
agp = "8.7.3"
kotlin = "2.1.0"
dokka = "2.0.0"
javaRelease = "11"
jvmTarget = "11"
kotlinxCoroutinesAndroid = "1.9.0"
material = "1.12.0"
nexusPlugin = "0.30.0"
binaryCompatibility = "0.16.3"
binaryCompatibility = "0.17.0"
androidxActivity = "1.9.3"
androidxCompose = "1.7.5"
androidxCompose = "1.8.0-alpha07"
androidxComposeConstraintLayout = "1.1.0"
androidxCore = "1.15.0"
androidxTest = "1.6.2"
androidxJunit = "1.2.1"
androidxMacroBenchmark = "1.3.3"
androidxProfileinstaller = "1.4.1"
androidxUiAutomator = "2.3.0"
landscapist = "2.4.2"
landscapist = "2.4.4"
spotless = "6.7.0"

[plugins]
Expand All @@ -30,6 +31,7 @@ spotless = { id = "com.diffplug.spotless", version.ref = "spotless" }
kotlinBinaryCompatibilityValidator = { id = "org.jetbrains.kotlinx.binary-compatibility-validator", version.ref = "binaryCompatibility" }

[libraries]
kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "kotlinxCoroutinesAndroid" }
material = { module = "com.google.android.material:material", version.ref = "material" }
androidx-compose-ui = { group = "androidx.compose.ui", name = "ui", version.ref = "androidxCompose" }
androidx-compose-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling", version.ref = "androidxCompose" }
Expand Down
Loading