diff options
Diffstat (limited to 'video/out')
-rw-r--r-- | video/out/gpu/context.c | 4 | ||||
-rw-r--r-- | video/out/mac/metal_layer.swift | 43 | ||||
-rw-r--r-- | video/out/mac_common.swift | 177 | ||||
-rw-r--r-- | video/out/vulkan/common.h | 4 | ||||
-rw-r--r-- | video/out/vulkan/context_mac.m | 119 |
5 files changed, 347 insertions, 0 deletions
diff --git a/video/out/gpu/context.c b/video/out/gpu/context.c index 5b4987eeef..28f88014aa 100644 --- a/video/out/gpu/context.c +++ b/video/out/gpu/context.c @@ -51,6 +51,7 @@ extern const struct ra_ctx_fns ra_ctx_vulkan_win; extern const struct ra_ctx_fns ra_ctx_vulkan_xlib; extern const struct ra_ctx_fns ra_ctx_vulkan_android; extern const struct ra_ctx_fns ra_ctx_vulkan_display; +extern const struct ra_ctx_fns ra_ctx_vulkan_mac; /* Direct3D 11 */ extern const struct ra_ctx_fns ra_ctx_d3d11; @@ -113,6 +114,9 @@ static const struct ra_ctx_fns *contexts[] = { #if HAVE_VK_KHR_DISPLAY &ra_ctx_vulkan_display, #endif +#if HAVE_COCOA && HAVE_SWIFT + &ra_ctx_vulkan_mac, +#endif #endif /* No API contexts: */ diff --git a/video/out/mac/metal_layer.swift b/video/out/mac/metal_layer.swift new file mode 100644 index 0000000000..7cea87c0b4 --- /dev/null +++ b/video/out/mac/metal_layer.swift @@ -0,0 +1,43 @@ +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see <http://www.gnu.org/licenses/>. + */ + +import Cocoa + +class MetalLayer: CAMetalLayer { + unowned var common: MacCommon + + init(common com: MacCommon) { + common = com + super.init() + + pixelFormat = .rgba16Float + backgroundColor = NSColor.black.cgColor + } + + // necessary for when the layer containing window changes the screen + override init(layer: Any) { + guard let oldLayer = layer as? MetalLayer else { + fatalError("init(layer: Any) passed an invalid layer") + } + common = oldLayer.common + super.init() + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } +} diff --git a/video/out/mac_common.swift b/video/out/mac_common.swift new file mode 100644 index 0000000000..12d2870add --- /dev/null +++ b/video/out/mac_common.swift @@ -0,0 +1,177 @@ +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see <http://www.gnu.org/licenses/>. + */ + +import Cocoa + +class MacCommon: Common { + @objc var layer: MetalLayer? + + var timer: PreciseTimer? + var swapTime: UInt64 = 0 + let swapLock: NSCondition = NSCondition() + + var needsICCUpdate: Bool = false + + @objc init(_ vo: UnsafeMutablePointer<vo>) { + let newlog = mp_log_new(vo, vo.pointee.log, "mac") + super.init(newlog) + mpv = MPVHelper(vo, log) + timer = PreciseTimer(common: self) + + DispatchQueue.main.sync { + layer = MetalLayer(common: self) + initMisc(vo) + } + } + + @objc func config(_ vo: UnsafeMutablePointer<vo>) -> Bool { + mpv?.vo = vo + + DispatchQueue.main.sync { + let previousActiveApp = getActiveApp() + initApp() + + let (_, _, wr) = getInitProperties(vo) + + guard let layer = self.layer else { + log.sendError("Something went wrong, no MetalLayer was initialized") + exit(1) + } + + if window == nil { + initView(vo, layer) + initWindow(vo, previousActiveApp) + initWindowState() + } + + if !NSEqualSizes(window?.unfsContentFramePixel.size ?? NSZeroSize, wr.size) { + window?.updateSize(wr.size) + } + + windowDidResize() + needsICCUpdate = true + } + + return true + } + + @objc func uninit(_ vo: UnsafeMutablePointer<vo>) { + window?.waitForAnimation() + + timer?.terminate() + + DispatchQueue.main.sync { + window?.delegate = nil + window?.close() + + uninitCommon() + } + } + + @objc func swapBuffer() { + if mpv?.macOpts.macos_render_timer ?? Int32(RENDER_TIMER_CALLBACK) != RENDER_TIMER_SYSTEM { + swapLock.lock() + while(swapTime < 1) { + swapLock.wait() + } + swapTime = 0 + swapLock.unlock() + } + + if needsICCUpdate { + needsICCUpdate = false + updateICCProfile() + } + } + + func updateRenderSize(_ size: NSSize) { + mpv?.vo.pointee.dwidth = Int32(size.width) + mpv?.vo.pointee.dheight = Int32(size.height) + flagEvents(VO_EVENT_RESIZE | VO_EVENT_EXPOSE) + } + + override func displayLinkCallback(_ displayLink: CVDisplayLink, + _ inNow: UnsafePointer<CVTimeStamp>, + _ inOutputTime: UnsafePointer<CVTimeStamp>, + _ flagsIn: CVOptionFlags, + _ flagsOut: UnsafeMutablePointer<CVOptionFlags>) -> CVReturn + { + let frameTimer = mpv?.macOpts.macos_render_timer ?? Int32(RENDER_TIMER_CALLBACK) + let signalSwap = { [self] in + swapLock.lock() + swapTime += 1 + swapLock.signal() + swapLock.unlock() + } + + if frameTimer != RENDER_TIMER_SYSTEM { + if let timer = self.timer, frameTimer == RENDER_TIMER_PRECISE { + timer.scheduleAt(time: inOutputTime.pointee.hostTime, closure: signalSwap) + return kCVReturnSuccess + } + + signalSwap() + } + + return kCVReturnSuccess + } + + override func startDisplayLink(_ vo: UnsafeMutablePointer<vo>) { + super.startDisplayLink(vo) + timer?.updatePolicy(periodSeconds: 1 / currentFps()) + } + + override func updateDisplaylink() { + super.updateDisplaylink() + timer?.updatePolicy(periodSeconds: 1 / currentFps()) + } + + override func lightSensorUpdate() { + flagEvents(VO_EVENT_AMBIENT_LIGHTING_CHANGED) + } + + @objc override func updateICCProfile() { + guard let colorSpace = window?.screen?.colorSpace else { + log.sendWarning("Couldn't update ICC Profile, no color space available") + return + } + + if #available(macOS 10.11, *) { + layer?.colorspace = colorSpace.cgColorSpace + } + + flagEvents(VO_EVENT_ICC_PROFILE_CHANGED) + } + + override func windowDidResize() { + guard let window = window else { + log.sendWarning("No window available on window resize event") + return + } + + updateRenderSize(window.framePixel.size) + } + + override func windowDidChangeScreenProfile() { + needsICCUpdate = true + } + + override func windowDidChangeBackingProperties() { + layer?.contentsScale = window?.backingScaleFactor ?? 1 + windowDidResize() + } +} diff --git a/video/out/vulkan/common.h b/video/out/vulkan/common.h index 85e6c50f4b..d006942d41 100644 --- a/video/out/vulkan/common.h +++ b/video/out/vulkan/common.h @@ -22,6 +22,10 @@ #if HAVE_WIN32_DESKTOP #define VK_USE_PLATFORM_WIN32_KHR #endif +#if HAVE_COCOA +#define VK_USE_PLATFORM_MACOS_MVK +#define VK_USE_PLATFORM_METAL_EXT +#endif #include <libplacebo/vulkan.h> diff --git a/video/out/vulkan/context_mac.m b/video/out/vulkan/context_mac.m new file mode 100644 index 0000000000..8ac6e169f6 --- /dev/null +++ b/video/out/vulkan/context_mac.m @@ -0,0 +1,119 @@ +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "video/out/gpu/context.h" +#include "osdep/macOS_swift.h" + +#include "common.h" +#include "context.h" +#include "utils.h" + +struct priv { + struct mpvk_ctx vk; + MacCommon *vo_mac; +}; + +static void mac_vk_uninit(struct ra_ctx *ctx) +{ + struct priv *p = ctx->priv; + + ra_vk_ctx_uninit(ctx); + mpvk_uninit(&p->vk); + [p->vo_mac uninit:ctx->vo]; +} + +static void mac_vk_swap_buffers(struct ra_ctx *ctx) +{ + struct priv *p = ctx->priv; + [p->vo_mac swapBuffer]; +} + +static bool mac_vk_init(struct ra_ctx *ctx) +{ + struct priv *p = ctx->priv = talloc_zero(ctx, struct priv); + struct mpvk_ctx *vk = &p->vk; + int msgl = ctx->opts.probing ? MSGL_V : MSGL_ERR; + + if (!mpvk_init(vk, ctx, VK_EXT_METAL_SURFACE_EXTENSION_NAME)) + goto error; + + p->vo_mac = [[MacCommon alloc] init:ctx->vo]; + if (!p->vo_mac) + goto error; + + VkMetalSurfaceCreateInfoEXT mac_info = { + .sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK, + .pNext = NULL, + .flags = 0, + .pLayer = p->vo_mac.layer, + }; + + struct ra_vk_ctx_params params = { + .swap_buffers = mac_vk_swap_buffers, + }; + + VkInstance inst = vk->vkinst->instance; + VkResult res = vkCreateMetalSurfaceEXT(inst, &mac_info, NULL, &vk->surface); + if (res != VK_SUCCESS) { + MP_MSG(ctx, msgl, "Failed creating metal surface\n"); + goto error; + } + + if (!ra_vk_ctx_init(ctx, vk, params, VK_PRESENT_MODE_FIFO_KHR)) + goto error; + + return true; +error: + if (p->vo_mac) + [p->vo_mac uninit:ctx->vo]; + return false; +} + +static bool resize(struct ra_ctx *ctx) +{ + return ra_vk_ctx_resize(ctx, ctx->vo->dwidth, ctx->vo->dheight); +} + +static bool mac_vk_reconfig(struct ra_ctx *ctx) +{ + struct priv *p = ctx->priv; + if (![p->vo_mac config:ctx->vo]) + return false; + return true; +} + +static int mac_vk_control(struct ra_ctx *ctx, int *events, int request, void *arg) +{ + struct priv *p = ctx->priv; + int ret = [p->vo_mac control:ctx->vo events:events request:request data:arg]; + + if (*events & VO_EVENT_RESIZE) { + if (!resize(ctx)) + return VO_ERROR; + } + + return ret; +} + +const struct ra_ctx_fns ra_ctx_vulkan_mac = { + .type = "vulkan", + .name = "macvk", + .reconfig = mac_vk_reconfig, + .control = mac_vk_control, + .init = mac_vk_init, + .uninit = mac_vk_uninit, +}; |