Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 40 additions & 27 deletions Platform/macOS/Display/VMDisplayQemuMetalWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,14 @@ class VMDisplayQemuMetalWindowController: VMDisplayQemuWindowController {
private var globalEventMonitor: Any? = nil
private var ctrlKeyDown: Bool = false
private var screenChangedToken: Any?
/// `CAMetalDisplayLink` on macOS 14 and later
/// `CADisplayLink` of the view on macOS 14 and later
private var displayLink: AnyObject?

@available(macOS 14, *)
private var viewDisplayLink: CADisplayLink? {
displayLink as? CADisplayLink
}

private var displayConfig: UTMQemuConfigurationDisplay? {
vmQemuConfig?.displays[id]
}
Expand Down Expand Up @@ -99,11 +104,12 @@ 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
if #available(macOS 14, *) {
// MTKView's own timer can stay at 60 Hz on a ProMotion display when the main display is a
// 60 Hz one; the view's display link follows the screen the view is on
let target = DisplayLinkTarget(view: metalView)
let link = metalView.displayLink(target: target, selector: #selector(DisplayLinkTarget.displayLinkDidFire(_:)))
link.isPaused = true // until the VM is live
link.add(to: .main, forMode: .common)
metalView.isPaused = true
displayLink = link
Expand Down Expand Up @@ -132,22 +138,27 @@ class VMDisplayQemuMetalWindowController: VMDisplayQemuWindowController {
}

/// Refresh at the rate of the screen the window is on, unless the user set a limit.
/// - Returns: The rate on the current screen, if it is known
@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 {
if #available(macOS 14, *), let link = viewDisplayLink {
if rendererFpsLimit > 0 {
// a screen slower than the limit lowers the rate by itself
let limit = Float(rendererFpsLimit)
link.preferredFrameRateRange = CAFrameRateRange(minimum: limit, maximum: limit, preferred: limit)
} else {
// the link runs at the rate of whichever screen the view is on, also after the
// window moves to another screen or the screen's refresh rate is changed
link.preferredFrameRateRange = .default
}
} else if let fps = preferredFramesPerSecond {
metalView.preferredFramesPerSecond = fps
}
return fps
return preferredFramesPerSecond
}

override func windowWillClose(_ notification: Notification) {
if #available(macOS 14, *), let link = displayLink as? CAMetalDisplayLink {
if #available(macOS 14, *), let link = viewDisplayLink {
link.invalidate()
}
displayLink = nil
Expand Down Expand Up @@ -188,7 +199,7 @@ class VMDisplayQemuMetalWindowController: VMDisplayQemuWindowController {
super.enterLive()
// pick up an FPS limit changed while the window was open
updateFrameRate()
if #available(macOS 14, *), let link = displayLink as? CAMetalDisplayLink {
if #available(macOS 14, *), let link = viewDisplayLink {
link.isPaused = false
}
setControl(.resize, isEnabled: false) // disable item
Expand All @@ -199,7 +210,7 @@ class VMDisplayQemuMetalWindowController: VMDisplayQemuWindowController {

override func enterSuspended(isBusy busy: Bool) {
if !busy {
if #available(macOS 14, *), let link = displayLink as? CAMetalDisplayLink {
if #available(macOS 14, *), let link = viewDisplayLink {
link.isPaused = true
}
metalView.isHidden = true
Expand Down Expand Up @@ -792,18 +803,20 @@ extension VMDisplayQemuMetalWindowController {

// MARK: - Display link

/// Target of the view's display link, which keeps the view alive only while it is in use.
@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
private final class DisplayLinkTarget: NSObject {
private weak var view: VMMetalView?

init(view: VMMetalView) {
self.view = view
}

@objc func displayLinkDidFire(_ link: CADisplayLink) {
guard let view = view else {
link.invalidate()
return
}
metalView.displayLinkDrawable = update.drawable
renderer.draw(in: metalView)
metalView.displayLinkDrawable = nil
view.drawDisplayLinkFrame()
}
}
60 changes: 3 additions & 57 deletions Platform/macOS/Display/VMMetalView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,63 +30,9 @@ 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
}

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
/// Draws a frame for a tick of the display link that drives this view on macOS 14 and later.
func drawDisplayLinkFrame() {
draw()
}

private enum ModifierKeySwap {
Expand Down
196 changes: 196 additions & 0 deletions Tests/macOS/VMMetalViewDrawableTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
//
// Copyright © 2026 Turing Software, LLC. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import XCTest
import MetalKit
import CocoaSpiceRenderer
@testable import UTM

/// A QEMU display window draws each frame from `VMMetalView.drawDisplayLinkFrame()` (macOS 14 and later).
/// A frame drawn into a drawable of another size than the view's pixels is scaled into the window, which
/// blurs the guest's display (#8191). These tests draw frames the way the window does, without a VM.
@MainActor
final class VMMetalViewDrawableTests: XCTestCase {
/// Records the drawable of each frame, then lets `renderer` draw it (or clears it).
private final class RecordingDelegate: NSObject, MTKViewDelegate {
let commandQueue: MTLCommandQueue
weak var renderer: CSMetalRenderer?
/// Pixel size of each frame's drawable
var sizes: [CGSize] = []
/// Pixels of the last frame `renderer` drew
var lastPixels: [UInt32] = []

init(device: MTLDevice, renderer: CSMetalRenderer? = nil) {
commandQueue = device.makeCommandQueue()!
self.renderer = renderer
}

func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {
renderer?.mtkView(view, drawableSizeWillChange: size)
}

func draw(in view: MTKView) {
guard let drawable = view.currentDrawable else {
return
}
sizes.append(CGSize(width: drawable.texture.width, height: drawable.texture.height))
if let renderer = renderer {
renderer.draw(in: view)
lastPixels = copyPixels(of: drawable.texture, behindWorkOn: renderer.commandQueue)
} else if let pass = view.currentRenderPassDescriptor,
let commandBuffer = commandQueue.makeCommandBuffer(),
let encoder = commandBuffer.makeRenderCommandEncoder(descriptor: pass) {
encoder.endEncoding()
commandBuffer.present(drawable)
commandBuffer.commit()
}
}

/// Copies a drawable's texture out on the queue the renderer drew it on, so the copy runs after that
/// draw, while the drawable is still held here
private func copyPixels(of texture: MTLTexture, behindWorkOn queue: MTLCommandQueue) -> [UInt32] {
let count = texture.width * texture.height
guard let buffer = queue.device.makeBuffer(length: count * 4, options: .storageModeShared),
let commandBuffer = queue.makeCommandBuffer(),
let blit = commandBuffer.makeBlitCommandEncoder() else {
return []
}
blit.copy(from: texture, sourceSlice: 0, sourceLevel: 0, sourceOrigin: MTLOriginMake(0, 0, 0),
sourceSize: MTLSizeMake(texture.width, texture.height, 1),
to: buffer, destinationOffset: 0, destinationBytesPerRow: texture.width * 4, destinationBytesPerImage: 0)
blit.endEncoding()
commandBuffer.commit()
commandBuffer.waitUntilCompleted()
return Array(UnsafeBufferPointer(start: buffer.contents().assumingMemoryBound(to: UInt32.self), count: count))
}
}

/// A guest display: a texture and the quad CocoaSpice builds for it (`CSDisplay.rebuildDisplayVertices`).
private final class TestRenderSource: NSObject, CSRenderSource {
let texture: MTLTexture?
let vertices: MTLBuffer?
let numVertices: UInt = 6
let isVisible = true
let offset = CGPoint.zero
let hasAlpha = false
let isInverted = false
weak var cursor: CSRenderSource?

init(device: MTLDevice, width: Int, height: Int, pixels: [UInt32]) {
let descriptor = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: .bgra8Unorm, width: width, height: height, mipmapped: false)
let texture = device.makeTexture(descriptor: descriptor)!
pixels.withUnsafeBytes { bytes in
texture.replace(region: MTLRegionMake2D(0, 0, width, height), mipmapLevel: 0, withBytes: bytes.baseAddress!, bytesPerRow: width * 4)
}
self.texture = texture
let w = Float(width) / 2, h = Float(height) / 2
let quad: [CSRenderVertex] = [
CSRenderVertex(position: [ w, h], textureCoordinate: [1, 0]),
CSRenderVertex(position: [-w, h], textureCoordinate: [0, 0]),
CSRenderVertex(position: [-w, -h], textureCoordinate: [0, 1]),
CSRenderVertex(position: [ w, h], textureCoordinate: [1, 0]),
CSRenderVertex(position: [-w, -h], textureCoordinate: [0, 1]),
CSRenderVertex(position: [ w, -h], textureCoordinate: [1, 1]),
]
vertices = device.makeBuffer(bytes: quad, length: MemoryLayout<CSRenderVertex>.stride * quad.count)
}
}

private var window: NSWindow?

override func tearDown() {
window?.close()
window = nil
super.tearDown()
}

/// A view set up as `VMDisplayQemuMetalWindowController` sets it up when a display link draws it.
private func makeView(points: CGSize) throws -> VMMetalView {
guard let device = MTLCreateSystemDefaultDevice() else {
throw XCTSkip("This host has no Metal device.")
}
let window = NSWindow(contentRect: CGRect(origin: .zero, size: points), styleMask: .borderless, backing: .buffered, defer: false)
window.isReleasedWhenClosed = false
self.window = window
let view = VMMetalView(frame: window.contentView!.bounds, device: device)
view.autoresizingMask = [.width, .height]
view.framebufferOnly = false
view.isPaused = true
window.contentView!.addSubview(view)
return view
}

/// Draws one frame, or skips the test on a host that gives the view no drawable.
private func drawFrame(_ view: VMMetalView, recordedBy delegate: RecordingDelegate) throws {
let count = delegate.sizes.count
view.drawDisplayLinkFrame()
if delegate.sizes.count == count {
throw XCTSkip("This host gives the view no drawable.")
}
}

private func pixelSize(of view: NSView) -> CGSize {
view.convertToBacking(view.bounds).size
}

/// Runs the main queue until the blocks queued on it so far have run.
private func drainMainQueue() {
let drained = expectation(description: "main queue")
DispatchQueue.main.async { drained.fulfill() }
wait(for: [drained], timeout: 5)
}

func testFramesAreDrawnAtTheViewsPixelSizeAfterResizing() throws {
let view = try makeView(points: CGSize(width: 400, height: 300))
let delegate = RecordingDelegate(device: view.device!)
view.delegate = delegate
for points in [CGSize(width: 400, height: 300), CGSize(width: 640, height: 400), CGSize(width: 320, height: 200)] {
window!.setContentSize(points)
try drawFrame(view, recordedBy: delegate)
XCTAssertEqual(delegate.sizes.last, pixelSize(of: view), "frame after resizing to \(points) points")
}
}

/// The guest's pixels reach the window unchanged at 1:1 (Retina mode on a Retina screen, or any mode
/// at scale 1): a one-pixel checkerboard comes back exactly, so any resampling on the way fails.
func testOneToOneFrameKeepsEveryGuestPixel() throws {
let (width, height) = (64, 48)
// the window opens at another size and is then fitted to the guest, as a VM window is
let view = try makeView(points: CGSize(width: 400, height: 300))
let scale = window!.backingScaleFactor
let device = view.device!
let renderer = CSMetalRenderer(metalKitView: view)
let delegate = RecordingDelegate(device: device, renderer: renderer)
view.delegate = delegate
try drawFrame(view, recordedBy: delegate)
window!.setContentSize(CGSize(width: CGFloat(width) / scale, height: CGFloat(height) / scale))
XCTAssertEqual(pixelSize(of: view), CGSize(width: width, height: height))
renderer.viewportScale = 1.0

let white: UInt32 = 0xFFFF_FFFF, black: UInt32 = 0xFF00_0000
let pattern = (0..<height).flatMap { y in (0..<width).map { x in (x + y) % 2 == 0 ? white : black } }
let source = TestRenderSource(device: device, width: width, height: height, pixels: pattern)
let drawn = expectation(description: "frame drawn")
renderer.invalidateRenderSource(source) { drawn.fulfill() }
drainMainQueue()
try drawFrame(view, recordedBy: delegate)
wait(for: [drawn], timeout: 5)

XCTAssertEqual(delegate.sizes.last, CGSize(width: width, height: height))
let mismatches = zip(delegate.lastPixels, pattern).filter { $0 != $1 }.count + abs(delegate.lastPixels.count - pattern.count)
XCTAssertEqual(mismatches, 0, "\(mismatches) of \(pattern.count) pixels differ from the guest's")
}
}
Loading
Loading