From 11478d230c6dff8a3d988a7b7b0e93780048a041 Mon Sep 17 00:00:00 2001 From: yunchan Date: Sun, 15 Nov 2026 09:47:33 +0900 Subject: [PATCH] macos: copy frames outside the queue the raster thread waits on (Kitedesk T-4204) The whole frame, buffer and copy included, ran in queue.sync, and the engine's raster thread takes the newest frame in copyPixelBuffer on the same queue, so it waited for the copy: R T-4201 measured a p90 of 4.0 ms at 60 Hz (6.5 ms at 120 Hz), and the raster thread draws the whole window. The frame is now copied into its pool buffer outside the queue; the queue only guards the pool, the newest frame and the handed buffers, so it is held for a pointer swap. Tests: raster_does_not_wait_for_copies passes (4K frames back to back: the raster's median wait was 0.9 to 1.1 ms in three runs, up to 537 of 600 calls over 1 ms; now 0.003 ms, at most 0.032 ms). Co-Authored-By: Claude Opus 5.5 --- macos/Classes/TextRgba.swift | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/macos/Classes/TextRgba.swift b/macos/Classes/TextRgba.swift index 18f19b0..9737716 100644 --- a/macos/Classes/TextRgba.swift +++ b/macos/Classes/TextRgba.swift @@ -6,8 +6,9 @@ // // Modified by the Kitedesk fork (2026, T-4204): frames go into the // IOSurface-backed buffers of a pool, which does not hand out again the -// buffers last handed to the engine; rows are copied one by one when their -// stride differs from the pixel buffer's. +// buffers last handed to the engine, and are copied outside the queue the +// raster thread waits on; rows are copied one by one when their stride +// differs from the pixel buffer's. // import Foundation @@ -21,7 +22,7 @@ import CoreVideo private var width: Int = 0 private var height: Int = 0 private let queue = DispatchQueue(label: "text_rgba_sync_queue") - // Buffers of width x height. + // Buffers of width x height. With data, width, height and handed, guarded by queue. private var pool: CVPixelBufferPool? // The buffers last handed to the engine, the newest last: not written again. private var handed: [CVPixelBuffer] = [] @@ -57,17 +58,13 @@ import CoreVideo guard width > 0 && height > 0 && len >= stride * height else { return false } - if (pool == nil || self.width != width || self.height != height) { - pool = TextRgba.newPool(width: width, height: height) - self.width = width - self.height = height - } var pixelBufferCopy: CVPixelBuffer? - guard let pool = pool, + guard let pool = poolOf(width: width, height: height), CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, pool, &pixelBufferCopy) == kCVReturnSuccess else { return false } + // Outside the queue: the raster thread waits on it for the newest frame. CVPixelBufferLockBaseAddress(pixelBufferCopy!, []) let ptr = CVPixelBufferGetBaseAddress(pixelBufferCopy!)! let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBufferCopy!) @@ -79,8 +76,10 @@ import CoreVideo } } CVPixelBufferUnlockBaseAddress(pixelBufferCopy!, []) - self.data = pixelBufferCopy - if textureId != -1 && self.data != nil { + queue.sync { + self.data = pixelBufferCopy + } + if textureId != -1 { registry?.textureFrameAvailable(textureId) return true } else { @@ -88,11 +87,21 @@ import CoreVideo } } - @objc public func markFrameAvaliableRaw(buffer: UnsafePointer, len: Int, width: Int, height: Int, stride_align: Int) -> Bool { + // The pool of buffers of width x height, a new one for a new size. + private func poolOf(width: Int, height: Int) -> CVPixelBufferPool? { queue.sync { - _markFrameAvaliable(buffer: buffer, len: len, width: width, height: height, stride_align: stride_align) + if (pool == nil || self.width != width || self.height != height) { + pool = TextRgba.newPool(width: width, height: height) + self.width = width + self.height = height + } + return pool } } + + @objc public func markFrameAvaliableRaw(buffer: UnsafePointer, len: Int, width: Int, height: Int, stride_align: Int) -> Bool { + _markFrameAvaliable(buffer: buffer, len: len, width: width, height: height, stride_align: stride_align) + } private static func newPool(width: Int, height: Int) -> CVPixelBufferPool? {