From dfebac1019560e278c605247de3b98cff1d73226 Mon Sep 17 00:00:00 2001 From: Berke Güzel Date: Mon, 12 Oct 2026 14:31:10 +0300 Subject: perf: cache Cocoa software MTLBuffer and optimize Wayland output/focus loops --- src/cocoa/window.m | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'src/cocoa/window.m') diff --git a/src/cocoa/window.m b/src/cocoa/window.m index 78ab0bd..8856fde 100644 --- a/src/cocoa/window.m +++ b/src/cocoa/window.m @@ -53,6 +53,8 @@ struct cocoa_surface { YaslView *view; CAMetalLayer *layer; id software_queue; + id software_buffer; + NSUInteger software_buffer_capacity; CADisplayLink *link; struct cocoa_surface *parent; @@ -1031,15 +1033,20 @@ bool cocoa_surface_present(struct cocoa_surface *w, const struct yasl_framebuffe return false; NSUInteger row_bytes = ((NSUInteger)w->size.x * 4 + 255) & ~(NSUInteger)255; NSUInteger length = row_bytes * (NSUInteger)w->size.y; - id buffer = [w->layer.device newBufferWithLength:length - options:MTLResourceStorageModeShared]; + if (!w->software_buffer || w->software_buffer_capacity < length) { + [w->software_buffer release]; + w->software_buffer = [w->layer.device newBufferWithLength:length + options:MTLResourceStorageModeShared]; + w->software_buffer_capacity = w->software_buffer ? length : 0; + } + id buffer = w->software_buffer; if (!buffer) return false; for (int y = 0; y < w->size.y; y++) memcpy((char *)buffer.contents + (NSUInteger)y * row_bytes, w->pixels + (size_t)y * w->size.x, (size_t)w->size.x * 4); id command = [w->software_queue commandBuffer]; id blit = [command blitCommandEncoder]; - if (!blit) { [buffer release]; return false; } + if (!blit) return false; id drawable = w->drawable; [blit copyFromBuffer:buffer sourceOffset:0 sourceBytesPerRow:row_bytes sourceBytesPerImage:length sourceSize:MTLSizeMake(w->size.x, w->size.y, 1) @@ -1048,7 +1055,6 @@ bool cocoa_surface_present(struct cocoa_surface *w, const struct yasl_framebuffe [blit endEncoding]; [command presentDrawable:drawable]; [command commit]; - [buffer release]; } yasl_clock_presented(&w->clock, yasl_loop_now_ns(), false); w->did_present = true; @@ -1394,6 +1400,7 @@ struct cocoa_surface *cocoa_surface_create_toplevel(struct cocoa_display *d, static void surface_free(struct cocoa_surface *w) { [w->link release]; + [w->software_buffer release]; [w->software_queue release]; [w->view release]; [w->controller release]; -- cgit v1.2.3