flutter_texture_rgba_renderer/macos/Classes/TextRgba.swift
yunchan f38b9420ac B252: keep closed textures valid until the engine is done with them
Windows: closeTexture hands the texture to UnregisterTexture's callback and
frees its frame buffers there; frames pushed after close are dropped. Without
"recycle" the object is kept for the process lifetime (the session may still
hold its pointer); an engine that shuts down keeps its open textures the same
way. A duplicate createTexture key gets -1, getTexturePtr of an unknown key 0.

macOS: closeTexture drops the frame and closes the texture to later frames;
only "recycle" removes it from the plugin.

Linux: the frame handed to the engine last is freed at that engine's next
copy_pixels or once the engine has shut down, not at close: from Flutter 3.32
the raster thread can still be uploading it.

Tests: windows/test/run_tests.sh (plain, --asan, --tsan) and
linux/test/run_tests.sh (plain, --asan).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 03:19:05 +09:00

101 lines
3.3 KiB
Swift

//
// TextRgba.swift
// texture_rgba_renderer
//
// Created by kingtous on 2023/2/17.
//
// Modified by the Kitedesk fork (2026, B252): close() drops the frame and
// every frame pushed afterwards.
//
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 var closed = false
private let queue = DispatchQueue(label: "text_rgba_sync_queue")
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
}
return Unmanaged.passRetained(data!)
}
}
// Called by closeTexture. The session may keep pushing frames to the
// pointer from getTexturePtr; they are dropped.
func close() {
queue.sync {
closed = true
data = nil
}
}
private func _markFrameAvaliable(buffer: UnsafePointer<UInt8>, len: Int, width: Int, height: Int, stride_align: Int) -> Bool {
if closed {
return false
}
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 {
return false
}
CVPixelBufferLockBaseAddress(pixelBufferCopy!, [])
let ptr = CVPixelBufferGetBaseAddress(pixelBufferCopy!)!
memcpy(ptr, buffer, len)
CVPixelBufferUnlockBaseAddress(pixelBufferCopy!, [])
self.data = pixelBufferCopy
self.width = width
self.height = height
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)
}
}
@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)
}
}
}