From 2a4bf7ca22e0513d7ad0f9072e1877d891e182ac Mon Sep 17 00:00:00 2001 From: der richter Date: Sat, 23 Mar 2024 22:07:06 +0100 Subject: [PATCH] cocoa-cb: use a separate mpv_handle for cocoa-cb to simplify shutdown --- osdep/mac/app_hub.swift | 6 ------ osdep/mac/application.m | 3 ++- osdep/mac/libmpv_helper.swift | 25 ++++++++++--------------- video/out/cocoa_cb_common.swift | 9 ++++----- 4 files changed, 16 insertions(+), 27 deletions(-) diff --git a/osdep/mac/app_hub.swift b/osdep/mac/app_hub.swift index cdfd03f153..d95faf733f 100644 --- a/osdep/mac/app_hub.swift +++ b/osdep/mac/app_hub.swift @@ -95,12 +95,6 @@ class AppHub: NSObject { switch event.pointee.event_id { case MPV_EVENT_SHUTDOWN: -#if HAVE_MACOS_COCOA_CB - if let app = NSApp as? Application, app.cocoaCB?.isShuttingDown ?? false { - mpv = nil; - return - } -#endif mpv_destroy(mpv) mpv = nil default: break diff --git a/osdep/mac/application.m b/osdep/mac/application.m index b56882b6b8..439a109b7e 100644 --- a/osdep/mac/application.m +++ b/osdep/mac/application.m @@ -175,7 +175,8 @@ static const char mac_icon[] = { #if HAVE_MACOS_COCOA_CB if (!_cocoa_cb) { - [NSApp setCocoaCB:[[CocoaCB alloc] init:ctx]]; + mpv_handle *mpv = mpv_create_client(ctx, "cocoacb"); + [NSApp setCocoaCB:[[CocoaCB alloc] init:mpv]]; } #endif } diff --git a/osdep/mac/libmpv_helper.swift b/osdep/mac/libmpv_helper.swift index 3d61b9126c..4be6ee6e45 100644 --- a/osdep/mac/libmpv_helper.swift +++ b/osdep/mac/libmpv_helper.swift @@ -26,7 +26,7 @@ class LibmpvHelper { var mpvHandle: OpaquePointer? var mpvRenderContext: OpaquePointer? var fbo: GLint = 1 - let deinitLock = NSLock() + let uninitLock = NSLock() init(_ mpv: OpaquePointer, _ mpLog: OpaquePointer?) { mpvHandle = mpv @@ -93,18 +93,18 @@ class LibmpvHelper { } func isRenderUpdateFrame() -> Bool { - deinitLock.lock() + uninitLock.lock() if mpvRenderContext == nil { - deinitLock.unlock() + uninitLock.unlock() return false } let flags: UInt64 = mpv_render_context_update(mpvRenderContext) - deinitLock.unlock() + uninitLock.unlock() return flags & UInt64(MPV_RENDER_UPDATE_FRAME.rawValue) > 0 } func drawRender(_ surface: NSSize, _ depth: GLint, _ ctx: CGLContextObj, skip: Bool = false) { - deinitLock.lock() + uninitLock.lock() if mpvRenderContext != nil { var i: GLint = 0 let flip: CInt = 1 @@ -137,7 +137,7 @@ class LibmpvHelper { if !skip { CGLFlushDrawable(ctx) } - deinitLock.unlock() + uninitLock.unlock() } func setRenderICCProfile(_ profile: NSColorSpace) { @@ -168,19 +168,14 @@ class LibmpvHelper { } } - func deinitRender() { + func uninit() { mpv_render_context_set_update_callback(mpvRenderContext, nil, nil) mp_render_context_set_control_callback(mpvRenderContext, nil, nil) - deinitLock.lock() + uninitLock.lock() mpv_render_context_free(mpvRenderContext) mpvRenderContext = nil - deinitLock.unlock() - } - - func deinitMPV(_ destroy: Bool = false) { - if destroy { - mpv_destroy(mpvHandle) - } + mpv_destroy(mpvHandle) mpvHandle = nil + uninitLock.unlock() } } diff --git a/video/out/cocoa_cb_common.swift b/video/out/cocoa_cb_common.swift index bd4ac44727..7ac0f35096 100644 --- a/video/out/cocoa_cb_common.swift +++ b/video/out/cocoa_cb_common.swift @@ -21,7 +21,7 @@ class CocoaCB: Common { var libmpv: LibmpvHelper var layer: GLLayer? - @objc var isShuttingDown: Bool = false + var isShuttingDown: Bool = false enum State { case uninitialized @@ -199,7 +199,7 @@ class CocoaCB: Common { return super.control(vo, events: events, request: request, data: data) } - func shutdown(_ destroy: Bool = false) { + func shutdown() { isShuttingDown = window?.isAnimating ?? false || window?.isInFullscreen ?? false && option.vo.native_fs if window?.isInFullscreen ?? false && !(window?.isAnimating ?? false) { @@ -211,14 +211,13 @@ class CocoaCB: Common { uninitCommon() layer?.lockCglContext() - libmpv.deinitRender() + libmpv.uninit() layer?.unlockCglContext() - libmpv.deinitMPV(destroy) } func checkShutdown() { if isShuttingDown { - shutdown(true) + shutdown() } }