Skip to content

[Skia] Drive the frame clock from native vsync timestamps #24962

Open

Description

@MartinZikmund

What would you like to be added? 💡

Skia hosts should pass the native vsync timestamp for each frame into CompositionTarget.OnNativePlatformFrameRequested. The frame clock introduced in #24735 would then use it as the frame time, instead of reading Stopwatch later on the UI thread and snapping it onto an estimated grid.

That is how the frameworks we compare against get their frame time:

Framework Frame time source
WinUI 3 The compositor scheduler thread wakes on vsync, ticks RefreshAlignedClock, and only then posts the UI tick (compositorscheduler.cpp RenderThreadFrame). The UI thread reads the stored value (TimeManager::GetLastTickTime), so dispatcher latency never enters it.
Flutter The engine passes the platform vsync time into SchedulerBinding.handleBeginFrame(rawTimeStamp).
Android Choreographer.FrameCallback.doFrame(frameTimeNanos).
Browsers The requestAnimationFrame callback argument, shared by every callback in the frame.
Avalonia MediaContext reads a stopwatch inside a dispatcher job. It has the same jitter problem Uno has today.

Proposed design

  1. Plumbing (Uno.UI)
    • Add an optional frame time parameter to OnNativePlatformFrameRequested (CompositionTarget.RenderScheduling.cs). It should be in Compositor.TimestampInTicks units (Stopwatch.GetTimestamp() * s_tickFrequency).
    • Agree on one meaning for it. Preferred: the target present time of the frame being produced (CADisplayLink targetTimestamp, Flutter's convention). Hosts that only know the last vblank add one refresh period.
    • Store it on the render thread, next to _lastNativeFrameTimestamp. SampleFrameTimestamp() (CompositionTarget.FrameClock.cs) consumes it when present, and must keep it monotonic (Math.Max with the previous value).
    • When it is present, bypass the FrameClock grid snap. Feed FrameClock's interval estimate from the native timestamps, or from a host-reported refresh period.
    • Hosts without a timestamp keep today's behaviour.
    • Every existing call site passes arguments positionally, so a trailing optional parameter doesn't break any host.
  2. Cheap interim step (all hosts): sample the fallback time on the render thread in OnNativePlatformFrameRequested, where _lastNativeFrameTimestamp is already taken. Today it is sampled after the NativeDispatcher.Main.EnqueueRender hop in EnqueueRenderCallback. This removes the dispatcher queuing delay from every host, even before any native timestamp exists.
  3. Per-host work (from a survey of the hosts at the head of feat(composition): Tick per-frame motion from a frame clock #24735):
Host / backend Pacing today Native timestamp Work
iOS/tvOS Metal + WebGPU CADisplayLink on a dedicated run-loop thread Available but dropped. CADisplayLink.Create(() => this.Draw()) ignores the link (UnoMetalView.cs:35, UnoWebGpuMetalView.cs:39). Read _link.TargetTimestamp in the callback and pass it through RootViewController.OnFrameRequested. Convert CACurrentMediaTime seconds to Stopwatch ticks through a one-time offset sample. Smallest change.
WebAssembly (WebGL / WebGPU / software) requestAnimationFrame on the main thread Available but dropped. window.requestAnimationFrame(() => instance.requestRender()) (ts/Runtime/BrowserRenderer.ts:48) ignores the timestamp. Pass the rAF timestamp through requestRender and the [JSExport] RenderFrame (BrowserRenderer.cs:106). Map the performance.now() timeline to Stopwatch ticks with an offset.
Linux framebuffer DRM/GLES DRM page-flip event (true vblank) Available but dropped. OnPageFlip(fd, sequence, tv_sec, tv_usec, ...) (DRMRenderer.cs:364). Thread tv_sec/tv_usec + one period through OnPageFlipCore → Render. Query DRM_CAP_TIMESTAMP_MONOTONIC to confirm the clock is CLOCK_MONOTONIC.
Android Vulkan (default) / WebGPU Event-driven render loop, Vulkan prefers MAILBOX: unpaced None Add a Choreographer.FrameCallback gate (an in-progress Vulkan pacing branch already does this) and pass frameTimeNanos. Verify Stopwatch is CLOCK_MONOTONIC on the Android runtime.
Android GL GLSurfaceView.OnDrawFrame (WhenDirty) None in the callback Register a Choreographer.FrameCallback from the UI looper and hand the latest frameTimeNanos to the GL thread.
Win32 GL / Vulkan / software Wakes on invalidate, draws, then blocks in Present (SwapBuffers with swap interval 1, or DwmFlush). Timer fallback. None DwmGetCompositionTimingInfo (qpcVBlank, qpcRefreshPeriod) is QPC, the same clock as Stopwatch. Add it to NativeMethods.txt. To match WinUI, restructure the render thread to wait for the vblank before drawing (DCompositionWaitForCompositorClock or DwmFlush first), so the timestamp belongs to the frame being drawn and not the previous one.
macOS Metal / software Managed FramePacer timer at NSScreen.maximumFramesPerSecond, MTKView paused None. No display link exists. Add a native display link (CVDisplayLink, or NSView.displayLink on macOS 14+) to drive frames and supply targetTimestamp. Interim: forward FramePacer._nextTargetTick, which is already in Stopwatch ticks but is not a real vsync.
X11 GLX / EGL / Vulkan / software FramePacer timer, no swap interval set, Vulkan MAILBOX None GLX_OML_sync_control (glXGetSyncValuesOML) or the X Present extension (PresentCompleteNotify UST) where available. Otherwise keep the FrameClock fallback. Lowest priority.
Linux framebuffer fbdev software FBIO_WAITFORVSYNC after drawing, result ignored None Keep the fallback. Check the ioctl result so we know when the loop is unpaced.

Suggested order: iOS → WASM → Linux DRM (timestamp already in hand) → Android (pair with Vulkan pacing) → Win32 (render-loop restructure) → macOS (new display link) → X11.

  1. Fallback hardening. Hosts without a timestamp still need FrameClock to handle refresh-rate changes better (see below). Options:
    • re-learn the period quickly when several consecutive intervals disagree with the median;
    • stop snapping while the intervals are unstable;
    • prefer a host-reported refresh period when the host knows it.

Why is this needed? 🤔

#24735 samples Stopwatch on the UI thread after the native frame callback has been marshalled through the dispatcher. The timestamp therefore contains a few ms of random queuing delay, and a scroll at speed v turns that into v·Δt of uneven stepping. FrameClock works around this by snapping to a grid estimated from the median of the last 32 intervals. That works well while the refresh rate is fixed, but:

  • Variable refresh rate: ProMotion iPhones/Macs, G-Sync/FreeSync, and Low Power Mode capping all lack a fixed grid. The iOS host requests a frame rate range of 30 Hz to the device maximum (UnoMetalView.cs:92-97), so iOS changes the rate exactly when scrolling starts or a fling decays.
  • Hosts not paced by vsync have a meaningless grid. One example: the Linux framebuffer CI failure in feat(composition): Tick per-frame motion from a frame clock #24735, where the measured interval came out below 0.8 ms.

I simulated the frame clock with a JavaScript port of FrameClock.NextTimestamp, using ±1.5 ms of UI-thread jitter, under which raw sampling gives about 1.2 ms RMS step error. The rows show what happens on refresh-rate changes:

Rate change Frames until settled RMS step error, first 15 frames Worst step Settled RMS
steady 120 Hz 7 (58 ms) 0.70 ms 1.75 ms 0.16 ms
120 → 60 Hz 23 (383 ms) 3.49 ms 7.6 ms 0.26 ms
60 → 120 Hz 29 (242 ms) 2.09 ms 7.0 ms 0.08 ms
120 → 80 Hz 23 (288 ms) 4.13 ms 9.3 ms 0.24 ms
120 → 48 Hz 22 (458 ms) 3.59 ms 6.5 ms 0.29 ms
120 → 30 Hz 25 (833 ms) 3.74 ms 8.3 ms 0.32 ms

After each change, the snapped clock is 2–4× worse than raw sampling for up to 0.8 s, with single-step errors close to a full 120 Hz frame. At 3000 px/s, an 8 ms error is a visible 24 px jump. A native vsync timestamp has none of these problems: it is correct at any rate, from the first frame.

Renderer 🎨

  • Skia
  • Native

For which platforms 📱💻🖥️

All platforms 🌍

Anything else we need to know? 💬

  • Follow-up to feat(composition): Tick per-frame motion from a frame clock #24735 (part of Scroll motion has no frame clock, and touch/wheel deltas are quantized 🩵 #23985). That PR's tick order and "animations start at their first frame" semantics stay as they are. Only the source of the frame time changes.
  • Clock domains to verify on device:
    • Windows: QPC == Stopwatch (known).
    • Linux: DRM timestamps should be CLOCK_MONOTONIC; check the capability.
    • Android: System.nanoTime() vs Stopwatch.
    • Apple: mach_absolute_time/CACurrentMediaTime vs Stopwatch, which differ on Intel Macs.
    • WASM: performance.now() vs Stopwatch.
    • Converting with an offset sampled once at the host edge (Stopwatch.GetTimestamp() minus the native clock's "now") works whatever clock Stopwatch is backed by. Re-sample the offset after system sleep.
  • Tests:
    • A runtime test per host that FrameStarting/Rendering receive the host-supplied time (± conversion error).
    • Unit tests for FrameClock under rate changes (120↔60, 120→80, 60→120) that cap the worst step error.
    • Update the feat(composition): Tick per-frame motion from a frame clock #24735 framebuffer test so it no longer assumes a vsync-paced interval.
  • Acceptance: with the scroll-smoothness probe from feat(composition): Measure scroll smoothness in SamplesApp #24711, judder is no worse at a fixed rate, and there is no transition spike when the rate changes on a ProMotion device.

Activity

  1. self-assigned this
    on Oct 3, 2026
  2. added
    kind/enhancement
    triage/untriaged
    on Oct 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/enhancementtriage/untriaged

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions