Skip to content

Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.

Blur the page backdrop at decode time

Leloniob817d573a149
diff --git a/.claude/settings.local.json b/.claude/settings.local.json
index 779832c..7fd96f3 100644
--- a/.claude/settings.local.json
+++ b/.claude/settings.local.json
@@ -85,7 +85,8 @@
"Bash(git -c user.name=Emanuele -c [email protected] commit -qm 'Play through Spotify Connect *)",
"Bash(./gradlew :app:compileDevKotlin)",
"Bash(git -c user.name=Emanuele -c [email protected] commit -qm 'Add the Connect device picker to the player *)",
- "Bash(git -c user.name=Emanuele -c [email protected] commit -qm 'Cut the cost of expanding the player *)"
+ "Bash(git -c user.name=Emanuele -c [email protected] commit -qm 'Cut the cost of expanding the player *)",
+ "Bash(git -c user.name=Emanuele -c [email protected] commit -qm 'Blur the page backdrop at decode time *)"
]
}
}
diff --git a/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/configure_fingerprint.bin b/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/configure_fingerprint.bin
index 61c29f9..25cd0f1 100644
--- a/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/configure_fingerprint.bin
+++ b/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/configure_fingerprint.bin
@@ -2,27 +2,27 @@ C/C++ Structured Logl
j
h/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/additional_project_files.txtC
A
-?com.android.build.gradle.internal.cxx.io.EncodedFileFingerPrint  �����3  ����3i
+?com.android.build.gradle.internal.cxx.io.EncodedFileFingerPrint  �����3  ����3i
g
-e/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/android_gradle_build.json  �����3� ����3n
+e/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/android_gradle_build.json  �����3� ����3n
l
-j/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/android_gradle_build_mini.json  �����3� �����3[
+j/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/android_gradle_build_mini.json  �����3� �����3[
Y
-W/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/build.ninja  �����3�� ����3_
+W/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/build.ninja  �����3�� ����3_
]
-[/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/build.ninja.txt  �����3d
+[/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/build.ninja.txt  �����3d
b
-`/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/build_file_index.txt  �����3 @ ����3e
+`/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/build_file_index.txt  �����3 @ ����3e
c
-a/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/compile_commands.json  �����3�� ����3i
+a/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/compile_commands.json  �����3�� ����3i
g
-e/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/compile_commands.json.bin  �����3 �" ����3o
+e/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/compile_commands.json.bin  �����3 �" ����3o
m
-k/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/metadata_generation_command.txt  �����3
+k/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/metadata_generation_command.txt  �����3
� ����3b
`
-^/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/prefab_config.json  �����3  ( ����3g
+^/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/prefab_config.json  �����3  ( ����3g
e
-c/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/symbol_folder_index.txt  �����3  b ����3D
+c/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/symbol_folder_index.txt  �����3  b ����3D
B
-@/Users/emanuele/Progetto Spotify/app/src/main/cpp/CMakeLists.txt  �����3 � �Ȅ��3
\ No newline at end of file
+@/Users/emanuele/Progetto Spotify/app/src/main/cpp/CMakeLists.txt  �����3 � �Ȅ��3
\ No newline at end of file
diff --git a/app/src/main/java/dev/emanuele/spot/ui/SpotApp.kt b/app/src/main/java/dev/emanuele/spot/ui/SpotApp.kt
index aec5006..1658574 100644
--- a/app/src/main/java/dev/emanuele/spot/ui/SpotApp.kt
+++ b/app/src/main/java/dev/emanuele/spot/ui/SpotApp.kt
@@ -69,6 +69,7 @@ import androidx.navigation.compose.rememberNavController
import androidx.compose.ui.platform.LocalContext
import coil.compose.AsyncImage
import coil.request.ImageRequest
+import dev.emanuele.spot.ui.components.BlurTransformation
import com.kyant.backdrop.Backdrop
import com.kyant.backdrop.backdrops.layerBackdrop
import com.kyant.backdrop.backdrops.rememberLayerBackdrop
@@ -117,9 +118,13 @@ private val BottomBarHeight = 62.dp
/**
* Decode size of the page backdrop, in pixels.
*
- * Small enough that stretching it across the window *is* the blur.
+ * Large enough that the blur has something to work with, small enough that
+ * blurring it costs nothing and it never reads as a photograph.
*/
-private const val BACKDROP_DECODE_PX = 32
+private const val BACKDROP_DECODE_PX = 128
+
+/** Radius in pixels of the decoded image; see [BlurTransformation]. */
+private val BackdropBlur = BlurTransformation(radius = 14, passes = 2)
@UnstableApi
@Composable
@@ -470,16 +475,19 @@ private fun AppBackdrop(artworkUrl: String?) {
.background(Color(0xFF0A0A0C)),
) {
if (artworkUrl != null) {
- // Softened by decoding it tiny and letting the upscale do the work,
- // not by `Modifier.blur`. An 80dp blur over the whole window is a
- // RenderEffect on a full-screen layer, re-run whenever that layer
- // changes — which during the player's expansion is every frame, and
- // was a large part of why it stuttered. A 32px bitmap stretched to
- // the window is the same picture for nothing.
+ // Blurred at decode time rather than by `Modifier.blur`. That
+ // modifier is a RenderEffect over the whole window, re-run whenever
+ // the layer changes — every frame while the player expands, which is
+ // most of what made it stutter. Stretching a 32px bitmap instead was
+ // free and looked it: bilinear upscaling from that size shows square
+ // blocks, not a wash. This does the blur once per cover, on a small
+ // bitmap, and Coil caches it.
AsyncImage(
model = ImageRequest.Builder(LocalContext.current)
.data(artworkUrl)
.size(BACKDROP_DECODE_PX)
+ .transformations(BackdropBlur)
+ .allowHardware(false)
.crossfade(true)
.build(),
contentDescription = null,
diff --git a/app/src/main/java/dev/emanuele/spot/ui/components/BlurTransformation.kt b/app/src/main/java/dev/emanuele/spot/ui/components/BlurTransformation.kt
new file mode 100644
index 0000000..a331869
--- /dev/null
+++ b/app/src/main/java/dev/emanuele/spot/ui/components/BlurTransformation.kt
@@ -0,0 +1,116 @@
+package dev.emanuele.spot.ui.components
+
+import android.graphics.Bitmap
+import coil.size.Size
+import coil.transform.Transformation
+
+/**
+ * Blurs an image while it is being decoded.
+ *
+ * The page backdrop went through two wrong versions before this one. First
+ * `Modifier.blur` at 80dp: correct-looking, but a RuntimeEffect over the whole
+ * window re-run on every frame the layer changed, which is most of what made
+ * expanding the player stutter. Then a 32px bitmap stretched to the window,
+ * which costs nothing and looks it — bilinear upscaling from that size leaves
+ * visible square blocks rather than a wash.
+ *
+ * Blurring at decode time is the version that is both: the work happens once
+ * per cover, on a small bitmap, and Coil caches the result. What reaches the
+ * screen is genuinely smooth, and drawing it is an ordinary image draw.
+ *
+ * A box blur run twice rather than a gaussian: two box passes approximate a
+ * gaussian closely enough that nobody can tell on an image that is about to be
+ * stretched thirty times, and it is a handful of adds per pixel.
+ *
+ * @param radius in pixels of the *decoded* image, not of the screen.
+ */
+class BlurTransformation(private val radius: Int = 12, private val passes: Int = 2) :
+ Transformation {
+
+ override val cacheKey: String = "${javaClass.name}-$radius-$passes"
+
+ override suspend fun transform(input: Bitmap, size: Size): Bitmap {
+ val width = input.width
+ val height = input.height
+ if (width <= 1 || height <= 1) return input
+
+ val pixels = IntArray(width * height)
+ input.getPixels(pixels, 0, width, 0, 0, width, height)
+
+ val scratch = IntArray(width * height)
+ repeat(passes) {
+ boxBlurHorizontal(pixels, scratch, width, height, radius)
+ boxBlurVertical(scratch, pixels, width, height, radius)
+ }
+
+ // ARGB_8888 rather than the input's config: a hardware bitmap cannot be
+ // read back, and Coil hands those out unless told otherwise.
+ return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888)
+ }
+
+ private fun boxBlurHorizontal(
+ source: IntArray,
+ target: IntArray,
+ width: Int,
+ height: Int,
+ radius: Int,
+ ) {
+ for (y in 0 until height) {
+ val row = y * width
+ for (x in 0 until width) {
+ var a = 0
+ var r = 0
+ var g = 0
+ var b = 0
+ var count = 0
+ // Clamped at the edges, which repeats the border pixel. On an
+ // image this blurred, the alternative — wrapping — would drag
+ // one side's colour onto the other.
+ for (offset in -radius..radius) {
+ val sample = source[row + (x + offset).coerceIn(0, width - 1)]
+ a += (sample ushr 24) and 0xFF
+ r += (sample ushr 16) and 0xFF
+ g += (sample ushr 8) and 0xFF
+ b += sample and 0xFF
+ count++
+ }
+ target[row + x] = pack(a / count, r / count, g / count, b / count)
+ }
+ }
+ }
+
+ private fun boxBlurVertical(
+ source: IntArray,
+ target: IntArray,
+ width: Int,
+ height: Int,
+ radius: Int,
+ ) {
+ for (x in 0 until width) {
+ for (y in 0 until height) {
+ var a = 0
+ var r = 0
+ var g = 0
+ var b = 0
+ var count = 0
+ for (offset in -radius..radius) {
+ val sample = source[(y + offset).coerceIn(0, height - 1) * width + x]
+ a += (sample ushr 24) and 0xFF
+ r += (sample ushr 16) and 0xFF
+ g += (sample ushr 8) and 0xFF
+ b += sample and 0xFF
+ count++
+ }
+ target[y * width + x] = pack(a / count, r / count, g / count, b / count)
+ }
+ }
+ }
+
+ private fun pack(a: Int, r: Int, g: Int, b: Int): Int =
+ (a shl 24) or (r shl 16) or (g shl 8) or b
+
+ override fun equals(other: Any?): Boolean =
+ other is BlurTransformation && other.radius == radius && other.passes == passes
+
+ override fun hashCode(): Int = cacheKey.hashCode()
+}