xref: /OK3568_Linux_fs/buildroot/package/weston/0042-backend-drm-Support-modifier.patch (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1From 96d5f6823314a2a1a2355d5a72dd2c95c5311ca8 Mon Sep 17 00:00:00 2001
2From: Jeffy Chen <jeffy.chen@rock-chips.com>
3Date: Wed, 6 Jan 2021 04:11:48 +0800
4Subject: [PATCH 42/95] backend-drm: Support modifier
5
6Tested on rk356x with ARM AFBC modifier.
7
8Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com>
9---
10 libweston/backend-drm/drm-internal.h |   1 +
11 libweston/backend-drm/drm.c          | 100 +++++++++++++++++++++------
12 libweston/backend-drm/kms.c          |   3 +-
13 libweston/pixel-formats.c            |   1 +
14 4 files changed, 84 insertions(+), 21 deletions(-)
15
16diff --git a/libweston/backend-drm/drm-internal.h b/libweston/backend-drm/drm-internal.h
17index e175f16..12ee031 100644
18--- a/libweston/backend-drm/drm-internal.h
19+++ b/libweston/backend-drm/drm-internal.h
20@@ -534,6 +534,7 @@ struct drm_plane {
21 	struct weston_drm_format_array formats;
22
23 	bool can_scale;
24+	bool has_modifiers;
25 };
26
27 struct drm_connector {
28diff --git a/libweston/backend-drm/drm.c b/libweston/backend-drm/drm.c
29index 756ee00..97e3382 100644
30--- a/libweston/backend-drm/drm.c
31+++ b/libweston/backend-drm/drm.c
32@@ -1265,6 +1265,42 @@ err:
33 	return NULL;
34 }
35
36+static inline bool
37+drm_plane_has_modifier(struct drm_plane *plane, uint32_t format)
38+{
39+	struct weston_drm_format *fmt;
40+	const uint64_t *modifiers;
41+	unsigned int num_modifiers, i;
42+
43+	fmt = weston_drm_format_array_find_format(&plane->formats, format);
44+	if (!fmt)
45+		return false;
46+
47+	modifiers = weston_drm_format_get_modifiers(fmt, &num_modifiers);
48+	for (i = 0; i < num_modifiers; i++) {
49+		if (DRM_MOD_VALID(modifiers[i]))
50+			return true;
51+	}
52+
53+	return false;
54+}
55+
56+static inline bool
57+drm_planes_have_modifier(struct drm_device *device)
58+{
59+	struct drm_plane *plane;
60+
61+	if (!device->fb_modifiers)
62+		return false;
63+
64+	wl_list_for_each_reverse(plane, &device->plane_list, link) {
65+		if (plane->has_modifiers)
66+			return true;
67+	}
68+
69+	return false;
70+}
71+
72 /**
73  * Find, or create, a special-purpose plane
74  *
75@@ -1279,7 +1315,10 @@ drm_output_find_special_plane(struct drm_device *device,
76 {
77 	struct drm_backend *b = device->backend;
78 	struct drm_plane *plane;
79+	bool prefer_modifier =
80+		device->fb_modifiers && type == WDRM_PLANE_TYPE_PRIMARY;
81
82+retry:
83 	wl_list_for_each(plane, &device->plane_list, link) {
84 		struct weston_output *base;
85 		bool found_elsewhere = false;
86@@ -1311,10 +1350,22 @@ drm_output_find_special_plane(struct drm_device *device,
87 		if (found_elsewhere)
88 			continue;
89
90+		if (prefer_modifier &&
91+		    !drm_plane_has_modifier(plane, output->gbm_format))
92+			continue;
93+
94+		if (plane->type != type)
95+			continue;
96+
97 		plane->possible_crtcs = (1 << output->crtc->pipe);
98 		return plane;
99 	}
100
101+	if (prefer_modifier) {
102+		prefer_modifier = false;
103+		goto retry;
104+	}
105+
106 	return NULL;
107 }
108
109@@ -4271,9 +4322,6 @@ drm_backend_create(struct weston_compositor *compositor,
110
111 	compositor->backend = &b->base;
112
113-	if (parse_gbm_format(config->gbm_format, DRM_FORMAT_XRGB8888, &b->gbm_format) < 0)
114-		goto err_compositor;
115-
116 	/* Check if we run drm-backend using a compatible launcher */
117 	compositor->launcher = weston_launcher_connect(compositor, seat_id, true);
118 	if (compositor->launcher == NULL) {
119@@ -4306,15 +4354,42 @@ drm_backend_create(struct weston_compositor *compositor,
120 		goto err_udev_dev;
121 	}
122
123+	res = drmModeGetResources(b->drm->drm.fd);
124+	if (!res) {
125+		weston_log("Failed to get drmModeRes\n");
126+		goto err_udev_dev;
127+	}
128+
129+	wl_list_init(&b->drm->crtc_list);
130+	if (drm_backend_create_crtc_list(b->drm, res) == -1) {
131+		weston_log("Failed to create CRTC list for DRM-backend\n");
132+		goto err_create_crtc_list;
133+	}
134+
135+	wl_list_init(&device->plane_list);
136+	create_sprites(b->drm, res);
137+
138+	if (!drm_planes_have_modifier(b->drm))
139+		device->fb_modifiers = false;
140+
141+	b->gbm_format = DRM_FORMAT_XRGB8888;
142+
143+	/* HACK: The modifiers only work with xbgr8888 now */
144+	if (device->fb_modifiers)
145+		b->gbm_format = DRM_FORMAT_XBGR8888;
146+
147+	if (parse_gbm_format(config->gbm_format, b->gbm_format, &b->gbm_format) < 0)
148+		goto err_sprite;
149+
150 	if (b->use_pixman) {
151 		if (init_pixman(b) < 0) {
152 			weston_log("failed to initialize pixman renderer\n");
153-			goto err_udev_dev;
154+			goto err_sprite;
155 		}
156 	} else {
157 		if (init_egl(b) < 0) {
158 			weston_log("failed to initialize egl\n");
159-			goto err_udev_dev;
160+			goto err_sprite;
161 		}
162 	}
163
164@@ -4328,21 +4403,6 @@ drm_backend_create(struct weston_compositor *compositor,
165
166 	weston_setup_vt_switch_bindings(compositor);
167
168-	res = drmModeGetResources(b->drm->drm.fd);
169-	if (!res) {
170-		weston_log("Failed to get drmModeRes\n");
171-		goto err_udev_dev;
172-	}
173-
174-	wl_list_init(&b->drm->crtc_list);
175-	if (drm_backend_create_crtc_list(b->drm, res) == -1) {
176-		weston_log("Failed to create CRTC list for DRM-backend\n");
177-		goto err_create_crtc_list;
178-	}
179-
180-	wl_list_init(&device->plane_list);
181-	create_sprites(b->drm, res);
182-
183 	if (udev_input_init(&b->input,
184 			    compositor, b->udev, seat_id,
185 			    config->configure_device) < 0) {
186diff --git a/libweston/backend-drm/kms.c b/libweston/backend-drm/kms.c
187index 3d97df2..52f0c68 100644
188--- a/libweston/backend-drm/kms.c
189+++ b/libweston/backend-drm/kms.c
190@@ -38,6 +38,7 @@
191
192 #include <libweston/libweston.h>
193 #include <libweston/backend-drm.h>
194+#include <libweston/linux-dmabuf.h>
195 #include "shared/helpers.h"
196 #include "shared/weston-drm-fourcc.h"
197 #include "drm-internal.h"
198@@ -1618,7 +1619,7 @@ init_kms_caps(struct drm_device *device)
199 	weston_log("DRM: %s atomic modesetting\n",
200 		   device->atomic_modeset ? "supports" : "does not support");
201
202-	if (!getenv("WESTON_DISABLE_GBM_MODIFIERS")) {
203+	if (getenv("WESTON_ALLOW_GBM_MODIFIERS")) {
204 		ret = drmGetCap(device->drm.fd, DRM_CAP_ADDFB2_MODIFIERS, &cap);
205 		if (ret == 0)
206 			device->fb_modifiers = cap;
207diff --git a/libweston/pixel-formats.c b/libweston/pixel-formats.c
208index 10b3ed9..a7ae8c0 100644
209--- a/libweston/pixel-formats.c
210+++ b/libweston/pixel-formats.c
211@@ -261,6 +261,7 @@ static const struct pixel_format_info pixel_format_table[] = {
212 	{
213 		DRM_FORMAT(XBGR8888),
214 		BITS_RGBA_FIXED(8, 8, 8, 0),
215+		.addfb_legacy_depth = 24,
216 		.bpp = 32,
217 		GL_FORMAT(GL_RGBA),
218 		GL_TYPE(GL_UNSIGNED_BYTE),
219--
2202.20.1
221
222