From 44ff920eb32759f63348040cc00c78d93b5c18a8 Mon Sep 17 00:00:00 2001 From: osy Date: Tue, 17 Nov 2026 22:08:01 -0800 Subject: [PATCH 1/2] display(macOS): revert "keep Retina displays sharp when the display link draws" This reverts commit 9f1068802eb2e578e9aaa0dd5139d6154c38b90e (#8195), a fix for the CAMetalDisplayLink of #7983, which is reverted next. Assisted-by: Claude:claude-opus-5-5 --- .../VMDisplayQemuMetalWindowController.swift | 7 ---- Platform/macOS/Display/VMMetalView.swift | 39 ------------------- 2 files changed, 46 deletions(-) diff --git a/Platform/macOS/Display/VMDisplayQemuMetalWindowController.swift b/Platform/macOS/Display/VMDisplayQemuMetalWindowController.swift index 4d05211465..1e4c926ff4 100644 --- a/Platform/macOS/Display/VMDisplayQemuMetalWindowController.swift +++ b/Platform/macOS/Display/VMDisplayQemuMetalWindowController.swift @@ -795,13 +795,6 @@ extension VMDisplayQemuMetalWindowController { @available(macOS 14, *) extension VMDisplayQemuMetalWindowController: CAMetalDisplayLinkDelegate { func metalDisplayLink(_ link: CAMetalDisplayLink, needsUpdate update: CAMetalDisplayLink.Update) { - let texture = update.drawable.texture - if let layer = metalView.layer as? CAMetalLayer, - CGFloat(texture.width) != layer.drawableSize.width || CGFloat(texture.height) != layer.drawableSize.height { - // taken before the view was resized: the renderer's viewport already has the new size, so - // draw into the next drawable, which has it too - return - } metalView.displayLinkDrawable = update.drawable renderer.draw(in: metalView) metalView.displayLinkDrawable = nil diff --git a/Platform/macOS/Display/VMMetalView.swift b/Platform/macOS/Display/VMMetalView.swift index db60d89f9f..8b14a04e1d 100644 --- a/Platform/macOS/Display/VMMetalView.swift +++ b/Platform/macOS/Display/VMMetalView.swift @@ -50,45 +50,6 @@ class VMMetalView: MTKView { return descriptor } - override var drawableSize: CGSize { - didSet { - updateLayerDrawableSize() - } - } - - override func setFrameSize(_ newSize: NSSize) { - super.setFrameSize(newSize) - updateLayerDrawableSize() - } - - override func setBoundsSize(_ newSize: NSSize) { - super.setBoundsSize(newSize) - updateLayerDrawableSize() - } - - override func viewDidChangeBackingProperties() { - super.viewDidChangeBackingProperties() - updateLayerDrawableSize() - } - - /// Gives the layer the view's drawable size as soon as it changes, while a display link draws. - /// - /// MTKView applies `drawableSize` to its layer only from its own draw loop, which is paused while a - /// display link draws. The layer would otherwise keep the size it had when the window opened, and - /// every frame would be scaled into the view, blurring it. The size is the view's `drawableSize` - /// because that is the one the renderer's viewport is given. - private func updateLayerDrawableSize() { - guard isPaused else { - // MTKView's own draw loop keeps the layer's size - return - } - let size = drawableSize - guard size.width > 0, size.height > 0, let layer = layer as? CAMetalLayer, layer.drawableSize != size else { - return - } - layer.drawableSize = size - } - private enum ModifierKeySwap { case none case ctrlCmd From ff73aded4275c71d2710b297107939767cd231a1 Mon Sep 17 00:00:00 2001 From: osy Date: Tue, 17 Nov 2026 22:08:02 -0800 Subject: [PATCH 2/2] display(macOS): revert "fix 60 Hz cap on ProMotion screens" This reverts commit 216660f0 (#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 --- .../VMDisplayQemuMetalWindowController.swift | 59 +++---------------- Platform/macOS/Display/VMMetalView.swift | 20 ------- 2 files changed, 7 insertions(+), 72 deletions(-) diff --git a/Platform/macOS/Display/VMDisplayQemuMetalWindowController.swift b/Platform/macOS/Display/VMDisplayQemuMetalWindowController.swift index 1e4c926ff4..e159444626 100644 --- a/Platform/macOS/Display/VMDisplayQemuMetalWindowController.swift +++ b/Platform/macOS/Display/VMDisplayQemuMetalWindowController.swift @@ -45,8 +45,6 @@ class VMDisplayQemuMetalWindowController: VMDisplayQemuWindowController { private var globalEventMonitor: Any? = nil private var ctrlKeyDown: Bool = false private var screenChangedToken: Any? - /// `CAMetalDisplayLink` on macOS 14 and later - private var displayLink: AnyObject? private var displayConfig: UTMQemuConfigurationDisplay? { vmQemuConfig?.displays[id] @@ -99,16 +97,9 @@ class VMDisplayQemuMetalWindowController: VMDisplayQemuWindowController { logger.critical("Failed to create renderer.") return } - if #available(macOS 14, *), let layer = metalView.layer as? CAMetalLayer { - // A CAMetalDisplayLink paces the layer at the rate it requests; MTKView's own - // timer can stay at 60 Hz on a ProMotion display. - let link = CAMetalDisplayLink(metalLayer: layer) - link.delegate = self - link.add(to: .main, forMode: .common) - metalView.isPaused = true - displayLink = link + if let fps = preferredFramesPerSecond { + metalView.preferredFramesPerSecond = fps } - updateFrameRate() renderer.changeUpscaler(displayConfig?.upscalingFilter.metalSamplerMinMagFilter ?? .linear, downscaler: displayConfig?.downscalingFilter.metalSamplerMinMagFilter ?? .linear) vmDisplay?.addRenderer(renderer) // can be nil if primary metalView.delegate = renderer @@ -131,26 +122,7 @@ class VMDisplayQemuMetalWindowController: VMDisplayQemuWindowController { super.windowDidLoad() } - /// Refresh at the rate of the screen the window is on, unless the user set a limit. - @discardableResult - private func updateFrameRate() -> Int? { - guard let fps = preferredFramesPerSecond else { - return nil - } - if #available(macOS 14, *), let link = displayLink as? CAMetalDisplayLink { - let rate = Float(fps) - link.preferredFrameRateRange = CAFrameRateRange(minimum: rate, maximum: rate, preferred: rate) - } else { - metalView.preferredFramesPerSecond = fps - } - return fps - } - override func windowWillClose(_ notification: Notification) { - if #available(macOS 14, *), let link = displayLink as? CAMetalDisplayLink { - link.invalidate() - } - displayLink = nil vmDisplay?.removeRenderer(renderer!) stopAllCapture() if let screenChangedToken = screenChangedToken { @@ -186,11 +158,6 @@ class VMDisplayQemuMetalWindowController: VMDisplayQemuWindowController { displaySizeDidChange(size: vmDisplay.displaySize) } super.enterLive() - // pick up an FPS limit changed while the window was open - updateFrameRate() - if #available(macOS 14, *), let link = displayLink as? CAMetalDisplayLink { - link.isPaused = false - } setControl(.resize, isEnabled: false) // disable item if isWindowFocusAutoCapture { captureMouse() @@ -199,9 +166,6 @@ class VMDisplayQemuMetalWindowController: VMDisplayQemuWindowController { override func enterSuspended(isBusy busy: Bool) { if !busy { - if #available(macOS 14, *), let link = displayLink as? CAMetalDisplayLink { - link.isPaused = true - } metalView.isHidden = true screenshotView.image = vm.screenshot?.image screenshotView.isHidden = false @@ -217,7 +181,11 @@ class VMDisplayQemuMetalWindowController: VMDisplayQemuWindowController { override func virtualMachineDisplayRefreshRate(_ vm: any UTMVirtualMachine) -> Int? { // the window may have moved screens since it was loaded - return updateFrameRate() + guard let fps = preferredFramesPerSecond else { + return nil + } + metalView.preferredFramesPerSecond = fps + return fps } /// Frame rate the display is rendered at on the current screen, respecting the user's limit @@ -351,8 +319,6 @@ extension VMDisplayQemuMetalWindowController { func windowDidChangeScreen(_ notification: Notification) { logger.debug("screen changed") - // the new screen can refresh at a different rate - updateFrameRate() if let vmDisplay = self.vmDisplay { displaySizeDidChange(size: vmDisplay.displaySize) } @@ -789,14 +755,3 @@ extension VMDisplayQemuMetalWindowController { window.beginSheet(sheetWindow) } } - -// MARK: - Display link - -@available(macOS 14, *) -extension VMDisplayQemuMetalWindowController: CAMetalDisplayLinkDelegate { - func metalDisplayLink(_ link: CAMetalDisplayLink, needsUpdate update: CAMetalDisplayLink.Update) { - metalView.displayLinkDrawable = update.drawable - renderer.draw(in: metalView) - metalView.displayLinkDrawable = nil - } -} diff --git a/Platform/macOS/Display/VMMetalView.swift b/Platform/macOS/Display/VMMetalView.swift index 8b14a04e1d..6ac9c2c1c8 100644 --- a/Platform/macOS/Display/VMMetalView.swift +++ b/Platform/macOS/Display/VMMetalView.swift @@ -30,26 +30,6 @@ class VMMetalView: MTKView { @Setting("IsISOKeySwapped") private var isISOKeySwapped = false @Setting("IsCapsLockKey") private var isCapsLockKey: Bool = false - /// Drawable supplied by a display link for the frame being drawn, used instead of one - /// acquired from the layer. - var displayLinkDrawable: CAMetalDrawable? - - override var currentDrawable: CAMetalDrawable? { - displayLinkDrawable ?? super.currentDrawable - } - - override var currentRenderPassDescriptor: MTLRenderPassDescriptor? { - guard let drawable = displayLinkDrawable else { - return super.currentRenderPassDescriptor - } - let descriptor = MTLRenderPassDescriptor() - descriptor.colorAttachments[0].texture = drawable.texture - descriptor.colorAttachments[0].loadAction = .clear - descriptor.colorAttachments[0].storeAction = .store - descriptor.colorAttachments[0].clearColor = clearColor - return descriptor - } - private enum ModifierKeySwap { case none case ctrlCmd