flutter_texture_rgba_renderer/macos/Classes/TextRgba.swift
yunchan 7af0d01118 macos: take frame buffers from a pool, keep the two last handed (Kitedesk T-4204)
Every frame made a new IOSurface-backed CVPixelBuffer (33 MB at 4K), and
the engine let it go soon after. R T-4201 measured 2.2 to 7.8 ms per 4K
frame alone, up to 110 ms, and a 6.5 ms median at 60 fps; upstream #16922
saw 37% of the CPU and memory swings of 1.85 to 2.43 GB.

Frames now go into the buffers of a CVPixelBufferPool with IOSurface
properties, made again when the frame size changes. A pool hands a buffer
out again as soon as nobody holds it, even while its IOSurface backs a live
Metal texture (measured), and engine a18df97c lets go of the pixel buffer and
of its CVMetalTexture as soon as it has the Metal texture, which Skia then
draws until the next frame. So the texture keeps the buffers it last handed
to the engine: the one drawn, and the one drawn before it, which GPU work
queued meanwhile may still read (the plugin cannot see when the GPU is done,
flutter#157379; inferred). With the newest frame and the one being written, a
texture uses three buffers while the engine keeps up and four when it falls
behind.

Tests: pool_reuses_buffers passes (60 IOSurfaces for 60 frames of a size,
now at most 4). drawn_frame_is_not_overwritten fails when no handed buffer
is kept, frame_drawn_before_is_not_overwritten when one is; both pass with
two.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 09:56:22 +09:00

129 lines
5.1 KiB
Swift

//
// TextRgba.swift
// texture_rgba_renderer
//
// Created by kingtous on 2023/2/17.
//
// 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
import FlutterMacOS
import CoreVideo
@objc public class TextRgba: NSObject, FlutterTexture {
public var textureId: Int64 = -1
private var registry: FlutterTextureRegistry?
private var data: CVPixelBuffer?
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()
textRgba.registry = registry
textRgba.textureId = registry?.register(textRgba) ?? -1
return textRgba
}
public func copyPixelBuffer() -> Unmanaged<CVPixelBuffer>? {
queue.sync {
if (data == nil) {
return nil
}
if (handed.last !== data) {
handed.append(data!)
if (handed.count > TextRgba.handedFramesKept) {
handed.removeFirst()
}
}
return Unmanaged.passRetained(data!)
}
}
private func _markFrameAvaliable(buffer: UnsafePointer<UInt8>, len: Int, width: Int, height: Int, stride_align: Int) -> Bool {
// The rows of buffer are padded to stride_align.
let row = width * 4
let stride = stride_align > 1 ? (row + stride_align - 1) / stride_align * stride_align : row
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,
CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, pool, &pixelBufferCopy) == kCVReturnSuccess else {
return false
}
CVPixelBufferLockBaseAddress(pixelBufferCopy!, [])
let ptr = CVPixelBufferGetBaseAddress(pixelBufferCopy!)!
let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBufferCopy!)
if (bytesPerRow == stride) {
memcpy(ptr, buffer, stride * height)
} else {
for y in 0..<height {
memcpy(ptr + y * bytesPerRow, buffer + y * stride, row)
}
}
CVPixelBufferUnlockBaseAddress(pixelBufferCopy!, [])
self.data = pixelBufferCopy
if textureId != -1 && self.data != nil {
registry?.textureFrameAvailable(textureId)
return true
} else {
return false
}
}
@objc public func markFrameAvaliableRaw(buffer: UnsafePointer<UInt8>, len: Int, width: Int, height: Int, stride_align: Int) -> Bool {
queue.sync {
_markFrameAvaliable(buffer: buffer, len: len, width: width, height: height, stride_align: stride_align)
}
}
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(
buffer: buffer.baseAddress!.assumingMemoryBound(to: UInt8.self),
len: data.count,
width: width,
height: height,
stride_align: stride_align)
}
}
}