flutter_texture_rgba_renderer/macos/Classes/TextureRgbaRendererPlugin.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

86 lines
3.2 KiB
Swift

// Modified by the Kitedesk fork (2026, B252): closeTexture drops the frame,
// closes the texture to later frames, and removes it only with "recycle".
import Cocoa
import FlutterMacOS
public class TextureRgbaRendererPlugin: NSObject, FlutterPlugin {
private var renderer: Dictionary<Int64, TextRgba> = [:]
private var texture_registry: FlutterTextureRegistry?
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "texture_rgba_renderer", binaryMessenger: registrar.messenger)
let instance = TextureRgbaRendererPlugin()
instance.setTextureRegistry(registry: registrar.textures)
registrar.addMethodCallDelegate(instance, channel: channel)
}
public func setTextureRegistry(registry: FlutterTextureRegistry) {
texture_registry = registry
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
case "getPlatformVersion":
result("macOS " + ProcessInfo.processInfo.operatingSystemVersionString)
case "createTexture":
let args = call.arguments as! Dictionary<String, Any?>
let key = args["key"] as! Int64
if (renderer[key] != nil) {
result(-1)
} else {
let textRgba = TextRgba.new(registry: texture_registry)
renderer[key] = textRgba
result(textRgba.textureId)
}
case "closeTexture":
let args = call.arguments as! Dictionary<String, Any?>
let key = args["key"] as! Int64
// "recycle": true promises that nothing pushes frames to the pointer
// from getTexturePtr any more (it is not retained), so the texture may
// be released; otherwise it stays here, closed.
let recycle = (args["recycle"] as? Bool) ?? false
if let textRgba = renderer[key] {
if (-1 != textRgba.textureId) {
texture_registry?.unregisterTexture(textRgba.textureId)
}
textRgba.close()
if (recycle) {
renderer.removeValue(forKey: key)
}
result(true)
} else {
result(false)
}
case "onRgba":
let args = call.arguments as! Dictionary<String, Any?>
let key = args["key"] as! Int64
let data = args["data"] as! FlutterStandardTypedData
let height = args["height"] as! Int
let width = args["width"] as! Int
let stride_align = args["stride_align"] as! Int
let textureRgba = renderer[key]
if (textureRgba == nil) {
result(false)
} else {
result(textureRgba!.markFrameAvaliable(data: data.data, width: width, height: height, stride_align: stride_align))
}
case "getTexturePtr":
let args = call.arguments as! Dictionary<String, Any?>
let key = args["key"] as! Int64
let textureRgba = renderer[key]
if (textureRgba == nil) {
result(0)
} else {
let unmanaged = Unmanaged.passUnretained(textureRgba!)
let intAddr = UInt(bitPattern: unmanaged.toOpaque())
result(intAddr)
}
default:
result(FlutterMethodNotImplemented)
}
}
}