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.

Make animated covers lighter, smoother and free of the grid in their fade

Leloniod16731bff47b
diff --git a/app/src/main/java/dev/lelonio/square/ui/components/HeroBackdrop.kt b/app/src/main/java/dev/lelonio/square/ui/components/HeroBackdrop.kt
index a4547b5..da30197 100644
--- a/app/src/main/java/dev/lelonio/square/ui/components/HeroBackdrop.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/components/HeroBackdrop.kt
@@ -41,6 +41,7 @@ import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.CompositingStrategy
import androidx.compose.ui.graphics.graphicsLayer
+import androidx.compose.ui.graphics.drawscope.translate
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
@@ -73,6 +74,8 @@ fun HeroBackdrop(
modifier: Modifier = Modifier,
/** The cover as a moving picture, where there is one; see [MotionCover]. */
motionUrl: String? = null,
+ /** False holds the moving cover still where it is; see [MotionCover]'s playing. */
+ motionPlaying: Boolean = true,
/**
* Whether the picture is still being looked up.
*
@@ -237,6 +240,7 @@ fun HeroBackdrop(
transformOrigin = TransformOrigin(0.5f, 0f)
},
onFrame = motion::take,
+ playing = motionPlaying,
)
}
@@ -248,35 +252,61 @@ fun HeroBackdrop(
modifier = Modifier.fillMaxSize(),
) { shown ->
val blur = shown?.blur ?: return@Crossfade
+ val stops = if (fadeToPage) {
+ softening(softenFrom, softenTo, Color.Black)
+ } else if (extendPicture) {
+ softening(
+ 1f - SLOT_FADE - SLOT_BLUR_LEAD,
+ 1f - SLOT_FADE + SLOT_BLUR_TAIL,
+ Color.Black,
+ )
+ } else {
+ softening(
+ (softenFrom - 0.16f).coerceAtLeast(0f),
+ softenFrom + 0.03f,
+ Color.Black,
+ )
+ }
+ // Only the part the mask lets through is laid out and drawn.
+ //
+ // Above where the softening starts this copy is fully masked
+ // away, and drawing it there was work nobody saw: a moving cover
+ // redraws it with every frame, and the blur that smooths it is
+ // paid by the pixel. The box starts a little above the mask, by
+ // the blur's own reach, so the blur has the rows it reads from.
+ val shift = remember { floatArrayOf(0f) }
Box(
modifier = Modifier
.fillMaxSize()
+ .layout { measurable, constraints ->
+ val full = constraints.maxHeight
+ val top = ((full * stops.first().first) - SMOOTH_PX * 2).toInt().coerceIn(0, full)
+ shift[0] = top.toFloat()
+ val placeable = measurable.measure(
+ constraints.copy(minHeight = full - top, maxHeight = full - top),
+ )
+ layout(constraints.maxWidth, full) { placeable.place(0, top) }
+ }
+ .then(if (motionUrl != null) Modifier.smoothedUp() else Modifier)
// Compose the bitmap and its alpha mask in one shader:
// no header-sized offscreen render target on each redraw.
.drawWithCache {
val frame = motion.latest.value
- val stops = if (fadeToPage) {
- softening(softenFrom, softenTo, Color.Black)
- } else if (extendPicture) {
- softening(
- 1f - SLOT_FADE - SLOT_BLUR_LEAD,
- 1f - SLOT_FADE + SLOT_BLUR_TAIL,
- Color.Black,
- )
- } else {
- softening(
- (softenFrom - 0.16f).coerceAtLeast(0f),
- softenFrom + 0.03f,
- Color.Black,
- )
- }
+ val top = shift[0]
+ // Built for the whole height, as before, and drawn
+ // moved up by what this box starts below the top.
+ val whole = androidx.compose.ui.geometry.Size(size.width, size.height + top)
val brush = maskedPictureBrush(
frame?.soft ?: blur,
shown.zoom,
- size,
+ whole,
stops,
)
- onDrawBehind { drawRect(brush = brush) }
+ onDrawBehind {
+ translate(top = -top) {
+ drawRect(brush = brush, topLeft = androidx.compose.ui.geometry.Offset(0f, top), size = size)
+ }
+ }
},
)
}
@@ -505,8 +535,12 @@ private fun PictureExtension(
val image = shown?.edge ?: return@Crossfade
val imageZoom = shown.zoom
val extension = remember { ExtensionPicture() }
+ // Whether the cover is moving, not each frame of it: read in
+ // composition, the frames themselves would recompose this every time.
+ val moving by remember(motion) { androidx.compose.runtime.derivedStateOf { motion.latest.value != null } }
Box(
Modifier
+ .then(if (moving) Modifier.smoothedUp() else Modifier)
.fillMaxSize()
.drawBehind {
// The slot, in this box's terms: across the top, as wide as
@@ -785,25 +819,28 @@ private class MotionFrames {
/**
* About as soft as the still's blurred copy: that one is blurred by ten
- * pixels across two hundred and forty, and this is two passes of two
- * across forty-eight.
+ * pixels across two hundred and forty, and this is two passes of four
+ * across ninety-six.
*/
private fun softened(bitmap: android.graphics.Bitmap): android.graphics.Bitmap {
val width = bitmap.width
val height = bitmap.height
val pixels = IntArray(width * height)
bitmap.getPixels(pixels, 0, width, 0, 0, width, height)
- val scratch = IntArray(pixels.size)
+ val scratch = pixels.copyOf()
+ // Only the lower rows: this copy is shown under the cover's fade and
+ // masked away above it, so blurring the top was work nobody saw.
+ val firstRow = (height * SOFT_ROWS_FROM).toInt()
repeat(2) {
- boxBlur(pixels, scratch, width, height, horizontal = true)
- boxBlur(scratch, pixels, width, height, horizontal = false)
+ boxBlur(pixels, scratch, width, height, horizontal = true, firstRow = firstRow)
+ boxBlur(scratch, pixels, width, height, horizontal = false, firstRow = firstRow)
}
return android.graphics.Bitmap.createBitmap(pixels, width, height, android.graphics.Bitmap.Config.ARGB_8888)
}
- private fun boxBlur(from: IntArray, to: IntArray, width: Int, height: Int, horizontal: Boolean) {
+ private fun boxBlur(from: IntArray, to: IntArray, width: Int, height: Int, horizontal: Boolean, firstRow: Int = 0) {
val radius = FRAME_SOFTEN
- for (y in 0 until height) {
+ for (y in firstRow until height) {
for (x in 0 until width) {
var red = 0
var green = 0
@@ -825,11 +862,41 @@ private class MotionFrames {
}
}
-/** The radius of each pass of [MotionFrames]' blur, in the copy's pixels. */
-private const val FRAME_SOFTEN = 2
+/**
+ * A blur over what is drawn from [MotionFrames]' copies, on the GPU.
+ *
+ * Those copies are a few dozen pixels across and drawn the width of the
+ * screen, and stretched that far even a smooth picture shows the stretch: the
+ * shading between neighbouring pixels runs in straight lines, and where the
+ * lines meet the eye sees a grid of squares down the fade. Twice as many
+ * pixels only made the grid finer. Blurred by a little more than the grid's
+ * own size, the lines bend and the squares are gone. Android 12 and later;
+ * before that the copies are drawn as they are.
+ */
+private fun Modifier.smoothedUp(): Modifier =
+ if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
+ graphicsLayer {
+ renderEffect = androidx.compose.ui.graphics.BlurEffect(SMOOTH_PX, SMOOTH_PX, androidx.compose.ui.graphics.TileMode.Clamp)
+ }
+ } else {
+ this
+ }
+
+/** About one and a half of a copy's pixels, as drawn on a phone's screen. */
+private const val SMOOTH_PX = 20f
+
+/** Where in a frame [MotionFrames]' soft copy starts to be seen, with room to spare. */
+private const val SOFT_ROWS_FROM = 0.4f
+
+/** The radius of each pass of [MotionFrames]' blur, in the copy's pixels: as soft as before, at twice the width. */
+private const val FRAME_SOFTEN = 4
-/** How far each copy of a frame moves [MotionFrames]' blend towards itself. */
-private const val FRAME_FOLLOW = 0.2f
+/**
+ * How far each copy of a frame moves [MotionFrames]' blend towards itself: at
+ * thirty copies a second, a little each time, so the colours glide after the
+ * cover a fraction of a second behind instead of stepping.
+ */
+private const val FRAME_FOLLOW = 0.25f
/** The same cropped picture and DstIn fade, without an intermediate surface. */
private fun maskedPictureBrush(
diff --git a/app/src/main/java/dev/lelonio/square/ui/components/MotionCover.kt b/app/src/main/java/dev/lelonio/square/ui/components/MotionCover.kt
index c2c5c10..73dc11c 100644
--- a/app/src/main/java/dev/lelonio/square/ui/components/MotionCover.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/components/MotionCover.kt
@@ -7,6 +7,9 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
+import androidx.compose.ui.graphics.graphicsLayer
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.setValue
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.viewinterop.AndroidView
@@ -45,6 +48,13 @@ fun MotionCover(
* above them. Null reads nothing.
*/
onFrame: ((android.graphics.Bitmap) -> Unit)? = null,
+ /**
+ * False holds it on the frame it is at, rather than taking it away: behind
+ * a panel the cover stands still, and when the panel closes it carries on
+ * from there. Taken away and made again, it came back as a new video
+ * appearing at once over the still, with nothing in between.
+ */
+ playing: Boolean = true,
) {
val context = LocalContext.current
// The surface the frames are read from, once the view exists.
@@ -64,12 +74,34 @@ fun MotionCover(
// Stopped while the app is away. A loop nobody is looking at is a radio
// that costs battery and data for a picture on a screen that is off.
+ val wanted = rememberUpdatedState(playing)
+ LaunchedEffect(player, playing) { player.playWhenReady = playing }
+
+ // Faded in on its first frame rather than appearing in one: until then the
+ // still underneath is what shows, and the change from one to the other is
+ // the cover beginning to move, not a second picture arriving on top.
+ var firstFrame by remember(url) { mutableStateOf(false) }
+ DisposableEffect(player) {
+ val listener = object : Player.Listener {
+ override fun onRenderedFirstFrame() {
+ firstFrame = true
+ }
+ }
+ player.addListener(listener)
+ onDispose { player.removeListener(listener) }
+ }
+ val appear by androidx.compose.animation.core.animateFloatAsState(
+ targetValue = if (firstFrame) 1f else 0f,
+ animationSpec = androidx.compose.animation.core.tween(APPEAR_MS),
+ label = "motionAppear",
+ )
+
val lifecycle = LocalLifecycleOwner.current.lifecycle
DisposableEffect(lifecycle, player) {
val observer = LifecycleEventObserver { _, event ->
when (event) {
Lifecycle.Event.ON_STOP -> player.pause()
- Lifecycle.Event.ON_START -> player.play()
+ Lifecycle.Event.ON_START -> if (wanted.value) player.play()
else -> Unit
}
}
@@ -141,16 +173,23 @@ fun MotionCover(
it.player = null
surface[0] = null
},
- modifier = modifier,
+ modifier = modifier.graphicsLayer { alpha = appear },
)
}
+/** How long a moving cover takes to come up over its still. */
+private const val APPEAR_MS = 450
+
/**
* How wide the copies of the frames are read, and how often.
*
- * Small, because what is made of them is a blur and a band of colour, and a
- * read of a frame is a copy off the GPU. Often enough that the blur under a
- * cover in motion follows it rather than stepping after it.
+ * Small, because what is made of them is a blur and a band of colour. Not as
+ * small as it was: forty-eight across, stretched to the screen under the
+ * cover, showed as a grid of squares down the fade. And as often as the cover
+ * changes, about thirty times a second: read less often, the colours under it
+ * trailed the picture and stepped from one to the next. The cover only moves
+ * on the player's own screen (a panel stills it), so this is the one place it
+ * costs anything.
*/
-private const val FRAME_PX = 48
+private const val FRAME_PX = 96
private const val FRAME_EVERY_MS = 66L
diff --git a/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt b/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt
index 139fb41..263464d 100644
--- a/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt
@@ -66,6 +66,7 @@ import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
+import androidx.compose.ui.layout.boundsInWindow
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.draw.blur
import androidx.compose.ui.draw.clip
@@ -423,6 +424,26 @@ fun PlayerScreen(
val canvasBackdrop = rememberLayerBackdrop()
val glassBackdrop = rememberCombinedBackdrop(backdrop, canvasBackdrop)
+ // The panes below the clip only refract it when they are actually over it.
+ //
+ // The clip's layer covers the whole screen and is redrawn with every frame
+ // of the video, so every pane reading it redid its blur thirty times a
+ // second, including the title, the controls and the tabs, which on most
+ // phones sit below the clip, where the layer is empty. Measured rather
+ // than assumed: on a short screen the title does reach into the clip, and
+ // there it keeps refracting it.
+ var clipBottom by remember { mutableFloatStateOf(Float.MAX_VALUE) }
+ var lowerTop by remember { mutableFloatStateOf(0f) }
+ val lowerBackdrop = if (lowerTop >= clipBottom) backdrop else glassBackdrop
+
+ // The panes over a moving Apple cover read the page's own blurred ground
+ // instead of the cover. The cover is in the layer they refract, and every
+ // frame of it had that whole layer recorded again and every pane over it
+ // blurred again; behind this much glass the motion is only a shimmer, and
+ // it was costing the player half its frames.
+ val coverMoving = coverMotionUrl != null && canvas == null
+ val upperBackdrop = if (coverMoving) backdrop else glassBackdrop
+
// Lyrics take over the middle of the screen rather than opening a panel at
// the bottom, and the Canvas goes out of focus behind them: a clip is
// motion, and reading over motion is the one thing that does not work. Blur
@@ -554,6 +575,9 @@ fun PlayerScreen(
// picture underneath is what the travel shows instead, and
// at that size and speed the swap is invisible.
motionUrl = coverMotionUrl.takeIf { LocalPlayerSettled.current },
+ // And still behind a panel, as a Canvas is, where it stands
+ // rather than taken away; see MotionCover's playing.
+ motionPlaying = !panelOpen,
title = state.title,
pageColor = coverTone,
pending = coverPending,
@@ -687,6 +711,7 @@ fun PlayerScreen(
.fillMaxWidth()
.aspectRatio(DETAIL_ASPECT)
.align(Alignment.TopCenter)
+ .onGloballyPositioned { clipBottom = it.boundsInWindow().bottom }
.graphicsLayer { compositingStrategy = CompositingStrategy.Offscreen }
.drawWithContent {
drawContent()
@@ -718,7 +743,13 @@ fun PlayerScreen(
// a black rectangle sliding down the screen is not.
clip.isVideo && LocalGlassEnabled.current -> CanvasSurface(
url = clip.url,
- isPlaying = state.isPlaying,
+ // Still behind a panel. Out of focus and dimmed, the
+ // motion is barely there, and it was the most costly
+ // thing on the screen: every new frame of the clip had
+ // the whole screen blurred again and every pane of
+ // glass redrawn over it, which is what made the lyrics
+ // stutter. A still frame is blurred once.
+ isPlaying = state.isPlaying && !panelOpen,
onFirstFrame = { canvasReady = true },
// Only while this clip is still the one playing.
//
@@ -814,7 +845,7 @@ fun PlayerScreen(
.padding(horizontal = 20.dp),
) {
TopBar(
- backdrop = glassBackdrop,
+ backdrop = upperBackdrop,
panel = panel,
source = state.source,
onOpenSource = state.contextUri?.let { uri ->
@@ -939,7 +970,7 @@ fun PlayerScreen(
.then(androidx.compose.ui.Modifier)
) {
- KaraokeBadge(karaokeAmount, glassBackdrop) {
+ KaraokeBadge(karaokeAmount, upperBackdrop) {
panel = PlayerPanel.LYRICS
karaokeExpand++
}
@@ -1206,7 +1237,7 @@ fun PlayerScreen(
exit = fadeOut(tween(180)) + scaleOut(tween(180), targetScale = 0.92f),
) {
GlassSurface(
- backdrop = glassBackdrop,
+ backdrop = upperBackdrop,
surfaceColor = LocalPlayerFilm.current,
shape = RoundedCornerShape(50),
// The gap to the title lives here rather than
@@ -1246,11 +1277,12 @@ fun PlayerScreen(
// Title and artist on their own capsule, with the two
// per-track actions on the right.
GlassSurface(
- backdrop = glassBackdrop,
+ backdrop = lowerBackdrop,
surfaceColor = LocalPlayerFilm.current,
shape = RoundedCornerShape(50),
modifier = Modifier
.fillMaxWidth()
+ .onGloballyPositioned { lowerTop = it.boundsInWindow().top }
.sharedPill(sharedScope, animatedScope),
) {
Row(
@@ -1270,7 +1302,7 @@ fun PlayerScreen(
// official client's own radio button does it.
if (onRadio != null) {
RoundGlassButton(
- backdrop = glassBackdrop,
+ backdrop = lowerBackdrop,
size = 40.dp,
onClick = onRadio,
) {
@@ -1288,7 +1320,7 @@ fun PlayerScreen(
// it, which is why it sits here and not in the
// segmented switch below.
RoundGlassButton(
- backdrop = glassBackdrop,
+ backdrop = lowerBackdrop,
size = 40.dp,
onClick = {
panel = if (panel == PlayerPanel.QUEUE) {
@@ -1312,7 +1344,7 @@ fun PlayerScreen(
if (playlistEditAvailable) {
Spacer(Modifier.size(8.dp))
RoundGlassButton(
- backdrop = glassBackdrop,
+ backdrop = lowerBackdrop,
size = 40.dp,
onClick = {
// A tick opens the lists, where it can
@@ -1391,7 +1423,7 @@ fun PlayerScreen(
Controls(
state = state,
- backdrop = glassBackdrop,
+ backdrop = lowerBackdrop,
onTogglePlay = onTogglePlay,
onNext = onNext,
onPrevious = onPrevious,
@@ -1421,7 +1453,7 @@ fun PlayerScreen(
onApplyPreset = onApplyPreset,
onSavePreset = onSavePreset,
onDeletePreset = onDeletePreset,
- backdrop = glassBackdrop,
+ backdrop = lowerBackdrop,
)
Spacer(Modifier.height(20.dp))