Skip to content

display(macOS): keep Retina displays sharp when the display link draws - #8195

Merged
osy merged 1 commit into
utmapp:mainfrom
UTMapp-bot:fix/issue-8191-retina-blur
Nov 17, 2026
Merged

osy merged 1 commit into
utmapp:mainfrom
UTMapp-bot:fix/issue-8191-retina-blur

Conversation

@UTMapp-bot

Copy link
Copy Markdown
Collaborator

Since 5.0.9 a QEMU display window with Retina mode on looks soft: text and UI in the guest are blurred.

The window now draws from a CAMetalDisplayLink (macOS 14 and later, #7983), and the MTKView is paused while it does. MTKView applies its drawableSize to its CAMetalLayer only from its own draw loop, so the layer kept the drawable size it had when the window opened, while the view's drawableSize (and the renderer's viewport, which drawableSizeWillChange sets) followed the guest. Logged on main in the display link callback, with a 1280x800 guest in a 640x400-point Retina window:

drawable 1600x1264 layer=(1600.0, 1264.0) view=(1280.0, 800.0) bounds=(640.0, 400.0)

Every frame was rendered at 1600x1264 and scaled into a 1280x800-pixel area, so a Retina display was never 1:1.

The change:

  • VMMetalView gives the layer the view's drawable size whenever the view's frame, bounds, backing scale or drawableSize changes, only while MTKView is paused (a display link draws). With MTKView's own loop running, nothing changes.
  • The display link callback skips a drawable it took before such a change, instead of drawing the new viewport into a texture of the old size. The renderer then draws into the next one.

Resolves #8191

Testing

Tested by an AI agent on a MacBook Pro (Apple M2 Max) with its built-in Retina display (backing scale 2), macOS 27.2.

Configuration from the report: QEMU, an Ubuntu 26.04 arm64 GNOME guest, virtio-gpu-gl-pci with GPU acceleration, Retina mode on, dynamic resolution on, each boot disposable. Guest mode 1280x800 in a 640x400-point window. Builds: the 5.0.8 and 5.0.9 releases, and main with this change.

Measurement. I captured the VM window at full pixel size (screencapture -o -l<window>). For each capture I computed the mean absolute luma difference between neighbouring pixels (horizontal / vertical) over the same 55x22 px of the guest's top bar (the "Nov 17" text, identical in every run). Blur lowers it.

Build text h / v whole guest area h / v
5.0.8 release 19.666 / 12.314 2.826 / 2.770
5.0.9 release (before) 14.644 / 9.556 2.701 / 2.638
main + this change (after) 19.666 / 12.314 2.828 / 2.769

Top bar at 1:1, 5.0.8 / 5.0.9 / with this change:

5.0.8
5.0.9
with this change

Full window, 5.0.9 and with this change.

Other checks:

  • With temporary logging (not committed), every frame logged drawable == layer == view, through the guest's mode changes at boot (640x480, 800x600, 1280x800). No drawable was skipped.
  • Retina mode off: the drawable follows the window (2560x1600 for 1280x800 points) and the display renders normally (screenshot).
  • The committed code, without the logging, was built and measured again for the "after" numbers above.

Not tested:

  • macOS 12 and 13. They have no display link, MTKView is not paused there, and the new code returns early.
  • Moving the window between screens of different scale (only one display here).
  • Resizing the window by dragging. The guest-driven resizes go through the same setFrameSize path.

Unchanged from 5.0.8: CocoaSpice's renderer does not redraw for a drawable size change alone. A guest that is not drawing keeps showing its previous frame until its next update, as with MTKView's own loop.

Since the window draws from a CAMetalDisplayLink (macOS 14 and later), the
MTKView is paused, and MTKView applies its drawable size to its layer only
in its own draw loop. The layer therefore kept the drawable size it had
when the window opened (1600x1264 here) while the view and the renderer's
viewport followed the guest (1280x800): every frame was rendered at the
stale size and scaled into the window, which made Retina mode blurry.

While MTKView is paused, give the layer the view's drawable size whenever
the view's size or backing scale changes, so the next drawable the link
hands out has it, and skip a drawable the link took before the change
instead of drawing the new viewport into it.

Fixes utmapp#8191

Assisted-by: Claude:claude-opus-5-5
@osy
osy merged commit f5655bf into utmapp:main Nov 17, 2026
31 checks passed
@osy

osy commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

@UTMapp-bot look into 1) any other issues that could have come from changing MTKView 2) see if the refresh rate could be solved in an simpler way that won't involve changing MTKView implementation 3) look at what tests can be done to prevent a major regression like this in the future.

