From efb0c5c4469ce4dc63fc3345293bdf7536e36581 Mon Sep 17 00:00:00 2001 From: Dudemanguy Date: Fri, 18 Sep 2020 12:29:53 -0500 Subject: 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. --- video/out/wayland_common.c | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) (limited to 'video/out/wayland_common.c') diff --git a/video/out/wayland_common.c b/video/out/wayland_common.c index c6c0e342c9..eb1e0f8c21 100644 --- a/video/out/wayland_common.c +++ b/video/out/wayland_common.c @@ -1005,6 +1005,11 @@ static void handle_toplevel_config(void *data, struct xdg_toplevel *toplevel, { wl->focused = !wl->focused; wl->pending_vo_events |= VO_EVENT_FOCUS; + + if (wl->activated) { + /* If the surface comes back into view, force a redraw. */ + wl->pending_vo_events |= VO_EVENT_EXPOSE; + } } } @@ -1607,6 +1612,13 @@ int vo_wayland_control(struct vo *vo, int *events, int request, void *arg) return VO_NOTIMPL; } +void vo_wayland_sync_clear(struct vo_wayland_state *wl) +{ + struct vo_wayland_sync sync = {0, 0, 0, 0}; + for (int i = 0; i < wl->sync_size; ++i) + wl->sync[i] = sync; +} + void vo_wayland_sync_shift(struct vo_wayland_state *wl) { for (int i = wl->sync_size - 1; i > 0; --i) { @@ -1630,7 +1642,6 @@ void queue_new_sync(struct vo_wayland_state *wl) wl->sync_size += 1; wl->sync = talloc_realloc(wl, wl->sync, struct vo_wayland_sync, wl->sync_size); vo_wayland_sync_shift(wl); - wl->sync[0].sbc = wl->user_sbc; } void wayland_sync_swap(struct vo_wayland_state *wl) @@ -1638,10 +1649,10 @@ void wayland_sync_swap(struct vo_wayland_state *wl) int index = wl->sync_size - 1; // If these are the same, presentation feedback has not been received. - // This will happen if the window is obscured/hidden in some way. Set - // these values to -1 to disable presentation feedback in mpv's core. + // This can happen if a frame takes too long and misses vblank. Don't + // attempt to use these statistics and wait until the next presentation + // event arrives. if (wl->sync[index].ust == wl->last_ust) { - wl->last_sbc += 1; wl->last_skipped_vsyncs = -1; wl->vsync_duration = -1; wl->last_queue_display_time = -1; @@ -1654,27 +1665,19 @@ void wayland_sync_swap(struct vo_wayland_state *wl) wl->last_ust = wl->sync[index].ust; int64_t msc_passed = wl->sync[index].msc ? wl->sync[index].msc - wl->last_msc: 0; wl->last_msc = wl->sync[index].msc; - int64_t sbc_passed = wl->sync[index].sbc ? wl->sync[index].sbc - wl->last_sbc: 0; - wl->last_sbc = wl->sync[index].sbc; if (msc_passed && ust_passed) wl->vsync_duration = ust_passed / msc_passed; - if (sbc_passed) { - struct timespec ts; - if (clock_gettime(CLOCK_MONOTONIC, &ts)) { - return; - } - - uint64_t now_monotonic = ts.tv_sec * 1000000LL + ts.tv_nsec / 1000; - uint64_t ust_mp_time = mp_time_us() - (now_monotonic - wl->sync[index].ust); - wl->last_sbc_mp_time = ust_mp_time; + struct timespec ts; + if (clock_gettime(CLOCK_MONOTONIC, &ts)) { + return; } - if (!wl->sync[index].sbc) - return; + uint64_t now_monotonic = ts.tv_sec * 1000000LL + ts.tv_nsec / 1000; + uint64_t ust_mp_time = mp_time_us() - (now_monotonic - wl->sync[index].ust); - wl->last_queue_display_time = wl->last_sbc_mp_time + sbc_passed*wl->vsync_duration; + wl->last_queue_display_time = ust_mp_time + wl->vsync_duration; } void vo_wayland_wakeup(struct vo *vo) -- cgit v1.2.3