Stop mpv render from blocking until frame target time

mpv_render_context_render slept until each frame's display time
(~40ms at 25fps) inside CAOpenGLLayer.display(), blocking main-thread
CoreAnimation commits and hitching UI scrolling during playback. Pass
MPV_RENDER_PARAM_BLOCK_FOR_TARGET_TIME=0 and let CGL vsync pace
presentation instead.
This commit is contained in:
Arkadiusz Fal
2026-09-24 06:38:01 +02:00
parent 22451804db
commit ed2884d7ec

View File

@@ -2271,18 +2271,28 @@ final class MPVClient: @unchecked Sendable {
var flipY: Int32 = 1
var bufferDepth = depth
// Without this, mpv sleeps inside mpv_render_context_render until the
// frame's target display time (a full frame duration, ~40ms at 25fps).
// That sleep happens inside CAOpenGLLayer.display() and blocks the
// main thread's CoreAnimation commits for its whole duration, hitching
// UI scrolling during playback. Vsync on the CGL context paces
// presentation instead.
var blockForTargetTime: Int32 = 0
withUnsafeMutablePointer(to: &fboData) { fboPtr in
withUnsafeMutablePointer(to: &flipY) { flipPtr in
withUnsafeMutablePointer(to: &bufferDepth) { depthPtr in
var params: [mpv_render_param] = [
mpv_render_param(type: MPV_RENDER_PARAM_OPENGL_FBO, data: fboPtr),
mpv_render_param(type: MPV_RENDER_PARAM_FLIP_Y, data: flipPtr),
mpv_render_param(type: MPV_RENDER_PARAM_DEPTH, data: depthPtr),
mpv_render_param(type: MPV_RENDER_PARAM_INVALID, data: nil)
]
_ = params.withUnsafeMutableBufferPointer { paramsPtr in
mpv_render_context_render(renderContext, paramsPtr.baseAddress)
withUnsafeMutablePointer(to: &blockForTargetTime) { blockPtr in
var params: [mpv_render_param] = [
mpv_render_param(type: MPV_RENDER_PARAM_OPENGL_FBO, data: fboPtr),
mpv_render_param(type: MPV_RENDER_PARAM_FLIP_Y, data: flipPtr),
mpv_render_param(type: MPV_RENDER_PARAM_DEPTH, data: depthPtr),
mpv_render_param(type: MPV_RENDER_PARAM_BLOCK_FOR_TARGET_TIME, data: blockPtr),
mpv_render_param(type: MPV_RENDER_PARAM_INVALID, data: nil)
]
_ = params.withUnsafeMutableBufferPointer { paramsPtr in
mpv_render_context_render(renderContext, paramsPtr.baseAddress)
}
}
}
}