Repository navigation
display(macOS): keep Retina displays sharp when the display link draws - #8195
Conversation
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
|
@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. |
|
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 ( 1. Other issues from the MTKView change (#7983, then #8195)
2. A simpler way to get the frame rateThe branch
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, One trade-off: like 5.0.8's MTKView timer, 3. Tests against a regression like thisCommit 9b35798 on the same branch adds
Two more tests would close the gaps these can't reach. I haven't written either:
|
|
@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. |
|
Done in #8199, four commits in this order:
The pinned rate. 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:
Two points from the review are not in this PR:
|
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
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 theMTKViewis paused while it does.MTKViewapplies itsdrawableSizeto itsCAMetalLayeronly from its own draw loop, so the layer kept the drawable size it had when the window opened, while the view'sdrawableSize(and the renderer's viewport, whichdrawableSizeWillChangesets) followed the guest. Logged onmainin the display link callback, with a 1280x800 guest in a 640x400-point Retina window:Every frame was rendered at 1600x1264 and scaled into a 1280x800-pixel area, so a Retina display was never 1:1.
The change:
VMMetalViewgives the layer the view's drawable size whenever the view's frame, bounds, backing scale ordrawableSizechanges, only whileMTKViewis paused (a display link draws). With MTKView's own loop running, nothing changes.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-pciwith 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, andmainwith 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.main+ this change (after)Top bar at 1:1, 5.0.8 / 5.0.9 / with this change:
Full window, 5.0.9 and with this change.
Other checks:
Not tested:
MTKViewis not paused there, and the new code returns early.setFrameSizepath.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.