Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielittner committed Jan 14, 2024
1 parent c163311 commit c7cc4e5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
26 changes: 14 additions & 12 deletions paparazzi/src/main/java/app/cash/paparazzi/Paparazzi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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

Expand Down
6 changes: 1 addition & 5 deletions paparazzi/src/test/java/app/cash/paparazzi/PaparazziTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}

0 comments on commit c7cc4e5

Please sign in to comment.