Skip to content

feat(core): pass the display's frame time to RequestAnimationFrame - #1068

Merged
houko merged 3 commits into
nsgo-dev:mainfrom
houko:feat/issue-1048-frame-time
Nov 21, 2026
Merged

houko merged 3 commits into
nsgo-dev:mainfrom
houko:feat/issue-1048-frame-time

Conversation

@houko

@houko houko commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator

Scope.RequestAnimationFrame now gets the display frame's time where the platform gives one, instead of the engine clock's time when the engine goroutine got to the frame. Hosts report it with Engine.FrameAt, which runs Scope.RunFrameAt; driver.DisplayTime reads the platform's stamp and its own clock together in the frame callback and moves time.Now by the difference, so the time keeps the display's cadence however late the frame reaches the engine. A frame's time never precedes the previous one's, and an engine with an app clock of its own (engine.Options.Clock, as tests use) keeps that clock's time, so nsgotest's Advance stays deterministic.

Each target's time: macOS and iOS the display link's targetTimestamp (macOS falls back to the graph clock while a timer stands in for the link), the web the requestAnimationFrame timestamp, Android Choreographer's frameTimeNanos (the Java runtime now sends it with FRAME; classes.dex and classes.jar rebuilt), Linux the GdkFrameClock's frame time, Windows when DwmFlush last returned, HarmonyOS displaySync's targetTimestamp, moved onto Go's clock by the bridge's quickest delivery seen (driver.DisplayClock), and watchOS the TimelineView's date. Mini programs keep their 16 ms timer and the graph clock, as the logic layer has no display frames.

Linux now ticks from the surface's GdkFrameClock (gtk_widget_add_tick_callback) instead of a 16 ms GLib timer, so it follows 120 and 144 Hz displays. While the frame clock stands still, as for a window not shown, a 100 ms watch ticks in its place so animations still end and the app's frames still come, as macOS's stall timer does.

docs/architecture/animation.md gets a table naming each target's frame source and what its time is, and its native-animation table now says Windows waits on DwmFlush and Linux ticks from the frame clock. The device acceptance (0.5 ms cadence on hardware, Linux at 120 Hz) is recorded in docs/roadmap.md, as only headless tests and builds ran.

Validation:

  • ubuild mise exec [email protected] -- go tool nsgo check -target all -jobs 12
  • rbuild mise exec [email protected] -- go tool nsgo check -target ios,ios_simulator,tvos,tvos_simulator,watchos,watchos_simulator -jobs 6
  • ubuild mise exec [email protected] -- go tool nsgo test ./core
  • ubuild mise exec [email protected] -- go tool nsgo test ./internal/engine
  • ubuild mise exec [email protected] -- go tool nsgo test ./internal/driver
  • ubuild mise exec [email protected] -- go tool nsgo test ./nsgotest
  • ubuild mise exec [email protected] -- go tool nsgo test ./internal/moduletest
  • ubuild mise exec [email protected] -- go tool nsgo test ./cmd/nsgo -- -run 'Harmony|Ets|Template'
  • mise exec java@21 -- env ANDROID_HOME=$HOME/Library/Android/sdk sh internal/platform/android/runtime/build.sh

Screenshots

None

Closes #1048

RequestAnimationFrame's time was the engine clock's when the engine goroutine got to the frame, so a loop stepping by the difference between two frames carried the engine's scheduling jitter. Hosts now report the display's time with Engine.FrameAt and Scope.RunFrameAt: the display link's targetTimestamp on macOS and iOS, the animation frame's timestamp on the web, Choreographer's frameTimeNanos on Android, the GdkFrameClock's frame time on Linux, DwmFlush's return on Windows, displaySync's targetTimestamp on HarmonyOS and the TimelineView's date on watchOS. driver.DisplayTime moves time.Now by the difference between the display's stamp and the platform clock read with it; a frame's time never precedes the previous one's, and an engine with its own clock keeps that clock's time, so nsgotest stays deterministic.

Linux ticks from the surface's GdkFrameClock rather than a 16 ms GLib timer, with a 100 ms watch ticking in its place while the frame clock stands still, as for a window not shown. The frame-clock tables in animation.md now name each target's source and time.

Closes nsgo-dev#1048
The core may now give a frame's time up to a frame ahead of the clock, as macOS's and iOS's display links tell when the frame shows, so performance.now() - ts went negative, which no browser does. TSX holds the timestamp it passes to JS to the clock's now; core keeps its semantics.
… when the platform clock may be read

The committed HarmonyOS host was not rebuilt and still calls nsgoFrame() without displaySync's time, so an app packaged without the OpenHarmony SDK gets the engine clock's. DisplayTime's doc and the animation architecture now say the platform clock may be read later on any goroutine, as Android reads it after the pipe, since the delay cancels out.
@github-actions github-actions Bot added the tsx label Nov 21, 2026
@houko
houko merged commit 41cc58d into nsgo-dev:main Nov 21, 2026
13 of 20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Frames: RequestAnimationFrame's time is the engine's, not the display's, and Linux ticks on a fixed 16 ms timer

1 participant