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>
This commit is contained in:
parent
2c334da6f9
commit
7af0d01118
1 changed files with 42 additions and 14 deletions
|
|
@ -4,8 +4,10 @@
|
||||||
//
|
//
|
||||||
// Created by kingtous on 2023/2/17.
|
// Created by kingtous on 2023/2/17.
|
||||||
//
|
//
|
||||||
// Modified by the Kitedesk fork (2026, T-4204): rows are copied one by one
|
// Modified by the Kitedesk fork (2026, T-4204): frames go into the
|
||||||
// when their stride differs from the pixel buffer's.
|
// 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 Foundation
|
||||||
|
|
@ -19,6 +21,12 @@ import CoreVideo
|
||||||
private var width: Int = 0
|
private var width: Int = 0
|
||||||
private var height: Int = 0
|
private var height: Int = 0
|
||||||
private let queue = DispatchQueue(label: "text_rgba_sync_queue")
|
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 {
|
public static func new(registry: FlutterTextureRegistry?) -> TextRgba {
|
||||||
let textRgba = TextRgba()
|
let textRgba = TextRgba()
|
||||||
|
|
@ -32,6 +40,12 @@ import CoreVideo
|
||||||
if (data == nil) {
|
if (data == nil) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
if (handed.last !== data) {
|
||||||
|
handed.append(data!)
|
||||||
|
if (handed.count > TextRgba.handedFramesKept) {
|
||||||
|
handed.removeFirst()
|
||||||
|
}
|
||||||
|
}
|
||||||
return Unmanaged.passRetained(data!)
|
return Unmanaged.passRetained(data!)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -43,17 +57,14 @@ import CoreVideo
|
||||||
guard width > 0 && height > 0 && len >= stride * height else {
|
guard width > 0 && height > 0 && len >= stride * height else {
|
||||||
return false
|
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?
|
var pixelBufferCopy: CVPixelBuffer?
|
||||||
// macOS only support 32BGRA currently.
|
guard let pool = pool,
|
||||||
let dict: [String: Any] = [
|
CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, pool, &pixelBufferCopy) == kCVReturnSuccess else {
|
||||||
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 {
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -69,8 +80,6 @@ import CoreVideo
|
||||||
}
|
}
|
||||||
CVPixelBufferUnlockBaseAddress(pixelBufferCopy!, [])
|
CVPixelBufferUnlockBaseAddress(pixelBufferCopy!, [])
|
||||||
self.data = pixelBufferCopy
|
self.data = pixelBufferCopy
|
||||||
self.width = width
|
|
||||||
self.height = height
|
|
||||||
if textureId != -1 && self.data != nil {
|
if textureId != -1 && self.data != nil {
|
||||||
registry?.textureFrameAvailable(textureId)
|
registry?.textureFrameAvailable(textureId)
|
||||||
return true
|
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 {
|
@objc public func markFrameAvaliable(data: Data, width: Int, height: Int, stride_align: Int) -> Bool {
|
||||||
data.withUnsafeBytes { buffer in
|
data.withUnsafeBytes { buffer in
|
||||||
markFrameAvaliableRaw(
|
markFrameAvaliableRaw(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue