macos: let closed textures' buffers go, keep closed identities (Kitedesk T-4204)

closeTexture unregistered the texture but kept it in the plugin's map with
its last frame (33 MB at 4K) until the engine went away, and the texture
went on taking and announcing frames pushed to its pointer. Kitedesk makes a
texture per display, so each display switch and closed tab leaked a frame.

closeTexture now takes the texture out of the map and closes it: frames
pushed afterwards are dropped and not announced, the engine gets no frame any
more, and the frames, the handed buffers and the pool go. As on Windows
(B252), a texture closed without "recycle" may still get frames through the
pointer from getTexturePtr (a tab moving to another window keeps its
session's pointer), so it is kept for the process lifetime and its address
is never another texture's; with "recycle" it is freed once the engine has
unregistered it. The textures still open when the engine goes away are closed
and kept the same way: the registry's reference to them used to keep them
alive, announcing every frame to a registry without an engine. A closed
texture no longer holds the registry.

Tests: closing_frees_frame_buffers (634 MB more after 20 4K textures, now
0.2 MB), closed_texture_drops_late_frames,
closing_keeps_only_unrecycled_textures, open_textures_outlive_their_engine
and frames_racing_close pass: 13 of 13.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
yunchan 2026-11-15 09:52:18 +09:00
parent 2a36975b62
commit 6c3425539f
3 changed files with 55 additions and 11 deletions

View file

@ -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<UInt8>, len: Int, width: Int, height: Int, stride_align: Int) -> Bool {
_markFrameAvaliable(buffer: buffer, len: len, width: width, height: height, stride_align: stride_align)
}