blob: 3fb66243a759caed50eab5b2a3e82b9957b38436 [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
leng.fang91856072024-06-07 14:12:54 +08002107#ifdef ENABLE_MODE_POLICY
2108 if (head->base.connected)
2109 init_mode_policy_without_mode(&head->base,
2110 head->backend->drm.fd, head->connector.conn);
2111 if (head->base.device_changed)
2112 mode_policy_set_hotplug(head->base.connected ?
2113 AML_WESTON_HOTPLUG_PLUG : AML_WESTON_HOTPLUG_UNPLUG);
2114#endif
2115
Pekka Paalanen456dc732017-11-09 15:10:11 +02002116 if (head->base.device_changed)
2117 drm_head_log_info(head, "updated");
Leandro Ribeiro70863962020-09-09 13:12:35 -03002118
2119 return ret;
Pekka Paalanend2e62422017-09-08 15:48:07 +03002120}
2121
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002122/** Update writeback connector
2123 *
2124 * @param writeback The writeback to update.
2125 * @param conn DRM connector object.
2126 * @returns 0 on success, -1 on failure.
2127 *
2128 * Takes ownership of @c connector on success, not on failure.
2129 */
2130static int
2131drm_writeback_update_info(struct drm_writeback *writeback, drmModeConnector *conn)
2132{
2133 int ret;
2134
2135 ret = drm_connector_assign_connector_info(&writeback->connector, conn);
2136
2137 return ret;
2138}
2139
Daniel Stone087ddf02017-02-14 17:51:30 +00002140/**
Pekka Paalanenc112f002017-08-28 16:27:20 +03002141 * Create a Weston head for a connector
2142 *
2143 * Given a DRM connector, create a matching drm_head structure and add it
2144 * to Weston's head list.
2145 *
Marius Vlada2dace22019-06-12 16:05:44 +03002146 * @param backend Weston backend structure
Leandro Ribeiro70863962020-09-09 13:12:35 -03002147 * @param conn DRM connector object
Pekka Paalanenc112f002017-08-28 16:27:20 +03002148 * @param drm_device udev device pointer
Leandro Ribeiro70863962020-09-09 13:12:35 -03002149 * @returns 0 on success, -1 on failure
Leandro Ribeiro4a5b67a2020-09-15 15:30:35 -03002150 *
2151 * Takes ownership of @c connector on success, not on failure.
Pekka Paalanenc112f002017-08-28 16:27:20 +03002152 */
Leandro Ribeiro70863962020-09-09 13:12:35 -03002153static int
2154drm_head_create(struct drm_backend *backend, drmModeConnector *conn,
Pekka Paalanenc112f002017-08-28 16:27:20 +03002155 struct udev_device *drm_device)
2156{
2157 struct drm_head *head;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002158 char *name;
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002159 int ret;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002160
2161 head = zalloc(sizeof *head);
2162 if (!head)
Leandro Ribeiro70863962020-09-09 13:12:35 -03002163 return -1;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002164
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002165 head->backend = backend;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002166
Leandro Ribeiro70863962020-09-09 13:12:35 -03002167 drm_connector_init(backend, &head->connector, conn->connector_id);
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002168
2169 name = make_connector_name(conn);
Pekka Paalanenc112f002017-08-28 16:27:20 +03002170 if (!name)
Leandro Ribeiro70863962020-09-09 13:12:35 -03002171 goto err;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002172
2173 weston_head_init(&head->base, name);
2174 free(name);
2175
Leandro Ribeiro67941642020-09-15 00:11:02 -03002176 ret = drm_head_update_info(head, conn);
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002177 if (ret < 0)
Leandro Ribeiro67941642020-09-15 00:11:02 -03002178 goto err_update;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002179
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002180 head->backlight = backlight_init(drm_device, conn->connector_type);
Pekka Paalanence724242017-09-04 12:21:24 +03002181
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002182 if (conn->connector_type == DRM_MODE_CONNECTOR_LVDS ||
2183 conn->connector_type == DRM_MODE_CONNECTOR_eDP)
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002184 weston_head_set_internal(&head->base);
Pekka Paalanenc112f002017-08-28 16:27:20 +03002185
Pekka Paalanen9c03a7c2017-11-28 14:30:10 +02002186 if (drm_head_read_current_setup(head, backend) < 0) {
Pekka Paalanen13d233e2017-09-11 14:06:11 +03002187 weston_log("Failed to retrieve current mode from connector %d.\n",
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002188 head->connector.connector_id);
Pekka Paalanen6fae2be2017-11-28 14:33:52 +02002189 /* Not fatal. */
Pekka Paalanen13d233e2017-09-11 14:06:11 +03002190 }
2191
Pekka Paalanenc112f002017-08-28 16:27:20 +03002192 weston_compositor_add_head(backend->compositor, &head->base);
Pekka Paalanen456dc732017-11-09 15:10:11 +02002193 drm_head_log_info(head, "found");
leng.fang32af9fc2024-06-13 11:22:15 +08002194 drm_append_helper_connector(head);
Pekka Paalanenc112f002017-08-28 16:27:20 +03002195
Leandro Ribeiro70863962020-09-09 13:12:35 -03002196 return 0;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002197
Leandro Ribeiro67941642020-09-15 00:11:02 -03002198err_update:
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002199 weston_head_release(&head->base);
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002200err:
Leandro Ribeiro70863962020-09-09 13:12:35 -03002201 drm_connector_fini(&head->connector);
Pekka Paalanenc112f002017-08-28 16:27:20 +03002202 free(head);
Leandro Ribeiro70863962020-09-09 13:12:35 -03002203 return -1;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002204}
2205
2206static void
2207drm_head_destroy(struct drm_head *head)
2208{
leng.fang32af9fc2024-06-13 11:22:15 +08002209 drm_delete_helper_connector(head);
Pekka Paalanenc112f002017-08-28 16:27:20 +03002210 weston_head_release(&head->base);
Pekka Paalanence724242017-09-04 12:21:24 +03002211
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002212 drm_connector_fini(&head->connector);
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002213
Pekka Paalanence724242017-09-04 12:21:24 +03002214 if (head->backlight)
2215 backlight_destroy(head->backlight);
2216
Pekka Paalanenc112f002017-08-28 16:27:20 +03002217 free(head);
2218}
2219
2220/**
Armin Krezović08368132016-09-30 14:11:05 +02002221 * Create a Weston output structure
2222 *
Pekka Paalanend2e62422017-09-08 15:48:07 +03002223 * Create an "empty" drm_output. This is the implementation of
2224 * weston_backend::create_output.
Armin Krezović08368132016-09-30 14:11:05 +02002225 *
Pekka Paalanend2e62422017-09-08 15:48:07 +03002226 * Creating an output is usually followed by drm_output_attach_head()
2227 * and drm_output_enable() to make use of it.
2228 *
2229 * @param compositor The compositor instance.
2230 * @param name Name for the new output.
2231 * @returns The output, or NULL on failure.
Armin Krezović08368132016-09-30 14:11:05 +02002232 */
Pekka Paalanend2e62422017-09-08 15:48:07 +03002233static struct weston_output *
2234drm_output_create(struct weston_compositor *compositor, const char *name)
Armin Krezović08368132016-09-30 14:11:05 +02002235{
Pekka Paalanend2e62422017-09-08 15:48:07 +03002236 struct drm_backend *b = to_drm_backend(compositor);
Armin Krezović08368132016-09-30 14:11:05 +02002237 struct drm_output *output;
Armin Krezović08368132016-09-30 14:11:05 +02002238
Armin Krezović08368132016-09-30 14:11:05 +02002239 output = zalloc(sizeof *output);
2240 if (output == NULL)
Pekka Paalanend2e62422017-09-08 15:48:07 +03002241 return NULL;
Armin Krezović08368132016-09-30 14:11:05 +02002242
Daniel Stone64dbbee2018-07-20 19:00:06 +01002243 output->backend = b;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002244 output->crtc = NULL;
2245
Stefan Agnerccf24072019-07-09 22:02:00 +02002246#ifdef BUILD_DRM_GBM
Tomohito Esaki718a40b2018-01-31 17:50:15 +09002247 output->gbm_bo_flags = GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING;
Stefan Agnerccf24072019-07-09 22:02:00 +02002248#endif
Daniel Stone64dbbee2018-07-20 19:00:06 +01002249
Pekka Paalanend2e62422017-09-08 15:48:07 +03002250 weston_output_init(&output->base, compositor, name);
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002251
Armin Krezović08368132016-09-30 14:11:05 +02002252 output->base.enable = drm_output_enable;
2253 output->base.destroy = drm_output_destroy;
2254 output->base.disable = drm_output_disable;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002255 output->base.attach_head = drm_output_attach_head;
Pekka Paalanen7f853792017-11-29 14:33:33 +02002256 output->base.detach_head = drm_output_detach_head;
Armin Krezović08368132016-09-30 14:11:05 +02002257
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01002258 output->destroy_pending = false;
2259 output->disable_pending = false;
Armin Krezović08368132016-09-30 14:11:05 +02002260
Pekka Paalanen01f60212017-03-24 15:39:24 +02002261 output->state_cur = drm_output_state_alloc(output, NULL);
Pekka Paalanena0bfedc2017-04-03 14:42:51 +03002262
leng.fang32af9fc2024-06-13 11:22:15 +08002263 drm_init_aml_output_config(compositor, output);
Armin Krezović08368132016-09-30 14:11:05 +02002264 weston_compositor_add_pending_output(&output->base, b->compositor);
2265
Pekka Paalanend2e62422017-09-08 15:48:07 +03002266 return &output->base;
Armin Krezović08368132016-09-30 14:11:05 +02002267}
2268
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002269/**
2270 * Create a Weston writeback for a writeback connector
Leandro Ribeiro72430222020-09-14 17:41:41 -03002271 *
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002272 * Given a DRM connector of type writeback, create a matching drm_writeback
2273 * structure and add it to Weston's writeback list.
2274 *
2275 * @param b Weston backend structure
2276 * @param conn DRM connector object of type writeback
2277 * @returns 0 on success, -1 on failure
2278 *
2279 * Takes ownership of @c connector on success, not on failure.
2280 */
2281static int
2282drm_writeback_create(struct drm_backend *b, drmModeConnector *conn)
2283{
2284 struct drm_writeback *writeback;
2285 int ret;
2286
2287 writeback = zalloc(sizeof *writeback);
2288 assert(writeback);
2289
2290 writeback->backend = b;
2291
2292 drm_connector_init(b, &writeback->connector, conn->connector_id);
2293
2294 ret = drm_writeback_update_info(writeback, conn);
2295 if (ret < 0)
2296 goto err;
2297
2298 wl_list_insert(&b->writeback_connector_list, &writeback->link);
2299 return 0;
2300
2301err:
2302 drm_connector_fini(&writeback->connector);
2303 free(writeback);
2304 return -1;
2305}
2306
2307static void
2308drm_writeback_destroy(struct drm_writeback *writeback)
2309{
2310 drm_connector_fini(&writeback->connector);
2311 wl_list_remove(&writeback->link);
2312
2313 free(writeback);
2314}
2315
2316/** Given the DRM connector object of a connector, create drm_head or
2317 * drm_writeback object (depending on the type of connector) for it.
2318 *
2319 * The object is then added to the DRM-backend list of heads or writebacks.
Leandro Ribeiro72430222020-09-14 17:41:41 -03002320 *
2321 * @param b The DRM-backend structure
2322 * @param conn The DRM connector object
2323 * @param drm_device udev device pointer
2324 * @return 0 on success, -1 on failure
2325 */
2326static int
2327drm_backend_add_connector(struct drm_backend *b, drmModeConnector *conn,
2328 struct udev_device *drm_device)
2329{
2330 int ret;
2331
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002332 if (conn->connector_type == DRM_MODE_CONNECTOR_WRITEBACK) {
2333 ret = drm_writeback_create(b, conn);
2334 if (ret < 0)
2335 weston_log("DRM: failed to create writeback for connector %d.\n",
2336 conn->connector_id);
2337 } else {
2338 ret = drm_head_create(b, conn, drm_device);
2339 if (ret < 0)
2340 weston_log("DRM: failed to create head for connector %d.\n",
2341 conn->connector_id);
2342 }
Leandro Ribeiro72430222020-09-14 17:41:41 -03002343
2344 return ret;
2345}
2346
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002347/** Find all connectors of the fd and create drm_head or drm_writeback objects
2348 * (depending on the type of connector they are) for each of them
2349 *
2350 * These objects are added to the DRM-backend lists of heads and writebacks.
2351 *
2352 * @param b The DRM-backend structure
2353 * @param drm_device udev device pointer
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03002354 * @param resources The DRM resources, it is taken with drmModeGetResources
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002355 * @return 0 on success, -1 on failure
2356 */
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002357static int
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03002358drm_backend_discover_connectors(struct drm_backend *b, struct udev_device *drm_device,
2359 drmModeRes *resources)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002360{
Leandro Ribeiro70863962020-09-09 13:12:35 -03002361 drmModeConnector *conn;
2362 int i, ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002363
Giulio Camuffo954f1832014-10-11 18:27:30 +03002364 b->min_width = resources->min_width;
2365 b->max_width = resources->max_width;
2366 b->min_height = resources->min_height;
2367 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05002368
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002369 for (i = 0; i < resources->count_connectors; i++) {
Pekka Paalanend2e62422017-09-08 15:48:07 +03002370 uint32_t connector_id = resources->connectors[i];
Daniel Stone02cf4662017-03-03 16:19:39 +00002371
Leandro Ribeiro70863962020-09-09 13:12:35 -03002372 conn = drmModeGetConnector(b->drm.fd, connector_id);
2373 if (!conn)
2374 continue;
2375
Leandro Ribeiro72430222020-09-14 17:41:41 -03002376 ret = drm_backend_add_connector(b, conn, drm_device);
2377 if (ret < 0)
Leandro Ribeiro70863962020-09-09 13:12:35 -03002378 drmModeFreeConnector(conn);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002379 }
2380
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002381 return 0;
2382}
2383
Leandro Ribeiro99611c82020-09-14 17:19:12 -03002384static bool
2385resources_has_connector(drmModeRes *resources, uint32_t connector_id)
2386{
2387 for (int i = 0; i < resources->count_connectors; i++) {
2388 if (resources->connectors[i] == connector_id)
2389 return true;
2390 }
2391
2392 return false;
2393}
2394
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002395static void
Leandro Ribeiro993920d2020-09-14 17:01:01 -03002396drm_backend_update_connectors(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002397{
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002398 drmModeRes *resources;
Leandro Ribeiro70863962020-09-09 13:12:35 -03002399 drmModeConnector *conn;
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002400 struct weston_head *base, *base_next;
Pekka Paalanena0a37462017-08-31 15:41:57 +03002401 struct drm_head *head;
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002402 struct drm_writeback *writeback, *writeback_next;
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002403 uint32_t connector_id;
Leandro Ribeiro70863962020-09-09 13:12:35 -03002404 int i, ret;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002405
Giulio Camuffo954f1832014-10-11 18:27:30 +03002406 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002407 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002408 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002409 return;
2410 }
2411
Pekka Paalanend2e62422017-09-08 15:48:07 +03002412 /* collect new connectors that have appeared, e.g. MST */
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002413 for (i = 0; i < resources->count_connectors; i++) {
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002414 connector_id = resources->connectors[i];
Benjamin Franzke117483d2011-08-30 11:38:26 +02002415
Leandro Ribeiro70863962020-09-09 13:12:35 -03002416 conn = drmModeGetConnector(b->drm.fd, connector_id);
2417 if (!conn)
2418 continue;
2419
Pekka Paalanend2e62422017-09-08 15:48:07 +03002420 head = drm_head_find_by_connector(b, connector_id);
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002421 writeback = drm_writeback_find_by_connector(b, connector_id);
2422
2423 /* Connector can't be owned by both a head and a writeback, so
2424 * one of the searches must fail. */
2425 assert(head == NULL || writeback == NULL);
2426
Leandro Ribeiro72430222020-09-14 17:41:41 -03002427 if (head)
Leandro Ribeiro70863962020-09-09 13:12:35 -03002428 ret = drm_head_update_info(head, conn);
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002429 else if (writeback)
2430 ret = drm_writeback_update_info(writeback, conn);
Leandro Ribeiro72430222020-09-14 17:41:41 -03002431 else
2432 ret = drm_backend_add_connector(b, conn, drm_device);
2433
2434 if (ret < 0)
2435 drmModeFreeConnector(conn);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002436 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002437
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002438 /* Destroy head objects of connectors (except writeback connectors) that
2439 * have disappeared. */
2440 wl_list_for_each_safe(base, base_next,
Pekka Paalanena0a37462017-08-31 15:41:57 +03002441 &b->compositor->head_list, compositor_link) {
Pekka Paalanena0a37462017-08-31 15:41:57 +03002442 head = to_drm_head(base);
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002443 connector_id = head->connector.connector_id;
Pekka Paalanena0a37462017-08-31 15:41:57 +03002444
Leandro Ribeiro99611c82020-09-14 17:19:12 -03002445 if (resources_has_connector(resources, connector_id))
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002446 continue;
2447
Pekka Paalanend2e62422017-09-08 15:48:07 +03002448 weston_log("DRM: head '%s' (connector %d) disappeared.\n",
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002449 head->base.name, connector_id);
Pekka Paalanend2e62422017-09-08 15:48:07 +03002450 drm_head_destroy(head);
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002451 }
2452
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002453 /* Destroy writeback objects of writeback connectors that have
2454 * disappeared. */
2455 wl_list_for_each_safe(writeback, writeback_next,
2456 &b->writeback_connector_list, link) {
2457 connector_id = writeback->connector.connector_id;
2458
2459 if (resources_has_connector(resources, connector_id))
2460 continue;
2461
2462 weston_log("DRM: writeback connector (connector %d) disappeared.\n",
2463 connector_id);
2464 drm_writeback_destroy(writeback);
2465 }
2466
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002467 drmModeFreeResources(resources);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002468}
2469
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302470static enum wdrm_connector_property
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002471drm_connector_find_property_by_id(struct drm_connector *connector,
2472 uint32_t property_id)
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302473{
2474 int i;
2475 enum wdrm_connector_property prop = WDRM_CONNECTOR__COUNT;
2476
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002477 if (!connector || !property_id)
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302478 return WDRM_CONNECTOR__COUNT;
2479
2480 for (i = 0; i < WDRM_CONNECTOR__COUNT; i++)
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002481 if (connector->props[i].prop_id == property_id) {
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302482 prop = (enum wdrm_connector_property) i;
2483 break;
2484 }
2485 return prop;
2486}
2487
2488static void
2489drm_backend_update_conn_props(struct drm_backend *b,
2490 uint32_t connector_id,
2491 uint32_t property_id)
2492{
2493 struct drm_head *head;
2494 enum wdrm_connector_property conn_prop;
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302495
2496 head = drm_head_find_by_connector(b, connector_id);
2497 if (!head) {
2498 weston_log("DRM: failed to find head for connector id: %d.\n",
2499 connector_id);
2500 return;
2501 }
2502
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002503 conn_prop = drm_connector_find_property_by_id(&head->connector, property_id);
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302504 if (conn_prop >= WDRM_CONNECTOR__COUNT)
2505 return;
2506
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03002507 if (drm_connector_update_properties(&head->connector) < 0)
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302508 return;
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03002509
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302510 if (conn_prop == WDRM_CONNECTOR_CONTENT_PROTECTION) {
2511 weston_head_set_content_protection_status(&head->base,
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03002512 drm_head_get_current_protection(head));
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302513 }
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302514}
2515
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002516static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002517udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002518{
David Herrmannd7488c22012-03-11 20:05:21 +01002519 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01002520 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01002521
2522 sysnum = udev_device_get_sysnum(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002523 if (!sysnum || atoi(sysnum) != b->drm.id)
David Herrmannd7488c22012-03-11 20:05:21 +01002524 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02002525
David Herrmann6ac52db2012-03-11 20:05:22 +01002526 val = udev_device_get_property_value(device, "HOTPLUG");
2527 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002528 return 0;
2529
David Herrmann6ac52db2012-03-11 20:05:22 +01002530 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002531}
2532
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002533static int
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302534udev_event_is_conn_prop_change(struct drm_backend *b,
2535 struct udev_device *device,
2536 uint32_t *connector_id,
2537 uint32_t *property_id)
2538
2539{
2540 const char *val;
2541 int id;
2542
2543 val = udev_device_get_property_value(device, "CONNECTOR");
2544 if (!val || !safe_strtoint(val, &id))
2545 return 0;
2546 else
2547 *connector_id = id;
2548
2549 val = udev_device_get_property_value(device, "PROPERTY");
2550 if (!val || !safe_strtoint(val, &id))
2551 return 0;
2552 else
2553 *property_id = id;
2554
2555 return 1;
2556}
2557
2558static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002559udev_drm_event(int fd, uint32_t mask, void *data)
2560{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002561 struct drm_backend *b = data;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002562 struct udev_device *event;
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302563 uint32_t conn_id, prop_id;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002564
Giulio Camuffo954f1832014-10-11 18:27:30 +03002565 event = udev_monitor_receive_device(b->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002566
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302567 if (udev_event_is_hotplug(b, event)) {
leng.fang91856072024-06-07 14:12:54 +08002568#ifdef ENABLE_MODE_POLICY
2569 mode_policy_set_hotplug(AML_WESTON_HOTPLUG_PLUG | AML_WESTON_HOTPLUG_UNPLUG);
2570#endif
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302571 if (udev_event_is_conn_prop_change(b, event, &conn_id, &prop_id))
2572 drm_backend_update_conn_props(b, conn_id, prop_id);
2573 else
Leandro Ribeiro993920d2020-09-14 17:01:01 -03002574 drm_backend_update_connectors(b, event);
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302575 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002576
leng.fang32af9fc2024-06-13 11:22:15 +08002577#ifdef BUILD_AML_TV
2578 drm_backend_afr_update(b, udev_event_frame_rate_hint(event),
2579 udev_event_is_vdin_event(event));
2580#endif
2581
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002582 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002583
2584 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002585}
2586
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002587static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002588drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002589{
Armin Krezović545dba62016-08-05 15:54:18 +02002590 struct drm_backend *b = to_drm_backend(ec);
Pekka Paalanenc112f002017-08-28 16:27:20 +03002591 struct weston_head *base, *next;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002592 struct drm_crtc *crtc, *crtc_tmp;
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002593 struct drm_writeback *writeback, *writeback_tmp;
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002594
leng.fang32af9fc2024-06-13 11:22:15 +08002595#ifdef ENABLE_DRM_HELP
2596 stop_help_worker();
2597#endif
2598
Giulio Camuffo954f1832014-10-11 18:27:30 +03002599 udev_input_destroy(&b->input);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002600
Giulio Camuffo954f1832014-10-11 18:27:30 +03002601 wl_event_source_remove(b->udev_drm_source);
2602 wl_event_source_remove(b->drm_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002603
Daniel Stoneb57c6a02017-10-05 16:27:21 +01002604 b->shutting_down = true;
2605
Giulio Camuffo954f1832014-10-11 18:27:30 +03002606 destroy_sprites(b);
Kristian Høgsberg3d64a3e2013-05-10 12:36:04 -04002607
Leandro Ribeirof0149642019-12-18 15:52:18 -03002608 weston_log_scope_destroy(b->debug);
Daniel Stone1cbe1f92018-07-20 10:21:28 +01002609 b->debug = NULL;
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03002610 weston_compositor_shutdown(ec);
2611
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002612 wl_list_for_each_safe(crtc, crtc_tmp, &b->crtc_list, link)
2613 drm_crtc_destroy(crtc);
2614
Pekka Paalanenc112f002017-08-28 16:27:20 +03002615 wl_list_for_each_safe(base, next, &ec->head_list, compositor_link)
2616 drm_head_destroy(to_drm_head(base));
2617
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002618 wl_list_for_each_safe(writeback, writeback_tmp,
2619 &b->writeback_connector_list, link)
2620 drm_writeback_destroy(writeback);
2621
Stefan Agnerccf24072019-07-09 22:02:00 +02002622#ifdef BUILD_DRM_GBM
Giulio Camuffo954f1832014-10-11 18:27:30 +03002623 if (b->gbm)
2624 gbm_device_destroy(b->gbm);
Stefan Agnerccf24072019-07-09 22:02:00 +02002625#endif
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002626
Pekka Paalanen5b0aa552017-12-07 16:06:05 +02002627 udev_monitor_unref(b->udev_monitor);
Pekka Paalanen2a0c6c32017-09-13 16:48:01 +03002628 udev_unref(b->udev);
2629
Marius Vlad7a44ee72021-06-23 16:02:35 +03002630 weston_launcher_close(ec->launcher, b->drm.fd);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002631 weston_launcher_destroy(ec->launcher);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002632
Pekka Paalanen9bf4f372017-12-07 16:05:29 +02002633 free(b->drm.filename);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002634 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002635}
2636
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002637static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002638session_notify(struct wl_listener *listener, void *data)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002639{
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002640 struct weston_compositor *compositor = data;
Armin Krezović545dba62016-08-05 15:54:18 +02002641 struct drm_backend *b = to_drm_backend(compositor);
Daniel Stone085d2b92015-05-21 00:00:57 +01002642 struct drm_plane *plane;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002643 struct drm_output *output;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002644 struct drm_crtc *crtc;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002645
Giulio Camuffo954f1832014-10-11 18:27:30 +03002646 if (compositor->session_active) {
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002647 weston_log("activating session\n");
Daniel Stonef33e1042016-11-05 08:10:13 +00002648 weston_compositor_wake(compositor);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002649 weston_compositor_damage_all(compositor);
Daniel Stone6020f472018-02-05 15:46:20 +00002650 b->state_invalid = true;
leng.fang32af9fc2024-06-13 11:22:15 +08002651 b->allow_modeset = true;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002652 udev_input_enable(&b->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002653 } else {
2654 weston_log("deactivating session\n");
leng.fang32af9fc2024-06-13 11:22:15 +08002655 drm_change_to_dummy_mode(b->compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002656 udev_input_disable(&b->input);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04002657
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002658 weston_compositor_offscreen(compositor);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04002659
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002660 /* If we have a repaint scheduled (either from a
2661 * pending pageflip or the idle handler), make sure we
2662 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002663 * vt switched away. The OFFSCREEN state will prevent
Abdur Rehman4dca0e12017-01-01 19:46:35 +05002664 * further attempts at repainting. When we switch
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002665 * back, we schedule a repaint, which will process
2666 * pending frame callbacks. */
2667
Giulio Camuffo954f1832014-10-11 18:27:30 +03002668 wl_list_for_each(output, &compositor->output_list, base.link) {
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002669 crtc = output->crtc;
Daniel Stone09a97e22017-03-01 11:34:06 +00002670 output->base.repaint_needed = false;
Daniel Stone2ba17f42015-05-19 20:02:41 +01002671 if (output->cursor_plane)
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002672 drmModeSetCursor(b->drm.fd, crtc->crtc_id,
Daniel Stone2ba17f42015-05-19 20:02:41 +01002673 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002674 }
2675
Giulio Camuffo954f1832014-10-11 18:27:30 +03002676 output = container_of(compositor->output_list.next,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002677 struct drm_output, base.link);
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002678 crtc = output->crtc;
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002679
Daniel Stone085d2b92015-05-21 00:00:57 +01002680 wl_list_for_each(plane, &b->plane_list, link) {
2681 if (plane->type != WDRM_PLANE_TYPE_OVERLAY)
2682 continue;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002683 drmModeSetPlane(b->drm.fd, plane->plane_id, crtc->crtc_id,
2684 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
Daniel Stone085d2b92015-05-21 00:00:57 +01002685 }
2686 }
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002687}
2688
Robert Beckett8d23ab72019-06-13 16:55:44 +01002689
2690/**
2691 * Handle KMS GPU being added/removed
2692 *
2693 * If the device being added/removed is the KMS device, we activate/deactivate
2694 * the compositor session.
2695 *
2696 * @param compositor The compositor instance.
2697 * @param device The device being added/removed.
2698 * @param added Whether the device is being added (or removed)
2699 */
2700static void
2701drm_device_changed(struct weston_compositor *compositor,
2702 dev_t device, bool added)
2703{
2704 struct drm_backend *b = to_drm_backend(compositor);
2705
Robert Beckett49dc3202019-07-02 16:31:22 +01002706 if (b->drm.fd < 0 || b->drm.devnum != device ||
2707 compositor->session_active == added)
Robert Beckett8d23ab72019-06-13 16:55:44 +01002708 return;
2709
2710 compositor->session_active = added;
2711 wl_signal_emit(&compositor->session_signal, compositor);
2712}
2713
Daniel Stoneefa504f2016-12-19 16:48:20 +00002714/**
2715 * Determines whether or not a device is capable of modesetting. If successful,
2716 * sets b->drm.fd and b->drm.filename to the opened device.
2717 */
2718static bool
2719drm_device_is_kms(struct drm_backend *b, struct udev_device *device)
2720{
2721 const char *filename = udev_device_get_devnode(device);
2722 const char *sysnum = udev_device_get_sysnum(device);
Robert Beckett8d23ab72019-06-13 16:55:44 +01002723 dev_t devnum = udev_device_get_devnum(device);
Daniel Stoneefa504f2016-12-19 16:48:20 +00002724 drmModeRes *res;
Marius Vlad7d070ca2018-11-23 14:02:07 +02002725 int id = -1, fd;
Daniel Stoneefa504f2016-12-19 16:48:20 +00002726
2727 if (!filename)
2728 return false;
2729
2730 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
2731 if (fd < 0)
2732 return false;
2733
2734 res = drmModeGetResources(fd);
2735 if (!res)
2736 goto out_fd;
2737
2738 if (res->count_crtcs <= 0 || res->count_connectors <= 0 ||
2739 res->count_encoders <= 0)
2740 goto out_res;
2741
2742 if (sysnum)
2743 id = atoi(sysnum);
2744 if (!sysnum || id < 0) {
2745 weston_log("couldn't get sysnum for device %s\n", filename);
2746 goto out_res;
2747 }
2748
2749 /* We can be called successfully on multiple devices; if we have,
2750 * clean up old entries. */
2751 if (b->drm.fd >= 0)
2752 weston_launcher_close(b->compositor->launcher, b->drm.fd);
2753 free(b->drm.filename);
2754
2755 b->drm.fd = fd;
2756 b->drm.id = id;
2757 b->drm.filename = strdup(filename);
Robert Beckett8d23ab72019-06-13 16:55:44 +01002758 b->drm.devnum = devnum;
Daniel Stoneefa504f2016-12-19 16:48:20 +00002759
Sergi Granellceb59812017-03-28 12:44:04 +02002760 drmModeFreeResources(res);
2761
Daniel Stoneefa504f2016-12-19 16:48:20 +00002762 return true;
2763
2764out_res:
2765 drmModeFreeResources(res);
2766out_fd:
2767 weston_launcher_close(b->compositor->launcher, fd);
2768 return false;
2769}
2770
David Herrmann0af066f2012-10-29 19:21:16 +01002771/*
2772 * Find primary GPU
2773 * Some systems may have multiple DRM devices attached to a single seat. This
2774 * function loops over all devices and tries to find a PCI device with the
2775 * boot_vga sysfs attribute set to 1.
2776 * If no such device is found, the first DRM device reported by udev is used.
Daniel Stoneefa504f2016-12-19 16:48:20 +00002777 * Devices are also vetted to make sure they are are capable of modesetting,
2778 * rather than pure render nodes (GPU with no display), or pure
2779 * memory-allocation devices (VGEM).
David Herrmann0af066f2012-10-29 19:21:16 +01002780 */
2781static struct udev_device*
Giulio Camuffo954f1832014-10-11 18:27:30 +03002782find_primary_gpu(struct drm_backend *b, const char *seat)
David Herrmann0af066f2012-10-29 19:21:16 +01002783{
2784 struct udev_enumerate *e;
2785 struct udev_list_entry *entry;
2786 const char *path, *device_seat, *id;
2787 struct udev_device *device, *drm_device, *pci;
2788
Giulio Camuffo954f1832014-10-11 18:27:30 +03002789 e = udev_enumerate_new(b->udev);
David Herrmann0af066f2012-10-29 19:21:16 +01002790 udev_enumerate_add_match_subsystem(e, "drm");
2791 udev_enumerate_add_match_sysname(e, "card[0-9]*");
2792
2793 udev_enumerate_scan_devices(e);
2794 drm_device = NULL;
2795 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Daniel Stoneefa504f2016-12-19 16:48:20 +00002796 bool is_boot_vga = false;
2797
David Herrmann0af066f2012-10-29 19:21:16 +01002798 path = udev_list_entry_get_name(entry);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002799 device = udev_device_new_from_syspath(b->udev, path);
David Herrmann0af066f2012-10-29 19:21:16 +01002800 if (!device)
2801 continue;
2802 device_seat = udev_device_get_property_value(device, "ID_SEAT");
2803 if (!device_seat)
2804 device_seat = default_seat;
2805 if (strcmp(device_seat, seat)) {
2806 udev_device_unref(device);
2807 continue;
2808 }
2809
2810 pci = udev_device_get_parent_with_subsystem_devtype(device,
2811 "pci", NULL);
2812 if (pci) {
2813 id = udev_device_get_sysattr_value(pci, "boot_vga");
Daniel Stoneefa504f2016-12-19 16:48:20 +00002814 if (id && !strcmp(id, "1"))
2815 is_boot_vga = true;
David Herrmann0af066f2012-10-29 19:21:16 +01002816 }
2817
Daniel Stoneefa504f2016-12-19 16:48:20 +00002818 /* If we already have a modesetting-capable device, and this
2819 * device isn't our boot-VGA device, we aren't going to use
2820 * it. */
2821 if (!is_boot_vga && drm_device) {
David Herrmann0af066f2012-10-29 19:21:16 +01002822 udev_device_unref(device);
Daniel Stoneefa504f2016-12-19 16:48:20 +00002823 continue;
2824 }
2825
2826 /* Make sure this device is actually capable of modesetting;
2827 * if this call succeeds, b->drm.{fd,filename} will be set,
2828 * and any old values freed. */
2829 if (!drm_device_is_kms(b, device)) {
2830 udev_device_unref(device);
2831 continue;
2832 }
2833
2834 /* There can only be one boot_vga device, and we try to use it
2835 * at all costs. */
2836 if (is_boot_vga) {
2837 if (drm_device)
2838 udev_device_unref(drm_device);
2839 drm_device = device;
2840 break;
2841 }
2842
2843 /* Per the (!is_boot_vga && drm_device) test above, we only
2844 * trump existing saved devices with boot-VGA devices, so if
2845 * we end up here, this must be the first device we've seen. */
2846 assert(!drm_device);
2847 drm_device = device;
David Herrmann0af066f2012-10-29 19:21:16 +01002848 }
2849
Daniel Stoneefa504f2016-12-19 16:48:20 +00002850 /* If we're returning a device to use, we must have an open FD for
2851 * it. */
2852 assert(!!drm_device == (b->drm.fd >= 0));
2853
David Herrmann0af066f2012-10-29 19:21:16 +01002854 udev_enumerate_unref(e);
2855 return drm_device;
2856}
2857
Pekka Paalanenb45ed8b2017-03-28 18:04:27 +03002858static struct udev_device *
2859open_specific_drm_device(struct drm_backend *b, const char *name)
2860{
2861 struct udev_device *device;
2862
2863 device = udev_device_new_from_subsystem_sysname(b->udev, "drm", name);
2864 if (!device) {
2865 weston_log("ERROR: could not open DRM device '%s'\n", name);
2866 return NULL;
2867 }
2868
2869 if (!drm_device_is_kms(b, device)) {
2870 udev_device_unref(device);
2871 weston_log("ERROR: DRM device '%s' is not a KMS device.\n", name);
2872 return NULL;
2873 }
2874
2875 /* If we're returning a device to use, we must have an open FD for
2876 * it. */
2877 assert(b->drm.fd >= 0);
2878
2879 return device;
2880}
2881
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002882static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002883planes_binding(struct weston_keyboard *keyboard, const struct timespec *time,
2884 uint32_t key, void *data)
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002885{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002886 struct drm_backend *b = data;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002887
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002888 switch (key) {
2889 case KEY_C:
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01002890 b->cursors_are_broken ^= true;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002891 break;
2892 case KEY_V:
Daniel Stone87fab1c2019-06-17 11:13:20 +01002893 /* We don't support overlay-plane usage with legacy KMS. */
2894 if (b->atomic_modeset)
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01002895 b->sprites_are_broken ^= true;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002896 break;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002897 default:
2898 break;
2899 }
Ander Conselvan de Oliveira180f42a2012-11-21 15:11:37 +02002900}
2901
Kristian Høgsberg0eac34a2013-08-30 14:28:22 -07002902#ifdef BUILD_VAAPI_RECORDER
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002903static void
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002904recorder_destroy(struct drm_output *output)
2905{
2906 vaapi_recorder_destroy(output->recorder);
2907 output->recorder = NULL;
2908
Ankit Nautiyal93dde242019-07-08 11:46:42 +05302909 weston_output_disable_planes_decr(&output->base);
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002910
2911 wl_list_remove(&output->recorder_frame_listener.link);
2912 weston_log("[libva recorder] done\n");
2913}
2914
2915static void
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002916recorder_frame_notify(struct wl_listener *listener, void *data)
2917{
2918 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002919 struct drm_backend *b;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002920 int fd, ret;
2921
2922 output = container_of(listener, struct drm_output,
2923 recorder_frame_listener);
Armin Krezović545dba62016-08-05 15:54:18 +02002924 b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002925
2926 if (!output->recorder)
2927 return;
2928
Daniel Stonee2e80132018-01-16 15:37:33 +00002929 ret = drmPrimeHandleToFD(b->drm.fd,
Daniel Stone8eece0c2016-11-17 17:54:00 +00002930 output->scanout_plane->state_cur->fb->handles[0],
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002931 DRM_CLOEXEC, &fd);
2932 if (ret) {
2933 weston_log("[libva recorder] "
2934 "failed to create prime fd for front buffer\n");
2935 return;
2936 }
2937
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002938 ret = vaapi_recorder_frame(output->recorder, fd,
Daniel Stone8eece0c2016-11-17 17:54:00 +00002939 output->scanout_plane->state_cur->fb->strides[0]);
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002940 if (ret < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +02002941 weston_log("[libva recorder] aborted: %s\n", strerror(errno));
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002942 recorder_destroy(output);
2943 }
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002944}
2945
2946static void *
Giulio Camuffo954f1832014-10-11 18:27:30 +03002947create_recorder(struct drm_backend *b, int width, int height,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002948 const char *filename)
2949{
2950 int fd;
2951 drm_magic_t magic;
2952
Giulio Camuffo954f1832014-10-11 18:27:30 +03002953 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002954 if (fd < 0)
2955 return NULL;
2956
2957 drmGetMagic(fd, &magic);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002958 drmAuthMagic(b->drm.fd, magic);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002959
2960 return vaapi_recorder_create(fd, width, height, filename);
2961}
2962
2963static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002964recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time,
2965 uint32_t key, void *data)
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002966{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002967 struct drm_backend *b = data;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002968 struct drm_output *output;
2969 int width, height;
2970
Giulio Camuffo954f1832014-10-11 18:27:30 +03002971 output = container_of(b->compositor->output_list.next,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002972 struct drm_output, base.link);
2973
2974 if (!output->recorder) {
Stefan Agner4a18f302019-10-20 18:25:42 +02002975 if (output->gbm_format != DRM_FORMAT_XRGB8888) {
Ander Conselvan de Oliveira2ef1cd12014-05-06 16:49:06 +03002976 weston_log("failed to start vaapi recorder: "
2977 "output format not supported\n");
2978 return;
2979 }
2980
Hardeningff39efa2013-09-18 23:56:35 +02002981 width = output->base.current_mode->width;
2982 height = output->base.current_mode->height;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002983
2984 output->recorder =
Giulio Camuffo954f1832014-10-11 18:27:30 +03002985 create_recorder(b, width, height, "capture.h264");
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002986 if (!output->recorder) {
2987 weston_log("failed to create vaapi recorder\n");
2988 return;
2989 }
2990
Ankit Nautiyal93dde242019-07-08 11:46:42 +05302991 weston_output_disable_planes_incr(&output->base);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002992
2993 output->recorder_frame_listener.notify = recorder_frame_notify;
2994 wl_signal_add(&output->base.frame_signal,
2995 &output->recorder_frame_listener);
2996
2997 weston_output_schedule_repaint(&output->base);
2998
2999 weston_log("[libva recorder] initialized\n");
3000 } else {
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003001 recorder_destroy(output);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003002 }
3003}
3004#else
3005static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003006recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time,
3007 uint32_t key, void *data)
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003008{
3009 weston_log("Compiled without libva support\n");
3010}
3011#endif
3012
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003013
Armin Krezović08368132016-09-30 14:11:05 +02003014static const struct weston_drm_output_api api = {
3015 drm_output_set_mode,
3016 drm_output_set_gbm_format,
3017 drm_output_set_seat,
3018};
3019
Giulio Camuffo954f1832014-10-11 18:27:30 +03003020static struct drm_backend *
3021drm_backend_create(struct weston_compositor *compositor,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003022 struct weston_drm_backend_config *config)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003023{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003024 struct drm_backend *b;
David Herrmann0af066f2012-10-29 19:21:16 +01003025 struct udev_device *drm_device;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003026 struct wl_event_loop *loop;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003027 const char *seat_id = default_seat;
nerdopolisb16c4ac2018-06-29 08:17:46 -04003028 const char *session_seat;
Leandro Ribeiro54293022021-10-12 14:48:36 -03003029 struct weston_drm_format_array *scanout_formats;
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03003030 drmModeRes *res;
Armin Krezović08368132016-09-30 14:11:05 +02003031 int ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003032
nerdopolisb16c4ac2018-06-29 08:17:46 -04003033 session_seat = getenv("XDG_SEAT");
3034 if (session_seat)
3035 seat_id = session_seat;
3036
3037 if (config->seat_id)
3038 seat_id = config->seat_id;
3039
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003040 weston_log("initializing drm backend\n");
3041
Giulio Camuffo954f1832014-10-11 18:27:30 +03003042 b = zalloc(sizeof *b);
3043 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003044 return NULL;
Daniel Stone725c2c32012-06-22 14:04:36 +01003045
Daniel Stone6020f472018-02-05 15:46:20 +00003046 b->state_invalid = true;
leng.fang32af9fc2024-06-13 11:22:15 +08003047 b->allow_modeset = true;
Daniel Stoneefa504f2016-12-19 16:48:20 +00003048 b->drm.fd = -1;
3049
leng.fang32af9fc2024-06-13 11:22:15 +08003050#ifdef BUILD_AML_TV
3051 b->vdin_detect_fd = -1;
3052#endif
3053
Giulio Camuffo954f1832014-10-11 18:27:30 +03003054 b->compositor = compositor;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003055 b->use_pixman = config->use_pixman;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00003056 b->pageflip_timeout = config->pageflip_timeout;
Pekka Paalanendee412d2018-04-23 11:44:58 +02003057 b->use_pixman_shadow = config->use_pixman_shadow;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003058
Leandro Ribeiro172afc22019-12-26 16:23:43 -03003059 b->debug = weston_compositor_add_log_scope(compositor, "drm-backend",
3060 "Debug messages from DRM/KMS backend\n",
3061 NULL, NULL, NULL);
Daniel Stone1cbe1f92018-07-20 10:21:28 +01003062
Pekka Paalanen7da9a382017-08-30 11:29:49 +03003063 compositor->backend = &b->base;
3064
Stefan Agner0bfebeb2019-07-08 00:30:44 +02003065 if (parse_gbm_format(config->gbm_format, DRM_FORMAT_XRGB8888, &b->gbm_format) < 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003066 goto err_compositor;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003067
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003068 /* Check if we run drm-backend using weston-launch */
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003069 compositor->launcher = weston_launcher_connect(compositor, config->tty,
3070 seat_id, true);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003071 if (compositor->launcher == NULL) {
Pekka Paalanena453f4d2017-10-31 10:19:48 +02003072 weston_log("fatal: drm backend should be run using "
3073 "weston-launch binary, or your system should "
3074 "provide the logind D-Bus API.\n");
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003075 goto err_compositor;
3076 }
3077
Giulio Camuffo954f1832014-10-11 18:27:30 +03003078 b->udev = udev_new();
3079 if (b->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003080 weston_log("failed to initialize udev context\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003081 goto err_launcher;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003082 }
3083
Giulio Camuffo954f1832014-10-11 18:27:30 +03003084 b->session_listener.notify = session_notify;
3085 wl_signal_add(&compositor->session_signal, &b->session_listener);
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05003086
leng.fang32af9fc2024-06-13 11:22:15 +08003087 drm_config_fixed_UIsize(b, config);
3088
Pekka Paalanenb45ed8b2017-03-28 18:04:27 +03003089 if (config->specific_device)
3090 drm_device = open_specific_drm_device(b, config->specific_device);
3091 else
3092 drm_device = find_primary_gpu(b, seat_id);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04003093 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003094 weston_log("no drm device found\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003095 goto err_udev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003096 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003097
Daniel Stoneefa504f2016-12-19 16:48:20 +00003098 if (init_kms_caps(b) < 0) {
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003099 weston_log("failed to initialize kms\n");
3100 goto err_udev_dev;
3101 }
3102
Giulio Camuffo954f1832014-10-11 18:27:30 +03003103 if (b->use_pixman) {
3104 if (init_pixman(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003105 weston_log("failed to initialize pixman renderer\n");
3106 goto err_udev_dev;
3107 }
3108 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +03003109 if (init_egl(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003110 weston_log("failed to initialize egl\n");
3111 goto err_udev_dev;
3112 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003113 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05003114
Giulio Camuffo954f1832014-10-11 18:27:30 +03003115 b->base.destroy = drm_destroy;
Daniel Stoneeedf84c2017-02-10 18:06:04 +00003116 b->base.repaint_begin = drm_repaint_begin;
3117 b->base.repaint_flush = drm_repaint_flush;
3118 b->base.repaint_cancel = drm_repaint_cancel;
Pekka Paalanenc112f002017-08-28 16:27:20 +03003119 b->base.create_output = drm_output_create;
Robert Beckett8d23ab72019-06-13 16:55:44 +01003120 b->base.device_changed = drm_device_changed;
Marius Vlad81bada52019-11-11 00:27:17 +02003121 b->base.can_scanout_dmabuf = drm_can_scanout_dmabuf;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02003122
leng.fang32af9fc2024-06-13 11:22:15 +08003123 drm_add_aml_callback(b);
Bob Ham91880f12016-01-12 10:21:47 +00003124 weston_setup_vt_switch_bindings(compositor);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04003125
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03003126 res = drmModeGetResources(b->drm.fd);
3127 if (!res) {
3128 weston_log("Failed to get drmModeRes\n");
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03003129 goto err_udev_dev;
3130 }
3131
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03003132 wl_list_init(&b->crtc_list);
3133 if (drm_backend_create_crtc_list(b, res) == -1) {
3134 weston_log("Failed to create CRTC list for DRM-backend\n");
3135 goto err_create_crtc_list;
3136 }
3137
Daniel Stone085d2b92015-05-21 00:00:57 +01003138 wl_list_init(&b->plane_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003139 create_sprites(b);
Jesse Barnes58ef3792012-02-23 09:45:49 -05003140
Giulio Camuffo954f1832014-10-11 18:27:30 +03003141 if (udev_input_init(&b->input,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03003142 compositor, b->udev, seat_id,
3143 config->configure_device) < 0) {
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003144 weston_log("failed to create input devices\n");
3145 goto err_sprite;
3146 }
3147
leng.fang32af9fc2024-06-13 11:22:15 +08003148#ifdef ENABLE_DRM_HELP
3149 weston_log("drm start_help_worker \n");
3150 start_help_worker(b->drm.fd, b->atomic_modeset);
3151#endif
3152
leng.fang91856072024-06-07 14:12:54 +08003153#ifdef ENABLE_MODE_POLICY
3154 mode_policy_set_hotplug(AML_WESTON_HOTPLUG_BOOT);
3155#endif
3156
Leandro Ribeiro96bef052020-09-09 15:23:49 -03003157 wl_list_init(&b->writeback_connector_list);
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03003158 if (drm_backend_discover_connectors(b, drm_device, res) < 0) {
Pekka Paalanend2e62422017-09-08 15:48:07 +03003159 weston_log("Failed to create heads for %s\n", b->drm.filename);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003160 goto err_udev_input;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003161 }
3162
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03003163 drmModeFreeResources(res);
3164
Marius Vlade83e7502019-10-29 17:29:37 +02003165 /* 'compute' faked zpos values in case HW doesn't expose any */
3166 drm_backend_create_faked_zpos(b);
3167
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003168 /* A this point we have some idea of whether or not we have a working
3169 * cursor plane. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003170 if (!b->cursors_are_broken)
3171 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003172
Giulio Camuffo954f1832014-10-11 18:27:30 +03003173 loop = wl_display_get_event_loop(compositor->wl_display);
3174 b->drm_source =
3175 wl_event_loop_add_fd(loop, b->drm.fd,
3176 WL_EVENT_READABLE, on_drm_input, b);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003177
Giulio Camuffo954f1832014-10-11 18:27:30 +03003178 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
3179 if (b->udev_monitor == NULL) {
Abdur Rehman4dca0e12017-01-01 19:46:35 +05003180 weston_log("failed to initialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003181 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003182 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03003183 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003184 "drm", NULL);
leng.fang32af9fc2024-06-13 11:22:15 +08003185#ifdef BUILD_AML_TV
3186 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
3187 "vdin", NULL);
3188 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
3189 "framerate_adapter", NULL);
3190#endif
3191
Giulio Camuffo954f1832014-10-11 18:27:30 +03003192 b->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02003193 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03003194 udev_monitor_get_fd(b->udev_monitor),
3195 WL_EVENT_READABLE, udev_drm_event, b);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003196
Giulio Camuffo954f1832014-10-11 18:27:30 +03003197 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003198 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003199 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003200 }
3201
Daniel Stonea96b93c2012-06-22 14:04:37 +01003202 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003203
Giulio Camuffo954f1832014-10-11 18:27:30 +03003204 weston_compositor_add_debug_binding(compositor, KEY_O,
3205 planes_binding, b);
3206 weston_compositor_add_debug_binding(compositor, KEY_C,
3207 planes_binding, b);
3208 weston_compositor_add_debug_binding(compositor, KEY_V,
3209 planes_binding, b);
3210 weston_compositor_add_debug_binding(compositor, KEY_Q,
3211 recorder_binding, b);
3212 weston_compositor_add_debug_binding(compositor, KEY_W,
3213 renderer_switch_binding, b);
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003214
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003215 if (compositor->renderer->import_dmabuf) {
3216 if (linux_dmabuf_setup(compositor) < 0)
3217 weston_log("Error: initializing dmabuf "
3218 "support failed.\n");
Leandro Ribeiro54293022021-10-12 14:48:36 -03003219 if (compositor->default_dmabuf_feedback) {
3220 /* We were able to create the compositor's default
3221 * dma-buf feedback in the renderer, that means that the
3222 * table was already created and populated with
3223 * renderer's format/modifier pairs. So now we must
3224 * compute the scanout formats indices in the table */
3225 scanout_formats = get_scanout_formats(b);
3226 if (!scanout_formats)
3227 goto err_udev_monitor;
3228 ret = weston_dmabuf_feedback_format_table_set_scanout_indices(compositor->dmabuf_feedback_format_table,
3229 scanout_formats);
3230 weston_drm_format_array_fini(scanout_formats);
Daniel Stonef5086032021-12-06 16:52:19 +00003231 free(scanout_formats);
Leandro Ribeiro54293022021-10-12 14:48:36 -03003232 if (ret < 0)
3233 goto err_udev_monitor;
3234 }
Marius Vladebd10e52019-11-16 19:22:48 +02003235 if (weston_direct_display_setup(compositor) < 0)
3236 weston_log("Error: initializing direct-display "
3237 "support failed.\n");
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003238 }
3239
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003240 if (compositor->capabilities & WESTON_CAP_EXPLICIT_SYNC) {
3241 if (linux_explicit_synchronization_setup(compositor) < 0)
3242 weston_log("Error: initializing explicit "
3243 " synchronization support failed.\n");
3244 }
3245
Ankit Nautiyala344fe32019-05-14 18:36:08 +05303246 if (b->atomic_modeset)
3247 if (weston_compositor_enable_content_protection(compositor) < 0)
3248 weston_log("Error: initializing content-protection "
3249 "support failed.\n");
3250
Armin Krezović08368132016-09-30 14:11:05 +02003251 ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME,
3252 &api, sizeof(api));
3253
3254 if (ret < 0) {
3255 weston_log("Failed to register output API.\n");
3256 goto err_udev_monitor;
3257 }
3258
Stefan Agner3654c672019-07-09 00:50:30 +02003259 ret = drm_backend_init_virtual_output_api(compositor);
Tomohito Esakib1fb00d2018-01-31 17:50:48 +09003260 if (ret < 0) {
3261 weston_log("Failed to register virtual output API.\n");
3262 goto err_udev_monitor;
3263 }
leng.fang91856072024-06-07 14:12:54 +08003264
leng.fang32af9fc2024-06-13 11:22:15 +08003265#ifdef ENABLE_DRM_HELP
3266 help_set_printinfo_function(compositor, weston_print_info);
3267#endif
Tomohito Esakib1fb00d2018-01-31 17:50:48 +09003268
leng.fang91856072024-06-07 14:12:54 +08003269#ifdef ENABLE_MODE_POLICY
3270 weston_start_update_env_thread();
3271#endif
3272
Giulio Camuffo954f1832014-10-11 18:27:30 +03003273 return b;
Daniel Stonea96b93c2012-06-22 14:04:37 +01003274
3275err_udev_monitor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003276 wl_event_source_remove(b->udev_drm_source);
3277 udev_monitor_unref(b->udev_monitor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003278err_drm_source:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003279 wl_event_source_remove(b->drm_source);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003280err_udev_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003281 udev_input_destroy(&b->input);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003282err_sprite:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003283 destroy_sprites(b);
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03003284err_create_crtc_list:
3285 drmModeFreeResources(res);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003286err_udev_dev:
3287 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003288err_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003289 udev_unref(b->udev);
Marius Vlad5130a8c2020-07-30 14:47:32 +03003290err_launcher:
3291 weston_launcher_destroy(compositor->launcher);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003292err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003293 weston_compositor_shutdown(compositor);
Marius Vladd171c7b2021-04-01 00:12:00 +03003294#ifdef BUILD_DRM_GBM
3295 if (b->gbm)
3296 gbm_device_destroy(b->gbm);
3297#endif
Giulio Camuffo954f1832014-10-11 18:27:30 +03003298 free(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003299 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003300}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003301
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003302static void
3303config_init_to_defaults(struct weston_drm_backend_config *config)
3304{
Pekka Paalanendee412d2018-04-23 11:44:58 +02003305 config->use_pixman_shadow = true;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003306}
3307
Giulio Camuffo954f1832014-10-11 18:27:30 +03003308WL_EXPORT int
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01003309weston_backend_init(struct weston_compositor *compositor,
3310 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003311{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003312 struct drm_backend *b;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003313 struct weston_drm_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003314
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003315 if (config_base == NULL ||
3316 config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION ||
3317 config_base->struct_size > sizeof(struct weston_drm_backend_config)) {
3318 weston_log("drm backend config structure is invalid\n");
3319 return -1;
3320 }
Benjamin Franzke117483d2011-08-30 11:38:26 +02003321
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003322 config_init_to_defaults(&config);
3323 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003324
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003325 b = drm_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003326 if (b == NULL)
3327 return -1;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003328
Giulio Camuffo954f1832014-10-11 18:27:30 +03003329 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003330}