From 9a377b61551a02e19f68059f9e8592cbd1adfbdc Mon Sep 17 00:00:00 2001 From: yunchan Date: Sun, 15 Nov 2026 09:56:22 +0900 Subject: [PATCH 1/7] macos: add headless texture tests (Kitedesk T-4204) Standalone Objective-C tests for the macOS texture, built against FlutterMacOS.framework of the active Flutter SDK by macos/test/run_tests.sh (optionally with ASan or TSan). The plugin sources are compiled into the test; the plugin registers through a fake plugin registrar and is driven through handleMethodCall:result:. The fake engine follows FlutterTextureRegistrar.mm and FlutterExternalTexture.mm of engine a18df97c: the texture id is the texture's address, the registrar keeps registered textures, and copyPixelBuffer is wrapped into a Metal texture whose CVMetalTexture and pixel buffer are released at once while the Metal texture stays drawn. Frames come through FlutterRgbaRendererPluginOnRgba on a thread without an autorelease pool. Memory is the task's phys_footprint, which counts IOSurfaces, read after the engine's CVMetalTextureCache has let go of what it wrapped (it keeps that until it makes a texture a second later, measured); it is not checked under ASan, whose shadow of each new IOSurface mapping counts in it. No app, window or screen capture. On 50efc18e ten of the thirteen tests fail (Mac mini M4, macOS 26.5.2): every frame gets a new IOSurface (60 for 60 frames), the raster thread waits for the copy (median 0.9 to 1.1 ms in three runs, up to 537 of 600 calls over 1 ms, with 4K frames), frames are announced off the main thread, closed textures keep their last frame (634 MB more after 20 4K textures) and still take and announce late frames, a texture closed with recycle is never freed, and rows of another stride than the pixel buffer's are copied in one piece (shifted rows for 1366, 1001, 1279 and 3838 pixels wide with stride_align 1, and 1001 with 256), short frames included. The two tests on frames the engine was handed pass, as no buffer is ever written twice yet. Co-Authored-By: Claude Opus 5.5 --- macos/test/run_tests.sh | 65 ++ .../test/texture_rgba_renderer_plugin_test.m | 750 ++++++++++++++++++ 2 files changed, 815 insertions(+) create mode 100755 macos/test/run_tests.sh create mode 100644 macos/test/texture_rgba_renderer_plugin_test.m diff --git a/macos/test/run_tests.sh b/macos/test/run_tests.sh new file mode 100755 index 0000000..3c4b9d7 --- /dev/null +++ b/macos/test/run_tests.sh @@ -0,0 +1,65 @@ +#!/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 new file mode 100644 index 0000000..e1a6e11 --- /dev/null +++ b/macos/test/texture_rgba_renderer_plugin_test.m @@ -0,0 +1,750 @@ +// 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; +} From 2c334da6f91a2818888e7bb968f22f703b6c8c8e Mon Sep 17 00:00:00 2001 From: yunchan Date: Sun, 15 Nov 2026 09:44:38 +0900 Subject: [PATCH 2/7] macos: copy rows of another stride one by one (Kitedesk T-4204) FlutterRgbaRendererPluginOnRgba gets rows padded to stride_align, and the pixel buffer has its own bytes per row ((width * 4 + 63) & ~63 on Apple Silicon). The frame was copied in one memcpy of len bytes, so rows came out shifted whenever the two differ: Kitedesk sends stride_align 1 for a session drawn in software, whose "all displays" view still uses textures, and any width that is not a multiple of 16 then shears (1366, 1001, 1279, 3838). A stride above the pixel buffer's wrote past its data. Rows are now copied one by one, width * 4 bytes each, when the strides differ, and in one memcpy of the rows otherwise. A frame shorter than its rows promise is dropped instead of being read past its end. Tests: rows_follow_the_frame_stride and short_frames_are_dropped pass; the other eight that failed still do. Co-Authored-By: Claude Opus 5.5 --- macos/Classes/TextRgba.swift | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/macos/Classes/TextRgba.swift b/macos/Classes/TextRgba.swift index 1708c42..c2860bf 100644 --- a/macos/Classes/TextRgba.swift +++ b/macos/Classes/TextRgba.swift @@ -4,6 +4,9 @@ // // Created by kingtous on 2023/2/17. // +// Modified by the Kitedesk fork (2026, T-4204): rows are copied one by one +// when their stride differs from the pixel buffer's. +// import Foundation import FlutterMacOS @@ -34,6 +37,12 @@ import CoreVideo } 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? // macOS only support 32BGRA currently. let dict: [String: Any] = [ @@ -50,7 +59,14 @@ import CoreVideo CVPixelBufferLockBaseAddress(pixelBufferCopy!, []) let ptr = CVPixelBufferGetBaseAddress(pixelBufferCopy!)! - memcpy(ptr, buffer, len) + let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBufferCopy!) + if (bytesPerRow == stride) { + memcpy(ptr, buffer, stride * height) + } else { + for y in 0.. Date: Sun, 15 Nov 2026 09:46:49 +0900 Subject: [PATCH 3/7] macos: take frame buffers from a pool, keep the two last handed (Kitedesk T-4204) Every frame made a new IOSurface-backed CVPixelBuffer (33 MB at 4K), and the engine let it go soon after. R T-4201 measured 2.2 to 7.8 ms per 4K frame alone, up to 110 ms, and a 6.5 ms median at 60 fps; upstream #16922 saw 37% of the CPU and memory swings of 1.85 to 2.43 GB. Frames now go into the buffers of a CVPixelBufferPool with IOSurface properties, made again when the frame size changes. A pool hands a buffer out again as soon as nobody holds it, even while its IOSurface backs a live Metal texture (measured), and engine a18df97c lets go of the pixel buffer and of its CVMetalTexture as soon as it has the Metal texture, which Skia then draws until the next frame. So the texture keeps the buffers it last handed to the engine: the one drawn, and the one drawn before it, which GPU work queued meanwhile may still read (the plugin cannot see when the GPU is done, flutter#157379; inferred). With the newest frame and the one being written, a texture uses three buffers while the engine keeps up and four when it falls behind. Tests: pool_reuses_buffers passes (60 IOSurfaces for 60 frames of a size, now at most 4). drawn_frame_is_not_overwritten fails when no handed buffer is kept, frame_drawn_before_is_not_overwritten when one is; both pass with two. Co-Authored-By: Claude Opus 5.5 --- macos/Classes/TextRgba.swift | 56 +++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/macos/Classes/TextRgba.swift b/macos/Classes/TextRgba.swift index c2860bf..18f19b0 100644 --- a/macos/Classes/TextRgba.swift +++ b/macos/Classes/TextRgba.swift @@ -4,8 +4,10 @@ // // Created by kingtous on 2023/2/17. // -// Modified by the Kitedesk fork (2026, T-4204): rows are copied one by one -// when their stride differs from the pixel buffer's. +// 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; rows are copied one by one when their +// stride differs from the pixel buffer's. // import Foundation @@ -19,6 +21,12 @@ import CoreVideo private var width: Int = 0 private var height: Int = 0 private let queue = DispatchQueue(label: "text_rgba_sync_queue") + // Buffers of width x height. + private var pool: CVPixelBufferPool? + // 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() @@ -32,6 +40,12 @@ 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!) } } @@ -43,17 +57,14 @@ import CoreVideo guard width > 0 && height > 0 && len >= stride * height else { return false } + if (pool == nil || self.width != width || self.height != height) { + pool = TextRgba.newPool(width: width, height: height) + self.width = width + self.height = height + } var pixelBufferCopy: CVPixelBuffer? - // macOS only support 32BGRA currently. - let dict: [String: Any] = [ - kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA, - kCVPixelBufferMetalCompatibilityKey as String: true, - kCVPixelBufferOpenGLCompatibilityKey as String: true, - // https://developer.apple.com/forums/thread/712709 - kCVPixelBufferBytesPerRowAlignmentKey as String: 64 - ] - let result = CVPixelBufferCreate(kCFAllocatorDefault, width, height, kCVPixelFormatType_32BGRA, dict as CFDictionary, &pixelBufferCopy) - guard result == kCVReturnSuccess else { + guard let pool = pool, + CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, pool, &pixelBufferCopy) == kCVReturnSuccess else { return false } @@ -69,8 +80,6 @@ import CoreVideo } CVPixelBufferUnlockBaseAddress(pixelBufferCopy!, []) self.data = pixelBufferCopy - self.width = width - self.height = height if textureId != -1 && self.data != nil { registry?.textureFrameAvailable(textureId) return true @@ -86,6 +95,25 @@ import CoreVideo } + 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 + } + @objc public func markFrameAvaliable(data: Data, width: Int, height: Int, stride_align: Int) -> Bool { data.withUnsafeBytes { buffer in markFrameAvaliableRaw( From 11478d230c6dff8a3d988a7b7b0e93780048a041 Mon Sep 17 00:00:00 2001 From: yunchan Date: Sun, 15 Nov 2026 09:47:33 +0900 Subject: [PATCH 4/7] macos: copy frames outside the queue the raster thread waits on (Kitedesk T-4204) The whole frame, buffer and copy included, ran in queue.sync, and the engine's raster thread takes the newest frame in copyPixelBuffer on the same queue, so it waited for the copy: R T-4201 measured a p90 of 4.0 ms at 60 Hz (6.5 ms at 120 Hz), and the raster thread draws the whole window. The frame is now copied into its pool buffer outside the queue; the queue only guards the pool, the newest frame and the handed buffers, so it is held for a pointer swap. Tests: raster_does_not_wait_for_copies passes (4K frames back to back: the raster's median wait was 0.9 to 1.1 ms in three runs, up to 537 of 600 calls over 1 ms; now 0.003 ms, at most 0.032 ms). Co-Authored-By: Claude Opus 5.5 --- macos/Classes/TextRgba.swift | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/macos/Classes/TextRgba.swift b/macos/Classes/TextRgba.swift index 18f19b0..9737716 100644 --- a/macos/Classes/TextRgba.swift +++ b/macos/Classes/TextRgba.swift @@ -6,8 +6,9 @@ // // 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; rows are copied one by one when their -// stride differs from the pixel buffer's. +// 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. // import Foundation @@ -21,7 +22,7 @@ import CoreVideo private var width: Int = 0 private var height: Int = 0 private let queue = DispatchQueue(label: "text_rgba_sync_queue") - // Buffers of width x height. + // Buffers of width x height. With data, width, height and handed, guarded by queue. private var pool: CVPixelBufferPool? // The buffers last handed to the engine, the newest last: not written again. private var handed: [CVPixelBuffer] = [] @@ -57,17 +58,13 @@ import CoreVideo guard width > 0 && height > 0 && len >= stride * height else { return false } - if (pool == nil || self.width != width || self.height != height) { - pool = TextRgba.newPool(width: width, height: height) - self.width = width - self.height = height - } var pixelBufferCopy: CVPixelBuffer? - guard let pool = pool, + 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!) @@ -79,8 +76,10 @@ import CoreVideo } } CVPixelBufferUnlockBaseAddress(pixelBufferCopy!, []) - self.data = pixelBufferCopy - if textureId != -1 && self.data != nil { + queue.sync { + self.data = pixelBufferCopy + } + if textureId != -1 { registry?.textureFrameAvailable(textureId) return true } else { @@ -88,11 +87,21 @@ import CoreVideo } } - @objc public func markFrameAvaliableRaw(buffer: UnsafePointer, len: Int, width: Int, height: Int, stride_align: Int) -> Bool { + // The pool of buffers of width x height, a new one for a new size. + private func poolOf(width: Int, height: Int) -> CVPixelBufferPool? { queue.sync { - _markFrameAvaliable(buffer: buffer, len: len, width: width, height: height, stride_align: stride_align) + if (pool == nil || self.width != width || self.height != height) { + pool = TextRgba.newPool(width: width, height: height) + self.width = width + self.height = height + } + return pool } } + + @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? { From 2a36975b628b043090b5373aed5987f7329ad48c Mon Sep 17 00:00:00 2001 From: yunchan Date: Sun, 15 Nov 2026 09:47:57 +0900 Subject: [PATCH 5/7] macos: announce frames on the main queue (Kitedesk T-4204) textureFrameAvailable was called on the thread that pushed the frame, a Kitedesk video thread. The engine wants it on the platform thread (Shell::OnPlatformViewMarkTextureFrameAvailable checks it, engine a18df97c), as it wants registerTexture and unregisterTexture, which already run there; Kitedesk's own GPU texture (KdMatoyaTexture.swift) posts its notices the same way. The notice is now posted to the main queue; the registry is only used on the main thread. Tests: frames_are_announced_on_the_main_thread passes. Co-Authored-By: Claude Opus 5.5 --- macos/Classes/TextRgba.swift | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/macos/Classes/TextRgba.swift b/macos/Classes/TextRgba.swift index 9737716..ba405be 100644 --- a/macos/Classes/TextRgba.swift +++ b/macos/Classes/TextRgba.swift @@ -8,7 +8,7 @@ // 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. +// differs from the pixel buffer's; frames are announced on the main queue. // import Foundation @@ -17,6 +17,7 @@ import CoreVideo @objc public class TextRgba: NSObject, FlutterTexture { public var textureId: Int64 = -1 + // Main thread only. private var registry: FlutterTextureRegistry? private var data: CVPixelBuffer? private var width: Int = 0 @@ -80,7 +81,10 @@ import CoreVideo self.data = pixelBufferCopy } if textureId != -1 { - registry?.textureFrameAvailable(textureId) + // On the platform (main) thread, which the engine checks. + DispatchQueue.main.async { + self.registry?.textureFrameAvailable(self.textureId) + } return true } else { return false From 6c3425539f46426b9c603569af3dde14d36e757c Mon Sep 17 00:00:00 2001 From: yunchan Date: Sun, 15 Nov 2026 09:52:18 +0900 Subject: [PATCH 6/7] macos: let closed textures' buffers go, keep closed identities (Kitedesk T-4204) closeTexture unregistered the texture but kept it in the plugin's map with its last frame (33 MB at 4K) until the engine went away, and the texture went on taking and announcing frames pushed to its pointer. Kitedesk makes a texture per display, so each display switch and closed tab leaked a frame. closeTexture now takes the texture out of the map and closes it: frames pushed afterwards are dropped and not announced, the engine gets no frame any more, and the frames, the handed buffers and the pool go. As on Windows (B252), a texture closed without "recycle" may still get frames through the pointer from getTexturePtr (a tab moving to another window keeps its session's pointer), so it is kept for the process lifetime and its address is never another texture's; with "recycle" it is freed once the engine has unregistered it. The textures still open when the engine goes away are closed and kept the same way: the registry's reference to them used to keep them alive, announcing every frame to a registry without an engine. A closed texture no longer holds the registry. Tests: closing_frees_frame_buffers (634 MB more after 20 4K textures, now 0.2 MB), closed_texture_drops_late_frames, closing_keeps_only_unrecycled_textures, open_textures_outlive_their_engine and frames_racing_close pass: 13 of 13. Co-Authored-By: Claude Opus 5.5 --- lib/texture_rgba_renderer.dart | 4 ++- macos/Classes/TextRgba.swift | 32 +++++++++++++++---- macos/Classes/TextureRgbaRendererPlugin.swift | 30 +++++++++++++++-- 3 files changed, 55 insertions(+), 11 deletions(-) diff --git a/lib/texture_rgba_renderer.dart b/lib/texture_rgba_renderer.dart index f8d1011..cca1094 100644 --- a/lib/texture_rgba_renderer.dart +++ b/lib/texture_rgba_renderer.dart @@ -1,4 +1,4 @@ -// Modified by the Kitedesk fork (2026, B212/B252): closeTexture ownership. +// Modified by the Kitedesk fork (2026, B212/B252/T-4204): closeTexture ownership. import 'dart:typed_data'; export './texture_rgba_ffi.dart' show Native; @@ -25,6 +25,8 @@ class TextureRgbaRenderer { /// 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. diff --git a/macos/Classes/TextRgba.swift b/macos/Classes/TextRgba.swift index ba405be..704d7b8 100644 --- a/macos/Classes/TextRgba.swift +++ b/macos/Classes/TextRgba.swift @@ -8,7 +8,8 @@ // IOSurface-backed buffers of a pool, which does not hand out again the // buffers last handed to the engine, and are copied outside the queue the // raster thread waits on; rows are copied one by one when their stride -// differs from the pixel buffer's; frames are announced on the main queue. +// differs from the pixel buffer's; frames are announced on the main queue; +// close() drops the frames pushed after it and lets the buffers go. // import Foundation @@ -17,14 +18,15 @@ import CoreVideo @objc public class TextRgba: NSObject, FlutterTexture { public var textureId: Int64 = -1 - // Main thread only. + // Main thread only. Nil once closed. private var registry: FlutterTextureRegistry? private var data: CVPixelBuffer? private var width: Int = 0 private var height: Int = 0 private let queue = DispatchQueue(label: "text_rgba_sync_queue") - // Buffers of width x height. With data, width, height and handed, guarded by queue. + // Buffers of width x height. With data, width, height, handed and closed, guarded by queue. private var pool: CVPixelBufferPool? + private var closed = false // The buffers last handed to the engine, the newest last: not written again. private var handed: [CVPixelBuffer] = [] // 2: the engine (3.24.5) drops its CVMetalTexture at once, so GPU work may still read the buffer handed before the drawn one (flutter#157379, inferred). @@ -77,10 +79,14 @@ import CoreVideo } } CVPixelBufferUnlockBaseAddress(pixelBufferCopy!, []) - queue.sync { + let open = queue.sync { () -> Bool in + if (closed) { + return false + } self.data = pixelBufferCopy + return true } - if textureId != -1 { + if open && textureId != -1 { // On the platform (main) thread, which the engine checks. DispatchQueue.main.async { self.registry?.textureFrameAvailable(self.textureId) @@ -91,10 +97,10 @@ import CoreVideo } } - // The pool of buffers of width x height, a new one for a new size. + // The pool of buffers of width x height, a new one for a new size; none once closed. private func poolOf(width: Int, height: Int) -> CVPixelBufferPool? { queue.sync { - if (pool == nil || self.width != width || self.height != height) { + if (!closed && (pool == nil || self.width != width || self.height != height)) { pool = TextRgba.newPool(width: width, height: height) self.width = width self.height = height @@ -103,6 +109,18 @@ import CoreVideo } } + // Main thread: closeTexture, or the engine going away. Frames pushed from now on are + // dropped and not announced, the engine gets no frame any more, and the buffers go. + func close() { + registry = nil + queue.sync { + closed = true + data = nil + handed = [] + pool = nil + } + } + @objc public func markFrameAvaliableRaw(buffer: UnsafePointer, len: Int, width: Int, height: Int, stride_align: Int) -> Bool { _markFrameAvaliable(buffer: buffer, len: len, width: width, height: height, stride_align: stride_align) } diff --git a/macos/Classes/TextureRgbaRendererPlugin.swift b/macos/Classes/TextureRgbaRendererPlugin.swift index 8c991a6..56b771e 100644 --- a/macos/Classes/TextureRgbaRendererPlugin.swift +++ b/macos/Classes/TextureRgbaRendererPlugin.swift @@ -1,3 +1,7 @@ +// 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 @@ -5,6 +9,12 @@ 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) @@ -12,6 +22,14 @@ 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 @@ -34,9 +52,15 @@ public class TextureRgbaRendererPlugin: NSObject, FlutterPlugin { case "closeTexture": let args = call.arguments as! Dictionary let key = args["key"] as! Int64 - if (renderer[key] != nil) { - if (-1 != renderer[key]?.textureId) { - texture_registry?.unregisterTexture(renderer[key]?.textureId ?? -1) + // "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) } result(true) } else { From b87d45fd09919a7edca63d70cc18ef107a554467 Mon Sep 17 00:00:00 2001 From: yunchan Date: Sun, 15 Nov 2026 09:52:53 +0900 Subject: [PATCH 7/7] macos: drain an autorelease pool around each frame (Kitedesk T-4204) FlutterRgbaRendererPluginOnRgba runs on the caller's thread, a Kitedesk video thread, which has no autorelease pool and lives as long as the session: an object autoreleased there would stay until the session ends (pointed out in upstream PR #2). Each frame now runs in an @autoreleasepool. On macOS 26.5.2 the frame path autoreleases nothing on that thread, before and after this series (frames_leave_no_autoreleased_objects passes on 50efc18e too), so this is a guard: a probe saw CoreVideo autorelease objects when a process first made an IOSurface on such a thread, and the engine's registrar logs with NSLog for a frame announced to it after its engine is gone, which the old code did on the video thread. Tests: 13 of 13 pass. Co-Authored-By: Claude Opus 5.5 --- macos/Classes/TextureRgbaApi.m | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/macos/Classes/TextureRgbaApi.m b/macos/Classes/TextureRgbaApi.m index 3b4fd37..6dc2812 100644 --- a/macos/Classes/TextureRgbaApi.m +++ b/macos/Classes/TextureRgbaApi.m @@ -4,6 +4,9 @@ // // 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" @@ -14,8 +17,12 @@ 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) { - TextRgba* texture_rgba = (__bridge TextRgba *)(texture_rgba_ptr); - [texture_rgba markFrameAvaliableRawWithBuffer:buffer len:len width:width height:height stride_align: 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]; + } } #if __cplusplus