mirror of
https://github.com/yattee/yattee.git
synced 2025-11-16 06:58:43 +00:00
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:
@@ -666,11 +666,8 @@ final class MPVClient: ObservableObject {
|
||||
func glUpdate(_ ctx: UnsafeMutableRawPointer?) {
|
||||
let videoLayer = unsafeBitCast(ctx, to: VideoLayer.self)
|
||||
|
||||
videoLayer.client?.queue?.async {
|
||||
if !videoLayer.isAsynchronous {
|
||||
videoLayer.display()
|
||||
}
|
||||
}
|
||||
// Request a redraw when MPV signals that new content is available
|
||||
videoLayer.requestRedraw()
|
||||
}
|
||||
#else
|
||||
func getProcAddress(_: UnsafeMutableRawPointer?, _ name: UnsafePointer<Int8>?) -> UnsafeMutableRawPointer? {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user