summaryrefslogtreecommitdiffstats
path: root/video/out/wayland_common.c
diff options
context:
space:
mode:
authordudemanguy <random342@airmail.cc>2019-10-07 15:58:36 -0500
committerDudemanguy911 <random342@airmail.cc>2019-10-10 17:41:19 +0000
commitea4685b233090bb155cfa4ec6bf87f421a553168 (patch)
tree35e1f69c4fa348401a6bbab877a2fb096910ba6b /video/out/wayland_common.c
parente0895e097bcf15cdabc93f340dae24a88b0dc89c (diff)
downloadmpv-ea4685b233090bb155cfa4ec6bf87f421a553168.tar.bz2
mpv-ea4685b233090bb155cfa4ec6bf87f421a553168.tar.xz
wayland: use callback flag + poll for buffer swap
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.
Diffstat (limited to 'video/out/wayland_common.c')
-rw-r--r--video/out/wayland_common.c46
1 files changed, 27 insertions, 19 deletions
diff --git a/video/out/wayland_common.c b/video/out/wayland_common.c
index cc07296a43..fa409a5541 100644
--- a/video/out/wayland_common.c
+++ b/video/out/wayland_common.c
@@ -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)
{
@@ -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++) {
@@ -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;