Skip to content

Commit

Permalink
wayland: use callback flag + poll for buffer swap
Browse files Browse the repository at this point in the history
The old way of using wayland in mpv relied on an external renderloop for
semi-accurate timings. This had multiple issues though. Display sync
would break whenever the window was hidden (since the frame callback
stopped being executed) which was really annoying. Also the entire
external renderloop logic was kind of fragile and didn't play well with
mpv's internal structure (i.e. using presentation time in that old
paradigm breaks stats.lua).

Basically the problem is that swap buffers blocks on wayland which is
crap whenever you hide the mpv window since it looks up the entire
player. So you have to make swap buffers not block, but this has a
different problem. Timings will be terrible if you use the unblocked
swap buffers call.

Based on some discussion in #wayland, the trick here is relatively
simple and works well enough for our purposes. Instead we basically
build a way to block with a timeout in the wayland buffer swap
functions.

A bool is set in the frame callback function that indicates whether or
not mpv is waiting for a frame to be displayed. In the actual buffer
swap function, we enter into a while loop waiting for this flag to be
set. At the same time, the wl_display is polled to block the thread and
wakeup if it receives any events from the compositor. This loop only
breaks if enough time has passed or if the frame callback bool is
received.

In the near future, it is better to set whether or not frame a frame has
been displayed in the presentation feedback. However as a first pass,
doing it in the frame callback is more than good enough.

The "downside" is that we render frames that aren't actually shown on
screen when the player is hidden (it seems like wayland people don't
like that). But who cares. Accurate timings are way more important. It's
probably not too hard to add that behavior back in the player though.
  • Loading branch information
Dudemanguy committed Oct 10, 2019
1 parent e0895e0 commit ea4685b
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 26 deletions.
26 changes: 26 additions & 0 deletions video/out/opengl/context_wayland.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ struct priv {
struct wl_egl_window *egl_window;
};

static const struct wl_callback_listener frame_listener;

static void frame_callback(void *data, struct wl_callback *callback, uint32_t time)
{
struct vo_wayland_state *wl = data;

if (callback)
wl_callback_destroy(callback);

wl->frame_callback = wl_surface_frame(wl->surface);
wl_callback_add_listener(wl->frame_callback, &frame_listener, wl);
wl->callback_wait = false;
}

static const struct wl_callback_listener frame_listener = {
frame_callback,
};


static void resize(struct ra_ctx *ctx)
{
struct priv *p = ctx->priv;
Expand All @@ -56,7 +75,11 @@ static void resize(struct ra_ctx *ctx)
static void wayland_egl_swap_buffers(struct ra_ctx *ctx)
{
struct priv *p = ctx->priv;
struct vo_wayland_state *wl = ctx->vo->wl;

eglSwapBuffers(p->egl_display, p->egl_surface);
vo_wayland_wait_frame(wl);
wl->callback_wait = true;
}

static bool egl_create_context(struct ra_ctx *ctx)
Expand Down Expand Up @@ -87,6 +110,9 @@ static bool egl_create_context(struct ra_ctx *ctx)

ra_add_native_resource(ctx->ra, "wl", wl->display);

wl->frame_callback = wl_surface_frame(wl->surface);
wl_callback_add_listener(wl->frame_callback, &frame_listener, wl);

return true;
}

Expand Down
11 changes: 8 additions & 3 deletions video/out/vulkan/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ const struct m_sub_options vulkan_conf = {
struct priv {
struct mpvk_ctx *vk;
struct vulkan_opts *opts;
struct ra_vk_ctx_params params;
const struct pl_swapchain *swapchain;
struct ra_tex proxy_tex;
};
Expand Down Expand Up @@ -147,6 +148,7 @@ void ra_vk_ctx_uninit(struct ra_ctx *ctx)
}

