From 760d471cd83f27edf932564f41f87a9acd66e02c Mon Sep 17 00:00:00 2001 From: Akemi Date: Wed, 28 Feb 2018 15:08:41 +0100 Subject: [PATCH] cocoa-cb: use new libmpv API instead of opengl-cb a new replacement API was introduced with b037121 and the old one was deprecated. porting cocoa-cb to the new API. --- osdep/macOS_mpv_helper.swift | 107 +++++++++++++++------------ osdep/macOS_swift_bridge.h | 3 +- video/out/cocoa-cb/events_view.swift | 2 +- video/out/cocoa-cb/video_layer.swift | 14 ++-- video/out/cocoa_cb_common.swift | 12 +-- 5 files changed, 76 insertions(+), 62 deletions(-) diff --git a/osdep/macOS_mpv_helper.swift b/osdep/macOS_mpv_helper.swift index 75f23251a9..29019f77db 100644 --- a/osdep/macOS_mpv_helper.swift +++ b/osdep/macOS_mpv_helper.swift @@ -24,7 +24,7 @@ let glDummy: @convention(c) () -> Void = {} class MPVHelper: NSObject { var mpvHandle: OpaquePointer? - var mpvGLCBContext: OpaquePointer? + var mpvRenderContext: OpaquePointer? var mpvLog: OpaquePointer? var inputContext: OpaquePointer? var mpctx: UnsafeMutablePointer? @@ -44,30 +44,29 @@ class MPVHelper: NSObject { mpv_observe_property(mpvHandle, 0, "macos-title-bar-style", MPV_FORMAT_STRING) } - func setGLCB() { - if mpvHandle == nil { - sendError("No mpv handle available.") - exit(1) - } - mpvGLCBContext = OpaquePointer(mp_get_sub_api2(mpvHandle, MPV_SUB_API_OPENGL_CB, false)) - if mpvGLCBContext == nil { - sendError("libmpv does not have the opengl-cb sub-API.") + func initRender() { + let api = UnsafeMutableRawPointer(mutating: (MPV_RENDER_API_TYPE_OPENGL as NSString).utf8String) + var pAddress = mpv_opengl_init_params(get_proc_address: getProcAddress, + get_proc_address_ctx: nil, + extra_exts: nil) + var params: [mpv_render_param] = [ + mpv_render_param(type: MPV_RENDER_PARAM_API_TYPE, data: api), + mpv_render_param(type: MPV_RENDER_PARAM_OPENGL_INIT_PARAMS, data: &pAddress), + mpv_render_param() + ] + + if (mpv_render_context_create(&mpvRenderContext, mpvHandle, ¶ms) < 0) + { + sendError("Render context init has failed.") exit(1) } } - func initGLCB() { - if mpvGLCBContext == nil { - setGLCB() - } - if mpv_opengl_cb_init_gl(mpvGLCBContext, nil, getProcAddress, nil) < 0 { - sendError("GL init has failed.") - exit(1) - } - } - - let getProcAddress: mpv_opengl_cb_get_proc_address_fn = { - (ctx: UnsafeMutableRawPointer?, name: UnsafePointer?) -> UnsafeMutableRawPointer? in + let getProcAddress: (@convention(c) (UnsafeMutableRawPointer?, UnsafePointer?) + -> UnsafeMutableRawPointer?)! = + { + (ctx: UnsafeMutableRawPointer?, name: UnsafePointer?) + -> UnsafeMutableRawPointer? in let symbol: CFString = CFStringCreateWithCString( kCFAllocatorDefault, name, kCFStringEncodingASCII) let indentifier = CFBundleGetBundleWithIdentifier("com.apple.opengl" as CFString) @@ -80,54 +79,68 @@ class MPVHelper: NSObject { return addr } - func setGLCBUpdateCallback(_ callback: @escaping mpv_opengl_cb_update_fn, context object: AnyObject) { - if mpvGLCBContext == nil { - sendWarning("Init mpv opengl-cb first.") + func setRenderUpdateCallback(_ callback: @escaping mpv_render_update_fn, context object: AnyObject) { + if mpvRenderContext == nil { + sendWarning("Init mpv render context first.") } else { - mpv_opengl_cb_set_update_callback(mpvGLCBContext, callback, MPVHelper.bridge(obj: object)) + mpv_render_context_set_update_callback(mpvRenderContext, callback, MPVHelper.bridge(obj: object)) } } - func setGLCBControlCallback(_ callback: @escaping mpv_opengl_cb_control_fn, context object: AnyObject) { - if mpvGLCBContext == nil { - sendWarning("Init mpv opengl-cb first.") + func setRenderControlCallback(_ callback: @escaping mp_render_cb_control_fn, context object: AnyObject) { + if mpvRenderContext == nil { + sendWarning("Init mpv render context first.") } else { - mp_client_set_control_callback(mpvGLCBContext, callback, MPVHelper.bridge(obj: object)) + mp_render_context_set_control_callback(mpvRenderContext, callback, MPVHelper.bridge(obj: object)) } } - func reportGLCBFlip() { - if mpvGLCBContext == nil { return } - mpv_opengl_cb_report_flip(mpvGLCBContext, 0) + func reportRenderFlip() { + if mpvRenderContext == nil { return } + mpv_render_context_report_swap(mpvRenderContext) } - func drawGLCB(_ surface: NSSize) { - if mpvGLCBContext != nil { + func drawRender(_ surface: NSSize) { + if mpvRenderContext != nil { var i: GLint = 0 + var flip: CInt = 1 glGetIntegerv(GLenum(GL_DRAW_FRAMEBUFFER_BINDING), &i) // CAOpenGLLayer has ownership of FBO zero yet can return it to us, // so only utilize a newly received FBO ID if it is nonzero. fbo = i != 0 ? i : fbo - mpv_opengl_cb_draw(mpvGLCBContext, fbo, Int32(surface.width), Int32(-surface.height)) + var data = mpv_opengl_fbo(fbo: Int32(i), + w: Int32(surface.width), + h: Int32(surface.height), + internal_format: 0) + var params: [mpv_render_param] = [ + mpv_render_param(type: MPV_RENDER_PARAM_OPENGL_FBO, data: &data), + mpv_render_param(type: MPV_RENDER_PARAM_FLIP_Y, data: &flip), + mpv_render_param() + ] + mpv_render_context_render(mpvRenderContext, ¶ms); } else { glClearColor(0, 0, 0, 1) glClear(GLbitfield(GL_COLOR_BUFFER_BIT)) } } - func setGLCBICCProfile(_ profile: NSColorSpace) { - if mpvGLCBContext == nil { return } + func setRenderICCProfile(_ profile: NSColorSpace) { + if mpvRenderContext == nil { return } var iccData = profile.iccProfileData iccData!.withUnsafeMutableBytes { (u8Ptr: UnsafeMutablePointer) in - let icc = bstrdup(nil, bstr(start: u8Ptr, len: iccData!.count)) - mp_client_set_icc_profile(mpvGLCBContext, icc) + let iccBstr = bstrdup(nil, bstr(start: u8Ptr, len: iccData!.count)) + var icc = mpv_byte_array(data: iccBstr.start, size: iccBstr.len) + let params = mpv_render_param(type: MPV_RENDER_PARAM_ICC_PROFILE, data: &icc) + mpv_render_context_set_parameter(mpvRenderContext, params) } } - func setGLCBLux(_ lux: Int) { - if mpvGLCBContext == nil { return } - mp_client_set_ambient_lux(mpvGLCBContext, Int32(lux)) + func setRenderLux(_ lux: Int) { + if mpvRenderContext == nil { return } + var light = lux + let params = mpv_render_param(type: MPV_RENDER_PARAM_AMBIENT_LIGHT, data: &light) + mpv_render_context_set_parameter(mpvRenderContext, params) } func command(_ cmd: String) { @@ -225,11 +238,11 @@ class MPVHelper: NSObject { print("\(level)[osx/cocoacb] \(msg)\u{001B}[0;30m") } - func deinitGLCB() { - mpv_opengl_cb_set_update_callback(mpvGLCBContext, nil, nil) - mp_client_set_control_callback(mpvGLCBContext, nil, nil) - mpv_opengl_cb_uninit_gl(mpvGLCBContext) - mpvGLCBContext = nil + func deinitRender() { + mpv_render_context_set_update_callback(mpvRenderContext, nil, nil) + mp_render_context_set_control_callback(mpvRenderContext, nil, nil) + mpv_render_context_free(mpvRenderContext) + mpvRenderContext = nil } func deinitMPV() { diff --git a/osdep/macOS_swift_bridge.h b/osdep/macOS_swift_bridge.h index 6886fce636..b0121d2a0f 100644 --- a/osdep/macOS_swift_bridge.h +++ b/osdep/macOS_swift_bridge.h @@ -20,7 +20,8 @@ #import #include "player/client.h" -#include "libmpv/opengl_cb.h" +#include "video/out/libmpv.h" +#include "libmpv/render_gl.h" #include "player/core.h" #include "input/input.h" diff --git a/video/out/cocoa-cb/events_view.swift b/video/out/cocoa-cb/events_view.swift index e2da8345f1..729a26263e 100644 --- a/video/out/cocoa-cb/events_view.swift +++ b/video/out/cocoa-cb/events_view.swift @@ -52,7 +52,7 @@ class EventsView: NSView { return } - tracker = NSTrackingArea(rect: self.bounds, + tracker = NSTrackingArea(rect: bounds, options: [.activeAlways, .mouseEnteredAndExited, .mouseMoved, .enabledDuringMouseDrag], owner: self, userInfo: nil) addTrackingArea(tracker!) diff --git a/video/out/cocoa-cb/video_layer.swift b/video/out/cocoa-cb/video_layer.swift index 8e2eee621b..c5604b88f7 100644 --- a/video/out/cocoa-cb/video_layer.swift +++ b/video/out/cocoa-cb/video_layer.swift @@ -76,10 +76,10 @@ class VideoLayer: CAOpenGLLayer { fatalError("init(coder:) has not been implemented") } - func setUpGLCB() { - self.mpv.initGLCB() - self.mpv.setGLCBUpdateCallback(self.updateCallback, context: self) - self.mpv.setGLCBControlCallback(self.cocoaCB.controlCallback, context: self.cocoaCB) + func setUpRender() { + mpv.initRender() + mpv.setRenderUpdateCallback(updateCallback, context: self) + mpv.setRenderControlCallback(cocoaCB.controlCallback, context: cocoaCB) } override func canDraw(inCGLContext ctx: CGLContextObj, @@ -114,7 +114,7 @@ class VideoLayer: CAOpenGLLayer { } } - mpv.drawGLCB(surfaceSize!) + mpv.drawRender(surfaceSize!) CGLFlushDrawable(ctx) drawLock.unlock() @@ -200,7 +200,7 @@ class VideoLayer: CAOpenGLLayer { return ctx } - let updateCallback: mpv_opengl_cb_update_fn = { (ctx) in + let updateCallback: mpv_render_update_fn = { (ctx) in let layer: VideoLayer = MPVHelper.bridge(ptr: ctx!) layer.neededFlips += 1 } @@ -218,7 +218,7 @@ class VideoLayer: CAOpenGLLayer { } func reportFlip() { - mpv.reportGLCBFlip() + mpv.reportRenderFlip() videoLock.lock() if !isAsynchronous && neededFlips > 0 && hasVideo { if !cocoaCB.window.occlusionState.contains(.visible) && diff --git a/video/out/cocoa_cb_common.swift b/video/out/cocoa_cb_common.swift index c18bb29971..426181d13e 100644 --- a/video/out/cocoa_cb_common.swift +++ b/video/out/cocoa_cb_common.swift @@ -61,7 +61,7 @@ class CocoaCB: NSObject { func setMpvHandle(_ ctx: OpaquePointer) { mpv = MPVHelper(ctx) - layer.setUpGLCB() + layer.setUpRender() } func preinit() { @@ -113,7 +113,7 @@ class CocoaCB: NSObject { NSApp.activate(ignoringOtherApps: true) layer.setVideo(true) - if self.mpv.getBoolProperty("fullscreen") { + if mpv.getBoolProperty("fullscreen") { DispatchQueue.main.async { self.window.toggleFullScreen(nil) } @@ -239,7 +239,7 @@ class CocoaCB: NSObject { func updateICCProfile() { if mpv.getBoolProperty("icc-profile-auto") { - mpv.setGLCBICCProfile(window.screen!.colorSpace!) + mpv.setRenderICCProfile(window.screen!.colorSpace!) } layer.colorspace = window.screen!.colorSpace!.cgColorSpace! } @@ -273,7 +273,7 @@ class CocoaCB: NSObject { var mean = (values[0] + values[1]) / 2 if ccb.lastLmu != mean { ccb.lastLmu = mean - ccb.mpv.setGLCBLux(ccb.lmuToLux(ccb.lastLmu)) + ccb.mpv.setRenderLux(ccb.lmuToLux(ccb.lastLmu)) } } } @@ -383,7 +383,7 @@ class CocoaCB: NSObject { return ev } - var controlCallback: mpv_opengl_cb_control_fn = { ( ctx, events, request, data ) -> Int32 in + var controlCallback: mp_render_cb_control_fn = { ( ctx, events, request, data ) -> Int32 in let ccb: CocoaCB = MPVHelper.bridge(ptr: ctx!) switch mp_voctrl(request) { @@ -466,7 +466,7 @@ class CocoaCB: NSObject { stopDisplaylink() uninitLightSensor() removeDisplayReconfigureObserver() - mpv.deinitGLCB() + mpv.deinitRender() mpv.deinitMPV() case MPV_EVENT_PROPERTY_CHANGE: if backendState == .init {