Improve video layer rendering on macOS

Refactored glUpdate to use requestRedraw method for better control. Added needsRedraw flag to prevent redundant display calls. Enabled asynchronous drawing on VideoLayer for improved performance. Modified displayLinkCallback to only report swap without triggering display to avoid flooding the main thread.
This commit is contained in:
Arkadiusz Fal
2025-11-14 20:24:33 +01:00
parent ddf997ee58
commit b88169c7dd
2 changed files with 19 additions and 6 deletions

View File

@@ -5,11 +5,15 @@ import OpenGL.GL3
final class VideoLayer: CAOpenGLLayer {
var client: MPVClient!
private var needsRedraw = false
private let redrawQueue = DispatchQueue(label: "com.yattee.videolayer.redraw", qos: .userInteractive)
override init() {
super.init()
autoresizingMask = [.layerWidthSizable, .layerHeightSizable]
backgroundColor = NSColor.black.cgColor
// Enable asynchronous drawing for better performance
isAsynchronous = true
}
@available(*, unavailable)
@@ -32,6 +36,8 @@ final class VideoLayer: CAOpenGLLayer {
forLayerTime _: CFTimeInterval,
displayTime _: UnsafePointer<CVTimeStamp>?
) {
needsRedraw = false
var i: GLint = 0
var flip: CInt = 1
var ditherDepth = 8
@@ -92,13 +98,23 @@ final class VideoLayer: CAOpenGLLayer {
}
override func display() {
// Mark as needing display without blocking
guard !needsRedraw else { return }
needsRedraw = true
super.display()
CATransaction.flush()
}
func requestRedraw() {
// Called from MPV's glUpdate callback - use setNeedsDisplay for efficient batching
DispatchQueue.main.async { [weak self] in
self?.setNeedsDisplay()
}
}
let displayLinkCallback: CVDisplayLinkOutputCallback = { _, _, _, _, _, displayLinkContext -> CVReturn in
let layer: VideoLayer = unsafeBitCast(displayLinkContext, to: VideoLayer.self)
if layer.client?.mpvGL != nil {
// Just report swap - don't trigger display here to avoid flooding main thread
mpv_render_context_report_swap(layer.client.mpvGL)
}
return kCVReturnSuccess