// Modified by the Kitedesk fork (2026, T-4204): closeTexture lets the // texture's buffers go, and keeps the closed texture for good unless "recycle" // is set; the textures of an engine going away are kept the same way. import Cocoa import FlutterMacOS public class TextureRgbaRendererPlugin: NSObject, FlutterPlugin { private var renderer: Dictionary = [:] private var texture_registry: FlutterTextureRegistry? // Textures closed while frames may still be pushed to their pointer from getTexturePtr: // closeTexture without "recycle" (a tab moving to another window keeps its session's // pointer), or an engine going away first. They are never freed, so such a frame finds a // closed texture, not freed memory, and their address is never another texture's (ABA). // Each keeps the object, not its buffers. Main thread only. private static var retired: [TextRgba] = [] 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) } deinit { // The engine is going away, and sessions may still push frames to these textures. for textRgba in renderer.values { textRgba.close() TextureRgbaRendererPlugin.retired.append(textRgba) } } 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 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 let key = args["key"] as! Int64 // "recycle": true promises that nothing pushes frames to the pointer any more. let recycle = args["recycle"] as? Bool ?? false if let textRgba = renderer.removeValue(forKey: key) { textRgba.close() if (-1 != textRgba.textureId) { texture_registry?.unregisterTexture(textRgba.textureId) } if (!recycle) { TextureRgbaRendererPlugin.retired.append(textRgba) } result(true) } else { result(false) } case "onRgba": let args = call.arguments as! Dictionary 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 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) } } }