flutter_texture_rgba_renderer/lib/texture_rgba_renderer.dart
yunchan 6c3425539f 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>
2026-11-15 09:56:22 +09:00

48 lines
1.8 KiB
Dart

// Modified by the Kitedesk fork (2026, B212/B252/T-4204): closeTexture ownership.
import 'dart:typed_data';
export './texture_rgba_ffi.dart' show Native;
import 'texture_rgba_renderer_platform_interface.dart';
class TextureRgbaRenderer {
/// Create a texture with unique [key].
///
/// @return a texture id which can be used with
/// ```dart
/// Texture(textureId: textureId)
/// ```
Future<int> createTexture(int key) {
return TextureRgbaRendererPlatform.instance.createTexture(key);
}
/// Close a texture with unique identifier [key].
///
/// Pass [recycle] = true only when nothing calls `onRgba` with the pointer
/// from [getTexturePtr] any more. On Linux the texture is then kept for the
/// next [createTexture] of the same window (Kitedesk fork, B212).
/// On Windows it may be destroyed only after the engine finishes unregistering
/// 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.
Future<bool> closeTexture(int key, {bool recycle = false}) {
return TextureRgbaRendererPlatform.instance
.closeTexture(key, recycle: recycle);
}
/// Provide the rgba data to the texture.
Future<bool> onRgba(
int key, Uint8List data, int height, int width, int strideAlign) {
return TextureRgbaRendererPlatform.instance
.onRgba(key, data, height, width, strideAlign);
}
Future<int> getTexturePtr(int key) {
return TextureRgbaRendererPlatform.instance.getTexturePtr(key);
}
}