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"
|
2022-06-10 16:49:28 +00:00
|
|
|
#include "video/out/present_sync.h"
|
2017-09-16 02:46:38 +00:00
|
|
|
#include "video/out/wayland_common.h"
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "context.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
struct priv {
|
|
|
|
struct mpvk_ctx vk;
|
|
|
|
};
|
|
|
|
|
2021-11-04 14:42:54 +00:00
|
|
|
static bool wayland_vk_check_visible(struct ra_ctx *ctx)
|
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
|
|
|
{
|
2022-04-07 04:01:21 +00:00
|
|
|
return vo_wayland_check_visible(ctx->vo);
|
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
|
|
|
}
|
|
|
|
|
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;
|
wayland: shuffle around the render loop again
Take two. f4e89dd went wrong by moving vo_wayland_wait_frame before
start_frame was called. Whether or not this matters depends on the
compositor, but some weird things can happen. Basically, it's a
scheduling issue. vo_wayland_wait_frame queues all events and sends them
to the server to process (with no blocking if presentation time is
available). If mpv changes state while rendering (and this function is
called before every frame is drawn), then that event also gets
dispatched and sent to the compositor. This, in some cases, can cause
some funny behavior because the next frame gets attached to the surface
while the old buffer is getting released. It's safer to call this
function after the swap already happens and well before mpv calls its
next draw. There's no weird scheduling of events, and the compositor log
is more normal.
The second part of this is to fix some stuttering issues. This is mostly
just conjecture, but probably what was happening was this thing called
"composition". The easiest way to see this is to play a video on the
default audio sync mode (probably easiest to see on a typical 23.976
video). Have that in a window and float it over firefox (floating
windows are bloat on a tiling wm anyway). Then in firefox, do some short
bursts of smooth scrolling (likely uses egl). Some stutter in video
rendering could be observed, particularly in panning shots.
Compositors are supposed to prevent tearing so what likely was happening
was that the compositor was simply holding the buffer a wee bit longer
to make sure it happened in sync with the smooth scrolling. Because the
mpv code waits precisely on presentation time, the loop would timeout on
occasion instead of receiving the frame callback. This would then lead
to a skipped frame when rendering and thus causing stuttering.
The fix is simple: just only count consecutive timeouts as not receiving
frame callback. If a compositor holds the mpv buffer slightly longer to
avoid tearing, then we will definitely receive frame callback on the
next round of the render loop. This logic also appears to be sound for
plasma (funfact: Plasma always returns frame callback even when the
window is hidden. Not sure what's up with that, but luckily it doesn't
matter to us.), so get rid of the goofy 1/vblank_time thing and just
keep it a simple > 1 check.
2021-05-23 19:36:19 +00:00
|
|
|
|
|
|
|
if (!wl->opts->disable_vsync)
|
|
|
|
vo_wayland_wait_frame(wl);
|
|
|
|
|
2022-10-04 16:05:00 +00:00
|
|
|
if (wl->use_present)
|
2022-06-10 16:49:28 +00:00
|
|
|
present_sync_swap(wl->present);
|
2019-10-10 19:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void wayland_vk_get_vsync(struct ra_ctx *ctx, struct vo_vsync_info *info)
|
|
|
|
{
|
|
|
|
struct vo_wayland_state *wl = ctx->vo->wl;
|
2022-10-04 16:05:00 +00:00
|
|
|
if (wl->use_present)
|
2022-06-10 16:49:28 +00:00
|
|
|
present_sync_get_info(wl->present, info);
|
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 = {
|
2021-11-04 14:42:54 +00:00
|
|
|
.check_visible = wayland_vk_check_visible,
|
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
|
|
|
|
2023-01-22 23:06:15 +00:00
|
|
|
const int32_t width = mp_rect_w(wl->geometry);
|
|
|
|
const int32_t height = 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);
|
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
|
|
|
{
|
2023-01-08 20:02:13 +00:00
|
|
|
return vo_wayland_reconfig(ctx->vo);
|
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
|
|
|
};
|