Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import dev.enro.NavigationContainer
import dev.enro.NavigationKey
import dev.enro.platform.EnroLog
import dev.enro.requestClose
import dev.enro.ui.animation.NonDecreasingFrameTime
import dev.enro.ui.scenes.DialogSceneStrategy
import dev.enro.ui.scenes.DirectOverlaySceneStrategy
import dev.enro.ui.scenes.OverlayTransitions
Expand Down Expand Up @@ -183,6 +184,9 @@ public fun NavigationDisplay(
// Create the transition state that manages animations between scenes
val transitionState = remember { SeekableTransitionState(sceneFrame) }
val transition = rememberTransition(transitionState, label = "scene")
// Shared across every place that drives transitionState, so the frame time can never
// regress between phases of one transition (see NonDecreasingFrameTime for why).
val transitionFrameTime = remember { NonDecreasingFrameTime() }

// Track entries from the transition's current state for isPop detection
// (like NavDisplay's transitionCurrentStateEntries)
Expand Down Expand Up @@ -243,14 +247,18 @@ public fun NavigationDisplay(
// During predictive back, seek to the previous scene based on gesture progress
if (transition.currentState != previousSceneFrame) {
LaunchedEffect(previousSceneFrame, progress) {
transitionState.seekTo(progress, previousSceneFrame)
transitionFrameTime.runPreventingFrameTimeRegression {
transitionState.seekTo(progress, previousSceneFrame)
}
}
}
} else {
LaunchedEffect(sceneFrame) {
if (transitionState.currentState != sceneFrame) {
// Animate to the new scene
transitionState.animateTo(sceneFrame)
transitionFrameTime.runPreventingFrameTimeRegression {
transitionState.animateTo(sceneFrame)
}
} else {
// Predictive back has either been completed or cancelled
// so now we need to seekTo+snapTo the final state
Expand All @@ -276,13 +284,15 @@ public fun NavigationDisplay(
animationSpec = tween(remainingDuration),
) { value, _ ->
this@LaunchedEffect.launch {
if (value != finalFraction) {
// Seek the transition towards the finalFraction
transitionState.seekTo(value)
}
if (value == finalFraction) {
// Once the animation finishes, we need to snap to the right state.
transitionState.snapTo(sceneFrame)
transitionFrameTime.runPreventingFrameTimeRegression {
if (value != finalFraction) {
// Seek the transition towards the finalFraction
transitionState.seekTo(value)
}
if (value == finalFraction) {
// Once the animation finishes, we need to snap to the right state.
transitionState.snapTo(sceneFrame)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package dev.enro.ui.animation

import androidx.compose.runtime.MonotonicFrameClock
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.withContext

/**
* Guards animation code against the frame time going backwards.
*
* Some devices (observed on Huawei and Honor) occasionally deliver a Choreographer frame time
* that is earlier than the previous frame's. [androidx.compose.animation.core.SeekableTransitionState]
* does not tolerate this: while animating a transition whose total duration is zero (e.g. a scene
* change with no enter/exit animations), a negative frame delta produces a NaN fraction and the
* next seek crashes with "IllegalArgumentException: Cannot round NaN value".
*
* [runPreventingFrameTimeRegression] runs [block] with a [MonotonicFrameClock] that clamps frame
* times to be non-decreasing. The clamp state is shared across every invocation on the same
* instance, so a regression between two phases of the same transition (e.g. a predictive-back
* seek handing over to the settle animation) is caught too.
*
* See https://issuetracker.google.com/issues/540477169 and
* https://github.com/arkivanov/Decompose/issues/1012 for the upstream reports.
*/
internal class NonDecreasingFrameTime {

private var lastFrameTimeNanos = Long.MIN_VALUE

internal suspend fun <R> runPreventingFrameTimeRegression(
block: suspend CoroutineScope.() -> R,
): R {
val clock = currentCoroutineContext()[MonotonicFrameClock]
?: return coroutineScope(block)
return withContext(NonDecreasingFrameClock(clock), block)
}

private inner class NonDecreasingFrameClock(
private val delegate: MonotonicFrameClock,
) : MonotonicFrameClock {
override suspend fun <R> withFrameNanos(onFrame: (frameTimeNanos: Long) -> R): R {
return delegate.withFrameNanos { frameTimeNanos ->
val time = maxOf(frameTimeNanos, lastFrameTimeNanos)
lastFrameTimeNanos = time
onFrame(time)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package dev.enro.ui.animation

import androidx.compose.runtime.MonotonicFrameClock
import androidx.compose.runtime.withFrameNanos
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.withContext
import kotlin.test.Test
import kotlin.test.assertEquals

class NonDecreasingFrameTimeTests {

/** Hands out a scripted sequence of frame times, including regressions. */
private class ScriptedFrameClock(private val frameTimes: List<Long>) : MonotonicFrameClock {
private var index = 0

override suspend fun <R> withFrameNanos(onFrame: (frameTimeNanos: Long) -> R): R {
return onFrame(frameTimes[index++])
}
}

private suspend fun NonDecreasingFrameTime.observeFrames(count: Int): List<Long> {
val observed = mutableListOf<Long>()
runPreventingFrameTimeRegression {
repeat(count) {
withFrameNanos { frameTimeNanos -> observed.add(frameTimeNanos) }
}
}
return observed
}

@Test
fun monotonicFrameTimesArePassedThroughUnchanged() = runTest {
val frameTime = NonDecreasingFrameTime()
val observed = withContext(ScriptedFrameClock(listOf(100L, 200L, 300L))) {
frameTime.observeFrames(3)
}
assertEquals(listOf(100L, 200L, 300L), observed)
}

@Test
fun aFrameTimeRegressionIsClampedToTheLastSeenFrameTime() = runTest {
val frameTime = NonDecreasingFrameTime()
// The 150L frame goes backwards relative to 200L; it must be clamped so the animation
// never sees a negative frame delta (the "Cannot round NaN value" crash).
val observed = withContext(ScriptedFrameClock(listOf(100L, 200L, 150L, 300L))) {
frameTime.observeFrames(4)
}
assertEquals(listOf(100L, 200L, 200L, 300L), observed)
}

@Test
fun theClampIsSharedAcrossSeparateInvocations() = runTest {
val frameTime = NonDecreasingFrameTime()
// Two invocations model two phases of one transition (e.g. a predictive-back seek
// handing over to the settle animation); a regression across the boundary is clamped too.
val first = withContext(ScriptedFrameClock(listOf(100L, 200L))) {
frameTime.observeFrames(2)
}
val second = withContext(ScriptedFrameClock(listOf(150L, 300L))) {
frameTime.observeFrames(2)
}
assertEquals(listOf(100L, 200L), first)
assertEquals(listOf(200L, 300L), second)
}

@Test
fun runsWithoutAFrameClockInTheContext() = runTest {
val frameTime = NonDecreasingFrameTime()
var ran = false
frameTime.runPreventingFrameTimeRegression { ran = true }
assertEquals(true, ran)
}
}
Loading