@UTMapp-bot

Copy link
Copy Markdown
Collaborator Author

Answers to the three points. Testing was on a MacBook Pro (M2 Max) with only its built-in 120 Hz Retina panel attached, macOS 27.2, and an Ubuntu 26.04 GNOME guest (virtio-gpu-gl-pci), booted disposable.

1. Other issues from the MTKView change (#7983, then #8195)

  • Stale drawable size (5.0.9 regression: Retina mode appears blurry in Windows and Debian #8191). This was not limited to Retina mode. The layer kept the size it had when the window opened, so every frame was resampled at any other window size: resizing, full screen, Retina mode off. display(macOS): keep Retina displays sharp when the display link draws #8195 fixes it.
  • Replaced MTKView internals. VMMetalView builds its own render pass descriptor and hands out its own currentDrawable. Nothing uses MTKView's depth, stencil or sample-count settings today, but they would be silently ignored if anything did. Any draw outside the link callback also falls back to MTKView's path at the layer's size.
  • Drawables taken before a resize. CAMetalDisplayLink takes drawables ahead of time, so one can still have the old size after a resize. display(macOS): keep Retina displays sharp when the display link draws #8195 skips those frames.
  • Idle cost. With a static guest, idle CPU went from 0.4% (5.0.8 release) to 2.1% (5.0.9 release); display: fix 60 Hz cap on ProMotion screens #7983's own retest measured 2.9%. The link ticks at 120 Hz, and every tick takes a drawable even when there is nothing to draw, because CSMetalRenderer reads currentRenderPassDescriptor before it checks renderNeedsUpdate. That is true of any 120 Hz driver. Checking first in CocoaSpice would cut the cost for all of them.
  • No other reports since 5.0.9. I searched the issues opened since 5.0.9 and found none that trace back to the display link. Severe KWin display lag with VirGL acceleration since UTM 5.x #8197 (KWin lag) is guest-side buffer-age handling, reported since 4.x.
  • Older behavior, not from this change. CSMetalRenderer does not redraw when only the drawable size changes. A guest that isn't drawing keeps its previous frame, scaled, until it next updates. 5.0.8's MTKView timer behaves the same.

2. A simpler way to get the frame rate

The branch display/mtkview-explicit-draw on UTMapp-bot/UTM (commit becc8ba) uses the display link NSView makes on macOS 14 and later (metalView.displayLink(target:selector:)).

Tested:

Not tested: the case #7983 fixed, a window on the 120 Hz panel while a 60 Hz external display is the main display. No external display is attached to this Mac. With only the built-in panel, main already reaches 120 Hz, so this setup can't show the difference. For that reason it is a branch and not a PR yet (CONTRIBUTING rule 3). I'll open the PR once that run is done here with an external display, or after someone runs the branch on such a setup.

One trade-off: like 5.0.8's MTKView timer, draw() takes its drawable on the main thread, which can block when the GPU is behind. CAMetalDisplayLink avoided that by delivering a drawable that was already free.

3. Tests against a regression like this

Commit 9b35798 on the same branch adds Tests/macOS/VMMetalViewDrawableTests.swift. These are macOS-hosted unit tests that need no VM. They skip when there is no Metal device, and both run in 0.06 s. They draw frames through VMMetalView.drawDisplayLinkFrame(), the method the display link calls.

  • testFramesAreDrawnAtTheViewsPixelSizeAfterResizing resizes the view three times. After each resize, the frame's drawable must have the view's pixel size.
  • testOneToOneFrameKeepsEveryGuestPixel opens the view at one size and fits it to the guest, as a VM window does. CSMetalRenderer then draws a 64x48 one-pixel checkerboard at viewportScale 1. The test reads the drawable back, and every pixel must match the guest's, so any resampling fails it.
Per-frame path Result
5.0.9 (the display link hands in the layer's next drawable) both fail: the drawable stays 800x600, 3051 pixels differ
main (#8195's size sync) both pass
the branch both pass

Two more tests would close the gaps these can't reach. I haven't written either:

  • A VMLifecycleTests scenario that boots the firmware shell with Retina mode on and compares the window's pixels with capture screen. That would cover the controller's scaling end to end, but the runner needs Screen Recording permission.
  • A CocoaSpice renderer test that a drawable size change alone triggers a redraw. It would fail today (see the last point of section 1).

@osy

osy commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

@UTMapp-bot I made the decision that we should revert #7983 + #8195 and then commit becc8ba + 9b35798. Open a new PR and make the two revert commits and then the proper commit + test in a single PR. Make sure your new commit handles the following gap: the rate is pinned and isn’t updated when the window changes screens.

@UTMapp-bot

Copy link
Copy Markdown
Collaborator Author

Done in #8199, four commits in this order:

  1. Revert display(macOS): keep Retina displays sharp when the display link draws #8195.
  2. Revert display: fix 60 Hz cap on ProMotion screens #7983.
  3. The display-link commit (becc8ba, reworked as described below).
  4. The tests (9b35798, reworked as described below).

The pinned rate. updateFrameRate() is called when the window changes screens: windowDidChangeScreen is a window delegate method and AppKit calls it. The rate was still pinned, though: min = max = preferred = the screen's rate at the moment it was read. A change of the screen's own refresh rate never updated it, and the view's display link has no reason to be pinned at all, because it follows its screen.

The new commit leaves the link at its default range when there is no FPS limit. The default is the native rate of whichever screen the link is on. With a limit, the link is pinned to the limit.

Tested on this Mac's built-in panel. I opened the VM window with the panel at 60 Hz, then switched the panel to 120 Hz. The rates come from temporary logging in the link callback, which is not committed.

Moving the window to another screen goes through the same default range, but I couldn't try it: no external display is attached here.

Changes from the code review. UTM's review ran on the branch, and these came from it:

  • The link targets a small proxy that holds the view weakly, so the link no longer keeps the view alive. It invalidates itself if the view is gone.
  • The link starts paused until the VM is live.
  • The four CADisplayLink casts are now one accessor.
  • The duplicated comments are gone.
  • The pixel test copies the frame on the renderer's queue, behind its draw, while it still holds the drawable.
  • Both tests skip on a host that gives the view no drawable.

Two points from the review are not in this PR:

  • An idle tick still takes a drawable, because CSMetalRenderer checks for a new frame only after taking one. That belongs in CocoaSpice.
  • With an FPS limit that does not divide the screen's rate, QEMU is still told the limit. That predates this change.

osy added a commit that referenced this pull request Nov 18, 2026
…ink draws"

This reverts commit 9f10688 (#8195), a
fix for the CAMetalDisplayLink of #7983, which is reverted next.

Assisted-by: Claude:claude-opus-5-5
osy added a commit that referenced this pull request Nov 18, 2026
This reverts commit 216660f (#7983).

Drawing from a CAMetalDisplayLink while MTKView still owns the drawing
setup made the display blurry in 5.0.9 (#8191, fixed by #8195, reverted
before this) and can crash at VM start when a draw MTKView's own timer
queued asks the layer for a drawable while the link owns it (#8204).
Each fix adds another override of MTKView's internals. Go back to
MTKView's own timer: a window on a 120 Hz panel is again drawn at the
main display's rate when that is a slower external display.

Assisted-by: Claude:claude-opus-5-5
osy added a commit that referenced this pull request Nov 18, 2026
display(macOS): revert the display link and its follow-up fix (#7983, #8195)
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.

5.0.9 regression: Retina mode appears blurry in Windows and Debian

2 participants