diff --git a/lib/texture_rgba_renderer.dart b/lib/texture_rgba_renderer.dart index cca1094..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/B252/T-4204): closeTexture ownership. import 'dart:typed_data'; export './texture_rgba_ffi.dart' show Native; @@ -18,21 +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 (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 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/macos/Classes/TextRgba.swift b/macos/Classes/TextRgba.swift index 704d7b8..1708c42 100644 --- a/macos/Classes/TextRgba.swift +++ b/macos/Classes/TextRgba.swift @@ -4,13 +4,6 @@ // // Created by kingtous on 2023/2/17. // -// Modified by the Kitedesk fork (2026, T-4204): frames go into the -// 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; -// close() drops the frames pushed after it and lets the buffers go. -// import Foundation import FlutterMacOS @@ -18,19 +11,11 @@ import CoreVideo @objc public class TextRgba: NSObject, FlutterTexture { public var textureId: Int64 = -1 - // 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, 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). - private static let handedFramesKept = 2 public static func new(registry: FlutterTextureRegistry?) -> TextRgba { let textRgba = TextRgba() @@ -44,107 +29,47 @@ import CoreVideo if (data == nil) { return nil } - if (handed.last !== data) { - handed.append(data!) - if (handed.count > TextRgba.handedFramesKept) { - handed.removeFirst() - } - } return Unmanaged.passRetained(data!) } } private func _markFrameAvaliable(buffer: UnsafePointer, len: Int, width: Int, height: Int, stride_align: Int) -> Bool { - // The rows of buffer are padded to stride_align. - let row = width * 4 - let stride = stride_align > 1 ? (row + stride_align - 1) / stride_align * stride_align : row - guard width > 0 && height > 0 && len >= stride * height else { - return false - } var pixelBufferCopy: CVPixelBuffer? - guard let pool = poolOf(width: width, height: height), - CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, pool, &pixelBufferCopy) == kCVReturnSuccess else { - return false - } - - // Outside the queue: the raster thread waits on it for the newest frame. - CVPixelBufferLockBaseAddress(pixelBufferCopy!, []) - let ptr = CVPixelBufferGetBaseAddress(pixelBufferCopy!)! - let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBufferCopy!) - if (bytesPerRow == stride) { - memcpy(ptr, buffer, stride * height) - } else { - for y in 0.. Bool in - if (closed) { - return false - } - self.data = pixelBufferCopy - return true - } - if open && textureId != -1 { - // On the platform (main) thread, which the engine checks. - DispatchQueue.main.async { - self.registry?.textureFrameAvailable(self.textureId) - } - return true - } else { - return false - } - } - - // 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 (!closed && (pool == nil || self.width != width || self.height != height)) { - pool = TextRgba.newPool(width: width, height: height) - self.width = width - self.height = height - } - return pool - } - } - - // 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) - } - - - private static func newPool(width: Int, height: Int) -> CVPixelBufferPool? { // macOS only support 32BGRA currently. let dict: [String: Any] = [ kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA, - kCVPixelBufferWidthKey as String: width, - kCVPixelBufferHeightKey as String: height, - kCVPixelBufferIOSurfacePropertiesKey as String: [String: Any](), kCVPixelBufferMetalCompatibilityKey as String: true, kCVPixelBufferOpenGLCompatibilityKey as String: true, // https://developer.apple.com/forums/thread/712709 kCVPixelBufferBytesPerRowAlignmentKey as String: 64 ] - // The handed buffers, the newest frame and the one being written. - let poolDict: [String: Any] = [kCVPixelBufferPoolMinimumBufferCountKey as String: handedFramesKept + 2] - var pool: CVPixelBufferPool? - CVPixelBufferPoolCreate(kCFAllocatorDefault, poolDict as CFDictionary, dict as CFDictionary, &pool) - return pool + let result = CVPixelBufferCreate(kCFAllocatorDefault, width, height, kCVPixelFormatType_32BGRA, dict as CFDictionary, &pixelBufferCopy) + guard result == kCVReturnSuccess else { + return false + } + + CVPixelBufferLockBaseAddress(pixelBufferCopy!, []) + let ptr = CVPixelBufferGetBaseAddress(pixelBufferCopy!)! + memcpy(ptr, buffer, len) + CVPixelBufferUnlockBaseAddress(pixelBufferCopy!, []) + self.data = pixelBufferCopy + self.width = width + self.height = height + if textureId != -1 && self.data != nil { + registry?.textureFrameAvailable(textureId) + return true + } else { + return false + } } + @objc public func markFrameAvaliableRaw(buffer: UnsafePointer, len: Int, width: Int, height: Int, stride_align: Int) -> Bool { + queue.sync { + _markFrameAvaliable(buffer: buffer, len: len, width: width, height: height, stride_align: stride_align) + } + } + + @objc public func markFrameAvaliable(data: Data, width: Int, height: Int, stride_align: Int) -> Bool { data.withUnsafeBytes { buffer in markFrameAvaliableRaw( diff --git a/macos/Classes/TextureRgbaApi.m b/macos/Classes/TextureRgbaApi.m index 6dc2812..3b4fd37 100644 --- a/macos/Classes/TextureRgbaApi.m +++ b/macos/Classes/TextureRgbaApi.m @@ -4,9 +4,6 @@ // // Created by kingtous on 2023/2/19. // -// Modified by the Kitedesk fork (2026, T-4204): an autorelease pool around -// each frame. -// #import #import "texture_rgba_renderer/texture_rgba_renderer-Swift.h" @@ -17,12 +14,8 @@ extern "C" { /// Keep the same name with Windows. void FlutterRgbaRendererPluginOnRgba(void* texture_rgba_ptr, const uint8_t* buffer, int len, int width, int height, int stride_align) { - // The caller's thread, a session's video thread, has no autorelease pool and - // lives as long as the session. - @autoreleasepool { - TextRgba* texture_rgba = (__bridge TextRgba *)(texture_rgba_ptr); - [texture_rgba markFrameAvaliableRawWithBuffer:buffer len:len width:width height:height stride_align: stride_align]; - } + TextRgba* texture_rgba = (__bridge TextRgba *)(texture_rgba_ptr); + [texture_rgba markFrameAvaliableRawWithBuffer:buffer len:len width:width height:height stride_align: stride_align]; } #if __cplusplus diff --git a/macos/Classes/TextureRgbaRendererPlugin.swift b/macos/Classes/TextureRgbaRendererPlugin.swift index 56b771e..8c991a6 100644 --- a/macos/Classes/TextureRgbaRendererPlugin.swift +++ b/macos/Classes/TextureRgbaRendererPlugin.swift @@ -1,7 +1,3 @@ -// 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 @@ -9,12 +5,6 @@ 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) @@ -22,14 +12,6 @@ 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 @@ -52,15 +34,9 @@ public class TextureRgbaRendererPlugin: NSObject, FlutterPlugin { case "closeTexture": let args = call.arguments as! Dictionary let key = args["key"] as! Int64 - // "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) + if (renderer[key] != nil) { + if (-1 != renderer[key]?.textureId) { + texture_registry?.unregisterTexture(renderer[key]?.textureId ?? -1) } result(true) } else { diff --git a/macos/test/run_tests.sh b/macos/test/run_tests.sh deleted file mode 100755 index 3c4b9d7..0000000 --- a/macos/test/run_tests.sh +++ /dev/null @@ -1,65 +0,0 @@ -#!/usr/bin/env bash -# Kitedesk fork, 2026: builds macos/test/texture_rgba_renderer_plugin_test.m with -# the plugin sources of macos/Classes against FlutterMacOS.framework, by default -# the darwin-x64 engine artifacts of the Flutter SDK on PATH (or FLUTTER_ROOT) as -# `flutter precache --macos` leaves them, and runs each test in its own process. -# Swift 5 mode, as the podspec. No app, window or screen capture; Metal is used -# without a display. -# run_tests.sh plain build -# run_tests.sh --asan the same with AddressSanitizer -# run_tests.sh --tsan the same with ThreadSanitizer -# FLUTTER_MACOS_FRAMEWORKS: the directory holding FlutterMacOS.framework. -# OUT_DIR (default: a new temporary directory) receives the build. -set -euo pipefail -here=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) -classes=$here/../Classes -if [ -z "${FLUTTER_MACOS_FRAMEWORKS:-}" ]; then - root=${FLUTTER_ROOT:-$(dirname "$(dirname "$(readlink -f "$(command -v flutter)")")")} - FLUTTER_MACOS_FRAMEWORKS=$root/bin/cache/artifacts/engine/darwin-x64/FlutterMacOS.xcframework/macos-arm64_x86_64 -fi -frameworks=$FLUTTER_MACOS_FRAMEWORKS -[ -f "$frameworks/FlutterMacOS.framework/Headers/FlutterTexture.h" ] || { - echo "no FlutterMacOS.framework in $frameworks" \ - "(flutter precache --macos, or set FLUTTER_MACOS_FRAMEWORKS)" >&2 - exit 2 -} -out=${OUT_DIR:-$(mktemp -d)} -mkdir -p "$out/include/texture_rgba_renderer" -sanitizer=() -case "${1:-}" in - "") ;; - --asan) sanitizer=(-sanitize=address) ;; - --tsan) - sanitizer=(-sanitize=thread) - export TSAN_OPTIONS=${TSAN_OPTIONS:-halt_on_error=1} - ;; - *) echo "usage: $0 [--asan | --tsan]" >&2; exit 2 ;; -esac -clang_sanitizer=() -[ ${#sanitizer[@]} -eq 0 ] || clang_sanitizer=("-f${sanitizer[0]#-}" -fno-omit-frame-pointer) -cache=$out/module-cache -xcrun swiftc -c -wmo -g -O -swift-version 5 -parse-as-library -module-name texture_rgba_renderer \ - ${sanitizer[@]+"${sanitizer[@]}"} -F "$frameworks" -module-cache-path "$cache" \ - -emit-objc-header-path "$out/include/texture_rgba_renderer/texture_rgba_renderer-Swift.h" \ - "$classes"/*.swift -o "$out/plugin.o" -objc=(xcrun clang -c -g -O1 -fobjc-arc -fmodules -fmodules-cache-path="$cache" - ${clang_sanitizer[@]+"${clang_sanitizer[@]}"} -F "$frameworks" -I "$out/include") -"${objc[@]}" "$classes/TextureRgbaApi.m" -o "$out/api.o" -"${objc[@]}" -Wall -Werror "$here/texture_rgba_renderer_plugin_test.m" -o "$out/test.o" -bin=$out/texture_rgba_renderer_plugin_test -xcrun swiftc ${sanitizer[@]+"${sanitizer[@]}"} -F "$frameworks" -framework FlutterMacOS \ - -framework CoreVideo -framework IOSurface -framework Metal -Xlinker -rpath -Xlinker "$frameworks" \ - "$out/plugin.o" "$out/api.o" "$out/test.o" -o "$bin" -count=0 -failed=0 -for test in $("$bin" --list); do - count=$((count + 1)) - if "$bin" "$test"; then - echo "PASS $test" - else - echo "FAIL $test" - failed=$((failed + 1)) - fi -done -echo "$count tests, $((count - failed)) passed, $failed failed" -[ "$failed" -eq 0 ] diff --git a/macos/test/texture_rgba_renderer_plugin_test.m b/macos/test/texture_rgba_renderer_plugin_test.m deleted file mode 100644 index e1a6e11..0000000 --- a/macos/test/texture_rgba_renderer_plugin_test.m +++ /dev/null @@ -1,750 +0,0 @@ -// Kitedesk fork, 2026: tests for the macOS texture of this plugin (Kitedesk -// backlog T-4204). Licensed under the Apache License, Version 2.0, like the rest -// of this package. -// -// macos/test/run_tests.sh builds this program with the plugin sources against -// FlutterMacOS.framework of the Flutter engine (a18df97c, Flutter 3.24.5) and -// runs each test in its own process. Nothing is drawn on screen: the engine is -// the fake below. It follows FlutterTextureRegistrar.mm and -// FlutterExternalTexture.mm of that engine: the registrar keeps a registered -// texture until it is unregistered and uses its address as the texture id; for -// a new frame the raster thread calls copyPixelBuffer, makes a Metal texture of -// the pixel buffer with a CVMetalTextureCache and releases the CVMetalTexture and -// the pixel buffer right away, while Skia goes on drawing that Metal texture -// until the next frame. textureFrameAvailable: belongs on the platform (main) -// thread (Shell::OnPlatformViewMarkTextureFrameAvailable checks it). When a -// window closes, its engine, registrar and plugins go away. The Rust video -// thread is FlutterRgbaRendererPluginOnRgba with the pointer from getTexturePtr, -// on a thread of its own without an autorelease pool. -// -// Usage: texture_rgba_renderer_plugin_test [--list | ] - -#import -#import -#import -#import -#import -#include -#include -#include -#include -#include -#include -#include -#include - -// The generated header declares copyPixelBuffer's CVPixelBufferRef __unsafe_unretained. -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wignored-attributes" -#import "texture_rgba_renderer/texture_rgba_renderer-Swift.h" -#pragma clang diagnostic pop - -void FlutterRgbaRendererPluginOnRgba(void* texture_rgba_ptr, const uint8_t* buffer, int len, int width, - int height, int stride_align); - -#if defined(__has_feature) -#if __has_feature(address_sanitizer) -#define KD_ASAN 1 -#endif -#endif - -static int failures = 0; - -#define EXPECT(condition, ...) \ - do { \ - if (!(condition)) { \ - failures++; \ - fprintf(stderr, "%s:%d: expected %s: ", __FILE__, __LINE__, #condition); \ - fprintf(stderr, __VA_ARGS__); \ - fprintf(stderr, "\n"); \ - } \ - } while (0) - -// textureFrameAvailable: calls, and those not on the main thread. -static atomic_int notices = 0; -static atomic_int off_main_notices = 0; - -static uint64_t now_ns(void) { return clock_gettime_nsec_np(CLOCK_UPTIME_RAW); } - -// Memory of the process, IOSurfaces included (what Activity Monitor shows). -static double footprint_mb(void) { - task_vm_info_data_t info; - mach_msg_type_number_t count = TASK_VM_INFO_COUNT; - if (task_info(mach_task_self(), TASK_VM_INFO, (task_info_t)&info, &count) != KERN_SUCCESS) { - return 0; - } - return info.phys_footprint / 1048576.0; -} - -@interface FakeEngine : NSObject -// What the raster thread does for a new frame of the texture: the Metal texture -// it draws from now on, or nil when the texture gives no frame. -- (id)draw:(int64_t)textureId; -@end - -@implementation FakeEngine { - // FlutterTextureRegistrar's textures (FlutterExternalTexture keeps the texture). - NSMutableDictionary*>* _textures; - // Skia's last image of each texture. - NSMutableDictionary>* _drawn; - id _device; - CVMetalTextureCacheRef _cache; -} - -- (instancetype)init { - if (self = [super init]) { - _textures = [NSMutableDictionary dictionary]; - _drawn = [NSMutableDictionary dictionary]; - _device = MTLCreateSystemDefaultDevice(); - CVMetalTextureCacheCreate(kCFAllocatorDefault, NULL, _device, NULL, &_cache); - } - return self; -} - -- (void)dealloc { - if (_cache) { - CFRelease(_cache); - } -} - -- (int64_t)registerTexture:(NSObject*)texture { - int64_t textureId = (int64_t)(intptr_t)(__bridge void*)texture; - _textures[@(textureId)] = texture; - return textureId; -} - -- (void)textureFrameAvailable:(int64_t)textureId { - atomic_fetch_add(¬ices, 1); - if (![NSThread isMainThread]) { - atomic_fetch_add(&off_main_notices, 1); - } -} - -- (void)unregisterTexture:(int64_t)textureId { - [_textures removeObjectForKey:@(textureId)]; - [_drawn removeObjectForKey:@(textureId)]; -} - -- (id)draw:(int64_t)textureId { - CVPixelBufferRef pixelBuffer = [_textures[@(textureId)] copyPixelBuffer]; - id drawn = nil; - if (pixelBuffer) { - CVMetalTextureRef cvMetalTexture = NULL; - CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault, _cache, pixelBuffer, NULL, - MTLPixelFormatBGRA8Unorm, CVPixelBufferGetWidth(pixelBuffer), - CVPixelBufferGetHeight(pixelBuffer), 0, &cvMetalTexture); - if (cvMetalTexture) { - drawn = CVMetalTextureGetTexture(cvMetalTexture); - CFRelease(cvMetalTexture); - } - CVPixelBufferRelease(pixelBuffer); - } - if (drawn) { - _drawn[@(textureId)] = drawn; - } else { - [_drawn removeObjectForKey:@(textureId)]; - } - return drawn; -} - -@end - -// Plugins only make their channel with the engine's binary messenger here. -@interface FakeMessenger : NSObject -@end - -@implementation FakeMessenger -- (void)sendOnChannel:(NSString*)channel message:(NSData*)message { -} -- (void)sendOnChannel:(NSString*)channel message:(NSData*)message binaryReply:(FlutterBinaryReply)callback { -} -- (FlutterBinaryMessengerConnection)setMessageHandlerOnChannel:(NSString*)channel - binaryMessageHandler:(FlutterBinaryMessageHandler)handler { - return 0; -} -- (void)cleanUpConnection:(FlutterBinaryMessengerConnection)connection { -} -@end - -// The plugin registrar of one engine (window). It keeps the plugin, as the -// engine's channel handler does, until the engine goes away. -@interface FakeRegistrar : NSObject -- (instancetype)initWithEngine:(FakeEngine*)engine; -@property(nonatomic, readonly) NSObject* plugin; -@end - -@implementation FakeRegistrar { - FakeEngine* _engine; - FakeMessenger* _messenger; -} -- (instancetype)initWithEngine:(FakeEngine*)engine { - if (self = [super init]) { - _engine = engine; - _messenger = [FakeMessenger new]; - } - return self; -} -- (id)messenger { - return _messenger; -} -- (id)textures { - return _engine; -} -- (NSView*)view { - return nil; -} -- (void)addMethodCallDelegate:(id)delegate channel:(FlutterMethodChannel*)channel { - _plugin = delegate; -} -- (void)addApplicationDelegate:(NSObject*)delegate { -} -- (void)registerViewFactory:(NSObject*)factory withId:(NSString*)factoryId { -} -- (void)publish:(NSObject*)value { -} -- (NSString*)lookupKeyForAsset:(NSString*)asset { - return asset; -} -- (NSString*)lookupKeyForAsset:(NSString*)asset fromPackage:(NSString*)package { - return asset; -} -@end - -static id call(TextureRgbaRendererPlugin* plugin, NSString* method, NSDictionary* arguments) { - __block id answer = nil; - __block BOOL answered = NO; - [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:method arguments:arguments] - result:^(id result) { - answer = result; - answered = YES; - }]; - EXPECT(answered, "an answer to %s", method.UTF8String); - return answer; -} - -// The plugin, registered as an engine registers it. -static TextureRgbaRendererPlugin* new_plugin(FakeEngine* engine) { - FakeRegistrar* registrar = [[FakeRegistrar alloc] initWithEngine:engine]; - [TextureRgbaRendererPlugin registerWithRegistrar:registrar]; - EXPECT([registrar.plugin isKindOfClass:[TextureRgbaRendererPlugin class]], "the plugin registered"); - return (TextureRgbaRendererPlugin*)registrar.plugin; -} - -static int64_t create_texture(TextureRgbaRendererPlugin* plugin, int64_t key) { - return [call(plugin, @"createTexture", @{@"key" : @(key)}) longLongValue]; -} - -static void* texture_pointer(TextureRgbaRendererPlugin* plugin, int64_t key) { - return (void*)(uintptr_t)[call(plugin, @"getTexturePtr", @{@"key" : @(key)}) unsignedLongValue]; -} - -static void close_texture(TextureRgbaRendererPlugin* plugin, int64_t key, BOOL recycle) { - call(plugin, @"closeTexture", @{@"key" : @(key), @"recycle" : @(recycle)}); -} - -// Runs the main queue up to what is queued now, as the platform thread does. -static void drain_main_queue(void) { - __block BOOL done = NO; - dispatch_async(dispatch_get_main_queue(), ^{ - done = YES; - }); - while (!done) { - CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.01, true); - } -} - -static void* run_block(void* block) { - ((__bridge_transfer void (^)(void))block)(); - return NULL; -} - -// Runs |block| on a new thread with no autorelease pool, as a Rust video thread. -static pthread_t start_video_thread(void (^block)(void)) { - pthread_t thread; - pthread_create(&thread, NULL, run_block, (__bridge_retained void*)[block copy]); - return thread; -} - -static void on_video_thread(void (^block)(void)) { pthread_join(start_video_thread(block), NULL); } - -// A frame of |width| x |height| BGRA pixels in rows of |stride| bytes; the -// padding at the end of a row is 0xee. -static NSData* make_frame(int width, int height, int stride, int seed) { - NSMutableData* frame = [NSMutableData dataWithLength:(NSUInteger)stride * height]; - uint8_t* bytes = frame.mutableBytes; - memset(bytes, 0xee, frame.length); - for (int y = 0; y < height; y++) { - for (int x = 0; x < width * 4; x++) { - bytes[(size_t)y * stride + x] = (uint8_t)(seed * 31 + y * 7 + x); - } - } - return frame; -} - -static void push_bytes(void* pointer, NSData* frame, int length, int width, int height, int stride_align) { - const uint8_t* bytes = frame.bytes; - on_video_thread(^{ - FlutterRgbaRendererPluginOnRgba(pointer, bytes, length, width, height, stride_align); - }); -} - -static void push(void* pointer, NSData* frame, int width, int height, int stride_align) { - push_bytes(pointer, frame, (int)frame.length, width, height, stride_align); -} - -// The first row of |surface| that differs from the frame, or -1. -static int first_wrong_row(IOSurfaceRef surface, NSData* frame, int width, int height, int stride) { - if (!surface || (int)IOSurfaceGetWidth(surface) != width || (int)IOSurfaceGetHeight(surface) != height) { - return 0; - } - int wrong = -1; - IOSurfaceLock(surface, kIOSurfaceLockReadOnly, NULL); - const uint8_t* pixels = IOSurfaceGetBaseAddress(surface); - size_t row = IOSurfaceGetBytesPerRow(surface); - for (int y = 0; y < height && wrong < 0; y++) { - if (memcmp(pixels + y * row, (const uint8_t*)frame.bytes + (size_t)y * stride, (size_t)width * 4) != 0) { - wrong = y; - } - } - IOSurfaceUnlock(surface, kIOSurfaceLockReadOnly, NULL); - return wrong; -} - -static int compare_u64(const void* a, const void* b) { - uint64_t x = *(const uint64_t*)a, y = *(const uint64_t*)b; - return x < y ? -1 : x > y; -} - -// Frames of one size go to a few pooled IOSurfaces, as many whatever the -// number of frames; a new size gets buffers of its own. -static void pool_reuses_buffers(void) { - FakeEngine* engine = [FakeEngine new]; - TextureRgbaRendererPlugin* plugin = new_plugin(engine); - int64_t texture = create_texture(plugin, 1); - void* pointer = texture_pointer(plugin, 1); - static const int sizes[][2] = {{640, 480}, {800, 600}}; - for (int s = 0; s < 2; s++) { - int width = sizes[s][0], height = sizes[s][1]; - // Kept, so that no IOSurface id is reused for a new IOSurface. - NSMutableArray>* drawn = [NSMutableArray array]; - NSMutableSet* surfaces = [NSMutableSet set]; - for (int i = 0; i < 60; i++) { - push(pointer, make_frame(width, height, width * 4, i), width, height, 1); - drain_main_queue(); - id frame = [engine draw:texture]; - EXPECT(frame && frame.iosurface && (int)frame.width == width && (int)frame.height == height, - "frame %d of %dx%d drawn from an IOSurface of that size", i, width, height); - if (frame.iosurface) { - [drawn addObject:frame]; - [surfaces addObject:@(IOSurfaceGetID(frame.iosurface))]; - } - } - EXPECT(surfaces.count <= 4, "%lu IOSurfaces for 60 frames of %dx%d", (unsigned long)surfaces.count, width, - height); - } -} - -// The pool does not wait until the engine's GPU work with a buffer is done (it -// hands out a buffer whose IOSurface backs a live Metal texture), and the -// engine lets go of its pixel buffer as soon as it has the Metal texture: the -// buffer the engine draws must not be written while frames come and the engine -// takes none. -static void drawn_frame_is_not_overwritten(void) { - FakeEngine* engine = [FakeEngine new]; - TextureRgbaRendererPlugin* plugin = new_plugin(engine); - int64_t texture = create_texture(plugin, 1); - void* pointer = texture_pointer(plugin, 1); - const int width = 320, height = 240, stride = width * 4; - NSData* first = make_frame(width, height, stride, 1); - push(pointer, first, width, height, 1); - drain_main_queue(); - id drawn = [engine draw:texture]; - NSData* last = nil; - for (int i = 2; i <= 8; i++) { - last = make_frame(width, height, stride, i); - push(pointer, last, width, height, 1); - drain_main_queue(); - } - int wrong = first_wrong_row(drawn.iosurface, first, width, height, stride); - EXPECT(wrong < 0, "the frame the engine draws unchanged by newer frames, row %d differs", wrong); - wrong = first_wrong_row([engine draw:texture].iosurface, last, width, height, stride); - EXPECT(wrong < 0, "the newest frame drawn next, row %d differs", wrong); -} - -// Nor the frame the engine was handed before that one: GPU work queued while it -// was drawn may not have run yet, and the plugin cannot see when it has. -static void frame_drawn_before_is_not_overwritten(void) { - FakeEngine* engine = [FakeEngine new]; - TextureRgbaRendererPlugin* plugin = new_plugin(engine); - int64_t texture = create_texture(plugin, 1); - void* pointer = texture_pointer(plugin, 1); - const int width = 320, height = 240, stride = width * 4; - NSData* before = make_frame(width, height, stride, 1); - push(pointer, before, width, height, 1); - drain_main_queue(); - id drawn_before = [engine draw:texture]; - NSData* drawn_frame = make_frame(width, height, stride, 2); - push(pointer, drawn_frame, width, height, 1); - drain_main_queue(); - id drawn = [engine draw:texture]; - NSData* last = nil; - for (int i = 3; i <= 9; i++) { - last = make_frame(width, height, stride, i); - push(pointer, last, width, height, 1); - drain_main_queue(); - } - int wrong = first_wrong_row(drawn_before.iosurface, before, width, height, stride); - EXPECT(wrong < 0, "the frame drawn before the last one unchanged by newer frames, row %d differs", wrong); - wrong = first_wrong_row(drawn.iosurface, drawn_frame, width, height, stride); - EXPECT(wrong < 0, "the frame the engine draws unchanged by newer frames, row %d differs", wrong); - wrong = first_wrong_row([engine draw:texture].iosurface, last, width, height, stride); - EXPECT(wrong < 0, "the newest frame drawn next, row %d differs", wrong); -} - -// The raster thread asks for the newest frame (copyPixelBuffer) while the video -// thread copies 4K frames back to back: it must not wait for a copy. -static void raster_does_not_wait_for_copies(void) { - FakeEngine* engine = [FakeEngine new]; - TextureRgbaRendererPlugin* plugin = new_plugin(engine); - create_texture(plugin, 1); - void* pointer = texture_pointer(plugin, 1); - TextRgba* texture = (__bridge TextRgba*)pointer; - const int width = 3840, height = 2160; - NSData* frame = make_frame(width, height, width * 4, 1); - const uint8_t* bytes = frame.bytes; - const int length = (int)frame.length; - static atomic_bool stop = false; - static atomic_int pushed = 0; - pthread_t video = start_video_thread(^{ - while (!atomic_load(&stop)) { - FlutterRgbaRendererPluginOnRgba(pointer, bytes, length, width, height, 64); - atomic_fetch_add(&pushed, 1); - } - }); - enum { kCalls = 600 }; - static uint64_t waits[kCalls]; - int calls = 0; - for (; calls < kCalls; calls++) { - uint64_t start = now_ns(); - CVPixelBufferRef newest = [texture copyPixelBuffer]; - waits[calls] = now_ns() - start; - if (newest) { - CVPixelBufferRelease(newest); - } - usleep(1000); - } - atomic_store(&stop, true); - pthread_join(video, NULL); - drain_main_queue(); - qsort(waits, calls, sizeof waits[0], compare_u64); - int over_1ms = 0; - for (int i = 0; i < calls; i++) { - over_1ms += waits[i] > 1000000; - } - double p50 = waits[calls / 2] / 1e6, max = waits[calls - 1] / 1e6; - fprintf(stderr, "raster waits: %d calls, %d frames pushed, p50 %.3f ms, max %.3f ms, %d over 1 ms\n", calls, - atomic_load(&pushed), p50, max, over_1ms); - EXPECT(atomic_load(&pushed) > 0, "frames pushed meanwhile"); - EXPECT(p50 < 0.25, "a median wait of %.3f ms", p50); - EXPECT(over_1ms * 100 <= calls, "%d of %d calls waited over 1 ms", over_1ms, calls); -} - -// textureFrameAvailable: on the main thread, not on the video thread. -static void frames_are_announced_on_the_main_thread(void) { - FakeEngine* engine = [FakeEngine new]; - TextureRgbaRendererPlugin* plugin = new_plugin(engine); - create_texture(plugin, 1); - void* pointer = texture_pointer(plugin, 1); - push(pointer, make_frame(64, 64, 256, 1), 64, 64, 1); - drain_main_queue(); - EXPECT(atomic_load(¬ices) == 1, "one notice for one frame, got %d", atomic_load(¬ices)); - EXPECT(atomic_load(&off_main_notices) == 0, "%d notices off the main thread", atomic_load(&off_main_notices)); -} - -// The engine's CVMetalTextureCache keeps what it wrapped until it makes a -// texture again about a second later (measured): age that out as the engine -// does when it draws its next frame, so that only what the plugin keeps is left. -static void age_texture_cache(TextureRgbaRendererPlugin* plugin, FakeEngine* engine, int64_t key) { - usleep(1500000); - @autoreleasepool { - int64_t texture = create_texture(plugin, key); - push(texture_pointer(plugin, key), make_frame(64, 64, 256, 1), 64, 64, 1); - drain_main_queue(); - [engine draw:texture]; - close_texture(plugin, key, YES); - drain_main_queue(); - } -} - -// Each texture a session opens and closes (a display switch, a tab closing) -// leaves no frame buffer behind, with or without "recycle". -static void closing_frees_frame_buffers(void) { - FakeEngine* engine = [FakeEngine new]; - TextureRgbaRendererPlugin* plugin = new_plugin(engine); - const int width = 3840, height = 2160; - NSData* frame = make_frame(width, height, width * 4, 1); - double before = 0; - for (int key = 0; key <= 20; key++) { - @autoreleasepool { - int64_t texture = create_texture(plugin, key); - void* pointer = texture_pointer(plugin, key); - push(pointer, frame, width, height, 64); - drain_main_queue(); - [engine draw:texture]; - push(pointer, frame, width, height, 64); - drain_main_queue(); - close_texture(plugin, key, key % 2 == 0); - drain_main_queue(); - } - if (key == 0) { - age_texture_cache(plugin, engine, 100); - before = footprint_mb(); - } - } - age_texture_cache(plugin, engine, 101); - double growth = footprint_mb() - before; - fprintf(stderr, "closing: %.1f MB more after 20 more 4K textures were opened and closed\n", growth); -#ifndef KD_ASAN - // ASan keeps shadow memory, an eighth of it, for each new IOSurface mapping - // memcpy writes to, and that counts too (76 MB here, its quarantine off). - EXPECT(growth < 64, "%.1f MB more after 20 more 4K textures were opened and closed (a frame is %.1f MB)", - growth, width * height * 4 / 1048576.0); -#endif -} - -// After closeTexture without "recycle" the session may still push frames to the -// pointer for a while: they are dropped, without a notice or a frame for the -// engine, and the texture stays a valid object. -static void closed_texture_drops_late_frames(void) { - FakeEngine* engine = [FakeEngine new]; - TextureRgbaRendererPlugin* plugin = new_plugin(engine); - int64_t texture = create_texture(plugin, 1); - void* pointer = texture_pointer(plugin, 1); - NSData* frame = make_frame(640, 480, 2560, 1); - push(pointer, frame, 640, 480, 64); - drain_main_queue(); - [engine draw:texture]; - close_texture(plugin, 1, NO); - drain_main_queue(); - int before = atomic_load(¬ices); - for (int i = 0; i < 5; i++) { - push(pointer, frame, 640, 480, 64); - } - drain_main_queue(); - EXPECT(atomic_load(¬ices) == before, "%d notices for frames after closing", atomic_load(¬ices) - before); - CVPixelBufferRef late = [(__bridge TextRgba*)pointer copyPixelBuffer]; - EXPECT(late == NULL, "no frame from a closed texture"); - if (late) { - CVPixelBufferRelease(late); - } -} - -// Without "recycle" the pointer of a closed texture may still get frames, so the -// texture is never freed; with "recycle" nothing pushes frames to it any more, -// and it goes once the engine has unregistered it. -static void closing_keeps_only_unrecycled_textures(void) { - __weak TextRgba* kept = nil; - __weak TextRgba* recycled = nil; - FakeEngine* engine = [FakeEngine new]; - TextureRgbaRendererPlugin* plugin = new_plugin(engine); - @autoreleasepool { - NSData* frame = make_frame(64, 64, 256, 1); - for (int key = 1; key <= 2; key++) { - int64_t texture = create_texture(plugin, key); - void* pointer = texture_pointer(plugin, key); - push(pointer, frame, 64, 64, 1); - drain_main_queue(); - [engine draw:texture]; - if (key == 1) { - kept = (__bridge TextRgba*)pointer; - } else { - recycled = (__bridge TextRgba*)pointer; - } - close_texture(plugin, key, key == 2); - drain_main_queue(); - } - } - EXPECT(kept != nil, "the texture closed without recycle still there"); - EXPECT(recycled == nil, "the texture closed with recycle freed"); -} - -// A window closing first: its engine and plugin go away while a session may -// still push frames to the pointer of a texture nobody closed. -static void open_textures_outlive_their_engine(void) { - __weak TextRgba* weak = nil; - void* pointer = NULL; - NSData* frame = make_frame(64, 64, 256, 1); - @autoreleasepool { - FakeEngine* engine = [FakeEngine new]; - TextureRgbaRendererPlugin* plugin = new_plugin(engine); - int64_t texture = create_texture(plugin, 1); - pointer = texture_pointer(plugin, 1); - push(pointer, frame, 64, 64, 1); - drain_main_queue(); - [engine draw:texture]; - weak = (__bridge TextRgba*)pointer; - } - drain_main_queue(); - EXPECT(weak != nil, "the texture alive after its engine went away"); - if (weak) { - int before = atomic_load(¬ices); - push(pointer, frame, 64, 64, 1); - drain_main_queue(); - EXPECT(atomic_load(¬ices) == before, "no notice for a frame after the engine went away"); - } -} - -static int round_up(int value, int alignment) { - return alignment > 1 ? (value + alignment - 1) / alignment * alignment : value; -} - -// Rows come padded to stride_align (src/flutter.rs rgba_align: 64 for a macOS -// texture, 1 for a session drawn in software, whose "all displays" view still -// uses textures), the pixel buffer has its own bytes per row. -static void rows_follow_the_frame_stride(void) { - FakeEngine* engine = [FakeEngine new]; - TextureRgbaRendererPlugin* plugin = new_plugin(engine); - static const int cases[][2] = {{1366, 1}, {1001, 1}, {1279, 1}, {3838, 1}, - {1366, 64}, {1001, 64}, {3840, 64}, {1001, 256}}; - const int height = 16; - for (int c = 0; c < (int)(sizeof cases / sizeof cases[0]); c++) { - int width = cases[c][0], align = cases[c][1], stride = round_up(width * 4, align); - int64_t texture = create_texture(plugin, c); - void* pointer = texture_pointer(plugin, c); - NSData* frame = make_frame(width, height, stride, c); - push(pointer, frame, width, height, align); - drain_main_queue(); - int wrong = first_wrong_row([engine draw:texture].iosurface, frame, width, height, stride); - EXPECT(wrong < 0, "%d pixels wide in rows of %d bytes: row %d differs", width, stride, wrong); - close_texture(plugin, c, YES); - drain_main_queue(); - } -} - -// A frame shorter than its rows promise is not read past its end. -static void short_frames_are_dropped(void) { - FakeEngine* engine = [FakeEngine new]; - TextureRgbaRendererPlugin* plugin = new_plugin(engine); - int64_t texture = create_texture(plugin, 1); - void* pointer = texture_pointer(plugin, 1); - NSData* frame = make_frame(640, 480, 2560, 1); - push_bytes(pointer, frame, (int)frame.length - 1, 640, 480, 64); - drain_main_queue(); - EXPECT(atomic_load(¬ices) == 0, "no notice for a short frame"); - EXPECT([engine draw:texture] == nil, "no frame for the engine"); -} - -// The video thread has no autorelease pool: whatever the plugin autoreleases -// there would pile up until the session ends. OBJC_DEBUG_MISSING_POOLS makes the -// runtime report each such object on stderr. -static void frames_leave_no_autoreleased_objects(void) { - EXPECT(getenv("OBJC_DEBUG_MISSING_POOLS") != NULL, "OBJC_DEBUG_MISSING_POOLS set (main sets it)"); - FakeEngine* engine = [FakeEngine new]; - TextureRgbaRendererPlugin* plugin = new_plugin(engine); - int64_t texture = create_texture(plugin, 1); - void* pointer = texture_pointer(plugin, 1); - char path[] = "/tmp/texture_rgba_renderer_test.XXXXXX"; - int log = mkstemp(path); - int saved = dup(STDERR_FILENO); - fflush(stderr); - dup2(log, STDERR_FILENO); - // New sizes, so that buffers are made, not only reused. - for (int i = 0; i < 3; i++) { - int width = 320 + 64 * i, height = 240; - push(pointer, make_frame(width, height, width * 4, i), width, height, 64); - } - fflush(stderr); - dup2(saved, STDERR_FILENO); - close(saved); - drain_main_queue(); - [engine draw:texture]; - NSString* reported = [NSString stringWithContentsOfFile:@(path) encoding:NSUTF8StringEncoding error:nil] ?: @""; - close(log); - unlink(path); - NSUInteger objects = [reported componentsSeparatedByString:@"autoreleased with no pool in place"].count - 1; - EXPECT(objects == 0, "%lu objects autoreleased on the video thread:\n%s", (unsigned long)objects, - reported.UTF8String); -} - -// Frames pushed while the texture is being closed, for the sanitizers. -static void frames_racing_close(void) { - FakeEngine* engine = [FakeEngine new]; - TextureRgbaRendererPlugin* plugin = new_plugin(engine); - const int width = 1920, height = 1080; - NSData* frame = make_frame(width, height, width * 4, 1); - const uint8_t* bytes = frame.bytes; - const int length = (int)frame.length; - for (int key = 0; key < 20; key++) { - @autoreleasepool { - int64_t texture = create_texture(plugin, key); - void* pointer = texture_pointer(plugin, key); - static atomic_bool stop; - atomic_store(&stop, false); - pthread_t video = start_video_thread(^{ - while (!atomic_load(&stop)) { - FlutterRgbaRendererPluginOnRgba(pointer, bytes, length, width, height, 64); - } - }); - usleep(2000 + key * 300); - [engine draw:texture]; - close_texture(plugin, key, NO); - usleep(2000); - atomic_store(&stop, true); - pthread_join(video, NULL); - drain_main_queue(); - int before = atomic_load(¬ices); - push(pointer, frame, width, height, 64); - drain_main_queue(); - EXPECT(atomic_load(¬ices) == before, "texture %d: a notice after closing", key); - } - } -} - -typedef struct { - const char* name; - void (*run)(void); -} Test; - -static const Test kTests[] = { - {"pool_reuses_buffers", pool_reuses_buffers}, - {"drawn_frame_is_not_overwritten", drawn_frame_is_not_overwritten}, - {"frame_drawn_before_is_not_overwritten", frame_drawn_before_is_not_overwritten}, - {"raster_does_not_wait_for_copies", raster_does_not_wait_for_copies}, - {"frames_are_announced_on_the_main_thread", frames_are_announced_on_the_main_thread}, - {"closing_frees_frame_buffers", closing_frees_frame_buffers}, - {"closed_texture_drops_late_frames", closed_texture_drops_late_frames}, - {"closing_keeps_only_unrecycled_textures", closing_keeps_only_unrecycled_textures}, - {"open_textures_outlive_their_engine", open_textures_outlive_their_engine}, - {"rows_follow_the_frame_stride", rows_follow_the_frame_stride}, - {"short_frames_are_dropped", short_frames_are_dropped}, - {"frames_leave_no_autoreleased_objects", frames_leave_no_autoreleased_objects}, - {"frames_racing_close", frames_racing_close}, -}; - -int main(int argc, char** argv) { - if (argc == 2 && strcmp(argv[1], "--list") == 0) { - for (size_t i = 0; i < sizeof kTests / sizeof kTests[0]; i++) { - printf("%s\n", kTests[i].name); - } - return 0; - } - if (argc != 2) { - fprintf(stderr, "usage: %s [--list | ]\n", argv[0]); - return 2; - } - // The runtime reads it at startup. - if (strcmp(argv[1], "frames_leave_no_autoreleased_objects") == 0 && !getenv("OBJC_DEBUG_MISSING_POOLS")) { - setenv("OBJC_DEBUG_MISSING_POOLS", "YES", 1); - execv(argv[0], argv); - } - for (size_t i = 0; i < sizeof kTests / sizeof kTests[0]; i++) { - if (strcmp(argv[1], kTests[i].name) == 0) { - @autoreleasepool { - kTests[i].run(); - } - return failures == 0 ? 0 : 1; - } - } - fprintf(stderr, "no test %s\n", argv[1]); - return 2; -} 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; - }); } diff --git a/windows/test/run_tests.sh b/windows/test/run_tests.sh deleted file mode 100755 index d66c28c..0000000 --- a/windows/test/run_tests.sh +++ /dev/null @@ -1,58 +0,0 @@ -#!/usr/bin/env bash -# Kitedesk fork, 2026: builds windows/test/texture_rgba_renderer_plugin_test.cc -# on Linux with the plugin sources and the C++ client wrapper of the Flutter -# Windows engine, and runs each test in its own process. The engine files come -# from FLUTTER_WINDOWS_ENGINE, by default the windows-x64 engine artifacts of the -# Flutter SDK on PATH (or FLUTTER_ROOT) as `flutter precache --windows` leaves -# them: the C headers and cpp_client_wrapper/. No display, GPU or Windows needed. -# run_tests.sh -Wall -Werror build -# run_tests.sh --asan the same with AddressSanitizer and UBSan -# run_tests.sh --tsan the same with ThreadSanitizer -# OUT_DIR (default: a new temporary directory) receives the test binary. -set -euo pipefail -here=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) -if [ -z "${FLUTTER_WINDOWS_ENGINE:-}" ]; then - root=${FLUTTER_ROOT:-$(dirname "$(dirname "$(readlink -f "$(command -v flutter)")")")} - FLUTTER_WINDOWS_ENGINE=$root/bin/cache/artifacts/engine/windows-x64 -fi -engine=$FLUTTER_WINDOWS_ENGINE -wrapper=$engine/cpp_client_wrapper -[ -f "$engine/flutter_texture_registrar.h" ] && [ -f "$wrapper/core_implementations.cc" ] || { - echo "no Flutter Windows engine headers and client wrapper in $engine" \ - "(flutter precache --windows, or set FLUTTER_WINDOWS_ENGINE)" >&2 - exit 2 -} -out=${OUT_DIR:-$(mktemp -d)} -mkdir -p "$out" -# The client wrapper headers expect to come with the standard headers, -# as MSVC's do; libstdc++'s do not. -flags=(-std=c++17 -Wall -Werror -O1 -g -pthread -include cstdint -DFLUTTER_PLUGIN_IMPL '-D__declspec(x)=') -case "${1:-}" in - "") ;; - --asan) flags+=('-fsanitize=address,undefined' -fno-omit-frame-pointer -fno-sanitize-recover=undefined) ;; - --tsan) - flags+=(-fsanitize=thread) - export TSAN_OPTIONS=${TSAN_OPTIONS:-halt_on_error=1} - ;; - *) echo "usage: $0 [--asan | --tsan]" >&2; exit 2 ;; -esac -bin=$out/texture_rgba_renderer_plugin_test -"${CXX:-clang++}" "${flags[@]}" -I"$here/shim" -I"$wrapper/include" -I"$engine" \ - "$here/texture_rgba_renderer_plugin_test.cc" \ - "$here/../texture_rgba.cpp" "$here/../texture_rgba_renderer_plugin.cpp" \ - "$here/../texture_rgba_renderer_plugin_c_api.cpp" \ - "$wrapper/core_implementations.cc" "$wrapper/plugin_registrar.cc" "$wrapper/standard_codec.cc" \ - -o "$bin" -count=0 -failed=0 -for test in $("$bin" --list); do - count=$((count + 1)) - if "$bin" "$test"; then - echo "PASS $test" - else - echo "FAIL $test" - failed=$((failed + 1)) - fi -done -echo "$count tests, $((count - failed)) passed, $failed failed" -[ "$failed" -eq 0 ] diff --git a/windows/test/shim/VersionHelpers.h b/windows/test/shim/VersionHelpers.h deleted file mode 100644 index 6eaf25f..0000000 --- a/windows/test/shim/VersionHelpers.h +++ /dev/null @@ -1,3 +0,0 @@ -// Kitedesk fork, 2026: test-only stand-in for (windows/test). -// The plugin includes it but uses nothing from it. Licensed under the Apache -// License, Version 2.0, like the rest of this package. diff --git a/windows/test/shim/flutter_windows.h b/windows/test/shim/flutter_windows.h deleted file mode 100644 index 1a05fd8..0000000 --- a/windows/test/shim/flutter_windows.h +++ /dev/null @@ -1,63 +0,0 @@ -// Kitedesk fork, 2026: test-only stand-in for flutter_windows.h of the Flutter -// Windows engine (a18df97c) with the declarations that the client wrapper's -// plugin_registrar_windows.h and flutter_view.h use; windows/test defines them. -// Licensed under the Apache License, Version 2.0, like the rest of this -// package. - -#ifndef KD_TEST_SHIM_FLUTTER_WINDOWS_H_ -#define KD_TEST_SHIM_FLUTTER_WINDOWS_H_ - -#include -#include -#include - -#include "flutter_export.h" -#include "flutter_messenger.h" -#include "flutter_plugin_registrar.h" - -#if defined(__cplusplus) -extern "C" { -#endif - -struct IDXGIAdapter; - -struct FlutterDesktopView; -typedef struct FlutterDesktopView* FlutterDesktopViewRef; - -typedef int64_t FlutterDesktopViewId; - -typedef bool (*FlutterDesktopWindowProcCallback)(HWND /* hwnd */, - UINT /* uMsg */, - WPARAM /*wParam*/, - LPARAM /* lParam*/, - void* /* user data */, - LRESULT* result); - -FLUTTER_EXPORT HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view); - -FLUTTER_EXPORT struct IDXGIAdapter* FlutterDesktopViewGetGraphicsAdapter( - FlutterDesktopViewRef view); - -FLUTTER_EXPORT FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetView( - FlutterDesktopPluginRegistrarRef registrar); - -FLUTTER_EXPORT FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetViewById( - FlutterDesktopPluginRegistrarRef registrar, - FlutterDesktopViewId view_id); - -FLUTTER_EXPORT void -FlutterDesktopPluginRegistrarRegisterTopLevelWindowProcDelegate( - FlutterDesktopPluginRegistrarRef registrar, - FlutterDesktopWindowProcCallback delegate, - void* user_data); - -FLUTTER_EXPORT void -FlutterDesktopPluginRegistrarUnregisterTopLevelWindowProcDelegate( - FlutterDesktopPluginRegistrarRef registrar, - FlutterDesktopWindowProcCallback delegate); - -#if defined(__cplusplus) -} // extern "C" -#endif - -#endif // KD_TEST_SHIM_FLUTTER_WINDOWS_H_ diff --git a/windows/test/shim/windows.h b/windows/test/shim/windows.h deleted file mode 100644 index 3b89c24..0000000 --- a/windows/test/shim/windows.h +++ /dev/null @@ -1,17 +0,0 @@ -// Kitedesk fork, 2026: test-only stand-in for , so that the plugin -// and the engine's C++ client wrapper build on Linux for windows/test. It has -// only the types those headers use. Licensed under the Apache License, -// Version 2.0, like the rest of this package. - -#ifndef KD_TEST_SHIM_WINDOWS_H_ -#define KD_TEST_SHIM_WINDOWS_H_ - -#include - -typedef struct HWND__* HWND; -typedef unsigned int UINT; -typedef uintptr_t WPARAM; -typedef intptr_t LPARAM; -typedef intptr_t LRESULT; - -#endif // KD_TEST_SHIM_WINDOWS_H_ diff --git a/windows/test/texture_rgba_renderer_plugin_test.cc b/windows/test/texture_rgba_renderer_plugin_test.cc deleted file mode 100644 index e190ef3..0000000 --- a/windows/test/texture_rgba_renderer_plugin_test.cc +++ /dev/null @@ -1,1002 +0,0 @@ -// Kitedesk fork, 2026: lifetime tests for the Windows texture of this plugin -// (Kitedesk backlog B252). Licensed under the Apache License, Version 2.0, -// like the rest of this package. -// -// windows/test/run_tests.sh builds this program on Linux together with the -// plugin sources and the C++ client wrapper of the Flutter Windows engine -// (core_implementations.cc, plugin_registrar.cc, standard_codec.cc); shim/ -// stands in for and flutter_windows.h. The C API of -// flutter_windows.dll is the fake engine below. It follows -// FlutterWindowsTextureRegistrar and FlutterWindowsEngine::Stop of engine -// a18df97c (Flutter 3.24.5): unregistering a texture only queues a task for -// the raster thread, which removes the texture from the engine and then runs -// the callback; until that task has run, the raster thread may still call the -// texture's pixel buffer callback and read the returned buffer (glTexImage2D). -// When a window closes, the registrar is destroyed (and the plugin with it) -// while the engine still runs. The Rust video thread is -// FlutterRgbaRendererPluginOnRgba with the pointer from getTexturePtr. Frame -// buffers the plugin allocates are counted. -// -// Usage: texture_rgba_renderer_plugin_test [--list | ] - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#include "../include/texture_rgba_renderer/texture_rgba_renderer_plugin_c_api.h" - -#if defined(__has_feature) -#if __has_feature(address_sanitizer) || __has_feature(thread_sanitizer) -#define KD_SANITIZER 1 -#endif -#endif -#if defined(__SANITIZE_ADDRESS__) || defined(__SANITIZE_THREAD__) -#define KD_SANITIZER 1 -#endif - -#ifdef KD_SANITIZER -#include -#endif - -namespace { - -constexpr int kWidth = 321; -constexpr int kHeight = 123; -constexpr size_t kFrameBytes = static_cast(kWidth) * kHeight * 4; - -// Heap accounting: live frame buffers (blocks of exactly kFrameBytes; nothing -// else here allocates that size), live bytes, and whether watched blocks were -// freed. The sanitizer runtimes report every allocation through their hooks; -// otherwise operators new and delete are replaced below. -std::atomic g_live_frames{0}; -std::atomic g_live_bytes{0}; -constexpr int kWatchSlots = 64; -std::atomic g_watched[kWatchSlots]; -std::atomic g_watched_freed[kWatchSlots]; - -void on_allocate(size_t size) { - if (size == kFrameBytes) { - g_live_frames++; - } - g_live_bytes += static_cast(size); -} - -void on_free(const void* ptr, size_t size) { - if (size == kFrameBytes) { - g_live_frames--; - } - g_live_bytes -= static_cast(size); - for (int i = 0; i < kWatchSlots; i++) { - if (g_watched[i].load() == ptr) { - g_watched_freed[i] = true; - } - } -} - -#ifdef KD_SANITIZER - -void malloc_hook(const volatile void* ptr, size_t size) { - on_allocate(size); -} - -void free_hook(const volatile void* ptr) { - on_free(const_cast(ptr), __sanitizer_get_allocated_size(ptr)); -} - -void install_heap_accounting() { - __sanitizer_install_malloc_and_free_hooks(malloc_hook, free_hook); -} - -} // namespace - -#else // KD_SANITIZER - -constexpr size_t kHeader = 16; - -void* allocate(size_t size) noexcept { - char* p = static_cast(std::malloc(size + kHeader)); - if (p == nullptr) { - return nullptr; - } - std::memcpy(p, &size, sizeof(size)); - on_allocate(size); - return p + kHeader; -} - -void deallocate(void* ptr) noexcept { - if (ptr == nullptr) { - return; - } - char* p = static_cast(ptr) - kHeader; - size_t size; - std::memcpy(&size, p, sizeof(size)); - on_free(ptr, size); - std::free(p); -} - -void install_heap_accounting() {} - -} // namespace - -void* operator new(size_t size) { - void* p = allocate(size); - if (p == nullptr) { - throw std::bad_alloc(); - } - return p; -} -void* operator new[](size_t size) { return operator new(size); } -void* operator new(size_t size, const std::nothrow_t&) noexcept { - return allocate(size); -} -void* operator new[](size_t size, const std::nothrow_t&) noexcept { - return allocate(size); -} -void operator delete(void* p) noexcept { deallocate(p); } -void operator delete[](void* p) noexcept { deallocate(p); } -void operator delete(void* p, size_t) noexcept { deallocate(p); } -void operator delete[](void* p, size_t) noexcept { deallocate(p); } -void operator delete(void* p, const std::nothrow_t&) noexcept { deallocate(p); } -void operator delete[](void* p, const std::nothrow_t&) noexcept { - deallocate(p); -} - -#endif // KD_SANITIZER - -// --------------------------------------------------------------------------- -// Fake engine: the C API of flutter_windows.dll that the plugin and the client -// wrapper call. - -constexpr int64_t kMaxTextureIds = 1024; - -struct FlutterDesktopMessenger { - std::mutex lock; - std::map> - handlers; -}; - -struct _FlutterPlatformMessageResponseHandle { - bool replied = false; - std::vector reply; -}; - -struct FlutterDesktopTextureRegistrar { - std::mutex mutex; - int64_t next_id = 1; - // Engine outcomes: normal queued erase, inline completed erase, or stopped - // engine whose PostRasterThreadTask fails and invokes completion inline. - enum class Completion { deferred, immediate, post_failure }; - Completion completion = Completion::deferred; - // The engine's textures: texture id -> what PopulateTexture calls. - std::map textures; - // Tasks posted to the raster thread (PostRasterThreadTask). - std::deque> raster_tasks; - std::array unregisters{}; - std::array marks{}; -}; - -struct FlutterDesktopPluginRegistrar { - FlutterDesktopMessenger messenger; - FlutterDesktopTextureRegistrar textures; - FlutterDesktopOnPluginRegistrarDestroyed on_destroyed = nullptr; -}; - -extern "C" { - -bool FlutterDesktopMessengerSend(FlutterDesktopMessengerRef messenger, - const char* channel, - const uint8_t* message, - const size_t message_size) { - return false; -} - -bool FlutterDesktopMessengerSendWithReply(FlutterDesktopMessengerRef messenger, - const char* channel, - const uint8_t* message, - const size_t message_size, - const FlutterDesktopBinaryReply reply, - void* user_data) { - return false; -} - -void FlutterDesktopMessengerSendResponse( - FlutterDesktopMessengerRef messenger, - const FlutterDesktopMessageResponseHandle* handle, - const uint8_t* data, - size_t data_length) { - auto* h = const_cast(handle); - h->replied = true; - h->reply.assign(data, data + data_length); -} - -void FlutterDesktopMessengerSetCallback(FlutterDesktopMessengerRef messenger, - const char* channel, - FlutterDesktopMessageCallback callback, - void* user_data) { - if (callback == nullptr) { - messenger->handlers.erase(channel); - } else { - messenger->handlers[channel] = {callback, user_data}; - } -} - -FlutterDesktopMessengerRef FlutterDesktopMessengerAddRef( - FlutterDesktopMessengerRef messenger) { - return messenger; -} - -void FlutterDesktopMessengerRelease(FlutterDesktopMessengerRef messenger) {} - -bool FlutterDesktopMessengerIsAvailable(FlutterDesktopMessengerRef messenger) { - return true; -} - -FlutterDesktopMessengerRef FlutterDesktopMessengerLock( - FlutterDesktopMessengerRef messenger) { - messenger->lock.lock(); - return messenger; -} - -void FlutterDesktopMessengerUnlock(FlutterDesktopMessengerRef messenger) { - messenger->lock.unlock(); -} - -int64_t FlutterDesktopTextureRegistrarRegisterExternalTexture( - FlutterDesktopTextureRegistrarRef registrar, - const FlutterDesktopTextureInfo* info) { - if (info->type != kFlutterDesktopPixelBufferTexture || - info->pixel_buffer_config.callback == nullptr) { - return -1; - } - std::lock_guard lock(registrar->mutex); - int64_t id = registrar->next_id++; - if (id >= kMaxTextureIds) { - std::abort(); - } - registrar->textures[id] = info->pixel_buffer_config; - return id; -} - -// FlutterWindowsTextureRegistrar::UnregisterTexture: the engine drops the -// texture on the raster thread, then calls back. -void FlutterDesktopTextureRegistrarUnregisterExternalTexture( - FlutterDesktopTextureRegistrarRef registrar, - int64_t texture_id, - void (*callback)(void* user_data), - void* user_data) { - std::unique_lock lock(registrar->mutex); - if (texture_id > 0 && texture_id < kMaxTextureIds) { - registrar->unregisters[texture_id]++; - } - if (registrar->completion != FlutterDesktopTextureRegistrar::Completion::deferred) { - if (registrar->completion == FlutterDesktopTextureRegistrar::Completion::immediate) { - registrar->textures.erase(texture_id); - } - // Match exact engine a18df97: !posted invokes callback without erasing the - // registrar's external texture. A stopped raster thread never draws it. - // Engine completion is outside map_mutex_, also on post failure. Calling - // it under our fake registry lock would invent an ABBA lock order. - lock.unlock(); - if (callback != nullptr) callback(user_data); - return; - } - registrar->raster_tasks.push_back( - [registrar, texture_id, callback, user_data]() { - { - std::lock_guard lock(registrar->mutex); - registrar->textures.erase(texture_id); - } - if (callback != nullptr) { - callback(user_data); - } - }); -} - -bool FlutterDesktopTextureRegistrarMarkExternalTextureFrameAvailable( - FlutterDesktopTextureRegistrarRef registrar, - int64_t texture_id) { - std::lock_guard lock(registrar->mutex); - if (texture_id > 0 && texture_id < kMaxTextureIds) { - registrar->marks[texture_id]++; - } - return true; -} - -FlutterDesktopMessengerRef FlutterDesktopPluginRegistrarGetMessenger( - FlutterDesktopPluginRegistrarRef registrar) { - return ®istrar->messenger; -} - -FlutterDesktopTextureRegistrarRef FlutterDesktopRegistrarGetTextureRegistrar( - FlutterDesktopPluginRegistrarRef registrar) { - return ®istrar->textures; -} - -void FlutterDesktopPluginRegistrarSetDestructionHandler( - FlutterDesktopPluginRegistrarRef registrar, - FlutterDesktopOnPluginRegistrarDestroyed callback) { - registrar->on_destroyed = callback; -} - -HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view) { - return nullptr; -} - -struct IDXGIAdapter* FlutterDesktopViewGetGraphicsAdapter( - FlutterDesktopViewRef view) { - return nullptr; -} - -FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetView( - FlutterDesktopPluginRegistrarRef registrar) { - return nullptr; -} - -FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetViewById( - FlutterDesktopPluginRegistrarRef registrar, - FlutterDesktopViewId view_id) { - return nullptr; -} - -void FlutterDesktopPluginRegistrarRegisterTopLevelWindowProcDelegate( - FlutterDesktopPluginRegistrarRef registrar, - FlutterDesktopWindowProcCallback delegate, - void* user_data) {} - -void FlutterDesktopPluginRegistrarUnregisterTopLevelWindowProcDelegate( - FlutterDesktopPluginRegistrarRef registrar, - FlutterDesktopWindowProcCallback delegate) {} - -} // extern "C" - -namespace { - -// --------------------------------------------------------------------------- -// One engine (= one Flutter window) with the plugin registered on it. - -struct Engine { - FlutterDesktopPluginRegistrar* registrar; -}; - -Engine* engine_new() { - Engine* e = new Engine{new FlutterDesktopPluginRegistrar()}; - TextureRgbaRendererPluginCApiRegisterWithRegistrar(e->registrar); - return e; -} - -FlutterDesktopTextureRegistrar& textures(Engine* e) { - return e->registrar->textures; -} - -// Runs the tasks queued for the raster thread (unregistrations). -void run_raster_tasks(Engine* e) { - for (;;) { - std::function task; - { - std::lock_guard lock(textures(e).mutex); - if (textures(e).raster_tasks.empty()) { - return; - } - task = std::move(textures(e).raster_tasks.front()); - textures(e).raster_tasks.pop_front(); - } - task(); - } -} - -// The window closes. As in FlutterWindowsEngine::Stop, the registrar's -// destruction handler runs first, which destroys the plugin, while the -// engine and its raster thread keep running. -void engine_destroy_plugins(Engine* e) { - e->registrar->on_destroyed(e->registrar); -} - -// Then the engine shuts down: its raster thread runs what was queued and the -// engine's memory is freed. -void engine_free(Engine* e) { - run_raster_tasks(e); - delete e->registrar; - delete e; -} - -void engine_shutdown(Engine* e) { - engine_destroy_plugins(e); - engine_free(e); -} - -constexpr int64_t kNoReply = INT64_MIN; - -// Calls a plugin method like Dart does. Returns the int or bool result, or -// kNoReply when the plugin did not answer. -int64_t invoke(Engine* e, const char* method, flutter::EncodableMap args) { - const auto& codec = flutter::StandardMethodCodec::GetInstance(); - flutter::MethodCall call( - method, std::make_unique(std::move(args))); - std::unique_ptr> message = codec.EncodeMethodCall(call); - _FlutterPlatformMessageResponseHandle handle; - FlutterDesktopMessage m = {sizeof(FlutterDesktopMessage), - "texture_rgba_renderer", message->data(), - message->size(), &handle}; - FlutterDesktopMessenger& messenger = e->registrar->messenger; - auto handler = messenger.handlers.at("texture_rgba_renderer"); - handler.first(&messenger, &m, handler.second); - if (!handle.replied) { - return kNoReply; - } - int64_t out = kNoReply; - flutter::MethodResultFunctions result( - [&out](const flutter::EncodableValue* value) { - if (value == nullptr) { - return; - } - if (auto v = std::get_if(value)) { - out = *v; - } else if (auto v64 = std::get_if(value)) { - out = *v64; - } else if (auto b = std::get_if(value)) { - out = *b ? 1 : 0; - } - }, - nullptr, nullptr); - codec.DecodeAndProcessResponseEnvelope(handle.reply.data(), - handle.reply.size(), &result); - return out; -} - -flutter::EncodableMap key_args(int key) { - return {{flutter::EncodableValue("key"), flutter::EncodableValue(key)}}; -} - -int64_t create_texture(Engine* e, int key) { - return invoke(e, "createTexture", key_args(key)); -} - -int64_t texture_ptr(Engine* e, int key) { - return invoke(e, "getTexturePtr", key_args(key)); -} - -// recycle < 0: no "recycle" argument (callers before the Kitedesk change). -int64_t close_texture(Engine* e, int key, int recycle) { - flutter::EncodableMap args = key_args(key); - if (recycle >= 0) { - args[flutter::EncodableValue("recycle")] = - flutter::EncodableValue(recycle != 0); - } - return invoke(e, "closeTexture", std::move(args)); -} - -// Decoded frames, filled before any test runs (frame i is all bytes i). -constexpr int kFills = 16; -uint8_t g_frames[kFills][kFrameBytes]; - -// A decoded frame from the Rust video thread. -void send_frame(int64_t ptr, uint8_t fill) { - FlutterRgbaRendererPluginOnRgba(reinterpret_cast(ptr), - g_frames[fill % kFills], - static_cast(kFrameBytes), kWidth, - kHeight, 0); -} - -volatile uint8_t g_sink; - -struct Pulled { - bool ok; - uint8_t first; - size_t width; - size_t height; -}; - -// What the raster thread does for a texture on a frame -// (ExternalTexturePixelBuffer::CopyPixelBuffer): ask for the pixel buffer and -// upload it, which reads all of it. -Pulled populate(const FlutterDesktopPixelBufferTextureConfig& config) { - Pulled p = {false, 0, 0, 0}; - const FlutterDesktopPixelBuffer* buffer = - config.callback(1920, 1080, config.user_data); - if (buffer == nullptr || buffer->buffer == nullptr) { - return p; - } - uint8_t x = 0; - for (size_t i = 0; i < buffer->width * buffer->height * 4; i++) { - x ^= buffer->buffer[i]; - } - g_sink = x; - p = {true, buffer->buffer[0], buffer->width, buffer->height}; - if (buffer->release_callback != nullptr) { - buffer->release_callback(buffer->release_context); - } - return p; -} - -Pulled pull(Engine* e, int64_t id) { - FlutterDesktopPixelBufferTextureConfig config; - { - std::lock_guard lock(textures(e).mutex); - auto it = textures(e).textures.find(id); - if (it == textures(e).textures.end()) { - return {false, 0, 0, 0}; - } - config = it->second; - } - return populate(config); -} - -// One raster frame: every texture the engine still has is drawn. -void pull_all(Engine* e) { - std::vector configs; - { - std::lock_guard lock(textures(e).mutex); - for (const auto& entry : textures(e).textures) { - configs.push_back(entry.second); - } - } - for (const auto& config : configs) { - populate(config); - } -} - -bool registered(Engine* e, int64_t id) { - std::lock_guard lock(textures(e).mutex); - return textures(e).textures.count(id) != 0; -} - -int unregisters(Engine* e, int64_t id) { - std::lock_guard lock(textures(e).mutex); - return id > 0 && id < kMaxTextureIds ? textures(e).unregisters[id] : -1; -} - -int marks(Engine* e, int64_t id) { - std::lock_guard lock(textures(e).mutex); - return id > 0 && id < kMaxTextureIds ? textures(e).marks[id] : -1; -} - -int live_frames() { - return g_live_frames.load(); -} - -// Watches the heap block at ptr; freed(slot) tells whether it was freed since. -int watch(int64_t ptr) { - for (int i = 0; i < kWatchSlots; i++) { - if (g_watched[i].load() == nullptr) { - g_watched_freed[i] = false; - g_watched[i] = reinterpret_cast(ptr); - return i; - } - } - std::abort(); -} - -bool freed(int slot) { - return g_watched_freed[slot].load(); -} - -// The Rust session's VideoRenderer (src/flutter.rs): frames go to the -// registered pointer under a read lock, and registerPixelbufferTexture(..., 0) -// clears it under the write lock. -struct Session { - std::shared_mutex lock; - int64_t ptr = 0; - - void set(int64_t p) { - std::unique_lock l(lock); - ptr = p; - } - - void push(uint8_t fill) { - std::shared_lock l(lock); - if (ptr != 0) { - send_frame(ptr, fill); - } - } -}; - -// --------------------------------------------------------------------------- - -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) - -// N-01, raster thread: closeTexture only queues the engine's unregistration, -// and the engine may draw the texture once more before it is done. The texture -// must stay valid and hand out no frame until the engine calls back; then its -// frame buffers are freed. With "recycle" (nothing pushes to the pointer any -// more) it is destroyed, without it, it is kept. -void test_close_keeps_texture_until_engine_unregisters() { - for (int recycle = 1; recycle >= 0; recycle--) { - const int base = live_frames(); - Engine* e = engine_new(); - int64_t id = create_texture(e, 10); - int64_t ptr = texture_ptr(e, 10); - CHECK(id > 0 && ptr != 0 && ptr != -1 && registered(e, id)); - send_frame(ptr, 1); - Pulled shown = pull(e, id); - CHECK(shown.ok && shown.first == 1 && shown.width == kWidth && - shown.height == kHeight); - send_frame(ptr, 2); - CHECK(live_frames() == base + 2); - const int slot = watch(ptr); - CHECK(close_texture(e, 10, recycle) == 1); - CHECK(registered(e, id)); - Pulled late = pull(e, id); - CHECK(!late.ok); - CHECK(!freed(slot)); - run_raster_tasks(e); - CHECK(!registered(e, id)); - CHECK(live_frames() == base); - CHECK(freed(slot) == (recycle == 1)); - engine_shutdown(e); - } -} - -// N-01, video thread: without "recycle" the session keeps the pointer (a tab -// moving to another window), so frames keep arriving after closeTexture. They -// are dropped without telling the engine, before and after it unregistered the -// texture, and after the engine itself is gone. The closed texture's address -// is never handed out again (ABA: a late frame would show in another session). -void test_late_frames_after_close_are_dropped() { - for (int recycle = 0; recycle >= -1; recycle--) { - const int base = live_frames(); - Engine* e = engine_new(); - int64_t a = create_texture(e, 20); - int64_t pa = texture_ptr(e, 20); - CHECK(a > 0 && pa != 0 && pa != -1); - send_frame(pa, 3); - CHECK(pull(e, a).ok); - CHECK(close_texture(e, 20, recycle) == 1); - const int announced = marks(e, a); - send_frame(pa, 4); - CHECK(marks(e, a) == announced); - run_raster_tasks(e); - CHECK(live_frames() == base); - send_frame(pa, 5); - CHECK(marks(e, a) == announced && live_frames() == base); - std::set ids; - for (int key = 21; key < 53; key++) { - int64_t id = create_texture(e, key); - CHECK(id > 0 && texture_ptr(e, key) != pa); - ids.insert(id); - } - send_frame(pa, 6); - for (int64_t id : ids) { - CHECK(marks(e, id) == 0); - } - engine_shutdown(e); - send_frame(pa, 7); - CHECK(live_frames() == base); - } -} - -// A key that is in use gets -1. Without an answer the Dart future would never -// complete. -void test_duplicate_key_gets_minus_one() { - Engine* e = engine_new(); - int64_t a = create_texture(e, 30); - int64_t pa = texture_ptr(e, 30); - CHECK(a > 0); - CHECK(create_texture(e, 30) == -1); - CHECK(texture_ptr(e, 30) == pa); - CHECK(close_texture(e, 30, 1) == 1); - run_raster_tasks(e); - engine_shutdown(e); -} - -// The texture is unregistered once: by closeTexture, or when the window goes -// away while it is open, not again when the object is destroyed. -void test_texture_is_unregistered_once() { - Engine* e = engine_new(); - int64_t a = create_texture(e, 40); - CHECK(close_texture(e, 40, 1) == 1); - int64_t b = create_texture(e, 41); - CHECK(close_texture(e, 41, 0) == 1); - int64_t c = create_texture(e, 42); - run_raster_tasks(e); - CHECK(unregisters(e, a) == 1 && unregisters(e, b) == 1); - engine_destroy_plugins(e); - run_raster_tasks(e); - CHECK(unregisters(e, c) == 1 && !registered(e, c)); - engine_free(e); -} - -// When the window closes, the textures it still has are unregistered but not -// destroyed: the sessions may still push frames to them, and the raster -// thread may draw them until the engine has dropped them. -void test_engine_shutdown_retires_open_textures() { - const int base = live_frames(); - Engine* e = engine_new(); - int64_t a = create_texture(e, 50); - int64_t pa = texture_ptr(e, 50); - CHECK(a > 0 && pa != 0 && pa != -1); - send_frame(pa, 8); - CHECK(pull(e, a).ok); - send_frame(pa, 9); - engine_destroy_plugins(e); - send_frame(pa, 10); - CHECK(registered(e, a)); - CHECK(!pull(e, a).ok); - engine_free(e); - send_frame(pa, 11); - CHECK(live_frames() == base); -} - -// A texture closed without "recycle" is kept for the rest of the process. It -// keeps no frame buffer once the engine has unregistered it; what it keeps is -// measured here (the retired bound). -void test_retired_textures_keep_no_frame_buffers() { - Engine* e = engine_new(); - auto cycle = [e](int key) { - int64_t id = create_texture(e, key); - int64_t ptr = texture_ptr(e, key); - if (id <= 0 || ptr == 0 || ptr == -1) { - return false; - } - send_frame(ptr, 12); - pull(e, id); - send_frame(ptr, 13); - const bool closed = close_texture(e, key, 0) == 1; - run_raster_tasks(e); - return closed; - }; - CHECK(cycle(60)); - const int base_frames = live_frames(); - const long long base_bytes = g_live_bytes.load(); - constexpr int kRetired = 16; - for (int i = 0; i < kRetired; i++) { - CHECK(cycle(61 + i)); - } - CHECK(live_frames() == base_frames); - const long long per_texture = (g_live_bytes.load() - base_bytes) / kRetired; - std::printf(" retired texture keeps %lld bytes of heap (%d textures)\n", - per_texture, kRetired); - CHECK(per_texture < 1024); - engine_shutdown(e); -} - -// getTexturePtr of a key that is not open (closed meanwhile) answers 0, which -// the session treats as no texture, like on Linux and macOS. -void test_texture_ptr_of_closed_key_is_null() { - Engine* e = engine_new(); - CHECK(texture_ptr(e, 80) == 0); - CHECK(create_texture(e, 81) > 0); - CHECK(close_texture(e, 81, 0) == 1); - CHECK(texture_ptr(e, 81) == 0); - run_raster_tasks(e); - engine_shutdown(e); -} - -// N-01 with threads: the video thread pushes frames and the raster thread draws -// every texture and runs the engine's tasks while the platform thread closes -// textures. With release_first the session first clears its pointer (like -// destroy(true) in Dart) and closes with "recycle"; otherwise it keeps pushing -// to the closed texture. -void racing_close(bool release_first) { - const int base = live_frames(); - Engine* e = engine_new(); - std::atomic stop{false}; - std::thread raster([e, &stop]() { - while (!stop.load()) { - pull_all(e); - run_raster_tasks(e); - } - }); - constexpr int kTextures = 20; - int64_t closed[kTextures] = {}; - bool created = true; - for (int i = 0; i < kTextures && created; i++) { - const int key = 700 + i; - int64_t id = create_texture(e, key); - int64_t ptr = texture_ptr(e, key); - created = id > 0 && ptr != 0 && ptr != -1; - if (!created) { - break; - } - Session session; - session.set(ptr); - std::atomic done{false}; - std::thread video([&session, &done]() { - for (int n = 0; !done.load(); n++) { - session.push(static_cast(1 + n % 2)); - } - }); - std::this_thread::sleep_for(std::chrono::milliseconds(2)); - if (release_first) { - session.set(0); - } - closed[i] = close_texture(e, key, release_first ? 1 : 0); - std::this_thread::sleep_for(std::chrono::milliseconds(2)); - done = true; - video.join(); - } - stop = true; - raster.join(); - run_raster_tasks(e); - CHECK(created); - for (int i = 0; i < kTextures; i++) { - CHECK(closed[i] == 1); - } - CHECK(live_frames() == base); - engine_shutdown(e); -} - -void test_frames_racing_close_without_recycle() { - racing_close(false); -} - -void test_frames_racing_close_with_recycle() { - racing_close(true); -} - - -// The engine reads returned bytes after the PixelBufferTexture callback has -// returned. Pause exactly in that gap, close on the platform thread, and read -// the original bytes before the raster unregister task is allowed to complete. -// An eager buffer/object deletion breaks this with a real ASan UAF. -void test_held_raster_buffer_survives_close() { - for (int recycle : {0, 1}) { - const int base = live_frames(); - Engine* e = engine_new(); - const int64_t id = create_texture(e, 111); - send_frame(texture_ptr(e, 111), 11); - FlutterDesktopPixelBufferTextureConfig config; - { - std::lock_guard lock(textures(e).mutex); - config = textures(e).textures.at(id); - } - const FlutterDesktopPixelBuffer* held = config.callback(kWidth, kHeight, config.user_data); - CHECK(held && held->buffer && held->width == kWidth && held->height == kHeight); - const uint8_t* bytes = held->buffer; - const size_t length = held->width * held->height * 4; - CHECK(close_texture(e, 111, recycle) == 1); - // Actual retained consumer memory, not a new post-close callback or copied - // data. This read must remain safe until unregister completion. - for (size_t i = 0; i < length; ++i) CHECK(bytes[i] == 11); - CHECK(live_frames() == base + 1); - if (held->release_callback) held->release_callback(held->release_context); - run_raster_tasks(e); - CHECK(live_frames() == base); - CHECK(!registered(e, id)); - engine_shutdown(e); - } -} - -void test_inline_unregister_and_failed_raster_post() { - for (auto mode : {FlutterDesktopTextureRegistrar::Completion::immediate, - FlutterDesktopTextureRegistrar::Completion::post_failure}) { - for (int recycle : {0, 1}) { - const int base = live_frames(); - Engine* e = engine_new(); - const int64_t id = create_texture(e, 112); - const int64_t ptr = texture_ptr(e, 112); - send_frame(ptr, 12); - CHECK(pull(e, id).first == 12); - textures(e).completion = mode; - CHECK(close_texture(e, 112, recycle) == 1); - CHECK(unregisters(e, id) == 1); - CHECK(live_frames() == base); - CHECK(close_texture(e, 112, recycle) == 0); - CHECK(texture_ptr(e, 112) == 0); - if (recycle == 0) { - const int count = marks(e, id); - send_frame(ptr, 13); - CHECK(marks(e, id) == count && live_frames() == base); - } - // For post_failure the external texture wrapper can still exist, but - // raster is stopped: don't invent a draw after the completion promise. - CHECK(registered(e, id) == (mode == FlutterDesktopTextureRegistrar::Completion::post_failure)); - engine_shutdown(e); - if (recycle == 0) { send_frame(ptr, 14); CHECK(live_frames() == base); } - } - } -} - -void test_unregister_with_missing_engine_registration() { - for (int recycle : {0, 1}) { - const int base = live_frames(); - Engine* e = engine_new(); - const int64_t id = create_texture(e, 113); - const int64_t ptr = texture_ptr(e, 113); - send_frame(ptr, 1); - CHECK(pull(e, id).ok); - { std::lock_guard lock(textures(e).mutex); textures(e).textures.erase(id); } - CHECK(close_texture(e, 113, recycle) == 1); - CHECK(close_texture(e, 113, recycle) == 0); - run_raster_tasks(e); - CHECK(unregisters(e, id) == 1 && live_frames() == base); - CHECK(texture_ptr(e, 113) == 0); - if (!recycle) send_frame(ptr, 2); - engine_shutdown(e); - if (!recycle) send_frame(ptr, 3); - CHECK(live_frames() == base); - } -} - -struct Test { - const char* name; - void (*run)(); -}; - -const Test kTests[] = { - {"held_raster_buffer_survives_close", test_held_raster_buffer_survives_close}, - {"inline_unregister_and_failed_raster_post", test_inline_unregister_and_failed_raster_post}, - {"unregister_with_missing_engine_registration", test_unregister_with_missing_engine_registration}, - {"close_keeps_texture_until_engine_unregisters", - test_close_keeps_texture_until_engine_unregisters}, - {"late_frames_after_close_are_dropped", - test_late_frames_after_close_are_dropped}, - {"duplicate_key_gets_minus_one", test_duplicate_key_gets_minus_one}, - {"texture_is_unregistered_once", test_texture_is_unregistered_once}, - {"engine_shutdown_retires_open_textures", - test_engine_shutdown_retires_open_textures}, - {"retired_textures_keep_no_frame_buffers", - test_retired_textures_keep_no_frame_buffers}, - {"texture_ptr_of_closed_key_is_null", - test_texture_ptr_of_closed_key_is_null}, - {"frames_racing_close_without_recycle", - test_frames_racing_close_without_recycle}, - {"frames_racing_close_with_recycle", test_frames_racing_close_with_recycle}, -}; - -} // namespace - -int main(int argc, char** argv) { - install_heap_accounting(); - for (int i = 0; i < kFills; i++) { - std::memset(g_frames[i], i, kFrameBytes); - } - if (argc == 2 && std::strcmp(argv[1], "--list") == 0) { - for (const Test& t : kTests) { - std::printf("%s\n", t.name); - } - return 0; - } - int count = 0; - int failed = 0; - for (const Test& t : kTests) { - if (argc == 2 && std::strcmp(argv[1], t.name) != 0) { - continue; - } - const int before = g_failed_checks; - t.run(); - const bool ok = g_failed_checks == before; - if (argc != 2) { - std::printf("%s %s\n", ok ? "PASS" : "FAIL", t.name); - } - std::fflush(stdout); - failed += ok ? 0 : 1; - count++; - } - if (count == 0) { - std::fprintf(stderr, "unknown test %s\n", argv[1]); - return 2; - } - return failed == 0 ? 0 : 1; -} diff --git a/windows/texture_rgba.cpp b/windows/texture_rgba.cpp index c511dd7..91aaacc 100644 --- a/windows/texture_rgba.cpp +++ b/windows/texture_rgba.cpp @@ -1,6 +1,3 @@ -// Modified by the Kitedesk fork (2026, B252): no UnregisterTexture in the -// destructor; Terminate and FreeBuffers for closeTexture. - #include "texture_rgba.h" #include @@ -14,6 +11,11 @@ TextureRgba::TextureRgba(flutter::TextureRegistrar *texture_registrar) this->texture_id_ = texture_registrar->RegisterTexture(texture_.get()); } +TextureRgba::~TextureRgba() +{ + texture_registrar_->UnregisterTexture(texture_id_); +} + void TextureRgba::MarkVideoFrameAvailable( std::vector &buffer, size_t width, size_t height) { @@ -43,11 +45,6 @@ void TextureRgba::MarkVideoFrameAvailable2(const uint8_t* buffer, const size_t b return; } const std::lock_guard lock(mutex_); - if (terminated_) - { - // Closed while the session still pushes frames to this pointer. - return; - } int bg_index = fg_index_ ^ 1; buffer_tmp_[bg_index].assign(buffer, buffer + buffer_length); width_[bg_index] = width; @@ -64,11 +61,6 @@ void TextureRgba::MarkVideoFrameAvailable2(const uint8_t* buffer, const size_t b inline const FlutterDesktopPixelBuffer *TextureRgba::buffer() { const std::lock_guard lock(mutex_); - if (terminated_) - { - // Closed; the engine may draw the texture until it has unregistered it. - return nullptr; - } if (buff_ready_) { fg_index_ ^= 1; @@ -79,18 +71,3 @@ inline const FlutterDesktopPixelBuffer *TextureRgba::buffer() } return &this->flutter_pixel_buffer_; } - -void TextureRgba::Terminate() -{ - const std::lock_guard lock(mutex_); - terminated_ = true; -} - -void TextureRgba::FreeBuffers() -{ - const std::lock_guard lock(mutex_); - std::vector().swap(buffer_tmp_[0]); - std::vector().swap(buffer_tmp_[1]); - flutter_pixel_buffer_.buffer = nullptr; - buff_ready_ = false; -} diff --git a/windows/texture_rgba.h b/windows/texture_rgba.h index 04a9000..8273039 100644 --- a/windows/texture_rgba.h +++ b/windows/texture_rgba.h @@ -1,14 +1,7 @@ -// Modified by the Kitedesk fork (2026, B252): the texture no longer -// unregisters itself when destroyed (closeTexture does, and the plugin keeps -// the object alive until the engine is done with it); a closed texture drops -// pushed frames, hands the engine no buffer, and frees its frame buffers once -// the engine has unregistered it. - #ifndef TEXTURE_RGBA_H_ #define TEXTURE_RGBA_H_ #include -#include #include #include @@ -16,6 +9,7 @@ class TextureRgba { public: TextureRgba(flutter::TextureRegistrar* texture_registrar); + ~TextureRgba(); void MarkVideoFrameAvailable(std::vector& buffer, size_t width, size_t height); @@ -25,23 +19,12 @@ public: const FlutterDesktopPixelBuffer* buffer(); - // Closes the texture: frames pushed from now on are dropped without - // touching the texture registrar, and the engine gets no buffer any more. - void Terminate(); - - // Frees the frame buffers. Call after Terminate, once the engine has - // unregistered the texture and so no longer reads them. - void FreeBuffers(); - private: FlutterDesktopPixelBuffer flutter_pixel_buffer_{}; flutter::TextureRegistrar* texture_registrar_ = nullptr; std::unique_ptr texture_ = nullptr; int64_t texture_id_; std::mutex mutex_; - // Set and read with mutex_ held, so that no frame is copied or announced - // to the engine after Terminate returns. - std::atomic terminated_{false}; int fg_index_ = 0; bool buff_ready_ = false; size_t width_[2]; diff --git a/windows/texture_rgba_renderer_plugin.cpp b/windows/texture_rgba_renderer_plugin.cpp index b3b13fe..a5186bf 100644 --- a/windows/texture_rgba_renderer_plugin.cpp +++ b/windows/texture_rgba_renderer_plugin.cpp @@ -1,9 +1,3 @@ -// Modified by the Kitedesk fork (2026, B252): closeTexture no longer destroys -// the texture before the engine is done with it, and keeps it for good unless -// "recycle" is set; the textures of a closing engine are kept the same way; -// createTexture of a key in use answers -1; getTexturePtr of a key that is not -// open answers 0. - #include "texture_rgba_renderer_plugin.h" // This must be included before many other Windows headers. @@ -17,32 +11,10 @@ #include #include -#include #include -#include namespace texture_rgba_renderer { -namespace { - -// Textures closed while frames may still be pushed to the 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 destroyed, so such a push finds a closed texture and not freed memory, -// and their address is never handed out again for another texture (ABA). Their -// lifetime is the process; each one keeps the object, not its frame buffers. -struct Retired { - std::mutex mutex; - std::vector> textures; -}; - -Retired& retired() { - static Retired* instance = new Retired(); - return *instance; -} - -} // namespace - // static void TextureRgbaRendererPlugin::RegisterWithRegistrar( flutter::PluginRegistrarWindows *registrar) { @@ -66,28 +38,7 @@ TextureRgbaRendererPlugin::TextureRgbaRendererPlugin() { this->texture_registrar = nullptr; } -TextureRgbaRendererPlugin::~TextureRgbaRendererPlugin() { - // The engine is going away (its texture registrar outlives the plugins), and - // sessions may still push frames to these textures. - for (auto &entry : textures_) { - CloseTexture(std::move(entry.second), false); - } -} - -void TextureRgbaRendererPlugin::CloseTexture( - std::unique_ptr texture, bool recycle) { - std::shared_ptr closed = std::move(texture); - closed->Terminate(); - if (!recycle) { - std::lock_guard lock(retired().mutex); - retired().textures.push_back(closed); - } - // The engine removes the texture on its raster thread, which may draw it - // until then, and calls back there. Only then are the frame buffers freed, - // and a recycled texture is destroyed with the last reference. - this->texture_registrar->UnregisterTexture(closed->texture_id(), - [closed] { closed->FreeBuffers(); }); -} +TextureRgbaRendererPlugin::~TextureRgbaRendererPlugin() {} void TextureRgbaRendererPlugin::HandleMethodCall( const flutter::MethodCall &method_call, @@ -101,27 +52,16 @@ void TextureRgbaRendererPlugin::HandleMethodCall( it->second = std::make_unique(this->texture_registrar); result->Success(flutter::EncodableValue(it->second->texture_id())); } - else { - // The key is in use. Without an answer the Dart future never completes. - result->Success(flutter::EncodableValue(int64_t(-1))); - } } else if (method_call.method_name().compare("closeTexture") == 0) { auto args = std::get(*method_call.arguments()); auto key = std::get(args.at(flutter::EncodableValue("key"))); - auto it = textures_.find(key); - if (it == textures_.end()) { + if (textures_.find(key) == textures_.end()) { result->Success(flutter::EncodableValue(false)); } else { - // "recycle": true promises that nothing pushes frames to the pointer - // from getTexturePtr any more, so the texture may be destroyed. - auto recycle = args.find(flutter::EncodableValue("recycle")); - const bool* recycle_value = recycle == args.end() - ? nullptr : std::get_if(&recycle->second); - CloseTexture(std::move(it->second), - recycle_value != nullptr && *recycle_value); - textures_.erase(it); + this->texture_registrar->UnregisterTexture(textures_[key]->texture_id()); + textures_.erase(key); result->Success(flutter::EncodableValue(true)); } } @@ -143,8 +83,7 @@ void TextureRgbaRendererPlugin::HandleMethodCall( auto args = std::get(*method_call.arguments()); auto key = std::get(args.at(flutter::EncodableValue("key"))); if (textures_.find(key) == textures_.end()) { - // Not open (e.g. closed meanwhile): no pointer for the session. - result->Success(flutter::EncodableValue(0)); + result->Success(flutter::EncodableValue(-1)); } else { // Return an address. diff --git a/windows/texture_rgba_renderer_plugin.h b/windows/texture_rgba_renderer_plugin.h index 0cb67bf..a87c3b0 100644 --- a/windows/texture_rgba_renderer_plugin.h +++ b/windows/texture_rgba_renderer_plugin.h @@ -1,7 +1,3 @@ -// Modified by the Kitedesk fork (2026, B252): closeTexture keeps the texture -// alive until the engine has unregistered it, and for good unless "recycle" -// says that nothing pushes frames to its pointer any more. - #ifndef FLUTTER_PLUGIN_TEXTURE_RGBA_RENDERER_PLUGIN_H_ #define FLUTTER_PLUGIN_TEXTURE_RGBA_RENDERER_PLUGIN_H_ @@ -35,8 +31,6 @@ class TextureRgbaRendererPlugin : public flutter::Plugin { void HandleMethodCall( const flutter::MethodCall &method_call, std::unique_ptr> result); - - void CloseTexture(std::unique_ptr texture, bool recycle); }; } // namespace texture_rgba_renderer