diff --git a/lib/texture_rgba_renderer.dart b/lib/texture_rgba_renderer.dart index f8d1011..cca1094 100644 --- a/lib/texture_rgba_renderer.dart +++ b/lib/texture_rgba_renderer.dart @@ -1,4 +1,4 @@ -// Modified by the Kitedesk fork (2026, B212/B252): closeTexture ownership. +// Modified by the Kitedesk fork (2026, B212/B252/T-4204): closeTexture ownership. import 'dart:typed_data'; export './texture_rgba_ffi.dart' show Native; @@ -25,6 +25,8 @@ class TextureRgbaRenderer { /// it. Without [recycle], Windows retains its closed identity for the process /// lifetime so late raw-pointer frames cannot reach freed or reused memory; /// its frame buffers are freed after unregistering (Kitedesk fork, B252). + /// macOS frees the frame buffers on closing and treats [recycle] as Windows + /// does (Kitedesk fork, T-4204). /// Other platforms ignore [recycle]. /// /// @return a boolean to indicate whether the operation is sucessfully executed. diff --git a/macos/Classes/TextRgba.swift b/macos/Classes/TextRgba.swift index ba405be..704d7b8 100644 --- a/macos/Classes/TextRgba.swift +++ b/macos/Classes/TextRgba.swift @@ -8,7 +8,8 @@ // 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. +// differs from the pixel buffer's; frames are announced on the main queue; +// close() drops the frames pushed after it and lets the buffers go. // import Foundation @@ -17,14 +18,15 @@ import CoreVideo @objc public class TextRgba: NSObject, FlutterTexture { public var textureId: Int64 = -1 - // Main thread only. + // Main thread only. Nil once closed. 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. + // Buffers of width x height. With data, width, height, handed and closed, guarded by queue. private var pool: CVPixelBufferPool? + private var closed = false // 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). @@ -77,10 +79,14 @@ import CoreVideo } } CVPixelBufferUnlockBaseAddress(pixelBufferCopy!, []) - queue.sync { + let open = queue.sync { () -> Bool in + if (closed) { + return false + } self.data = pixelBufferCopy + return true } - if textureId != -1 { + if open && textureId != -1 { // On the platform (main) thread, which the engine checks. DispatchQueue.main.async { self.registry?.textureFrameAvailable(self.textureId) @@ -91,10 +97,10 @@ import CoreVideo } } - // The pool of buffers of width x height, a new one for a new size. + // The pool of buffers of width x height, a new one for a new size; none once closed. private func poolOf(width: Int, height: Int) -> CVPixelBufferPool? { queue.sync { - if (pool == nil || self.width != width || self.height != height) { + if (!closed && (pool == nil || self.width != width || self.height != height)) { pool = TextRgba.newPool(width: width, height: height) self.width = width self.height = height @@ -103,6 +109,18 @@ import CoreVideo } } + // Main thread: closeTexture, or the engine going away. Frames pushed from now on are + // dropped and not announced, the engine gets no frame any more, and the buffers go. + func close() { + registry = nil + queue.sync { + closed = true + data = nil + handed = [] + pool = nil + } + } + @objc public func markFrameAvaliableRaw(buffer: UnsafePointer, len: Int, width: Int, height: Int, stride_align: Int) -> Bool { _markFrameAvaliable(buffer: buffer, len: len, width: width, height: height, stride_align: stride_align) } diff --git a/macos/Classes/TextureRgbaRendererPlugin.swift b/macos/Classes/TextureRgbaRendererPlugin.swift index 8c991a6..56b771e 100644 --- a/macos/Classes/TextureRgbaRendererPlugin.swift +++ b/macos/Classes/TextureRgbaRendererPlugin.swift @@ -1,3 +1,7 @@ +// 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 @@ -5,6 +9,12 @@ 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) @@ -12,6 +22,14 @@ public class TextureRgbaRendererPlugin: NSObject, FlutterPlugin { 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 @@ -34,9 +52,15 @@ public class TextureRgbaRendererPlugin: NSObject, FlutterPlugin { case "closeTexture": let args = call.arguments as! Dictionary let key = args["key"] as! Int64 - if (renderer[key] != nil) { - if (-1 != renderer[key]?.textureId) { - texture_registry?.unregisterTexture(renderer[key]?.textureId ?? -1) + // "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 {