From d2acd4c36271ed87c7f8849a4f3c957fb36666fa Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Wed, 14 Feb 2024 18:30:16 +0100 Subject: [PATCH] backend/drm: move restore logic to drm.c We'll need to use a bunch of internal functions from that file in the next commits. --- backend/drm/backend.c | 39 +----------------------------------- backend/drm/drm.c | 42 ++++++++++++++++++++++++++++++++++++++- include/backend/drm/drm.h | 3 +-- 3 files changed, 43 insertions(+), 41 deletions(-) diff --git a/backend/drm/backend.c b/backend/drm/backend.c index 43bfd9da..27e5585d 100644 --- a/backend/drm/backend.c +++ b/backend/drm/backend.c @@ -95,24 +95,6 @@ struct wlr_backend *wlr_drm_backend_get_parent(struct wlr_backend *backend) { return drm->parent ? &drm->parent->backend : NULL; } -static void build_current_connector_state(struct wlr_output_state *state, - struct wlr_drm_connector *conn) { - bool enabled = conn->status != DRM_MODE_DISCONNECTED && conn->output.enabled; - - wlr_output_state_init(state); - wlr_output_state_set_enabled(state, enabled); - if (!enabled) { - return; - } - - if (conn->output.current_mode != NULL) { - wlr_output_state_set_mode(state, conn->output.current_mode); - } else { - wlr_output_state_set_custom_mode(state, - conn->output.width, conn->output.height, conn->output.refresh); - } -} - static void handle_session_active(struct wl_listener *listener, void *data) { struct wlr_drm_backend *drm = wl_container_of(listener, drm, session_active); @@ -125,26 +107,7 @@ static void handle_session_active(struct wl_listener *listener, void *data) { } scan_drm_connectors(drm, NULL); - - // The previous DRM master leaves KMS in an undefined state. We need - // to restore out own state, but be careful to avoid invalid - // configurations. The connector/CRTC mapping may have changed, so - // first disable all CRTCs, then light up the ones we were using - // before the VT switch. - // TODO: better use the atomic API to improve restoration after a VT switch - if (!drm->iface->reset(drm)) { - wlr_log(WLR_ERROR, "Failed to reset state after VT switch"); - } - - struct wlr_drm_connector *conn; - wl_list_for_each(conn, &drm->connectors, link) { - struct wlr_output_state state; - build_current_connector_state(&state, conn); - if (!drm_connector_commit_state(conn, &state)) { - wlr_drm_conn_log(conn, WLR_ERROR, "Failed to restore state after VT switch"); - } - wlr_output_state_finish(&state); - } + restore_drm_device(drm); } static void handle_dev_change(struct wl_listener *listener, void *data) { diff --git a/backend/drm/drm.c b/backend/drm/drm.c index 10485a8a..10564305 100644 --- a/backend/drm/drm.c +++ b/backend/drm/drm.c @@ -766,7 +766,7 @@ bool drm_connector_supports_vrr(struct wlr_drm_connector *conn) { return true; } -bool drm_connector_commit_state(struct wlr_drm_connector *conn, +static bool drm_connector_commit_state(struct wlr_drm_connector *conn, const struct wlr_output_state *base) { struct wlr_drm_backend *drm = conn->backend; @@ -1720,6 +1720,46 @@ void scan_drm_leases(struct wlr_drm_backend *drm) { drmFree(list); } +static void build_current_connector_state(struct wlr_output_state *state, + struct wlr_drm_connector *conn) { + bool enabled = conn->status != DRM_MODE_DISCONNECTED && conn->output.enabled; + + wlr_output_state_init(state); + wlr_output_state_set_enabled(state, enabled); + if (!enabled) { + return; + } + + if (conn->output.current_mode != NULL) { + wlr_output_state_set_mode(state, conn->output.current_mode); + } else { + wlr_output_state_set_custom_mode(state, + conn->output.width, conn->output.height, conn->output.refresh); + } +} + +void restore_drm_device(struct wlr_drm_backend *drm) { + // The previous DRM master leaves KMS in an undefined state. We need + // to restore out own state, but be careful to avoid invalid + // configurations. The connector/CRTC mapping may have changed, so + // first disable all CRTCs, then light up the ones we were using + // before the VT switch. + // TODO: better use the atomic API to improve restoration after a VT switch + if (!drm->iface->reset(drm)) { + wlr_log(WLR_ERROR, "Failed to reset state after VT switch"); + } + + struct wlr_drm_connector *conn; + wl_list_for_each(conn, &drm->connectors, link) { + struct wlr_output_state state; + build_current_connector_state(&state, conn); + if (!drm_connector_commit_state(conn, &state)) { + wlr_drm_conn_log(conn, WLR_ERROR, "Failed to restore state after VT switch"); + } + wlr_output_state_finish(&state); + } +} + static int mhz_to_nsec(int mhz) { return 1000000000000LL / mhz; } diff --git a/include/backend/drm/drm.h b/include/backend/drm/drm.h index 4b75b8a4..e2c74a9f 100644 --- a/include/backend/drm/drm.h +++ b/include/backend/drm/drm.h @@ -185,10 +185,9 @@ void finish_drm_resources(struct wlr_drm_backend *drm); void scan_drm_connectors(struct wlr_drm_backend *state, struct wlr_device_hotplug_event *event); void scan_drm_leases(struct wlr_drm_backend *drm); +void restore_drm_device(struct wlr_drm_backend *drm); int handle_drm_event(int fd, uint32_t mask, void *data); void destroy_drm_connector(struct wlr_drm_connector *conn); -bool drm_connector_commit_state(struct wlr_drm_connector *conn, - const struct wlr_output_state *state); bool drm_connector_is_cursor_visible(struct wlr_drm_connector *conn); bool drm_connector_supports_vrr(struct wlr_drm_connector *conn); size_t drm_crtc_get_gamma_lut_size(struct wlr_drm_backend *drm,