Skip to content

Commit

Permalink
cocoa-cb: lock CGLContext on uninit and manual redraw
Browse files Browse the repository at this point in the history
this fixes a crash on quit, when a CATransaction from a system owned
thread/event is happening at the same time. locking the context
synchronises these access and prevents the race condition.

the draw operation induced by any display call from the CAOpenGLLayer
doesn't need that lock, since the display function already does lock
that current context.

Fixes mpv-player#11681
  • Loading branch information
Akemi committed Feb 16, 2024
1 parent 2935320 commit c1d2d9a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions video/out/cocoa_cb_common.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ class CocoaCB: Common {
uninit()
uninitCommon()

layer?.lockCglContext()
libmpv.deinitRender()
layer?.unlockCglContext()
libmpv.deinitMPV(destroy)
}

Expand Down
10 changes: 10 additions & 0 deletions video/out/mac/gl_layer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ class GLLayer: CAOpenGLLayer {
}
}

func lockCglContext() {
CGLLockContext(cglContext)
}

func unlockCglContext() {
CGLUnlockContext(cglContext)
}

override func copyCGLPixelFormat(forDisplayMask mask: UInt32) -> CGLPixelFormatObj {
return cglPixelFormat
}
Expand All @@ -219,10 +227,12 @@ class GLLayer: CAOpenGLLayer {
super.display()
CATransaction.flush()
if isUpdate && needsFlip {
lockCglContext()
CGLSetCurrentContext(cglContext)
if libmpv.isRenderUpdateFrame() {
libmpv.drawRender(NSZeroSize, bufferDepth, cglContext, skip: true)
}
unlockCglContext()
}
displayLock.unlock()
}
Expand Down

0 comments on commit c1d2d9a

Please sign in to comment.