bool ra_vk_ctx_init(struct ra_ctx *ctx, struct mpvk_ctx *vk,
struct ra_vk_ctx_params params,
VkPresentModeKHR preferred_mode)
{
struct ra_swapchain *sw = ctx->swapchain = talloc_zero(NULL, struct ra_swapchain);
Expand All @@ -155,6 +157,7 @@ bool ra_vk_ctx_init(struct ra_ctx *ctx, struct mpvk_ctx *vk,

struct priv *p = sw->priv = talloc_zero(sw, struct priv);
p->vk = vk;
p->params = params;
p->opts = mp_get_config_group(p, ctx->global, &vulkan_conf);

assert(vk->ctx);
Expand All @@ -176,16 +179,16 @@ bool ra_vk_ctx_init(struct ra_ctx *ctx, struct mpvk_ctx *vk,
goto error;

// Create the swapchain
struct pl_vulkan_swapchain_params params = {
struct pl_vulkan_swapchain_params pl_params = {
.surface = vk->surface,
.present_mode = preferred_mode,
.swapchain_depth = ctx->vo->opts->swapchain_depth,
};

if (p->opts->swap_mode >= 0) // user override
params.present_mode = p->opts->swap_mode;
pl_params.present_mode = p->opts->swap_mode;

p->swapchain = pl_vulkan_create_swapchain(vk->vulkan, &params);
p->swapchain = pl_vulkan_create_swapchain(vk->vulkan, &pl_params);
if (!p->swapchain)
goto error;

Expand Down Expand Up @@ -239,6 +242,8 @@ static void swap_buffers(struct ra_swapchain *sw)
{
struct priv *p = sw->priv;
pl_swapchain_swap_buffers(p->swapchain);
if (p->params.swap_buffers)
p->params.swap_buffers(sw->ctx);
}

static const struct ra_swapchain_fns vulkan_swapchain = {
Expand Down
6 changes: 6 additions & 0 deletions video/out/vulkan/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
#include "video/out/gpu/context.h"
#include "common.h"

struct ra_vk_ctx_params {
// In case something special needs to be done on the buffer swap.
void (*swap_buffers)(struct ra_ctx *ctx);
};

// Helpers for ra_ctx based on ra_vk. These initialize ctx->ra and ctx->swchain.
void ra_vk_ctx_uninit(struct ra_ctx *ctx);
bool ra_vk_ctx_init(struct ra_ctx *ctx, struct mpvk_ctx *vk,
struct ra_vk_ctx_params params,
VkPresentModeKHR preferred_mode);

// Handles a resize request, and updates ctx->vo->dwidth/dheight
Expand Down
4 changes: 3 additions & 1 deletion video/out/vulkan/context_android.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,16 @@ static bool android_init(struct ra_ctx *ctx)
.window = vo_android_native_window(ctx->vo)
};

struct ra_vk_ctx_params params = {0};

VkInstance inst = vk->vkinst->instance;
VkResult res = vkCreateAndroidSurfaceKHR(inst, &info, NULL, &vk->surface);
if (res != VK_SUCCESS) {
MP_MSG(ctx, msgl, "Failed creating Android surface\n");
goto fail;
}

if (!ra_vk_ctx_init(ctx, vk, VK_PRESENT_MODE_FIFO_KHR))
if (!ra_vk_ctx_init(ctx, vk, params, VK_PRESENT_MODE_FIFO_KHR))
goto fail;

return true;
Expand Down
35 changes: 34 additions & 1 deletion video/out/vulkan/context_wayland.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,32 @@ struct priv {
struct mpvk_ctx vk;
};

static const struct wl_callback_listener frame_listener;

static void frame_callback(void *data, struct wl_callback *callback, uint32_t time)
{
struct vo_wayland_state *wl = data;

if (callback)
wl_callback_destroy(callback);

wl->frame_callback = wl_surface_frame(wl->surface);
wl_callback_add_listener(wl->frame_callback, &frame_listener, wl);
wl->callback_wait = false;
}

static const struct wl_callback_listener frame_listener = {
frame_callback,
};

static void wayland_vk_swap_buffers(struct ra_ctx *ctx)
{
struct vo_wayland_state *wl = ctx->vo->wl;

vo_wayland_wait_frame(wl);
wl->callback_wait = true;
}

static void wayland_vk_uninit(struct ra_ctx *ctx)
{
struct priv *p = ctx->priv;
Expand Down Expand Up @@ -53,6 +79,10 @@ static bool wayland_vk_init(struct ra_ctx *ctx)
.surface = ctx->vo->wl->surface,
};

struct ra_vk_ctx_params params = {
.swap_buffers = wayland_vk_swap_buffers,
};

VkInstance inst = vk->vkinst->instance;
VkResult res = vkCreateWaylandSurfaceKHR(inst, &wlinfo, NULL, &vk->surface);
if (res != VK_SUCCESS) {
Expand All @@ -66,11 +96,14 @@ static bool wayland_vk_init(struct ra_ctx *ctx)
* 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 */
if (!ra_vk_ctx_init(ctx, vk, VK_PRESENT_MODE_MAILBOX_KHR))
if (!ra_vk_ctx_init(ctx, vk, params, VK_PRESENT_MODE_MAILBOX_KHR))
goto error;

ra_add_native_resource(ctx->ra, "wl", ctx->vo->wl->display);

ctx->vo->wl->frame_callback = wl_surface_frame(ctx->vo->wl->surface);
wl_callback_add_listener(ctx->vo->wl->frame_callback, &frame_listener, ctx->vo->wl);

return true;

error:
Expand Down
4 changes: 3 additions & 1 deletion video/out/vulkan/context_win.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@ static bool win_init(struct ra_ctx *ctx)
.hwnd = vo_w32_hwnd(ctx->vo),
};

struct ra_vk_ctx_params params = {0};

VkInstance inst = vk->vkinst->instance;
VkResult res = vkCreateWin32SurfaceKHR(inst, &wininfo, NULL, &vk->surface);
if (res != VK_SUCCESS) {
MP_MSG(ctx, msgl, "Failed creating Windows surface\n");
goto error;
}

if (!ra_vk_ctx_init(ctx, vk, VK_PRESENT_MODE_FIFO_KHR))
if (!ra_vk_ctx_init(ctx, vk, params, VK_PRESENT_MODE_FIFO_KHR))
goto error;

return true;
Expand Down
4 changes: 3 additions & 1 deletion video/out/vulkan/context_xlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@ static bool xlib_init(struct ra_ctx *ctx)
.window = ctx->vo->x11->window,
};

struct ra_vk_ctx_params params = {0};

VkInstance inst = vk->vkinst->instance;
VkResult res = vkCreateXlibSurfaceKHR(inst, &xinfo, NULL, &vk->surface);
if (res != VK_SUCCESS) {
MP_MSG(ctx, msgl, "Failed creating Xlib surface\n");
goto error;
}

if (!ra_vk_ctx_init(ctx, vk, VK_PRESENT_MODE_FIFO_KHR))
if (!ra_vk_ctx_init(ctx, vk, params, VK_PRESENT_MODE_FIFO_KHR))
goto error;

ra_add_native_resource(ctx->ra, "x11", ctx->vo->x11->display);
Expand Down
46 changes: 27 additions & 19 deletions video/out/wayland_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -796,23 +796,6 @@ static const struct wl_surface_listener surface_listener = {
surface_handle_leave,
};

static const struct wl_callback_listener frame_listener;

static void frame_callback(void *data, struct wl_callback *callback, uint32_t time)
{
struct vo_wayland_state *wl = data;

if (callback)
wl_callback_destroy(callback);

wl->frame_callback = wl_surface_frame(wl->surface);
wl_callback_add_listener(wl->frame_callback, &frame_listener, wl);
}

static const struct wl_callback_listener frame_listener = {
frame_callback,
};

static void registry_handle_add(void *data, struct wl_registry *reg, uint32_t id,
const char *interface, uint32_t ver)
{
Expand All @@ -824,8 +807,6 @@ static void registry_handle_add(void *data, struct wl_registry *reg, uint32_t id
wl->surface = wl_compositor_create_surface(wl->compositor);
wl->cursor_surface = wl_compositor_create_surface(wl->compositor);
wl_surface_add_listener(wl->surface, &surface_listener, wl);
wl->frame_callback = wl_surface_frame(wl->surface);
wl_callback_add_listener(wl->frame_callback, &frame_listener, wl);
}

if (!strcmp(interface, wl_output_interface.name) && (ver >= 2) && found++) {
Expand Down Expand Up @@ -1428,6 +1409,33 @@ void vo_wayland_wakeup(struct vo *vo)
(void)write(wl->wakeup_pipe[1], &(char){0}, 1);
}

void vo_wayland_wait_frame(struct vo_wayland_state *wl)
{
struct pollfd fds[1] = {
{.fd = wl->display_fd, .events = POLLIN },
};

double vblank_time = 1e6 / wl->current_output->refresh_rate;
int64_t finish_time = mp_time_us() + vblank_time;

while (wl->callback_wait && finish_time > mp_time_us()) {

while (wl_display_prepare_read(wl->display) != 0)
wl_display_dispatch_pending(wl->display);
wl_display_flush(wl->display);

int poll_time = (finish_time - mp_time_us()) / 1000;
if (poll_time < 0) {
poll_time = 0;
}

poll(fds, 1, poll_time);

wl_display_read_events(wl->display);
wl_display_dispatch_pending(wl->display);
}
}

void vo_wayland_wait_events(struct vo *vo, int64_t until_time_us)
{
struct vo_wayland_state *wl = vo->wl;
Expand Down
2 changes: 2 additions & 0 deletions video/out/wayland_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ struct vo_wayland_state {
bool fullscreen;
bool maximized;
bool configured;
bool callback_wait;
int wakeup_pipe[2];
int pending_vo_events;
int mouse_x;
Expand Down Expand Up @@ -112,5 +113,6 @@ void vo_wayland_check_events(struct vo *vo);
void vo_wayland_uninit(struct vo *vo);
void vo_wayland_wakeup(struct vo *vo);
void vo_wayland_wait_events(struct vo *vo, int64_t until_time_us);
void vo_wayland_wait_frame(struct vo_wayland_state *wl);

#endif /* MPLAYER_WAYLAND_COMMON_H */

0 comments on commit ea4685b

Please sign in to comment.