From 4405072e29263ef6e194cd977dd6e0f0338e5afa Mon Sep 17 00:00:00 2001 From: Dossy Shiobara Date: Tue, 3 May 2022 17:24:46 +0200 Subject: [PATCH] mac-capture: Improve window capture performance Replaces the current implementation of Window Capture, which uses Core Graphics to render a bitmap raster, with one that accesses the window's CGImage bitmap directly, blits it, and hands that off as a frame to OBS instead. Co-Authored-By: Sebastian Beckmann --- plugins/mac-capture/mac-window-capture.m | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/plugins/mac-capture/mac-window-capture.m b/plugins/mac-capture/mac-window-capture.m index 17c0c283e..594090ed7 100644 --- a/plugins/mac-capture/mac-window-capture.m +++ b/plugins/mac-capture/mac-window-capture.m @@ -50,28 +50,28 @@ static inline void capture_frame(struct window_capture *wc) size_t width = CGImageGetWidth(img); size_t height = CGImageGetHeight(img); - CGRect rect = {{0, 0}, {width, height}}; - da_resize(wc->buffer, width * height * 4); - uint8_t *data = wc->buffer.array; + if (!width || !height || CGImageGetBitsPerPixel(img) != 32 || + CGImageGetBitsPerComponent(img) != 8) { + CGImageRelease(img); + return; + } - CGContextRef cg_context = CGBitmapContextCreate( - data, width, height, 8, width * 4, wc->color_space, - kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst); - CGContextSetBlendMode(cg_context, kCGBlendModeCopy); - CGContextDrawImage(cg_context, rect, img); - CGContextRelease(cg_context); - CGImageRelease(img); + CGDataProviderRef provider = CGImageGetDataProvider(img); + CFDataRef data = CGDataProviderCopyData(provider); struct obs_source_frame frame = { .format = VIDEO_FORMAT_BGRA, .width = width, .height = height, - .data[0] = data, - .linesize[0] = width * 4, + .data[0] = (uint8_t *)CFDataGetBytePtr(data), + .linesize[0] = CGImageGetBytesPerRow(img), .timestamp = ts, }; obs_source_output_video(wc->source, &frame); + + CGImageRelease(img); + CFRelease(data); } static void *capture_thread(void *data)