Profiling fix B: Catch Up cards decode their pictures at card size

Cards drew 1280-pixel pictures (about 3.7 MB each) at about 400 pixels; now decoded at 640 (inSampleSize 2).
Measured downstairs (8 November 2026, same key presses): Catch Up drawing thread 24.5/23.3 -> 22.3/21.3 ms median;
whole-frame time about the same (31/28 -> 30/30 ms).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-08 13:13:36 +00:00
co-authored by Claude Opus 5.5
parent a1cfc7c265
commit 869f147cdd
3 changed files with 41 additions and 3 deletions
@@ -306,7 +306,7 @@ private fun CatchUpTile(entry: CatchUpEntry, nowMs: Long, on: Boolean, session:
Column(modifier.background(if (on) Navy else Panel, corner).border(if (on) 3.dp else 0.dp, if (on) Yellow else Color.Transparent, corner)) {
Box(Modifier.fillMaxWidth().height(96.dp).clip(RoundedCornerShape(topStart = 8.dp, topEnd = 8.dp)).background(Deep)) {
val seriesId = remember(entry.channel, entry.show.startMs) { (entry.channel as? ScheduledChannel)?.seriesIdAt(entry.show.startMs) }
if (session != null) QueuedArtwork(session, entry.show.media.item.id, seriesId, Modifier.matchParentSize())
if (session != null) QueuedArtwork(session, entry.show.media.item.id, seriesId, Modifier.matchParentSize(), maxWidthPx = CATCH_UP_CARD_PICTURE_PX)
val mark = ChannelBrands.forNumber(entry.channel.number)
Box(Modifier.padding(6.dp).background(Navy.copy(alpha = .8f), RoundedCornerShape(4.dp)).padding(horizontal = 5.dp, vertical = 3.dp)) {
if (mark != null) Image(cachedPainter(mark.logo), mark.displayName, Modifier.width(56.dp).height(16.dp))
@@ -369,3 +369,6 @@ internal fun DriftWaiting(modifier: Modifier = Modifier) {
}
}
}
/** A Catch Up card's picture is about 400 pixels wide on the box; decoded no smaller than this (profiling fix B). */
internal const val CATCH_UP_CARD_PICTURE_PX = 480
@@ -78,13 +78,34 @@ internal fun ProgrammeArtwork(session: EmbySampleSession, channel: SampleChannel
}
}
/**
* Decodes a fetched picture no wider than it needs to be (profiling fix B, 8 November 2026): Catch Up's cards drew
* 1280-pixel pictures, about 3.7 MB each, at card size. Halving until the next halving would go below [maxWidthPx] keeps
* it sharp at that size and uses a quarter of the memory or less.
*/
internal fun artworkSampleSize(width: Int, maxWidthPx: Int?): Int {
if (maxWidthPx == null || maxWidthPx <= 0 || width <= 0) return 1
var sample = 1
while (width / (sample * 2) >= maxWidthPx) sample *= 2
return sample
}
internal fun decodeArtwork(bytes: ByteArray, maxWidthPx: Int?): android.graphics.Bitmap? {
if (maxWidthPx == null) return BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
BitmapFactory.decodeByteArray(bytes, 0, bytes.size, bounds)
return BitmapFactory.decodeByteArray(bytes, 0, bytes.size, BitmapFactory.Options().apply { inSampleSize = artworkSampleSize(bounds.outWidth, maxWidthPx) })
}
/** A small picture of what's queued next (Up next in the watching bar), undimmed; nothing while it loads or if there isn't one. */
@Composable
internal fun QueuedArtwork(session: EmbySampleSession, itemId: String, seriesId: String?, modifier: Modifier = Modifier) {
internal fun QueuedArtwork(session: EmbySampleSession, itemId: String, seriesId: String?, modifier: Modifier = Modifier,
/** The widest it's shown, in pixels; a smaller copy is decoded for a small card (profiling fix B). Null: as fetched. */
maxWidthPx: Int? = null) {
val context = LocalContext.current
val picture by produceState<ImageBitmap?>(null, itemId, seriesId) {
value = if (itemId.isEmpty()) null else withContext(Dispatchers.IO) {
runCatching { loadArtwork(ArtworkCache(Workspaces.current(context).artworkDir), itemId, seriesId, session::fetchArtwork)?.let { BitmapFactory.decodeByteArray(it, 0, it.size)?.asImageBitmap() } }.getOrNull()
runCatching { loadArtwork(ArtworkCache(Workspaces.current(context).artworkDir), itemId, seriesId, session::fetchArtwork)?.let { decodeArtwork(it, maxWidthPx)?.asImageBitmap() } }.getOrNull()
}
}
picture?.let { Image(it, null, modifier, contentScale = ContentScale.Crop) }
@@ -0,0 +1,14 @@
package tv.orbit.classic
import org.junit.Assert.assertEquals
import org.junit.Test
/** 8 November 2026 (profiling fix B): a card's picture is decoded smaller, never below the size it's shown. */
class ArtworkSampleSizeTest {
@Test fun halvesOnlyWhileStillWideEnough() {
assertEquals(2, artworkSampleSize(1280, CATCH_UP_CARD_PICTURE_PX)) // 640 px, shown at about 400
assertEquals(1, artworkSampleSize(900, CATCH_UP_CARD_PICTURE_PX)) // 450 would be too small
assertEquals(4, artworkSampleSize(1920, 480))
assertEquals(1, artworkSampleSize(1280, null)); assertEquals(1, artworkSampleSize(0, 480))
}
}