From 8cd34a80ffae73d6e4bb007badf64b8a0a7325ac Mon Sep 17 00:00:00 2001 From: yunchan Date: Sun, 11 Oct 2026 15:38:06 +0900 Subject: [PATCH] linux: recycle closed textures per engine, closeTexture(recycle:) (Kitedesk B212) Each session created a new TextureRgba and closeTexture never released it, so the engine's FlPixelBufferTexture kept its GL texture (one frame of GPU or, with llvmpipe, process memory per session). Releasing the object is not safe: FlPixelBufferTexture deletes its GL texture in dispose without selecting a GL context, so a release from another window deletes that window's texture of the same name (upstream PR #2 turned the remaining window white). closeTexture(key, recycle: true) now keeps the texture in a list of the plugin instance, i.e. of the engine (window) that created it, and the next createTexture of that engine registers it again. Its GL texture is only ever touched by that engine's populate in its own context, nothing is released, and the GL memory is bounded by the number of textures a window had open at once. When the engine goes away the recycled textures stay referenced for the same reason. Without recycle (old callers, or a tab that moves to another window while its session still holds the pointer) a closed texture stays terminated and is never handed out again. With no frame waiting, copy_pixels hands out one transparent pixel instead of no buffer, so a recycled texture cannot show the last frame of its previous session before the first new frame. Dart: closeTexture(int key, {bool recycle = false}) sends "recycle"; other platforms ignore it. Tests: all ten Linux lifecycle tests pass (clang and gcc, -Wall -Werror, also under ASan/UBSan); Dart 4/4. Co-Authored-By: Claude Opus 5.5 (1M context) --- lib/texture_rgba_renderer.dart | 11 +++++- lib/texture_rgba_renderer_method_channel.dart | 7 ++-- ...ture_rgba_renderer_platform_interface.dart | 3 +- .../texture_rgba_renderer.h | 20 +++++++++- linux/texture_rgba_renderer_plugin.cc | 37 +++++++++++++++++-- ...ure_rgba_renderer_method_channel_test.dart | 17 +++++++++ test/texture_rgba_renderer_test.dart | 23 +++++++++++- 7 files changed, 106 insertions(+), 12 deletions(-) diff --git a/lib/texture_rgba_renderer.dart b/lib/texture_rgba_renderer.dart index 2502177..04ee24a 100644 --- a/lib/texture_rgba_renderer.dart +++ b/lib/texture_rgba_renderer.dart @@ -1,3 +1,4 @@ +// Modified by the Kitedesk fork (2026, B212): closeTexture takes `recycle`. import 'dart:typed_data'; export './texture_rgba_ffi.dart' show Native; @@ -17,9 +18,15 @@ class TextureRgbaRenderer { /// 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 instead of being leaked + /// (Kitedesk fork, B212); other platforms ignore it. + /// /// @return a boolean to indicate whether the operation is sucessfully executed. - Future closeTexture(int key) { - return TextureRgbaRendererPlatform.instance.closeTexture(key); + Future closeTexture(int key, {bool recycle = false}) { + return TextureRgbaRendererPlatform.instance + .closeTexture(key, recycle: recycle); } /// Provide the rgba data to the texture. diff --git a/lib/texture_rgba_renderer_method_channel.dart b/lib/texture_rgba_renderer_method_channel.dart index 6137a94..ac61506 100644 --- a/lib/texture_rgba_renderer_method_channel.dart +++ b/lib/texture_rgba_renderer_method_channel.dart @@ -1,3 +1,4 @@ +// Modified by the Kitedesk fork (2026, B212): closeTexture takes `recycle`. // ignore: unused_import, unnecessary_import import 'dart:io'; // Compatible with lint in Flutter 3.0.5 @@ -30,9 +31,9 @@ class MethodChannelTextureRgbaRenderer extends TextureRgbaRendererPlatform { } @override - Future closeTexture(int key) async { - return await methodChannel - .invokeMethod('closeTexture', {"key": key}) ?? + Future closeTexture(int key, {bool recycle = false}) async { + return await methodChannel.invokeMethod( + 'closeTexture', {"key": key, "recycle": recycle}) ?? false; } diff --git a/lib/texture_rgba_renderer_platform_interface.dart b/lib/texture_rgba_renderer_platform_interface.dart index fd1d491..f28a650 100644 --- a/lib/texture_rgba_renderer_platform_interface.dart +++ b/lib/texture_rgba_renderer_platform_interface.dart @@ -1,3 +1,4 @@ +// Modified by the Kitedesk fork (2026, B212): closeTexture takes `recycle`. import 'dart:typed_data'; import 'package:flutter/foundation.dart'; @@ -35,7 +36,7 @@ abstract class TextureRgbaRendererPlatform extends PlatformInterface { throw UnimplementedError('createTexture() has not been implemented.'); } - Future closeTexture(int key) { + Future closeTexture(int key, {bool recycle = false}) { throw UnimplementedError('closeTexture() has not been implemented.'); } diff --git a/linux/include/texture_rgba_renderer/texture_rgba_renderer.h b/linux/include/texture_rgba_renderer/texture_rgba_renderer.h index f2e17a7..1a575c6 100644 --- a/linux/include/texture_rgba_renderer/texture_rgba_renderer.h +++ b/linux/include/texture_rgba_renderer/texture_rgba_renderer.h @@ -1,5 +1,7 @@ // Modified by the Kitedesk fork (2026, B212): frame buffers are freed when the -// texture is closed and when the object is finalized. +// texture is closed and when the object is finalized; a closed texture can be +// restarted for reuse; with no frame waiting, one transparent pixel is handed +// out instead of no buffer. #ifndef TEXTURE_RGBA_RENDERER_H #define TEXTURE_RGBA_RENDERER_H @@ -66,6 +68,14 @@ static void texture_rgba_terminate(TextureRgba* self) { g_mutex_unlock(&self->mutex); } +// Makes a texture that was closed for recycling usable again (its frame +// buffers were freed when it was closed). +static void texture_rgba_restart(TextureRgba* self) { + g_mutex_lock(&self->mutex); + g_atomic_int_set(&self->terminated, FALSE); + g_mutex_unlock(&self->mutex); +} + static gboolean texture_rgba_is_terminate(TextureRgba* self) { return g_atomic_int_get(&self->terminated); } @@ -109,6 +119,14 @@ static gboolean texture_rgba_copy_pixels(FlPixelBufferTexture* texture, *error = g_error_new(g_quark_from_static_string("TextureRgba Renderer"), -1, "the texture is already terminated, ignoring."); return FALSE; } else { + // No frame yet (first raster frame, or a recycled texture). Hand out one + // transparent pixel instead of no buffer: the engine would otherwise + // re-specify the GL texture without data and could show its previous + // content, e.g. the last frame of the session that used this texture. + static const uint8_t no_frame[4] = {0, 0, 0, 0}; + *out_buffer = no_frame; + *width = 1; + *height = 1; g_mutex_unlock(&self->mutex); // *error = g_error_new(g_quark_from_static_string("TextureRgba Renderer"), 1, "waiting for new incoming images."); return TRUE; diff --git a/linux/texture_rgba_renderer_plugin.cc b/linux/texture_rgba_renderer_plugin.cc index 1b62124..5037af1 100644 --- a/linux/texture_rgba_renderer_plugin.cc +++ b/linux/texture_rgba_renderer_plugin.cc @@ -1,3 +1,7 @@ +// Modified by the Kitedesk fork (2026, B212): closeTexture with +// "recycle": true keeps the texture for the next createTexture of the same +// engine instead of leaking it. + #include "include/texture_rgba_renderer/texture_rgba_renderer_plugin.h" #include "include/texture_rgba_renderer/texture_rgba_renderer.h" @@ -19,6 +23,10 @@ struct _TextureRgbaRendererPlugin { GObject parent_instance; FlTextureRegistrar *texture_registrar; + // Textures closed with "recycle": true, for the next createTexture of this + // engine (window). Their engine GL texture belongs to this window's GL + // context, so they are never released and never given to another engine. + GPtrArray *recycled; }; G_DEFINE_TYPE(TextureRgbaRendererPlugin, texture_rgba_renderer_plugin, g_object_get_type()) @@ -42,7 +50,13 @@ static void texture_rgba_renderer_plugin_handle_method_call( } else { - TextureRgba* texture_rgba = texture_rgba_new(self->texture_registrar); + TextureRgba* texture_rgba = nullptr; + if (self->recycled->len > 0) { + texture_rgba = TEXTURE_RGBA(g_ptr_array_remove_index_fast(self->recycled, self->recycled->len - 1)); + texture_rgba_restart(texture_rgba); + } else { + texture_rgba = texture_rgba_new(self->texture_registrar); + } auto texture_id = reinterpret_cast(FL_TEXTURE(texture_rgba)); texture_rgba->texture_id = texture_id; g_renderer_map.insert(std::make_pair(key, texture_rgba)); @@ -55,11 +69,19 @@ static void texture_rgba_renderer_plugin_handle_method_call( { auto args = fl_method_call_get_args(method_call); int64_t key = fl_value_get_int(fl_value_lookup_string(args, "key")); + // The caller sets "recycle" only when nothing can call + // FlutterRgbaRendererPluginOnRgba with this texture any more. + FlValue* recycle = fl_value_lookup_string(args, "recycle"); if (g_renderer_map.find(key) != g_renderer_map.end()) { - fl_texture_registrar_unregister_texture(self->texture_registrar, FL_TEXTURE(g_renderer_map[key])); - texture_rgba_terminate(g_renderer_map[key]); + TextureRgba* texture_rgba = g_renderer_map[key]; + fl_texture_registrar_unregister_texture(self->texture_registrar, FL_TEXTURE(texture_rgba)); + texture_rgba_terminate(texture_rgba); g_renderer_map.erase(key); + if (recycle != nullptr && fl_value_get_type(recycle) == FL_VALUE_TYPE_BOOL && fl_value_get_bool(recycle) && + texture_rgba->texture_registrar == self->texture_registrar) { + g_ptr_array_add(self->recycled, texture_rgba); + } } response = FL_METHOD_RESPONSE(fl_method_success_response_new(fl_value_new_bool(true))); } @@ -104,6 +126,11 @@ static void texture_rgba_renderer_plugin_handle_method_call( static void texture_rgba_renderer_plugin_dispose(GObject *object) { + TextureRgbaRendererPlugin *self = TEXTURE_RGBA_RENDERER_PLUGIN(object); + // The engine is going away. The recycled textures keep their references on + // purpose: releasing one makes the engine delete its GL texture in whatever + // GL context is current, which can belong to another window. + g_clear_pointer(&self->recycled, g_ptr_array_unref); G_OBJECT_CLASS(texture_rgba_renderer_plugin_parent_class)->dispose(object); } @@ -112,7 +139,9 @@ static void texture_rgba_renderer_plugin_class_init(TextureRgbaRendererPluginCla G_OBJECT_CLASS(klass)->dispose = texture_rgba_renderer_plugin_dispose; } -static void texture_rgba_renderer_plugin_init(TextureRgbaRendererPlugin *self) {} +static void texture_rgba_renderer_plugin_init(TextureRgbaRendererPlugin *self) { + self->recycled = g_ptr_array_new(); +} static void method_call_cb(FlMethodChannel *channel, FlMethodCall *method_call, gpointer user_data) diff --git a/test/texture_rgba_renderer_method_channel_test.dart b/test/texture_rgba_renderer_method_channel_test.dart index 9702f9a..a281bc4 100644 --- a/test/texture_rgba_renderer_method_channel_test.dart +++ b/test/texture_rgba_renderer_method_channel_test.dart @@ -1,3 +1,4 @@ +// Modified by the Kitedesk fork (2026, B212): closeTexture takes `recycle`. import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:texture_rgba_renderer/texture_rgba_renderer_method_channel.dart'; @@ -22,4 +23,20 @@ void main() { test('getPlatformVersion', () async { expect(await platform.getPlatformVersion(), '42'); }); + + // Kitedesk fork (B212): closeTexture tells the Linux side whether the + // texture may be recycled. + test('closeTexture sends recycle', () async { + final calls = []; + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger + .setMockMethodCallHandler(channel, (MethodCall methodCall) async { + calls.add(methodCall); + return true; + }); + expect(await platform.closeTexture(7), true); + expect(await platform.closeTexture(8, recycle: true), true); + expect(calls.map((c) => c.method), ['closeTexture', 'closeTexture']); + expect(calls[0].arguments, {'key': 7, 'recycle': false}); + expect(calls[1].arguments, {'key': 8, 'recycle': true}); + }); } diff --git a/test/texture_rgba_renderer_test.dart b/test/texture_rgba_renderer_test.dart index 554c7ea..1493096 100644 --- a/test/texture_rgba_renderer_test.dart +++ b/test/texture_rgba_renderer_test.dart @@ -1,6 +1,8 @@ +// Modified by the Kitedesk fork (2026, B212): closeTexture takes `recycle`. import 'dart:typed_data'; import 'package:flutter_test/flutter_test.dart'; +import 'package:texture_rgba_renderer/texture_rgba_renderer.dart'; import 'package:texture_rgba_renderer/texture_rgba_renderer_platform_interface.dart'; import 'package:texture_rgba_renderer/texture_rgba_renderer_method_channel.dart'; import 'package:plugin_platform_interface/plugin_platform_interface.dart'; @@ -11,8 +13,13 @@ class MockTextureRgbaRendererPlatform @override Future getPlatformVersion() => Future.value('42'); + final closed = >[]; + @override - Future closeTexture(int key) => Future.value(true); + Future closeTexture(int key, {bool recycle = false}) { + closed.add([key, recycle]); + return Future.value(true); + } @override Future createTexture(int key) => Future.value(1); @@ -35,4 +42,18 @@ void main() { test('$MethodChannelTextureRgbaRenderer is the default instance', () { expect(initialPlatform, isInstanceOf()); }); + + // Kitedesk fork (B212). + test('closeTexture forwards recycle', () async { + final fake = MockTextureRgbaRendererPlatform(); + TextureRgbaRendererPlatform.instance = fake; + final renderer = TextureRgbaRenderer(); + expect(await renderer.closeTexture(9), true); + expect(await renderer.closeTexture(10, recycle: true), true); + expect(fake.closed, [ + [9, false], + [10, true], + ]); + TextureRgbaRendererPlatform.instance = initialPlatform; + }); }