textureFrameAvailable was called on the thread that pushed the frame, a Kitedesk video thread. The engine wants it on the platform thread (Shell::OnPlatformViewMarkTextureFrameAvailable checks it, engine a18df97c), as it wants registerTexture and unregisterTexture, which already run there; Kitedesk's own GPU texture (KdMatoyaTexture.swift) posts its notices the same way. The notice is now posted to the main queue; the registry is only used on the main thread. Tests: frames_are_announced_on_the_main_thread passes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
142 lines
5.7 KiB
Swift
142 lines
5.7 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, 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; frames are announced on the main queue.
|
|
//
|
|
|
|
import Foundation
|
|
import FlutterMacOS
|
|
import CoreVideo
|
|
|
|
@objc public class TextRgba: NSObject, FlutterTexture {
|
|
public var textureId: Int64 = -1
|
|
// Main thread only.
|
|
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. 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] = []
|
|
// 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
|
|
}
|
|
var pixelBufferCopy: CVPixelBuffer?
|
|
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!)
|
|
if (bytesPerRow == stride) {
|
|
memcpy(ptr, buffer, stride * height)
|
|
} else {
|
|
for y in 0..<height {
|
|
memcpy(ptr + y * bytesPerRow, buffer + y * stride, row)
|
|
}
|
|
}
|
|
CVPixelBufferUnlockBaseAddress(pixelBufferCopy!, [])
|
|
queue.sync {
|
|
self.data = pixelBufferCopy
|
|
}
|
|
if textureId != -1 {
|
|
// On the platform (main) thread, which the engine checks.
|
|
DispatchQueue.main.async {
|
|
self.registry?.textureFrameAvailable(self.textureId)
|
|
}
|
|
return true
|
|
} else {
|
|
return false
|
|
}
|
|
}
|
|
|
|
// 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 {
|
|
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<UInt8>, 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? {
|
|
// 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)
|
|
}
|
|
}
|
|
}
|
|
|
|
|