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 <noreply@anthropic.com>
This commit is contained in:
parent
50efc18ec5
commit
9a377b6155
2 changed files with 815 additions and 0 deletions
65
macos/test/run_tests.sh
Executable file
65
macos/test/run_tests.sh
Executable file
|
|
@ -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 ]
|
||||
750
macos/test/texture_rgba_renderer_plugin_test.m
Normal file
750
macos/test/texture_rgba_renderer_plugin_test.m
Normal file
|
|
@ -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 | <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(¬ices, 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(¬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 | <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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue