blob: 9c901100566562bab6e9a6ff142b32f55d6da06e [file] [log] [blame]
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
Pekka Paalanen925788f2018-04-19 14:20:01 +03004 * Copyright © 2017, 2018 Collabora, Ltd.
5 * Copyright © 2017, 2018 General Electric Company
Pekka Paalanen62a94362018-09-26 14:33:36 +03006 * Copyright (c) 2018 DisplayLink (UK) Ltd.
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04007 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07008 * Permission is hereby granted, free of charge, to any person obtaining
9 * a copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sublicense, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040015 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070016 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial
18 * portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040028 */
29
Daniel Stonec228e232013-05-22 18:03:19 +030030#include "config.h"
Kristian Høgsberg0b9334a2011-04-12 11:34:32 -040031
Jesse Barnes58ef3792012-02-23 09:45:49 -050032#include <errno.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030033#include <stdint.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040034#include <stdlib.h>
Richard Hughes2b2092a2013-04-24 14:58:02 +010035#include <ctype.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040036#include <string.h>
37#include <fcntl.h>
38#include <unistd.h>
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -040039#include <linux/input.h>
Kristian Høgsberg3f495872013-09-18 23:00:17 -070040#include <linux/vt.h>
Ander Conselvan de Oliveirafd1f4c62012-06-26 17:09:14 +030041#include <assert.h>
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +020042#include <sys/mman.h>
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +030043#include <time.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040044
Benjamin Franzkec649a922011-03-02 11:56:04 +010045#include <xf86drm.h>
46#include <xf86drmMode.h>
47
Pekka Paalanen33156972012-08-03 13:30:30 -040048#include <libudev.h>
Benjamin Franzke060cf802011-04-30 09:32:11 +020049
Pekka Paalanen3d5d9472019-03-28 16:28:47 +020050#include <libweston/libweston.h>
Pekka Paalanen75710272019-03-29 16:39:12 +020051#include <libweston/backend-drm.h>
Marius Vladc901e892019-06-21 22:49:18 +030052#include <libweston/weston-log.h>
Daniel Stonedd1bc502019-06-17 12:13:46 +010053#include "drm-internal.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070054#include "shared/helpers.h"
Mario Kleinerf507ec32015-06-21 21:25:14 +020055#include "shared/timespec-util.h"
Ankit Nautiyala344fe32019-05-14 18:36:08 +053056#include "shared/string-helpers.h"
Pekka Paalanen4b301fe2021-02-04 17:39:45 +020057#include "shared/weston-drm-fourcc.h"
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +020058#include "pixman-renderer.h"
Daniel Stone0b70fa42017-04-04 17:54:23 +010059#include "pixel-formats.h"
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -070060#include "libbacklight.h"
Peter Hutterer823ad332014-11-26 07:06:31 +100061#include "libinput-seat.h"
Benjamin Franzkebfeda132012-01-30 14:04:04 +010062#include "launcher-util.h"
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +030063#include "vaapi-recorder.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020064#include "presentation-time-server-protocol.h"
Pekka Paalanene4d231e2014-06-12 15:12:48 +030065#include "linux-dmabuf.h"
Micah Fedkec8890122017-02-01 15:28:23 -050066#include "linux-dmabuf-unstable-v1-server-protocol.h"
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030067#include "linux-explicit-synchronization.h"
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040068
leng.fang32af9fc2024-06-13 11:22:15 +080069#include "aml-weston/aml-backend.h"
70
Kristian Høgsberg98cfea62013-02-18 16:15:53 -050071static const char default_seat[] = "seat0";
Pekka Paalanen33156972012-08-03 13:30:30 -040072
Daniel Stone087ddf02017-02-14 17:51:30 +000073static void
Marius Vlade83e7502019-10-29 17:29:37 +020074drm_backend_create_faked_zpos(struct drm_backend *b)
75{
76 struct drm_plane *plane;
77 uint64_t zpos = 0ULL;
78 uint64_t zpos_min_primary;
79 uint64_t zpos_min_overlay;
80 uint64_t zpos_min_cursor;
81
82 zpos_min_primary = zpos;
83 wl_list_for_each(plane, &b->plane_list, link) {
84 /* if the property is there, bail out sooner */
85 if (plane->props[WDRM_PLANE_ZPOS].prop_id != 0)
86 return;
87
88 if (plane->type != WDRM_PLANE_TYPE_PRIMARY)
89 continue;
90 zpos++;
91 }
92
93 zpos_min_overlay = zpos;
94 wl_list_for_each(plane, &b->plane_list, link) {
95 if (plane->type != WDRM_PLANE_TYPE_OVERLAY)
96 continue;
97 zpos++;
98 }
99
100 zpos_min_cursor = zpos;
101 wl_list_for_each(plane, &b->plane_list, link) {
102 if (plane->type != WDRM_PLANE_TYPE_CURSOR)
103 continue;
104 zpos++;
105 }
106
107 drm_debug(b, "[drm-backend] zpos property not found. "
108 "Using invented immutable zpos values:\n");
109 /* assume that invented zpos values are immutable */
110 wl_list_for_each(plane, &b->plane_list, link) {
111 if (plane->type == WDRM_PLANE_TYPE_PRIMARY) {
112 plane->zpos_min = zpos_min_primary;
113 plane->zpos_max = zpos_min_primary;
114 } else if (plane->type == WDRM_PLANE_TYPE_OVERLAY) {
115 plane->zpos_min = zpos_min_overlay;
116 plane->zpos_max = zpos_min_overlay;
117 } else if (plane->type == WDRM_PLANE_TYPE_CURSOR) {
118 plane->zpos_min = zpos_min_cursor;
119 plane->zpos_max = zpos_min_cursor;
120 }
121 drm_debug(b, "\t[plane] %s plane %d, zpos_min %"PRIu64", "
122 "zpos_max %"PRIu64"\n",
123 drm_output_get_plane_type_name(plane),
124 plane->plane_id, plane->zpos_min, plane->zpos_max);
125 }
126}
127
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000128static int
129pageflip_timeout(void *data) {
130 /*
131 * Our timer just went off, that means we're not receiving drm
132 * page flip events anymore for that output. Let's gracefully exit
133 * weston with a return value so devs can debug what's going on.
134 */
135 struct drm_output *output = data;
136 struct weston_compositor *compositor = output->base.compositor;
137
138 weston_log("Pageflip timeout reached on output %s, your "
139 "driver is probably buggy! Exiting.\n",
140 output->base.name);
141 weston_compositor_exit_with_code(compositor, EXIT_FAILURE);
142
143 return 0;
144}
145
146/* Creates the pageflip timer. Note that it isn't armed by default */
147static int
148drm_output_pageflip_timer_create(struct drm_output *output)
149{
150 struct wl_event_loop *loop = NULL;
151 struct weston_compositor *ec = output->base.compositor;
152
153 loop = wl_display_get_event_loop(ec->wl_display);
154 assert(loop);
155 output->pageflip_timer = wl_event_loop_add_timer(loop,
156 pageflip_timeout,
157 output);
158
159 if (output->pageflip_timer == NULL) {
Antonio Borneo39578632019-04-26 23:57:31 +0200160 weston_log("creating drm pageflip timer failed: %s\n",
161 strerror(errno));
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000162 return -1;
163 }
164
165 return 0;
166}
167
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000168static void
169drm_output_destroy(struct weston_output *output_base);
170
Daniel Stone5ff289a2017-10-07 12:59:02 +0100171/**
172 * Returns true if the plane can be used on the given output for its current
173 * repaint cycle.
174 */
Daniel Stonee404b722019-06-22 18:40:31 +0100175bool
Daniel Stone5ff289a2017-10-07 12:59:02 +0100176drm_plane_is_available(struct drm_plane *plane, struct drm_output *output)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500177{
Daniel Stone5ff289a2017-10-07 12:59:02 +0100178 assert(plane->state_cur);
179
Tomohito Esakib1fb00d2018-01-31 17:50:48 +0900180 if (output->virtual)
181 return false;
182
Daniel Stone5ff289a2017-10-07 12:59:02 +0100183 /* The plane still has a request not yet completed by the kernel. */
184 if (!plane->state_cur->complete)
185 return false;
186
187 /* The plane is still active on another output. */
leng.fang32af9fc2024-06-13 11:22:15 +0800188 if (plane->state_cur->output && plane->state_cur->output != output) {
189 //plane->state_cur->output = output;
190 //return false;
191 }
Daniel Stone5ff289a2017-10-07 12:59:02 +0100192 /* Check whether the plane can be used with this CRTC; possible_crtcs
193 * is a bitmask of CRTC indices (pipe), rather than CRTC object ID. */
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300194 return !!(plane->possible_crtcs & (1 << output->crtc->pipe));
Jesse Barnes58ef3792012-02-23 09:45:49 -0500195}
196
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300197struct drm_crtc *
198drm_crtc_find(struct drm_backend *b, uint32_t crtc_id)
Daniel Stone72c0e1b2017-02-09 13:49:15 +0000199{
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300200 struct drm_crtc *crtc;
Daniel Stone72c0e1b2017-02-09 13:49:15 +0000201
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300202 wl_list_for_each(crtc, &b->crtc_list, link) {
203 if (crtc->crtc_id == crtc_id)
204 return crtc;
Daniel Stone72c0e1b2017-02-09 13:49:15 +0000205 }
206
Daniel Stone72c0e1b2017-02-09 13:49:15 +0000207 return NULL;
208}
209
Daniel Stone4c2fc702019-06-18 11:12:07 +0100210struct drm_head *
Pekka Paalanen54cc47c2017-08-31 11:58:41 +0300211drm_head_find_by_connector(struct drm_backend *backend, uint32_t connector_id)
212{
213 struct weston_head *base;
214 struct drm_head *head;
215
216 wl_list_for_each(base,
217 &backend->compositor->head_list, compositor_link) {
218 head = to_drm_head(base);
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300219 if (head->connector.connector_id == connector_id)
Pekka Paalanen54cc47c2017-08-31 11:58:41 +0300220 return head;
221 }
222
223 return NULL;
224}
225
Leandro Ribeiro96bef052020-09-09 15:23:49 -0300226static struct drm_writeback *
227drm_writeback_find_by_connector(struct drm_backend *backend, uint32_t connector_id)
228{
229 struct drm_writeback *writeback;
230
231 wl_list_for_each(writeback, &backend->writeback_connector_list, link) {
232 if (writeback->connector.connector_id == connector_id)
233 return writeback;
234 }
235
236 return NULL;
237}
238
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000239/**
Daniel Stonea08512f2016-11-08 17:46:10 +0000240 * Get output state to disable output
241 *
242 * Returns a pointer to an output_state object which can be used to disable
243 * an output (e.g. DPMS off).
244 *
245 * @param pending_state The pending state object owning this update
246 * @param output The output to disable
247 * @returns A drm_output_state to disable the output
248 */
249static struct drm_output_state *
250drm_output_get_disable_state(struct drm_pending_state *pending_state,
251 struct drm_output *output)
252{
253 struct drm_output_state *output_state;
254
255 output_state = drm_output_state_duplicate(output->state_cur,
256 pending_state,
257 DRM_OUTPUT_STATE_CLEAR_PLANES);
258 output_state->dpms = WESTON_DPMS_OFF;
259
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530260 output_state->protection = WESTON_HDCP_DISABLE;
261
Daniel Stonea08512f2016-11-08 17:46:10 +0000262 return output_state;
263}
264
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000265
266/**
267 * Mark a drm_output_state (the output's last state) as complete. This handles
268 * any post-completion actions such as updating the repaint timer, disabling the
269 * output, and finally freeing the state.
270 */
Daniel Stone4c2fc702019-06-18 11:12:07 +0100271void
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000272drm_output_update_complete(struct drm_output *output, uint32_t flags,
273 unsigned int sec, unsigned int usec)
274{
Daniel Stonea08512f2016-11-08 17:46:10 +0000275 struct drm_backend *b = to_drm_backend(output->base.compositor);
Daniel Stonebc15f682016-11-14 16:57:01 +0000276 struct drm_plane_state *ps;
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000277 struct timespec ts;
278
279 /* Stop the pageflip timer instead of rearming it here */
280 if (output->pageflip_timer)
281 wl_event_source_timer_update(output->pageflip_timer, 0);
282
Daniel Stonebc15f682016-11-14 16:57:01 +0000283 wl_list_for_each(ps, &output->state_cur->plane_list, link)
284 ps->complete = true;
285
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000286 drm_output_state_free(output->state_last);
287 output->state_last = NULL;
288
289 if (output->destroy_pending) {
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +0100290 output->destroy_pending = false;
291 output->disable_pending = false;
292 output->dpms_off_pending = false;
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000293 drm_output_destroy(&output->base);
294 return;
295 } else if (output->disable_pending) {
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +0100296 output->disable_pending = false;
297 output->dpms_off_pending = false;
Daniel Stonea08512f2016-11-08 17:46:10 +0000298 weston_output_disable(&output->base);
299 return;
300 } else if (output->dpms_off_pending) {
301 struct drm_pending_state *pending = drm_pending_state_alloc(b);
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +0100302 output->dpms_off_pending = false;
Daniel Stonea08512f2016-11-08 17:46:10 +0000303 drm_output_get_disable_state(pending, output);
304 drm_pending_state_apply_sync(pending);
Michael Olbrichd70e7122020-08-06 09:57:54 +0200305 }
306 if (output->state_cur->dpms == WESTON_DPMS_OFF &&
307 output->base.repaint_status != REPAINT_AWAITING_COMPLETION) {
Daniel Stonea08512f2016-11-08 17:46:10 +0000308 /* DPMS can happen to us either in the middle of a repaint
309 * cycle (when we have painted fresh content, only to throw it
310 * away for DPMS off), or at any other random point. If the
311 * latter is true, then we cannot go through finish_frame,
312 * because the repaint machinery does not expect this. */
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000313 return;
314 }
315
316 ts.tv_sec = sec;
317 ts.tv_nsec = usec * 1000;
Derek Foreman74bdb352021-07-29 09:02:04 -0500318
319 if (output->state_cur->dpms != WESTON_DPMS_OFF)
320 weston_output_finish_frame(&output->base, &ts, flags);
321 else
322 weston_output_finish_frame(&output->base, NULL,
323 WP_PRESENTATION_FEEDBACK_INVALID);
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000324
325 /* We can't call this from frame_notify, because the output's
326 * repaint needed flag is cleared just after that */
327 if (output->recorder)
328 weston_output_schedule_repaint(&output->base);
329}
330
Daniel Stone95d48a22017-04-04 17:54:30 +0100331static struct drm_fb *
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000332drm_output_render_pixman(struct drm_output_state *state,
333 pixman_region32_t *damage)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200334{
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000335 struct drm_output *output = state->output;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200336 struct weston_compositor *ec = output->base.compositor;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200337
338 output->current_image ^= 1;
339
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200340 pixman_renderer_output_set_buffer(&output->base,
341 output->image[output->current_image]);
Pekka Paalanenacf50c32018-04-23 11:44:56 +0200342 pixman_renderer_output_set_hw_extra_damage(&output->base,
343 &output->previous_damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200344
Pekka Paalanenacf50c32018-04-23 11:44:56 +0200345 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200346
Pekka Paalanenacf50c32018-04-23 11:44:56 +0200347 pixman_region32_copy(&output->previous_damage, damage);
Daniel Stone95d48a22017-04-04 17:54:30 +0100348
349 return drm_fb_ref(output->dumb[output->current_image]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200350}
351
Stefan Agner3654c672019-07-09 00:50:30 +0200352void
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000353drm_output_render(struct drm_output_state *state, pixman_region32_t *damage)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200354{
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000355 struct drm_output *output = state->output;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300356 struct weston_compositor *c = output->base.compositor;
Daniel Stonee2e80132018-01-16 15:37:33 +0000357 struct drm_plane_state *scanout_state;
Daniel Stonee95169b2016-11-14 17:46:59 +0000358 struct drm_plane *scanout_plane = output->scanout_plane;
Scott Anderson15c603c2020-06-02 17:39:43 +1200359 struct drm_property_info *damage_info =
360 &scanout_plane->props[WDRM_PLANE_FB_DAMAGE_CLIPS];
Armin Krezović545dba62016-08-05 15:54:18 +0200361 struct drm_backend *b = to_drm_backend(c);
Daniel Stone95d48a22017-04-04 17:54:30 +0100362 struct drm_fb *fb;
Scott Anderson15c603c2020-06-02 17:39:43 +1200363 pixman_region32_t scanout_damage;
364 pixman_box32_t *rects;
365 int n_rects;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200366
Daniel Stone4e84f7d2017-04-04 17:54:29 +0100367 /* If we already have a client buffer promoted to scanout, then we don't
368 * want to render. */
Leandro Ribeiro05cecc82020-08-13 17:34:58 -0300369 scanout_state = drm_output_state_get_plane(state, scanout_plane);
Daniel Stonee2e80132018-01-16 15:37:33 +0000370 if (scanout_state->fb)
Daniel Stone4e84f7d2017-04-04 17:54:29 +0100371 return;
372
Daniel Stone98d75e12020-03-06 11:03:14 +0000373 /*
374 * If we don't have any damage on the primary plane, and we already
375 * have a renderer buffer active, we can reuse it; else we pass
376 * the damaged region into the renderer to re-render the affected
Pekka Paalanen7f1a1132020-12-07 14:59:44 +0200377 * area. But, we still have to call the renderer anyway if any screen
378 * capture is pending, otherwise the capture will not complete.
Daniel Stone98d75e12020-03-06 11:03:14 +0000379 */
Daniel Stonee95169b2016-11-14 17:46:59 +0000380 if (!pixman_region32_not_empty(damage) &&
Pekka Paalanen7f1a1132020-12-07 14:59:44 +0200381 wl_list_empty(&output->base.frame_signal.listener_list) &&
Daniel Stonee95169b2016-11-14 17:46:59 +0000382 scanout_plane->state_cur->fb &&
383 (scanout_plane->state_cur->fb->type == BUFFER_GBM_SURFACE ||
Daniel Stone98d75e12020-03-06 11:03:14 +0000384 scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB)) {
Daniel Stonee95169b2016-11-14 17:46:59 +0000385 fb = drm_fb_ref(scanout_plane->state_cur->fb);
386 } else if (b->use_pixman) {
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000387 fb = drm_output_render_pixman(state, damage);
Daniel Stonee95169b2016-11-14 17:46:59 +0000388 } else {
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000389 fb = drm_output_render_gl(state, damage);
Daniel Stonee95169b2016-11-14 17:46:59 +0000390 }
Daniel Stone95d48a22017-04-04 17:54:30 +0100391
Daniel Stonee2e80132018-01-16 15:37:33 +0000392 if (!fb) {
393 drm_plane_state_put_back(scanout_state);
Daniel Stone95d48a22017-04-04 17:54:30 +0100394 return;
Daniel Stonee2e80132018-01-16 15:37:33 +0000395 }
396
397 scanout_state->fb = fb;
398 scanout_state->output = output;
399
400 scanout_state->src_x = 0;
401 scanout_state->src_y = 0;
Daniel Stonef9a61622020-03-06 11:07:15 +0000402 scanout_state->src_w = fb->width << 16;
403 scanout_state->src_h = fb->height << 16;
Daniel Stonee2e80132018-01-16 15:37:33 +0000404
405 scanout_state->dest_x = 0;
406 scanout_state->dest_y = 0;
Daniel Stonef9a61622020-03-06 11:07:15 +0000407 scanout_state->dest_w = output->base.current_mode->width;
408 scanout_state->dest_h = output->base.current_mode->height;
Daniel Stonee2e80132018-01-16 15:37:33 +0000409
leng.fang32af9fc2024-06-13 11:22:15 +0800410 drm_output_render_aml_config(output, scanout_state);
411
Scott Anderson15c603c2020-06-02 17:39:43 +1200412 pixman_region32_subtract(&c->primary_plane.damage,
413 &c->primary_plane.damage, damage);
414
415 /* Don't bother calculating plane damage if the plane doesn't support it */
416 if (damage_info->prop_id == 0)
417 return;
418
419 pixman_region32_init(&scanout_damage);
420 pixman_region32_copy(&scanout_damage, damage);
421
Deepak Rawat46a1c722018-07-24 14:13:34 -0700422 if (output->base.zoom.active) {
Derek Foremandb7e85d2022-01-20 10:27:17 -0600423 pixman_region32_t clip;
424
Scott Anderson15c603c2020-06-02 17:39:43 +1200425 weston_matrix_transform_region(&scanout_damage,
Deepak Rawat46a1c722018-07-24 14:13:34 -0700426 &output->base.matrix,
Scott Anderson15c603c2020-06-02 17:39:43 +1200427 &scanout_damage);
Derek Foremandb7e85d2022-01-20 10:27:17 -0600428 pixman_region32_init_rect(&clip, 0, 0,
429 output->base.width,
430 output->base.height);
431 pixman_region32_intersect(&scanout_damage, &scanout_damage, &clip);
432 pixman_region32_fini(&clip);
Deepak Rawat46a1c722018-07-24 14:13:34 -0700433 } else {
Scott Anderson15c603c2020-06-02 17:39:43 +1200434 pixman_region32_translate(&scanout_damage,
Deepak Rawat46a1c722018-07-24 14:13:34 -0700435 -output->base.x, -output->base.y);
436 weston_transformed_region(output->base.width,
437 output->base.height,
438 output->base.transform,
439 output->base.current_scale,
Scott Anderson15c603c2020-06-02 17:39:43 +1200440 &scanout_damage,
441 &scanout_damage);
Deepak Rawat46a1c722018-07-24 14:13:34 -0700442 }
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200443
Scott Anderson15c603c2020-06-02 17:39:43 +1200444 assert(scanout_state->damage_blob_id == 0);
445
446 rects = pixman_region32_rectangles(&scanout_damage, &n_rects);
447
448 /*
449 * If this function fails, the blob id should still be 0.
450 * This tells the kernel there is no damage information, which means
451 * that it will consider the whole plane damaged. While this may
452 * affect efficiency, it should still produce correct results.
453 */
454 drmModeCreatePropertyBlob(b->drm.fd, rects,
455 sizeof(*rects) * n_rects,
456 &scanout_state->damage_blob_id);
457
458 pixman_region32_fini(&scanout_damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200459}
460
Daniel Stonea08512f2016-11-08 17:46:10 +0000461static int
462drm_output_repaint(struct weston_output *output_base,
463 pixman_region32_t *damage,
464 void *repaint_data)
465{
466 struct drm_pending_state *pending_state = repaint_data;
467 struct drm_output *output = to_drm_output(output_base);
Daniel Stonea08512f2016-11-08 17:46:10 +0000468 struct drm_output_state *state = NULL;
469 struct drm_plane_state *scanout_state;
470
Tomohito Esakib1fb00d2018-01-31 17:50:48 +0900471 assert(!output->virtual);
472
Daniel Stonea08512f2016-11-08 17:46:10 +0000473 if (output->disable_pending || output->destroy_pending)
474 goto err;
475
476 assert(!output->state_last);
477
478 /* If planes have been disabled in the core, we might not have
479 * hit assign_planes at all, so might not have valid output state
480 * here. */
481 state = drm_pending_state_get_output(pending_state, output);
482 if (!state)
483 state = drm_output_state_duplicate(output->state_cur,
484 pending_state,
485 DRM_OUTPUT_STATE_CLEAR_PLANES);
486 state->dpms = WESTON_DPMS_ON;
487
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530488 if (output_base->allow_protection)
489 state->protection = output_base->desired_protection;
490 else
491 state->protection = WESTON_HDCP_DISABLE;
492
Daniel Stonea08512f2016-11-08 17:46:10 +0000493 drm_output_render(state, damage);
494 scanout_state = drm_output_state_get_plane(state,
495 output->scanout_plane);
496 if (!scanout_state || !scanout_state->fb)
497 goto err;
498
Daniel Stonea08512f2016-11-08 17:46:10 +0000499 return 0;
500
501err:
502 drm_output_state_free(state);
David Herrmann1edf44c2013-10-22 17:11:26 +0200503 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400504}
505
Daniel Stone4c2fc702019-06-18 11:12:07 +0100506/* Determine the type of vblank synchronization to use for the output.
507 *
508 * The pipe parameter indicates which CRTC is in use. Knowing this, we
509 * can determine which vblank sequence type to use for it. Traditional
510 * cards had only two CRTCs, with CRTC 0 using no special flags, and
511 * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
512 * parameter indicates this.
513 *
514 * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
515 * 0-31. If this is non-zero it indicates we're dealing with a
516 * multi-gpu situation and we need to calculate the vblank sync
517 * using DRM_BLANK_HIGH_CRTC_MASK.
518 */
519static unsigned int
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300520drm_waitvblank_pipe(struct drm_crtc *crtc)
Daniel Stone4c2fc702019-06-18 11:12:07 +0100521{
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300522 if (crtc->pipe > 1)
523 return (crtc->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
Daniel Stone4c2fc702019-06-18 11:12:07 +0100524 DRM_VBLANK_HIGH_CRTC_MASK;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300525 else if (crtc->pipe > 0)
Daniel Stone4c2fc702019-06-18 11:12:07 +0100526 return DRM_VBLANK_SECONDARY;
527 else
528 return 0;
529}
530
Antonio Borneoc90fccc2019-06-30 15:51:10 +0200531static int
Jonas Ådahle5a12252013-04-05 23:07:11 +0200532drm_output_start_repaint_loop(struct weston_output *output_base)
533{
Armin Krezović545dba62016-08-05 15:54:18 +0200534 struct drm_output *output = to_drm_output(output_base);
Daniel Stone8747f952016-11-29 20:17:32 +0000535 struct drm_pending_state *pending_state;
Daniel Stonee2e80132018-01-16 15:37:33 +0000536 struct drm_plane *scanout_plane = output->scanout_plane;
Armin Krezović545dba62016-08-05 15:54:18 +0200537 struct drm_backend *backend =
538 to_drm_backend(output_base->compositor);
Mario Kleinerf507ec32015-06-21 21:25:14 +0200539 struct timespec ts, tnow;
540 struct timespec vbl2now;
541 int64_t refresh_nsec;
542 int ret;
543 drmVBlank vbl = {
544 .request.type = DRM_VBLANK_RELATIVE,
545 .request.sequence = 0,
546 .request.signal = 0,
547 };
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300548
Armin Krezović08368132016-09-30 14:11:05 +0200549 if (output->disable_pending || output->destroy_pending)
Antonio Borneoc90fccc2019-06-30 15:51:10 +0200550 return 0;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800551
Leandro Ribeiro05cecc82020-08-13 17:34:58 -0300552 if (!scanout_plane->state_cur->fb) {
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300553 /* We can't page flip if there's no mode set */
David Herrmann3c688c52013-10-22 17:11:25 +0200554 goto finish_frame;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300555 }
556
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +0300557 /* Need to smash all state in from scratch; current timings might not
558 * be what we want, page flip might not work, etc.
559 */
Daniel Stone6020f472018-02-05 15:46:20 +0000560 if (backend->state_invalid)
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +0300561 goto finish_frame;
562
Daniel Stonee2e80132018-01-16 15:37:33 +0000563 assert(scanout_plane->state_cur->output == output);
564
Mario Kleinerf507ec32015-06-21 21:25:14 +0200565 /* Try to get current msc and timestamp via instant query */
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300566 vbl.request.type |= drm_waitvblank_pipe(output->crtc);
Mario Kleinerf507ec32015-06-21 21:25:14 +0200567 ret = drmWaitVBlank(backend->drm.fd, &vbl);
568
569 /* Error ret or zero timestamp means failure to get valid timestamp */
570 if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
571 ts.tv_sec = vbl.reply.tval_sec;
572 ts.tv_nsec = vbl.reply.tval_usec * 1000;
573
574 /* Valid timestamp for most recent vblank - not stale?
575 * Stale ts could happen on Linux 3.17+, so make sure it
576 * is not older than 1 refresh duration since now.
577 */
578 weston_compositor_read_presentation_clock(backend->compositor,
579 &tnow);
580 timespec_sub(&vbl2now, &tnow, &ts);
581 refresh_nsec =
582 millihz_to_nsec(output->base.current_mode->refresh);
583 if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
584 drm_output_update_msc(output, vbl.reply.sequence);
585 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200586 WP_PRESENTATION_FEEDBACK_INVALID);
Antonio Borneoc90fccc2019-06-30 15:51:10 +0200587 return 0;
Mario Kleinerf507ec32015-06-21 21:25:14 +0200588 }
589 }
590
591 /* Immediate query didn't provide valid timestamp.
592 * Use pageflip fallback.
593 */
Jonas Ådahle5a12252013-04-05 23:07:11 +0200594
Daniel Stone205c0a02017-04-04 17:54:33 +0100595 assert(!output->page_flip_pending);
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000596 assert(!output->state_last);
597
598 pending_state = drm_pending_state_alloc(backend);
Daniel Stone8747f952016-11-29 20:17:32 +0000599 drm_output_state_duplicate(output->state_cur, pending_state,
600 DRM_OUTPUT_STATE_PRESERVE_PLANES);
Daniel Stone205c0a02017-04-04 17:54:33 +0100601
Daniel Stone8747f952016-11-29 20:17:32 +0000602 ret = drm_pending_state_apply(pending_state);
603 if (ret != 0) {
Antonio Borneo39578632019-04-26 23:57:31 +0200604 weston_log("applying repaint-start state failed: %s\n",
605 strerror(errno));
Antonio Borneoc90fccc2019-06-30 15:51:10 +0200606 if (ret == -EACCES)
607 return -1;
David Herrmann3c688c52013-10-22 17:11:25 +0200608 goto finish_frame;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200609 }
David Herrmann3c688c52013-10-22 17:11:25 +0200610
Antonio Borneoc90fccc2019-06-30 15:51:10 +0200611 return 0;
David Herrmann3c688c52013-10-22 17:11:25 +0200612
613finish_frame:
614 /* if we cannot page-flip, immediately finish frame */
Daniel Stone3615ce12017-03-01 11:34:05 +0000615 weston_output_finish_frame(output_base, NULL,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200616 WP_PRESENTATION_FEEDBACK_INVALID);
Antonio Borneoc90fccc2019-06-30 15:51:10 +0200617 return 0;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200618}
619
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000620/**
621 * Begin a new repaint cycle
622 *
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000623 * Called by the core compositor at the beginning of a repaint cycle. Creates
624 * a new pending_state structure to own any output state created by individual
625 * output repaint functions until the repaint is flushed or cancelled.
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000626 */
627static void *
628drm_repaint_begin(struct weston_compositor *compositor)
629{
630 struct drm_backend *b = to_drm_backend(compositor);
631 struct drm_pending_state *ret;
632
633 ret = drm_pending_state_alloc(b);
634 b->repaint_data = ret;
635
Marius Vlad7e4db952019-04-17 13:47:06 +0300636 if (weston_log_scope_is_enabled(b->debug)) {
Daniel Stone1cbe1f92018-07-20 10:21:28 +0100637 char *dbg = weston_compositor_print_scene_graph(compositor);
638 drm_debug(b, "[repaint] Beginning repaint; pending_state %p\n",
639 ret);
640 drm_debug(b, "%s", dbg);
641 free(dbg);
642 }
643
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000644 return ret;
645}
646
647/**
648 * Flush a repaint set
649 *
650 * Called by the core compositor when a repaint cycle has been completed
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000651 * and should be flushed. Frees the pending state, transitioning ownership
652 * of the output state from the pending state, to the update itself. When
653 * the update completes (see drm_output_update_complete), the output
654 * state will be freed.
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000655 */
Antonio Borneoc90fccc2019-06-30 15:51:10 +0200656static int
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000657drm_repaint_flush(struct weston_compositor *compositor, void *repaint_data)
658{
659 struct drm_backend *b = to_drm_backend(compositor);
660 struct drm_pending_state *pending_state = repaint_data;
Antonio Borneoc90fccc2019-06-30 15:51:10 +0200661 int ret;
Daniel Stone6020f472018-02-05 15:46:20 +0000662
Antonio Borneoc90fccc2019-06-30 15:51:10 +0200663 ret = drm_pending_state_apply(pending_state);
664 if (ret != 0)
665 weston_log("repaint-flush failed: %s\n", strerror(errno));
666
Daniel Stone1cbe1f92018-07-20 10:21:28 +0100667 drm_debug(b, "[repaint] flushed pending_state %p\n", pending_state);
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000668 b->repaint_data = NULL;
leng.fang32af9fc2024-06-13 11:22:15 +0800669#ifdef ENABLE_DRM_HELP
670 help_do_repaint_cycle_completed();
671#endif
Antonio Borneoc90fccc2019-06-30 15:51:10 +0200672
673 return (ret == -EACCES) ? -1 : 0;
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000674}
675
676/**
677 * Cancel a repaint set
678 *
679 * Called by the core compositor when a repaint has finished, so the data
680 * held across the repaint cycle should be discarded.
681 */
682static void
683drm_repaint_cancel(struct weston_compositor *compositor, void *repaint_data)
684{
685 struct drm_backend *b = to_drm_backend(compositor);
686 struct drm_pending_state *pending_state = repaint_data;
687
688 drm_pending_state_free(pending_state);
Daniel Stone1cbe1f92018-07-20 10:21:28 +0100689 drm_debug(b, "[repaint] cancel pending_state %p\n", pending_state);
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000690 b->repaint_data = NULL;
691}
692
Alex Wub7b8bda2012-04-17 17:20:48 +0800693static int
Giulio Camuffo954f1832014-10-11 18:27:30 +0300694drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +0000695static void
696drm_output_fini_pixman(struct drm_output *output);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -0200697
698static int
Alex Wub7b8bda2012-04-17 17:20:48 +0800699drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
700{
Daniel Stone02d487a2017-10-07 14:01:45 +0100701 struct drm_output *output = to_drm_output(output_base);
702 struct drm_backend *b = to_drm_backend(output_base->compositor);
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100703 struct drm_mode *drm_mode = drm_output_choose_mode(output, mode);
Alex Wub7b8bda2012-04-17 17:20:48 +0800704
705 if (!drm_mode) {
Daniel Stone02d487a2017-10-07 14:01:45 +0100706 weston_log("%s: invalid resolution %dx%d\n",
707 output_base->name, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +0800708 return -1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -0200709 }
710
Hardeningff39efa2013-09-18 23:56:35 +0200711 if (&drm_mode->base == output->base.current_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +0800712 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +0800713
Hardeningff39efa2013-09-18 23:56:35 +0200714 output->base.current_mode->flags = 0;
Alex Wub7b8bda2012-04-17 17:20:48 +0800715
leng.fang32af9fc2024-06-13 11:22:15 +0800716 drm_switch_mode_aml_config_ui_size(output, output_base, drm_mode);
717
Hardeningff39efa2013-09-18 23:56:35 +0200718 output->base.current_mode = &drm_mode->base;
719 output->base.current_mode->flags =
Alex Wub7b8bda2012-04-17 17:20:48 +0800720 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
721
leng.fang32af9fc2024-06-13 11:22:15 +0800722 drm_switch_mode_aml_config_help(output, output_base, drm_mode);
723
Daniel Stonef30a18c2017-04-04 17:54:31 +0100724 /* XXX: This drops our current buffer too early, before we've started
725 * displaying it. Ideally this should be much more atomic and
726 * integrated with a full repaint cycle, rather than doing a
727 * sledgehammer modeswitch first, and only later showing new
728 * content.
729 */
Daniel Stone6020f472018-02-05 15:46:20 +0000730 b->state_invalid = true;
Alex Wub7b8bda2012-04-17 17:20:48 +0800731
leng.fang32af9fc2024-06-13 11:22:15 +0800732 b->allow_modeset = true;
733#ifdef MESON_DRM_FIX_UI_SIZE
734 // fixed ui mode needn't reset egl.
735 return 0;
736#endif
737
Giulio Camuffo954f1832014-10-11 18:27:30 +0300738 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200739 drm_output_fini_pixman(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300740 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200741 weston_log("failed to init output pixman state with "
742 "new mode\n");
743 return -1;
744 }
745 } else {
Daniel Stone3e661f72016-11-04 17:24:06 +0000746 drm_output_fini_egl(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300747 if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200748 weston_log("failed to init output egl state with "
749 "new mode");
750 return -1;
751 }
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -0200752 }
753
Alex Wub7b8bda2012-04-17 17:20:48 +0800754 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +0800755}
756
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200757static int
Giulio Camuffo954f1832014-10-11 18:27:30 +0300758init_pixman(struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200759{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300760 return pixman_renderer_init(b->compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200761}
762
Pekka Paalanen7b36b422014-06-04 14:00:53 +0300763/**
Pekka Paalanenec272712014-06-05 11:22:25 +0300764 * Create a drm_plane for a hardware plane
765 *
766 * Creates one drm_plane structure for a hardware plane, and initialises its
767 * properties and formats.
768 *
769 * This function does not add the plane to the list of usable planes in Weston
770 * itself; the caller is responsible for this.
771 *
772 * Call drm_plane_destroy to clean up the plane.
773 *
Daniel Stone2ba17f42015-05-19 20:02:41 +0100774 * @sa drm_output_find_special_plane
Pekka Paalanenec272712014-06-05 11:22:25 +0300775 * @param b DRM compositor backend
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -0300776 * @param kplane DRM plane to create
Pekka Paalanenec272712014-06-05 11:22:25 +0300777 */
778static struct drm_plane *
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -0300779drm_plane_create(struct drm_backend *b, const drmModePlane *kplane)
Pekka Paalanenec272712014-06-05 11:22:25 +0300780{
781 struct drm_plane *plane;
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100782 drmModeObjectProperties *props;
Marius Vladcdd6fa22019-08-29 20:42:00 +0300783 uint64_t *zpos_range_values;
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100784
Scott Anderson74663092021-02-01 15:46:33 -0300785 plane = zalloc(sizeof(*plane));
Pekka Paalanenec272712014-06-05 11:22:25 +0300786 if (!plane) {
787 weston_log("%s: out of memory\n", __func__);
788 return NULL;
789 }
790
791 plane->backend = b;
Daniel Stone57d609a2021-11-16 18:56:09 +0000792 plane->plane_idx = b->next_plane_idx++;
Daniel Stonebc15f682016-11-14 16:57:01 +0000793 plane->state_cur = drm_plane_state_alloc(NULL, plane);
794 plane->state_cur->complete = true;
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -0300795 plane->possible_crtcs = kplane->possible_crtcs;
796 plane->plane_id = kplane->plane_id;
Pekka Paalanenec272712014-06-05 11:22:25 +0300797
Scott Anderson74663092021-02-01 15:46:33 -0300798 weston_drm_format_array_init(&plane->formats);
799
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -0300800 props = drmModeObjectGetProperties(b->drm.fd, kplane->plane_id,
801 DRM_MODE_OBJECT_PLANE);
802 if (!props) {
803 weston_log("couldn't get plane properties\n");
804 goto err;
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100805 }
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -0300806
807 drm_property_info_populate(b, plane_props, plane->props,
808 WDRM_PLANE__COUNT, props);
809 plane->type =
810 drm_property_get_value(&plane->props[WDRM_PLANE_TYPE],
811 props,
812 WDRM_PLANE_TYPE__COUNT);
813
814 zpos_range_values =
815 drm_property_get_range_values(&plane->props[WDRM_PLANE_ZPOS],
816 props);
817
818 if (zpos_range_values) {
819 plane->zpos_min = zpos_range_values[0];
820 plane->zpos_max = zpos_range_values[1];
821 } else {
Marius Vladcdd6fa22019-08-29 20:42:00 +0300822 plane->zpos_min = DRM_PLANE_ZPOS_INVALID_PLANE;
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -0300823 plane->zpos_max = DRM_PLANE_ZPOS_INVALID_PLANE;
Daniel Stone2ba17f42015-05-19 20:02:41 +0100824 }
825
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -0300826 if (drm_plane_populate_formats(plane, kplane, props,
827 b->fb_modifiers) < 0) {
828 drmModeFreeObjectProperties(props);
829 goto err;
830 }
831
832 drmModeFreeObjectProperties(props);
833
Daniel Stone2ba17f42015-05-19 20:02:41 +0100834 if (plane->type == WDRM_PLANE_TYPE__COUNT)
835 goto err_props;
836
Pekka Paalanenec272712014-06-05 11:22:25 +0300837 weston_plane_init(&plane->base, b->compositor, 0, 0);
Daniel Stone085d2b92015-05-21 00:00:57 +0100838 wl_list_insert(&b->plane_list, &plane->link);
Pekka Paalanenec272712014-06-05 11:22:25 +0300839
840 return plane;
Daniel Stone2ba17f42015-05-19 20:02:41 +0100841
842err_props:
843 drm_property_info_free(plane->props, WDRM_PLANE__COUNT);
844err:
Scott Anderson74663092021-02-01 15:46:33 -0300845 weston_drm_format_array_fini(&plane->formats);
Daniel Stone2ba17f42015-05-19 20:02:41 +0100846 drm_plane_state_free(plane->state_cur, true);
847 free(plane);
848 return NULL;
849}
850
851/**
852 * Find, or create, a special-purpose plane
853 *
Daniel Stone2ba17f42015-05-19 20:02:41 +0100854 * @param b DRM backend
855 * @param output Output to use for plane
856 * @param type Type of plane
857 */
858static struct drm_plane *
859drm_output_find_special_plane(struct drm_backend *b, struct drm_output *output,
860 enum wdrm_plane_type type)
861{
862 struct drm_plane *plane;
863
Daniel Stone2ba17f42015-05-19 20:02:41 +0100864 wl_list_for_each(plane, &b->plane_list, link) {
865 struct drm_output *tmp;
866 bool found_elsewhere = false;
867
868 if (plane->type != type)
869 continue;
870 if (!drm_plane_is_available(plane, output))
871 continue;
872
873 /* On some platforms, primary/cursor planes can roam
874 * between different CRTCs, so make sure we don't claim the
875 * same plane for two outputs. */
Leandro Ribeiro05cecc82020-08-13 17:34:58 -0300876 wl_list_for_each(tmp, &b->compositor->output_list, base.link) {
Daniel Stonee2e80132018-01-16 15:37:33 +0000877 if (tmp->cursor_plane == plane ||
878 tmp->scanout_plane == plane) {
Daniel Stone2ba17f42015-05-19 20:02:41 +0100879 found_elsewhere = true;
880 break;
881 }
882 }
883
884 if (found_elsewhere)
885 continue;
886
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300887 plane->possible_crtcs = (1 << output->crtc->pipe);
Daniel Stone2ba17f42015-05-19 20:02:41 +0100888 return plane;
889 }
890
891 return NULL;
Pekka Paalanenec272712014-06-05 11:22:25 +0300892}
893
894/**
895 * Destroy one DRM plane
896 *
897 * Destroy a DRM plane, removing it from screen and releasing its retained
898 * buffers in the process. The counterpart to drm_plane_create.
899 *
900 * @param plane Plane to deallocate (will be freed)
901 */
902static void
903drm_plane_destroy(struct drm_plane *plane)
904{
Daniel Stone2ba17f42015-05-19 20:02:41 +0100905 if (plane->type == WDRM_PLANE_TYPE_OVERLAY)
906 drmModeSetPlane(plane->backend->drm.fd, plane->plane_id,
907 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
Daniel Stonebc15f682016-11-14 16:57:01 +0000908 drm_plane_state_free(plane->state_cur, true);
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100909 drm_property_info_free(plane->props, WDRM_PLANE__COUNT);
Pekka Paalanenec272712014-06-05 11:22:25 +0300910 weston_plane_release(&plane->base);
Scott Anderson74663092021-02-01 15:46:33 -0300911 weston_drm_format_array_fini(&plane->formats);
Pekka Paalanenec272712014-06-05 11:22:25 +0300912 wl_list_remove(&plane->link);
913 free(plane);
914}
915
916/**
917 * Initialise sprites (overlay planes)
918 *
919 * Walk the list of provided DRM planes, and add overlay planes.
920 *
921 * Call destroy_sprites to free these planes.
922 *
923 * @param b DRM compositor backend
924 */
925static void
926create_sprites(struct drm_backend *b)
927{
928 drmModePlaneRes *kplane_res;
929 drmModePlane *kplane;
930 struct drm_plane *drm_plane;
931 uint32_t i;
Pekka Paalanenec272712014-06-05 11:22:25 +0300932 kplane_res = drmModeGetPlaneResources(b->drm.fd);
933 if (!kplane_res) {
934 weston_log("failed to get plane resources: %s\n",
935 strerror(errno));
936 return;
937 }
leng.fang32af9fc2024-06-13 11:22:15 +0800938 for (i = kplane_res->count_planes; i >0; i--) {
939 kplane = drmModeGetPlane(b->drm.fd, kplane_res->planes[i-1]);
Pekka Paalanenec272712014-06-05 11:22:25 +0300940
Pekka Paalanenec272712014-06-05 11:22:25 +0300941 if (!kplane)
942 continue;
943
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -0300944 drm_plane = drm_plane_create(b, kplane);
Pekka Paalanenec272712014-06-05 11:22:25 +0300945 drmModeFreePlane(kplane);
946 if (!drm_plane)
947 continue;
948
Daniel Stone085d2b92015-05-21 00:00:57 +0100949 if (drm_plane->type == WDRM_PLANE_TYPE_OVERLAY)
950 weston_compositor_stack_plane(b->compositor,
951 &drm_plane->base,
952 &b->compositor->primary_plane);
Pekka Paalanenec272712014-06-05 11:22:25 +0300953 }
954
955 drmModeFreePlaneResources(kplane_res);
956}
957
958/**
959 * Clean up sprites (overlay planes)
960 *
961 * The counterpart to create_sprites.
962 *
963 * @param b DRM compositor backend
964 */
965static void
966destroy_sprites(struct drm_backend *b)
967{
968 struct drm_plane *plane, *next;
969
Daniel Stone085d2b92015-05-21 00:00:57 +0100970 wl_list_for_each_safe(plane, next, &b->plane_list, link)
Pekka Paalanenec272712014-06-05 11:22:25 +0300971 drm_plane_destroy(plane);
972}
973
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -0300974/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200975static uint32_t
Pekka Paalanence724242017-09-04 12:21:24 +0300976drm_get_backlight(struct drm_head *head)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200977{
978 long brightness, max_brightness, norm;
979
Pekka Paalanence724242017-09-04 12:21:24 +0300980 brightness = backlight_get_brightness(head->backlight);
981 max_brightness = backlight_get_max_brightness(head->backlight);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200982
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -0300983 /* convert it on a scale of 0 to 255 */
984 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200985
986 return (uint32_t) norm;
987}
988
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -0300989/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200990static void
991drm_set_backlight(struct weston_output *output_base, uint32_t value)
992{
Pekka Paalanenecc8cce2017-09-12 16:14:31 +0300993 struct drm_output *output = to_drm_output(output_base);
994 struct drm_head *head;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200995 long max_brightness, new_brightness;
996
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400997 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200998 return;
999
Pekka Paalanenecc8cce2017-09-12 16:14:31 +03001000 wl_list_for_each(head, &output->base.head_list, base.output_link) {
1001 if (!head->backlight)
1002 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001003
Pekka Paalanenecc8cce2017-09-12 16:14:31 +03001004 max_brightness = backlight_get_max_brightness(head->backlight);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001005
Pekka Paalanenecc8cce2017-09-12 16:14:31 +03001006 /* get denormalized value */
1007 new_brightness = (value * max_brightness) / 255;
1008
1009 backlight_set_brightness(head->backlight, new_brightness);
1010 }
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001011}
1012
Pekka Paalanenf8b850d2017-11-15 12:51:01 +02001013static void
1014drm_output_init_backlight(struct drm_output *output)
1015{
1016 struct weston_head *base;
1017 struct drm_head *head;
1018
1019 output->base.set_backlight = NULL;
1020
1021 wl_list_for_each(base, &output->base.head_list, output_link) {
1022 head = to_drm_head(base);
1023
1024 if (head->backlight) {
1025 weston_log("Initialized backlight for head '%s', device %s\n",
1026 head->base.name, head->backlight->path);
1027
1028 if (!output->base.set_backlight) {
1029 output->base.set_backlight = drm_set_backlight;
1030 output->base.backlight_current =
1031 drm_get_backlight(head);
1032 }
1033 }
1034 }
Pekka Paalanenf8b850d2017-11-15 12:51:01 +02001035}
1036
Daniel Stonea08512f2016-11-08 17:46:10 +00001037/**
1038 * Power output on or off
1039 *
1040 * The DPMS/power level of an output is used to switch it on or off. This
1041 * is DRM's hook for doing so, which can called either as part of repaint,
1042 * or independently of the repaint loop.
1043 *
1044 * If we are called as part of repaint, we simply set the relevant bit in
1045 * state and return.
Tomohito Esakib1fb00d2018-01-31 17:50:48 +09001046 *
1047 * This function is never called on a virtual output.
Daniel Stonea08512f2016-11-08 17:46:10 +00001048 */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001049static void
1050drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1051{
Armin Krezović545dba62016-08-05 15:54:18 +02001052 struct drm_output *output = to_drm_output(output_base);
Daniel Stonea08512f2016-11-08 17:46:10 +00001053 struct drm_backend *b = to_drm_backend(output_base->compositor);
1054 struct drm_pending_state *pending_state = b->repaint_data;
1055 struct drm_output_state *state;
Daniel Stone36609c72015-06-18 07:49:02 +01001056 int ret;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001057
Tomohito Esakib1fb00d2018-01-31 17:50:48 +09001058 assert(!output->virtual);
1059
Daniel Stonea08512f2016-11-08 17:46:10 +00001060 if (output->state_cur->dpms == level)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001061 return;
1062
Daniel Stonea08512f2016-11-08 17:46:10 +00001063 /* If we're being called during the repaint loop, then this is
1064 * simple: discard any previously-generated state, and create a new
1065 * state where we disable everything. When we come to flush, this
1066 * will be applied.
1067 *
1068 * However, we need to be careful: we can be called whilst another
1069 * output is in its repaint cycle (pending_state exists), but our
1070 * output still has an incomplete state application outstanding.
1071 * In that case, we need to wait until that completes. */
1072 if (pending_state && !output->state_last) {
1073 /* The repaint loop already sets DPMS on; we don't need to
1074 * explicitly set it on here, as it will already happen
1075 * whilst applying the repaint state. */
1076 if (level == WESTON_DPMS_ON)
1077 return;
1078
1079 state = drm_pending_state_get_output(pending_state, output);
1080 if (state)
1081 drm_output_state_free(state);
1082 state = drm_output_get_disable_state(pending_state, output);
Daniel Stone36609c72015-06-18 07:49:02 +01001083 return;
1084 }
1085
Daniel Stonea08512f2016-11-08 17:46:10 +00001086 /* As we throw everything away when disabling, just send us back through
1087 * a repaint cycle. */
1088 if (level == WESTON_DPMS_ON) {
1089 if (output->dpms_off_pending)
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01001090 output->dpms_off_pending = false;
Daniel Stonea08512f2016-11-08 17:46:10 +00001091 weston_output_schedule_repaint(output_base);
1092 return;
1093 }
1094
1095 /* If we've already got a request in the pipeline, then we need to
1096 * park our DPMS request until that request has quiesced. */
1097 if (output->state_last) {
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01001098 output->dpms_off_pending = true;
Daniel Stonea08512f2016-11-08 17:46:10 +00001099 return;
1100 }
1101
1102 pending_state = drm_pending_state_alloc(b);
1103 drm_output_get_disable_state(pending_state, output);
1104 ret = drm_pending_state_apply_sync(pending_state);
1105 if (ret != 0)
1106 weston_log("drm_set_dpms: couldn't disable output?\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001107}
1108
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001109static const char * const connector_type_names[] = {
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001110 [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
1111 [DRM_MODE_CONNECTOR_VGA] = "VGA",
1112 [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
1113 [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
1114 [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
1115 [DRM_MODE_CONNECTOR_Composite] = "Composite",
1116 [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
1117 [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
1118 [DRM_MODE_CONNECTOR_Component] = "Component",
1119 [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
1120 [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
1121 [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
1122 [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
1123 [DRM_MODE_CONNECTOR_TV] = "TV",
1124 [DRM_MODE_CONNECTOR_eDP] = "eDP",
1125 [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
1126 [DRM_MODE_CONNECTOR_DSI] = "DSI",
Stefan Agner30e283d2018-08-20 17:11:38 +02001127 [DRM_MODE_CONNECTOR_DPI] = "DPI",
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04001128};
1129
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03001130/** Create a name given a DRM connector
1131 *
1132 * \param con The DRM connector whose type and id form the name.
1133 * \return A newly allocate string, or NULL on error. Must be free()'d
1134 * after use.
1135 *
1136 * The name does not identify the DRM display device.
1137 */
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001138static char *
1139make_connector_name(const drmModeConnector *con)
1140{
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03001141 char *name;
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001142 const char *type_name = NULL;
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03001143 int ret;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001144
1145 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
1146 type_name = connector_type_names[con->connector_type];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001147
1148 if (!type_name)
1149 type_name = "UNNAMED";
1150
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03001151 ret = asprintf(&name, "%s-%d", type_name, con->connector_type_id);
1152 if (ret < 0)
1153 return NULL;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001154
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03001155 return name;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001156}
1157
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001158static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001159drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001160{
Hardeningff39efa2013-09-18 23:56:35 +02001161 int w = output->base.current_mode->width;
1162 int h = output->base.current_mode->height;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001163 uint32_t format = output->gbm_format;
1164 uint32_t pixman_format;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001165 unsigned int i;
Daniel Stone61abf352020-03-06 12:46:30 +00001166 const struct pixman_renderer_output_options options = {
1167 .use_shadow = b->use_pixman_shadow,
1168 };
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001169
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001170 switch (format) {
Stefan Agner0bfebeb2019-07-08 00:30:44 +02001171 case DRM_FORMAT_XRGB8888:
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001172 pixman_format = PIXMAN_x8r8g8b8;
1173 break;
Stefan Agner0bfebeb2019-07-08 00:30:44 +02001174 case DRM_FORMAT_RGB565:
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001175 pixman_format = PIXMAN_r5g6b5;
1176 break;
1177 default:
1178 weston_log("Unsupported pixman format 0x%x\n", format);
1179 return -1;
1180 }
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001181
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001182 /* FIXME error checking */
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001183 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001184 output->dumb[i] = drm_fb_create_dumb(b, w, h, format);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001185 if (!output->dumb[i])
1186 goto err;
1187
1188 output->image[i] =
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001189 pixman_image_create_bits(pixman_format, w, h,
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001190 output->dumb[i]->map,
Daniel Stone8eece0c2016-11-17 17:54:00 +00001191 output->dumb[i]->strides[0]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001192 if (!output->image[i])
1193 goto err;
1194 }
1195
Daniel Stone61abf352020-03-06 12:46:30 +00001196 if (pixman_renderer_output_create(&output->base, &options) < 0)
Dongjin Kimba89f002024-09-30 17:49:36 +09001197 goto err;
Ankit Nautiyala21c3932097-03-19 00:24:57 +05301198
Pekka Paalanendee412d2018-04-23 11:44:58 +02001199 weston_log("DRM: output %s %s shadow framebuffer.\n", output->base.name,
1200 b->use_pixman_shadow ? "uses" : "does not use");
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001201
1202 pixman_region32_init_rect(&output->previous_damage,
Alexander Larsson0b135062013-05-28 16:23:36 +02001203 output->base.x, output->base.y, output->base.width, output->base.height);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001204
1205 return 0;
1206
1207err:
1208 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1209 if (output->dumb[i])
Daniel Stone6e7a9612017-04-04 17:54:26 +01001210 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001211 if (output->image[i])
1212 pixman_image_unref(output->image[i]);
1213
1214 output->dumb[i] = NULL;
1215 output->image[i] = NULL;
1216 }
1217
1218 return -1;
1219}
1220
1221static void
1222drm_output_fini_pixman(struct drm_output *output)
1223{
Daniel Stonee2e80132018-01-16 15:37:33 +00001224 struct drm_backend *b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001225 unsigned int i;
1226
Daniel Stonee2e80132018-01-16 15:37:33 +00001227 /* Destroying the Pixman surface will destroy all our buffers,
1228 * regardless of refcount. Ensure we destroy them here. */
1229 if (!b->shutting_down &&
1230 output->scanout_plane->state_cur->fb &&
1231 output->scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB) {
Alexandros Frantzis99751342020-05-18 15:22:49 +03001232 drm_plane_reset_state(output->scanout_plane);
Daniel Stonee2e80132018-01-16 15:37:33 +00001233 }
1234
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001235 pixman_renderer_output_destroy(&output->base);
1236 pixman_region32_fini(&output->previous_damage);
1237
1238 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001239 pixman_image_unref(output->image[i]);
Daniel Stone6e7a9612017-04-04 17:54:26 +01001240 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001241 output->dumb[i] = NULL;
1242 output->image[i] = NULL;
1243 }
1244}
1245
Richard Hughes2b2092a2013-04-24 14:58:02 +01001246static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001247setup_output_seat_constraint(struct drm_backend *b,
Rob Bradford66bd9f52013-06-25 18:56:42 +01001248 struct weston_output *output,
1249 const char *s)
1250{
1251 if (strcmp(s, "") != 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05001252 struct weston_pointer *pointer;
Rob Bradford66bd9f52013-06-25 18:56:42 +01001253 struct udev_seat *seat;
1254
Giulio Camuffo954f1832014-10-11 18:27:30 +03001255 seat = udev_seat_get_named(&b->input, s);
Derek Foreman0720ea32015-07-15 13:00:35 -05001256 if (!seat)
1257 return;
Rob Bradford66bd9f52013-06-25 18:56:42 +01001258
Derek Foreman0720ea32015-07-15 13:00:35 -05001259 seat->base.output = output;
1260
Derek Foreman1281a362015-07-31 16:55:32 -05001261 pointer = weston_seat_get_pointer(&seat->base);
1262 if (pointer)
1263 weston_pointer_clamp(pointer,
1264 &pointer->x,
1265 &pointer->y);
Rob Bradford66bd9f52013-06-25 18:56:42 +01001266 }
1267}
1268
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001269static int
Pekka Paalanenc112f002017-08-28 16:27:20 +03001270drm_output_attach_head(struct weston_output *output_base,
1271 struct weston_head *head_base)
1272{
Pekka Paalanend5f98d82017-12-08 14:45:00 +02001273 struct drm_backend *b = to_drm_backend(output_base->compositor);
1274
Pekka Paalanenc112f002017-08-28 16:27:20 +03001275 if (wl_list_length(&output_base->head_list) >= MAX_CLONED_CONNECTORS)
1276 return -1;
1277
Pekka Paalanend5f98d82017-12-08 14:45:00 +02001278 if (!output_base->enabled)
1279 return 0;
1280
1281 /* XXX: ensure the configuration will work.
1282 * This is actually impossible without major infrastructure
1283 * work. */
1284
1285 /* Need to go through modeset to add connectors. */
1286 /* XXX: Ideally we'd do this per-output, not globally. */
1287 /* XXX: Doing it globally, what guarantees another output's update
1288 * will not clear the flag before this output is updated?
1289 */
1290 b->state_invalid = true;
1291
1292 weston_output_schedule_repaint(output_base);
1293
Pekka Paalanenc112f002017-08-28 16:27:20 +03001294 return 0;
1295}
1296
Pekka Paalanen7f853792017-11-29 14:33:33 +02001297static void
1298drm_output_detach_head(struct weston_output *output_base,
1299 struct weston_head *head_base)
1300{
1301 struct drm_backend *b = to_drm_backend(output_base->compositor);
1302
1303 if (!output_base->enabled)
1304 return;
1305
1306 /* Need to go through modeset to drop connectors that should no longer
1307 * be driven. */
1308 /* XXX: Ideally we'd do this per-output, not globally. */
1309 b->state_invalid = true;
1310
1311 weston_output_schedule_repaint(output_base);
1312}
1313
Stefan Agner3654c672019-07-09 00:50:30 +02001314int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001315parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format)
Neil Roberts77c1a5b2014-03-07 18:05:50 +00001316{
Pekka Paalanen62a94362018-09-26 14:33:36 +03001317 const struct pixel_format_info *pinfo;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00001318
Pekka Paalanen62a94362018-09-26 14:33:36 +03001319 if (s == NULL) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001320 *gbm_format = default_value;
Pekka Paalanen62a94362018-09-26 14:33:36 +03001321
1322 return 0;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00001323 }
1324
Pekka Paalanen62a94362018-09-26 14:33:36 +03001325 pinfo = pixel_format_get_info_by_drm_name(s);
1326 if (!pinfo) {
1327 weston_log("fatal: unrecognized pixel format: %s\n", s);
1328
1329 return -1;
1330 }
1331
1332 /* GBM formats and DRM formats are identical. */
1333 *gbm_format = pinfo->format;
1334
1335 return 0;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00001336}
1337
Pekka Paalaneneee580b2014-06-04 16:43:06 +03001338static int
Pekka Paalanen9c03a7c2017-11-28 14:30:10 +02001339drm_head_read_current_setup(struct drm_head *head, struct drm_backend *backend)
Pekka Paalaneneee580b2014-06-04 16:43:06 +03001340{
Pekka Paalanen9c03a7c2017-11-28 14:30:10 +02001341 int drm_fd = backend->drm.fd;
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001342 drmModeConnector *conn = head->connector.conn;
Pekka Paalaneneee580b2014-06-04 16:43:06 +03001343 drmModeEncoder *encoder;
1344 drmModeCrtc *crtc;
1345
1346 /* Get the current mode on the crtc that's currently driving
1347 * this connector. */
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001348 encoder = drmModeGetEncoder(drm_fd, conn->encoder_id);
Pekka Paalaneneee580b2014-06-04 16:43:06 +03001349 if (encoder != NULL) {
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001350 head->inherited_crtc_id = encoder->crtc_id;
1351
Pekka Paalaneneee580b2014-06-04 16:43:06 +03001352 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
1353 drmModeFreeEncoder(encoder);
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001354
Pekka Paalaneneee580b2014-06-04 16:43:06 +03001355 if (crtc == NULL)
1356 return -1;
1357 if (crtc->mode_valid)
Pekka Paalanen6fae2be2017-11-28 14:33:52 +02001358 head->inherited_mode = crtc->mode;
Pekka Paalaneneee580b2014-06-04 16:43:06 +03001359 drmModeFreeCrtc(crtc);
1360 }
1361
1362 return 0;
1363}
1364
Armin Krezović08368132016-09-30 14:11:05 +02001365static void
1366drm_output_set_gbm_format(struct weston_output *base,
1367 const char *gbm_format)
1368{
1369 struct drm_output *output = to_drm_output(base);
1370 struct drm_backend *b = to_drm_backend(base->compositor);
1371
1372 if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1)
1373 output->gbm_format = b->gbm_format;
1374}
1375
1376static void
1377drm_output_set_seat(struct weston_output *base,
1378 const char *seat)
1379{
1380 struct drm_output *output = to_drm_output(base);
1381 struct drm_backend *b = to_drm_backend(base->compositor);
1382
1383 setup_output_seat_constraint(b, &output->base,
1384 seat ? seat : "");
1385}
1386
1387static int
Pekka Paalanenc4db6f72017-09-05 16:37:03 +03001388drm_output_init_gamma_size(struct drm_output *output)
1389{
1390 struct drm_backend *backend = to_drm_backend(output->base.compositor);
1391 drmModeCrtc *crtc;
1392
1393 assert(output->base.compositor);
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001394 assert(output->crtc);
1395 crtc = drmModeGetCrtc(backend->drm.fd, output->crtc->crtc_id);
Pekka Paalanenc4db6f72017-09-05 16:37:03 +03001396 if (!crtc)
1397 return -1;
1398
1399 output->base.gamma_size = crtc->gamma_size;
1400
1401 drmModeFreeCrtc(crtc);
1402
1403 return 0;
1404}
1405
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001406static uint32_t
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001407drm_connector_get_possible_crtcs_mask(struct drm_connector *connector)
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001408{
1409 uint32_t possible_crtcs = 0;
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001410 drmModeConnector *conn = connector->conn;
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001411 drmModeEncoder *encoder;
1412 int i;
1413
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001414 for (i = 0; i < conn->count_encoders; i++) {
1415 encoder = drmModeGetEncoder(connector->backend->drm.fd,
1416 conn->encoders[i]);
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001417 if (!encoder)
1418 continue;
1419
1420 possible_crtcs |= encoder->possible_crtcs;
1421 drmModeFreeEncoder(encoder);
1422 }
1423
1424 return possible_crtcs;
1425}
1426
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001427/** Pick a CRTC that might be able to drive all attached connectors
1428 *
1429 * @param output The output whose attached heads to include.
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001430 * @return CRTC object to pick, or NULL on failure or not found.
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001431 */
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001432static struct drm_crtc *
1433drm_output_pick_crtc(struct drm_output *output)
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001434{
1435 struct drm_backend *backend;
1436 struct weston_head *base;
1437 struct drm_head *head;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001438 struct drm_crtc *crtc;
1439 struct drm_crtc *best_crtc = NULL;
1440 struct drm_crtc *fallback_crtc = NULL;
1441 struct drm_crtc *existing_crtc[32];
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001442 uint32_t possible_crtcs = 0xffffffff;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001443 unsigned n = 0;
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001444 uint32_t crtc_id;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001445 unsigned int i;
Pekka Paalanendb4c7d72017-11-28 16:11:00 +02001446 bool match;
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001447
1448 backend = to_drm_backend(output->base.compositor);
1449
1450 /* This algorithm ignores drmModeEncoder::possible_clones restriction,
1451 * because it is more often set wrong than not in the kernel. */
1452
1453 /* Accumulate a mask of possible crtcs and find existing routings. */
1454 wl_list_for_each(base, &output->base.head_list, output_link) {
1455 head = to_drm_head(base);
1456
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001457 possible_crtcs &=
1458 drm_connector_get_possible_crtcs_mask(&head->connector);
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001459
1460 crtc_id = head->inherited_crtc_id;
1461 if (crtc_id > 0 && n < ARRAY_LENGTH(existing_crtc))
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001462 existing_crtc[n++] = drm_crtc_find(backend, crtc_id);
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001463 }
1464
1465 /* Find a crtc that could drive each connector individually at least,
1466 * and prefer existing routings. */
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001467 wl_list_for_each(crtc, &backend->crtc_list, link) {
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001468
1469 /* Could the crtc not drive each connector? */
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001470 if (!(possible_crtcs & (1 << crtc->pipe)))
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001471 continue;
1472
1473 /* Is the crtc already in use? */
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001474 if (crtc->output)
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001475 continue;
1476
1477 /* Try to preserve the existing CRTC -> connector routing;
1478 * it makes initialisation faster, and also since we have a
1479 * very dumb picking algorithm, may preserve a better
1480 * choice. */
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001481 for (i = 0; i < n; i++) {
1482 if (existing_crtc[i] == crtc)
1483 return crtc;
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001484 }
1485
Pekka Paalanendb4c7d72017-11-28 16:11:00 +02001486 /* Check if any other head had existing routing to this CRTC.
1487 * If they did, this is not the best CRTC as it might be needed
1488 * for another output we haven't enabled yet. */
1489 match = false;
1490 wl_list_for_each(base, &backend->compositor->head_list,
1491 compositor_link) {
1492 head = to_drm_head(base);
1493
1494 if (head->base.output == &output->base)
1495 continue;
1496
1497 if (weston_head_is_enabled(&head->base))
1498 continue;
1499
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001500 if (head->inherited_crtc_id == crtc->crtc_id) {
Pekka Paalanendb4c7d72017-11-28 16:11:00 +02001501 match = true;
1502 break;
1503 }
1504 }
1505 if (!match)
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001506 best_crtc = crtc;
Pekka Paalanendb4c7d72017-11-28 16:11:00 +02001507
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001508 fallback_crtc = crtc;
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001509 }
1510
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001511 if (best_crtc)
1512 return best_crtc;
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001513
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001514 if (fallback_crtc)
1515 return fallback_crtc;
Pekka Paalanendb4c7d72017-11-28 16:11:00 +02001516
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001517 /* Likely possible_crtcs was empty due to asking for clones,
1518 * but since the DRM documentation says the kernel lies, let's
1519 * pick one crtc anyway. Trial and error is the only way to
1520 * be sure if something doesn't work. */
1521
1522 /* First pick any existing assignment. */
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001523 for (i = 0; i < n; i++) {
1524 crtc = existing_crtc[i];
1525 if (!crtc->output)
1526 return crtc;
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001527 }
1528
1529 /* Otherwise pick any available crtc. */
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001530 wl_list_for_each(crtc, &backend->crtc_list, link) {
1531 if (!crtc->output)
1532 return crtc;
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001533 }
1534
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001535 return NULL;
1536}
1537
1538/** Create an "empty" drm_crtc. It will only set its ID, pipe and props. After
1539 * all, it adds the object to the DRM-backend CRTC list.
1540 */
1541static struct drm_crtc *
1542drm_crtc_create(struct drm_backend *b, uint32_t crtc_id, uint32_t pipe)
1543{
1544 struct drm_crtc *crtc;
1545 drmModeObjectPropertiesPtr props;
1546
1547 props = drmModeObjectGetProperties(b->drm.fd, crtc_id,
1548 DRM_MODE_OBJECT_CRTC);
1549 if (!props) {
1550 weston_log("failed to get CRTC properties\n");
1551 return NULL;
1552 }
1553
1554 crtc = zalloc(sizeof(*crtc));
1555 if (!crtc)
1556 goto ret;
1557
1558 drm_property_info_populate(b, crtc_props, crtc->props_crtc,
1559 WDRM_CRTC__COUNT, props);
1560 crtc->backend = b;
1561 crtc->crtc_id = crtc_id;
1562 crtc->pipe = pipe;
1563 crtc->output = NULL;
1564
1565 /* Add it to the last position of the DRM-backend CRTC list */
1566 wl_list_insert(b->crtc_list.prev, &crtc->link);
1567
1568ret:
1569 drmModeFreeObjectProperties(props);
1570 return crtc;
1571}
1572
1573/** Destroy a drm_crtc object that was created with drm_crtc_create(). It will
1574 * also remove it from the DRM-backend CRTC list.
1575 */
1576static void
1577drm_crtc_destroy(struct drm_crtc *crtc)
1578{
1579 /* TODO: address the issue below to be able to remove the comment
1580 * from the assert.
1581 *
1582 * https://gitlab.freedesktop.org/wayland/weston/-/issues/421
1583 */
1584
1585 //assert(!crtc->output);
1586
1587 wl_list_remove(&crtc->link);
1588 drm_property_info_free(crtc->props_crtc, WDRM_CRTC__COUNT);
1589 free(crtc);
1590}
1591
1592/** Find all CRTCs of the fd and create drm_crtc objects for them.
1593 *
1594 * The CRTCs are saved in a list of the drm_backend and will keep there until
1595 * the fd gets closed.
1596 *
1597 * @param b The DRM-backend structure.
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03001598 * @param resources The DRM resources, it is taken with drmModeGetResources
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001599 * @return 0 on success (at least one CRTC in the list), -1 on failure.
1600 */
1601static int
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03001602drm_backend_create_crtc_list(struct drm_backend *b, drmModeRes *resources)
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001603{
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001604 struct drm_crtc *crtc, *crtc_tmp;
1605 int i;
1606
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001607 /* Iterate through all CRTCs */
1608 for (i = 0; i < resources->count_crtcs; i++) {
1609
1610 /* Let's create an object for the CRTC and add it to the list */
1611 crtc = drm_crtc_create(b, resources->crtcs[i], i);
1612 if (!crtc)
1613 goto err;
1614 }
1615
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001616 return 0;
1617
1618err:
1619 wl_list_for_each_safe(crtc, crtc_tmp, &b->crtc_list, link)
1620 drm_crtc_destroy(crtc);
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001621 return -1;
1622}
1623
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001624
1625/** Populates scanout and cursor planes for the output. Also sets the topology
1626 * of the planes by adding them to the plane stacking list.
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001627 */
1628static int
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001629drm_output_init_planes(struct drm_output *output)
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001630{
1631 struct drm_backend *b = to_drm_backend(output->base.compositor);
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001632
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001633 output->scanout_plane =
1634 drm_output_find_special_plane(b, output,
1635 WDRM_PLANE_TYPE_PRIMARY);
1636 if (!output->scanout_plane) {
1637 weston_log("Failed to find primary plane for output %s\n",
1638 output->base.name);
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001639 return -1;
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001640 }
1641
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001642 weston_compositor_stack_plane(b->compositor,
1643 &output->scanout_plane->base,
1644 &b->compositor->primary_plane);
1645
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001646 /* Failing to find a cursor plane is not fatal, as we'll fall back
1647 * to software cursor. */
1648 output->cursor_plane =
1649 drm_output_find_special_plane(b, output,
1650 WDRM_PLANE_TYPE_CURSOR);
1651
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001652 if (output->cursor_plane)
1653 weston_compositor_stack_plane(b->compositor,
1654 &output->cursor_plane->base,
1655 NULL);
1656 else
1657 b->cursors_are_broken = true;
Pekka Paalanen663d5e92017-09-08 13:32:40 +03001658
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001659 return 0;
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001660}
1661
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001662/** The opposite of drm_output_init_planes(). First of all it removes the planes
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -03001663 * from the plane stacking list. After all it sets the planes of the output as NULL.
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001664 */
1665static void
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001666drm_output_deinit_planes(struct drm_output *output)
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001667{
1668 struct drm_backend *b = to_drm_backend(output->base.compositor);
1669
Alexandros Frantzis53a71cb2020-05-18 15:26:01 +03001670 /* If the compositor is already shutting down, the planes have already
1671 * been destroyed. */
1672 if (!b->shutting_down) {
Pekka Paalanen3d6721e2020-09-16 15:04:04 +03001673 wl_list_remove(&output->scanout_plane->base.link);
1674 wl_list_init(&output->scanout_plane->base.link);
1675
1676 if (output->cursor_plane) {
1677 wl_list_remove(&output->cursor_plane->base.link);
1678 wl_list_init(&output->cursor_plane->base.link);
1679 /* Turn off hardware cursor */
1680 drmModeSetCursor(b->drm.fd, output->crtc->crtc_id, 0, 0, 0);
1681 }
1682
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -03001683 /* With universal planes, the planes are allocated at startup,
1684 * freed at shutdown, and live on the plane list in between.
1685 * We want the planes to continue to exist and be freed up
1686 * for other outputs.
1687 */
1688 if (output->cursor_plane)
1689 drm_plane_reset_state(output->cursor_plane);
1690 if (output->scanout_plane)
1691 drm_plane_reset_state(output->scanout_plane);
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001692 }
1693
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001694 output->cursor_plane = NULL;
1695 output->scanout_plane = NULL;
1696}
1697
Leandro Ribeiro54293022021-10-12 14:48:36 -03001698static struct weston_drm_format_array *
1699get_scanout_formats(struct drm_backend *b)
1700{
1701 struct weston_compositor *ec = b->compositor;
1702 const struct weston_drm_format_array *renderer_formats;
1703 struct weston_drm_format_array *scanout_formats, union_planes_formats;
1704 struct drm_plane *plane;
1705 int ret;
1706
1707 /* If we got here it means that dma-buf feedback is supported and that
1708 * the renderer has formats/modifiers to expose. */
1709 assert(ec->renderer->get_supported_formats != NULL);
1710 renderer_formats = ec->renderer->get_supported_formats(ec);
1711
1712 scanout_formats = zalloc(sizeof(*scanout_formats));
1713 if (!scanout_formats) {
1714 weston_log("%s: out of memory\n", __func__);
1715 return NULL;
1716 }
1717
1718 weston_drm_format_array_init(&union_planes_formats);
1719 weston_drm_format_array_init(scanout_formats);
1720
1721 /* Compute the union of the format/modifiers of the KMS planes */
1722 wl_list_for_each(plane, &b->plane_list, link) {
1723 /* The scanout formats are used by the dma-buf feedback. But for
1724 * now cursor planes do not support dma-buf buffers, only wl_shm
1725 * buffers. So we skip cursor planes here. */
1726 if (plane->type == WDRM_PLANE_TYPE_CURSOR)
1727 continue;
1728
1729 ret = weston_drm_format_array_join(&union_planes_formats,
1730 &plane->formats);
1731 if (ret < 0)
1732 goto err;
1733 }
1734
1735 /* Compute the intersection between the union of format/modifiers of KMS
1736 * planes and the formats supported by the renderer */
1737 ret = weston_drm_format_array_replace(scanout_formats,
1738 renderer_formats);
1739 if (ret < 0)
1740 goto err;
1741
1742 ret = weston_drm_format_array_intersect(scanout_formats,
1743 &union_planes_formats);
1744 if (ret < 0)
1745 goto err;
1746
1747 weston_drm_format_array_fini(&union_planes_formats);
1748
1749 return scanout_formats;
1750
1751err:
1752 weston_drm_format_array_fini(&union_planes_formats);
1753 weston_drm_format_array_fini(scanout_formats);
Daniel Stonef5086032021-12-06 16:52:19 +00001754 free(scanout_formats);
Leandro Ribeiro54293022021-10-12 14:48:36 -03001755 return NULL;
1756}
1757
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001758/** Pick a CRTC and reserve it for the output.
1759 *
1760 * On failure, the output remains without a CRTC.
1761 *
1762 * @param output The output with no CRTC associated.
1763 * @return 0 on success, -1 on failure.
1764 */
1765static int
1766drm_output_attach_crtc(struct drm_output *output)
1767{
1768 output->crtc = drm_output_pick_crtc(output);
1769 if (!output->crtc) {
1770 weston_log("Output '%s': No available CRTCs.\n",
1771 output->base.name);
1772 return -1;
1773 }
1774
1775 /* Reserve the CRTC for the output */
1776 output->crtc->output = output;
1777
1778 return 0;
1779}
1780
1781/** Release reservation of the CRTC.
1782 *
1783 * Make the CRTC free to be reserved and used by another output.
1784 *
1785 * @param output The output that will release its CRTC.
1786 */
1787static void
1788drm_output_detach_crtc(struct drm_output *output)
1789{
1790 struct drm_backend *b = output->backend;
1791 struct drm_crtc *crtc = output->crtc;
Pekka Paalanen663d5e92017-09-08 13:32:40 +03001792
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001793 crtc->output = NULL;
1794 output->crtc = NULL;
1795
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001796 /* Force resetting unused CRTCs */
1797 b->state_invalid = true;
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001798}
1799
Pekka Paalanenc4db6f72017-09-05 16:37:03 +03001800static int
Armin Krezović08368132016-09-30 14:11:05 +02001801drm_output_enable(struct weston_output *base)
1802{
1803 struct drm_output *output = to_drm_output(base);
1804 struct drm_backend *b = to_drm_backend(base->compositor);
Pekka Paalanen663d5e92017-09-08 13:32:40 +03001805 int ret;
1806
Tomohito Esakib1fb00d2018-01-31 17:50:48 +09001807 assert(!output->virtual);
1808
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001809 ret = drm_output_attach_crtc(output);
Pekka Paalanen663d5e92017-09-08 13:32:40 +03001810 if (ret < 0)
1811 return -1;
1812
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001813 ret = drm_output_init_planes(output);
1814 if (ret < 0)
1815 goto err_crtc;
1816
Pekka Paalanen663d5e92017-09-08 13:32:40 +03001817 if (drm_output_init_gamma_size(output) < 0)
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001818 goto err_planes;
Armin Krezović08368132016-09-30 14:11:05 +02001819
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001820 if (b->pageflip_timeout)
1821 drm_output_pageflip_timer_create(output);
1822
Giulio Camuffo954f1832014-10-11 18:27:30 +03001823 if (b->use_pixman) {
1824 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001825 weston_log("Failed to init output pixman state\n");
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001826 goto err_planes;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001827 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03001828 } else if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001829 weston_log("Failed to init output gl state\n");
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001830 goto err_planes;
Kristian Høgsberg1d1e0a52012-10-21 13:29:26 -04001831 }
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04001832
leng.fang32af9fc2024-06-13 11:22:15 +08001833 drm_enable_aml_output_config(output, base, b);
Pekka Paalanenf8b850d2017-11-15 12:51:01 +02001834 drm_output_init_backlight(output);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001835
Jonas Ådahle5a12252013-04-05 23:07:11 +02001836 output->base.start_repaint_loop = drm_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05001837 output->base.repaint = drm_output_repaint;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001838 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001839 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08001840 output->base.switch_mode = drm_output_switch_mode;
Richard Hughese7299962013-05-01 21:52:12 +01001841 output->base.set_gamma = drm_output_set_gamma;
1842
Pekka Paalanenc0eb2542017-11-15 13:37:18 +02001843 weston_log("Output %s (crtc %d) video modes:\n",
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001844 output->base.name, output->crtc->crtc_id);
Pekka Paalanenc0eb2542017-11-15 13:37:18 +02001845 drm_output_print_modes(output);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001846
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001847 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01001848
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001849err_planes:
1850 drm_output_deinit_planes(output);
1851err_crtc:
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001852 drm_output_detach_crtc(output);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001853 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001854}
1855
Jesse Barnes58ef3792012-02-23 09:45:49 -05001856static void
Armin Krezović08368132016-09-30 14:11:05 +02001857drm_output_deinit(struct weston_output *base)
1858{
1859 struct drm_output *output = to_drm_output(base);
1860 struct drm_backend *b = to_drm_backend(base->compositor);
1861
Daniel Stone3e661f72016-11-04 17:24:06 +00001862 if (b->use_pixman)
Armin Krezović08368132016-09-30 14:11:05 +02001863 drm_output_fini_pixman(output);
Daniel Stone3e661f72016-11-04 17:24:06 +00001864 else
1865 drm_output_fini_egl(output);
Armin Krezović08368132016-09-30 14:11:05 +02001866
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001867 drm_output_deinit_planes(output);
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001868 drm_output_detach_crtc(output);
Armin Krezović08368132016-09-30 14:11:05 +02001869}
1870
1871static void
Pekka Paalanenc112f002017-08-28 16:27:20 +03001872drm_head_destroy(struct drm_head *head);
1873
1874static void
Armin Krezović08368132016-09-30 14:11:05 +02001875drm_output_destroy(struct weston_output *base)
1876{
1877 struct drm_output *output = to_drm_output(base);
1878 struct drm_backend *b = to_drm_backend(base->compositor);
Armin Krezović08368132016-09-30 14:11:05 +02001879
Tomohito Esakib1fb00d2018-01-31 17:50:48 +09001880 assert(!output->virtual);
1881
Daniel Stone31838bf2019-06-17 11:23:25 +01001882 if (output->page_flip_pending || output->atomic_complete_pending) {
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01001883 output->destroy_pending = true;
Armin Krezović08368132016-09-30 14:11:05 +02001884 weston_log("destroy output while page flip pending\n");
1885 return;
1886 }
1887
Alexandros Frantzis10937fe2021-06-14 13:09:44 +03001888 drm_output_set_cursor_view(output, NULL);
1889
Armin Krezović08368132016-09-30 14:11:05 +02001890 if (output->base.enabled)
1891 drm_output_deinit(&output->base);
1892
Pekka Paalanen383b3af2017-09-11 14:40:48 +03001893 drm_mode_list_destroy(b, &output->base.mode_list);
Armin Krezović445b41b2016-10-09 23:48:16 +02001894
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001895 if (output->pageflip_timer)
1896 wl_event_source_remove(output->pageflip_timer);
1897
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03001898 weston_output_release(&output->base);
Armin Krezović08368132016-09-30 14:11:05 +02001899
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001900 assert(!output->state_last);
1901 drm_output_state_free(output->state_cur);
1902
Armin Krezović08368132016-09-30 14:11:05 +02001903 free(output);
1904}
1905
1906static int
1907drm_output_disable(struct weston_output *base)
1908{
1909 struct drm_output *output = to_drm_output(base);
Armin Krezović08368132016-09-30 14:11:05 +02001910
Tomohito Esakib1fb00d2018-01-31 17:50:48 +09001911 assert(!output->virtual);
leng.fang32af9fc2024-06-13 11:22:15 +08001912 drm_output_disable_aml(output, base);
Daniel Stonea08512f2016-11-08 17:46:10 +00001913
Armin Krezović08368132016-09-30 14:11:05 +02001914 if (output->base.enabled)
1915 drm_output_deinit(&output->base);
1916
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01001917 output->disable_pending = false;
Armin Krezović08368132016-09-30 14:11:05 +02001918
Armin Krezović08368132016-09-30 14:11:05 +02001919 return 0;
1920}
1921
Ankit Nautiyala344fe32019-05-14 18:36:08 +05301922/*
1923 * This function converts the protection status from drm values to
1924 * weston_hdcp_protection status. The drm values as read from the connector
1925 * properties "Content Protection" and "HDCP Content Type" need to be converted
1926 * to appropriate weston values, that can be sent to a client application.
1927 */
1928static int
1929get_weston_protection_from_drm(enum wdrm_content_protection_state protection,
1930 enum wdrm_hdcp_content_type type,
1931 enum weston_hdcp_protection *weston_protection)
1932
1933{
1934 if (protection >= WDRM_CONTENT_PROTECTION__COUNT)
1935 return -1;
1936 if (protection == WDRM_CONTENT_PROTECTION_DESIRED ||
1937 protection == WDRM_CONTENT_PROTECTION_UNDESIRED) {
1938 *weston_protection = WESTON_HDCP_DISABLE;
1939 return 0;
1940 }
1941 if (type >= WDRM_HDCP_CONTENT_TYPE__COUNT)
1942 return -1;
1943 if (type == WDRM_HDCP_CONTENT_TYPE0) {
1944 *weston_protection = WESTON_HDCP_ENABLE_TYPE_0;
1945 return 0;
1946 }
1947 if (type == WDRM_HDCP_CONTENT_TYPE1) {
1948 *weston_protection = WESTON_HDCP_ENABLE_TYPE_1;
1949 return 0;
1950 }
1951 return -1;
1952}
1953
1954/**
1955 * Get current content-protection status for a given head.
1956 *
1957 * @param head drm_head, whose protection is to be retrieved
Ankit Nautiyala344fe32019-05-14 18:36:08 +05301958 * @return protection status in case of success, -1 otherwise
1959 */
1960static enum weston_hdcp_protection
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03001961drm_head_get_current_protection(struct drm_head *head)
Ankit Nautiyala344fe32019-05-14 18:36:08 +05301962{
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03001963 drmModeObjectProperties *props = head->connector.props_drm;
Ankit Nautiyala344fe32019-05-14 18:36:08 +05301964 struct drm_property_info *info;
1965 enum wdrm_content_protection_state protection;
1966 enum wdrm_hdcp_content_type type;
1967 enum weston_hdcp_protection weston_hdcp = WESTON_HDCP_DISABLE;
1968
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001969 info = &head->connector.props[WDRM_CONNECTOR_CONTENT_PROTECTION];
Ankit Nautiyala344fe32019-05-14 18:36:08 +05301970 protection = drm_property_get_value(info, props,
1971 WDRM_CONTENT_PROTECTION__COUNT);
1972
1973 if (protection == WDRM_CONTENT_PROTECTION__COUNT)
1974 return WESTON_HDCP_DISABLE;
1975
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001976 info = &head->connector.props[WDRM_CONNECTOR_HDCP_CONTENT_TYPE];
Ankit Nautiyala344fe32019-05-14 18:36:08 +05301977 type = drm_property_get_value(info, props,
1978 WDRM_HDCP_CONTENT_TYPE__COUNT);
1979
1980 /*
1981 * In case of platforms supporting HDCP1.4, only property
1982 * 'Content Protection' is exposed and not the 'HDCP Content Type'
1983 * for such cases HDCP Type 0 should be considered as the content-type.
1984 */
1985
1986 if (type == WDRM_HDCP_CONTENT_TYPE__COUNT)
1987 type = WDRM_HDCP_CONTENT_TYPE0;
1988
1989 if (get_weston_protection_from_drm(protection, type,
1990 &weston_hdcp) == -1) {
1991 weston_log("Invalid drm protection:%d type:%d, for head:%s connector-id:%d\n",
1992 protection, type, head->base.name,
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001993 head->connector.connector_id);
Ankit Nautiyala344fe32019-05-14 18:36:08 +05301994 return WESTON_HDCP_DISABLE;
1995 }
1996
1997 return weston_hdcp;
1998}
1999
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03002000static int
2001drm_connector_update_properties(struct drm_connector *connector)
2002{
2003 drmModeObjectProperties *props;
2004
2005 props = drmModeObjectGetProperties(connector->backend->drm.fd,
2006 connector->connector_id,
2007 DRM_MODE_OBJECT_CONNECTOR);
2008 if (!props) {
2009 weston_log("Error: failed to get connector properties\n");
2010 return -1;
2011 }
2012
2013 if (connector->props_drm)
2014 drmModeFreeObjectProperties(connector->props_drm);
2015 connector->props_drm = props;
2016
2017 return 0;
2018}
2019
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002020/** Replace connector data and monitor information
2021 *
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002022 * @param connector The drm_connector object to be updated.
2023 * @param conn The connector data to be owned by the drm_connector, must match
2024 * the current drm_connector ID.
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002025 * @return 0 on success, -1 on failure.
2026 *
2027 * Takes ownership of @c connector on success, not on failure.
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002028 */
2029static int
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002030drm_connector_assign_connector_info(struct drm_connector *connector,
2031 drmModeConnector *conn)
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002032{
Leandro Ribeiro70863962020-09-09 13:12:35 -03002033 assert(connector->conn != conn);
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002034 assert(connector->connector_id == conn->connector_id);
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002035
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03002036 if (drm_connector_update_properties(connector) < 0)
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002037 return -1;
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002038
leng.fang32af9fc2024-06-13 11:22:15 +08002039 drm_update_helper_connector(connector, conn);
2040
Leandro Ribeiro70863962020-09-09 13:12:35 -03002041 if (connector->conn)
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002042 drmModeFreeConnector(connector->conn);
2043 connector->conn = conn;
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002044
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002045 drm_property_info_free(connector->props, WDRM_CONNECTOR__COUNT);
2046 drm_property_info_populate(connector->backend, connector_props,
2047 connector->props,
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03002048 WDRM_CONNECTOR__COUNT, connector->props_drm);
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002049 return 0;
2050}
2051
Leandro Ribeiro70863962020-09-09 13:12:35 -03002052static void
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002053drm_connector_init(struct drm_backend *b, struct drm_connector *connector,
2054 uint32_t connector_id)
2055{
2056 connector->backend = b;
2057 connector->connector_id = connector_id;
Leandro Ribeiro70863962020-09-09 13:12:35 -03002058 connector->conn = NULL;
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03002059 connector->props_drm = NULL;
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002060}
2061
2062static void
2063drm_connector_fini(struct drm_connector *connector)
2064{
2065 drmModeFreeConnector(connector->conn);
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03002066 drmModeFreeObjectProperties(connector->props_drm);
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002067 drm_property_info_free(connector->props, WDRM_CONNECTOR__COUNT);
2068}
2069
Pekka Paalanen456dc732017-11-09 15:10:11 +02002070static void
2071drm_head_log_info(struct drm_head *head, const char *msg)
2072{
2073 if (head->base.connected) {
2074 weston_log("DRM: head '%s' %s, connector %d is connected, "
2075 "EDID make '%s', model '%s', serial '%s'\n",
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002076 head->base.name, msg, head->connector.connector_id,
Pekka Paalanen456dc732017-11-09 15:10:11 +02002077 head->base.make, head->base.model,
2078 head->base.serial_number ?: "");
2079 } else {
2080 weston_log("DRM: head '%s' %s, connector %d is disconnected.\n",
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002081 head->base.name, msg, head->connector.connector_id);
Pekka Paalanen456dc732017-11-09 15:10:11 +02002082 }
2083}
2084
Pekka Paalanend2e62422017-09-08 15:48:07 +03002085/** Update connector and monitor information
2086 *
2087 * @param head The head to update.
Leandro Ribeiro70863962020-09-09 13:12:35 -03002088 * @param conn The DRM connector object.
2089 * @returns 0 on success, -1 on failure.
Pekka Paalanend2e62422017-09-08 15:48:07 +03002090 *
Leandro Ribeiro70863962020-09-09 13:12:35 -03002091 * Updates monitor information and connection status. This may schedule a
2092 * heads changed call to the user.
Leandro Ribeiro4a5b67a2020-09-15 15:30:35 -03002093 *
2094 * Takes ownership of @c connector on success, not on failure.
Pekka Paalanend2e62422017-09-08 15:48:07 +03002095 */
Leandro Ribeiro70863962020-09-09 13:12:35 -03002096static int
2097drm_head_update_info(struct drm_head *head, drmModeConnector *conn)
Pekka Paalanend2e62422017-09-08 15:48:07 +03002098{
Leandro Ribeiro70863962020-09-09 13:12:35 -03002099 int ret;
Pekka Paalanend2e62422017-09-08 15:48:07 +03002100
Leandro Ribeiro70863962020-09-09 13:12:35 -03002101 ret = drm_connector_assign_connector_info(&head->connector, conn);
Pekka Paalanen456dc732017-11-09 15:10:11 +02002102
Leandro Ribeiro67941642020-09-15 00:11:02 -03002103 update_head_from_connector(head);
2104 weston_head_set_content_protection_status(&head->base,
2105 drm_head_get_current_protection(head));
2106
Pekka Paalanen456dc732017-11-09 15:10:11 +02002107 if (head->base.device_changed)
2108 drm_head_log_info(head, "updated");
Leandro Ribeiro70863962020-09-09 13:12:35 -03002109
2110 return ret;
Pekka Paalanend2e62422017-09-08 15:48:07 +03002111}
2112
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002113/** Update writeback connector
2114 *
2115 * @param writeback The writeback to update.
2116 * @param conn DRM connector object.
2117 * @returns 0 on success, -1 on failure.
2118 *
2119 * Takes ownership of @c connector on success, not on failure.
2120 */
2121static int
2122drm_writeback_update_info(struct drm_writeback *writeback, drmModeConnector *conn)
2123{
2124 int ret;
2125
2126 ret = drm_connector_assign_connector_info(&writeback->connector, conn);
2127
2128 return ret;
2129}
2130
Daniel Stone087ddf02017-02-14 17:51:30 +00002131/**
Pekka Paalanenc112f002017-08-28 16:27:20 +03002132 * Create a Weston head for a connector
2133 *
2134 * Given a DRM connector, create a matching drm_head structure and add it
2135 * to Weston's head list.
2136 *
Marius Vlada2dace22019-06-12 16:05:44 +03002137 * @param backend Weston backend structure
Leandro Ribeiro70863962020-09-09 13:12:35 -03002138 * @param conn DRM connector object
Pekka Paalanenc112f002017-08-28 16:27:20 +03002139 * @param drm_device udev device pointer
Leandro Ribeiro70863962020-09-09 13:12:35 -03002140 * @returns 0 on success, -1 on failure
Leandro Ribeiro4a5b67a2020-09-15 15:30:35 -03002141 *
2142 * Takes ownership of @c connector on success, not on failure.
Pekka Paalanenc112f002017-08-28 16:27:20 +03002143 */
Leandro Ribeiro70863962020-09-09 13:12:35 -03002144static int
2145drm_head_create(struct drm_backend *backend, drmModeConnector *conn,
Pekka Paalanenc112f002017-08-28 16:27:20 +03002146 struct udev_device *drm_device)
2147{
2148 struct drm_head *head;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002149 char *name;
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002150 int ret;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002151
2152 head = zalloc(sizeof *head);
2153 if (!head)
Leandro Ribeiro70863962020-09-09 13:12:35 -03002154 return -1;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002155
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002156 head->backend = backend;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002157
Leandro Ribeiro70863962020-09-09 13:12:35 -03002158 drm_connector_init(backend, &head->connector, conn->connector_id);
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002159
2160 name = make_connector_name(conn);
Pekka Paalanenc112f002017-08-28 16:27:20 +03002161 if (!name)
Leandro Ribeiro70863962020-09-09 13:12:35 -03002162 goto err;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002163
2164 weston_head_init(&head->base, name);
2165 free(name);
2166
Leandro Ribeiro67941642020-09-15 00:11:02 -03002167 ret = drm_head_update_info(head, conn);
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002168 if (ret < 0)
Leandro Ribeiro67941642020-09-15 00:11:02 -03002169 goto err_update;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002170
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002171 head->backlight = backlight_init(drm_device, conn->connector_type);
Pekka Paalanence724242017-09-04 12:21:24 +03002172
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002173 if (conn->connector_type == DRM_MODE_CONNECTOR_LVDS ||
2174 conn->connector_type == DRM_MODE_CONNECTOR_eDP)
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002175 weston_head_set_internal(&head->base);
Pekka Paalanenc112f002017-08-28 16:27:20 +03002176
Pekka Paalanen9c03a7c2017-11-28 14:30:10 +02002177 if (drm_head_read_current_setup(head, backend) < 0) {
Pekka Paalanen13d233e2017-09-11 14:06:11 +03002178 weston_log("Failed to retrieve current mode from connector %d.\n",
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002179 head->connector.connector_id);
Pekka Paalanen6fae2be2017-11-28 14:33:52 +02002180 /* Not fatal. */
Pekka Paalanen13d233e2017-09-11 14:06:11 +03002181 }
2182
Pekka Paalanenc112f002017-08-28 16:27:20 +03002183 weston_compositor_add_head(backend->compositor, &head->base);
Pekka Paalanen456dc732017-11-09 15:10:11 +02002184 drm_head_log_info(head, "found");
leng.fang32af9fc2024-06-13 11:22:15 +08002185 drm_append_helper_connector(head);
Pekka Paalanenc112f002017-08-28 16:27:20 +03002186
Leandro Ribeiro70863962020-09-09 13:12:35 -03002187 return 0;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002188
Leandro Ribeiro67941642020-09-15 00:11:02 -03002189err_update:
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002190 weston_head_release(&head->base);
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002191err:
Leandro Ribeiro70863962020-09-09 13:12:35 -03002192 drm_connector_fini(&head->connector);
Pekka Paalanenc112f002017-08-28 16:27:20 +03002193 free(head);
Leandro Ribeiro70863962020-09-09 13:12:35 -03002194 return -1;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002195}
2196
2197static void
2198drm_head_destroy(struct drm_head *head)
2199{
leng.fang32af9fc2024-06-13 11:22:15 +08002200 drm_delete_helper_connector(head);
Pekka Paalanenc112f002017-08-28 16:27:20 +03002201 weston_head_release(&head->base);
Pekka Paalanence724242017-09-04 12:21:24 +03002202
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002203 drm_connector_fini(&head->connector);
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002204
Pekka Paalanence724242017-09-04 12:21:24 +03002205 if (head->backlight)
2206 backlight_destroy(head->backlight);
2207
Pekka Paalanenc112f002017-08-28 16:27:20 +03002208 free(head);
2209}
2210
2211/**
Armin Krezović08368132016-09-30 14:11:05 +02002212 * Create a Weston output structure
2213 *
Pekka Paalanend2e62422017-09-08 15:48:07 +03002214 * Create an "empty" drm_output. This is the implementation of
2215 * weston_backend::create_output.
Armin Krezović08368132016-09-30 14:11:05 +02002216 *
Pekka Paalanend2e62422017-09-08 15:48:07 +03002217 * Creating an output is usually followed by drm_output_attach_head()
2218 * and drm_output_enable() to make use of it.
2219 *
2220 * @param compositor The compositor instance.
2221 * @param name Name for the new output.
2222 * @returns The output, or NULL on failure.
Armin Krezović08368132016-09-30 14:11:05 +02002223 */
Pekka Paalanend2e62422017-09-08 15:48:07 +03002224static struct weston_output *
2225drm_output_create(struct weston_compositor *compositor, const char *name)
Armin Krezović08368132016-09-30 14:11:05 +02002226{
Pekka Paalanend2e62422017-09-08 15:48:07 +03002227 struct drm_backend *b = to_drm_backend(compositor);
Armin Krezović08368132016-09-30 14:11:05 +02002228 struct drm_output *output;
Armin Krezović08368132016-09-30 14:11:05 +02002229
Armin Krezović08368132016-09-30 14:11:05 +02002230 output = zalloc(sizeof *output);
2231 if (output == NULL)
Pekka Paalanend2e62422017-09-08 15:48:07 +03002232 return NULL;
Armin Krezović08368132016-09-30 14:11:05 +02002233
Daniel Stone64dbbee2018-07-20 19:00:06 +01002234 output->backend = b;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002235 output->crtc = NULL;
2236
Stefan Agnerccf24072019-07-09 22:02:00 +02002237#ifdef BUILD_DRM_GBM
Tomohito Esaki718a40b2018-01-31 17:50:15 +09002238 output->gbm_bo_flags = GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING;
Stefan Agnerccf24072019-07-09 22:02:00 +02002239#endif
Daniel Stone64dbbee2018-07-20 19:00:06 +01002240
Pekka Paalanend2e62422017-09-08 15:48:07 +03002241 weston_output_init(&output->base, compositor, name);
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002242
Armin Krezović08368132016-09-30 14:11:05 +02002243 output->base.enable = drm_output_enable;
2244 output->base.destroy = drm_output_destroy;
2245 output->base.disable = drm_output_disable;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002246 output->base.attach_head = drm_output_attach_head;
Pekka Paalanen7f853792017-11-29 14:33:33 +02002247 output->base.detach_head = drm_output_detach_head;
Armin Krezović08368132016-09-30 14:11:05 +02002248
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01002249 output->destroy_pending = false;
2250 output->disable_pending = false;
Armin Krezović08368132016-09-30 14:11:05 +02002251
Pekka Paalanen01f60212017-03-24 15:39:24 +02002252 output->state_cur = drm_output_state_alloc(output, NULL);
Pekka Paalanena0bfedc2017-04-03 14:42:51 +03002253
leng.fang32af9fc2024-06-13 11:22:15 +08002254 drm_init_aml_output_config(compositor, output);
Armin Krezović08368132016-09-30 14:11:05 +02002255 weston_compositor_add_pending_output(&output->base, b->compositor);
2256
Pekka Paalanend2e62422017-09-08 15:48:07 +03002257 return &output->base;
Armin Krezović08368132016-09-30 14:11:05 +02002258}
2259
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002260/**
2261 * Create a Weston writeback for a writeback connector
Leandro Ribeiro72430222020-09-14 17:41:41 -03002262 *
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002263 * Given a DRM connector of type writeback, create a matching drm_writeback
2264 * structure and add it to Weston's writeback list.
2265 *
2266 * @param b Weston backend structure
2267 * @param conn DRM connector object of type writeback
2268 * @returns 0 on success, -1 on failure
2269 *
2270 * Takes ownership of @c connector on success, not on failure.
2271 */
2272static int
2273drm_writeback_create(struct drm_backend *b, drmModeConnector *conn)
2274{
2275 struct drm_writeback *writeback;
2276 int ret;
2277
2278 writeback = zalloc(sizeof *writeback);
2279 assert(writeback);
2280
2281 writeback->backend = b;
2282
2283 drm_connector_init(b, &writeback->connector, conn->connector_id);
2284
2285 ret = drm_writeback_update_info(writeback, conn);
2286 if (ret < 0)
2287 goto err;
2288
2289 wl_list_insert(&b->writeback_connector_list, &writeback->link);
2290 return 0;
2291
2292err:
2293 drm_connector_fini(&writeback->connector);
2294 free(writeback);
2295 return -1;
2296}
2297
2298static void
2299drm_writeback_destroy(struct drm_writeback *writeback)
2300{
2301 drm_connector_fini(&writeback->connector);
2302 wl_list_remove(&writeback->link);
2303
2304 free(writeback);
2305}
2306
2307/** Given the DRM connector object of a connector, create drm_head or
2308 * drm_writeback object (depending on the type of connector) for it.
2309 *
2310 * The object is then added to the DRM-backend list of heads or writebacks.
Leandro Ribeiro72430222020-09-14 17:41:41 -03002311 *
2312 * @param b The DRM-backend structure
2313 * @param conn The DRM connector object
2314 * @param drm_device udev device pointer
2315 * @return 0 on success, -1 on failure
2316 */
2317static int
2318drm_backend_add_connector(struct drm_backend *b, drmModeConnector *conn,
2319 struct udev_device *drm_device)
2320{
2321 int ret;
2322
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002323 if (conn->connector_type == DRM_MODE_CONNECTOR_WRITEBACK) {
2324 ret = drm_writeback_create(b, conn);
2325 if (ret < 0)
2326 weston_log("DRM: failed to create writeback for connector %d.\n",
2327 conn->connector_id);
2328 } else {
2329 ret = drm_head_create(b, conn, drm_device);
2330 if (ret < 0)
2331 weston_log("DRM: failed to create head for connector %d.\n",
2332 conn->connector_id);
2333 }
Leandro Ribeiro72430222020-09-14 17:41:41 -03002334
2335 return ret;
2336}
2337
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002338/** Find all connectors of the fd and create drm_head or drm_writeback objects
2339 * (depending on the type of connector they are) for each of them
2340 *
2341 * These objects are added to the DRM-backend lists of heads and writebacks.
2342 *
2343 * @param b The DRM-backend structure
2344 * @param drm_device udev device pointer
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03002345 * @param resources The DRM resources, it is taken with drmModeGetResources
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002346 * @return 0 on success, -1 on failure
2347 */
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002348static int
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03002349drm_backend_discover_connectors(struct drm_backend *b, struct udev_device *drm_device,
2350 drmModeRes *resources)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002351{
Leandro Ribeiro70863962020-09-09 13:12:35 -03002352 drmModeConnector *conn;
2353 int i, ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002354
Giulio Camuffo954f1832014-10-11 18:27:30 +03002355 b->min_width = resources->min_width;
2356 b->max_width = resources->max_width;
2357 b->min_height = resources->min_height;
2358 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05002359
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002360 for (i = 0; i < resources->count_connectors; i++) {
Pekka Paalanend2e62422017-09-08 15:48:07 +03002361 uint32_t connector_id = resources->connectors[i];
Daniel Stone02cf4662017-03-03 16:19:39 +00002362
Leandro Ribeiro70863962020-09-09 13:12:35 -03002363 conn = drmModeGetConnector(b->drm.fd, connector_id);
2364 if (!conn)
2365 continue;
2366
Leandro Ribeiro72430222020-09-14 17:41:41 -03002367 ret = drm_backend_add_connector(b, conn, drm_device);
2368 if (ret < 0)
Leandro Ribeiro70863962020-09-09 13:12:35 -03002369 drmModeFreeConnector(conn);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002370 }
2371
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002372 return 0;
2373}
2374
Leandro Ribeiro99611c82020-09-14 17:19:12 -03002375static bool
2376resources_has_connector(drmModeRes *resources, uint32_t connector_id)
2377{
2378 for (int i = 0; i < resources->count_connectors; i++) {
2379 if (resources->connectors[i] == connector_id)
2380 return true;
2381 }
2382
2383 return false;
2384}
2385
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002386static void
Leandro Ribeiro993920d2020-09-14 17:01:01 -03002387drm_backend_update_connectors(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002388{
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002389 drmModeRes *resources;
Leandro Ribeiro70863962020-09-09 13:12:35 -03002390 drmModeConnector *conn;
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002391 struct weston_head *base, *base_next;
Pekka Paalanena0a37462017-08-31 15:41:57 +03002392 struct drm_head *head;
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002393 struct drm_writeback *writeback, *writeback_next;
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002394 uint32_t connector_id;
Leandro Ribeiro70863962020-09-09 13:12:35 -03002395 int i, ret;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002396
Giulio Camuffo954f1832014-10-11 18:27:30 +03002397 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002398 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002399 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002400 return;
2401 }
2402
Pekka Paalanend2e62422017-09-08 15:48:07 +03002403 /* collect new connectors that have appeared, e.g. MST */
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002404 for (i = 0; i < resources->count_connectors; i++) {
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002405 connector_id = resources->connectors[i];
Benjamin Franzke117483d2011-08-30 11:38:26 +02002406
Leandro Ribeiro70863962020-09-09 13:12:35 -03002407 conn = drmModeGetConnector(b->drm.fd, connector_id);
2408 if (!conn)
2409 continue;
2410
Pekka Paalanend2e62422017-09-08 15:48:07 +03002411 head = drm_head_find_by_connector(b, connector_id);
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002412 writeback = drm_writeback_find_by_connector(b, connector_id);
2413
2414 /* Connector can't be owned by both a head and a writeback, so
2415 * one of the searches must fail. */
2416 assert(head == NULL || writeback == NULL);
2417
Leandro Ribeiro72430222020-09-14 17:41:41 -03002418 if (head)
Leandro Ribeiro70863962020-09-09 13:12:35 -03002419 ret = drm_head_update_info(head, conn);
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002420 else if (writeback)
2421 ret = drm_writeback_update_info(writeback, conn);
Leandro Ribeiro72430222020-09-14 17:41:41 -03002422 else
2423 ret = drm_backend_add_connector(b, conn, drm_device);
2424
2425 if (ret < 0)
2426 drmModeFreeConnector(conn);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002427 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002428
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002429 /* Destroy head objects of connectors (except writeback connectors) that
2430 * have disappeared. */
2431 wl_list_for_each_safe(base, base_next,
Pekka Paalanena0a37462017-08-31 15:41:57 +03002432 &b->compositor->head_list, compositor_link) {
Pekka Paalanena0a37462017-08-31 15:41:57 +03002433 head = to_drm_head(base);
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002434 connector_id = head->connector.connector_id;
Pekka Paalanena0a37462017-08-31 15:41:57 +03002435
Leandro Ribeiro99611c82020-09-14 17:19:12 -03002436 if (resources_has_connector(resources, connector_id))
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002437 continue;
2438
Pekka Paalanend2e62422017-09-08 15:48:07 +03002439 weston_log("DRM: head '%s' (connector %d) disappeared.\n",
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002440 head->base.name, connector_id);
Pekka Paalanend2e62422017-09-08 15:48:07 +03002441 drm_head_destroy(head);
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002442 }
2443
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002444 /* Destroy writeback objects of writeback connectors that have
2445 * disappeared. */
2446 wl_list_for_each_safe(writeback, writeback_next,
2447 &b->writeback_connector_list, link) {
2448 connector_id = writeback->connector.connector_id;
2449
2450 if (resources_has_connector(resources, connector_id))
2451 continue;
2452
2453 weston_log("DRM: writeback connector (connector %d) disappeared.\n",
2454 connector_id);
2455 drm_writeback_destroy(writeback);
2456 }
2457
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002458 drmModeFreeResources(resources);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002459}
2460
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302461static enum wdrm_connector_property
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002462drm_connector_find_property_by_id(struct drm_connector *connector,
2463 uint32_t property_id)
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302464{
2465 int i;
2466 enum wdrm_connector_property prop = WDRM_CONNECTOR__COUNT;
2467
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002468 if (!connector || !property_id)
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302469 return WDRM_CONNECTOR__COUNT;
2470
2471 for (i = 0; i < WDRM_CONNECTOR__COUNT; i++)
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002472 if (connector->props[i].prop_id == property_id) {
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302473 prop = (enum wdrm_connector_property) i;
2474 break;
2475 }
2476 return prop;
2477}
2478
2479static void
2480drm_backend_update_conn_props(struct drm_backend *b,
2481 uint32_t connector_id,
2482 uint32_t property_id)
2483{
2484 struct drm_head *head;
2485 enum wdrm_connector_property conn_prop;
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302486
2487 head = drm_head_find_by_connector(b, connector_id);
2488 if (!head) {
2489 weston_log("DRM: failed to find head for connector id: %d.\n",
2490 connector_id);
2491 return;
2492 }
2493
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002494 conn_prop = drm_connector_find_property_by_id(&head->connector, property_id);
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302495 if (conn_prop >= WDRM_CONNECTOR__COUNT)
2496 return;
2497
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03002498 if (drm_connector_update_properties(&head->connector) < 0)
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302499 return;
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03002500
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302501 if (conn_prop == WDRM_CONNECTOR_CONTENT_PROTECTION) {
2502 weston_head_set_content_protection_status(&head->base,
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03002503 drm_head_get_current_protection(head));
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302504 }
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302505}
2506
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002507static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002508udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002509{
David Herrmannd7488c22012-03-11 20:05:21 +01002510 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01002511 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01002512
2513 sysnum = udev_device_get_sysnum(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002514 if (!sysnum || atoi(sysnum) != b->drm.id)
David Herrmannd7488c22012-03-11 20:05:21 +01002515 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02002516
David Herrmann6ac52db2012-03-11 20:05:22 +01002517 val = udev_device_get_property_value(device, "HOTPLUG");
2518 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002519 return 0;
2520
David Herrmann6ac52db2012-03-11 20:05:22 +01002521 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002522}
2523
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002524static int
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302525udev_event_is_conn_prop_change(struct drm_backend *b,
2526 struct udev_device *device,
2527 uint32_t *connector_id,
2528 uint32_t *property_id)
2529
2530{
2531 const char *val;
2532 int id;
2533
2534 val = udev_device_get_property_value(device, "CONNECTOR");
2535 if (!val || !safe_strtoint(val, &id))
2536 return 0;
2537 else
2538 *connector_id = id;
2539
2540 val = udev_device_get_property_value(device, "PROPERTY");
2541 if (!val || !safe_strtoint(val, &id))
2542 return 0;
2543 else
2544 *property_id = id;
2545
2546 return 1;
2547}
2548
2549static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002550udev_drm_event(int fd, uint32_t mask, void *data)
2551{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002552 struct drm_backend *b = data;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002553 struct udev_device *event;
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302554 uint32_t conn_id, prop_id;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002555
Giulio Camuffo954f1832014-10-11 18:27:30 +03002556 event = udev_monitor_receive_device(b->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002557
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302558 if (udev_event_is_hotplug(b, event)) {
2559 if (udev_event_is_conn_prop_change(b, event, &conn_id, &prop_id))
2560 drm_backend_update_conn_props(b, conn_id, prop_id);
2561 else
Leandro Ribeiro993920d2020-09-14 17:01:01 -03002562 drm_backend_update_connectors(b, event);
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302563 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002564
leng.fang32af9fc2024-06-13 11:22:15 +08002565#ifdef BUILD_AML_TV
2566 drm_backend_afr_update(b, udev_event_frame_rate_hint(event),
2567 udev_event_is_vdin_event(event));
2568#endif
2569
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002570 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002571
2572 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002573}
2574
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002575static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002576drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002577{
Armin Krezović545dba62016-08-05 15:54:18 +02002578 struct drm_backend *b = to_drm_backend(ec);
Pekka Paalanenc112f002017-08-28 16:27:20 +03002579 struct weston_head *base, *next;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002580 struct drm_crtc *crtc, *crtc_tmp;
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002581 struct drm_writeback *writeback, *writeback_tmp;
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002582
leng.fang32af9fc2024-06-13 11:22:15 +08002583#ifdef ENABLE_DRM_HELP
2584 stop_help_worker();
2585#endif
2586
Giulio Camuffo954f1832014-10-11 18:27:30 +03002587 udev_input_destroy(&b->input);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002588
Giulio Camuffo954f1832014-10-11 18:27:30 +03002589 wl_event_source_remove(b->udev_drm_source);
2590 wl_event_source_remove(b->drm_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002591
Daniel Stoneb57c6a02017-10-05 16:27:21 +01002592 b->shutting_down = true;
2593
Giulio Camuffo954f1832014-10-11 18:27:30 +03002594 destroy_sprites(b);
Kristian Høgsberg3d64a3e2013-05-10 12:36:04 -04002595
Leandro Ribeirof0149642019-12-18 15:52:18 -03002596 weston_log_scope_destroy(b->debug);
Daniel Stone1cbe1f92018-07-20 10:21:28 +01002597 b->debug = NULL;
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03002598 weston_compositor_shutdown(ec);
2599
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002600 wl_list_for_each_safe(crtc, crtc_tmp, &b->crtc_list, link)
2601 drm_crtc_destroy(crtc);
2602
Pekka Paalanenc112f002017-08-28 16:27:20 +03002603 wl_list_for_each_safe(base, next, &ec->head_list, compositor_link)
2604 drm_head_destroy(to_drm_head(base));
2605
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002606 wl_list_for_each_safe(writeback, writeback_tmp,
2607 &b->writeback_connector_list, link)
2608 drm_writeback_destroy(writeback);
2609
Stefan Agnerccf24072019-07-09 22:02:00 +02002610#ifdef BUILD_DRM_GBM
Giulio Camuffo954f1832014-10-11 18:27:30 +03002611 if (b->gbm)
2612 gbm_device_destroy(b->gbm);
Stefan Agnerccf24072019-07-09 22:02:00 +02002613#endif
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002614
Pekka Paalanen5b0aa552017-12-07 16:06:05 +02002615 udev_monitor_unref(b->udev_monitor);
Pekka Paalanen2a0c6c32017-09-13 16:48:01 +03002616 udev_unref(b->udev);
2617
Marius Vlad7a44ee72021-06-23 16:02:35 +03002618 weston_launcher_close(ec->launcher, b->drm.fd);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002619 weston_launcher_destroy(ec->launcher);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002620
Pekka Paalanen9bf4f372017-12-07 16:05:29 +02002621 free(b->drm.filename);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002622 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002623}
2624
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002625static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002626session_notify(struct wl_listener *listener, void *data)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002627{
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002628 struct weston_compositor *compositor = data;
Armin Krezović545dba62016-08-05 15:54:18 +02002629 struct drm_backend *b = to_drm_backend(compositor);
Daniel Stone085d2b92015-05-21 00:00:57 +01002630 struct drm_plane *plane;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002631 struct drm_output *output;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002632 struct drm_crtc *crtc;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002633
Giulio Camuffo954f1832014-10-11 18:27:30 +03002634 if (compositor->session_active) {
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002635 weston_log("activating session\n");
Daniel Stonef33e1042016-11-05 08:10:13 +00002636 weston_compositor_wake(compositor);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002637 weston_compositor_damage_all(compositor);
Daniel Stone6020f472018-02-05 15:46:20 +00002638 b->state_invalid = true;
leng.fang32af9fc2024-06-13 11:22:15 +08002639 b->allow_modeset = true;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002640 udev_input_enable(&b->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002641 } else {
2642 weston_log("deactivating session\n");
leng.fang32af9fc2024-06-13 11:22:15 +08002643 drm_change_to_dummy_mode(b->compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002644 udev_input_disable(&b->input);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04002645
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002646 weston_compositor_offscreen(compositor);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04002647
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002648 /* If we have a repaint scheduled (either from a
2649 * pending pageflip or the idle handler), make sure we
2650 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002651 * vt switched away. The OFFSCREEN state will prevent
Abdur Rehman4dca0e12017-01-01 19:46:35 +05002652 * further attempts at repainting. When we switch
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002653 * back, we schedule a repaint, which will process
2654 * pending frame callbacks. */
2655
Giulio Camuffo954f1832014-10-11 18:27:30 +03002656 wl_list_for_each(output, &compositor->output_list, base.link) {
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002657 crtc = output->crtc;
Daniel Stone09a97e22017-03-01 11:34:06 +00002658 output->base.repaint_needed = false;
Daniel Stone2ba17f42015-05-19 20:02:41 +01002659 if (output->cursor_plane)
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002660 drmModeSetCursor(b->drm.fd, crtc->crtc_id,
Daniel Stone2ba17f42015-05-19 20:02:41 +01002661 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002662 }
2663
Giulio Camuffo954f1832014-10-11 18:27:30 +03002664 output = container_of(compositor->output_list.next,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002665 struct drm_output, base.link);
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002666 crtc = output->crtc;
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002667
Daniel Stone085d2b92015-05-21 00:00:57 +01002668 wl_list_for_each(plane, &b->plane_list, link) {
2669 if (plane->type != WDRM_PLANE_TYPE_OVERLAY)
2670 continue;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002671 drmModeSetPlane(b->drm.fd, plane->plane_id, crtc->crtc_id,
2672 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
Daniel Stone085d2b92015-05-21 00:00:57 +01002673 }
2674 }
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002675}
2676
Robert Beckett8d23ab72019-06-13 16:55:44 +01002677
2678/**
2679 * Handle KMS GPU being added/removed
2680 *
2681 * If the device being added/removed is the KMS device, we activate/deactivate
2682 * the compositor session.
2683 *
2684 * @param compositor The compositor instance.
2685 * @param device The device being added/removed.
2686 * @param added Whether the device is being added (or removed)
2687 */
2688static void
2689drm_device_changed(struct weston_compositor *compositor,
2690 dev_t device, bool added)
2691{
2692 struct drm_backend *b = to_drm_backend(compositor);
2693
Robert Beckett49dc3202019-07-02 16:31:22 +01002694 if (b->drm.fd < 0 || b->drm.devnum != device ||
2695 compositor->session_active == added)
Robert Beckett8d23ab72019-06-13 16:55:44 +01002696 return;
2697
2698 compositor->session_active = added;
2699 wl_signal_emit(&compositor->session_signal, compositor);
2700}
2701
Daniel Stoneefa504f2016-12-19 16:48:20 +00002702/**
2703 * Determines whether or not a device is capable of modesetting. If successful,
2704 * sets b->drm.fd and b->drm.filename to the opened device.
2705 */
2706static bool
2707drm_device_is_kms(struct drm_backend *b, struct udev_device *device)
2708{
2709 const char *filename = udev_device_get_devnode(device);
2710 const char *sysnum = udev_device_get_sysnum(device);
Robert Beckett8d23ab72019-06-13 16:55:44 +01002711 dev_t devnum = udev_device_get_devnum(device);
Daniel Stoneefa504f2016-12-19 16:48:20 +00002712 drmModeRes *res;
Marius Vlad7d070ca2018-11-23 14:02:07 +02002713 int id = -1, fd;
Daniel Stoneefa504f2016-12-19 16:48:20 +00002714
2715 if (!filename)
2716 return false;
2717
2718 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
2719 if (fd < 0)
2720 return false;
2721
2722 res = drmModeGetResources(fd);
2723 if (!res)
2724 goto out_fd;
2725
2726 if (res->count_crtcs <= 0 || res->count_connectors <= 0 ||
2727 res->count_encoders <= 0)
2728 goto out_res;
2729
2730 if (sysnum)
2731 id = atoi(sysnum);
2732 if (!sysnum || id < 0) {
2733 weston_log("couldn't get sysnum for device %s\n", filename);
2734 goto out_res;
2735 }
2736
2737 /* We can be called successfully on multiple devices; if we have,
2738 * clean up old entries. */
2739 if (b->drm.fd >= 0)
2740 weston_launcher_close(b->compositor->launcher, b->drm.fd);
2741 free(b->drm.filename);
2742
2743 b->drm.fd = fd;
2744 b->drm.id = id;
2745 b->drm.filename = strdup(filename);
Robert Beckett8d23ab72019-06-13 16:55:44 +01002746 b->drm.devnum = devnum;
Daniel Stoneefa504f2016-12-19 16:48:20 +00002747
Sergi Granellceb59812017-03-28 12:44:04 +02002748 drmModeFreeResources(res);
2749
Daniel Stoneefa504f2016-12-19 16:48:20 +00002750 return true;
2751
2752out_res:
2753 drmModeFreeResources(res);
2754out_fd:
2755 weston_launcher_close(b->compositor->launcher, fd);
2756 return false;
2757}
2758
David Herrmann0af066f2012-10-29 19:21:16 +01002759/*
2760 * Find primary GPU
2761 * Some systems may have multiple DRM devices attached to a single seat. This
2762 * function loops over all devices and tries to find a PCI device with the
2763 * boot_vga sysfs attribute set to 1.
2764 * If no such device is found, the first DRM device reported by udev is used.
Daniel Stoneefa504f2016-12-19 16:48:20 +00002765 * Devices are also vetted to make sure they are are capable of modesetting,
2766 * rather than pure render nodes (GPU with no display), or pure
2767 * memory-allocation devices (VGEM).
David Herrmann0af066f2012-10-29 19:21:16 +01002768 */
2769static struct udev_device*
Giulio Camuffo954f1832014-10-11 18:27:30 +03002770find_primary_gpu(struct drm_backend *b, const char *seat)
David Herrmann0af066f2012-10-29 19:21:16 +01002771{
2772 struct udev_enumerate *e;
2773 struct udev_list_entry *entry;
2774 const char *path, *device_seat, *id;
2775 struct udev_device *device, *drm_device, *pci;
2776
Giulio Camuffo954f1832014-10-11 18:27:30 +03002777 e = udev_enumerate_new(b->udev);
David Herrmann0af066f2012-10-29 19:21:16 +01002778 udev_enumerate_add_match_subsystem(e, "drm");
2779 udev_enumerate_add_match_sysname(e, "card[0-9]*");
2780
2781 udev_enumerate_scan_devices(e);
2782 drm_device = NULL;
2783 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Daniel Stoneefa504f2016-12-19 16:48:20 +00002784 bool is_boot_vga = false;
2785
David Herrmann0af066f2012-10-29 19:21:16 +01002786 path = udev_list_entry_get_name(entry);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002787 device = udev_device_new_from_syspath(b->udev, path);
David Herrmann0af066f2012-10-29 19:21:16 +01002788 if (!device)
2789 continue;
2790 device_seat = udev_device_get_property_value(device, "ID_SEAT");
2791 if (!device_seat)
2792 device_seat = default_seat;
2793 if (strcmp(device_seat, seat)) {
2794 udev_device_unref(device);
2795 continue;
2796 }
2797
2798 pci = udev_device_get_parent_with_subsystem_devtype(device,
2799 "pci", NULL);
2800 if (pci) {
2801 id = udev_device_get_sysattr_value(pci, "boot_vga");
Daniel Stoneefa504f2016-12-19 16:48:20 +00002802 if (id && !strcmp(id, "1"))
2803 is_boot_vga = true;
David Herrmann0af066f2012-10-29 19:21:16 +01002804 }
2805
Daniel Stoneefa504f2016-12-19 16:48:20 +00002806 /* If we already have a modesetting-capable device, and this
2807 * device isn't our boot-VGA device, we aren't going to use
2808 * it. */
2809 if (!is_boot_vga && drm_device) {
David Herrmann0af066f2012-10-29 19:21:16 +01002810 udev_device_unref(device);
Daniel Stoneefa504f2016-12-19 16:48:20 +00002811 continue;
2812 }
2813
2814 /* Make sure this device is actually capable of modesetting;
2815 * if this call succeeds, b->drm.{fd,filename} will be set,
2816 * and any old values freed. */
2817 if (!drm_device_is_kms(b, device)) {
2818 udev_device_unref(device);
2819 continue;
2820 }
2821
2822 /* There can only be one boot_vga device, and we try to use it
2823 * at all costs. */
2824 if (is_boot_vga) {
2825 if (drm_device)
2826 udev_device_unref(drm_device);
2827 drm_device = device;
2828 break;
2829 }
2830
2831 /* Per the (!is_boot_vga && drm_device) test above, we only
2832 * trump existing saved devices with boot-VGA devices, so if
2833 * we end up here, this must be the first device we've seen. */
2834 assert(!drm_device);
2835 drm_device = device;
David Herrmann0af066f2012-10-29 19:21:16 +01002836 }
2837
Daniel Stoneefa504f2016-12-19 16:48:20 +00002838 /* If we're returning a device to use, we must have an open FD for
2839 * it. */
2840 assert(!!drm_device == (b->drm.fd >= 0));
2841
David Herrmann0af066f2012-10-29 19:21:16 +01002842 udev_enumerate_unref(e);
2843 return drm_device;
2844}
2845
Pekka Paalanenb45ed8b2017-03-28 18:04:27 +03002846static struct udev_device *
2847open_specific_drm_device(struct drm_backend *b, const char *name)
2848{
2849 struct udev_device *device;
2850
2851 device = udev_device_new_from_subsystem_sysname(b->udev, "drm", name);
2852 if (!device) {
2853 weston_log("ERROR: could not open DRM device '%s'\n", name);
2854 return NULL;
2855 }
2856
2857 if (!drm_device_is_kms(b, device)) {
2858 udev_device_unref(device);
2859 weston_log("ERROR: DRM device '%s' is not a KMS device.\n", name);
2860 return NULL;
2861 }
2862
2863 /* If we're returning a device to use, we must have an open FD for
2864 * it. */
2865 assert(b->drm.fd >= 0);
2866
2867 return device;
2868}
2869
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002870static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002871planes_binding(struct weston_keyboard *keyboard, const struct timespec *time,
2872 uint32_t key, void *data)
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002873{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002874 struct drm_backend *b = data;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002875
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002876 switch (key) {
2877 case KEY_C:
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01002878 b->cursors_are_broken ^= true;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002879 break;
2880 case KEY_V:
Daniel Stone87fab1c2019-06-17 11:13:20 +01002881 /* We don't support overlay-plane usage with legacy KMS. */
2882 if (b->atomic_modeset)
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01002883 b->sprites_are_broken ^= true;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002884 break;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002885 default:
2886 break;
2887 }
Ander Conselvan de Oliveira180f42a2012-11-21 15:11:37 +02002888}
2889
Kristian Høgsberg0eac34a2013-08-30 14:28:22 -07002890#ifdef BUILD_VAAPI_RECORDER
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002891static void
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002892recorder_destroy(struct drm_output *output)
2893{
2894 vaapi_recorder_destroy(output->recorder);
2895 output->recorder = NULL;
2896
Ankit Nautiyal93dde242019-07-08 11:46:42 +05302897 weston_output_disable_planes_decr(&output->base);
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002898
2899 wl_list_remove(&output->recorder_frame_listener.link);
2900 weston_log("[libva recorder] done\n");
2901}
2902
2903static void
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002904recorder_frame_notify(struct wl_listener *listener, void *data)
2905{
2906 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002907 struct drm_backend *b;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002908 int fd, ret;
2909
2910 output = container_of(listener, struct drm_output,
2911 recorder_frame_listener);
Armin Krezović545dba62016-08-05 15:54:18 +02002912 b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002913
2914 if (!output->recorder)
2915 return;
2916
Daniel Stonee2e80132018-01-16 15:37:33 +00002917 ret = drmPrimeHandleToFD(b->drm.fd,
Daniel Stone8eece0c2016-11-17 17:54:00 +00002918 output->scanout_plane->state_cur->fb->handles[0],
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002919 DRM_CLOEXEC, &fd);
2920 if (ret) {
2921 weston_log("[libva recorder] "
2922 "failed to create prime fd for front buffer\n");
2923 return;
2924 }
2925
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002926 ret = vaapi_recorder_frame(output->recorder, fd,
Daniel Stone8eece0c2016-11-17 17:54:00 +00002927 output->scanout_plane->state_cur->fb->strides[0]);
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002928 if (ret < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +02002929 weston_log("[libva recorder] aborted: %s\n", strerror(errno));
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002930 recorder_destroy(output);
2931 }
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002932}
2933
2934static void *
Giulio Camuffo954f1832014-10-11 18:27:30 +03002935create_recorder(struct drm_backend *b, int width, int height,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002936 const char *filename)
2937{
2938 int fd;
2939 drm_magic_t magic;
2940
Giulio Camuffo954f1832014-10-11 18:27:30 +03002941 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002942 if (fd < 0)
2943 return NULL;
2944
2945 drmGetMagic(fd, &magic);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002946 drmAuthMagic(b->drm.fd, magic);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002947
2948 return vaapi_recorder_create(fd, width, height, filename);
2949}
2950
2951static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002952recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time,
2953 uint32_t key, void *data)
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002954{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002955 struct drm_backend *b = data;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002956 struct drm_output *output;
2957 int width, height;
2958
Giulio Camuffo954f1832014-10-11 18:27:30 +03002959 output = container_of(b->compositor->output_list.next,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002960 struct drm_output, base.link);
2961
2962 if (!output->recorder) {
Stefan Agner4a18f302019-10-20 18:25:42 +02002963 if (output->gbm_format != DRM_FORMAT_XRGB8888) {
Ander Conselvan de Oliveira2ef1cd12014-05-06 16:49:06 +03002964 weston_log("failed to start vaapi recorder: "
2965 "output format not supported\n");
2966 return;
2967 }
2968
Hardeningff39efa2013-09-18 23:56:35 +02002969 width = output->base.current_mode->width;
2970 height = output->base.current_mode->height;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002971
2972 output->recorder =
Giulio Camuffo954f1832014-10-11 18:27:30 +03002973 create_recorder(b, width, height, "capture.h264");
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002974 if (!output->recorder) {
2975 weston_log("failed to create vaapi recorder\n");
2976 return;
2977 }
2978
Ankit Nautiyal93dde242019-07-08 11:46:42 +05302979 weston_output_disable_planes_incr(&output->base);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002980
2981 output->recorder_frame_listener.notify = recorder_frame_notify;
2982 wl_signal_add(&output->base.frame_signal,
2983 &output->recorder_frame_listener);
2984
2985 weston_output_schedule_repaint(&output->base);
2986
2987 weston_log("[libva recorder] initialized\n");
2988 } else {
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002989 recorder_destroy(output);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002990 }
2991}
2992#else
2993static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002994recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time,
2995 uint32_t key, void *data)
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002996{
2997 weston_log("Compiled without libva support\n");
2998}
2999#endif
3000
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003001
Armin Krezović08368132016-09-30 14:11:05 +02003002static const struct weston_drm_output_api api = {
3003 drm_output_set_mode,
3004 drm_output_set_gbm_format,
3005 drm_output_set_seat,
3006};
3007
Giulio Camuffo954f1832014-10-11 18:27:30 +03003008static struct drm_backend *
3009drm_backend_create(struct weston_compositor *compositor,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003010 struct weston_drm_backend_config *config)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003011{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003012 struct drm_backend *b;
David Herrmann0af066f2012-10-29 19:21:16 +01003013 struct udev_device *drm_device;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003014 struct wl_event_loop *loop;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003015 const char *seat_id = default_seat;
nerdopolisb16c4ac2018-06-29 08:17:46 -04003016 const char *session_seat;
Leandro Ribeiro54293022021-10-12 14:48:36 -03003017 struct weston_drm_format_array *scanout_formats;
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03003018 drmModeRes *res;
Armin Krezović08368132016-09-30 14:11:05 +02003019 int ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003020
nerdopolisb16c4ac2018-06-29 08:17:46 -04003021 session_seat = getenv("XDG_SEAT");
3022 if (session_seat)
3023 seat_id = session_seat;
3024
3025 if (config->seat_id)
3026 seat_id = config->seat_id;
3027
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003028 weston_log("initializing drm backend\n");
3029
Giulio Camuffo954f1832014-10-11 18:27:30 +03003030 b = zalloc(sizeof *b);
3031 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003032 return NULL;
Daniel Stone725c2c32012-06-22 14:04:36 +01003033
Daniel Stone6020f472018-02-05 15:46:20 +00003034 b->state_invalid = true;
leng.fang32af9fc2024-06-13 11:22:15 +08003035 b->allow_modeset = true;
Daniel Stoneefa504f2016-12-19 16:48:20 +00003036 b->drm.fd = -1;
3037
leng.fang32af9fc2024-06-13 11:22:15 +08003038#ifdef BUILD_AML_TV
3039 b->vdin_detect_fd = -1;
3040#endif
3041
Giulio Camuffo954f1832014-10-11 18:27:30 +03003042 b->compositor = compositor;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003043 b->use_pixman = config->use_pixman;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00003044 b->pageflip_timeout = config->pageflip_timeout;
Pekka Paalanendee412d2018-04-23 11:44:58 +02003045 b->use_pixman_shadow = config->use_pixman_shadow;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003046
Leandro Ribeiro172afc22019-12-26 16:23:43 -03003047 b->debug = weston_compositor_add_log_scope(compositor, "drm-backend",
3048 "Debug messages from DRM/KMS backend\n",
3049 NULL, NULL, NULL);
Daniel Stone1cbe1f92018-07-20 10:21:28 +01003050
Pekka Paalanen7da9a382017-08-30 11:29:49 +03003051 compositor->backend = &b->base;
3052
Stefan Agner0bfebeb2019-07-08 00:30:44 +02003053 if (parse_gbm_format(config->gbm_format, DRM_FORMAT_XRGB8888, &b->gbm_format) < 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003054 goto err_compositor;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003055
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003056 /* Check if we run drm-backend using weston-launch */
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003057 compositor->launcher = weston_launcher_connect(compositor, config->tty,
3058 seat_id, true);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003059 if (compositor->launcher == NULL) {
Pekka Paalanena453f4d2017-10-31 10:19:48 +02003060 weston_log("fatal: drm backend should be run using "
3061 "weston-launch binary, or your system should "
3062 "provide the logind D-Bus API.\n");
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003063 goto err_compositor;
3064 }
3065
Giulio Camuffo954f1832014-10-11 18:27:30 +03003066 b->udev = udev_new();
3067 if (b->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003068 weston_log("failed to initialize udev context\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003069 goto err_launcher;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003070 }
3071
Giulio Camuffo954f1832014-10-11 18:27:30 +03003072 b->session_listener.notify = session_notify;
3073 wl_signal_add(&compositor->session_signal, &b->session_listener);
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05003074
leng.fang32af9fc2024-06-13 11:22:15 +08003075 drm_config_fixed_UIsize(b, config);
3076
Pekka Paalanenb45ed8b2017-03-28 18:04:27 +03003077 if (config->specific_device)
3078 drm_device = open_specific_drm_device(b, config->specific_device);
3079 else
3080 drm_device = find_primary_gpu(b, seat_id);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04003081 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003082 weston_log("no drm device found\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003083 goto err_udev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003084 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003085
Daniel Stoneefa504f2016-12-19 16:48:20 +00003086 if (init_kms_caps(b) < 0) {
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003087 weston_log("failed to initialize kms\n");
3088 goto err_udev_dev;
3089 }
3090
Giulio Camuffo954f1832014-10-11 18:27:30 +03003091 if (b->use_pixman) {
3092 if (init_pixman(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003093 weston_log("failed to initialize pixman renderer\n");
3094 goto err_udev_dev;
3095 }
3096 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +03003097 if (init_egl(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003098 weston_log("failed to initialize egl\n");
3099 goto err_udev_dev;
3100 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003101 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05003102
Giulio Camuffo954f1832014-10-11 18:27:30 +03003103 b->base.destroy = drm_destroy;
Daniel Stoneeedf84c2017-02-10 18:06:04 +00003104 b->base.repaint_begin = drm_repaint_begin;
3105 b->base.repaint_flush = drm_repaint_flush;
3106 b->base.repaint_cancel = drm_repaint_cancel;
Pekka Paalanenc112f002017-08-28 16:27:20 +03003107 b->base.create_output = drm_output_create;
Robert Beckett8d23ab72019-06-13 16:55:44 +01003108 b->base.device_changed = drm_device_changed;
Marius Vlad81bada52019-11-11 00:27:17 +02003109 b->base.can_scanout_dmabuf = drm_can_scanout_dmabuf;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02003110
leng.fang32af9fc2024-06-13 11:22:15 +08003111 drm_add_aml_callback(b);
Bob Ham91880f12016-01-12 10:21:47 +00003112 weston_setup_vt_switch_bindings(compositor);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04003113
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03003114 res = drmModeGetResources(b->drm.fd);
3115 if (!res) {
3116 weston_log("Failed to get drmModeRes\n");
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03003117 goto err_udev_dev;
3118 }
3119
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03003120 wl_list_init(&b->crtc_list);
3121 if (drm_backend_create_crtc_list(b, res) == -1) {
3122 weston_log("Failed to create CRTC list for DRM-backend\n");
3123 goto err_create_crtc_list;
3124 }
3125
Daniel Stone085d2b92015-05-21 00:00:57 +01003126 wl_list_init(&b->plane_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003127 create_sprites(b);
Jesse Barnes58ef3792012-02-23 09:45:49 -05003128
Giulio Camuffo954f1832014-10-11 18:27:30 +03003129 if (udev_input_init(&b->input,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03003130 compositor, b->udev, seat_id,
3131 config->configure_device) < 0) {
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003132 weston_log("failed to create input devices\n");
3133 goto err_sprite;
3134 }
3135
leng.fang32af9fc2024-06-13 11:22:15 +08003136#ifdef ENABLE_DRM_HELP
3137 weston_log("drm start_help_worker \n");
3138 start_help_worker(b->drm.fd, b->atomic_modeset);
3139#endif
3140
Leandro Ribeiro96bef052020-09-09 15:23:49 -03003141 wl_list_init(&b->writeback_connector_list);
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03003142 if (drm_backend_discover_connectors(b, drm_device, res) < 0) {
Pekka Paalanend2e62422017-09-08 15:48:07 +03003143 weston_log("Failed to create heads for %s\n", b->drm.filename);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003144 goto err_udev_input;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003145 }
3146
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03003147 drmModeFreeResources(res);
3148
Marius Vlade83e7502019-10-29 17:29:37 +02003149 /* 'compute' faked zpos values in case HW doesn't expose any */
3150 drm_backend_create_faked_zpos(b);
3151
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003152 /* A this point we have some idea of whether or not we have a working
3153 * cursor plane. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003154 if (!b->cursors_are_broken)
3155 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003156
Giulio Camuffo954f1832014-10-11 18:27:30 +03003157 loop = wl_display_get_event_loop(compositor->wl_display);
3158 b->drm_source =
3159 wl_event_loop_add_fd(loop, b->drm.fd,
3160 WL_EVENT_READABLE, on_drm_input, b);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003161
Giulio Camuffo954f1832014-10-11 18:27:30 +03003162 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
3163 if (b->udev_monitor == NULL) {
Abdur Rehman4dca0e12017-01-01 19:46:35 +05003164 weston_log("failed to initialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003165 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003166 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03003167 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003168 "drm", NULL);
leng.fang32af9fc2024-06-13 11:22:15 +08003169#ifdef BUILD_AML_TV
3170 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
3171 "vdin", NULL);
3172 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
3173 "framerate_adapter", NULL);
3174#endif
3175
Giulio Camuffo954f1832014-10-11 18:27:30 +03003176 b->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02003177 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03003178 udev_monitor_get_fd(b->udev_monitor),
3179 WL_EVENT_READABLE, udev_drm_event, b);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003180
Giulio Camuffo954f1832014-10-11 18:27:30 +03003181 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003182 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003183 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003184 }
3185
Daniel Stonea96b93c2012-06-22 14:04:37 +01003186 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003187
Giulio Camuffo954f1832014-10-11 18:27:30 +03003188 weston_compositor_add_debug_binding(compositor, KEY_O,
3189 planes_binding, b);
3190 weston_compositor_add_debug_binding(compositor, KEY_C,
3191 planes_binding, b);
3192 weston_compositor_add_debug_binding(compositor, KEY_V,
3193 planes_binding, b);
3194 weston_compositor_add_debug_binding(compositor, KEY_Q,
3195 recorder_binding, b);
3196 weston_compositor_add_debug_binding(compositor, KEY_W,
3197 renderer_switch_binding, b);
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003198
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003199 if (compositor->renderer->import_dmabuf) {
3200 if (linux_dmabuf_setup(compositor) < 0)
3201 weston_log("Error: initializing dmabuf "
3202 "support failed.\n");
Leandro Ribeiro54293022021-10-12 14:48:36 -03003203 if (compositor->default_dmabuf_feedback) {
3204 /* We were able to create the compositor's default
3205 * dma-buf feedback in the renderer, that means that the
3206 * table was already created and populated with
3207 * renderer's format/modifier pairs. So now we must
3208 * compute the scanout formats indices in the table */
3209 scanout_formats = get_scanout_formats(b);
3210 if (!scanout_formats)
3211 goto err_udev_monitor;
3212 ret = weston_dmabuf_feedback_format_table_set_scanout_indices(compositor->dmabuf_feedback_format_table,
3213 scanout_formats);
3214 weston_drm_format_array_fini(scanout_formats);
Daniel Stonef5086032021-12-06 16:52:19 +00003215 free(scanout_formats);
Leandro Ribeiro54293022021-10-12 14:48:36 -03003216 if (ret < 0)
3217 goto err_udev_monitor;
3218 }
Marius Vladebd10e52019-11-16 19:22:48 +02003219 if (weston_direct_display_setup(compositor) < 0)
3220 weston_log("Error: initializing direct-display "
3221 "support failed.\n");
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003222 }
3223
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003224 if (compositor->capabilities & WESTON_CAP_EXPLICIT_SYNC) {
3225 if (linux_explicit_synchronization_setup(compositor) < 0)
3226 weston_log("Error: initializing explicit "
3227 " synchronization support failed.\n");
3228 }
3229
Ankit Nautiyala344fe32019-05-14 18:36:08 +05303230 if (b->atomic_modeset)
3231 if (weston_compositor_enable_content_protection(compositor) < 0)
3232 weston_log("Error: initializing content-protection "
3233 "support failed.\n");
3234
Armin Krezović08368132016-09-30 14:11:05 +02003235 ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME,
3236 &api, sizeof(api));
3237
3238 if (ret < 0) {
3239 weston_log("Failed to register output API.\n");
3240 goto err_udev_monitor;
3241 }
3242
Stefan Agner3654c672019-07-09 00:50:30 +02003243 ret = drm_backend_init_virtual_output_api(compositor);
Tomohito Esakib1fb00d2018-01-31 17:50:48 +09003244 if (ret < 0) {
3245 weston_log("Failed to register virtual output API.\n");
3246 goto err_udev_monitor;
3247 }
leng.fang32af9fc2024-06-13 11:22:15 +08003248#ifdef ENABLE_DRM_HELP
3249 help_set_printinfo_function(compositor, weston_print_info);
3250#endif
Tomohito Esakib1fb00d2018-01-31 17:50:48 +09003251
Giulio Camuffo954f1832014-10-11 18:27:30 +03003252 return b;
Daniel Stonea96b93c2012-06-22 14:04:37 +01003253
3254err_udev_monitor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003255 wl_event_source_remove(b->udev_drm_source);
3256 udev_monitor_unref(b->udev_monitor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003257err_drm_source:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003258 wl_event_source_remove(b->drm_source);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003259err_udev_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003260 udev_input_destroy(&b->input);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003261err_sprite:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003262 destroy_sprites(b);
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03003263err_create_crtc_list:
3264 drmModeFreeResources(res);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003265err_udev_dev:
3266 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003267err_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003268 udev_unref(b->udev);
Marius Vlad5130a8c2020-07-30 14:47:32 +03003269err_launcher:
3270 weston_launcher_destroy(compositor->launcher);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003271err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003272 weston_compositor_shutdown(compositor);
Marius Vladd171c7b2021-04-01 00:12:00 +03003273#ifdef BUILD_DRM_GBM
3274 if (b->gbm)
3275 gbm_device_destroy(b->gbm);
3276#endif
Giulio Camuffo954f1832014-10-11 18:27:30 +03003277 free(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003278 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003279}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003280
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003281static void
3282config_init_to_defaults(struct weston_drm_backend_config *config)
3283{
Pekka Paalanendee412d2018-04-23 11:44:58 +02003284 config->use_pixman_shadow = true;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003285}
3286
Giulio Camuffo954f1832014-10-11 18:27:30 +03003287WL_EXPORT int
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01003288weston_backend_init(struct weston_compositor *compositor,
3289 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003290{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003291 struct drm_backend *b;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003292 struct weston_drm_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003293
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003294 if (config_base == NULL ||
3295 config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION ||
3296 config_base->struct_size > sizeof(struct weston_drm_backend_config)) {
3297 weston_log("drm backend config structure is invalid\n");
3298 return -1;
3299 }
Benjamin Franzke117483d2011-08-30 11:38:26 +02003300
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003301 config_init_to_defaults(&config);
3302 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003303
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003304 b = drm_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003305 if (b == NULL)
3306 return -1;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003307
Giulio Camuffo954f1832014-10-11 18:27:30 +03003308 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003309}