Compare commits

..

No commits in common. "b87d45fd09919a7edca63d70cc18ef107a554467" and "50efc18ec5114aec26b81f49c7fc6cef5b773bcd" have entirely different histories.

6 changed files with 31 additions and 954 deletions

View file

@ -1,4 +1,4 @@
// Modified by the Kitedesk fork (2026, B212/B252/T-4204): closeTexture ownership.
// Modified by the Kitedesk fork (2026, B212/B252): closeTexture ownership.
import 'dart:typed_data';
export './texture_rgba_ffi.dart' show Native;
@ -25,8 +25,6 @@ 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.

View file

@ -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<UInt8>, 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..<height {
memcpy(ptr + y * bytesPerRow, buffer + y * stride, row)
}
}
CVPixelBufferUnlockBaseAddress(pixelBufferCopy!, [])
let open = queue.sync { () -> 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<UInt8>, 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<UInt8>, 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(

View file

@ -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 <Foundation/Foundation.h>
#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

View file

@ -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<Int64, TextRgba> = [:]
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<String, Any?>
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 {

View file

@ -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 ]

View file

@ -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 | <test>]
#import <CoreVideo/CoreVideo.h>
#import <FlutterMacOS/FlutterMacOS.h>
#import <Foundation/Foundation.h>
#import <IOSurface/IOSurface.h>
#import <Metal/Metal.h>
#include <fcntl.h>
#include <mach/mach.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
// 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 <FlutterTextureRegistry>
// 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<MTLTexture>)draw:(int64_t)textureId;
@end
@implementation FakeEngine {
// FlutterTextureRegistrar's textures (FlutterExternalTexture keeps the texture).
NSMutableDictionary<NSNumber*, NSObject<FlutterTexture>*>* _textures;
// Skia's last image of each texture.
NSMutableDictionary<NSNumber*, id<MTLTexture>>* _drawn;
id<MTLDevice> _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<FlutterTexture>*)texture {
int64_t textureId = (int64_t)(intptr_t)(__bridge void*)texture;
_textures[@(textureId)] = texture;
return textureId;
}
- (void)textureFrameAvailable:(int64_t)textureId {
atomic_fetch_add(&notices, 1);
if (![NSThread isMainThread]) {
atomic_fetch_add(&off_main_notices, 1);
}
}
- (void)unregisterTexture:(int64_t)textureId {
[_textures removeObjectForKey:@(textureId)];
[_drawn removeObjectForKey:@(textureId)];
}
- (id<MTLTexture>)draw:(int64_t)textureId {
CVPixelBufferRef pixelBuffer = [_textures[@(textureId)] copyPixelBuffer];
id<MTLTexture> 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 <FlutterBinaryMessenger>
@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 <FlutterPluginRegistrar>
- (instancetype)initWithEngine:(FakeEngine*)engine;
@property(nonatomic, readonly) NSObject<FlutterPlugin>* plugin;
@end
@implementation FakeRegistrar {
FakeEngine* _engine;
FakeMessenger* _messenger;
}
- (instancetype)initWithEngine:(FakeEngine*)engine {
if (self = [super init]) {
_engine = engine;
_messenger = [FakeMessenger new];
}
return self;
}
- (id<FlutterBinaryMessenger>)messenger {
return _messenger;
}
- (id<FlutterTextureRegistry>)textures {
return _engine;
}
- (NSView*)view {
return nil;
}
- (void)addMethodCallDelegate:(id<FlutterPlugin>)delegate channel:(FlutterMethodChannel*)channel {
_plugin = delegate;
}
- (void)addApplicationDelegate:(NSObject<FlutterAppLifecycleDelegate>*)delegate {
}
- (void)registerViewFactory:(NSObject<FlutterPlatformViewFactory>*)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<id<MTLTexture>>* drawn = [NSMutableArray array];
NSMutableSet<NSNumber*>* 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<MTLTexture> 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<MTLTexture> 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<MTLTexture> 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<MTLTexture> 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(&notices) == 1, "one notice for one frame, got %d", atomic_load(&notices));
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(&notices);
for (int i = 0; i < 5; i++) {
push(pointer, frame, 640, 480, 64);
}
drain_main_queue();
EXPECT(atomic_load(&notices) == before, "%d notices for frames after closing", atomic_load(&notices) - 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(&notices);
push(pointer, frame, 64, 64, 1);
drain_main_queue();
EXPECT(atomic_load(&notices) == 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(&notices) == 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(&notices);
push(pointer, frame, width, height, 64);
drain_main_queue();
EXPECT(atomic_load(&notices) == 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 | <test>]\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;
}