2023-09-30 14:01:04 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2024-03-27 22:22:56 +00:00
|
|
|
#import <QuartzCore/QuartzCore.h>
|
|
|
|
|
2023-09-30 14:01:04 +00:00
|
|
|
#include "video/out/gpu/context.h"
|
2024-02-27 15:55:29 +00:00
|
|
|
#include "osdep/mac/swift.h"
|
2023-09-30 14:01:04 +00:00
|
|
|
|
|
|
|
#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];
|
|
|
|
}
|
|
|
|
|
2024-04-06 21:31:05 +00:00
|
|
|
static void mac_vk_get_vsync(struct ra_ctx *ctx, struct vo_vsync_info *info)
|
|
|
|
{
|
|
|
|
struct priv *p = ctx->priv;
|
|
|
|
[p->vo_mac fillVsyncWithInfo:info];
|
|
|
|
}
|
|
|
|
|
2023-09-30 14:01:04 +00:00
|
|
|
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;
|
|
|
|
|
2024-07-13 12:10:04 +00:00
|
|
|
if (!NSApp) {
|
|
|
|
MP_ERR(ctx, "Failed to initialize macvk context, no NSApplication initialized.\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2023-09-30 14:01:04 +00:00
|
|
|
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 = {
|
2024-02-29 00:54:46 +00:00
|
|
|
.sType = VK_STRUCTURE_TYPE_METAL_SURFACE_CREATE_INFO_EXT,
|
2023-09-30 14:01:04 +00:00
|
|
|
.pNext = NULL,
|
|
|
|
.flags = 0,
|
|
|
|
.pLayer = p->vo_mac.layer,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ra_vk_ctx_params params = {
|
|
|
|
.swap_buffers = mac_vk_swap_buffers,
|
2024-04-06 21:31:05 +00:00
|
|
|
.get_vsync = mac_vk_get_vsync,
|
2023-09-30 14:01:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2024-03-06 21:02:43 +00:00
|
|
|
struct priv *p = ctx->priv;
|
|
|
|
|
|
|
|
if (!p->vo_mac.window) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
CGSize size = p->vo_mac.window.framePixel.size;
|
|
|
|
|
|
|
|
return ra_vk_ctx_resize(ctx, (int)size.width, (int)size.height);
|
2023-09-30 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
2024-04-16 03:18:44 +00:00
|
|
|
.description = "mac/Vulkan (via Metal)",
|
2023-09-30 14:01:04 +00:00
|
|
|
.reconfig = mac_vk_reconfig,
|
|
|
|
.control = mac_vk_control,
|
|
|
|
.init = mac_vk_init,
|
|
|
|
.uninit = mac_vk_uninit,
|
|
|
|
};
|