From 50ac9f988630033f5fd0cc987f571dba3d22c878 Mon Sep 17 00:00:00 2001 From: kalarina Date: Tue, 3 Nov 2026 13:47:51 +0200 Subject: [PATCH] Decode station logos directly at full size --- .../fogwave/ui/components/StationLogo.kt | 78 ++++++++++++------- 1 file changed, 50 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/cc/skysparkle/fogwave/ui/components/StationLogo.kt b/app/src/main/java/cc/skysparkle/fogwave/ui/components/StationLogo.kt index 42ad78b..46e1990 100644 --- a/app/src/main/java/cc/skysparkle/fogwave/ui/components/StationLogo.kt +++ b/app/src/main/java/cc/skysparkle/fogwave/ui/components/StationLogo.kt @@ -1,17 +1,24 @@ package cc.skysparkle.fogwave.ui.components +import android.content.res.Resources +import android.graphics.BitmapFactory +import androidx.annotation.DrawableRes +import androidx.compose.foundation.Image import androidx.compose.runtime.Composable import androidx.compose.runtime.ReadOnlyComposable -import androidx.compose.runtime.remember +import androidx.compose.runtime.getValue +import androidx.compose.runtime.produceState import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.ImageBitmap +import androidx.compose.ui.graphics.asImageBitmap import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.platform.LocalContext import cc.skysparkle.fogwave.data.model.RadioStation import cc.skysparkle.fogwave.ui.theme.LocalGeoColors -import coil.compose.AsyncImage -import coil.request.ImageRequest -import coil.size.Size +import java.util.concurrent.ConcurrentHashMap +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext private val LightLogoBackdrop = Color(0xFFECE8F1) @@ -21,34 +28,49 @@ private val LightLogoBackdrop = Color(0xFFECE8F1) fun stationTileColor(station: RadioStation): Color = if (station.lightBackdrop) LightLogoBackdrop else LocalGeoColors.current.surfaceContainer -// Decoded off the main thread and kept in Coil's memory cache, so no logo is decoded during a frame. +// Station logos are small bundled files, so each is decoded once, at its full size and off the main +// thread, and the same bitmap is shared by the station list, the mini player and the now-playing sheet. +// They are read straight from the resource with density scaling off: going through an image loader's +// resource fetcher let release builds decode them a few pixels wide, which showed as a smear. +private object StationLogoCache { + private val bitmaps = ConcurrentHashMap() + + fun get(@DrawableRes resId: Int): ImageBitmap? = bitmaps[resId] + + fun load(resources: Resources, @DrawableRes resId: Int): ImageBitmap? { + bitmaps[resId]?.let { return it } + val options = BitmapFactory.Options().apply { inScaled = false } + val bitmap = resources.openRawResource(resId).use { input -> + BitmapFactory.decodeStream(input, null, options) + } ?: return null + return bitmap.asImageBitmap().also { bitmaps[resId] = it } + } +} + @Composable +private fun rememberStationLogo(@DrawableRes resId: Int): ImageBitmap? { + val resources = LocalContext.current.resources + val logo by produceState(StationLogoCache.get(resId), resources, resId) { + value = StationLogoCache.get(resId) + ?: withContext(Dispatchers.IO) { StationLogoCache.load(resources, resId) } + } + return logo +} + +@Composable fun StationLogoImage(station: RadioStation, modifier: Modifier = Modifier) { - AsyncImage( - model = station.iconResId, - contentDescription = null, - contentScale = ContentScale.Crop, - modifier = modifier - ) + val logo = rememberStationLogo(station.iconResId) + if (logo != null) { + Image( + bitmap = logo, + contentDescription = null, + contentScale = ContentScale.Crop, + modifier = modifier + ) + } } -// Player artwork is decoded at full size under its own cache key: the mini player and the -// now-playing sheet share one decode, which the station list never evicts, and show it without a fade. @Composable fun StationArtwork(station: RadioStation, modifier: Modifier = Modifier) { - val context = LocalContext.current - val request = remember(context, station.iconResId) { - ImageRequest.Builder(context) - .data(station.iconResId) - .size(Size.ORIGINAL) - .memoryCacheKey("artwork_${station.iconResId}") - .crossfade(false) - .build() - } - AsyncImage( - model = request, - contentDescription = null, - contentScale = ContentScale.Crop, - modifier = modifier - ) + StationLogoImage(station, modifier) }