louiscad
05/05/2025, 9:55 PMImageComposeScene
renders one frame late, or just renders wrong?
I am using this composable where I call render
repeatedly on the same ImageComposeScene
, but with ever increasing nanoTime
values, and I only get the correct render if I call render
a second time, which is quite of a dirty workaround.
@Composable
fun JustFrameNumberComposable() {
val frameNumber by produceState(0L) {
while (true) withFrameMillis { frameTimeMillis ->
value = frameTimeMillis * 60L / 1000L
}
}
Text("Frame number: $frameNumber")
}
Alexander Maryanovsky
05/06/2025, 8:03 AMAlexander Maryanovsky
05/06/2025, 8:04 AMlouiscad
05/06/2025, 8:04 AMlouiscad
05/06/2025, 8:07 AMAlexander Maryanovsky
05/06/2025, 8:09 AMlouiscad
05/06/2025, 8:10 AMAlexander Maryanovsky
05/06/2025, 8:11 AMlouiscad
05/06/2025, 8:12 AMlouiscad
05/06/2025, 8:12 AMAlexander Maryanovsky
05/06/2025, 8:13 AMlouiscad
05/12/2025, 12:07 AMApplication.desktop.kt
?
private object YieldFrameClock : MonotonicFrameClock {
override suspend fun <R> withFrameNanos(
onFrame: (frameTimeNanos: Long) -> R
): R {
// We call `yield` to avoid blocking UI thread. If we don't call this then application
// can be frozen for the user in some cases as it will not receive any input events.
//
// Swing dispatcher will process all pending events and resume after `yield`.
yield()
return onFrame(System.nanoTime())
}
}
To me, the yield()
should come after onFrame(…)
in a finally
block, or after with the result of onFrame
being put in a local val
.Alexander Maryanovsky
05/12/2025, 6:45 AM