path: root/video/out/opengl/context_wayland.c
diff options
authorDudemanguy <>2020-09-18 12:29:53 -0500
committerDudemanguy <>2020-09-21 20:42:17 +0000
commitefb0c5c4469ce4dc63fc3345293bdf7536e36581 (patch)
tree051f59036b59009114b3f4da05043226fbdc85be /video/out/opengl/context_wayland.c
parent9ba90b4f6ff06bcc2a131237df9df1078c75d9d8 (diff)
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.
Diffstat (limited to 'video/out/opengl/context_wayland.c')
1 files changed, 26 insertions, 9 deletions
diff --git a/video/out/opengl/context_wayland.c b/video/out/opengl/context_wayland.c
index 410a490ee7..13040b54d8 100644
--- a/video/out/opengl/context_wayland.c
+++ b/video/out/opengl/context_wayland.c
@@ -73,7 +73,6 @@ static void feedback_presented(void *data, struct wp_presentation_feedback *fbac
index = 0;
int64_t sec = (uint64_t) tv_sec_lo + ((uint64_t) tv_sec_hi << 32);
- wl->sync[index].sbc = wl->user_sbc;
wl->sync[index].ust = sec * 1000000LL + (uint64_t) tv_nsec / 1000;
wl->sync[index].msc = (uint64_t) seq_lo + ((uint64_t) seq_hi << 32);
wl->sync[index].filled = true;
@@ -134,8 +133,25 @@ static void resize(struct ra_ctx *ctx)
wl->vo->dheight = height;
-static void wayland_egl_swap_buffers(struct ra_ctx *ctx)
+static bool wayland_egl_start_frame(struct ra_swapchain *sw, struct ra_fbo *out_fbo)
+ struct ra_ctx *ctx = sw->ctx;
+ struct vo_wayland_state *wl = ctx->vo->wl;
+ bool render = !wl->frame_wait || wl->opts->disable_vsync;
+ if (wl->frame_wait && wl->presentation)
+ vo_wayland_sync_clear(wl);
+ if (render)
+ wl->frame_wait = true;
+ return render ? ra_gl_ctx_start_frame(sw, out_fbo) : false;
+static void wayland_egl_swap_buffers(struct ra_swapchain *sw)
+ struct ra_ctx *ctx = sw->ctx;
struct priv *p = ctx->priv;
struct vo_wayland_state *wl = ctx->vo->wl;
@@ -144,14 +160,15 @@ static void wayland_egl_swap_buffers(struct ra_ctx *ctx)
if (!wl->opts->disable_vsync)
- if (wl->presentation) {
- wl->user_sbc += 1;
+ if (wl->presentation)
- }
- wl->frame_wait = true;
+static const struct ra_swapchain_fns wayland_egl_swapchain = {
+ .start_frame = wayland_egl_start_frame,
+ .swap_buffers = wayland_egl_swap_buffers,
static void wayland_egl_get_vsync(struct ra_ctx *ctx, struct vo_vsync_info *info)
struct vo_wayland_state *wl = ctx->vo->wl;
@@ -184,8 +201,8 @@ static bool egl_create_context(struct ra_ctx *ctx)
mpegl_load_functions(&p->gl, wl->log);
struct ra_gl_ctx_params params = {
- .swap_buffers = wayland_egl_swap_buffers,
- .get_vsync = wayland_egl_get_vsync,
+ .external_swapchain = &wayland_egl_swapchain,
+ .get_vsync = &wayland_egl_get_vsync,
if (!ra_gl_ctx_init(ctx, &p->gl, params))