From f8e02db4bcd297f6bef2387dc1a65b837619c07c Mon Sep 17 00:00:00 2001
From: Simon Ser <contact@emersion.fr>
Date: Thu, 7 May 2020 21:11:43 +0200
Subject: backend/drm: remove conn_enable from interface

Use crtc_commit instead.
---
 backend/drm/atomic.c | 76 ++++++++++++++++++++++++++--------------------------
 backend/drm/drm.c    | 32 +++++++++++++++-------
 backend/drm/legacy.c | 54 ++++++++++++++++++++-----------------
 3 files changed, 90 insertions(+), 72 deletions(-)

(limited to 'backend/drm')

diff --git a/backend/drm/atomic.c b/backend/drm/atomic.c
index 0b2cf8b3..0b258cfb 100644
--- a/backend/drm/atomic.c
+++ b/backend/drm/atomic.c
@@ -69,6 +69,22 @@ static void atomic_add(struct atomic *atom, uint32_t id, uint32_t prop, uint64_t
 	}
 }
 
+static bool create_mode_blob(struct wlr_drm_backend *drm,
+		struct wlr_drm_crtc *crtc, uint32_t *blob_id) {
+	if (!crtc->active) {
+		*blob_id = 0;
+		return true;
+	}
+
+	if (drmModeCreatePropertyBlob(drm->fd, &crtc->mode,
+			sizeof(drmModeModeInfo), blob_id)) {
+		wlr_log_errno(WLR_ERROR, "Unable to create mode property blob");
+		return false;
+	}
+
+	return true;
+}
+
 static bool create_gamma_lut_blob(struct wlr_drm_backend *drm,
 		struct wlr_drm_crtc *crtc, uint32_t *blob_id) {
 	if (crtc->gamma_table_size == 0) {
@@ -148,15 +164,12 @@ static bool atomic_crtc_commit(struct wlr_drm_backend *drm,
 		struct wlr_drm_connector *conn, uint32_t flags) {
 	struct wlr_drm_crtc *crtc = conn->crtc;
 
-	bool modeset = crtc->pending & WLR_DRM_CRTC_MODE;
-	if (modeset) {
+	if (crtc->pending & WLR_DRM_CRTC_MODE) {
 		if (crtc->mode_id != 0) {
 			drmModeDestroyPropertyBlob(drm->fd, crtc->mode_id);
 		}
 
-		if (drmModeCreatePropertyBlob(drm->fd, &crtc->mode,
-				sizeof(drmModeModeInfo), &crtc->mode_id)) {
-			wlr_log_errno(WLR_ERROR, "Unable to create mode property blob");
+		if (!create_mode_blob(drm, crtc, &crtc->mode_id)) {
 			return false;
 		}
 	}
@@ -179,6 +192,7 @@ static bool atomic_crtc_commit(struct wlr_drm_backend *drm,
 		}
 	}
 
+	bool modeset = crtc->pending & WLR_DRM_CRTC_MODE;
 	if (modeset) {
 		flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
 	} else {
@@ -187,20 +201,28 @@ static bool atomic_crtc_commit(struct wlr_drm_backend *drm,
 
 	struct atomic atom;
 	atomic_begin(crtc, &atom);
-	atomic_add(&atom, conn->id, conn->props.crtc_id, crtc->id);
-	if (modeset && conn->props.link_status != 0) {
+	atomic_add(&atom, conn->id, conn->props.crtc_id,
+		crtc->active ? crtc->id : 0);
+	if (modeset && crtc->active && conn->props.link_status != 0) {
 		atomic_add(&atom, conn->id, conn->props.link_status,
 			DRM_MODE_LINK_STATUS_GOOD);
 	}
 	atomic_add(&atom, crtc->id, crtc->props.mode_id, crtc->mode_id);
-	atomic_add(&atom, crtc->id, crtc->props.active, 1);
-	atomic_add(&atom, crtc->id, crtc->props.gamma_lut, crtc->gamma_lut);
-	set_plane_props(&atom, drm, crtc->primary, crtc->id, 0, 0);
-	if (crtc->cursor) {
-		if (crtc->cursor->cursor_enabled) {
-			set_plane_props(&atom, drm, crtc->cursor, crtc->id,
-				conn->cursor_x, conn->cursor_y);
-		} else {
+	atomic_add(&atom, crtc->id, crtc->props.active, crtc->active);
+	if (crtc->active) {
+		atomic_add(&atom, crtc->id, crtc->props.gamma_lut, crtc->gamma_lut);
+		set_plane_props(&atom, drm, crtc->primary, crtc->id, 0, 0);
+		if (crtc->cursor) {
+			if (crtc->cursor->cursor_enabled) {
+				set_plane_props(&atom, drm, crtc->cursor, crtc->id,
+					conn->cursor_x, conn->cursor_y);
+			} else {
+				plane_disable(&atom, crtc->cursor);
+			}
+		}
+	} else {
+		plane_disable(&atom, crtc->primary);
+		if (crtc->cursor) {
 			plane_disable(&atom, crtc->cursor);
 		}
 	}
@@ -215,33 +237,12 @@ static bool atomic_crtc_commit(struct wlr_drm_backend *drm,
 		return false;
 	}
 
-	if (crtc->cursor) {
+	if (crtc->active && crtc->cursor) {
 		drm_fb_move(&crtc->cursor->queued_fb, &crtc->cursor->pending_fb);
 	}
 	return true;
 }
 
-static bool atomic_conn_enable(struct wlr_drm_backend *drm,
-		struct wlr_drm_connector *conn, bool enable) {
-	struct wlr_drm_crtc *crtc = conn->crtc;
-	if (crtc == NULL) {
-		return !enable;
-	}
-
-	struct atomic atom;
-	atomic_begin(crtc, &atom);
-	atomic_add(&atom, crtc->id, crtc->props.active, enable);
-	if (enable) {
-		atomic_add(&atom, conn->id, conn->props.crtc_id, crtc->id);
-		atomic_add(&atom, crtc->id, crtc->props.mode_id, crtc->mode_id);
-	} else {
-		atomic_add(&atom, conn->id, conn->props.crtc_id, 0);
-		atomic_add(&atom, crtc->id, crtc->props.mode_id, 0);
-	}
-	return atomic_commit(drm->fd, &atom, conn, DRM_MODE_ATOMIC_ALLOW_MODESET,
-		true);
-}
-
 static bool atomic_crtc_set_cursor(struct wlr_drm_backend *drm,
 		struct wlr_drm_crtc *crtc, struct gbm_bo *bo) {
 	/* Cursor updates happen when we pageflip */
@@ -265,7 +266,6 @@ static size_t atomic_crtc_get_gamma_size(struct wlr_drm_backend *drm,
 }
 
 const struct wlr_drm_interface atomic_iface = {
-	.conn_enable = atomic_conn_enable,
 	.crtc_commit = atomic_crtc_commit,
 	.crtc_set_cursor = atomic_crtc_set_cursor,
 	.crtc_get_gamma_size = atomic_crtc_get_gamma_size,
diff --git a/backend/drm/drm.c b/backend/drm/drm.c
index 9c397752..ed01459b 100644
--- a/backend/drm/drm.c
+++ b/backend/drm/drm.c
@@ -334,10 +334,17 @@ static bool drm_connector_attach_render(struct wlr_output *output,
 	return drm_surface_make_current(&conn->crtc->primary->surf, buffer_age);
 }
 
-static bool drm_crtc_page_flip(struct wlr_drm_connector *conn) {
+static bool drm_crtc_commit(struct wlr_drm_connector *conn, uint32_t flags) {
 	struct wlr_drm_backend *drm =
 		get_drm_backend_from_backend(conn->output.backend);
 	struct wlr_drm_crtc *crtc = conn->crtc;
+	bool ok = drm->iface->crtc_commit(drm, conn, flags);
+	crtc->pending = 0;
+	return ok;
+}
+
+static bool drm_crtc_page_flip(struct wlr_drm_connector *conn) {
+	struct wlr_drm_crtc *crtc = conn->crtc;
 
 	if (conn->pageflip_pending) {
 		wlr_log(WLR_ERROR, "Failed to page-flip output '%s': "
@@ -345,11 +352,9 @@ static bool drm_crtc_page_flip(struct wlr_drm_connector *conn) {
 		return false;
 	}
 
-	bool ok = drm->iface->crtc_commit(drm, conn, DRM_MODE_PAGE_FLIP_EVENT);
-
-	crtc->pending = 0;
-
-	if (!ok) {
+	assert(crtc->active);
+	assert(plane_get_next_fb(crtc->primary)->type != WLR_DRM_FB_TYPE_NONE);
+	if (!drm_crtc_commit(conn, DRM_MODE_PAGE_FLIP_EVENT)) {
 		return false;
 	}
 
@@ -690,6 +695,7 @@ static void drm_connector_start_renderer(struct wlr_drm_connector *conn) {
 
 	struct wlr_drm_mode *mode = (struct wlr_drm_mode *)conn->output.current_mode;
 	memcpy(&crtc->mode, &mode->drm_mode, sizeof(drmModeModeInfo));
+	crtc->active = true;
 	crtc->pending |= WLR_DRM_CRTC_MODE;
 
 	if (!drm_connector_pageflip_renderer(conn)) {
@@ -721,6 +727,7 @@ static bool drm_connector_init_renderer(struct wlr_drm_connector *conn,
 
 	crtc->pending |= WLR_DRM_CRTC_MODE;
 	memcpy(&crtc->mode, &mode->drm_mode, sizeof(drmModeModeInfo));
+	crtc->active = true;
 
 	int width = mode->wlr_mode.width;
 	int height = mode->wlr_mode.height;
@@ -796,8 +803,14 @@ bool enable_drm_connector(struct wlr_output *output, bool enable) {
 		realloc_crtcs(drm);
 	}
 
-	bool ok = drm->iface->conn_enable(drm, conn, enable);
-	if (!ok) {
+	if (conn->crtc == NULL) {
+		wlr_output_update_enabled(&conn->output, false);
+		return !enable;
+	}
+
+	conn->crtc->active = enable;
+	conn->crtc->pending |= WLR_DRM_CRTC_MODE;
+	if (!drm_crtc_commit(conn, 0)) {
 		return false;
 	}
 
@@ -1112,11 +1125,10 @@ static void dealloc_crtc(struct wlr_drm_connector *conn) {
 		conn->crtc - drm->crtcs, conn->output.name);
 
 	set_drm_connector_gamma(&conn->output, 0, NULL, NULL, NULL);
+	enable_drm_connector(&conn->output, false);
 	drm_plane_finish_surface(conn->crtc->primary);
 	drm_plane_finish_surface(conn->crtc->cursor);
 
-	drm->iface->conn_enable(drm, conn, false);
-
 	conn->crtc = NULL;
 }
 
diff --git a/backend/drm/legacy.c b/backend/drm/legacy.c
index 4ca9d57c..ecf67092 100644
--- a/backend/drm/legacy.c
+++ b/backend/drm/legacy.c
@@ -11,21 +11,41 @@ static bool legacy_crtc_commit(struct wlr_drm_backend *drm,
 	struct wlr_drm_crtc *crtc = conn->crtc;
 	struct wlr_drm_plane *cursor = crtc->cursor;
 
-	struct wlr_drm_fb *fb = plane_get_next_fb(crtc->primary);
-	struct gbm_bo *bo = drm_fb_acquire(fb, drm, &crtc->primary->mgpu_surf);
-	if (!bo) {
-		return false;
-	}
+	uint32_t fb_id = 0;
+	if (crtc->active) {
+		struct wlr_drm_fb *fb = plane_get_next_fb(crtc->primary);
+		struct gbm_bo *bo = drm_fb_acquire(fb, drm, &crtc->primary->mgpu_surf);
+		if (!bo) {
+			return false;
+		}
 
-	uint32_t fb_id = get_fb_for_bo(bo, drm->addfb2_modifiers);
-	if (!fb_id) {
-		return false;
+		fb_id = get_fb_for_bo(bo, drm->addfb2_modifiers);
+		if (!fb_id) {
+			return false;
+		}
 	}
 
 	if (crtc->pending & WLR_DRM_CRTC_MODE) {
+		uint32_t *conns = NULL;
+		size_t conns_len = 0;
+		drmModeModeInfo *mode = NULL;
+		if (crtc->active) {
+			conns = &conn->id;
+			conns_len = 1;
+			mode = &crtc->mode;
+		}
+
+		if (drmModeConnectorSetProperty(drm->fd, conn->id, conn->props.dpms,
+				crtc->active ? DRM_MODE_DPMS_ON : DRM_MODE_DPMS_OFF) != 0) {
+			wlr_log_errno(WLR_ERROR, "%s: failed to set DPMS property",
+				conn->output.name);
+			return false;
+		}
+
 		if (drmModeSetCrtc(drm->fd, crtc->id, fb_id, 0, 0,
-				&conn->id, 1, &crtc->mode)) {
-			wlr_log_errno(WLR_ERROR, "%s: Failed to set CRTC", conn->output.name);
+				conns, conns_len, mode)) {
+			wlr_log_errno(WLR_ERROR, "%s: failed to set CRTC",
+				conn->output.name);
 			return false;
 		}
 	}
@@ -53,19 +73,6 @@ static bool legacy_crtc_commit(struct wlr_drm_backend *drm,
 	return true;
 }
 
-static bool legacy_conn_enable(struct wlr_drm_backend *drm,
-		struct wlr_drm_connector *conn, bool enable) {
-	int ret = drmModeConnectorSetProperty(drm->fd, conn->id, conn->props.dpms,
-		enable ? DRM_MODE_DPMS_ON : DRM_MODE_DPMS_OFF);
-
-	if (!enable) {
-		drmModeSetCrtc(drm->fd, conn->crtc->id, 0, 0, 0, NULL, 0,
-					   NULL);
-	}
-
-	return ret >= 0;
-}
-
 bool legacy_crtc_set_cursor(struct wlr_drm_backend *drm,
 		struct wlr_drm_crtc *crtc, struct gbm_bo *bo) {
 	if (!crtc || !crtc->cursor) {
@@ -116,7 +123,6 @@ static size_t legacy_crtc_get_gamma_size(struct wlr_drm_backend *drm,
 }
 
 const struct wlr_drm_interface legacy_iface = {
-	.conn_enable = legacy_conn_enable,
 	.crtc_commit = legacy_crtc_commit,
 	.crtc_set_cursor = legacy_crtc_set_cursor,
 	.crtc_get_gamma_size = legacy_crtc_get_gamma_size,
-- 
cgit v1.2.3