From c7cc4e509783d9f63be13c7c0d3cb617d3b54e4b Mon Sep 17 00:00:00 2001 From: Gabriel Ittner Date: Sun, 14 Jan 2024 02:51:01 +0100 Subject: [PATCH] wip --- .../main/java/app/cash/paparazzi/Paparazzi.kt | 26 ++++++++++--------- .../java/app/cash/paparazzi/PaparazziTest.kt | 6 +---- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/paparazzi/src/main/java/app/cash/paparazzi/Paparazzi.kt b/paparazzi/src/main/java/app/cash/paparazzi/Paparazzi.kt index 69bbe7c8db..747b56ee4a 100644 --- a/paparazzi/src/main/java/app/cash/paparazzi/Paparazzi.kt +++ b/paparazzi/src/main/java/app/cash/paparazzi/Paparazzi.kt @@ -22,12 +22,8 @@ import android.graphics.Bitmap import android.os.Handler_Delegate import android.util.AttributeSet import android.util.DisplayMetrics -import android.view.BridgeInflater -import android.view.Choreographer_Delegate -import android.view.LayoutInflater -import android.view.View +import android.view.* import android.view.View.NO_ID -import android.view.ViewGroup import androidx.activity.setViewTreeOnBackPressedDispatcherOwner import androidx.annotation.LayoutRes import androidx.compose.runtime.Composable @@ -286,11 +282,9 @@ class Paparazzi @JvmOverloads constructor( // System_Delegate.nanoTime() is nanoTime - previousNanoTime + sChoreographerTime. // Because of that setNanosTime needs to be called with 0 first and because sChoreographerTime // is only allowed to go up, for the second call the negated sChoreographerTime is used to - // achieve a nanoTime() of 0. - val initialChoreographerNanos = Choreographer_Delegate.sChoreographerTime - System_Delegate.setNanosTime(0L) - System_Delegate.setNanosTime(-initialChoreographerNanos) + // achieve a nanoTime() of TIME_OFFSET_NANOS. try { + withTime(-Choreographer_Delegate.sChoreographerTime) {} if (hasComposeRuntime) { // During onAttachedToWindow, AbstractComposeView will attempt to resolve its parent's // CompositionContext, which requires first finding the "content view", then using that @@ -320,7 +314,7 @@ class Paparazzi @JvmOverloads constructor( viewGroup.addView(modifiedView) for (frame in 0 until frameCount) { - val nowNanos = initialChoreographerNanos + (startNanos + (frame * 1_000_000_000.0 / fps)).toLong() + val nowNanos = (startNanos + (frame * 1_000_000_000.0 / fps)).toLong() withTime(nowNanos) { val result = renderSession.render(true) if (result.status == ERROR_UNKNOWN) { @@ -351,14 +345,19 @@ class Paparazzi @JvmOverloads constructor( timeNanos: Long, block: () -> Unit ) { + println("Time is currently ${System_Delegate.nanoTime()}") + println("Time nanos is $timeNanos") + System_Delegate.setNanosTime(0L) + System_Delegate.setNanosTime(timeNanos) + println("Time is now ${System_Delegate.nanoTime()}") // Execute the block at the requested time. // doFrame() will update the current time returned by System_Delegate.nanoTime(). - Choreographer_Delegate.doFrame(timeNanos) + Choreographer_Delegate.doCallbacks(Choreographer.getInstance(), 1, timeNanos) // This will execute callbacks scheduled at timeNanos. // Using Choreographer_Delegate.doCallbacks() would execute them a second time // if the time is advanced again. Using timeNanos + 1 immediately would result in // the previous time being used. - Choreographer_Delegate.doFrame(timeNanos + 1) +// Choreographer_Delegate.doFrame(timeNanos + 1) executeHandlerCallbacks() block() @@ -608,6 +607,9 @@ class Paparazzi @JvmOverloads constructor( } companion object { + /** The choreographer doesn't like 0 as a frame time, so start an hour later. */ + internal val TIME_OFFSET_NANOS = TimeUnit.HOURS.toNanos(0L) + internal lateinit var renderer: Renderer internal val isInitialized get() = ::renderer.isInitialized diff --git a/paparazzi/src/test/java/app/cash/paparazzi/PaparazziTest.kt b/paparazzi/src/test/java/app/cash/paparazzi/PaparazziTest.kt index dce05d12ca..b10faeefaa 100644 --- a/paparazzi/src/test/java/app/cash/paparazzi/PaparazziTest.kt +++ b/paparazzi/src/test/java/app/cash/paparazzi/PaparazziTest.kt @@ -107,16 +107,12 @@ class PaparazziTest { "onAnimationStart time=2000 animationElapsed=0.0", "onAnimationUpdate time=2000 animationElapsed=0.0", "onDraw time=2000 animationElapsed=0.0", - "onAnimationUpdate time=2000 animationElapsed=0.0", "onAnimationUpdate time=2250 animationElapsed=0.25", "onDraw time=2250 animationElapsed=0.25", - "onAnimationUpdate time=2250 animationElapsed=0.25", "onAnimationUpdate time=2500 animationElapsed=0.5", "onDraw time=2500 animationElapsed=0.5", - "onAnimationUpdate time=2500 animationElapsed=0.5", "onAnimationUpdate time=2750 animationElapsed=0.75", "onDraw time=2750 animationElapsed=0.75", - "onAnimationUpdate time=2750 animationElapsed=0.75", "onAnimationUpdate time=3000 animationElapsed=1.0", "onAnimationEnd time=3000 animationElapsed=1.0", "onDraw time=3000 animationElapsed=1.0" @@ -224,6 +220,6 @@ class PaparazziTest { private val time: Long get() { - return TimeUnit.NANOSECONDS.toMillis(System_Delegate.nanoTime()) + return TimeUnit.NANOSECONDS.toMillis(System_Delegate.nanoTime() - Paparazzi.TIME_OFFSET_NANOS) } }