diff --git a/macos/Classes/TextRgba.swift b/macos/Classes/TextRgba.swift index c2860bf..18f19b0 100644 --- a/macos/Classes/TextRgba.swift +++ b/macos/Classes/TextRgba.swift @@ -4,8 +4,10 @@ // // Created by kingtous on 2023/2/17. // -// Modified by the Kitedesk fork (2026, T-4204): rows are copied one by one -// when their stride differs from the pixel buffer's. +// 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. // import Foundation @@ -19,6 +21,12 @@ 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. + private var pool: CVPixelBufferPool? + // The buffers last handed to the engine, the newest last: not written again. + private var handed: [CVPixelBuffer] = [] + // 2: the engine (3.24.5) drops its CVMetalTexture at once, so GPU work may still read the buffer handed before the drawn one (flutter#157379, inferred). + private static let handedFramesKept = 2 public static func new(registry: FlutterTextureRegistry?) -> TextRgba { let textRgba = TextRgba() @@ -32,6 +40,12 @@ import CoreVideo if (data == nil) { return nil } + if (handed.last !== data) { + handed.append(data!) + if (handed.count > TextRgba.handedFramesKept) { + handed.removeFirst() + } + } return Unmanaged.passRetained(data!) } } @@ -43,17 +57,14 @@ 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? - // macOS only support 32BGRA currently. - let dict: [String: Any] = [ - kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA, - kCVPixelBufferMetalCompatibilityKey as String: true, - kCVPixelBufferOpenGLCompatibilityKey as String: true, - // https://developer.apple.com/forums/thread/712709 - kCVPixelBufferBytesPerRowAlignmentKey as String: 64 - ] - let result = CVPixelBufferCreate(kCFAllocatorDefault, width, height, kCVPixelFormatType_32BGRA, dict as CFDictionary, &pixelBufferCopy) - guard result == kCVReturnSuccess else { + guard let pool = pool, + CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, pool, &pixelBufferCopy) == kCVReturnSuccess else { return false } @@ -69,8 +80,6 @@ import CoreVideo } CVPixelBufferUnlockBaseAddress(pixelBufferCopy!, []) self.data = pixelBufferCopy - self.width = width - self.height = height if textureId != -1 && self.data != nil { registry?.textureFrameAvailable(textureId) return true @@ -86,6 +95,25 @@ import CoreVideo } + private static func newPool(width: Int, height: Int) -> CVPixelBufferPool? { + // macOS only support 32BGRA currently. + let dict: [String: Any] = [ + kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA, + kCVPixelBufferWidthKey as String: width, + kCVPixelBufferHeightKey as String: height, + kCVPixelBufferIOSurfacePropertiesKey as String: [String: Any](), + kCVPixelBufferMetalCompatibilityKey as String: true, + kCVPixelBufferOpenGLCompatibilityKey as String: true, + // https://developer.apple.com/forums/thread/712709 + kCVPixelBufferBytesPerRowAlignmentKey as String: 64 + ] + // The handed buffers, the newest frame and the one being written. + let poolDict: [String: Any] = [kCVPixelBufferPoolMinimumBufferCountKey as String: handedFramesKept + 2] + var pool: CVPixelBufferPool? + CVPixelBufferPoolCreate(kCFAllocatorDefault, poolDict as CFDictionary, dict as CFDictionary, &pool) + return pool + } + @objc public func markFrameAvaliable(data: Data, width: Int, height: Int, stride_align: Int) -> Bool { data.withUnsafeBytes { buffer in markFrameAvaliableRaw(