2017-09-16 02:46:38 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "video/out/gpu/context.h"
|
|
|
|
#include "video/out/wayland_common.h"
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "context.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
2019-10-10 19:14:40 +00:00
|
|
|
// Generated from presentation-time.xml
|
2020-06-04 21:12:09 +00:00
|
|
|
#include "generated/wayland/presentation-time.h"
|
2019-10-10 19:14:40 +00:00
|
|
|
|
2017-09-16 02:46:38 +00:00
|
|
|
struct priv {
|
|
|
|
struct mpvk_ctx vk;
|
|
|
|
};
|
|
|
|
|
wayland: only render if we have frame callback
Back in the olden days, mpv's wayland backend was driven by the frame
callback. This had several issues and was removed in favor of the
current approach which allowed some advanced features (like
display-resample and presentation time) to actually work properly.
However as a consequence, it meant that mpv always rendered, even if the
surface was hidden. Wayland people consider this "wasteful" (and well
they aren't wrong). This commit aims to avoid wasteful rendering by
doing some additional checks in the swapchain. There's three main parts
to this.
1. Wayland EGL now uses an external swapchain (like the drm context).
Before we start a new frame, we check to see if we are waiting on a
callback from the compositor. If there is no wait, then go ahead and
proceed to render the frame, swap buffers, and then initiate
vo_wayland_wait_frame to poll (with a timeout) for the next potential
callback. If we are still waiting on callback from the compositor when
starting a new frame, then we simple skip rendering it entirely until
the surface comes back into view.
2. Wayland on vulkan has essentially the same approach although the
details are a little different. The ra_vk_ctx does not have support for
an external swapchain and although such a mechanism could theoretically
be added, it doesn't make much sense with libplacebo. Instead,
start_frame was added as a param and used to check for callback.
3. For wlshm, it's simply a matter of adding frame callback to it,
leveraging vo_wayland_wait_frame, and using the frame callback value to
whether or not to draw the image.
2020-09-18 17:29:53 +00:00
|
|
|
static bool wayland_vk_start_frame(struct ra_ctx *ctx)
|
|
|
|
{
|
|
|
|
struct vo_wayland_state *wl = ctx->vo->wl;
|
|
|
|
|
2020-10-13 15:36:08 +00:00
|
|
|
bool render = !wl->hidden || wl->opts->disable_vsync;
|
wayland: only render if we have frame callback
Back in the olden days, mpv's wayland backend was driven by the frame
callback. This had several issues and was removed in favor of the
current approach which allowed some advanced features (like
display-resample and presentation time) to actually work properly.
However as a consequence, it meant that mpv always rendered, even if the
surface was hidden. Wayland people consider this "wasteful" (and well
they aren't wrong). This commit aims to avoid wasteful rendering by
doing some additional checks in the swapchain. There's three main parts
to this.
1. Wayland EGL now uses an external swapchain (like the drm context).
Before we start a new frame, we check to see if we are waiting on a
callback from the compositor. If there is no wait, then go ahead and
proceed to render the frame, swap buffers, and then initiate
vo_wayland_wait_frame to poll (with a timeout) for the next potential
callback. If we are still waiting on callback from the compositor when
starting a new frame, then we simple skip rendering it entirely until
the surface comes back into view.
2. Wayland on vulkan has essentially the same approach although the
details are a little different. The ra_vk_ctx does not have support for
an external swapchain and although such a mechanism could theoretically
be added, it doesn't make much sense with libplacebo. Instead,
start_frame was added as a param and used to check for callback.
3. For wlshm, it's simply a matter of adding frame callback to it,
leveraging vo_wayland_wait_frame, and using the frame callback value to
whether or not to draw the image.
2020-09-18 17:29:53 +00:00
|
|
|
|
|
|
|
if (wl->frame_wait && wl->presentation)
|
|
|
|
vo_wayland_sync_clear(wl);
|
|
|
|
|
|
|
|
if (render)
|
|
|
|
wl->frame_wait = true;
|
|
|
|
|
|
|
|
return render;
|
|
|
|
}
|
|
|
|
|
2020-08-15 21:07:53 +00:00
|
|
|
static void wayland_vk_swap_buffers(struct ra_ctx *ctx)
|
|
|
|
{
|
|
|
|
struct vo_wayland_state *wl = ctx->vo->wl;
|
|
|
|
|
2019-10-14 17:16:42 +00:00
|
|
|
if (!wl->opts->disable_vsync)
|
2020-01-30 17:19:22 +00:00
|
|
|
vo_wayland_wait_frame(wl);
|
2019-10-10 19:14:40 +00:00
|
|
|
|
wayland: only render if we have frame callback
Back in the olden days, mpv's wayland backend was driven by the frame
callback. This had several issues and was removed in favor of the
current approach which allowed some advanced features (like
display-resample and presentation time) to actually work properly.
However as a consequence, it meant that mpv always rendered, even if the
surface was hidden. Wayland people consider this "wasteful" (and well
they aren't wrong). This commit aims to avoid wasteful rendering by
doing some additional checks in the swapchain. There's three main parts
to this.
1. Wayland EGL now uses an external swapchain (like the drm context).
Before we start a new frame, we check to see if we are waiting on a
callback from the compositor. If there is no wait, then go ahead and
proceed to render the frame, swap buffers, and then initiate
vo_wayland_wait_frame to poll (with a timeout) for the next potential
callback. If we are still waiting on callback from the compositor when
starting a new frame, then we simple skip rendering it entirely until
the surface comes back into view.
2. Wayland on vulkan has essentially the same approach although the
details are a little different. The ra_vk_ctx does not have support for
an external swapchain and although such a mechanism could theoretically
be added, it doesn't make much sense with libplacebo. Instead,
start_frame was added as a param and used to check for callback.
3. For wlshm, it's simply a matter of adding frame callback to it,
leveraging vo_wayland_wait_frame, and using the frame callback value to
whether or not to draw the image.
2020-09-18 17:29:53 +00:00
|
|
|
if (wl->presentation)
|
2019-10-10 19:14:40 +00:00
|
|
|
wayland_sync_swap(wl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wayland_vk_get_vsync(struct ra_ctx *ctx, struct vo_vsync_info *info)
|
|
|
|
{
|
|
|
|
struct vo_wayland_state *wl = ctx->vo->wl;
|
2020-04-09 16:17:03 +00:00
|
|
|
if (wl->presentation) {
|
2019-10-10 19:14:40 +00:00
|
|
|
info->vsync_duration = wl->vsync_duration;
|
|
|
|
info->skipped_vsyncs = wl->last_skipped_vsyncs;
|
|
|
|
info->last_queue_display_time = wl->last_queue_display_time;
|
|
|
|
}
|
2019-10-07 20:58:36 +00:00
|
|
|
}
|
|
|
|
|
2017-10-01 20:16:49 +00:00
|
|
|
static void wayland_vk_uninit(struct ra_ctx *ctx)
|
2017-09-16 02:46:38 +00:00
|
|
|
{
|
|
|
|
struct priv *p = ctx->priv;
|
|
|
|
|
|
|
|
ra_vk_ctx_uninit(ctx);
|
|
|
|
mpvk_uninit(&p->vk);
|
|
|
|
vo_wayland_uninit(ctx->vo);
|
|
|
|
}
|
|
|
|
|
2017-10-01 20:16:49 +00:00
|
|
|
static bool wayland_vk_init(struct ra_ctx *ctx)
|
2017-09-16 02:46:38 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2018-11-10 11:53:33 +00:00
|
|
|
if (!mpvk_init(vk, ctx, VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME))
|
2017-09-16 02:46:38 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!vo_wayland_init(ctx->vo))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
VkWaylandSurfaceCreateInfoKHR wlinfo = {
|
|
|
|
.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR,
|
2017-10-01 20:16:49 +00:00
|
|
|
.display = ctx->vo->wl->display,
|
|
|
|
.surface = ctx->vo->wl->surface,
|
2017-09-16 02:46:38 +00:00
|
|
|
};
|
|
|
|
|
2019-10-07 20:58:36 +00:00
|
|
|
struct ra_vk_ctx_params params = {
|
wayland: only render if we have frame callback
Back in the olden days, mpv's wayland backend was driven by the frame
callback. This had several issues and was removed in favor of the
current approach which allowed some advanced features (like
display-resample and presentation time) to actually work properly.
However as a consequence, it meant that mpv always rendered, even if the
surface was hidden. Wayland people consider this "wasteful" (and well
they aren't wrong). This commit aims to avoid wasteful rendering by
doing some additional checks in the swapchain. There's three main parts
to this.
1. Wayland EGL now uses an external swapchain (like the drm context).
Before we start a new frame, we check to see if we are waiting on a
callback from the compositor. If there is no wait, then go ahead and
proceed to render the frame, swap buffers, and then initiate
vo_wayland_wait_frame to poll (with a timeout) for the next potential
callback. If we are still waiting on callback from the compositor when
starting a new frame, then we simple skip rendering it entirely until
the surface comes back into view.
2. Wayland on vulkan has essentially the same approach although the
details are a little different. The ra_vk_ctx does not have support for
an external swapchain and although such a mechanism could theoretically
be added, it doesn't make much sense with libplacebo. Instead,
start_frame was added as a param and used to check for callback.
3. For wlshm, it's simply a matter of adding frame callback to it,
leveraging vo_wayland_wait_frame, and using the frame callback value to
whether or not to draw the image.
2020-09-18 17:29:53 +00:00
|
|
|
.start_frame = wayland_vk_start_frame,
|
2019-10-07 20:58:36 +00:00
|
|
|
.swap_buffers = wayland_vk_swap_buffers,
|
2019-10-10 19:14:40 +00:00
|
|
|
.get_vsync = wayland_vk_get_vsync,
|
2019-10-07 20:58:36 +00:00
|
|
|
};
|
|
|
|
|
2018-11-10 11:53:33 +00:00
|
|
|
VkInstance inst = vk->vkinst->instance;
|
|
|
|
VkResult res = vkCreateWaylandSurfaceKHR(inst, &wlinfo, NULL, &vk->surface);
|
2017-09-16 02:46:38 +00:00
|
|
|
if (res != VK_SUCCESS) {
|
2018-11-10 11:53:33 +00:00
|
|
|
MP_MSG(ctx, msgl, "Failed creating Wayland surface\n");
|
2017-09-16 02:46:38 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Because in Wayland clients render whenever they receive a callback from
|
|
|
|
* the compositor, and the fact that the compositor usually stops sending
|
|
|
|
* callbacks once the surface is no longer visible, using FIFO here would
|
|
|
|
* mean the entire player would block on acquiring swapchain images. Hence,
|
|
|
|
* use MAILBOX to guarantee that there'll always be a swapchain image and
|
|
|
|
* the player won't block waiting on those */
|
2019-10-07 20:58:36 +00:00
|
|
|
if (!ra_vk_ctx_init(ctx, vk, params, VK_PRESENT_MODE_MAILBOX_KHR))
|
2017-09-16 02:46:38 +00:00
|
|
|
goto error;
|
|
|
|
|
2018-12-30 22:59:48 +00:00
|
|
|
ra_add_native_resource(ctx->ra, "wl", ctx->vo->wl->display);
|
|
|
|
|
2017-09-16 02:46:38 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
error:
|
2017-10-01 20:16:49 +00:00
|
|
|
wayland_vk_uninit(ctx);
|
2017-09-16 02:46:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-10 11:53:33 +00:00
|
|
|
static bool resize(struct ra_ctx *ctx)
|
2017-09-16 02:46:38 +00:00
|
|
|
{
|
2017-10-01 20:16:49 +00:00
|
|
|
struct vo_wayland_state *wl = ctx->vo->wl;
|
2017-09-16 02:46:38 +00:00
|
|
|
|
2017-10-09 01:08:36 +00:00
|
|
|
MP_VERBOSE(wl, "Handling resize on the vk side\n");
|
2017-09-16 02:46:38 +00:00
|
|
|
|
2020-10-05 15:28:37 +00:00
|
|
|
const int32_t width = wl->scaling * mp_rect_w(wl->geometry);
|
|
|
|
const int32_t height = wl->scaling * mp_rect_h(wl->geometry);
|
2020-10-01 16:00:58 +00:00
|
|
|
|
2020-10-05 15:28:37 +00:00
|
|
|
vo_wayland_set_opaque_region(wl, ctx->opts.want_alpha);
|
2017-10-01 20:16:49 +00:00
|
|
|
wl_surface_set_buffer_scale(wl->surface, wl->scaling);
|
2020-11-08 15:51:52 +00:00
|
|
|
return ra_vk_ctx_resize(ctx, width, height);
|
2017-09-16 02:46:38 +00:00
|
|
|
}
|
|
|
|
|
2017-10-01 20:16:49 +00:00
|
|
|
static bool wayland_vk_reconfig(struct ra_ctx *ctx)
|
2017-09-16 02:46:38 +00:00
|
|
|
{
|
2017-10-01 20:16:49 +00:00
|
|
|
if (!vo_wayland_reconfig(ctx->vo))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2017-09-16 02:46:38 +00:00
|
|
|
}
|
|
|
|
|
2017-10-01 20:16:49 +00:00
|
|
|
static int wayland_vk_control(struct ra_ctx *ctx, int *events, int request, void *arg)
|
2017-09-16 02:46:38 +00:00
|
|
|
{
|
|
|
|
int ret = vo_wayland_control(ctx->vo, events, request, arg);
|
|
|
|
if (*events & VO_EVENT_RESIZE) {
|
2018-11-10 11:53:33 +00:00
|
|
|
if (!resize(ctx))
|
2017-09-16 02:46:38 +00:00
|
|
|
return VO_ERROR;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-10-01 20:16:49 +00:00
|
|
|
static void wayland_vk_wakeup(struct ra_ctx *ctx)
|
2017-09-16 02:46:38 +00:00
|
|
|
{
|
|
|
|
vo_wayland_wakeup(ctx->vo);
|
|
|
|
}
|
|
|
|
|
2017-10-01 20:16:49 +00:00
|
|
|
static void wayland_vk_wait_events(struct ra_ctx *ctx, int64_t until_time_us)
|
2017-09-16 02:46:38 +00:00
|
|
|
{
|
|
|
|
vo_wayland_wait_events(ctx->vo, until_time_us);
|
|
|
|
}
|
|
|
|
|
2020-10-05 15:28:37 +00:00
|
|
|
static void wayland_vk_update_render_opts(struct ra_ctx *ctx)
|
|
|
|
{
|
|
|
|
struct vo_wayland_state *wl = ctx->vo->wl;
|
|
|
|
vo_wayland_set_opaque_region(wl, ctx->opts.want_alpha);
|
|
|
|
wl_surface_commit(wl->surface);
|
|
|
|
}
|
|
|
|
|
2017-09-16 02:46:38 +00:00
|
|
|
const struct ra_ctx_fns ra_ctx_vulkan_wayland = {
|
2020-10-05 15:28:37 +00:00
|
|
|
.type = "vulkan",
|
|
|
|
.name = "waylandvk",
|
|
|
|
.reconfig = wayland_vk_reconfig,
|
|
|
|
.control = wayland_vk_control,
|
|
|
|
.wakeup = wayland_vk_wakeup,
|
|
|
|
.wait_events = wayland_vk_wait_events,
|
|
|
|
.update_render_opts = wayland_vk_update_render_opts,
|
|
|
|
.init = wayland_vk_init,
|
|
|
|
.uninit = wayland_vk_uninit,
|
2017-09-16 02:46:38 +00:00
|
|
|
};
|