Skip to content

display(macOS): draw on the view's display link at the screen's rate - #8199

Closed
UTMapp-bot wants to merge 4 commits into
utmapp:mainfrom
UTMapp-bot:display/view-display-link
Closed

UTMapp-bot wants to merge 4 commits into
utmapp:mainfrom
UTMapp-bot:display/view-display-link

Conversation

@UTMapp-bot

Copy link
Copy Markdown
Collaborator

Replaces the CAMetalDisplayLink of #7983 (and #8195's fix for it) with the display link NSView makes on macOS 14 and later. Each tick only asks MTKView to draw, so MTKView handles its drawables as it does with its own timer. As decided in #8195, this reverts both and lands the new commit and its tests in one PR:

  1. Revert display(macOS): keep Retina displays sharp when the display link draws #8195, the layer-size sync.
  2. Revert display: fix 60 Hz cap on ProMotion screens #7983, the CAMetalDisplayLink. It handed out the layer's drawables itself, so VMMetalView had to replace MTKView's drawable, render pass descriptor and drawable sizing, and frames were drawn at a stale size (5.0.9 regression: Retina mode appears blurry in Windows and Debian #8191).
  3. Draw on the view's display link at the screen's rate.
    • The link follows the screen the view is on, and each tick calls VMMetalView.drawDisplayLinkFrame() (draw()). MTKView acquires and sizes the drawables, and VMMetalView overrides none of it.
    • Without an FPS limit, the link keeps its default rate range: the native rate of whichever screen it is on. In 5.0.9 the range was pinned to the rate of the screen when it was read. A window that opened on a 60 Hz screen stayed at 60 Hz after its screen went to 120 Hz.
    • With a limit, the link is pinned to the limit.
    • The link targets a small proxy that holds the view weakly, and it starts paused until the VM is live.
    • macOS 12 and 13 keep MTKView's own timer.
  4. Tests (Tests/macOS/VMMetalViewDrawableTests.swift): frames drawn through drawDisplayLinkFrame(), without a VM.
    • After each resize, the drawable must have the view's pixel size.
    • A one-pixel checkerboard drawn by CSMetalRenderer at 1:1 must come back unchanged.

Testing

Tested by an AI agent on a MacBook Pro (Apple M2 Max) with only its built-in panel, a Retina display at backing scale 2 that supports ProMotion and fixed rates, on macOS 27.2. The guest was Ubuntu 26.04 arm64 GNOME on QEMU with virtio-gpu-gl-pci, GPU acceleration on and dynamic resolution on, each boot disposable.

Frame rate follows the screen. To show the pinned rate, I switched the built-in panel's mode for the login session with CGConfigureDisplayWithDisplayMode. I opened the VM window with the panel at 60 Hz, then switched the panel to 120 Hz (ProMotion). The link's rate comes from temporary logging of 1 / (targetTimestamp - timestamp) in the link callback; the logging is not committed.

