diff --git a/lib/texture_rgba_renderer.dart b/lib/texture_rgba_renderer.dart index 04ee24a..2502177 100644 --- a/lib/texture_rgba_renderer.dart +++ b/lib/texture_rgba_renderer.dart @@ -1,4 +1,3 @@ -// Modified by the Kitedesk fork (2026, B212): closeTexture takes `recycle`. import 'dart:typed_data'; export './texture_rgba_ffi.dart' show Native; @@ -18,15 +17,9 @@ 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, {bool recycle = false}) { - return TextureRgbaRendererPlatform.instance - .closeTexture(key, recycle: recycle); + Future closeTexture(int key) { + return TextureRgbaRendererPlatform.instance.closeTexture(key); } /// 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 ac61506..6137a94 100644 --- a/lib/texture_rgba_renderer_method_channel.dart +++ b/lib/texture_rgba_renderer_method_channel.dart @@ -1,4 +1,3 @@ -// 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 @@ -31,9 +30,9 @@ class MethodChannelTextureRgbaRenderer extends TextureRgbaRendererPlatform { } @override - Future closeTexture(int key, {bool recycle = false}) async { - return await methodChannel.invokeMethod( - 'closeTexture', {"key": key, "recycle": recycle}) ?? + Future closeTexture(int key) async { + return await methodChannel + .invokeMethod('closeTexture', {"key": key}) ?? false; } diff --git a/lib/texture_rgba_renderer_platform_interface.dart b/lib/texture_rgba_renderer_platform_interface.dart index f28a650..fd1d491 100644 --- a/lib/texture_rgba_renderer_platform_interface.dart +++ b/lib/texture_rgba_renderer_platform_interface.dart @@ -1,4 +1,3 @@ -// Modified by the Kitedesk fork (2026, B212): closeTexture takes `recycle`. import 'dart:typed_data'; import 'package:flutter/foundation.dart'; @@ -36,7 +35,7 @@ abstract class TextureRgbaRendererPlatform extends PlatformInterface { throw UnimplementedError('createTexture() has not been implemented.'); } - Future closeTexture(int key, {bool recycle = false}) { + Future closeTexture(int key) { 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 1a575c6..3bdaae0 100644 --- a/linux/include/texture_rgba_renderer/texture_rgba_renderer.h +++ b/linux/include/texture_rgba_renderer/texture_rgba_renderer.h @@ -1,8 +1,3 @@ -// Modified by the Kitedesk fork (2026, B212): frame buffers are freed when the -// 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 @@ -45,34 +40,9 @@ G_DEFINE_TYPE(TextureRgba, texture_rgba, TextureRgba)) -// Frees the frame waiting for the next raster frame and the frame last handed -// to the engine. Call with the mutex held, or when no other thread can use the -// texture any more. -static void texture_rgba_free_buffers(TextureRgba* self) { - if (g_atomic_int_get(&self->buffer_ready)) { - // Not handed to the engine yet; prior_buffer is the older frame. - delete[] self->buffer; - } - delete[] self->prior_buffer; - self->buffer = nullptr; - self->prior_buffer = nullptr; - g_atomic_int_set(&self->buffer_ready, FALSE); -} - static void texture_rgba_terminate(TextureRgba* self) { g_mutex_lock(&self->mutex); g_atomic_int_set(&self->terminated, TRUE); - // The texture is unregistered, so the engine reads neither frame again, and - // OnRgba stops copying frames once terminated is set. - texture_rgba_free_buffers(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); } @@ -119,14 +89,6 @@ 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; @@ -175,16 +137,8 @@ static TextureRgba *texture_rgba_new(FlTextureRegistrar* registrar) return texture; } -static void texture_rgba_finalize(GObject* object) { - TextureRgba* self = TEXTURE_RGBA(object); - texture_rgba_free_buffers(self); - g_mutex_clear(&self->mutex); - G_OBJECT_CLASS(texture_rgba_parent_class)->finalize(object); -} - static void texture_rgba_class_init(TextureRgbaClass *klass) { - G_OBJECT_CLASS(klass)->finalize = texture_rgba_finalize; FL_PIXEL_BUFFER_TEXTURE_CLASS(klass)->copy_pixels = texture_rgba_copy_pixels; } diff --git a/linux/test/run_tests.sh b/linux/test/run_tests.sh deleted file mode 100755 index fc4a1f2..0000000 --- a/linux/test/run_tests.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/env bash -# Kitedesk fork, 2026: builds and runs linux/test/texture_rgba_renderer_plugin_test.cc -# against the Linux engine of the Flutter SDK on PATH (or FLUTTER_ROOT). -# The test does not open a display or a GL context. -# run_tests.sh -Wall -Werror build, like the plugin in an app build -# run_tests.sh --asan the same with AddressSanitizer and UBSan -# OUT_DIR (default: a new temporary directory) receives the test binary. -set -euo pipefail -here=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) -root=${FLUTTER_ROOT:-$(dirname "$(dirname "$(readlink -f "$(command -v flutter)")")")} -engine=$root/bin/cache/artifacts/engine/linux-x64 -[ -f "$engine/libflutter_linux_gtk.so" ] || { echo "no Linux engine in $engine (run flutter precache --linux)" >&2; exit 2; } -out=${OUT_DIR:-$(mktemp -d)} -mkdir -p "$out" -flags=(-std=gnu++14 -Wall -Werror -O1 -g -DFLUTTER_PLUGIN_IMPL) -if [ "${1:-}" = --asan ]; then - flags+=(-fsanitize=address,undefined -fno-omit-frame-pointer -fno-sanitize-recover=undefined) - export ASAN_OPTIONS=${ASAN_OPTIONS:-detect_leaks=0} -fi -# shellcheck disable=SC2046 -"${CXX:-clang++}" "${flags[@]}" -I"$engine" -I"$here/../include" $(pkg-config --cflags gtk+-3.0) \ - "$here/texture_rgba_renderer_plugin_test.cc" -o "$out/texture_rgba_renderer_plugin_test" \ - -L"$engine" -Wl,-rpath,"$engine" -lflutter_linux_gtk $(pkg-config --libs gtk+-3.0) -pthread -"$out/texture_rgba_renderer_plugin_test" diff --git a/linux/test/texture_rgba_renderer_plugin_test.cc b/linux/test/texture_rgba_renderer_plugin_test.cc deleted file mode 100644 index e15baa5..0000000 --- a/linux/test/texture_rgba_renderer_plugin_test.cc +++ /dev/null @@ -1,836 +0,0 @@ -// Kitedesk fork, 2026: lifecycle tests for the Linux texture of this plugin -// (Kitedesk backlog B212). Licensed under the Apache License, Version 2.0, -// like the rest of this package. -// -// The plugin source is compiled into this program and driven through a real -// FlMethodChannel on fake engine objects: a binary messenger, a texture -// registrar with the reference rules of the Flutter 3.24 engine, and a plugin -// registrar. Frame buffers that the plugin copies with new[] are counted. -// The engine's GL work (fl_pixel_buffer_texture_populate) is replaced by calls -// to the copy_pixels hook it would make; no GL context is needed. -// -// Build and run: linux/test/run_tests.sh [--asan] - -#include "../texture_rgba_renderer_plugin.cc" - -#include -#include -#include -#include -#include -#include - -namespace { - -constexpr int kWidth = 321; -constexpr int kHeight = 123; -constexpr size_t kFrameBytes = static_cast(kWidth) * kHeight * 4; - -// Live frame buffers: new[] blocks of exactly kFrameBytes (no other code in -// this program allocates that size). -constexpr int kSlots = 4096; -std::atomic_flag g_frames_lock = ATOMIC_FLAG_INIT; -void* g_frames[kSlots]; -int g_live_frames = 0; - -void lock_frames() { - while (g_frames_lock.test_and_set(std::memory_order_acquire)) { - } -} - -void unlock_frames() { g_frames_lock.clear(std::memory_order_release); } - -int live_frames() { - lock_frames(); - int n = g_live_frames; - unlock_frames(); - return n; -} - -} // namespace - -void* operator new[](size_t size) { - void* p = std::malloc(size ? size : 1); - if (p == nullptr) { - throw std::bad_alloc(); - } - if (size == kFrameBytes) { - lock_frames(); - for (int i = 0; i < kSlots; i++) { - if (g_frames[i] == nullptr) { - g_frames[i] = p; - g_live_frames++; - break; - } - } - unlock_frames(); - } - return p; -} - -void operator delete[](void* p) noexcept { - if (p != nullptr) { - lock_frames(); - for (int i = 0; i < kSlots; i++) { - if (g_frames[i] == p) { - g_frames[i] = nullptr; - g_live_frames--; - break; - } - } - unlock_frames(); - } - std::free(p); -} - -void operator delete[](void* p, size_t) noexcept { operator delete[](p); } - -// --------------------------------------------------------------------------- -// Fake texture registrar: register takes a reference and keys the texture by -// its address, unregister drops it (fl_texture_registrar.cc @ engine a18df97c). - -G_DECLARE_FINAL_TYPE(FakeTextureRegistrar, - fake_texture_registrar, - FAKE, - TEXTURE_REGISTRAR, - GObject) - -struct _FakeTextureRegistrar { - GObject parent_instance; - GHashTable* textures; // FlTexture* -> FlTexture* (reference) - GMutex mutex; - gint marks; -}; - -static void fake_texture_registrar_iface_init( - FlTextureRegistrarInterface* iface); - -G_DEFINE_TYPE_WITH_CODE( - FakeTextureRegistrar, - fake_texture_registrar, - G_TYPE_OBJECT, - G_IMPLEMENT_INTERFACE(fl_texture_registrar_get_type(), - fake_texture_registrar_iface_init)) - -static gboolean ftr_register(FlTextureRegistrar* registrar, - FlTexture* texture) { - FakeTextureRegistrar* self = FAKE_TEXTURE_REGISTRAR(registrar); - FL_TEXTURE_GET_IFACE(texture)->set_id(texture, - reinterpret_cast(texture)); - g_mutex_lock(&self->mutex); - g_hash_table_insert(self->textures, texture, g_object_ref(texture)); - g_mutex_unlock(&self->mutex); - return TRUE; -} - -static FlTexture* ftr_lookup(FlTextureRegistrar* registrar, int64_t id) { - FakeTextureRegistrar* self = FAKE_TEXTURE_REGISTRAR(registrar); - g_mutex_lock(&self->mutex); - FlTexture* texture = static_cast( - g_hash_table_lookup(self->textures, reinterpret_cast(id))); - g_mutex_unlock(&self->mutex); - return texture; -} - -static gboolean ftr_mark(FlTextureRegistrar* registrar, FlTexture* texture) { - g_atomic_int_inc(&FAKE_TEXTURE_REGISTRAR(registrar)->marks); - return TRUE; -} - -static gboolean ftr_unregister(FlTextureRegistrar* registrar, - FlTexture* texture) { - FakeTextureRegistrar* self = FAKE_TEXTURE_REGISTRAR(registrar); - g_mutex_lock(&self->mutex); - gboolean removed = g_hash_table_remove(self->textures, texture); - g_mutex_unlock(&self->mutex); - return removed; -} - -static void ftr_shutdown(FlTextureRegistrar* registrar) { - FakeTextureRegistrar* self = FAKE_TEXTURE_REGISTRAR(registrar); - g_mutex_lock(&self->mutex); - g_hash_table_remove_all(self->textures); - g_mutex_unlock(&self->mutex); -} - -static void fake_texture_registrar_iface_init( - FlTextureRegistrarInterface* iface) { - iface->register_texture = ftr_register; - iface->lookup_texture = ftr_lookup; - iface->mark_texture_frame_available = ftr_mark; - iface->unregister_texture = ftr_unregister; - iface->shutdown = ftr_shutdown; -} - -static void fake_texture_registrar_dispose(GObject* object) { - FakeTextureRegistrar* self = FAKE_TEXTURE_REGISTRAR(object); - g_clear_pointer(&self->textures, g_hash_table_unref); - G_OBJECT_CLASS(fake_texture_registrar_parent_class)->dispose(object); -} - -static void fake_texture_registrar_class_init( - FakeTextureRegistrarClass* klass) { - G_OBJECT_CLASS(klass)->dispose = fake_texture_registrar_dispose; -} - -static void fake_texture_registrar_init(FakeTextureRegistrar* self) { - self->textures = g_hash_table_new_full(g_direct_hash, g_direct_equal, - nullptr, g_object_unref); - g_mutex_init(&self->mutex); -} - -static gboolean registered(FakeTextureRegistrar* self, int64_t ptr) { - return ftr_lookup(FL_TEXTURE_REGISTRAR(self), ptr) != nullptr; -} - -// --------------------------------------------------------------------------- -// Fake binary messenger. send_on_channel delivers to the handler registered on -// the peer messenger, like Dart -> engine -> plugin. - -G_DECLARE_FINAL_TYPE(FakeResponseHandle, - fake_response_handle, - FAKE, - RESPONSE_HANDLE, - FlBinaryMessengerResponseHandle) - -struct _FakeResponseHandle { - FlBinaryMessengerResponseHandle parent_instance; - GTask* task; -}; - -G_DEFINE_TYPE(FakeResponseHandle, - fake_response_handle, - fl_binary_messenger_response_handle_get_type()) - -static void fake_response_handle_dispose(GObject* object) { - FakeResponseHandle* self = FAKE_RESPONSE_HANDLE(object); - if (self->task != nullptr) { - g_task_return_new_error(self->task, G_IO_ERROR, G_IO_ERROR_FAILED, - "no response"); - g_clear_object(&self->task); - } - G_OBJECT_CLASS(fake_response_handle_parent_class)->dispose(object); -} - -static void fake_response_handle_class_init(FakeResponseHandleClass* klass) { - G_OBJECT_CLASS(klass)->dispose = fake_response_handle_dispose; -} - -static void fake_response_handle_init(FakeResponseHandle* self) {} - -G_DECLARE_FINAL_TYPE(FakeMessenger, fake_messenger, FAKE, MESSENGER, GObject) - -struct Handler { - FlBinaryMessengerMessageHandler handler; - gpointer user_data; - GDestroyNotify destroy_notify; -}; - -static void handler_free(gpointer data) { - Handler* h = static_cast(data); - if (h->destroy_notify != nullptr) { - h->destroy_notify(h->user_data); - } - g_free(h); -} - -struct _FakeMessenger { - GObject parent_instance; - GHashTable* handlers; // channel -> Handler* - FakeMessenger* peer; // not owned -}; - -static void fake_messenger_iface_init(FlBinaryMessengerInterface* iface); - -G_DEFINE_TYPE_WITH_CODE(FakeMessenger, - fake_messenger, - G_TYPE_OBJECT, - G_IMPLEMENT_INTERFACE(fl_binary_messenger_get_type(), - fake_messenger_iface_init)) - -static void fm_set_handler(FlBinaryMessenger* messenger, - const gchar* channel, - FlBinaryMessengerMessageHandler handler, - gpointer user_data, - GDestroyNotify destroy_notify) { - FakeMessenger* self = FAKE_MESSENGER(messenger); - if (handler == nullptr) { - g_hash_table_remove(self->handlers, channel); - return; - } - Handler* h = g_new0(Handler, 1); - h->handler = handler; - h->user_data = user_data; - h->destroy_notify = destroy_notify; - g_hash_table_insert(self->handlers, g_strdup(channel), h); -} - -static gboolean fm_send_response(FlBinaryMessenger* messenger, - FlBinaryMessengerResponseHandle* handle, - GBytes* response, - GError** error) { - FakeResponseHandle* h = FAKE_RESPONSE_HANDLE(handle); - if (h->task == nullptr) { - g_set_error(error, G_IO_ERROR, G_IO_ERROR_FAILED, "already responded"); - return FALSE; - } - g_task_return_pointer(h->task, g_bytes_ref(response), - reinterpret_cast(g_bytes_unref)); - g_clear_object(&h->task); - return TRUE; -} - -static void fm_send_on_channel(FlBinaryMessenger* messenger, - const gchar* channel, - GBytes* message, - GCancellable* cancellable, - GAsyncReadyCallback callback, - gpointer user_data) { - FakeMessenger* self = FAKE_MESSENGER(messenger); - // As in FlBinaryMessengerImpl, the reference from g_task_new belongs to - // send_on_channel_finish; the response handle holds its own. - GTask* task = g_task_new(self, cancellable, callback, user_data); - Handler* h = self->peer == nullptr - ? nullptr - : static_cast( - g_hash_table_lookup(self->peer->handlers, channel)); - if (h == nullptr) { - g_task_return_new_error(task, G_IO_ERROR, G_IO_ERROR_NOT_FOUND, - "no handler on %s", channel); - return; - } - FakeResponseHandle* handle = FAKE_RESPONSE_HANDLE( - g_object_new(fake_response_handle_get_type(), nullptr)); - handle->task = G_TASK(g_object_ref(task)); - h->handler(FL_BINARY_MESSENGER(self->peer), channel, message, - FL_BINARY_MESSENGER_RESPONSE_HANDLE(handle), h->user_data); - g_object_unref(handle); -} - -static GBytes* fm_send_on_channel_finish(FlBinaryMessenger* messenger, - GAsyncResult* result, - GError** error) { - g_autoptr(GTask) task = G_TASK(result); - return static_cast(g_task_propagate_pointer(task, error)); -} - -static void fm_resize_channel(FlBinaryMessenger* messenger, - const gchar* channel, - int64_t new_size) {} - -static void fm_set_warns(FlBinaryMessenger* messenger, - const gchar* channel, - bool warns) {} - -static void fm_shutdown(FlBinaryMessenger* messenger) { - g_hash_table_remove_all(FAKE_MESSENGER(messenger)->handlers); -} - -static void fake_messenger_iface_init(FlBinaryMessengerInterface* iface) { - iface->set_message_handler_on_channel = fm_set_handler; - iface->send_response = fm_send_response; - iface->send_on_channel = fm_send_on_channel; - iface->send_on_channel_finish = fm_send_on_channel_finish; - iface->resize_channel = fm_resize_channel; - iface->set_warns_on_channel_overflow = fm_set_warns; - iface->shutdown = fm_shutdown; -} - -static void fake_messenger_dispose(GObject* object) { - FakeMessenger* self = FAKE_MESSENGER(object); - g_clear_pointer(&self->handlers, g_hash_table_unref); - G_OBJECT_CLASS(fake_messenger_parent_class)->dispose(object); -} - -static void fake_messenger_class_init(FakeMessengerClass* klass) { - G_OBJECT_CLASS(klass)->dispose = fake_messenger_dispose; -} - -static void fake_messenger_init(FakeMessenger* self) { - self->handlers = - g_hash_table_new_full(g_str_hash, g_str_equal, g_free, handler_free); -} - -// --------------------------------------------------------------------------- -// Fake plugin registrar. - -G_DECLARE_FINAL_TYPE(FakePluginRegistrar, - fake_plugin_registrar, - FAKE, - PLUGIN_REGISTRAR, - GObject) - -struct _FakePluginRegistrar { - GObject parent_instance; - FlBinaryMessenger* messenger; - FlTextureRegistrar* textures; -}; - -static void fake_plugin_registrar_iface_init( - FlPluginRegistrarInterface* iface); - -G_DEFINE_TYPE_WITH_CODE( - FakePluginRegistrar, - fake_plugin_registrar, - G_TYPE_OBJECT, - G_IMPLEMENT_INTERFACE(fl_plugin_registrar_get_type(), - fake_plugin_registrar_iface_init)) - -static FlBinaryMessenger* fpr_get_messenger(FlPluginRegistrar* registrar) { - return FAKE_PLUGIN_REGISTRAR(registrar)->messenger; -} - -static FlTextureRegistrar* fpr_get_textures(FlPluginRegistrar* registrar) { - return FAKE_PLUGIN_REGISTRAR(registrar)->textures; -} - -static FlView* fpr_get_view(FlPluginRegistrar* registrar) { - return nullptr; -} - -static void fake_plugin_registrar_iface_init( - FlPluginRegistrarInterface* iface) { - iface->get_messenger = fpr_get_messenger; - iface->get_texture_registrar = fpr_get_textures; - iface->get_view = fpr_get_view; -} - -static void fake_plugin_registrar_dispose(GObject* object) { - FakePluginRegistrar* self = FAKE_PLUGIN_REGISTRAR(object); - g_clear_object(&self->messenger); - g_clear_object(&self->textures); - G_OBJECT_CLASS(fake_plugin_registrar_parent_class)->dispose(object); -} - -static void fake_plugin_registrar_class_init(FakePluginRegistrarClass* klass) { - G_OBJECT_CLASS(klass)->dispose = fake_plugin_registrar_dispose; -} - -static void fake_plugin_registrar_init(FakePluginRegistrar* self) {} - -// --------------------------------------------------------------------------- -// One "engine" (= one Flutter window): the plugin registered on it, plus a Dart -// side channel to call it. - -struct Engine { - FakeMessenger* messenger; - FakeTextureRegistrar* textures; - FakePluginRegistrar* registrar; - FakeMessenger* dart; - FlMethodChannel* channel; -}; - -static Engine* engine_new() { - Engine* e = g_new0(Engine, 1); - e->messenger = - FAKE_MESSENGER(g_object_new(fake_messenger_get_type(), nullptr)); - e->textures = FAKE_TEXTURE_REGISTRAR( - g_object_new(fake_texture_registrar_get_type(), nullptr)); - e->registrar = FAKE_PLUGIN_REGISTRAR( - g_object_new(fake_plugin_registrar_get_type(), nullptr)); - e->registrar->messenger = FL_BINARY_MESSENGER(g_object_ref(e->messenger)); - e->registrar->textures = FL_TEXTURE_REGISTRAR(g_object_ref(e->textures)); - texture_rgba_renderer_plugin_register_with_registrar( - FL_PLUGIN_REGISTRAR(e->registrar)); - e->dart = FAKE_MESSENGER(g_object_new(fake_messenger_get_type(), nullptr)); - e->dart->peer = e->messenger; - g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new(); - e->channel = fl_method_channel_new(FL_BINARY_MESSENGER(e->dart), - "texture_rgba_renderer", - FL_METHOD_CODEC(codec)); - return e; -} - -// What the engine does when its window goes away: the messenger drops its -// handlers (the plugin instance is released with them) and the texture -// registrar drops the textures that are still registered. -static void engine_shutdown(Engine* e) { - fm_shutdown(FL_BINARY_MESSENGER(e->messenger)); - fm_shutdown(FL_BINARY_MESSENGER(e->dart)); - ftr_shutdown(FL_TEXTURE_REGISTRAR(e->textures)); - g_clear_object(&e->channel); - g_clear_object(&e->dart); - g_clear_object(&e->registrar); - g_clear_object(&e->textures); - g_clear_object(&e->messenger); - g_free(e); -} - -struct CallResult { - gboolean done; - FlMethodResponse* response; -}; - -static void call_cb(GObject* object, GAsyncResult* result, gpointer user_data) { - CallResult* r = static_cast(user_data); - r->response = fl_method_channel_invoke_method_finish(FL_METHOD_CHANNEL(object), - result, nullptr); - r->done = TRUE; -} - -// Calls a plugin method like Dart does. Returns the int or bool result, or -// G_MININT64 when the call failed. -static int64_t invoke(Engine* e, const char* method, FlValue* args) { - CallResult r = {FALSE, nullptr}; - fl_method_channel_invoke_method(e->channel, method, args, nullptr, call_cb, - &r); - fl_value_unref(args); - for (int i = 0; i < 10000 && !r.done; i++) { - g_main_context_iteration(nullptr, FALSE); - } - int64_t out = G_MININT64; - if (r.response != nullptr) { - FlValue* value = fl_method_response_get_result(r.response, nullptr); - if (value != nullptr && fl_value_get_type(value) == FL_VALUE_TYPE_INT) { - out = fl_value_get_int(value); - } else if (value != nullptr && - fl_value_get_type(value) == FL_VALUE_TYPE_BOOL) { - out = fl_value_get_bool(value) ? 1 : 0; - } - g_object_unref(r.response); - } - return out; -} - -static FlValue* key_args(int64_t key) { - FlValue* args = fl_value_new_map(); - fl_value_set_string_take(args, "key", fl_value_new_int(key)); - return args; -} - -static int64_t create_texture(Engine* e, int64_t key) { - return invoke(e, "createTexture", key_args(key)); -} - -static int64_t texture_ptr(Engine* e, int64_t key) { - return invoke(e, "getTexturePtr", key_args(key)); -} - -// recycle < 0: no "recycle" argument (callers before the Kitedesk change). -static int64_t close_texture(Engine* e, int64_t key, int recycle) { - FlValue* args = key_args(key); - if (recycle >= 0) { - fl_value_set_string_take(args, "recycle", fl_value_new_bool(recycle != 0)); - } - return invoke(e, "closeTexture", args); -} - -// A decoded frame from the Rust video thread (FlutterRgbaRendererPluginOnRgba). -static void send_frame(int64_t ptr, uint8_t fill) { - static uint8_t frames[256][kFrameBytes]; - static gboolean filled[256]; - if (!filled[fill]) { - memset(frames[fill], fill, kFrameBytes); - filled[fill] = TRUE; - } - FlutterRgbaRendererPluginOnRgba(reinterpret_cast(ptr), frames[fill], - static_cast(kFrameBytes), kWidth, - kHeight, 0); -} - -struct Pulled { - gboolean ok; - const uint8_t* buffer; - uint32_t width; - uint32_t height; -}; - -// What fl_pixel_buffer_texture_populate asks the texture for on a raster -// frame. width/height start as the canvas size, like in the engine. -static Pulled pull_frame(int64_t ptr) { - Pulled p = {FALSE, nullptr, 999, 777}; - FlPixelBufferTexture* texture = - FL_PIXEL_BUFFER_TEXTURE(reinterpret_cast(ptr)); - g_autoptr(GError) error = nullptr; - p.ok = FL_PIXEL_BUFFER_TEXTURE_GET_CLASS(texture)->copy_pixels( - texture, &p.buffer, &p.width, &p.height, &error); - return p; -} - -static void set_flag(gpointer data, GObject* where_the_object_was) { - *static_cast(data) = TRUE; -} - -// --------------------------------------------------------------------------- - -int g_failed_checks = 0; - -#define CHECK(cond) \ - do { \ - if (!(cond)) { \ - std::fprintf(stderr, " check failed at line %d: %s\n", __LINE__, \ - #cond); \ - g_failed_checks++; \ - return; \ - } \ - } while (0) - -// D1: closing a texture frees the frame it shows and the frame still waiting -// for the next raster frame. -static void test_close_frees_frame_buffers() { - const int base = live_frames(); - Engine* e = engine_new(); - int64_t id = create_texture(e, 101); - int64_t ptr = texture_ptr(e, 101); - CHECK(id > 0 && ptr == id); - send_frame(ptr, 1); - Pulled shown = pull_frame(ptr); - CHECK(shown.ok && shown.buffer != nullptr && shown.width == kWidth && - shown.height == kHeight && shown.buffer[0] == 1); - send_frame(ptr, 2); - CHECK(live_frames() == base + 2); - CHECK(close_texture(e, 101, 0) == 1); - CHECK(live_frames() == base); - engine_shutdown(e); -} - -// D1: after close, late frames are dropped and the texture never hands out a -// frame again (it would be a freed buffer). -static void test_closed_texture_ignores_late_frames() { - const int base = live_frames(); - Engine* e = engine_new(); - int64_t ptr = create_texture(e, 111); - send_frame(ptr, 3); - CHECK(close_texture(e, 111, 0) == 1); - send_frame(ptr, 4); - CHECK(live_frames() == base); - Pulled late = pull_frame(ptr); - CHECK(!late.ok); - engine_shutdown(e); -} - -// D1: the GObject frees its frame buffers when it is finalized. -static void test_finalize_frees_frame_buffers() { - const int base = live_frames(); - FakeTextureRegistrar* registrar = FAKE_TEXTURE_REGISTRAR( - g_object_new(fake_texture_registrar_get_type(), nullptr)); - TextureRgba* texture = texture_rgba_new(FL_TEXTURE_REGISTRAR(registrar)); - texture->texture_id = reinterpret_cast(texture); - int64_t ptr = reinterpret_cast(texture); - send_frame(ptr, 5); - CHECK(pull_frame(ptr).ok); - send_frame(ptr, 6); - CHECK(live_frames() == base + 2); - gboolean finalized = FALSE; - g_object_weak_ref(G_OBJECT(texture), set_flag, &finalized); - g_object_unref(texture); - CHECK(finalized); - CHECK(live_frames() == base); - g_object_unref(registrar); -} - -// D2: a texture closed with recycle=true is the one the next createTexture of -// the same window gets back, so the engine's GL texture is reused instead of -// left behind; the object is never released (its GL name belongs to that -// window's context). -static void test_recycled_texture_is_reused_in_same_window() { - const int base = live_frames(); - Engine* e = engine_new(); - std::set seen; - gboolean finalized = FALSE; - for (int i = 0; i < 5; i++) { - int64_t key = 200 + i; - int64_t ptr = create_texture(e, key); - CHECK(ptr > 0 && texture_ptr(e, key) == ptr); - CHECK(registered(e->textures, ptr)); - if (seen.empty()) { - g_object_weak_ref(G_OBJECT(reinterpret_cast(ptr)), set_flag, - &finalized); - } - seen.insert(ptr); - send_frame(ptr, 7); - CHECK(pull_frame(ptr).ok); - send_frame(ptr, 8); - CHECK(close_texture(e, key, 1) == 1); - CHECK(!registered(e->textures, ptr)); - CHECK(live_frames() == base); - } - CHECK(seen.size() == 1); - CHECK(!finalized); - engine_shutdown(e); -} - -// D2, multi-window: windows never get each other's textures back. -static void test_recycling_stays_in_one_window() { - Engine* w1 = engine_new(); - Engine* w2 = engine_new(); - int64_t a = create_texture(w1, 300); - CHECK(close_texture(w1, 300, 1) == 1); - int64_t b = create_texture(w2, 301); - CHECK(b > 0 && b != a); - CHECK(!registered(w2->textures, a) && !registered(w1->textures, a)); - CHECK(close_texture(w2, 301, 1) == 1); - int64_t c = create_texture(w1, 302); - CHECK(c == a && registered(w1->textures, a)); - int64_t d = create_texture(w1, 303); - CHECK(d != b && d != a && registered(w1->textures, d)); - CHECK(!registered(w1->textures, b)); - int64_t f = create_texture(w2, 304); - CHECK(f == b && registered(w2->textures, b)); - engine_shutdown(w1); - engine_shutdown(w2); -} - -// D2: without recycle (old callers, or a tab moving to another window while -// its session keeps the texture pointer), the texture is never handed out -// again and stays a valid, inert object. -static void test_close_without_recycle_never_reuses() { - const int base = live_frames(); - Engine* e = engine_new(); - int64_t a = create_texture(e, 400); - gboolean finalized = FALSE; - g_object_weak_ref(G_OBJECT(reinterpret_cast(a)), set_flag, - &finalized); - CHECK(close_texture(e, 400, -1) == 1); - int64_t b = create_texture(e, 401); - CHECK(b > 0 && b != a); - send_frame(a, 9); - CHECK(live_frames() == base); - CHECK(!pull_frame(a).ok); - CHECK(close_texture(e, 401, 0) == 1); - int64_t c = create_texture(e, 402); - CHECK(c != a && c != b); - CHECK(!finalized); - engine_shutdown(e); -} - -// D2: a reused texture must not show the last frame of its previous session -// before the new session's first frame. While no frame is waiting the texture -// uploads one transparent pixel instead of leaving the old content. -static void test_reused_texture_shows_no_stale_frame() { - Engine* e = engine_new(); - int64_t a = create_texture(e, 500); - send_frame(a, 0xAB); - CHECK(pull_frame(a).ok); - CHECK(close_texture(e, 500, 1) == 1); - int64_t b = create_texture(e, 501); - CHECK(b == a); - Pulled first = pull_frame(b); - CHECK(first.ok && first.buffer != nullptr && first.width == 1 && - first.height == 1); - CHECK(first.buffer[0] == 0 && first.buffer[1] == 0 && first.buffer[2] == 0 && - first.buffer[3] == 0); - send_frame(b, 0xCD); - Pulled next = pull_frame(b); - CHECK(next.ok && next.width == kWidth && next.height == kHeight && - next.buffer[0] == 0xCD); - CHECK(close_texture(e, 501, 1) == 1); - engine_shutdown(e); -} - -// D2: when a window's engine goes away the recycled textures are kept alive: -// releasing them would make the engine delete their GL texture in whatever GL -// context is current (another window's), which is the upstream PR #2 defect. -static void test_engine_shutdown_keeps_recycled_textures() { - Engine* e = engine_new(); - int64_t a = create_texture(e, 600); - int64_t b = create_texture(e, 601); - gboolean a_finalized = FALSE; - gboolean b_finalized = FALSE; - g_object_weak_ref(G_OBJECT(reinterpret_cast(a)), set_flag, - &a_finalized); - g_object_weak_ref(G_OBJECT(reinterpret_cast(b)), set_flag, - &b_finalized); - CHECK(close_texture(e, 600, 1) == 1); - engine_shutdown(e); - CHECK(!a_finalized && !b_finalized); -} - -// D1: frames keep arriving from the video thread while the window closes the -// texture (no recycle: the native side may still hold the pointer). -static void test_frames_racing_close() { - const int base = live_frames(); - Engine* e = engine_new(); - int64_t ptr = create_texture(e, 700); - std::atomic stop{false}; - std::thread video([ptr, &stop]() { - for (int i = 0; i < 200000 && !stop.load(); i++) { - send_frame(ptr, static_cast(10 + i % 2)); - } - }); - for (int i = 0; i < 200; i++) { - pull_frame(ptr); - g_usleep(50); - } - int64_t closed = close_texture(e, 700, 0); - for (int i = 0; i < 200; i++) { - pull_frame(ptr); - g_usleep(50); - } - stop = true; - video.join(); - CHECK(closed == 1); - CHECK(live_frames() == base); - engine_shutdown(e); -} - -// D2, multi-monitor tab: textures of several displays in one window are -// recycled among themselves without touching the ones still on screen. -static void test_multi_display_tab() { - const int base = live_frames(); - Engine* e = engine_new(); - int64_t d0 = create_texture(e, 800); - int64_t d1 = create_texture(e, 801); - CHECK(d0 != d1); - send_frame(d0, 20); - CHECK(pull_frame(d0).ok); - send_frame(d1, 21); - CHECK(pull_frame(d1).ok); - // Show display 0 only. - CHECK(close_texture(e, 801, 1) == 1); - send_frame(d0, 22); - Pulled p0 = pull_frame(d0); - CHECK(p0.ok && p0.buffer[0] == 22 && registered(e->textures, d0)); - // All displays again. - int64_t d1b = create_texture(e, 802); - CHECK(d1b == d1 && registered(e->textures, d1)); - send_frame(d1b, 23); - Pulled p1 = pull_frame(d1b); - CHECK(p1.ok && p1.buffer[0] == 23); - // Close the tab, open another one with two displays. - CHECK(close_texture(e, 800, 1) == 1); - CHECK(close_texture(e, 802, 1) == 1); - CHECK(live_frames() == base); - int64_t n0 = create_texture(e, 803); - int64_t n1 = create_texture(e, 804); - CHECK(std::set({n0, n1}) == std::set({d0, d1})); - CHECK(close_texture(e, 803, 1) == 1); - CHECK(close_texture(e, 804, 1) == 1); - engine_shutdown(e); -} - -int main() { - struct { - const char* name; - void (*run)(); - } tests[] = { - {"close_frees_frame_buffers", test_close_frees_frame_buffers}, - {"closed_texture_ignores_late_frames", - test_closed_texture_ignores_late_frames}, - {"finalize_frees_frame_buffers", test_finalize_frees_frame_buffers}, - {"recycled_texture_is_reused_in_same_window", - test_recycled_texture_is_reused_in_same_window}, - {"recycling_stays_in_one_window", test_recycling_stays_in_one_window}, - {"close_without_recycle_never_reuses", - test_close_without_recycle_never_reuses}, - {"reused_texture_shows_no_stale_frame", - test_reused_texture_shows_no_stale_frame}, - {"engine_shutdown_keeps_recycled_textures", - test_engine_shutdown_keeps_recycled_textures}, - {"frames_racing_close", test_frames_racing_close}, - {"multi_display_tab", test_multi_display_tab}, - }; - int failed = 0; - int count = 0; - for (auto& t : tests) { - int before = g_failed_checks; - t.run(); - bool ok = g_failed_checks == before; - std::printf("%s %s\n", ok ? "PASS" : "FAIL", t.name); - std::fflush(stdout); - failed += ok ? 0 : 1; - count++; - } - std::printf("%d tests, %d passed, %d failed\n", count, count - failed, failed); - return failed == 0 ? 0 : 1; -} diff --git a/linux/texture_rgba_renderer_plugin.cc b/linux/texture_rgba_renderer_plugin.cc index 5037af1..1b62124 100644 --- a/linux/texture_rgba_renderer_plugin.cc +++ b/linux/texture_rgba_renderer_plugin.cc @@ -1,7 +1,3 @@ -// 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" @@ -23,10 +19,6 @@ 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()) @@ -50,13 +42,7 @@ static void texture_rgba_renderer_plugin_handle_method_call( } else { - 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); - } + TextureRgba* 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)); @@ -69,19 +55,11 @@ 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()) { - TextureRgba* texture_rgba = g_renderer_map[key]; - fl_texture_registrar_unregister_texture(self->texture_registrar, FL_TEXTURE(texture_rgba)); - texture_rgba_terminate(texture_rgba); + fl_texture_registrar_unregister_texture(self->texture_registrar, FL_TEXTURE(g_renderer_map[key])); + texture_rgba_terminate(g_renderer_map[key]); 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))); } @@ -126,11 +104,6 @@ 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); } @@ -139,9 +112,7 @@ 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) { - self->recycled = g_ptr_array_new(); -} +static void texture_rgba_renderer_plugin_init(TextureRgbaRendererPlugin *self) {} 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 a281bc4..9702f9a 100644 --- a/test/texture_rgba_renderer_method_channel_test.dart +++ b/test/texture_rgba_renderer_method_channel_test.dart @@ -1,4 +1,3 @@ -// 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'; @@ -23,20 +22,4 @@ 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 1493096..554c7ea 100644 --- a/test/texture_rgba_renderer_test.dart +++ b/test/texture_rgba_renderer_test.dart @@ -1,8 +1,6 @@ -// 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'; @@ -13,13 +11,8 @@ class MockTextureRgbaRendererPlatform @override Future getPlatformVersion() => Future.value('42'); - final closed = >[]; - @override - Future closeTexture(int key, {bool recycle = false}) { - closed.add([key, recycle]); - return Future.value(true); - } + Future closeTexture(int key) => Future.value(true); @override Future createTexture(int key) => Future.value(1); @@ -42,18 +35,4 @@ 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; - }); }