blob: 563a34668d333819d9b58cb5b5b7c8b923a30be0 [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
limin.tianc616dad2024-07-15 11:35:38 +0000711 if (&drm_mode->base == output->base.current_mode) {
712 weston_log("\n drm_output_switch_mode same mode return \n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800713 return 0;
limin.tianc616dad2024-07-15 11:35:38 +0000714 }
Hardeningff39efa2013-09-18 23:56:35 +0200715 output->base.current_mode->flags = 0;
Alex Wub7b8bda2012-04-17 17:20:48 +0800716
leng.fang32af9fc2024-06-13 11:22:15 +0800717 drm_switch_mode_aml_config_ui_size(output, output_base, drm_mode);
718
Hardeningff39efa2013-09-18 23:56:35 +0200719 output->base.current_mode = &drm_mode->base;
720 output->base.current_mode->flags =
Alex Wub7b8bda2012-04-17 17:20:48 +0800721 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
722
leng.fang32af9fc2024-06-13 11:22:15 +0800723 drm_switch_mode_aml_config_help(output, output_base, drm_mode);
724
Daniel Stonef30a18c2017-04-04 17:54:31 +0100725 /* XXX: This drops our current buffer too early, before we've started
726 * displaying it. Ideally this should be much more atomic and
727 * integrated with a full repaint cycle, rather than doing a
728 * sledgehammer modeswitch first, and only later showing new
729 * content.
730 */
Daniel Stone6020f472018-02-05 15:46:20 +0000731 b->state_invalid = true;
Alex Wub7b8bda2012-04-17 17:20:48 +0800732
leng.fang32af9fc2024-06-13 11:22:15 +0800733 b->allow_modeset = true;
734#ifdef MESON_DRM_FIX_UI_SIZE
735 // fixed ui mode needn't reset egl.
736 return 0;
737#endif
738
Giulio Camuffo954f1832014-10-11 18:27:30 +0300739 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200740 drm_output_fini_pixman(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300741 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200742 weston_log("failed to init output pixman state with "
743 "new mode\n");
744 return -1;
745 }
746 } else {
Daniel Stone3e661f72016-11-04 17:24:06 +0000747 drm_output_fini_egl(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300748 if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200749 weston_log("failed to init output egl state with "
750 "new mode");
751 return -1;
752 }
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -0200753 }
754
Alex Wub7b8bda2012-04-17 17:20:48 +0800755 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +0800756}
757
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200758static int
Giulio Camuffo954f1832014-10-11 18:27:30 +0300759init_pixman(struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200760{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300761 return pixman_renderer_init(b->compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200762}
763
Pekka Paalanen7b36b422014-06-04 14:00:53 +0300764/**
Pekka Paalanenec272712014-06-05 11:22:25 +0300765 * Create a drm_plane for a hardware plane
766 *
767 * Creates one drm_plane structure for a hardware plane, and initialises its
768 * properties and formats.
769 *
770 * This function does not add the plane to the list of usable planes in Weston
771 * itself; the caller is responsible for this.
772 *
773 * Call drm_plane_destroy to clean up the plane.
774 *
Daniel Stone2ba17f42015-05-19 20:02:41 +0100775 * @sa drm_output_find_special_plane
Pekka Paalanenec272712014-06-05 11:22:25 +0300776 * @param b DRM compositor backend
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -0300777 * @param kplane DRM plane to create
Pekka Paalanenec272712014-06-05 11:22:25 +0300778 */
779static struct drm_plane *
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -0300780drm_plane_create(struct drm_backend *b, const drmModePlane *kplane)
Pekka Paalanenec272712014-06-05 11:22:25 +0300781{
782 struct drm_plane *plane;
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100783 drmModeObjectProperties *props;
Marius Vladcdd6fa22019-08-29 20:42:00 +0300784 uint64_t *zpos_range_values;
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100785
Scott Anderson74663092021-02-01 15:46:33 -0300786 plane = zalloc(sizeof(*plane));
Pekka Paalanenec272712014-06-05 11:22:25 +0300787 if (!plane) {
788 weston_log("%s: out of memory\n", __func__);
789 return NULL;
790 }
791
792 plane->backend = b;
Daniel Stone57d609a2021-11-16 18:56:09 +0000793 plane->plane_idx = b->next_plane_idx++;
Daniel Stonebc15f682016-11-14 16:57:01 +0000794 plane->state_cur = drm_plane_state_alloc(NULL, plane);
795 plane->state_cur->complete = true;
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -0300796 plane->possible_crtcs = kplane->possible_crtcs;
797 plane->plane_id = kplane->plane_id;
Pekka Paalanenec272712014-06-05 11:22:25 +0300798
Scott Anderson74663092021-02-01 15:46:33 -0300799 weston_drm_format_array_init(&plane->formats);
800
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -0300801 props = drmModeObjectGetProperties(b->drm.fd, kplane->plane_id,
802 DRM_MODE_OBJECT_PLANE);
803 if (!props) {
804 weston_log("couldn't get plane properties\n");
805 goto err;
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100806 }
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -0300807
808 drm_property_info_populate(b, plane_props, plane->props,
809 WDRM_PLANE__COUNT, props);
810 plane->type =
811 drm_property_get_value(&plane->props[WDRM_PLANE_TYPE],
812 props,
813 WDRM_PLANE_TYPE__COUNT);
814
815 zpos_range_values =
816 drm_property_get_range_values(&plane->props[WDRM_PLANE_ZPOS],
817 props);
818
819 if (zpos_range_values) {
820 plane->zpos_min = zpos_range_values[0];
821 plane->zpos_max = zpos_range_values[1];
822 } else {
Marius Vladcdd6fa22019-08-29 20:42:00 +0300823 plane->zpos_min = DRM_PLANE_ZPOS_INVALID_PLANE;
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -0300824 plane->zpos_max = DRM_PLANE_ZPOS_INVALID_PLANE;
Daniel Stone2ba17f42015-05-19 20:02:41 +0100825 }
826
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -0300827 if (drm_plane_populate_formats(plane, kplane, props,
828 b->fb_modifiers) < 0) {
829 drmModeFreeObjectProperties(props);
830 goto err;
831 }
832
833 drmModeFreeObjectProperties(props);
834
Daniel Stone2ba17f42015-05-19 20:02:41 +0100835 if (plane->type == WDRM_PLANE_TYPE__COUNT)
836 goto err_props;
837
Pekka Paalanenec272712014-06-05 11:22:25 +0300838 weston_plane_init(&plane->base, b->compositor, 0, 0);
Daniel Stone085d2b92015-05-21 00:00:57 +0100839 wl_list_insert(&b->plane_list, &plane->link);
Pekka Paalanenec272712014-06-05 11:22:25 +0300840
841 return plane;
Daniel Stone2ba17f42015-05-19 20:02:41 +0100842
843err_props:
844 drm_property_info_free(plane->props, WDRM_PLANE__COUNT);
845err:
Scott Anderson74663092021-02-01 15:46:33 -0300846 weston_drm_format_array_fini(&plane->formats);
Daniel Stone2ba17f42015-05-19 20:02:41 +0100847 drm_plane_state_free(plane->state_cur, true);
848 free(plane);
849 return NULL;
850}
851
852/**
853 * Find, or create, a special-purpose plane
854 *
Daniel Stone2ba17f42015-05-19 20:02:41 +0100855 * @param b DRM backend
856 * @param output Output to use for plane
857 * @param type Type of plane
858 */
859static struct drm_plane *
860drm_output_find_special_plane(struct drm_backend *b, struct drm_output *output,
861 enum wdrm_plane_type type)
862{
863 struct drm_plane *plane;
864
Daniel Stone2ba17f42015-05-19 20:02:41 +0100865 wl_list_for_each(plane, &b->plane_list, link) {
866 struct drm_output *tmp;
867 bool found_elsewhere = false;
868
869 if (plane->type != type)
870 continue;
871 if (!drm_plane_is_available(plane, output))
872 continue;
873
874 /* On some platforms, primary/cursor planes can roam
875 * between different CRTCs, so make sure we don't claim the
876 * same plane for two outputs. */
Leandro Ribeiro05cecc82020-08-13 17:34:58 -0300877 wl_list_for_each(tmp, &b->compositor->output_list, base.link) {
Daniel Stonee2e80132018-01-16 15:37:33 +0000878 if (tmp->cursor_plane == plane ||
879 tmp->scanout_plane == plane) {
Daniel Stone2ba17f42015-05-19 20:02:41 +0100880 found_elsewhere = true;
881 break;
882 }
883 }
884
885 if (found_elsewhere)
886 continue;
887
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300888 plane->possible_crtcs = (1 << output->crtc->pipe);
Daniel Stone2ba17f42015-05-19 20:02:41 +0100889 return plane;
890 }
891
892 return NULL;
Pekka Paalanenec272712014-06-05 11:22:25 +0300893}
894
895/**
896 * Destroy one DRM plane
897 *
898 * Destroy a DRM plane, removing it from screen and releasing its retained
899 * buffers in the process. The counterpart to drm_plane_create.
900 *
901 * @param plane Plane to deallocate (will be freed)
902 */
903static void
904drm_plane_destroy(struct drm_plane *plane)
905{
Daniel Stone2ba17f42015-05-19 20:02:41 +0100906 if (plane->type == WDRM_PLANE_TYPE_OVERLAY)
907 drmModeSetPlane(plane->backend->drm.fd, plane->plane_id,
908 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
Daniel Stonebc15f682016-11-14 16:57:01 +0000909 drm_plane_state_free(plane->state_cur, true);
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100910 drm_property_info_free(plane->props, WDRM_PLANE__COUNT);
Pekka Paalanenec272712014-06-05 11:22:25 +0300911 weston_plane_release(&plane->base);
Scott Anderson74663092021-02-01 15:46:33 -0300912 weston_drm_format_array_fini(&plane->formats);
Pekka Paalanenec272712014-06-05 11:22:25 +0300913 wl_list_remove(&plane->link);
914 free(plane);
915}
916
917/**
918 * Initialise sprites (overlay planes)
919 *
920 * Walk the list of provided DRM planes, and add overlay planes.
921 *
922 * Call destroy_sprites to free these planes.
923 *
924 * @param b DRM compositor backend
925 */
926static void
927create_sprites(struct drm_backend *b)
928{
929 drmModePlaneRes *kplane_res;
930 drmModePlane *kplane;
931 struct drm_plane *drm_plane;
932 uint32_t i;
Pekka Paalanenec272712014-06-05 11:22:25 +0300933 kplane_res = drmModeGetPlaneResources(b->drm.fd);
934 if (!kplane_res) {
935 weston_log("failed to get plane resources: %s\n",
936 strerror(errno));
937 return;
938 }
leng.fang32af9fc2024-06-13 11:22:15 +0800939 for (i = kplane_res->count_planes; i >0; i--) {
940 kplane = drmModeGetPlane(b->drm.fd, kplane_res->planes[i-1]);
Pekka Paalanenec272712014-06-05 11:22:25 +0300941
Pekka Paalanenec272712014-06-05 11:22:25 +0300942 if (!kplane)
943 continue;
944
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -0300945 drm_plane = drm_plane_create(b, kplane);
Pekka Paalanenec272712014-06-05 11:22:25 +0300946 drmModeFreePlane(kplane);
947 if (!drm_plane)
948 continue;
949
Daniel Stone085d2b92015-05-21 00:00:57 +0100950 if (drm_plane->type == WDRM_PLANE_TYPE_OVERLAY)
951 weston_compositor_stack_plane(b->compositor,
952 &drm_plane->base,
953 &b->compositor->primary_plane);
Pekka Paalanenec272712014-06-05 11:22:25 +0300954 }
955
956 drmModeFreePlaneResources(kplane_res);
957}
958
959/**
960 * Clean up sprites (overlay planes)
961 *
962 * The counterpart to create_sprites.
963 *
964 * @param b DRM compositor backend
965 */
966static void
967destroy_sprites(struct drm_backend *b)
968{
969 struct drm_plane *plane, *next;
970
Daniel Stone085d2b92015-05-21 00:00:57 +0100971 wl_list_for_each_safe(plane, next, &b->plane_list, link)
Pekka Paalanenec272712014-06-05 11:22:25 +0300972 drm_plane_destroy(plane);
973}
974
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -0300975/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200976static uint32_t
Pekka Paalanence724242017-09-04 12:21:24 +0300977drm_get_backlight(struct drm_head *head)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200978{
979 long brightness, max_brightness, norm;
980
Pekka Paalanence724242017-09-04 12:21:24 +0300981 brightness = backlight_get_brightness(head->backlight);
982 max_brightness = backlight_get_max_brightness(head->backlight);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200983
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -0300984 /* convert it on a scale of 0 to 255 */
985 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200986
987 return (uint32_t) norm;
988}
989
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -0300990/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200991static void
992drm_set_backlight(struct weston_output *output_base, uint32_t value)
993{
Pekka Paalanenecc8cce2017-09-12 16:14:31 +0300994 struct drm_output *output = to_drm_output(output_base);
995 struct drm_head *head;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200996 long max_brightness, new_brightness;
997
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400998 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200999 return;
1000
Pekka Paalanenecc8cce2017-09-12 16:14:31 +03001001 wl_list_for_each(head, &output->base.head_list, base.output_link) {
1002 if (!head->backlight)
1003 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001004
Pekka Paalanenecc8cce2017-09-12 16:14:31 +03001005 max_brightness = backlight_get_max_brightness(head->backlight);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001006
Pekka Paalanenecc8cce2017-09-12 16:14:31 +03001007 /* get denormalized value */
1008 new_brightness = (value * max_brightness) / 255;
1009
1010 backlight_set_brightness(head->backlight, new_brightness);
1011 }
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001012}
1013
Pekka Paalanenf8b850d2017-11-15 12:51:01 +02001014static void
1015drm_output_init_backlight(struct drm_output *output)
1016{
1017 struct weston_head *base;
1018 struct drm_head *head;
1019
1020 output->base.set_backlight = NULL;
1021
1022 wl_list_for_each(base, &output->base.head_list, output_link) {
1023 head = to_drm_head(base);
1024
1025 if (head->backlight) {
1026 weston_log("Initialized backlight for head '%s', device %s\n",
1027 head->base.name, head->backlight->path);
1028
1029 if (!output->base.set_backlight) {
1030 output->base.set_backlight = drm_set_backlight;
1031 output->base.backlight_current =
1032 drm_get_backlight(head);
1033 }
1034 }
1035 }
Pekka Paalanenf8b850d2017-11-15 12:51:01 +02001036}
1037
Daniel Stonea08512f2016-11-08 17:46:10 +00001038/**
1039 * Power output on or off
1040 *
1041 * The DPMS/power level of an output is used to switch it on or off. This
1042 * is DRM's hook for doing so, which can called either as part of repaint,
1043 * or independently of the repaint loop.
1044 *
1045 * If we are called as part of repaint, we simply set the relevant bit in
1046 * state and return.
Tomohito Esakib1fb00d2018-01-31 17:50:48 +09001047 *
1048 * This function is never called on a virtual output.
Daniel Stonea08512f2016-11-08 17:46:10 +00001049 */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001050static void
1051drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1052{
Armin Krezović545dba62016-08-05 15:54:18 +02001053 struct drm_output *output = to_drm_output(output_base);
Daniel Stonea08512f2016-11-08 17:46:10 +00001054 struct drm_backend *b = to_drm_backend(output_base->compositor);
1055 struct drm_pending_state *pending_state = b->repaint_data;
1056 struct drm_output_state *state;
Daniel Stone36609c72015-06-18 07:49:02 +01001057 int ret;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001058
Tomohito Esakib1fb00d2018-01-31 17:50:48 +09001059 assert(!output->virtual);
1060
Daniel Stonea08512f2016-11-08 17:46:10 +00001061 if (output->state_cur->dpms == level)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001062 return;
1063
Daniel Stonea08512f2016-11-08 17:46:10 +00001064 /* If we're being called during the repaint loop, then this is
1065 * simple: discard any previously-generated state, and create a new
1066 * state where we disable everything. When we come to flush, this
1067 * will be applied.
1068 *
1069 * However, we need to be careful: we can be called whilst another
1070 * output is in its repaint cycle (pending_state exists), but our
1071 * output still has an incomplete state application outstanding.
1072 * In that case, we need to wait until that completes. */
1073 if (pending_state && !output->state_last) {
1074 /* The repaint loop already sets DPMS on; we don't need to
1075 * explicitly set it on here, as it will already happen
1076 * whilst applying the repaint state. */
1077 if (level == WESTON_DPMS_ON)
1078 return;
1079
1080 state = drm_pending_state_get_output(pending_state, output);
1081 if (state)
1082 drm_output_state_free(state);
1083 state = drm_output_get_disable_state(pending_state, output);
Daniel Stone36609c72015-06-18 07:49:02 +01001084 return;
1085 }
1086
Daniel Stonea08512f2016-11-08 17:46:10 +00001087 /* As we throw everything away when disabling, just send us back through
1088 * a repaint cycle. */
1089 if (level == WESTON_DPMS_ON) {
1090 if (output->dpms_off_pending)
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01001091 output->dpms_off_pending = false;
Daniel Stonea08512f2016-11-08 17:46:10 +00001092 weston_output_schedule_repaint(output_base);
1093 return;
1094 }
1095
1096 /* If we've already got a request in the pipeline, then we need to
1097 * park our DPMS request until that request has quiesced. */
1098 if (output->state_last) {
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01001099 output->dpms_off_pending = true;
Daniel Stonea08512f2016-11-08 17:46:10 +00001100 return;
1101 }
1102
1103 pending_state = drm_pending_state_alloc(b);
1104 drm_output_get_disable_state(pending_state, output);
1105 ret = drm_pending_state_apply_sync(pending_state);
1106 if (ret != 0)
1107 weston_log("drm_set_dpms: couldn't disable output?\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001108}
1109
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001110static const char * const connector_type_names[] = {
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001111 [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
1112 [DRM_MODE_CONNECTOR_VGA] = "VGA",
1113 [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
1114 [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
1115 [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
1116 [DRM_MODE_CONNECTOR_Composite] = "Composite",
1117 [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
1118 [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
1119 [DRM_MODE_CONNECTOR_Component] = "Component",
1120 [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
1121 [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
1122 [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
1123 [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
1124 [DRM_MODE_CONNECTOR_TV] = "TV",
1125 [DRM_MODE_CONNECTOR_eDP] = "eDP",
1126 [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
1127 [DRM_MODE_CONNECTOR_DSI] = "DSI",
Stefan Agner30e283d2018-08-20 17:11:38 +02001128 [DRM_MODE_CONNECTOR_DPI] = "DPI",
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04001129};
1130
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03001131/** Create a name given a DRM connector
1132 *
1133 * \param con The DRM connector whose type and id form the name.
1134 * \return A newly allocate string, or NULL on error. Must be free()'d
1135 * after use.
1136 *
1137 * The name does not identify the DRM display device.
1138 */
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001139static char *
1140make_connector_name(const drmModeConnector *con)
1141{
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03001142 char *name;
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001143 const char *type_name = NULL;
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03001144 int ret;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001145
1146 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
1147 type_name = connector_type_names[con->connector_type];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001148
1149 if (!type_name)
1150 type_name = "UNNAMED";
1151
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03001152 ret = asprintf(&name, "%s-%d", type_name, con->connector_type_id);
1153 if (ret < 0)
1154 return NULL;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001155
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03001156 return name;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001157}
1158
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001159static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001160drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001161{
Hardeningff39efa2013-09-18 23:56:35 +02001162 int w = output->base.current_mode->width;
1163 int h = output->base.current_mode->height;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001164 uint32_t format = output->gbm_format;
1165 uint32_t pixman_format;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001166 unsigned int i;
Daniel Stone61abf352020-03-06 12:46:30 +00001167 const struct pixman_renderer_output_options options = {
1168 .use_shadow = b->use_pixman_shadow,
1169 };
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001170
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001171 switch (format) {
Stefan Agner0bfebeb2019-07-08 00:30:44 +02001172 case DRM_FORMAT_XRGB8888:
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001173 pixman_format = PIXMAN_x8r8g8b8;
1174 break;
Stefan Agner0bfebeb2019-07-08 00:30:44 +02001175 case DRM_FORMAT_RGB565:
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001176 pixman_format = PIXMAN_r5g6b5;
1177 break;
1178 default:
1179 weston_log("Unsupported pixman format 0x%x\n", format);
1180 return -1;
1181 }
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001182
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001183 /* FIXME error checking */
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001184 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001185 output->dumb[i] = drm_fb_create_dumb(b, w, h, format);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001186 if (!output->dumb[i])
1187 goto err;
1188
1189 output->image[i] =
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001190 pixman_image_create_bits(pixman_format, w, h,
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001191 output->dumb[i]->map,
Daniel Stone8eece0c2016-11-17 17:54:00 +00001192 output->dumb[i]->strides[0]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001193 if (!output->image[i])
1194 goto err;
1195 }
1196
Daniel Stone61abf352020-03-06 12:46:30 +00001197 if (pixman_renderer_output_create(&output->base, &options) < 0)
Dongjin Kimba89f002024-09-30 17:49:36 +09001198 goto err;
Ankit Nautiyala21c3932097-03-19 00:24:57 +05301199
Pekka Paalanendee412d2018-04-23 11:44:58 +02001200 weston_log("DRM: output %s %s shadow framebuffer.\n", output->base.name,
1201 b->use_pixman_shadow ? "uses" : "does not use");
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001202
1203 pixman_region32_init_rect(&output->previous_damage,
Alexander Larsson0b135062013-05-28 16:23:36 +02001204 output->base.x, output->base.y, output->base.width, output->base.height);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001205
1206 return 0;
1207
1208err:
1209 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1210 if (output->dumb[i])
Daniel Stone6e7a9612017-04-04 17:54:26 +01001211 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001212 if (output->image[i])
1213 pixman_image_unref(output->image[i]);
1214
1215 output->dumb[i] = NULL;
1216 output->image[i] = NULL;
1217 }
1218
1219 return -1;
1220}
1221
1222static void
1223drm_output_fini_pixman(struct drm_output *output)
1224{
Daniel Stonee2e80132018-01-16 15:37:33 +00001225 struct drm_backend *b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001226 unsigned int i;
1227
Daniel Stonee2e80132018-01-16 15:37:33 +00001228 /* Destroying the Pixman surface will destroy all our buffers,
1229 * regardless of refcount. Ensure we destroy them here. */
1230 if (!b->shutting_down &&
1231 output->scanout_plane->state_cur->fb &&
1232 output->scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB) {
Alexandros Frantzis99751342020-05-18 15:22:49 +03001233 drm_plane_reset_state(output->scanout_plane);
Daniel Stonee2e80132018-01-16 15:37:33 +00001234 }
1235
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001236 pixman_renderer_output_destroy(&output->base);
1237 pixman_region32_fini(&output->previous_damage);
1238
1239 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001240 pixman_image_unref(output->image[i]);
Daniel Stone6e7a9612017-04-04 17:54:26 +01001241 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001242 output->dumb[i] = NULL;
1243 output->image[i] = NULL;
1244 }
1245}
1246
Richard Hughes2b2092a2013-04-24 14:58:02 +01001247static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001248setup_output_seat_constraint(struct drm_backend *b,
Rob Bradford66bd9f52013-06-25 18:56:42 +01001249 struct weston_output *output,
1250 const char *s)
1251{
1252 if (strcmp(s, "") != 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05001253 struct weston_pointer *pointer;
Rob Bradford66bd9f52013-06-25 18:56:42 +01001254 struct udev_seat *seat;
1255
Giulio Camuffo954f1832014-10-11 18:27:30 +03001256 seat = udev_seat_get_named(&b->input, s);
Derek Foreman0720ea32015-07-15 13:00:35 -05001257 if (!seat)
1258 return;
Rob Bradford66bd9f52013-06-25 18:56:42 +01001259
Derek Foreman0720ea32015-07-15 13:00:35 -05001260 seat->base.output = output;
1261
Derek Foreman1281a362015-07-31 16:55:32 -05001262 pointer = weston_seat_get_pointer(&seat->base);
1263 if (pointer)
1264 weston_pointer_clamp(pointer,
1265 &pointer->x,
1266 &pointer->y);
Rob Bradford66bd9f52013-06-25 18:56:42 +01001267 }
1268}
1269
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001270static int
Pekka Paalanenc112f002017-08-28 16:27:20 +03001271drm_output_attach_head(struct weston_output *output_base,
1272 struct weston_head *head_base)
1273{
Pekka Paalanend5f98d82017-12-08 14:45:00 +02001274 struct drm_backend *b = to_drm_backend(output_base->compositor);
1275
Pekka Paalanenc112f002017-08-28 16:27:20 +03001276 if (wl_list_length(&output_base->head_list) >= MAX_CLONED_CONNECTORS)
1277 return -1;
1278
Pekka Paalanend5f98d82017-12-08 14:45:00 +02001279 if (!output_base->enabled)
1280 return 0;
1281
1282 /* XXX: ensure the configuration will work.
1283 * This is actually impossible without major infrastructure
1284 * work. */
1285
1286 /* Need to go through modeset to add connectors. */
1287 /* XXX: Ideally we'd do this per-output, not globally. */
1288 /* XXX: Doing it globally, what guarantees another output's update
1289 * will not clear the flag before this output is updated?
1290 */
1291 b->state_invalid = true;
1292
1293 weston_output_schedule_repaint(output_base);
1294
Pekka Paalanenc112f002017-08-28 16:27:20 +03001295 return 0;
1296}
1297
Pekka Paalanen7f853792017-11-29 14:33:33 +02001298static void
1299drm_output_detach_head(struct weston_output *output_base,
1300 struct weston_head *head_base)
1301{
1302 struct drm_backend *b = to_drm_backend(output_base->compositor);
1303
1304 if (!output_base->enabled)
1305 return;
1306
1307 /* Need to go through modeset to drop connectors that should no longer
1308 * be driven. */
1309 /* XXX: Ideally we'd do this per-output, not globally. */
1310 b->state_invalid = true;
1311
1312 weston_output_schedule_repaint(output_base);
1313}
1314
Stefan Agner3654c672019-07-09 00:50:30 +02001315int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001316parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format)
Neil Roberts77c1a5b2014-03-07 18:05:50 +00001317{
Pekka Paalanen62a94362018-09-26 14:33:36 +03001318 const struct pixel_format_info *pinfo;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00001319
Pekka Paalanen62a94362018-09-26 14:33:36 +03001320 if (s == NULL) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001321 *gbm_format = default_value;
Pekka Paalanen62a94362018-09-26 14:33:36 +03001322
1323 return 0;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00001324 }
1325
Pekka Paalanen62a94362018-09-26 14:33:36 +03001326 pinfo = pixel_format_get_info_by_drm_name(s);
1327 if (!pinfo) {
1328 weston_log("fatal: unrecognized pixel format: %s\n", s);
1329
1330 return -1;
1331 }
1332
1333 /* GBM formats and DRM formats are identical. */
1334 *gbm_format = pinfo->format;
1335
1336 return 0;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00001337}
1338
Pekka Paalaneneee580b2014-06-04 16:43:06 +03001339static int
Pekka Paalanen9c03a7c2017-11-28 14:30:10 +02001340drm_head_read_current_setup(struct drm_head *head, struct drm_backend *backend)
Pekka Paalaneneee580b2014-06-04 16:43:06 +03001341{
Pekka Paalanen9c03a7c2017-11-28 14:30:10 +02001342 int drm_fd = backend->drm.fd;
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001343 drmModeConnector *conn = head->connector.conn;
Pekka Paalaneneee580b2014-06-04 16:43:06 +03001344 drmModeEncoder *encoder;
1345 drmModeCrtc *crtc;
1346
1347 /* Get the current mode on the crtc that's currently driving
1348 * this connector. */
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001349 encoder = drmModeGetEncoder(drm_fd, conn->encoder_id);
Pekka Paalaneneee580b2014-06-04 16:43:06 +03001350 if (encoder != NULL) {
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001351 head->inherited_crtc_id = encoder->crtc_id;
1352
Pekka Paalaneneee580b2014-06-04 16:43:06 +03001353 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
1354 drmModeFreeEncoder(encoder);
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001355
Pekka Paalaneneee580b2014-06-04 16:43:06 +03001356 if (crtc == NULL)
1357 return -1;
1358 if (crtc->mode_valid)
Pekka Paalanen6fae2be2017-11-28 14:33:52 +02001359 head->inherited_mode = crtc->mode;
Pekka Paalaneneee580b2014-06-04 16:43:06 +03001360 drmModeFreeCrtc(crtc);
1361 }
1362
1363 return 0;
1364}
1365
Armin Krezović08368132016-09-30 14:11:05 +02001366static void
1367drm_output_set_gbm_format(struct weston_output *base,
1368 const char *gbm_format)
1369{
1370 struct drm_output *output = to_drm_output(base);
1371 struct drm_backend *b = to_drm_backend(base->compositor);
1372
1373 if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1)
1374 output->gbm_format = b->gbm_format;
1375}
1376
1377static void
1378drm_output_set_seat(struct weston_output *base,
1379 const char *seat)
1380{
1381 struct drm_output *output = to_drm_output(base);
1382 struct drm_backend *b = to_drm_backend(base->compositor);
1383
1384 setup_output_seat_constraint(b, &output->base,
1385 seat ? seat : "");
1386}
1387
1388static int
Pekka Paalanenc4db6f72017-09-05 16:37:03 +03001389drm_output_init_gamma_size(struct drm_output *output)
1390{
1391 struct drm_backend *backend = to_drm_backend(output->base.compositor);
1392 drmModeCrtc *crtc;
1393
1394 assert(output->base.compositor);
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001395 assert(output->crtc);
1396 crtc = drmModeGetCrtc(backend->drm.fd, output->crtc->crtc_id);
Pekka Paalanenc4db6f72017-09-05 16:37:03 +03001397 if (!crtc)
1398 return -1;
1399
1400 output->base.gamma_size = crtc->gamma_size;
1401
1402 drmModeFreeCrtc(crtc);
1403
1404 return 0;
1405}
1406
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001407static uint32_t
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001408drm_connector_get_possible_crtcs_mask(struct drm_connector *connector)
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001409{
1410 uint32_t possible_crtcs = 0;
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001411 drmModeConnector *conn = connector->conn;
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001412 drmModeEncoder *encoder;
1413 int i;
1414
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001415 for (i = 0; i < conn->count_encoders; i++) {
1416 encoder = drmModeGetEncoder(connector->backend->drm.fd,
1417 conn->encoders[i]);
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001418 if (!encoder)
1419 continue;
1420
1421 possible_crtcs |= encoder->possible_crtcs;
1422 drmModeFreeEncoder(encoder);
1423 }
1424
1425 return possible_crtcs;
1426}
1427
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001428/** Pick a CRTC that might be able to drive all attached connectors
1429 *
1430 * @param output The output whose attached heads to include.
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001431 * @return CRTC object to pick, or NULL on failure or not found.
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001432 */
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001433static struct drm_crtc *
1434drm_output_pick_crtc(struct drm_output *output)
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001435{
1436 struct drm_backend *backend;
1437 struct weston_head *base;
1438 struct drm_head *head;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001439 struct drm_crtc *crtc;
1440 struct drm_crtc *best_crtc = NULL;
1441 struct drm_crtc *fallback_crtc = NULL;
1442 struct drm_crtc *existing_crtc[32];
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001443 uint32_t possible_crtcs = 0xffffffff;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001444 unsigned n = 0;
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001445 uint32_t crtc_id;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001446 unsigned int i;
Pekka Paalanendb4c7d72017-11-28 16:11:00 +02001447 bool match;
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001448
1449 backend = to_drm_backend(output->base.compositor);
1450
1451 /* This algorithm ignores drmModeEncoder::possible_clones restriction,
1452 * because it is more often set wrong than not in the kernel. */
1453
1454 /* Accumulate a mask of possible crtcs and find existing routings. */
1455 wl_list_for_each(base, &output->base.head_list, output_link) {
1456 head = to_drm_head(base);
1457
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001458 possible_crtcs &=
1459 drm_connector_get_possible_crtcs_mask(&head->connector);
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001460
1461 crtc_id = head->inherited_crtc_id;
1462 if (crtc_id > 0 && n < ARRAY_LENGTH(existing_crtc))
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001463 existing_crtc[n++] = drm_crtc_find(backend, crtc_id);
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001464 }
1465
1466 /* Find a crtc that could drive each connector individually at least,
1467 * and prefer existing routings. */
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001468 wl_list_for_each(crtc, &backend->crtc_list, link) {
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001469
1470 /* Could the crtc not drive each connector? */
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001471 if (!(possible_crtcs & (1 << crtc->pipe)))
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001472 continue;
1473
1474 /* Is the crtc already in use? */
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001475 if (crtc->output)
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001476 continue;
1477
1478 /* Try to preserve the existing CRTC -> connector routing;
1479 * it makes initialisation faster, and also since we have a
1480 * very dumb picking algorithm, may preserve a better
1481 * choice. */
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001482 for (i = 0; i < n; i++) {
1483 if (existing_crtc[i] == crtc)
1484 return crtc;
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001485 }
1486
Pekka Paalanendb4c7d72017-11-28 16:11:00 +02001487 /* Check if any other head had existing routing to this CRTC.
1488 * If they did, this is not the best CRTC as it might be needed
1489 * for another output we haven't enabled yet. */
1490 match = false;
1491 wl_list_for_each(base, &backend->compositor->head_list,
1492 compositor_link) {
1493 head = to_drm_head(base);
1494
1495 if (head->base.output == &output->base)
1496 continue;
1497
1498 if (weston_head_is_enabled(&head->base))
1499 continue;
1500
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001501 if (head->inherited_crtc_id == crtc->crtc_id) {
Pekka Paalanendb4c7d72017-11-28 16:11:00 +02001502 match = true;
1503 break;
1504 }
1505 }
1506 if (!match)
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001507 best_crtc = crtc;
Pekka Paalanendb4c7d72017-11-28 16:11:00 +02001508
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001509 fallback_crtc = crtc;
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001510 }
1511
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001512 if (best_crtc)
1513 return best_crtc;
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001514
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001515 if (fallback_crtc)
1516 return fallback_crtc;
Pekka Paalanendb4c7d72017-11-28 16:11:00 +02001517
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001518 /* Likely possible_crtcs was empty due to asking for clones,
1519 * but since the DRM documentation says the kernel lies, let's
1520 * pick one crtc anyway. Trial and error is the only way to
1521 * be sure if something doesn't work. */
1522
1523 /* First pick any existing assignment. */
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001524 for (i = 0; i < n; i++) {
1525 crtc = existing_crtc[i];
1526 if (!crtc->output)
1527 return crtc;
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001528 }
1529
1530 /* Otherwise pick any available crtc. */
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001531 wl_list_for_each(crtc, &backend->crtc_list, link) {
1532 if (!crtc->output)
1533 return crtc;
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001534 }
1535
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001536 return NULL;
1537}
1538
1539/** Create an "empty" drm_crtc. It will only set its ID, pipe and props. After
1540 * all, it adds the object to the DRM-backend CRTC list.
1541 */
1542static struct drm_crtc *
1543drm_crtc_create(struct drm_backend *b, uint32_t crtc_id, uint32_t pipe)
1544{
1545 struct drm_crtc *crtc;
1546 drmModeObjectPropertiesPtr props;
1547
1548 props = drmModeObjectGetProperties(b->drm.fd, crtc_id,
1549 DRM_MODE_OBJECT_CRTC);
1550 if (!props) {
1551 weston_log("failed to get CRTC properties\n");
1552 return NULL;
1553 }
1554
1555 crtc = zalloc(sizeof(*crtc));
1556 if (!crtc)
1557 goto ret;
1558
1559 drm_property_info_populate(b, crtc_props, crtc->props_crtc,
1560 WDRM_CRTC__COUNT, props);
1561 crtc->backend = b;
1562 crtc->crtc_id = crtc_id;
1563 crtc->pipe = pipe;
1564 crtc->output = NULL;
1565
1566 /* Add it to the last position of the DRM-backend CRTC list */
1567 wl_list_insert(b->crtc_list.prev, &crtc->link);
1568
1569ret:
1570 drmModeFreeObjectProperties(props);
1571 return crtc;
1572}
1573
1574/** Destroy a drm_crtc object that was created with drm_crtc_create(). It will
1575 * also remove it from the DRM-backend CRTC list.
1576 */
1577static void
1578drm_crtc_destroy(struct drm_crtc *crtc)
1579{
1580 /* TODO: address the issue below to be able to remove the comment
1581 * from the assert.
1582 *
1583 * https://gitlab.freedesktop.org/wayland/weston/-/issues/421
1584 */
1585
1586 //assert(!crtc->output);
1587
1588 wl_list_remove(&crtc->link);
1589 drm_property_info_free(crtc->props_crtc, WDRM_CRTC__COUNT);
1590 free(crtc);
1591}
1592
1593/** Find all CRTCs of the fd and create drm_crtc objects for them.
1594 *
1595 * The CRTCs are saved in a list of the drm_backend and will keep there until
1596 * the fd gets closed.
1597 *
1598 * @param b The DRM-backend structure.
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03001599 * @param resources The DRM resources, it is taken with drmModeGetResources
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001600 * @return 0 on success (at least one CRTC in the list), -1 on failure.
1601 */
1602static int
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03001603drm_backend_create_crtc_list(struct drm_backend *b, drmModeRes *resources)
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001604{
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001605 struct drm_crtc *crtc, *crtc_tmp;
1606 int i;
1607
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001608 /* Iterate through all CRTCs */
1609 for (i = 0; i < resources->count_crtcs; i++) {
1610
1611 /* Let's create an object for the CRTC and add it to the list */
1612 crtc = drm_crtc_create(b, resources->crtcs[i], i);
1613 if (!crtc)
1614 goto err;
1615 }
1616
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001617 return 0;
1618
1619err:
1620 wl_list_for_each_safe(crtc, crtc_tmp, &b->crtc_list, link)
1621 drm_crtc_destroy(crtc);
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001622 return -1;
1623}
1624
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001625
1626/** Populates scanout and cursor planes for the output. Also sets the topology
1627 * of the planes by adding them to the plane stacking list.
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001628 */
1629static int
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001630drm_output_init_planes(struct drm_output *output)
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001631{
1632 struct drm_backend *b = to_drm_backend(output->base.compositor);
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001633
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001634 output->scanout_plane =
1635 drm_output_find_special_plane(b, output,
1636 WDRM_PLANE_TYPE_PRIMARY);
1637 if (!output->scanout_plane) {
1638 weston_log("Failed to find primary plane for output %s\n",
1639 output->base.name);
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001640 return -1;
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001641 }
1642
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001643 weston_compositor_stack_plane(b->compositor,
1644 &output->scanout_plane->base,
1645 &b->compositor->primary_plane);
1646
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001647 /* Failing to find a cursor plane is not fatal, as we'll fall back
1648 * to software cursor. */
1649 output->cursor_plane =
1650 drm_output_find_special_plane(b, output,
1651 WDRM_PLANE_TYPE_CURSOR);
1652
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001653 if (output->cursor_plane)
1654 weston_compositor_stack_plane(b->compositor,
1655 &output->cursor_plane->base,
1656 NULL);
1657 else
1658 b->cursors_are_broken = true;
Pekka Paalanen663d5e92017-09-08 13:32:40 +03001659
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001660 return 0;
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001661}
1662
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001663/** The opposite of drm_output_init_planes(). First of all it removes the planes
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -03001664 * from the plane stacking list. After all it sets the planes of the output as NULL.
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001665 */
1666static void
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001667drm_output_deinit_planes(struct drm_output *output)
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001668{
1669 struct drm_backend *b = to_drm_backend(output->base.compositor);
1670
Alexandros Frantzis53a71cb2020-05-18 15:26:01 +03001671 /* If the compositor is already shutting down, the planes have already
1672 * been destroyed. */
1673 if (!b->shutting_down) {
Pekka Paalanen3d6721e2020-09-16 15:04:04 +03001674 wl_list_remove(&output->scanout_plane->base.link);
1675 wl_list_init(&output->scanout_plane->base.link);
1676
1677 if (output->cursor_plane) {
1678 wl_list_remove(&output->cursor_plane->base.link);
1679 wl_list_init(&output->cursor_plane->base.link);
1680 /* Turn off hardware cursor */
1681 drmModeSetCursor(b->drm.fd, output->crtc->crtc_id, 0, 0, 0);
1682 }
1683
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -03001684 /* With universal planes, the planes are allocated at startup,
1685 * freed at shutdown, and live on the plane list in between.
1686 * We want the planes to continue to exist and be freed up
1687 * for other outputs.
1688 */
1689 if (output->cursor_plane)
1690 drm_plane_reset_state(output->cursor_plane);
1691 if (output->scanout_plane)
1692 drm_plane_reset_state(output->scanout_plane);
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001693 }
1694
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001695 output->cursor_plane = NULL;
1696 output->scanout_plane = NULL;
1697}
1698
Leandro Ribeiro54293022021-10-12 14:48:36 -03001699static struct weston_drm_format_array *
1700get_scanout_formats(struct drm_backend *b)
1701{
1702 struct weston_compositor *ec = b->compositor;
1703 const struct weston_drm_format_array *renderer_formats;
1704 struct weston_drm_format_array *scanout_formats, union_planes_formats;
1705 struct drm_plane *plane;
1706 int ret;
1707
1708 /* If we got here it means that dma-buf feedback is supported and that
1709 * the renderer has formats/modifiers to expose. */
1710 assert(ec->renderer->get_supported_formats != NULL);
1711 renderer_formats = ec->renderer->get_supported_formats(ec);
1712
1713 scanout_formats = zalloc(sizeof(*scanout_formats));
1714 if (!scanout_formats) {
1715 weston_log("%s: out of memory\n", __func__);
1716 return NULL;
1717 }
1718
1719 weston_drm_format_array_init(&union_planes_formats);
1720 weston_drm_format_array_init(scanout_formats);
1721
1722 /* Compute the union of the format/modifiers of the KMS planes */
1723 wl_list_for_each(plane, &b->plane_list, link) {
1724 /* The scanout formats are used by the dma-buf feedback. But for
1725 * now cursor planes do not support dma-buf buffers, only wl_shm
1726 * buffers. So we skip cursor planes here. */
1727 if (plane->type == WDRM_PLANE_TYPE_CURSOR)
1728 continue;
1729
1730 ret = weston_drm_format_array_join(&union_planes_formats,
1731 &plane->formats);
1732 if (ret < 0)
1733 goto err;
1734 }
1735
1736 /* Compute the intersection between the union of format/modifiers of KMS
1737 * planes and the formats supported by the renderer */
1738 ret = weston_drm_format_array_replace(scanout_formats,
1739 renderer_formats);
1740 if (ret < 0)
1741 goto err;
1742
1743 ret = weston_drm_format_array_intersect(scanout_formats,
1744 &union_planes_formats);
1745 if (ret < 0)
1746 goto err;
1747
1748 weston_drm_format_array_fini(&union_planes_formats);
1749
1750 return scanout_formats;
1751
1752err:
1753 weston_drm_format_array_fini(&union_planes_formats);
1754 weston_drm_format_array_fini(scanout_formats);
Daniel Stonef5086032021-12-06 16:52:19 +00001755 free(scanout_formats);
Leandro Ribeiro54293022021-10-12 14:48:36 -03001756 return NULL;
1757}
1758
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001759/** Pick a CRTC and reserve it for the output.
1760 *
1761 * On failure, the output remains without a CRTC.
1762 *
1763 * @param output The output with no CRTC associated.
1764 * @return 0 on success, -1 on failure.
1765 */
1766static int
1767drm_output_attach_crtc(struct drm_output *output)
1768{
1769 output->crtc = drm_output_pick_crtc(output);
1770 if (!output->crtc) {
1771 weston_log("Output '%s': No available CRTCs.\n",
1772 output->base.name);
1773 return -1;
1774 }
1775
1776 /* Reserve the CRTC for the output */
1777 output->crtc->output = output;
1778
1779 return 0;
1780}
1781
1782/** Release reservation of the CRTC.
1783 *
1784 * Make the CRTC free to be reserved and used by another output.
1785 *
1786 * @param output The output that will release its CRTC.
1787 */
1788static void
1789drm_output_detach_crtc(struct drm_output *output)
1790{
1791 struct drm_backend *b = output->backend;
1792 struct drm_crtc *crtc = output->crtc;
Pekka Paalanen663d5e92017-09-08 13:32:40 +03001793
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001794 crtc->output = NULL;
1795 output->crtc = NULL;
1796
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001797 /* Force resetting unused CRTCs */
1798 b->state_invalid = true;
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001799}
1800
Pekka Paalanenc4db6f72017-09-05 16:37:03 +03001801static int
Armin Krezović08368132016-09-30 14:11:05 +02001802drm_output_enable(struct weston_output *base)
1803{
1804 struct drm_output *output = to_drm_output(base);
1805 struct drm_backend *b = to_drm_backend(base->compositor);
Pekka Paalanen663d5e92017-09-08 13:32:40 +03001806 int ret;
1807
Tomohito Esakib1fb00d2018-01-31 17:50:48 +09001808 assert(!output->virtual);
1809
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001810 ret = drm_output_attach_crtc(output);
Pekka Paalanen663d5e92017-09-08 13:32:40 +03001811 if (ret < 0)
1812 return -1;
1813
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001814 ret = drm_output_init_planes(output);
1815 if (ret < 0)
1816 goto err_crtc;
1817
Pekka Paalanen663d5e92017-09-08 13:32:40 +03001818 if (drm_output_init_gamma_size(output) < 0)
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001819 goto err_planes;
Armin Krezović08368132016-09-30 14:11:05 +02001820
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001821 if (b->pageflip_timeout)
1822 drm_output_pageflip_timer_create(output);
1823
Giulio Camuffo954f1832014-10-11 18:27:30 +03001824 if (b->use_pixman) {
1825 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001826 weston_log("Failed to init output pixman state\n");
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001827 goto err_planes;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001828 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03001829 } else if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001830 weston_log("Failed to init output gl state\n");
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001831 goto err_planes;
Kristian Høgsberg1d1e0a52012-10-21 13:29:26 -04001832 }
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04001833
leng.fang32af9fc2024-06-13 11:22:15 +08001834 drm_enable_aml_output_config(output, base, b);
Pekka Paalanenf8b850d2017-11-15 12:51:01 +02001835 drm_output_init_backlight(output);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001836
Jonas Ådahle5a12252013-04-05 23:07:11 +02001837 output->base.start_repaint_loop = drm_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05001838 output->base.repaint = drm_output_repaint;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001839 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001840 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08001841 output->base.switch_mode = drm_output_switch_mode;
Richard Hughese7299962013-05-01 21:52:12 +01001842 output->base.set_gamma = drm_output_set_gamma;
1843
Pekka Paalanenc0eb2542017-11-15 13:37:18 +02001844 weston_log("Output %s (crtc %d) video modes:\n",
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001845 output->base.name, output->crtc->crtc_id);
Pekka Paalanenc0eb2542017-11-15 13:37:18 +02001846 drm_output_print_modes(output);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001847
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001848 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01001849
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001850err_planes:
1851 drm_output_deinit_planes(output);
1852err_crtc:
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001853 drm_output_detach_crtc(output);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001854 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001855}
1856
Jesse Barnes58ef3792012-02-23 09:45:49 -05001857static void
Armin Krezović08368132016-09-30 14:11:05 +02001858drm_output_deinit(struct weston_output *base)
1859{
1860 struct drm_output *output = to_drm_output(base);
1861 struct drm_backend *b = to_drm_backend(base->compositor);
1862
Daniel Stone3e661f72016-11-04 17:24:06 +00001863 if (b->use_pixman)
Armin Krezović08368132016-09-30 14:11:05 +02001864 drm_output_fini_pixman(output);
Daniel Stone3e661f72016-11-04 17:24:06 +00001865 else
1866 drm_output_fini_egl(output);
Armin Krezović08368132016-09-30 14:11:05 +02001867
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001868 drm_output_deinit_planes(output);
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001869 drm_output_detach_crtc(output);
Armin Krezović08368132016-09-30 14:11:05 +02001870}
1871
1872static void
Pekka Paalanenc112f002017-08-28 16:27:20 +03001873drm_head_destroy(struct drm_head *head);
1874
1875static void
Armin Krezović08368132016-09-30 14:11:05 +02001876drm_output_destroy(struct weston_output *base)
1877{
1878 struct drm_output *output = to_drm_output(base);
1879 struct drm_backend *b = to_drm_backend(base->compositor);
Armin Krezović08368132016-09-30 14:11:05 +02001880
Tomohito Esakib1fb00d2018-01-31 17:50:48 +09001881 assert(!output->virtual);
1882
Daniel Stone31838bf2019-06-17 11:23:25 +01001883 if (output->page_flip_pending || output->atomic_complete_pending) {
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01001884 output->destroy_pending = true;
Armin Krezović08368132016-09-30 14:11:05 +02001885 weston_log("destroy output while page flip pending\n");
1886 return;
1887 }
1888
Alexandros Frantzis10937fe2021-06-14 13:09:44 +03001889 drm_output_set_cursor_view(output, NULL);
1890
Armin Krezović08368132016-09-30 14:11:05 +02001891 if (output->base.enabled)
1892 drm_output_deinit(&output->base);
1893
Pekka Paalanen383b3af2017-09-11 14:40:48 +03001894 drm_mode_list_destroy(b, &output->base.mode_list);
Armin Krezović445b41b2016-10-09 23:48:16 +02001895
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001896 if (output->pageflip_timer)
1897 wl_event_source_remove(output->pageflip_timer);
1898
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03001899 weston_output_release(&output->base);
Armin Krezović08368132016-09-30 14:11:05 +02001900
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001901 assert(!output->state_last);
1902 drm_output_state_free(output->state_cur);
1903
Armin Krezović08368132016-09-30 14:11:05 +02001904 free(output);
1905}
1906
1907static int
1908drm_output_disable(struct weston_output *base)
1909{
1910 struct drm_output *output = to_drm_output(base);
Armin Krezović08368132016-09-30 14:11:05 +02001911
Tomohito Esakib1fb00d2018-01-31 17:50:48 +09001912 assert(!output->virtual);
leng.fang32af9fc2024-06-13 11:22:15 +08001913 drm_output_disable_aml(output, base);
Daniel Stonea08512f2016-11-08 17:46:10 +00001914
Armin Krezović08368132016-09-30 14:11:05 +02001915 if (output->base.enabled)
1916 drm_output_deinit(&output->base);
1917
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01001918 output->disable_pending = false;
Armin Krezović08368132016-09-30 14:11:05 +02001919
Armin Krezović08368132016-09-30 14:11:05 +02001920 return 0;
1921}
1922
Ankit Nautiyala344fe32019-05-14 18:36:08 +05301923/*
1924 * This function converts the protection status from drm values to
1925 * weston_hdcp_protection status. The drm values as read from the connector
1926 * properties "Content Protection" and "HDCP Content Type" need to be converted
1927 * to appropriate weston values, that can be sent to a client application.
1928 */
1929static int
1930get_weston_protection_from_drm(enum wdrm_content_protection_state protection,
1931 enum wdrm_hdcp_content_type type,
1932 enum weston_hdcp_protection *weston_protection)
1933
1934{
1935 if (protection >= WDRM_CONTENT_PROTECTION__COUNT)
1936 return -1;
1937 if (protection == WDRM_CONTENT_PROTECTION_DESIRED ||
1938 protection == WDRM_CONTENT_PROTECTION_UNDESIRED) {
1939 *weston_protection = WESTON_HDCP_DISABLE;
1940 return 0;
1941 }
1942 if (type >= WDRM_HDCP_CONTENT_TYPE__COUNT)
1943 return -1;
1944 if (type == WDRM_HDCP_CONTENT_TYPE0) {
1945 *weston_protection = WESTON_HDCP_ENABLE_TYPE_0;
1946 return 0;
1947 }
1948 if (type == WDRM_HDCP_CONTENT_TYPE1) {
1949 *weston_protection = WESTON_HDCP_ENABLE_TYPE_1;
1950 return 0;
1951 }
1952 return -1;
1953}
1954
1955/**
1956 * Get current content-protection status for a given head.
1957 *
1958 * @param head drm_head, whose protection is to be retrieved
Ankit Nautiyala344fe32019-05-14 18:36:08 +05301959 * @return protection status in case of success, -1 otherwise
1960 */
1961static enum weston_hdcp_protection
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03001962drm_head_get_current_protection(struct drm_head *head)
Ankit Nautiyala344fe32019-05-14 18:36:08 +05301963{
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03001964 drmModeObjectProperties *props = head->connector.props_drm;
Ankit Nautiyala344fe32019-05-14 18:36:08 +05301965 struct drm_property_info *info;
1966 enum wdrm_content_protection_state protection;
1967 enum wdrm_hdcp_content_type type;
1968 enum weston_hdcp_protection weston_hdcp = WESTON_HDCP_DISABLE;
1969
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001970 info = &head->connector.props[WDRM_CONNECTOR_CONTENT_PROTECTION];
Ankit Nautiyala344fe32019-05-14 18:36:08 +05301971 protection = drm_property_get_value(info, props,
1972 WDRM_CONTENT_PROTECTION__COUNT);
1973
1974 if (protection == WDRM_CONTENT_PROTECTION__COUNT)
1975 return WESTON_HDCP_DISABLE;
1976
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001977 info = &head->connector.props[WDRM_CONNECTOR_HDCP_CONTENT_TYPE];
Ankit Nautiyala344fe32019-05-14 18:36:08 +05301978 type = drm_property_get_value(info, props,
1979 WDRM_HDCP_CONTENT_TYPE__COUNT);
1980
1981 /*
1982 * In case of platforms supporting HDCP1.4, only property
1983 * 'Content Protection' is exposed and not the 'HDCP Content Type'
1984 * for such cases HDCP Type 0 should be considered as the content-type.
1985 */
1986
1987 if (type == WDRM_HDCP_CONTENT_TYPE__COUNT)
1988 type = WDRM_HDCP_CONTENT_TYPE0;
1989
1990 if (get_weston_protection_from_drm(protection, type,
1991 &weston_hdcp) == -1) {
1992 weston_log("Invalid drm protection:%d type:%d, for head:%s connector-id:%d\n",
1993 protection, type, head->base.name,
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001994 head->connector.connector_id);
Ankit Nautiyala344fe32019-05-14 18:36:08 +05301995 return WESTON_HDCP_DISABLE;
1996 }
1997
1998 return weston_hdcp;
1999}
2000
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03002001static int
2002drm_connector_update_properties(struct drm_connector *connector)
2003{
2004 drmModeObjectProperties *props;
2005
2006 props = drmModeObjectGetProperties(connector->backend->drm.fd,
2007 connector->connector_id,
2008 DRM_MODE_OBJECT_CONNECTOR);
2009 if (!props) {
2010 weston_log("Error: failed to get connector properties\n");
2011 return -1;
2012 }
2013
2014 if (connector->props_drm)
2015 drmModeFreeObjectProperties(connector->props_drm);
2016 connector->props_drm = props;
2017
2018 return 0;
2019}
2020
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002021/** Replace connector data and monitor information
2022 *
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002023 * @param connector The drm_connector object to be updated.
2024 * @param conn The connector data to be owned by the drm_connector, must match
2025 * the current drm_connector ID.
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002026 * @return 0 on success, -1 on failure.
2027 *
2028 * Takes ownership of @c connector on success, not on failure.
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002029 */
2030static int
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002031drm_connector_assign_connector_info(struct drm_connector *connector,
2032 drmModeConnector *conn)
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002033{
Leandro Ribeiro70863962020-09-09 13:12:35 -03002034 assert(connector->conn != conn);
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002035 assert(connector->connector_id == conn->connector_id);
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002036
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03002037 if (drm_connector_update_properties(connector) < 0)
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002038 return -1;
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002039
leng.fang32af9fc2024-06-13 11:22:15 +08002040 drm_update_helper_connector(connector, conn);
2041
Leandro Ribeiro70863962020-09-09 13:12:35 -03002042 if (connector->conn)
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002043 drmModeFreeConnector(connector->conn);
2044 connector->conn = conn;
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002045
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002046 drm_property_info_free(connector->props, WDRM_CONNECTOR__COUNT);
2047 drm_property_info_populate(connector->backend, connector_props,
2048 connector->props,
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03002049 WDRM_CONNECTOR__COUNT, connector->props_drm);
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002050 return 0;
2051}
2052
Leandro Ribeiro70863962020-09-09 13:12:35 -03002053static void
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002054drm_connector_init(struct drm_backend *b, struct drm_connector *connector,
2055 uint32_t connector_id)
2056{
2057 connector->backend = b;
2058 connector->connector_id = connector_id;
Leandro Ribeiro70863962020-09-09 13:12:35 -03002059 connector->conn = NULL;
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03002060 connector->props_drm = NULL;
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002061}
2062
2063static void
2064drm_connector_fini(struct drm_connector *connector)
2065{
2066 drmModeFreeConnector(connector->conn);
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03002067 drmModeFreeObjectProperties(connector->props_drm);
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002068 drm_property_info_free(connector->props, WDRM_CONNECTOR__COUNT);
2069}
2070
Pekka Paalanen456dc732017-11-09 15:10:11 +02002071static void
2072drm_head_log_info(struct drm_head *head, const char *msg)
2073{
2074 if (head->base.connected) {
2075 weston_log("DRM: head '%s' %s, connector %d is connected, "
2076 "EDID make '%s', model '%s', serial '%s'\n",
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002077 head->base.name, msg, head->connector.connector_id,
Pekka Paalanen456dc732017-11-09 15:10:11 +02002078 head->base.make, head->base.model,
2079 head->base.serial_number ?: "");
2080 } else {
2081 weston_log("DRM: head '%s' %s, connector %d is disconnected.\n",
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002082 head->base.name, msg, head->connector.connector_id);
Pekka Paalanen456dc732017-11-09 15:10:11 +02002083 }
2084}
2085
Pekka Paalanend2e62422017-09-08 15:48:07 +03002086/** Update connector and monitor information
2087 *
2088 * @param head The head to update.
Leandro Ribeiro70863962020-09-09 13:12:35 -03002089 * @param conn The DRM connector object.
2090 * @returns 0 on success, -1 on failure.
Pekka Paalanend2e62422017-09-08 15:48:07 +03002091 *
Leandro Ribeiro70863962020-09-09 13:12:35 -03002092 * Updates monitor information and connection status. This may schedule a
2093 * heads changed call to the user.
Leandro Ribeiro4a5b67a2020-09-15 15:30:35 -03002094 *
2095 * Takes ownership of @c connector on success, not on failure.
Pekka Paalanend2e62422017-09-08 15:48:07 +03002096 */
Leandro Ribeiro70863962020-09-09 13:12:35 -03002097static int
2098drm_head_update_info(struct drm_head *head, drmModeConnector *conn)
Pekka Paalanend2e62422017-09-08 15:48:07 +03002099{
Leandro Ribeiro70863962020-09-09 13:12:35 -03002100 int ret;
Pekka Paalanend2e62422017-09-08 15:48:07 +03002101
Leandro Ribeiro70863962020-09-09 13:12:35 -03002102 ret = drm_connector_assign_connector_info(&head->connector, conn);
Pekka Paalanen456dc732017-11-09 15:10:11 +02002103
Leandro Ribeiro67941642020-09-15 00:11:02 -03002104 update_head_from_connector(head);
2105 weston_head_set_content_protection_status(&head->base,
2106 drm_head_get_current_protection(head));
2107
leng.fang91856072024-06-07 14:12:54 +08002108#ifdef ENABLE_MODE_POLICY
2109 if (head->base.connected)
2110 init_mode_policy_without_mode(&head->base,
2111 head->backend->drm.fd, head->connector.conn);
2112 if (head->base.device_changed)
2113 mode_policy_set_hotplug(head->base.connected ?
leng.fang13fd3982024-07-03 19:09:58 +08002114 AML_WESTON_HOTPLUG_PLUG : AML_WESTON_HOTPLUG_UNPLUG, false);
leng.fang91856072024-06-07 14:12:54 +08002115#endif
2116
Pekka Paalanen456dc732017-11-09 15:10:11 +02002117 if (head->base.device_changed)
2118 drm_head_log_info(head, "updated");
Leandro Ribeiro70863962020-09-09 13:12:35 -03002119
2120 return ret;
Pekka Paalanend2e62422017-09-08 15:48:07 +03002121}
2122
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002123/** Update writeback connector
2124 *
2125 * @param writeback The writeback to update.
2126 * @param conn DRM connector object.
2127 * @returns 0 on success, -1 on failure.
2128 *
2129 * Takes ownership of @c connector on success, not on failure.
2130 */
2131static int
2132drm_writeback_update_info(struct drm_writeback *writeback, drmModeConnector *conn)
2133{
2134 int ret;
2135
2136 ret = drm_connector_assign_connector_info(&writeback->connector, conn);
2137
2138 return ret;
2139}
2140
Daniel Stone087ddf02017-02-14 17:51:30 +00002141/**
Pekka Paalanenc112f002017-08-28 16:27:20 +03002142 * Create a Weston head for a connector
2143 *
2144 * Given a DRM connector, create a matching drm_head structure and add it
2145 * to Weston's head list.
2146 *
Marius Vlada2dace22019-06-12 16:05:44 +03002147 * @param backend Weston backend structure
Leandro Ribeiro70863962020-09-09 13:12:35 -03002148 * @param conn DRM connector object
Pekka Paalanenc112f002017-08-28 16:27:20 +03002149 * @param drm_device udev device pointer
Leandro Ribeiro70863962020-09-09 13:12:35 -03002150 * @returns 0 on success, -1 on failure
Leandro Ribeiro4a5b67a2020-09-15 15:30:35 -03002151 *
2152 * Takes ownership of @c connector on success, not on failure.
Pekka Paalanenc112f002017-08-28 16:27:20 +03002153 */
Leandro Ribeiro70863962020-09-09 13:12:35 -03002154static int
2155drm_head_create(struct drm_backend *backend, drmModeConnector *conn,
Pekka Paalanenc112f002017-08-28 16:27:20 +03002156 struct udev_device *drm_device)
2157{
2158 struct drm_head *head;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002159 char *name;
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002160 int ret;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002161
2162 head = zalloc(sizeof *head);
2163 if (!head)
Leandro Ribeiro70863962020-09-09 13:12:35 -03002164 return -1;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002165
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002166 head->backend = backend;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002167
Leandro Ribeiro70863962020-09-09 13:12:35 -03002168 drm_connector_init(backend, &head->connector, conn->connector_id);
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002169
2170 name = make_connector_name(conn);
Pekka Paalanenc112f002017-08-28 16:27:20 +03002171 if (!name)
Leandro Ribeiro70863962020-09-09 13:12:35 -03002172 goto err;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002173
2174 weston_head_init(&head->base, name);
2175 free(name);
2176
Leandro Ribeiro67941642020-09-15 00:11:02 -03002177 ret = drm_head_update_info(head, conn);
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002178 if (ret < 0)
Leandro Ribeiro67941642020-09-15 00:11:02 -03002179 goto err_update;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002180
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002181 head->backlight = backlight_init(drm_device, conn->connector_type);
Pekka Paalanence724242017-09-04 12:21:24 +03002182
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002183 if (conn->connector_type == DRM_MODE_CONNECTOR_LVDS ||
2184 conn->connector_type == DRM_MODE_CONNECTOR_eDP)
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002185 weston_head_set_internal(&head->base);
Pekka Paalanenc112f002017-08-28 16:27:20 +03002186
Pekka Paalanen9c03a7c2017-11-28 14:30:10 +02002187 if (drm_head_read_current_setup(head, backend) < 0) {
Pekka Paalanen13d233e2017-09-11 14:06:11 +03002188 weston_log("Failed to retrieve current mode from connector %d.\n",
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002189 head->connector.connector_id);
Pekka Paalanen6fae2be2017-11-28 14:33:52 +02002190 /* Not fatal. */
Pekka Paalanen13d233e2017-09-11 14:06:11 +03002191 }
2192
Pekka Paalanenc112f002017-08-28 16:27:20 +03002193 weston_compositor_add_head(backend->compositor, &head->base);
Pekka Paalanen456dc732017-11-09 15:10:11 +02002194 drm_head_log_info(head, "found");
leng.fang32af9fc2024-06-13 11:22:15 +08002195 drm_append_helper_connector(head);
Pekka Paalanenc112f002017-08-28 16:27:20 +03002196
Leandro Ribeiro70863962020-09-09 13:12:35 -03002197 return 0;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002198
Leandro Ribeiro67941642020-09-15 00:11:02 -03002199err_update:
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002200 weston_head_release(&head->base);
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002201err:
Leandro Ribeiro70863962020-09-09 13:12:35 -03002202 drm_connector_fini(&head->connector);
Pekka Paalanenc112f002017-08-28 16:27:20 +03002203 free(head);
Leandro Ribeiro70863962020-09-09 13:12:35 -03002204 return -1;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002205}
2206
2207static void
2208drm_head_destroy(struct drm_head *head)
2209{
leng.fang32af9fc2024-06-13 11:22:15 +08002210 drm_delete_helper_connector(head);
Pekka Paalanenc112f002017-08-28 16:27:20 +03002211 weston_head_release(&head->base);
Pekka Paalanence724242017-09-04 12:21:24 +03002212
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002213 drm_connector_fini(&head->connector);
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002214
Pekka Paalanence724242017-09-04 12:21:24 +03002215 if (head->backlight)
2216 backlight_destroy(head->backlight);
2217
Pekka Paalanenc112f002017-08-28 16:27:20 +03002218 free(head);
2219}
2220
2221/**
Armin Krezović08368132016-09-30 14:11:05 +02002222 * Create a Weston output structure
2223 *
Pekka Paalanend2e62422017-09-08 15:48:07 +03002224 * Create an "empty" drm_output. This is the implementation of
2225 * weston_backend::create_output.
Armin Krezović08368132016-09-30 14:11:05 +02002226 *
Pekka Paalanend2e62422017-09-08 15:48:07 +03002227 * Creating an output is usually followed by drm_output_attach_head()
2228 * and drm_output_enable() to make use of it.
2229 *
2230 * @param compositor The compositor instance.
2231 * @param name Name for the new output.
2232 * @returns The output, or NULL on failure.
Armin Krezović08368132016-09-30 14:11:05 +02002233 */
Pekka Paalanend2e62422017-09-08 15:48:07 +03002234static struct weston_output *
2235drm_output_create(struct weston_compositor *compositor, const char *name)
Armin Krezović08368132016-09-30 14:11:05 +02002236{
Pekka Paalanend2e62422017-09-08 15:48:07 +03002237 struct drm_backend *b = to_drm_backend(compositor);
Armin Krezović08368132016-09-30 14:11:05 +02002238 struct drm_output *output;
Armin Krezović08368132016-09-30 14:11:05 +02002239
Armin Krezović08368132016-09-30 14:11:05 +02002240 output = zalloc(sizeof *output);
2241 if (output == NULL)
Pekka Paalanend2e62422017-09-08 15:48:07 +03002242 return NULL;
Armin Krezović08368132016-09-30 14:11:05 +02002243
Daniel Stone64dbbee2018-07-20 19:00:06 +01002244 output->backend = b;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002245 output->crtc = NULL;
2246
Stefan Agnerccf24072019-07-09 22:02:00 +02002247#ifdef BUILD_DRM_GBM
Tomohito Esaki718a40b2018-01-31 17:50:15 +09002248 output->gbm_bo_flags = GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING;
Stefan Agnerccf24072019-07-09 22:02:00 +02002249#endif
Daniel Stone64dbbee2018-07-20 19:00:06 +01002250
Pekka Paalanend2e62422017-09-08 15:48:07 +03002251 weston_output_init(&output->base, compositor, name);
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002252
Armin Krezović08368132016-09-30 14:11:05 +02002253 output->base.enable = drm_output_enable;
2254 output->base.destroy = drm_output_destroy;
2255 output->base.disable = drm_output_disable;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002256 output->base.attach_head = drm_output_attach_head;
Pekka Paalanen7f853792017-11-29 14:33:33 +02002257 output->base.detach_head = drm_output_detach_head;
Armin Krezović08368132016-09-30 14:11:05 +02002258
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01002259 output->destroy_pending = false;
2260 output->disable_pending = false;
Armin Krezović08368132016-09-30 14:11:05 +02002261
Pekka Paalanen01f60212017-03-24 15:39:24 +02002262 output->state_cur = drm_output_state_alloc(output, NULL);
Pekka Paalanena0bfedc2017-04-03 14:42:51 +03002263
leng.fang32af9fc2024-06-13 11:22:15 +08002264 drm_init_aml_output_config(compositor, output);
Armin Krezović08368132016-09-30 14:11:05 +02002265 weston_compositor_add_pending_output(&output->base, b->compositor);
2266
Pekka Paalanend2e62422017-09-08 15:48:07 +03002267 return &output->base;
Armin Krezović08368132016-09-30 14:11:05 +02002268}
2269
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002270/**
2271 * Create a Weston writeback for a writeback connector
Leandro Ribeiro72430222020-09-14 17:41:41 -03002272 *
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002273 * Given a DRM connector of type writeback, create a matching drm_writeback
2274 * structure and add it to Weston's writeback list.
2275 *
2276 * @param b Weston backend structure
2277 * @param conn DRM connector object of type writeback
2278 * @returns 0 on success, -1 on failure
2279 *
2280 * Takes ownership of @c connector on success, not on failure.
2281 */
2282static int
2283drm_writeback_create(struct drm_backend *b, drmModeConnector *conn)
2284{
2285 struct drm_writeback *writeback;
2286 int ret;
2287
2288 writeback = zalloc(sizeof *writeback);
2289 assert(writeback);
2290
2291 writeback->backend = b;
2292
2293 drm_connector_init(b, &writeback->connector, conn->connector_id);
2294
2295 ret = drm_writeback_update_info(writeback, conn);
2296 if (ret < 0)
2297 goto err;
2298
2299 wl_list_insert(&b->writeback_connector_list, &writeback->link);
2300 return 0;
2301
2302err:
2303 drm_connector_fini(&writeback->connector);
2304 free(writeback);
2305 return -1;
2306}
2307
2308static void
2309drm_writeback_destroy(struct drm_writeback *writeback)
2310{
2311 drm_connector_fini(&writeback->connector);
2312 wl_list_remove(&writeback->link);
2313
2314 free(writeback);
2315}
2316
2317/** Given the DRM connector object of a connector, create drm_head or
2318 * drm_writeback object (depending on the type of connector) for it.
2319 *
2320 * The object is then added to the DRM-backend list of heads or writebacks.
Leandro Ribeiro72430222020-09-14 17:41:41 -03002321 *
2322 * @param b The DRM-backend structure
2323 * @param conn The DRM connector object
2324 * @param drm_device udev device pointer
2325 * @return 0 on success, -1 on failure
2326 */
2327static int
2328drm_backend_add_connector(struct drm_backend *b, drmModeConnector *conn,
2329 struct udev_device *drm_device)
2330{
2331 int ret;
2332
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002333 if (conn->connector_type == DRM_MODE_CONNECTOR_WRITEBACK) {
2334 ret = drm_writeback_create(b, conn);
2335 if (ret < 0)
2336 weston_log("DRM: failed to create writeback for connector %d.\n",
2337 conn->connector_id);
2338 } else {
2339 ret = drm_head_create(b, conn, drm_device);
2340 if (ret < 0)
2341 weston_log("DRM: failed to create head for connector %d.\n",
2342 conn->connector_id);
2343 }
Leandro Ribeiro72430222020-09-14 17:41:41 -03002344
2345 return ret;
2346}
2347
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002348/** Find all connectors of the fd and create drm_head or drm_writeback objects
2349 * (depending on the type of connector they are) for each of them
2350 *
2351 * These objects are added to the DRM-backend lists of heads and writebacks.
2352 *
2353 * @param b The DRM-backend structure
2354 * @param drm_device udev device pointer
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03002355 * @param resources The DRM resources, it is taken with drmModeGetResources
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002356 * @return 0 on success, -1 on failure
2357 */
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002358static int
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03002359drm_backend_discover_connectors(struct drm_backend *b, struct udev_device *drm_device,
2360 drmModeRes *resources)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002361{
Leandro Ribeiro70863962020-09-09 13:12:35 -03002362 drmModeConnector *conn;
2363 int i, ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002364
Giulio Camuffo954f1832014-10-11 18:27:30 +03002365 b->min_width = resources->min_width;
2366 b->max_width = resources->max_width;
2367 b->min_height = resources->min_height;
2368 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05002369
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002370 for (i = 0; i < resources->count_connectors; i++) {
Pekka Paalanend2e62422017-09-08 15:48:07 +03002371 uint32_t connector_id = resources->connectors[i];
Daniel Stone02cf4662017-03-03 16:19:39 +00002372
Leandro Ribeiro70863962020-09-09 13:12:35 -03002373 conn = drmModeGetConnector(b->drm.fd, connector_id);
2374 if (!conn)
2375 continue;
2376
Leandro Ribeiro72430222020-09-14 17:41:41 -03002377 ret = drm_backend_add_connector(b, conn, drm_device);
2378 if (ret < 0)
Leandro Ribeiro70863962020-09-09 13:12:35 -03002379 drmModeFreeConnector(conn);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002380 }
2381
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002382 return 0;
2383}
2384
Leandro Ribeiro99611c82020-09-14 17:19:12 -03002385static bool
2386resources_has_connector(drmModeRes *resources, uint32_t connector_id)
2387{
2388 for (int i = 0; i < resources->count_connectors; i++) {
2389 if (resources->connectors[i] == connector_id)
2390 return true;
2391 }
2392
2393 return false;
2394}
2395
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002396static void
Leandro Ribeiro993920d2020-09-14 17:01:01 -03002397drm_backend_update_connectors(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002398{
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002399 drmModeRes *resources;
Leandro Ribeiro70863962020-09-09 13:12:35 -03002400 drmModeConnector *conn;
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002401 struct weston_head *base, *base_next;
Pekka Paalanena0a37462017-08-31 15:41:57 +03002402 struct drm_head *head;
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002403 struct drm_writeback *writeback, *writeback_next;
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002404 uint32_t connector_id;
Leandro Ribeiro70863962020-09-09 13:12:35 -03002405 int i, ret;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002406
Giulio Camuffo954f1832014-10-11 18:27:30 +03002407 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002408 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002409 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002410 return;
2411 }
2412
Pekka Paalanend2e62422017-09-08 15:48:07 +03002413 /* collect new connectors that have appeared, e.g. MST */
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002414 for (i = 0; i < resources->count_connectors; i++) {
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002415 connector_id = resources->connectors[i];
Benjamin Franzke117483d2011-08-30 11:38:26 +02002416
Leandro Ribeiro70863962020-09-09 13:12:35 -03002417 conn = drmModeGetConnector(b->drm.fd, connector_id);
2418 if (!conn)
2419 continue;
2420
Pekka Paalanend2e62422017-09-08 15:48:07 +03002421 head = drm_head_find_by_connector(b, connector_id);
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002422 writeback = drm_writeback_find_by_connector(b, connector_id);
2423
2424 /* Connector can't be owned by both a head and a writeback, so
2425 * one of the searches must fail. */
2426 assert(head == NULL || writeback == NULL);
2427
Leandro Ribeiro72430222020-09-14 17:41:41 -03002428 if (head)
Leandro Ribeiro70863962020-09-09 13:12:35 -03002429 ret = drm_head_update_info(head, conn);
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002430 else if (writeback)
2431 ret = drm_writeback_update_info(writeback, conn);
Leandro Ribeiro72430222020-09-14 17:41:41 -03002432 else
2433 ret = drm_backend_add_connector(b, conn, drm_device);
2434
2435 if (ret < 0)
2436 drmModeFreeConnector(conn);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002437 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002438
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002439 /* Destroy head objects of connectors (except writeback connectors) that
2440 * have disappeared. */
2441 wl_list_for_each_safe(base, base_next,
Pekka Paalanena0a37462017-08-31 15:41:57 +03002442 &b->compositor->head_list, compositor_link) {
Pekka Paalanena0a37462017-08-31 15:41:57 +03002443 head = to_drm_head(base);
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002444 connector_id = head->connector.connector_id;
Pekka Paalanena0a37462017-08-31 15:41:57 +03002445
Leandro Ribeiro99611c82020-09-14 17:19:12 -03002446 if (resources_has_connector(resources, connector_id))
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002447 continue;
2448
Pekka Paalanend2e62422017-09-08 15:48:07 +03002449 weston_log("DRM: head '%s' (connector %d) disappeared.\n",
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002450 head->base.name, connector_id);
Pekka Paalanend2e62422017-09-08 15:48:07 +03002451 drm_head_destroy(head);
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002452 }
2453
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002454 /* Destroy writeback objects of writeback connectors that have
2455 * disappeared. */
2456 wl_list_for_each_safe(writeback, writeback_next,
2457 &b->writeback_connector_list, link) {
2458 connector_id = writeback->connector.connector_id;
2459
2460 if (resources_has_connector(resources, connector_id))
2461 continue;
2462
2463 weston_log("DRM: writeback connector (connector %d) disappeared.\n",
2464 connector_id);
2465 drm_writeback_destroy(writeback);
2466 }
2467
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002468 drmModeFreeResources(resources);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002469}
2470
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302471static enum wdrm_connector_property
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002472drm_connector_find_property_by_id(struct drm_connector *connector,
2473 uint32_t property_id)
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302474{
2475 int i;
2476 enum wdrm_connector_property prop = WDRM_CONNECTOR__COUNT;
2477
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002478 if (!connector || !property_id)
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302479 return WDRM_CONNECTOR__COUNT;
2480
2481 for (i = 0; i < WDRM_CONNECTOR__COUNT; i++)
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002482 if (connector->props[i].prop_id == property_id) {
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302483 prop = (enum wdrm_connector_property) i;
2484 break;
2485 }
2486 return prop;
2487}
2488
2489static void
2490drm_backend_update_conn_props(struct drm_backend *b,
2491 uint32_t connector_id,
2492 uint32_t property_id)
2493{
2494 struct drm_head *head;
2495 enum wdrm_connector_property conn_prop;
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302496
2497 head = drm_head_find_by_connector(b, connector_id);
2498 if (!head) {
2499 weston_log("DRM: failed to find head for connector id: %d.\n",
2500 connector_id);
2501 return;
2502 }
2503
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002504 conn_prop = drm_connector_find_property_by_id(&head->connector, property_id);
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302505 if (conn_prop >= WDRM_CONNECTOR__COUNT)
2506 return;
2507
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03002508 if (drm_connector_update_properties(&head->connector) < 0)
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302509 return;
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03002510
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302511 if (conn_prop == WDRM_CONNECTOR_CONTENT_PROTECTION) {
2512 weston_head_set_content_protection_status(&head->base,
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03002513 drm_head_get_current_protection(head));
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302514 }
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302515}
2516
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002517static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002518udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002519{
David Herrmannd7488c22012-03-11 20:05:21 +01002520 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01002521 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01002522
2523 sysnum = udev_device_get_sysnum(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002524 if (!sysnum || atoi(sysnum) != b->drm.id)
David Herrmannd7488c22012-03-11 20:05:21 +01002525 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02002526
David Herrmann6ac52db2012-03-11 20:05:22 +01002527 val = udev_device_get_property_value(device, "HOTPLUG");
2528 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002529 return 0;
2530
David Herrmann6ac52db2012-03-11 20:05:22 +01002531 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002532}
2533
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002534static int
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302535udev_event_is_conn_prop_change(struct drm_backend *b,
2536 struct udev_device *device,
2537 uint32_t *connector_id,
2538 uint32_t *property_id)
2539
2540{
2541 const char *val;
2542 int id;
2543
2544 val = udev_device_get_property_value(device, "CONNECTOR");
2545 if (!val || !safe_strtoint(val, &id))
2546 return 0;
2547 else
2548 *connector_id = id;
2549
2550 val = udev_device_get_property_value(device, "PROPERTY");
2551 if (!val || !safe_strtoint(val, &id))
2552 return 0;
2553 else
2554 *property_id = id;
2555
2556 return 1;
2557}
2558
2559static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002560udev_drm_event(int fd, uint32_t mask, void *data)
2561{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002562 struct drm_backend *b = data;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002563 struct udev_device *event;
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302564 uint32_t conn_id, prop_id;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002565
Giulio Camuffo954f1832014-10-11 18:27:30 +03002566 event = udev_monitor_receive_device(b->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002567
leng.fang9cf09e22024-07-17 20:01:11 +08002568 if (udev_event_is_hotplug(b, event) && b->display_enable) {
leng.fang91856072024-06-07 14:12:54 +08002569#ifdef ENABLE_MODE_POLICY
leng.fang13fd3982024-07-03 19:09:58 +08002570 mode_policy_set_hotplug(AML_WESTON_HOTPLUG_PLUG | AML_WESTON_HOTPLUG_UNPLUG, true);
leng.fang91856072024-06-07 14:12:54 +08002571#endif
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302572 if (udev_event_is_conn_prop_change(b, event, &conn_id, &prop_id))
2573 drm_backend_update_conn_props(b, conn_id, prop_id);
2574 else
Leandro Ribeiro993920d2020-09-14 17:01:01 -03002575 drm_backend_update_connectors(b, event);
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302576 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002577
leng.fang32af9fc2024-06-13 11:22:15 +08002578#ifdef BUILD_AML_TV
2579 drm_backend_afr_update(b, udev_event_frame_rate_hint(event),
2580 udev_event_is_vdin_event(event));
2581#endif
2582
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002583 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002584
2585 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002586}
2587
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002588static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002589drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002590{
Armin Krezović545dba62016-08-05 15:54:18 +02002591 struct drm_backend *b = to_drm_backend(ec);
Pekka Paalanenc112f002017-08-28 16:27:20 +03002592 struct weston_head *base, *next;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002593 struct drm_crtc *crtc, *crtc_tmp;
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002594 struct drm_writeback *writeback, *writeback_tmp;
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002595
leng.fang32af9fc2024-06-13 11:22:15 +08002596#ifdef ENABLE_DRM_HELP
2597 stop_help_worker();
2598#endif
2599
Giulio Camuffo954f1832014-10-11 18:27:30 +03002600 udev_input_destroy(&b->input);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002601
Giulio Camuffo954f1832014-10-11 18:27:30 +03002602 wl_event_source_remove(b->udev_drm_source);
2603 wl_event_source_remove(b->drm_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002604
Daniel Stoneb57c6a02017-10-05 16:27:21 +01002605 b->shutting_down = true;
2606
Giulio Camuffo954f1832014-10-11 18:27:30 +03002607 destroy_sprites(b);
Kristian Høgsberg3d64a3e2013-05-10 12:36:04 -04002608
Leandro Ribeirof0149642019-12-18 15:52:18 -03002609 weston_log_scope_destroy(b->debug);
Daniel Stone1cbe1f92018-07-20 10:21:28 +01002610 b->debug = NULL;
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03002611 weston_compositor_shutdown(ec);
2612
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002613 wl_list_for_each_safe(crtc, crtc_tmp, &b->crtc_list, link)
2614 drm_crtc_destroy(crtc);
2615
Pekka Paalanenc112f002017-08-28 16:27:20 +03002616 wl_list_for_each_safe(base, next, &ec->head_list, compositor_link)
2617 drm_head_destroy(to_drm_head(base));
2618
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002619 wl_list_for_each_safe(writeback, writeback_tmp,
2620 &b->writeback_connector_list, link)
2621 drm_writeback_destroy(writeback);
2622
Stefan Agnerccf24072019-07-09 22:02:00 +02002623#ifdef BUILD_DRM_GBM
Giulio Camuffo954f1832014-10-11 18:27:30 +03002624 if (b->gbm)
2625 gbm_device_destroy(b->gbm);
Stefan Agnerccf24072019-07-09 22:02:00 +02002626#endif
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002627
Pekka Paalanen5b0aa552017-12-07 16:06:05 +02002628 udev_monitor_unref(b->udev_monitor);
Pekka Paalanen2a0c6c32017-09-13 16:48:01 +03002629 udev_unref(b->udev);
2630
Marius Vlad7a44ee72021-06-23 16:02:35 +03002631 weston_launcher_close(ec->launcher, b->drm.fd);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002632 weston_launcher_destroy(ec->launcher);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002633
Pekka Paalanen9bf4f372017-12-07 16:05:29 +02002634 free(b->drm.filename);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002635 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002636}
2637
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002638static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002639session_notify(struct wl_listener *listener, void *data)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002640{
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002641 struct weston_compositor *compositor = data;
Armin Krezović545dba62016-08-05 15:54:18 +02002642 struct drm_backend *b = to_drm_backend(compositor);
Daniel Stone085d2b92015-05-21 00:00:57 +01002643 struct drm_plane *plane;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002644 struct drm_output *output;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002645 struct drm_crtc *crtc;
leng.fange9d800e2024-07-12 13:31:59 +08002646 struct weston_mode *wst_mode;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002647
Giulio Camuffo954f1832014-10-11 18:27:30 +03002648 if (compositor->session_active) {
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002649 weston_log("activating session\n");
leng.fange9d800e2024-07-12 13:31:59 +08002650
2651 mode_policy_resume();
2652 wst_mode = mode_policy_choose_mode(NULL);
2653 wl_list_for_each(output, &compositor->output_list, base.link)
2654 weston_output_mode_set_native(&output->base,
2655 wst_mode, output->base.current_scale);
2656
Daniel Stonef33e1042016-11-05 08:10:13 +00002657 weston_compositor_wake(compositor);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002658 weston_compositor_damage_all(compositor);
Daniel Stone6020f472018-02-05 15:46:20 +00002659 b->state_invalid = true;
leng.fang32af9fc2024-06-13 11:22:15 +08002660 b->allow_modeset = true;
leng.fang9cf09e22024-07-17 20:01:11 +08002661 b->display_enable = 1;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002662 udev_input_enable(&b->input);
leng.fange9d800e2024-07-12 13:31:59 +08002663 weston_compositor_resume_focus(compositor);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002664 } else {
2665 weston_log("deactivating session\n");
leng.fang32af9fc2024-06-13 11:22:15 +08002666 drm_change_to_dummy_mode(b->compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002667 udev_input_disable(&b->input);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04002668
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002669 weston_compositor_offscreen(compositor);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04002670
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002671 /* If we have a repaint scheduled (either from a
2672 * pending pageflip or the idle handler), make sure we
2673 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002674 * vt switched away. The OFFSCREEN state will prevent
Abdur Rehman4dca0e12017-01-01 19:46:35 +05002675 * further attempts at repainting. When we switch
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002676 * back, we schedule a repaint, which will process
2677 * pending frame callbacks. */
2678
Giulio Camuffo954f1832014-10-11 18:27:30 +03002679 wl_list_for_each(output, &compositor->output_list, base.link) {
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002680 crtc = output->crtc;
Daniel Stone09a97e22017-03-01 11:34:06 +00002681 output->base.repaint_needed = false;
Daniel Stone2ba17f42015-05-19 20:02:41 +01002682 if (output->cursor_plane)
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002683 drmModeSetCursor(b->drm.fd, crtc->crtc_id,
Daniel Stone2ba17f42015-05-19 20:02:41 +01002684 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002685 }
2686
Giulio Camuffo954f1832014-10-11 18:27:30 +03002687 output = container_of(compositor->output_list.next,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002688 struct drm_output, base.link);
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002689 crtc = output->crtc;
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002690
Daniel Stone085d2b92015-05-21 00:00:57 +01002691 wl_list_for_each(plane, &b->plane_list, link) {
2692 if (plane->type != WDRM_PLANE_TYPE_OVERLAY)
2693 continue;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002694 drmModeSetPlane(b->drm.fd, plane->plane_id, crtc->crtc_id,
2695 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
Daniel Stone085d2b92015-05-21 00:00:57 +01002696 }
2697 }
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002698}
2699
Robert Beckett8d23ab72019-06-13 16:55:44 +01002700
2701/**
2702 * Handle KMS GPU being added/removed
2703 *
2704 * If the device being added/removed is the KMS device, we activate/deactivate
2705 * the compositor session.
2706 *
2707 * @param compositor The compositor instance.
2708 * @param device The device being added/removed.
2709 * @param added Whether the device is being added (or removed)
2710 */
2711static void
2712drm_device_changed(struct weston_compositor *compositor,
2713 dev_t device, bool added)
2714{
2715 struct drm_backend *b = to_drm_backend(compositor);
2716
Robert Beckett49dc3202019-07-02 16:31:22 +01002717 if (b->drm.fd < 0 || b->drm.devnum != device ||
2718 compositor->session_active == added)
Robert Beckett8d23ab72019-06-13 16:55:44 +01002719 return;
2720
2721 compositor->session_active = added;
2722 wl_signal_emit(&compositor->session_signal, compositor);
2723}
2724
Daniel Stoneefa504f2016-12-19 16:48:20 +00002725/**
2726 * Determines whether or not a device is capable of modesetting. If successful,
2727 * sets b->drm.fd and b->drm.filename to the opened device.
2728 */
2729static bool
2730drm_device_is_kms(struct drm_backend *b, struct udev_device *device)
2731{
2732 const char *filename = udev_device_get_devnode(device);
2733 const char *sysnum = udev_device_get_sysnum(device);
Robert Beckett8d23ab72019-06-13 16:55:44 +01002734 dev_t devnum = udev_device_get_devnum(device);
Daniel Stoneefa504f2016-12-19 16:48:20 +00002735 drmModeRes *res;
Marius Vlad7d070ca2018-11-23 14:02:07 +02002736 int id = -1, fd;
Daniel Stoneefa504f2016-12-19 16:48:20 +00002737
2738 if (!filename)
2739 return false;
2740
2741 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
2742 if (fd < 0)
2743 return false;
2744
2745 res = drmModeGetResources(fd);
2746 if (!res)
2747 goto out_fd;
2748
2749 if (res->count_crtcs <= 0 || res->count_connectors <= 0 ||
2750 res->count_encoders <= 0)
2751 goto out_res;
2752
2753 if (sysnum)
2754 id = atoi(sysnum);
2755 if (!sysnum || id < 0) {
2756 weston_log("couldn't get sysnum for device %s\n", filename);
2757 goto out_res;
2758 }
2759
2760 /* We can be called successfully on multiple devices; if we have,
2761 * clean up old entries. */
2762 if (b->drm.fd >= 0)
2763 weston_launcher_close(b->compositor->launcher, b->drm.fd);
2764 free(b->drm.filename);
2765
2766 b->drm.fd = fd;
2767 b->drm.id = id;
2768 b->drm.filename = strdup(filename);
Robert Beckett8d23ab72019-06-13 16:55:44 +01002769 b->drm.devnum = devnum;
Daniel Stoneefa504f2016-12-19 16:48:20 +00002770
Sergi Granellceb59812017-03-28 12:44:04 +02002771 drmModeFreeResources(res);
2772
Daniel Stoneefa504f2016-12-19 16:48:20 +00002773 return true;
2774
2775out_res:
2776 drmModeFreeResources(res);
2777out_fd:
2778 weston_launcher_close(b->compositor->launcher, fd);
2779 return false;
2780}
2781
David Herrmann0af066f2012-10-29 19:21:16 +01002782/*
2783 * Find primary GPU
2784 * Some systems may have multiple DRM devices attached to a single seat. This
2785 * function loops over all devices and tries to find a PCI device with the
2786 * boot_vga sysfs attribute set to 1.
2787 * If no such device is found, the first DRM device reported by udev is used.
Daniel Stoneefa504f2016-12-19 16:48:20 +00002788 * Devices are also vetted to make sure they are are capable of modesetting,
2789 * rather than pure render nodes (GPU with no display), or pure
2790 * memory-allocation devices (VGEM).
David Herrmann0af066f2012-10-29 19:21:16 +01002791 */
2792static struct udev_device*
Giulio Camuffo954f1832014-10-11 18:27:30 +03002793find_primary_gpu(struct drm_backend *b, const char *seat)
David Herrmann0af066f2012-10-29 19:21:16 +01002794{
2795 struct udev_enumerate *e;
2796 struct udev_list_entry *entry;
2797 const char *path, *device_seat, *id;
2798 struct udev_device *device, *drm_device, *pci;
2799
Giulio Camuffo954f1832014-10-11 18:27:30 +03002800 e = udev_enumerate_new(b->udev);
David Herrmann0af066f2012-10-29 19:21:16 +01002801 udev_enumerate_add_match_subsystem(e, "drm");
2802 udev_enumerate_add_match_sysname(e, "card[0-9]*");
2803
2804 udev_enumerate_scan_devices(e);
2805 drm_device = NULL;
2806 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Daniel Stoneefa504f2016-12-19 16:48:20 +00002807 bool is_boot_vga = false;
2808
David Herrmann0af066f2012-10-29 19:21:16 +01002809 path = udev_list_entry_get_name(entry);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002810 device = udev_device_new_from_syspath(b->udev, path);
David Herrmann0af066f2012-10-29 19:21:16 +01002811 if (!device)
2812 continue;
2813 device_seat = udev_device_get_property_value(device, "ID_SEAT");
2814 if (!device_seat)
2815 device_seat = default_seat;
2816 if (strcmp(device_seat, seat)) {
2817 udev_device_unref(device);
2818 continue;
2819 }
2820
2821 pci = udev_device_get_parent_with_subsystem_devtype(device,
2822 "pci", NULL);
2823 if (pci) {
2824 id = udev_device_get_sysattr_value(pci, "boot_vga");
Daniel Stoneefa504f2016-12-19 16:48:20 +00002825 if (id && !strcmp(id, "1"))
2826 is_boot_vga = true;
David Herrmann0af066f2012-10-29 19:21:16 +01002827 }
2828
Daniel Stoneefa504f2016-12-19 16:48:20 +00002829 /* If we already have a modesetting-capable device, and this
2830 * device isn't our boot-VGA device, we aren't going to use
2831 * it. */
2832 if (!is_boot_vga && drm_device) {
David Herrmann0af066f2012-10-29 19:21:16 +01002833 udev_device_unref(device);
Daniel Stoneefa504f2016-12-19 16:48:20 +00002834 continue;
2835 }
2836
2837 /* Make sure this device is actually capable of modesetting;
2838 * if this call succeeds, b->drm.{fd,filename} will be set,
2839 * and any old values freed. */
2840 if (!drm_device_is_kms(b, device)) {
2841 udev_device_unref(device);
2842 continue;
2843 }
2844
2845 /* There can only be one boot_vga device, and we try to use it
2846 * at all costs. */
2847 if (is_boot_vga) {
2848 if (drm_device)
2849 udev_device_unref(drm_device);
2850 drm_device = device;
2851 break;
2852 }
2853
2854 /* Per the (!is_boot_vga && drm_device) test above, we only
2855 * trump existing saved devices with boot-VGA devices, so if
2856 * we end up here, this must be the first device we've seen. */
2857 assert(!drm_device);
2858 drm_device = device;
David Herrmann0af066f2012-10-29 19:21:16 +01002859 }
2860
Daniel Stoneefa504f2016-12-19 16:48:20 +00002861 /* If we're returning a device to use, we must have an open FD for
2862 * it. */
2863 assert(!!drm_device == (b->drm.fd >= 0));
2864
David Herrmann0af066f2012-10-29 19:21:16 +01002865 udev_enumerate_unref(e);
2866 return drm_device;
2867}
2868
Pekka Paalanenb45ed8b2017-03-28 18:04:27 +03002869static struct udev_device *
2870open_specific_drm_device(struct drm_backend *b, const char *name)
2871{
2872 struct udev_device *device;
2873
2874 device = udev_device_new_from_subsystem_sysname(b->udev, "drm", name);
2875 if (!device) {
2876 weston_log("ERROR: could not open DRM device '%s'\n", name);
2877 return NULL;
2878 }
2879
2880 if (!drm_device_is_kms(b, device)) {
2881 udev_device_unref(device);
2882 weston_log("ERROR: DRM device '%s' is not a KMS device.\n", name);
2883 return NULL;
2884 }
2885
2886 /* If we're returning a device to use, we must have an open FD for
2887 * it. */
2888 assert(b->drm.fd >= 0);
2889
2890 return device;
2891}
2892
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002893static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002894planes_binding(struct weston_keyboard *keyboard, const struct timespec *time,
2895 uint32_t key, void *data)
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002896{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002897 struct drm_backend *b = data;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002898
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002899 switch (key) {
2900 case KEY_C:
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01002901 b->cursors_are_broken ^= true;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002902 break;
2903 case KEY_V:
Daniel Stone87fab1c2019-06-17 11:13:20 +01002904 /* We don't support overlay-plane usage with legacy KMS. */
2905 if (b->atomic_modeset)
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01002906 b->sprites_are_broken ^= true;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002907 break;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002908 default:
2909 break;
2910 }
Ander Conselvan de Oliveira180f42a2012-11-21 15:11:37 +02002911}
2912
Kristian Høgsberg0eac34a2013-08-30 14:28:22 -07002913#ifdef BUILD_VAAPI_RECORDER
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002914static void
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002915recorder_destroy(struct drm_output *output)
2916{
2917 vaapi_recorder_destroy(output->recorder);
2918 output->recorder = NULL;
2919
Ankit Nautiyal93dde242019-07-08 11:46:42 +05302920 weston_output_disable_planes_decr(&output->base);
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002921
2922 wl_list_remove(&output->recorder_frame_listener.link);
2923 weston_log("[libva recorder] done\n");
2924}
2925
2926static void
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002927recorder_frame_notify(struct wl_listener *listener, void *data)
2928{
2929 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002930 struct drm_backend *b;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002931 int fd, ret;
2932
2933 output = container_of(listener, struct drm_output,
2934 recorder_frame_listener);
Armin Krezović545dba62016-08-05 15:54:18 +02002935 b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002936
2937 if (!output->recorder)
2938 return;
2939
Daniel Stonee2e80132018-01-16 15:37:33 +00002940 ret = drmPrimeHandleToFD(b->drm.fd,
Daniel Stone8eece0c2016-11-17 17:54:00 +00002941 output->scanout_plane->state_cur->fb->handles[0],
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002942 DRM_CLOEXEC, &fd);
2943 if (ret) {
2944 weston_log("[libva recorder] "
2945 "failed to create prime fd for front buffer\n");
2946 return;
2947 }
2948
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002949 ret = vaapi_recorder_frame(output->recorder, fd,
Daniel Stone8eece0c2016-11-17 17:54:00 +00002950 output->scanout_plane->state_cur->fb->strides[0]);
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002951 if (ret < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +02002952 weston_log("[libva recorder] aborted: %s\n", strerror(errno));
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002953 recorder_destroy(output);
2954 }
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002955}
2956
2957static void *
Giulio Camuffo954f1832014-10-11 18:27:30 +03002958create_recorder(struct drm_backend *b, int width, int height,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002959 const char *filename)
2960{
2961 int fd;
2962 drm_magic_t magic;
2963
Giulio Camuffo954f1832014-10-11 18:27:30 +03002964 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002965 if (fd < 0)
2966 return NULL;
2967
2968 drmGetMagic(fd, &magic);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002969 drmAuthMagic(b->drm.fd, magic);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002970
2971 return vaapi_recorder_create(fd, width, height, filename);
2972}
2973
2974static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002975recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time,
2976 uint32_t key, void *data)
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002977{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002978 struct drm_backend *b = data;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002979 struct drm_output *output;
2980 int width, height;
2981
Giulio Camuffo954f1832014-10-11 18:27:30 +03002982 output = container_of(b->compositor->output_list.next,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002983 struct drm_output, base.link);
2984
2985 if (!output->recorder) {
Stefan Agner4a18f302019-10-20 18:25:42 +02002986 if (output->gbm_format != DRM_FORMAT_XRGB8888) {
Ander Conselvan de Oliveira2ef1cd12014-05-06 16:49:06 +03002987 weston_log("failed to start vaapi recorder: "
2988 "output format not supported\n");
2989 return;
2990 }
2991
Hardeningff39efa2013-09-18 23:56:35 +02002992 width = output->base.current_mode->width;
2993 height = output->base.current_mode->height;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002994
2995 output->recorder =
Giulio Camuffo954f1832014-10-11 18:27:30 +03002996 create_recorder(b, width, height, "capture.h264");
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002997 if (!output->recorder) {
2998 weston_log("failed to create vaapi recorder\n");
2999 return;
3000 }
3001
Ankit Nautiyal93dde242019-07-08 11:46:42 +05303002 weston_output_disable_planes_incr(&output->base);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003003
3004 output->recorder_frame_listener.notify = recorder_frame_notify;
3005 wl_signal_add(&output->base.frame_signal,
3006 &output->recorder_frame_listener);
3007
3008 weston_output_schedule_repaint(&output->base);
3009
3010 weston_log("[libva recorder] initialized\n");
3011 } else {
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003012 recorder_destroy(output);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003013 }
3014}
3015#else
3016static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003017recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time,
3018 uint32_t key, void *data)
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003019{
3020 weston_log("Compiled without libva support\n");
3021}
3022#endif
3023
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003024
Armin Krezović08368132016-09-30 14:11:05 +02003025static const struct weston_drm_output_api api = {
3026 drm_output_set_mode,
3027 drm_output_set_gbm_format,
3028 drm_output_set_seat,
3029};
3030
Giulio Camuffo954f1832014-10-11 18:27:30 +03003031static struct drm_backend *
3032drm_backend_create(struct weston_compositor *compositor,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003033 struct weston_drm_backend_config *config)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003034{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003035 struct drm_backend *b;
David Herrmann0af066f2012-10-29 19:21:16 +01003036 struct udev_device *drm_device;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003037 struct wl_event_loop *loop;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003038 const char *seat_id = default_seat;
nerdopolisb16c4ac2018-06-29 08:17:46 -04003039 const char *session_seat;
Leandro Ribeiro54293022021-10-12 14:48:36 -03003040 struct weston_drm_format_array *scanout_formats;
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03003041 drmModeRes *res;
Armin Krezović08368132016-09-30 14:11:05 +02003042 int ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003043
nerdopolisb16c4ac2018-06-29 08:17:46 -04003044 session_seat = getenv("XDG_SEAT");
3045 if (session_seat)
3046 seat_id = session_seat;
3047
3048 if (config->seat_id)
3049 seat_id = config->seat_id;
3050
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003051 weston_log("initializing drm backend\n");
3052
Giulio Camuffo954f1832014-10-11 18:27:30 +03003053 b = zalloc(sizeof *b);
3054 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003055 return NULL;
Daniel Stone725c2c32012-06-22 14:04:36 +01003056
Daniel Stone6020f472018-02-05 15:46:20 +00003057 b->state_invalid = true;
leng.fang32af9fc2024-06-13 11:22:15 +08003058 b->allow_modeset = true;
Daniel Stoneefa504f2016-12-19 16:48:20 +00003059 b->drm.fd = -1;
leng.fang9cf09e22024-07-17 20:01:11 +08003060 b->display_enable = 1;
Daniel Stoneefa504f2016-12-19 16:48:20 +00003061
leng.fang32af9fc2024-06-13 11:22:15 +08003062#ifdef BUILD_AML_TV
3063 b->vdin_detect_fd = -1;
3064#endif
3065
Giulio Camuffo954f1832014-10-11 18:27:30 +03003066 b->compositor = compositor;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003067 b->use_pixman = config->use_pixman;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00003068 b->pageflip_timeout = config->pageflip_timeout;
Pekka Paalanendee412d2018-04-23 11:44:58 +02003069 b->use_pixman_shadow = config->use_pixman_shadow;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003070
Leandro Ribeiro172afc22019-12-26 16:23:43 -03003071 b->debug = weston_compositor_add_log_scope(compositor, "drm-backend",
3072 "Debug messages from DRM/KMS backend\n",
3073 NULL, NULL, NULL);
Daniel Stone1cbe1f92018-07-20 10:21:28 +01003074
Pekka Paalanen7da9a382017-08-30 11:29:49 +03003075 compositor->backend = &b->base;
3076
Stefan Agner0bfebeb2019-07-08 00:30:44 +02003077 if (parse_gbm_format(config->gbm_format, DRM_FORMAT_XRGB8888, &b->gbm_format) < 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003078 goto err_compositor;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003079
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003080 /* Check if we run drm-backend using weston-launch */
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003081 compositor->launcher = weston_launcher_connect(compositor, config->tty,
3082 seat_id, true);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003083 if (compositor->launcher == NULL) {
Pekka Paalanena453f4d2017-10-31 10:19:48 +02003084 weston_log("fatal: drm backend should be run using "
3085 "weston-launch binary, or your system should "
3086 "provide the logind D-Bus API.\n");
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003087 goto err_compositor;
3088 }
3089
Giulio Camuffo954f1832014-10-11 18:27:30 +03003090 b->udev = udev_new();
3091 if (b->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003092 weston_log("failed to initialize udev context\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003093 goto err_launcher;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003094 }
3095
Giulio Camuffo954f1832014-10-11 18:27:30 +03003096 b->session_listener.notify = session_notify;
3097 wl_signal_add(&compositor->session_signal, &b->session_listener);
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05003098
leng.fang32af9fc2024-06-13 11:22:15 +08003099 drm_config_fixed_UIsize(b, config);
3100
Pekka Paalanenb45ed8b2017-03-28 18:04:27 +03003101 if (config->specific_device)
3102 drm_device = open_specific_drm_device(b, config->specific_device);
3103 else
3104 drm_device = find_primary_gpu(b, seat_id);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04003105 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003106 weston_log("no drm device found\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003107 goto err_udev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003108 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003109
Daniel Stoneefa504f2016-12-19 16:48:20 +00003110 if (init_kms_caps(b) < 0) {
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003111 weston_log("failed to initialize kms\n");
3112 goto err_udev_dev;
3113 }
3114
Giulio Camuffo954f1832014-10-11 18:27:30 +03003115 if (b->use_pixman) {
3116 if (init_pixman(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003117 weston_log("failed to initialize pixman renderer\n");
3118 goto err_udev_dev;
3119 }
3120 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +03003121 if (init_egl(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003122 weston_log("failed to initialize egl\n");
3123 goto err_udev_dev;
3124 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003125 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05003126
Giulio Camuffo954f1832014-10-11 18:27:30 +03003127 b->base.destroy = drm_destroy;
Daniel Stoneeedf84c2017-02-10 18:06:04 +00003128 b->base.repaint_begin = drm_repaint_begin;
3129 b->base.repaint_flush = drm_repaint_flush;
3130 b->base.repaint_cancel = drm_repaint_cancel;
Pekka Paalanenc112f002017-08-28 16:27:20 +03003131 b->base.create_output = drm_output_create;
Robert Beckett8d23ab72019-06-13 16:55:44 +01003132 b->base.device_changed = drm_device_changed;
Marius Vlad81bada52019-11-11 00:27:17 +02003133 b->base.can_scanout_dmabuf = drm_can_scanout_dmabuf;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02003134
leng.fang32af9fc2024-06-13 11:22:15 +08003135 drm_add_aml_callback(b);
Bob Ham91880f12016-01-12 10:21:47 +00003136 weston_setup_vt_switch_bindings(compositor);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04003137
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03003138 res = drmModeGetResources(b->drm.fd);
3139 if (!res) {
3140 weston_log("Failed to get drmModeRes\n");
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03003141 goto err_udev_dev;
3142 }
3143
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03003144 wl_list_init(&b->crtc_list);
3145 if (drm_backend_create_crtc_list(b, res) == -1) {
3146 weston_log("Failed to create CRTC list for DRM-backend\n");
3147 goto err_create_crtc_list;
3148 }
3149
Daniel Stone085d2b92015-05-21 00:00:57 +01003150 wl_list_init(&b->plane_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003151 create_sprites(b);
Jesse Barnes58ef3792012-02-23 09:45:49 -05003152
Giulio Camuffo954f1832014-10-11 18:27:30 +03003153 if (udev_input_init(&b->input,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03003154 compositor, b->udev, seat_id,
3155 config->configure_device) < 0) {
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003156 weston_log("failed to create input devices\n");
3157 goto err_sprite;
3158 }
3159
leng.fang32af9fc2024-06-13 11:22:15 +08003160#ifdef ENABLE_DRM_HELP
3161 weston_log("drm start_help_worker \n");
3162 start_help_worker(b->drm.fd, b->atomic_modeset);
3163#endif
3164
leng.fang91856072024-06-07 14:12:54 +08003165#ifdef ENABLE_MODE_POLICY
leng.fang13fd3982024-07-03 19:09:58 +08003166 mode_policy_set_hotplug(AML_WESTON_HOTPLUG_BOOT, true);
leng.fang91856072024-06-07 14:12:54 +08003167#endif
3168
Leandro Ribeiro96bef052020-09-09 15:23:49 -03003169 wl_list_init(&b->writeback_connector_list);
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03003170 if (drm_backend_discover_connectors(b, drm_device, res) < 0) {
Pekka Paalanend2e62422017-09-08 15:48:07 +03003171 weston_log("Failed to create heads for %s\n", b->drm.filename);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003172 goto err_udev_input;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003173 }
3174
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03003175 drmModeFreeResources(res);
3176
Marius Vlade83e7502019-10-29 17:29:37 +02003177 /* 'compute' faked zpos values in case HW doesn't expose any */
3178 drm_backend_create_faked_zpos(b);
3179
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003180 /* A this point we have some idea of whether or not we have a working
3181 * cursor plane. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003182 if (!b->cursors_are_broken)
3183 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003184
Giulio Camuffo954f1832014-10-11 18:27:30 +03003185 loop = wl_display_get_event_loop(compositor->wl_display);
3186 b->drm_source =
3187 wl_event_loop_add_fd(loop, b->drm.fd,
3188 WL_EVENT_READABLE, on_drm_input, b);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003189
Giulio Camuffo954f1832014-10-11 18:27:30 +03003190 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
3191 if (b->udev_monitor == NULL) {
Abdur Rehman4dca0e12017-01-01 19:46:35 +05003192 weston_log("failed to initialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003193 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003194 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03003195 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003196 "drm", NULL);
leng.fang32af9fc2024-06-13 11:22:15 +08003197#ifdef BUILD_AML_TV
3198 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
3199 "vdin", NULL);
3200 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
3201 "framerate_adapter", NULL);
3202#endif
3203
Giulio Camuffo954f1832014-10-11 18:27:30 +03003204 b->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02003205 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03003206 udev_monitor_get_fd(b->udev_monitor),
3207 WL_EVENT_READABLE, udev_drm_event, b);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003208
Giulio Camuffo954f1832014-10-11 18:27:30 +03003209 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003210 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003211 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003212 }
3213
Daniel Stonea96b93c2012-06-22 14:04:37 +01003214 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003215
Giulio Camuffo954f1832014-10-11 18:27:30 +03003216 weston_compositor_add_debug_binding(compositor, KEY_O,
3217 planes_binding, b);
3218 weston_compositor_add_debug_binding(compositor, KEY_C,
3219 planes_binding, b);
3220 weston_compositor_add_debug_binding(compositor, KEY_V,
3221 planes_binding, b);
3222 weston_compositor_add_debug_binding(compositor, KEY_Q,
3223 recorder_binding, b);
3224 weston_compositor_add_debug_binding(compositor, KEY_W,
3225 renderer_switch_binding, b);
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003226
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003227 if (compositor->renderer->import_dmabuf) {
3228 if (linux_dmabuf_setup(compositor) < 0)
3229 weston_log("Error: initializing dmabuf "
3230 "support failed.\n");
Leandro Ribeiro54293022021-10-12 14:48:36 -03003231 if (compositor->default_dmabuf_feedback) {
3232 /* We were able to create the compositor's default
3233 * dma-buf feedback in the renderer, that means that the
3234 * table was already created and populated with
3235 * renderer's format/modifier pairs. So now we must
3236 * compute the scanout formats indices in the table */
3237 scanout_formats = get_scanout_formats(b);
3238 if (!scanout_formats)
3239 goto err_udev_monitor;
3240 ret = weston_dmabuf_feedback_format_table_set_scanout_indices(compositor->dmabuf_feedback_format_table,
3241 scanout_formats);
3242 weston_drm_format_array_fini(scanout_formats);
Daniel Stonef5086032021-12-06 16:52:19 +00003243 free(scanout_formats);
Leandro Ribeiro54293022021-10-12 14:48:36 -03003244 if (ret < 0)
3245 goto err_udev_monitor;
3246 }
Marius Vladebd10e52019-11-16 19:22:48 +02003247 if (weston_direct_display_setup(compositor) < 0)
3248 weston_log("Error: initializing direct-display "
3249 "support failed.\n");
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003250 }
3251
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003252 if (compositor->capabilities & WESTON_CAP_EXPLICIT_SYNC) {
3253 if (linux_explicit_synchronization_setup(compositor) < 0)
3254 weston_log("Error: initializing explicit "
3255 " synchronization support failed.\n");
3256 }
3257
Ankit Nautiyala344fe32019-05-14 18:36:08 +05303258 if (b->atomic_modeset)
3259 if (weston_compositor_enable_content_protection(compositor) < 0)
3260 weston_log("Error: initializing content-protection "
3261 "support failed.\n");
3262
Armin Krezović08368132016-09-30 14:11:05 +02003263 ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME,
3264 &api, sizeof(api));
3265
3266 if (ret < 0) {
3267 weston_log("Failed to register output API.\n");
3268 goto err_udev_monitor;
3269 }
3270
Stefan Agner3654c672019-07-09 00:50:30 +02003271 ret = drm_backend_init_virtual_output_api(compositor);
Tomohito Esakib1fb00d2018-01-31 17:50:48 +09003272 if (ret < 0) {
3273 weston_log("Failed to register virtual output API.\n");
3274 goto err_udev_monitor;
3275 }
leng.fang91856072024-06-07 14:12:54 +08003276
leng.fang32af9fc2024-06-13 11:22:15 +08003277#ifdef ENABLE_DRM_HELP
3278 help_set_printinfo_function(compositor, weston_print_info);
3279#endif
Tomohito Esakib1fb00d2018-01-31 17:50:48 +09003280
leng.fang91856072024-06-07 14:12:54 +08003281#ifdef ENABLE_MODE_POLICY
3282 weston_start_update_env_thread();
3283#endif
3284
Giulio Camuffo954f1832014-10-11 18:27:30 +03003285 return b;
Daniel Stonea96b93c2012-06-22 14:04:37 +01003286
3287err_udev_monitor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003288 wl_event_source_remove(b->udev_drm_source);
3289 udev_monitor_unref(b->udev_monitor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003290err_drm_source:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003291 wl_event_source_remove(b->drm_source);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003292err_udev_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003293 udev_input_destroy(&b->input);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003294err_sprite:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003295 destroy_sprites(b);
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03003296err_create_crtc_list:
3297 drmModeFreeResources(res);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003298err_udev_dev:
3299 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003300err_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003301 udev_unref(b->udev);
Marius Vlad5130a8c2020-07-30 14:47:32 +03003302err_launcher:
3303 weston_launcher_destroy(compositor->launcher);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003304err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003305 weston_compositor_shutdown(compositor);
Marius Vladd171c7b2021-04-01 00:12:00 +03003306#ifdef BUILD_DRM_GBM
3307 if (b->gbm)
3308 gbm_device_destroy(b->gbm);
3309#endif
Giulio Camuffo954f1832014-10-11 18:27:30 +03003310 free(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003311 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003312}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003313
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003314static void
3315config_init_to_defaults(struct weston_drm_backend_config *config)
3316{
Pekka Paalanendee412d2018-04-23 11:44:58 +02003317 config->use_pixman_shadow = true;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003318}
3319
Giulio Camuffo954f1832014-10-11 18:27:30 +03003320WL_EXPORT int
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01003321weston_backend_init(struct weston_compositor *compositor,
3322 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003323{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003324 struct drm_backend *b;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003325 struct weston_drm_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003326
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003327 if (config_base == NULL ||
3328 config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION ||
3329 config_base->struct_size > sizeof(struct weston_drm_backend_config)) {
3330 weston_log("drm backend config structure is invalid\n");
3331 return -1;
3332 }
Benjamin Franzke117483d2011-08-30 11:38:26 +02003333
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003334 config_init_to_defaults(&config);
3335 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003336
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003337 b = drm_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003338 if (b == NULL)
3339 return -1;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003340
Giulio Camuffo954f1832014-10-11 18:27:30 +03003341 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003342}