From ba9142274765869cd58ec0c55e7ef8da0354ed25 Mon Sep 17 00:00:00 2001 From: emersion Date: Fri, 5 Oct 2018 16:14:22 +0200 Subject: [PATCH 1/2] output: don't trigger a frame immediately in schedule_frame This desynchronizes our rendering loop with the vblank cycle. In case a compositor doesn't swap buffers but schedules a frame, emitting a frame event immediately enters a busy-loop. Instead, ask the backend to send a frame when appropriate. On Wayland we can just register a frame callback on our surface. On DRM we can do a no-op pageflip. Fixes #617 Fixes swaywm/sway#2748 --- backend/drm/drm.c | 38 +++++++++++++++++++++++++++++ backend/wayland/output.c | 16 +++++++++++- include/wlr/interfaces/wlr_output.h | 1 + types/wlr_output.c | 11 +++++++-- 4 files changed, 63 insertions(+), 3 deletions(-) diff --git a/backend/drm/drm.c b/backend/drm/drm.c index 0187e5d0..b9ab7f82 100644 --- a/backend/drm/drm.c +++ b/backend/drm/drm.c @@ -730,6 +730,43 @@ static bool drm_connector_move_cursor(struct wlr_output *output, return ok; } +static void drm_connector_schedule_frame(struct wlr_output *output) { + struct wlr_drm_connector *conn = get_drm_connector_from_output(output); + struct wlr_drm_backend *drm = get_drm_backend_from_backend(output->backend); + if (!drm->session->active) { + return; + } + + // We need to figure out where we are in the vblank cycle + // TODO: try using drmWaitVBlank and fallback to pageflipping + + struct wlr_drm_crtc *crtc = conn->crtc; + if (!crtc) { + return; + } + struct wlr_drm_plane *plane = crtc->primary; + struct gbm_bo *bo = plane->surf.back; + if (!bo) { + // We haven't swapped buffers yet -- can't do a pageflip + wlr_output_send_frame(output); + return; + } + uint32_t fb_id = get_fb_for_bo(bo); + + if (conn->pageflip_pending) { + wlr_log(WLR_ERROR, "Skipping pageflip on output '%s'", + conn->output.name); + return; + } + + if (!drm->iface->crtc_pageflip(drm, conn, crtc, fb_id, NULL)) { + return; + } + + conn->pageflip_pending = true; + wlr_output_update_enabled(output, true); +} + static void drm_connector_destroy(struct wlr_output *output) { struct wlr_drm_connector *conn = get_drm_connector_from_output(output); drm_connector_cleanup(conn); @@ -751,6 +788,7 @@ static const struct wlr_output_impl output_impl = { .set_gamma = set_drm_connector_gamma, .get_gamma_size = drm_connector_get_gamma_size, .export_dmabuf = drm_connector_export_dmabuf, + .schedule_frame = drm_connector_schedule_frame, }; bool wlr_output_is_drm(struct wlr_output *output) { diff --git a/backend/wayland/output.c b/backend/wayland/output.c index bc7a9f05..bc4067ab 100644 --- a/backend/wayland/output.c +++ b/backend/wayland/output.c @@ -192,11 +192,24 @@ void update_wl_output_cursor(struct wlr_wl_output *output) { } } -bool output_move_cursor(struct wlr_output *_output, int x, int y) { +static bool output_move_cursor(struct wlr_output *_output, int x, int y) { // TODO: only return true if x == current x and y == current y return true; } +static void output_schedule_frame(struct wlr_output *wlr_output) { + struct wlr_wl_output *output = get_wl_output_from_output(wlr_output); + + if (output->frame_callback != NULL) { + wlr_log(WLR_ERROR, "Skipping frame scheduling"); + return; + } + + output->frame_callback = wl_surface_frame(output->surface); + wl_callback_add_listener(output->frame_callback, &frame_listener, output); + wl_surface_commit(output->surface); +} + static const struct wlr_output_impl output_impl = { .set_custom_mode = output_set_custom_mode, .transform = output_transform, @@ -205,6 +218,7 @@ static const struct wlr_output_impl output_impl = { .swap_buffers = output_swap_buffers, .set_cursor = output_set_cursor, .move_cursor = output_move_cursor, + .schedule_frame = output_schedule_frame, }; bool wlr_output_is_wl(struct wlr_output *wlr_output) { diff --git a/include/wlr/interfaces/wlr_output.h b/include/wlr/interfaces/wlr_output.h index 52581768..da37cba1 100644 --- a/include/wlr/interfaces/wlr_output.h +++ b/include/wlr/interfaces/wlr_output.h @@ -33,6 +33,7 @@ struct wlr_output_impl { size_t (*get_gamma_size)(struct wlr_output *output); bool (*export_dmabuf)(struct wlr_output *output, struct wlr_dmabuf_attributes *attribs); + void (*schedule_frame)(struct wlr_output *output); }; void wlr_output_init(struct wlr_output *output, struct wlr_backend *backend, diff --git a/types/wlr_output.c b/types/wlr_output.c index b3840415..9b1e70ea 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -546,7 +546,13 @@ static void schedule_frame_handle_idle_timer(void *data) { struct wlr_output *output = data; output->idle_frame = NULL; if (!output->frame_pending) { - wlr_output_send_frame(output); + if (output->impl->schedule_frame) { + // Ask the backend to send a frame event when appropriate + output->frame_pending = true; + output->impl->schedule_frame(output); + } else { + wlr_output_send_frame(output); + } } } @@ -555,7 +561,8 @@ void wlr_output_schedule_frame(struct wlr_output *output) { return; } - // TODO: ask the backend to send a frame event when appropriate instead + // We're using an idle timer here in case a buffer swap happens right after + // this function is called struct wl_event_loop *ev = wl_display_get_event_loop(output->display); output->idle_frame = wl_event_loop_add_idle(ev, schedule_frame_handle_idle_timer, output); From 5afaccedfc50f722340500cf2cb2f1a84259831b Mon Sep 17 00:00:00 2001 From: emersion Date: Fri, 5 Oct 2018 16:26:40 +0200 Subject: [PATCH 2/2] output: fix busy loop when backend doesn't implement schedule_frame --- types/wlr_output.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/types/wlr_output.c b/types/wlr_output.c index 9b1e70ea..35a3ab14 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -545,14 +545,10 @@ void wlr_output_send_frame(struct wlr_output *output) { static void schedule_frame_handle_idle_timer(void *data) { struct wlr_output *output = data; output->idle_frame = NULL; - if (!output->frame_pending) { - if (output->impl->schedule_frame) { - // Ask the backend to send a frame event when appropriate - output->frame_pending = true; - output->impl->schedule_frame(output); - } else { - wlr_output_send_frame(output); - } + if (!output->frame_pending && output->impl->schedule_frame) { + // Ask the backend to send a frame event when appropriate + output->frame_pending = true; + output->impl->schedule_frame(output); } }