Build Window opened at 60 Hz After the panel went to 120 Hz
rate pinned on read (the code before step 3's fix) 60 Hz, range 60–60 60 Hz, range 60–60
this PR 60 Hz, default range 120 Hz, default range

With an FPS limit of 30 (QEMURendererFPSLimit) on the 120 Hz panel, the link ran at 30 Hz. The limit was absent before the test and was removed after it. The panel was set back to its original 120 Hz mode.

Retina sharpness (#8191): the VM window was captured at full pixel size and edge contrast measured over the same text in the guest's top bar, as in #8195.

Build horizontal / vertical
5.0.8 19.666 / 12.314
5.0.9 14.644 / 9.556
this PR 19.666 / 12.314

Top bar at 1:1, 5.0.9 then this PR:

5.0.9
this PR

Pause and resume: while the VM is paused, UTM uses 0.0% CPU. After resuming, the window updates again: vkcube frames reach it, at the rate this guest renders.

Unit tests: ./scripts/test_utm.sh -only-testing:UTMTests/VMMetalViewDrawableTests passes, and so does VMMetalViewKeyboardTests. Tests/Static/run_all.sh passes. For a fail-before check, I made drawDisplayLinkFrame() temporarily do what 5.0.9's display link did: hand the renderer the layer's next drawable. Both tests then fail: the drawable stays 800x600 while the view is 1280x800, 640x400 or 64x48 pixels, and the checkerboard does not come back.

Not tested:

  • A window on a 120 Hz panel next to a 60 Hz external main display, the setup in display: fix 60 Hz cap on ProMotion screens #7983. No external display is attached here. The rate switch above exercises the same behavior on one screen: the link follows its screen's rate.
  • Moving the window between two screens.
  • macOS 12 and 13, which keep MTKView's own timer.

Known and unchanged:

  • CSMetalRenderer takes a drawable on every tick before it checks whether there is a new frame, so an idle guest costs one drawable per tick (idle CPU 2.8% of a core here, Debug build). Checking first in CocoaSpice would save that.
  • With an FPS limit that does not divide the screen's rate, the link runs at a rate the screen supports, while QEMU is told the limit, as before.

osy added 4 commits November 17, 2026 09:49
…ink draws"

This reverts commit 9f10688 (utmapp#8195).

The CAMetalDisplayLink it fixed is reverted next and replaced by a display
link that lets MTKView keep handling its drawables, which does not need
this fix.

Assisted-by: Claude:claude-opus-5-5
This reverts commit 216660f (utmapp#7983).

Its CAMetalDisplayLink hands out the layer's drawables itself, so
VMMetalView had to replace MTKView's drawable, render pass descriptor and
drawable sizing, and frames were drawn at a stale size (utmapp#8191). The next
commit fixes the 60 Hz cap without changing how MTKView draws.

Assisted-by: Claude:claude-opus-5-5
MTKView's own timer can stay at 60 Hz for a window on a 120 Hz ProMotion
panel while the main display is a 60 Hz one (utmapp#7983). On macOS 14 and later,
draw from the display link NSView makes instead: it follows the screen the
view is on, and each tick only asks MTKView to draw, so MTKView keeps
acquiring and sizing its drawables as it does with its own timer, and a
frame always has the view's pixel size (utmapp#8191). macOS 12 and 13 keep
MTKView's own timer.

Without an FPS limit the link keeps its default rate range, the native
rate of whichever screen it is on, so the rate follows the window to
another screen and a change of the screen's refresh rate. A range pinned
to the screen's rate when it was read stayed at 60 Hz for a window that
opened on a 60 Hz screen. With a limit, the link is pinned to the limit.

Assisted-by: Claude:claude-opus-5-5
A QEMU display window drawn at another size than the view's pixels is
scaled, which blurred every guest in 5.0.9 (utmapp#8191). Draw frames through
VMMetalView.drawDisplayLinkFrame(), as the window's display link does,
without a VM: after each resize the drawable must have the view's pixel
size, and a one-pixel checkerboard drawn by CSMetalRenderer at 1:1 must
come back unchanged.

With drawDisplayLinkFrame() doing what 5.0.9's display link did (hand
the renderer the layer's next drawable), both fail: the drawable keeps
the size the view had first.

Assisted-by: Claude:claude-opus-5-5
@UTMapp-bot

Copy link
Copy Markdown
Collaborator Author

Closing this: with a real 60 Hz external display it does not fix the 60 Hz cap, and main passed every test I ran. Details below.

What was wrong with this PR

I had only tested with the built-in 120 Hz panel as the sole display, where every timer runs at 120. In #7983's setup (a 60 Hz external as the main display, the VM window on the 120 Hz built-in panel), draw ticks per second were:

Timer Ticks/s
MTKView's own timer (5.0.8) 60
NSView.displayLink (this PR), also with the rate pinned to 120 or the link recreated on screen change 60
NSScreen.displayLink for the built-in screen 60
CAMetalDisplayLink that presents its drawables (#7983, main) 120

So #7983 chose the right mechanism. The 5.0.9 blur came from how it handled the drawables, which #8195 fixed.

Test campaign

I compared three builds:

Setup and method:

  • MacBook Pro (M2 Max) running macOS 27.2, its built-in 120 Hz panel plus a 1280x720 60 Hz external display.

  • Ubuntu 26.04 guest on virtio-gpu-gl-pci, Retina mode on, booted disposable.

  • The guest's display mode was 120 Hz, and it ran kmscube on the console. That gives about 120 guest frames a second, so the window's real frame rate can be measured.

  • Per step I counted:

    All counters were temporary and not committed.

Scenario (60 Hz external is main) A revert B main C retarget
window on the built-in panel 60 fps 117 fps 120 fps
moved to the external and back 60 / 60 60 / 118 60 / 115
window straddling both displays 60 60 60
panel switched to 60 Hz, then back to 120 Hz 60 / 59 60 / 117 60 / 101
minimized: ticks, UTM CPU 60, 31% 0, 16% 120, 39%
covered by another window: ticks 60 0 116
VM paused: ticks, CPU 60, 0.6% 0, 0.0% 120, 0.9%
FPS limit 30, applied on resume not applied 30 30
external unplugged while the window was on it recovers recovers recovers
frames drawn at the wrong size, all steps 0 0 0

A few notes on the table:

  • In the unplug test the built-in panel was the main display, so A also reached 120 there.
  • The non-GL card (virtio-gpu-pci), with moves between the 2x and 1x screens and resizes, gave identical sharpness readings and no wrong-size frames on all three builds.
  • The fps differences between B and C after a change come from single runs.

Recommendation: keep main

B passed every step. It reaches 120 Hz where the cap used to be, follows every move, refresh-rate change and unplug, never draws at the wrong size, and stops drawing when the window can't be seen.

  • Reverting brings back the cap.
  • C works, but keeps drawing while minimized or paused, and it depends on a private MTKView ivar.

The regression tests from this PR still pass on main's code. They need rewiring to main's display-link path before they can land.

Not tested:

  • macOS 12 and 13. main keeps MTKView's own timer there, as in 5.0.8.
  • Guests with several displays.
  • Input latency.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants