blob: e84621d145c7d2debcd8c01d417400eeaa858555 [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;
limin.tianbf71b4c2024-08-27 09:09:34 +0000288 drm_send_video_display_time(output->state_cur, sec, usec);
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000289
290 if (output->destroy_pending) {
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +0100291 output->destroy_pending = false;
292 output->disable_pending = false;
293 output->dpms_off_pending = false;
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000294 drm_output_destroy(&output->base);
295 return;
296 } else if (output->disable_pending) {
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +0100297 output->disable_pending = false;
298 output->dpms_off_pending = false;
Daniel Stonea08512f2016-11-08 17:46:10 +0000299 weston_output_disable(&output->base);
300 return;
301 } else if (output->dpms_off_pending) {
302 struct drm_pending_state *pending = drm_pending_state_alloc(b);
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +0100303 output->dpms_off_pending = false;
Daniel Stonea08512f2016-11-08 17:46:10 +0000304 drm_output_get_disable_state(pending, output);
305 drm_pending_state_apply_sync(pending);
Michael Olbrichd70e7122020-08-06 09:57:54 +0200306 }
307 if (output->state_cur->dpms == WESTON_DPMS_OFF &&
308 output->base.repaint_status != REPAINT_AWAITING_COMPLETION) {
Daniel Stonea08512f2016-11-08 17:46:10 +0000309 /* DPMS can happen to us either in the middle of a repaint
310 * cycle (when we have painted fresh content, only to throw it
311 * away for DPMS off), or at any other random point. If the
312 * latter is true, then we cannot go through finish_frame,
313 * because the repaint machinery does not expect this. */
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000314 return;
315 }
316
317 ts.tv_sec = sec;
318 ts.tv_nsec = usec * 1000;
Derek Foreman74bdb352021-07-29 09:02:04 -0500319
320 if (output->state_cur->dpms != WESTON_DPMS_OFF)
321 weston_output_finish_frame(&output->base, &ts, flags);
322 else
323 weston_output_finish_frame(&output->base, NULL,
324 WP_PRESENTATION_FEEDBACK_INVALID);
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000325
326 /* We can't call this from frame_notify, because the output's
327 * repaint needed flag is cleared just after that */
328 if (output->recorder)
329 weston_output_schedule_repaint(&output->base);
330}
331
Daniel Stone95d48a22017-04-04 17:54:30 +0100332static struct drm_fb *
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000333drm_output_render_pixman(struct drm_output_state *state,
334 pixman_region32_t *damage)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200335{
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000336 struct drm_output *output = state->output;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200337 struct weston_compositor *ec = output->base.compositor;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200338
339 output->current_image ^= 1;
340
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200341 pixman_renderer_output_set_buffer(&output->base,
342 output->image[output->current_image]);
Pekka Paalanenacf50c32018-04-23 11:44:56 +0200343 pixman_renderer_output_set_hw_extra_damage(&output->base,
344 &output->previous_damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200345
Pekka Paalanenacf50c32018-04-23 11:44:56 +0200346 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200347
Pekka Paalanenacf50c32018-04-23 11:44:56 +0200348 pixman_region32_copy(&output->previous_damage, damage);
Daniel Stone95d48a22017-04-04 17:54:30 +0100349
350 return drm_fb_ref(output->dumb[output->current_image]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200351}
352
Stefan Agner3654c672019-07-09 00:50:30 +0200353void
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000354drm_output_render(struct drm_output_state *state, pixman_region32_t *damage)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200355{
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000356 struct drm_output *output = state->output;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300357 struct weston_compositor *c = output->base.compositor;
Daniel Stonee2e80132018-01-16 15:37:33 +0000358 struct drm_plane_state *scanout_state;
Daniel Stonee95169b2016-11-14 17:46:59 +0000359 struct drm_plane *scanout_plane = output->scanout_plane;
Scott Anderson15c603c2020-06-02 17:39:43 +1200360 struct drm_property_info *damage_info =
361 &scanout_plane->props[WDRM_PLANE_FB_DAMAGE_CLIPS];
Armin Krezović545dba62016-08-05 15:54:18 +0200362 struct drm_backend *b = to_drm_backend(c);
Daniel Stone95d48a22017-04-04 17:54:30 +0100363 struct drm_fb *fb;
Scott Anderson15c603c2020-06-02 17:39:43 +1200364 pixman_region32_t scanout_damage;
365 pixman_box32_t *rects;
366 int n_rects;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200367
Daniel Stone4e84f7d2017-04-04 17:54:29 +0100368 /* If we already have a client buffer promoted to scanout, then we don't
369 * want to render. */
Leandro Ribeiro05cecc82020-08-13 17:34:58 -0300370 scanout_state = drm_output_state_get_plane(state, scanout_plane);
Daniel Stonee2e80132018-01-16 15:37:33 +0000371 if (scanout_state->fb)
Daniel Stone4e84f7d2017-04-04 17:54:29 +0100372 return;
373
Daniel Stone98d75e12020-03-06 11:03:14 +0000374 /*
375 * If we don't have any damage on the primary plane, and we already
376 * have a renderer buffer active, we can reuse it; else we pass
377 * the damaged region into the renderer to re-render the affected
Pekka Paalanen7f1a1132020-12-07 14:59:44 +0200378 * area. But, we still have to call the renderer anyway if any screen
379 * capture is pending, otherwise the capture will not complete.
Daniel Stone98d75e12020-03-06 11:03:14 +0000380 */
Daniel Stonee95169b2016-11-14 17:46:59 +0000381 if (!pixman_region32_not_empty(damage) &&
Pekka Paalanen7f1a1132020-12-07 14:59:44 +0200382 wl_list_empty(&output->base.frame_signal.listener_list) &&
Daniel Stonee95169b2016-11-14 17:46:59 +0000383 scanout_plane->state_cur->fb &&
384 (scanout_plane->state_cur->fb->type == BUFFER_GBM_SURFACE ||
Daniel Stone98d75e12020-03-06 11:03:14 +0000385 scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB)) {
Daniel Stonee95169b2016-11-14 17:46:59 +0000386 fb = drm_fb_ref(scanout_plane->state_cur->fb);
387 } else if (b->use_pixman) {
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000388 fb = drm_output_render_pixman(state, damage);
Daniel Stonee95169b2016-11-14 17:46:59 +0000389 } else {
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000390 fb = drm_output_render_gl(state, damage);
Daniel Stonee95169b2016-11-14 17:46:59 +0000391 }
Daniel Stone95d48a22017-04-04 17:54:30 +0100392
Daniel Stonee2e80132018-01-16 15:37:33 +0000393 if (!fb) {
394 drm_plane_state_put_back(scanout_state);
Daniel Stone95d48a22017-04-04 17:54:30 +0100395 return;
Daniel Stonee2e80132018-01-16 15:37:33 +0000396 }
397
398 scanout_state->fb = fb;
399 scanout_state->output = output;
400
401 scanout_state->src_x = 0;
402 scanout_state->src_y = 0;
Daniel Stonef9a61622020-03-06 11:07:15 +0000403 scanout_state->src_w = fb->width << 16;
404 scanout_state->src_h = fb->height << 16;
Daniel Stonee2e80132018-01-16 15:37:33 +0000405
406 scanout_state->dest_x = 0;
407 scanout_state->dest_y = 0;
Daniel Stonef9a61622020-03-06 11:07:15 +0000408 scanout_state->dest_w = output->base.current_mode->width;
409 scanout_state->dest_h = output->base.current_mode->height;
Daniel Stonee2e80132018-01-16 15:37:33 +0000410
leng.fang32af9fc2024-06-13 11:22:15 +0800411 drm_output_render_aml_config(output, scanout_state);
412
Scott Anderson15c603c2020-06-02 17:39:43 +1200413 pixman_region32_subtract(&c->primary_plane.damage,
414 &c->primary_plane.damage, damage);
415
416 /* Don't bother calculating plane damage if the plane doesn't support it */
417 if (damage_info->prop_id == 0)
418 return;
419
420 pixman_region32_init(&scanout_damage);
421 pixman_region32_copy(&scanout_damage, damage);
422
Deepak Rawat46a1c722018-07-24 14:13:34 -0700423 if (output->base.zoom.active) {
Derek Foremandb7e85d2022-01-20 10:27:17 -0600424 pixman_region32_t clip;
425
Scott Anderson15c603c2020-06-02 17:39:43 +1200426 weston_matrix_transform_region(&scanout_damage,
Deepak Rawat46a1c722018-07-24 14:13:34 -0700427 &output->base.matrix,
Scott Anderson15c603c2020-06-02 17:39:43 +1200428 &scanout_damage);
Derek Foremandb7e85d2022-01-20 10:27:17 -0600429 pixman_region32_init_rect(&clip, 0, 0,
430 output->base.width,
431 output->base.height);
432 pixman_region32_intersect(&scanout_damage, &scanout_damage, &clip);
433 pixman_region32_fini(&clip);
Deepak Rawat46a1c722018-07-24 14:13:34 -0700434 } else {
Scott Anderson15c603c2020-06-02 17:39:43 +1200435 pixman_region32_translate(&scanout_damage,
Deepak Rawat46a1c722018-07-24 14:13:34 -0700436 -output->base.x, -output->base.y);
437 weston_transformed_region(output->base.width,
438 output->base.height,
439 output->base.transform,
440 output->base.current_scale,
Scott Anderson15c603c2020-06-02 17:39:43 +1200441 &scanout_damage,
442 &scanout_damage);
Deepak Rawat46a1c722018-07-24 14:13:34 -0700443 }
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200444
Scott Anderson15c603c2020-06-02 17:39:43 +1200445 assert(scanout_state->damage_blob_id == 0);
446
447 rects = pixman_region32_rectangles(&scanout_damage, &n_rects);
448
449 /*
450 * If this function fails, the blob id should still be 0.
451 * This tells the kernel there is no damage information, which means
452 * that it will consider the whole plane damaged. While this may
453 * affect efficiency, it should still produce correct results.
454 */
455 drmModeCreatePropertyBlob(b->drm.fd, rects,
456 sizeof(*rects) * n_rects,
457 &scanout_state->damage_blob_id);
458
459 pixman_region32_fini(&scanout_damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200460}
461
Daniel Stonea08512f2016-11-08 17:46:10 +0000462static int
463drm_output_repaint(struct weston_output *output_base,
464 pixman_region32_t *damage,
465 void *repaint_data)
466{
467 struct drm_pending_state *pending_state = repaint_data;
468 struct drm_output *output = to_drm_output(output_base);
Daniel Stonea08512f2016-11-08 17:46:10 +0000469 struct drm_output_state *state = NULL;
470 struct drm_plane_state *scanout_state;
471
Tomohito Esakib1fb00d2018-01-31 17:50:48 +0900472 assert(!output->virtual);
473
Daniel Stonea08512f2016-11-08 17:46:10 +0000474 if (output->disable_pending || output->destroy_pending)
475 goto err;
476
477 assert(!output->state_last);
478
479 /* If planes have been disabled in the core, we might not have
480 * hit assign_planes at all, so might not have valid output state
481 * here. */
482 state = drm_pending_state_get_output(pending_state, output);
483 if (!state)
484 state = drm_output_state_duplicate(output->state_cur,
485 pending_state,
486 DRM_OUTPUT_STATE_CLEAR_PLANES);
487 state->dpms = WESTON_DPMS_ON;
488
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530489 if (output_base->allow_protection)
490 state->protection = output_base->desired_protection;
491 else
492 state->protection = WESTON_HDCP_DISABLE;
493
Daniel Stonea08512f2016-11-08 17:46:10 +0000494 drm_output_render(state, damage);
495 scanout_state = drm_output_state_get_plane(state,
496 output->scanout_plane);
497 if (!scanout_state || !scanout_state->fb)
498 goto err;
499
Daniel Stonea08512f2016-11-08 17:46:10 +0000500 return 0;
501
502err:
503 drm_output_state_free(state);
David Herrmann1edf44c2013-10-22 17:11:26 +0200504 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400505}
506
Daniel Stone4c2fc702019-06-18 11:12:07 +0100507/* Determine the type of vblank synchronization to use for the output.
508 *
509 * The pipe parameter indicates which CRTC is in use. Knowing this, we
510 * can determine which vblank sequence type to use for it. Traditional
511 * cards had only two CRTCs, with CRTC 0 using no special flags, and
512 * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
513 * parameter indicates this.
514 *
515 * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
516 * 0-31. If this is non-zero it indicates we're dealing with a
517 * multi-gpu situation and we need to calculate the vblank sync
518 * using DRM_BLANK_HIGH_CRTC_MASK.
519 */
520static unsigned int
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300521drm_waitvblank_pipe(struct drm_crtc *crtc)
Daniel Stone4c2fc702019-06-18 11:12:07 +0100522{
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300523 if (crtc->pipe > 1)
524 return (crtc->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
Daniel Stone4c2fc702019-06-18 11:12:07 +0100525 DRM_VBLANK_HIGH_CRTC_MASK;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300526 else if (crtc->pipe > 0)
Daniel Stone4c2fc702019-06-18 11:12:07 +0100527 return DRM_VBLANK_SECONDARY;
528 else
529 return 0;
530}
531
Antonio Borneoc90fccc2019-06-30 15:51:10 +0200532static int
Jonas Ådahle5a12252013-04-05 23:07:11 +0200533drm_output_start_repaint_loop(struct weston_output *output_base)
534{
Armin Krezović545dba62016-08-05 15:54:18 +0200535 struct drm_output *output = to_drm_output(output_base);
Daniel Stone8747f952016-11-29 20:17:32 +0000536 struct drm_pending_state *pending_state;
Daniel Stonee2e80132018-01-16 15:37:33 +0000537 struct drm_plane *scanout_plane = output->scanout_plane;
Armin Krezović545dba62016-08-05 15:54:18 +0200538 struct drm_backend *backend =
539 to_drm_backend(output_base->compositor);
Mario Kleinerf507ec32015-06-21 21:25:14 +0200540 struct timespec ts, tnow;
541 struct timespec vbl2now;
542 int64_t refresh_nsec;
543 int ret;
544 drmVBlank vbl = {
545 .request.type = DRM_VBLANK_RELATIVE,
546 .request.sequence = 0,
547 .request.signal = 0,
548 };
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300549
Armin Krezović08368132016-09-30 14:11:05 +0200550 if (output->disable_pending || output->destroy_pending)
Antonio Borneoc90fccc2019-06-30 15:51:10 +0200551 return 0;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800552
Leandro Ribeiro05cecc82020-08-13 17:34:58 -0300553 if (!scanout_plane->state_cur->fb) {
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300554 /* We can't page flip if there's no mode set */
David Herrmann3c688c52013-10-22 17:11:25 +0200555 goto finish_frame;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300556 }
557
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +0300558 /* Need to smash all state in from scratch; current timings might not
559 * be what we want, page flip might not work, etc.
560 */
Daniel Stone6020f472018-02-05 15:46:20 +0000561 if (backend->state_invalid)
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +0300562 goto finish_frame;
563
Daniel Stonee2e80132018-01-16 15:37:33 +0000564 assert(scanout_plane->state_cur->output == output);
565
Mario Kleinerf507ec32015-06-21 21:25:14 +0200566 /* Try to get current msc and timestamp via instant query */
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300567 vbl.request.type |= drm_waitvblank_pipe(output->crtc);
Mario Kleinerf507ec32015-06-21 21:25:14 +0200568 ret = drmWaitVBlank(backend->drm.fd, &vbl);
569
570 /* Error ret or zero timestamp means failure to get valid timestamp */
571 if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
572 ts.tv_sec = vbl.reply.tval_sec;
573 ts.tv_nsec = vbl.reply.tval_usec * 1000;
574
575 /* Valid timestamp for most recent vblank - not stale?
576 * Stale ts could happen on Linux 3.17+, so make sure it
577 * is not older than 1 refresh duration since now.
578 */
579 weston_compositor_read_presentation_clock(backend->compositor,
580 &tnow);
581 timespec_sub(&vbl2now, &tnow, &ts);
582 refresh_nsec =
583 millihz_to_nsec(output->base.current_mode->refresh);
584 if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
585 drm_output_update_msc(output, vbl.reply.sequence);
586 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200587 WP_PRESENTATION_FEEDBACK_INVALID);
Antonio Borneoc90fccc2019-06-30 15:51:10 +0200588 return 0;
Mario Kleinerf507ec32015-06-21 21:25:14 +0200589 }
590 }
591
592 /* Immediate query didn't provide valid timestamp.
593 * Use pageflip fallback.
594 */
Jonas Ådahle5a12252013-04-05 23:07:11 +0200595
Daniel Stone205c0a02017-04-04 17:54:33 +0100596 assert(!output->page_flip_pending);
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000597 assert(!output->state_last);
598
599 pending_state = drm_pending_state_alloc(backend);
Daniel Stone8747f952016-11-29 20:17:32 +0000600 drm_output_state_duplicate(output->state_cur, pending_state,
601 DRM_OUTPUT_STATE_PRESERVE_PLANES);
Daniel Stone205c0a02017-04-04 17:54:33 +0100602
Daniel Stone8747f952016-11-29 20:17:32 +0000603 ret = drm_pending_state_apply(pending_state);
604 if (ret != 0) {
Antonio Borneo39578632019-04-26 23:57:31 +0200605 weston_log("applying repaint-start state failed: %s\n",
606 strerror(errno));
Antonio Borneoc90fccc2019-06-30 15:51:10 +0200607 if (ret == -EACCES)
608 return -1;
David Herrmann3c688c52013-10-22 17:11:25 +0200609 goto finish_frame;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200610 }
David Herrmann3c688c52013-10-22 17:11:25 +0200611
Antonio Borneoc90fccc2019-06-30 15:51:10 +0200612 return 0;
David Herrmann3c688c52013-10-22 17:11:25 +0200613
614finish_frame:
615 /* if we cannot page-flip, immediately finish frame */
Daniel Stone3615ce12017-03-01 11:34:05 +0000616 weston_output_finish_frame(output_base, NULL,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200617 WP_PRESENTATION_FEEDBACK_INVALID);
Antonio Borneoc90fccc2019-06-30 15:51:10 +0200618 return 0;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200619}
620
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000621/**
622 * Begin a new repaint cycle
623 *
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000624 * Called by the core compositor at the beginning of a repaint cycle. Creates
625 * a new pending_state structure to own any output state created by individual
626 * output repaint functions until the repaint is flushed or cancelled.
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000627 */
628static void *
629drm_repaint_begin(struct weston_compositor *compositor)
630{
631 struct drm_backend *b = to_drm_backend(compositor);
632 struct drm_pending_state *ret;
633
634 ret = drm_pending_state_alloc(b);
635 b->repaint_data = ret;
636
Marius Vlad7e4db952019-04-17 13:47:06 +0300637 if (weston_log_scope_is_enabled(b->debug)) {
Daniel Stone1cbe1f92018-07-20 10:21:28 +0100638 char *dbg = weston_compositor_print_scene_graph(compositor);
639 drm_debug(b, "[repaint] Beginning repaint; pending_state %p\n",
640 ret);
641 drm_debug(b, "%s", dbg);
642 free(dbg);
643 }
644
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000645 return ret;
646}
647
648/**
649 * Flush a repaint set
650 *
651 * Called by the core compositor when a repaint cycle has been completed
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000652 * and should be flushed. Frees the pending state, transitioning ownership
653 * of the output state from the pending state, to the update itself. When
654 * the update completes (see drm_output_update_complete), the output
655 * state will be freed.
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000656 */
Antonio Borneoc90fccc2019-06-30 15:51:10 +0200657static int
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000658drm_repaint_flush(struct weston_compositor *compositor, void *repaint_data)
659{
660 struct drm_backend *b = to_drm_backend(compositor);
661 struct drm_pending_state *pending_state = repaint_data;
Antonio Borneoc90fccc2019-06-30 15:51:10 +0200662 int ret;
Daniel Stone6020f472018-02-05 15:46:20 +0000663
Antonio Borneoc90fccc2019-06-30 15:51:10 +0200664 ret = drm_pending_state_apply(pending_state);
665 if (ret != 0)
666 weston_log("repaint-flush failed: %s\n", strerror(errno));
667
Daniel Stone1cbe1f92018-07-20 10:21:28 +0100668 drm_debug(b, "[repaint] flushed pending_state %p\n", pending_state);
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000669 b->repaint_data = NULL;
leng.fang32af9fc2024-06-13 11:22:15 +0800670#ifdef ENABLE_DRM_HELP
671 help_do_repaint_cycle_completed();
672#endif
Antonio Borneoc90fccc2019-06-30 15:51:10 +0200673
674 return (ret == -EACCES) ? -1 : 0;
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000675}
676
677/**
678 * Cancel a repaint set
679 *
680 * Called by the core compositor when a repaint has finished, so the data
681 * held across the repaint cycle should be discarded.
682 */
683static void
684drm_repaint_cancel(struct weston_compositor *compositor, void *repaint_data)
685{
686 struct drm_backend *b = to_drm_backend(compositor);
687 struct drm_pending_state *pending_state = repaint_data;
688
689 drm_pending_state_free(pending_state);
Daniel Stone1cbe1f92018-07-20 10:21:28 +0100690 drm_debug(b, "[repaint] cancel pending_state %p\n", pending_state);
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000691 b->repaint_data = NULL;
692}
693
Alex Wub7b8bda2012-04-17 17:20:48 +0800694static int
Giulio Camuffo954f1832014-10-11 18:27:30 +0300695drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +0000696static void
697drm_output_fini_pixman(struct drm_output *output);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -0200698
699static int
Alex Wub7b8bda2012-04-17 17:20:48 +0800700drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
701{
Daniel Stone02d487a2017-10-07 14:01:45 +0100702 struct drm_output *output = to_drm_output(output_base);
703 struct drm_backend *b = to_drm_backend(output_base->compositor);
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100704 struct drm_mode *drm_mode = drm_output_choose_mode(output, mode);
Alex Wub7b8bda2012-04-17 17:20:48 +0800705
706 if (!drm_mode) {
Daniel Stone02d487a2017-10-07 14:01:45 +0100707 weston_log("%s: invalid resolution %dx%d\n",
708 output_base->name, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +0800709 return -1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -0200710 }
711
limin.tianc616dad2024-07-15 11:35:38 +0000712 if (&drm_mode->base == output->base.current_mode) {
713 weston_log("\n drm_output_switch_mode same mode return \n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800714 return 0;
limin.tianc616dad2024-07-15 11:35:38 +0000715 }
Hardeningff39efa2013-09-18 23:56:35 +0200716 output->base.current_mode->flags = 0;
Alex Wub7b8bda2012-04-17 17:20:48 +0800717
leng.fang32af9fc2024-06-13 11:22:15 +0800718 drm_switch_mode_aml_config_ui_size(output, output_base, drm_mode);
719
Hardeningff39efa2013-09-18 23:56:35 +0200720 output->base.current_mode = &drm_mode->base;
721 output->base.current_mode->flags =
Alex Wub7b8bda2012-04-17 17:20:48 +0800722 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
723
leng.fang32af9fc2024-06-13 11:22:15 +0800724 drm_switch_mode_aml_config_help(output, output_base, drm_mode);
725
Daniel Stonef30a18c2017-04-04 17:54:31 +0100726 /* XXX: This drops our current buffer too early, before we've started
727 * displaying it. Ideally this should be much more atomic and
728 * integrated with a full repaint cycle, rather than doing a
729 * sledgehammer modeswitch first, and only later showing new
730 * content.
731 */
Daniel Stone6020f472018-02-05 15:46:20 +0000732 b->state_invalid = true;
Alex Wub7b8bda2012-04-17 17:20:48 +0800733
leng.fang32af9fc2024-06-13 11:22:15 +0800734 b->allow_modeset = true;
735#ifdef MESON_DRM_FIX_UI_SIZE
736 // fixed ui mode needn't reset egl.
737 return 0;
738#endif
739
Giulio Camuffo954f1832014-10-11 18:27:30 +0300740 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200741 drm_output_fini_pixman(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300742 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200743 weston_log("failed to init output pixman state with "
744 "new mode\n");
745 return -1;
746 }
747 } else {
Daniel Stone3e661f72016-11-04 17:24:06 +0000748 drm_output_fini_egl(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300749 if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200750 weston_log("failed to init output egl state with "
751 "new mode");
752 return -1;
753 }
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -0200754 }
755
Alex Wub7b8bda2012-04-17 17:20:48 +0800756 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +0800757}
758
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200759static int
Giulio Camuffo954f1832014-10-11 18:27:30 +0300760init_pixman(struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200761{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300762 return pixman_renderer_init(b->compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200763}
764
Pekka Paalanen7b36b422014-06-04 14:00:53 +0300765/**
Pekka Paalanenec272712014-06-05 11:22:25 +0300766 * Create a drm_plane for a hardware plane
767 *
768 * Creates one drm_plane structure for a hardware plane, and initialises its
769 * properties and formats.
770 *
771 * This function does not add the plane to the list of usable planes in Weston
772 * itself; the caller is responsible for this.
773 *
774 * Call drm_plane_destroy to clean up the plane.
775 *
Daniel Stone2ba17f42015-05-19 20:02:41 +0100776 * @sa drm_output_find_special_plane
Pekka Paalanenec272712014-06-05 11:22:25 +0300777 * @param b DRM compositor backend
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -0300778 * @param kplane DRM plane to create
Pekka Paalanenec272712014-06-05 11:22:25 +0300779 */
780static struct drm_plane *
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -0300781drm_plane_create(struct drm_backend *b, const drmModePlane *kplane)
Pekka Paalanenec272712014-06-05 11:22:25 +0300782{
783 struct drm_plane *plane;
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100784 drmModeObjectProperties *props;
Marius Vladcdd6fa22019-08-29 20:42:00 +0300785 uint64_t *zpos_range_values;
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100786
Scott Anderson74663092021-02-01 15:46:33 -0300787 plane = zalloc(sizeof(*plane));
Pekka Paalanenec272712014-06-05 11:22:25 +0300788 if (!plane) {
789 weston_log("%s: out of memory\n", __func__);
790 return NULL;
791 }
792
793 plane->backend = b;
Daniel Stone57d609a2021-11-16 18:56:09 +0000794 plane->plane_idx = b->next_plane_idx++;
Daniel Stonebc15f682016-11-14 16:57:01 +0000795 plane->state_cur = drm_plane_state_alloc(NULL, plane);
796 plane->state_cur->complete = true;
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -0300797 plane->possible_crtcs = kplane->possible_crtcs;
798 plane->plane_id = kplane->plane_id;
Pekka Paalanenec272712014-06-05 11:22:25 +0300799
Scott Anderson74663092021-02-01 15:46:33 -0300800 weston_drm_format_array_init(&plane->formats);
801
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -0300802 props = drmModeObjectGetProperties(b->drm.fd, kplane->plane_id,
803 DRM_MODE_OBJECT_PLANE);
804 if (!props) {
805 weston_log("couldn't get plane properties\n");
806 goto err;
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100807 }
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -0300808
809 drm_property_info_populate(b, plane_props, plane->props,
810 WDRM_PLANE__COUNT, props);
811 plane->type =
812 drm_property_get_value(&plane->props[WDRM_PLANE_TYPE],
813 props,
814 WDRM_PLANE_TYPE__COUNT);
815
816 zpos_range_values =
817 drm_property_get_range_values(&plane->props[WDRM_PLANE_ZPOS],
818 props);
819
820 if (zpos_range_values) {
821 plane->zpos_min = zpos_range_values[0];
822 plane->zpos_max = zpos_range_values[1];
823 } else {
Marius Vladcdd6fa22019-08-29 20:42:00 +0300824 plane->zpos_min = DRM_PLANE_ZPOS_INVALID_PLANE;
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -0300825 plane->zpos_max = DRM_PLANE_ZPOS_INVALID_PLANE;
Daniel Stone2ba17f42015-05-19 20:02:41 +0100826 }
827
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -0300828 if (drm_plane_populate_formats(plane, kplane, props,
829 b->fb_modifiers) < 0) {
830 drmModeFreeObjectProperties(props);
831 goto err;
832 }
833
834 drmModeFreeObjectProperties(props);
835
Daniel Stone2ba17f42015-05-19 20:02:41 +0100836 if (plane->type == WDRM_PLANE_TYPE__COUNT)
837 goto err_props;
838
Pekka Paalanenec272712014-06-05 11:22:25 +0300839 weston_plane_init(&plane->base, b->compositor, 0, 0);
Daniel Stone085d2b92015-05-21 00:00:57 +0100840 wl_list_insert(&b->plane_list, &plane->link);
Pekka Paalanenec272712014-06-05 11:22:25 +0300841
842 return plane;
Daniel Stone2ba17f42015-05-19 20:02:41 +0100843
844err_props:
845 drm_property_info_free(plane->props, WDRM_PLANE__COUNT);
846err:
Scott Anderson74663092021-02-01 15:46:33 -0300847 weston_drm_format_array_fini(&plane->formats);
Daniel Stone2ba17f42015-05-19 20:02:41 +0100848 drm_plane_state_free(plane->state_cur, true);
849 free(plane);
850 return NULL;
851}
852
853/**
854 * Find, or create, a special-purpose plane
855 *
Daniel Stone2ba17f42015-05-19 20:02:41 +0100856 * @param b DRM backend
857 * @param output Output to use for plane
858 * @param type Type of plane
859 */
860static struct drm_plane *
861drm_output_find_special_plane(struct drm_backend *b, struct drm_output *output,
862 enum wdrm_plane_type type)
863{
864 struct drm_plane *plane;
865
Daniel Stone2ba17f42015-05-19 20:02:41 +0100866 wl_list_for_each(plane, &b->plane_list, link) {
867 struct drm_output *tmp;
868 bool found_elsewhere = false;
869
870 if (plane->type != type)
871 continue;
872 if (!drm_plane_is_available(plane, output))
873 continue;
874
875 /* On some platforms, primary/cursor planes can roam
876 * between different CRTCs, so make sure we don't claim the
877 * same plane for two outputs. */
Leandro Ribeiro05cecc82020-08-13 17:34:58 -0300878 wl_list_for_each(tmp, &b->compositor->output_list, base.link) {
Daniel Stonee2e80132018-01-16 15:37:33 +0000879 if (tmp->cursor_plane == plane ||
880 tmp->scanout_plane == plane) {
Daniel Stone2ba17f42015-05-19 20:02:41 +0100881 found_elsewhere = true;
882 break;
883 }
884 }
885
886 if (found_elsewhere)
887 continue;
888
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300889 plane->possible_crtcs = (1 << output->crtc->pipe);
Daniel Stone2ba17f42015-05-19 20:02:41 +0100890 return plane;
891 }
892
893 return NULL;
Pekka Paalanenec272712014-06-05 11:22:25 +0300894}
895
896/**
897 * Destroy one DRM plane
898 *
899 * Destroy a DRM plane, removing it from screen and releasing its retained
900 * buffers in the process. The counterpart to drm_plane_create.
901 *
902 * @param plane Plane to deallocate (will be freed)
903 */
904static void
905drm_plane_destroy(struct drm_plane *plane)
906{
Daniel Stone2ba17f42015-05-19 20:02:41 +0100907 if (plane->type == WDRM_PLANE_TYPE_OVERLAY)
908 drmModeSetPlane(plane->backend->drm.fd, plane->plane_id,
909 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
Daniel Stonebc15f682016-11-14 16:57:01 +0000910 drm_plane_state_free(plane->state_cur, true);
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100911 drm_property_info_free(plane->props, WDRM_PLANE__COUNT);
Pekka Paalanenec272712014-06-05 11:22:25 +0300912 weston_plane_release(&plane->base);
Scott Anderson74663092021-02-01 15:46:33 -0300913 weston_drm_format_array_fini(&plane->formats);
Pekka Paalanenec272712014-06-05 11:22:25 +0300914 wl_list_remove(&plane->link);
915 free(plane);
916}
917
918/**
919 * Initialise sprites (overlay planes)
920 *
921 * Walk the list of provided DRM planes, and add overlay planes.
922 *
923 * Call destroy_sprites to free these planes.
924 *
925 * @param b DRM compositor backend
926 */
927static void
928create_sprites(struct drm_backend *b)
929{
930 drmModePlaneRes *kplane_res;
931 drmModePlane *kplane;
932 struct drm_plane *drm_plane;
933 uint32_t i;
Pekka Paalanenec272712014-06-05 11:22:25 +0300934 kplane_res = drmModeGetPlaneResources(b->drm.fd);
935 if (!kplane_res) {
936 weston_log("failed to get plane resources: %s\n",
937 strerror(errno));
938 return;
939 }
leng.fang32af9fc2024-06-13 11:22:15 +0800940 for (i = kplane_res->count_planes; i >0; i--) {
941 kplane = drmModeGetPlane(b->drm.fd, kplane_res->planes[i-1]);
Pekka Paalanenec272712014-06-05 11:22:25 +0300942
Pekka Paalanenec272712014-06-05 11:22:25 +0300943 if (!kplane)
944 continue;
945
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -0300946 drm_plane = drm_plane_create(b, kplane);
Pekka Paalanenec272712014-06-05 11:22:25 +0300947 drmModeFreePlane(kplane);
948 if (!drm_plane)
949 continue;
950
Daniel Stone085d2b92015-05-21 00:00:57 +0100951 if (drm_plane->type == WDRM_PLANE_TYPE_OVERLAY)
952 weston_compositor_stack_plane(b->compositor,
953 &drm_plane->base,
954 &b->compositor->primary_plane);
Pekka Paalanenec272712014-06-05 11:22:25 +0300955 }
956
957 drmModeFreePlaneResources(kplane_res);
958}
959
960/**
961 * Clean up sprites (overlay planes)
962 *
963 * The counterpart to create_sprites.
964 *
965 * @param b DRM compositor backend
966 */
967static void
968destroy_sprites(struct drm_backend *b)
969{
970 struct drm_plane *plane, *next;
971
Daniel Stone085d2b92015-05-21 00:00:57 +0100972 wl_list_for_each_safe(plane, next, &b->plane_list, link)
Pekka Paalanenec272712014-06-05 11:22:25 +0300973 drm_plane_destroy(plane);
974}
975
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -0300976/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200977static uint32_t
Pekka Paalanence724242017-09-04 12:21:24 +0300978drm_get_backlight(struct drm_head *head)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200979{
980 long brightness, max_brightness, norm;
981
Pekka Paalanence724242017-09-04 12:21:24 +0300982 brightness = backlight_get_brightness(head->backlight);
983 max_brightness = backlight_get_max_brightness(head->backlight);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200984
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -0300985 /* convert it on a scale of 0 to 255 */
986 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200987
988 return (uint32_t) norm;
989}
990
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -0300991/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200992static void
993drm_set_backlight(struct weston_output *output_base, uint32_t value)
994{
Pekka Paalanenecc8cce2017-09-12 16:14:31 +0300995 struct drm_output *output = to_drm_output(output_base);
996 struct drm_head *head;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200997 long max_brightness, new_brightness;
998
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400999 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001000 return;
1001
Pekka Paalanenecc8cce2017-09-12 16:14:31 +03001002 wl_list_for_each(head, &output->base.head_list, base.output_link) {
1003 if (!head->backlight)
1004 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001005
Pekka Paalanenecc8cce2017-09-12 16:14:31 +03001006 max_brightness = backlight_get_max_brightness(head->backlight);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001007
Pekka Paalanenecc8cce2017-09-12 16:14:31 +03001008 /* get denormalized value */
1009 new_brightness = (value * max_brightness) / 255;
1010
1011 backlight_set_brightness(head->backlight, new_brightness);
1012 }
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001013}
1014
Pekka Paalanenf8b850d2017-11-15 12:51:01 +02001015static void
1016drm_output_init_backlight(struct drm_output *output)
1017{
1018 struct weston_head *base;
1019 struct drm_head *head;
1020
1021 output->base.set_backlight = NULL;
1022
1023 wl_list_for_each(base, &output->base.head_list, output_link) {
1024 head = to_drm_head(base);
1025
1026 if (head->backlight) {
1027 weston_log("Initialized backlight for head '%s', device %s\n",
1028 head->base.name, head->backlight->path);
1029
1030 if (!output->base.set_backlight) {
1031 output->base.set_backlight = drm_set_backlight;
1032 output->base.backlight_current =
1033 drm_get_backlight(head);
1034 }
1035 }
1036 }
Pekka Paalanenf8b850d2017-11-15 12:51:01 +02001037}
1038
Daniel Stonea08512f2016-11-08 17:46:10 +00001039/**
1040 * Power output on or off
1041 *
1042 * The DPMS/power level of an output is used to switch it on or off. This
1043 * is DRM's hook for doing so, which can called either as part of repaint,
1044 * or independently of the repaint loop.
1045 *
1046 * If we are called as part of repaint, we simply set the relevant bit in
1047 * state and return.
Tomohito Esakib1fb00d2018-01-31 17:50:48 +09001048 *
1049 * This function is never called on a virtual output.
Daniel Stonea08512f2016-11-08 17:46:10 +00001050 */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001051static void
1052drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1053{
Armin Krezović545dba62016-08-05 15:54:18 +02001054 struct drm_output *output = to_drm_output(output_base);
Daniel Stonea08512f2016-11-08 17:46:10 +00001055 struct drm_backend *b = to_drm_backend(output_base->compositor);
1056 struct drm_pending_state *pending_state = b->repaint_data;
1057 struct drm_output_state *state;
Daniel Stone36609c72015-06-18 07:49:02 +01001058 int ret;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001059
Tomohito Esakib1fb00d2018-01-31 17:50:48 +09001060 assert(!output->virtual);
1061
Daniel Stonea08512f2016-11-08 17:46:10 +00001062 if (output->state_cur->dpms == level)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001063 return;
1064
Daniel Stonea08512f2016-11-08 17:46:10 +00001065 /* If we're being called during the repaint loop, then this is
1066 * simple: discard any previously-generated state, and create a new
1067 * state where we disable everything. When we come to flush, this
1068 * will be applied.
1069 *
1070 * However, we need to be careful: we can be called whilst another
1071 * output is in its repaint cycle (pending_state exists), but our
1072 * output still has an incomplete state application outstanding.
1073 * In that case, we need to wait until that completes. */
1074 if (pending_state && !output->state_last) {
1075 /* The repaint loop already sets DPMS on; we don't need to
1076 * explicitly set it on here, as it will already happen
1077 * whilst applying the repaint state. */
1078 if (level == WESTON_DPMS_ON)
1079 return;
1080
1081 state = drm_pending_state_get_output(pending_state, output);
1082 if (state)
1083 drm_output_state_free(state);
1084 state = drm_output_get_disable_state(pending_state, output);
Daniel Stone36609c72015-06-18 07:49:02 +01001085 return;
1086 }
1087
Daniel Stonea08512f2016-11-08 17:46:10 +00001088 /* As we throw everything away when disabling, just send us back through
1089 * a repaint cycle. */
1090 if (level == WESTON_DPMS_ON) {
1091 if (output->dpms_off_pending)
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01001092 output->dpms_off_pending = false;
Daniel Stonea08512f2016-11-08 17:46:10 +00001093 weston_output_schedule_repaint(output_base);
1094 return;
1095 }
1096
1097 /* If we've already got a request in the pipeline, then we need to
1098 * park our DPMS request until that request has quiesced. */
1099 if (output->state_last) {
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01001100 output->dpms_off_pending = true;
Daniel Stonea08512f2016-11-08 17:46:10 +00001101 return;
1102 }
1103
1104 pending_state = drm_pending_state_alloc(b);
1105 drm_output_get_disable_state(pending_state, output);
1106 ret = drm_pending_state_apply_sync(pending_state);
1107 if (ret != 0)
1108 weston_log("drm_set_dpms: couldn't disable output?\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001109}
1110
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001111static const char * const connector_type_names[] = {
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001112 [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
1113 [DRM_MODE_CONNECTOR_VGA] = "VGA",
1114 [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
1115 [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
1116 [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
1117 [DRM_MODE_CONNECTOR_Composite] = "Composite",
1118 [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
1119 [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
1120 [DRM_MODE_CONNECTOR_Component] = "Component",
1121 [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
1122 [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
1123 [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
1124 [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
1125 [DRM_MODE_CONNECTOR_TV] = "TV",
1126 [DRM_MODE_CONNECTOR_eDP] = "eDP",
1127 [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
1128 [DRM_MODE_CONNECTOR_DSI] = "DSI",
Stefan Agner30e283d2018-08-20 17:11:38 +02001129 [DRM_MODE_CONNECTOR_DPI] = "DPI",
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04001130};
1131
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03001132/** Create a name given a DRM connector
1133 *
1134 * \param con The DRM connector whose type and id form the name.
1135 * \return A newly allocate string, or NULL on error. Must be free()'d
1136 * after use.
1137 *
1138 * The name does not identify the DRM display device.
1139 */
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001140static char *
1141make_connector_name(const drmModeConnector *con)
1142{
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03001143 char *name;
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001144 const char *type_name = NULL;
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03001145 int ret;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001146
1147 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
1148 type_name = connector_type_names[con->connector_type];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001149
1150 if (!type_name)
1151 type_name = "UNNAMED";
1152
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03001153 ret = asprintf(&name, "%s-%d", type_name, con->connector_type_id);
1154 if (ret < 0)
1155 return NULL;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001156
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03001157 return name;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001158}
1159
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001160static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001161drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001162{
Hardeningff39efa2013-09-18 23:56:35 +02001163 int w = output->base.current_mode->width;
1164 int h = output->base.current_mode->height;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001165 uint32_t format = output->gbm_format;
1166 uint32_t pixman_format;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001167 unsigned int i;
Daniel Stone61abf352020-03-06 12:46:30 +00001168 const struct pixman_renderer_output_options options = {
1169 .use_shadow = b->use_pixman_shadow,
1170 };
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001171
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001172 switch (format) {
Stefan Agner0bfebeb2019-07-08 00:30:44 +02001173 case DRM_FORMAT_XRGB8888:
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001174 pixman_format = PIXMAN_x8r8g8b8;
1175 break;
Stefan Agner0bfebeb2019-07-08 00:30:44 +02001176 case DRM_FORMAT_RGB565:
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001177 pixman_format = PIXMAN_r5g6b5;
1178 break;
1179 default:
1180 weston_log("Unsupported pixman format 0x%x\n", format);
1181 return -1;
1182 }
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001183
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001184 /* FIXME error checking */
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001185 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001186 output->dumb[i] = drm_fb_create_dumb(b, w, h, format);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001187 if (!output->dumb[i])
1188 goto err;
1189
1190 output->image[i] =
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001191 pixman_image_create_bits(pixman_format, w, h,
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001192 output->dumb[i]->map,
Daniel Stone8eece0c2016-11-17 17:54:00 +00001193 output->dumb[i]->strides[0]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001194 if (!output->image[i])
1195 goto err;
1196 }
1197
Daniel Stone61abf352020-03-06 12:46:30 +00001198 if (pixman_renderer_output_create(&output->base, &options) < 0)
Dongjin Kimba89f002024-09-30 17:49:36 +09001199 goto err;
Ankit Nautiyala21c3932097-03-19 00:24:57 +05301200
Pekka Paalanendee412d2018-04-23 11:44:58 +02001201 weston_log("DRM: output %s %s shadow framebuffer.\n", output->base.name,
1202 b->use_pixman_shadow ? "uses" : "does not use");
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001203
1204 pixman_region32_init_rect(&output->previous_damage,
Alexander Larsson0b135062013-05-28 16:23:36 +02001205 output->base.x, output->base.y, output->base.width, output->base.height);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001206
1207 return 0;
1208
1209err:
1210 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1211 if (output->dumb[i])
Daniel Stone6e7a9612017-04-04 17:54:26 +01001212 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001213 if (output->image[i])
1214 pixman_image_unref(output->image[i]);
1215
1216 output->dumb[i] = NULL;
1217 output->image[i] = NULL;
1218 }
1219
1220 return -1;
1221}
1222
1223static void
1224drm_output_fini_pixman(struct drm_output *output)
1225{
Daniel Stonee2e80132018-01-16 15:37:33 +00001226 struct drm_backend *b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001227 unsigned int i;
1228
Daniel Stonee2e80132018-01-16 15:37:33 +00001229 /* Destroying the Pixman surface will destroy all our buffers,
1230 * regardless of refcount. Ensure we destroy them here. */
1231 if (!b->shutting_down &&
1232 output->scanout_plane->state_cur->fb &&
1233 output->scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB) {
Alexandros Frantzis99751342020-05-18 15:22:49 +03001234 drm_plane_reset_state(output->scanout_plane);
Daniel Stonee2e80132018-01-16 15:37:33 +00001235 }
1236
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001237 pixman_renderer_output_destroy(&output->base);
1238 pixman_region32_fini(&output->previous_damage);
1239
1240 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001241 pixman_image_unref(output->image[i]);
Daniel Stone6e7a9612017-04-04 17:54:26 +01001242 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001243 output->dumb[i] = NULL;
1244 output->image[i] = NULL;
1245 }
1246}
1247
Richard Hughes2b2092a2013-04-24 14:58:02 +01001248static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001249setup_output_seat_constraint(struct drm_backend *b,
Rob Bradford66bd9f52013-06-25 18:56:42 +01001250 struct weston_output *output,
1251 const char *s)
1252{
1253 if (strcmp(s, "") != 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05001254 struct weston_pointer *pointer;
Rob Bradford66bd9f52013-06-25 18:56:42 +01001255 struct udev_seat *seat;
1256
Giulio Camuffo954f1832014-10-11 18:27:30 +03001257 seat = udev_seat_get_named(&b->input, s);
Derek Foreman0720ea32015-07-15 13:00:35 -05001258 if (!seat)
1259 return;
Rob Bradford66bd9f52013-06-25 18:56:42 +01001260
Derek Foreman0720ea32015-07-15 13:00:35 -05001261 seat->base.output = output;
1262
Derek Foreman1281a362015-07-31 16:55:32 -05001263 pointer = weston_seat_get_pointer(&seat->base);
1264 if (pointer)
1265 weston_pointer_clamp(pointer,
1266 &pointer->x,
1267 &pointer->y);
Rob Bradford66bd9f52013-06-25 18:56:42 +01001268 }
1269}
1270
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001271static int
Pekka Paalanenc112f002017-08-28 16:27:20 +03001272drm_output_attach_head(struct weston_output *output_base,
1273 struct weston_head *head_base)
1274{
Pekka Paalanend5f98d82017-12-08 14:45:00 +02001275 struct drm_backend *b = to_drm_backend(output_base->compositor);
1276
Pekka Paalanenc112f002017-08-28 16:27:20 +03001277 if (wl_list_length(&output_base->head_list) >= MAX_CLONED_CONNECTORS)
1278 return -1;
1279
Pekka Paalanend5f98d82017-12-08 14:45:00 +02001280 if (!output_base->enabled)
1281 return 0;
1282
1283 /* XXX: ensure the configuration will work.
1284 * This is actually impossible without major infrastructure
1285 * work. */
1286
1287 /* Need to go through modeset to add connectors. */
1288 /* XXX: Ideally we'd do this per-output, not globally. */
1289 /* XXX: Doing it globally, what guarantees another output's update
1290 * will not clear the flag before this output is updated?
1291 */
1292 b->state_invalid = true;
1293
1294 weston_output_schedule_repaint(output_base);
1295
Pekka Paalanenc112f002017-08-28 16:27:20 +03001296 return 0;
1297}
1298
Pekka Paalanen7f853792017-11-29 14:33:33 +02001299static void
1300drm_output_detach_head(struct weston_output *output_base,
1301 struct weston_head *head_base)
1302{
1303 struct drm_backend *b = to_drm_backend(output_base->compositor);
1304
1305 if (!output_base->enabled)
1306 return;
1307
1308 /* Need to go through modeset to drop connectors that should no longer
1309 * be driven. */
1310 /* XXX: Ideally we'd do this per-output, not globally. */
1311 b->state_invalid = true;
1312
1313 weston_output_schedule_repaint(output_base);
1314}
1315
Stefan Agner3654c672019-07-09 00:50:30 +02001316int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001317parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format)
Neil Roberts77c1a5b2014-03-07 18:05:50 +00001318{
Pekka Paalanen62a94362018-09-26 14:33:36 +03001319 const struct pixel_format_info *pinfo;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00001320
Pekka Paalanen62a94362018-09-26 14:33:36 +03001321 if (s == NULL) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001322 *gbm_format = default_value;
Pekka Paalanen62a94362018-09-26 14:33:36 +03001323
1324 return 0;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00001325 }
1326
Pekka Paalanen62a94362018-09-26 14:33:36 +03001327 pinfo = pixel_format_get_info_by_drm_name(s);
1328 if (!pinfo) {
1329 weston_log("fatal: unrecognized pixel format: %s\n", s);
1330
1331 return -1;
1332 }
1333
1334 /* GBM formats and DRM formats are identical. */
1335 *gbm_format = pinfo->format;
1336
1337 return 0;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00001338}
1339
Pekka Paalaneneee580b2014-06-04 16:43:06 +03001340static int
Pekka Paalanen9c03a7c2017-11-28 14:30:10 +02001341drm_head_read_current_setup(struct drm_head *head, struct drm_backend *backend)
Pekka Paalaneneee580b2014-06-04 16:43:06 +03001342{
Pekka Paalanen9c03a7c2017-11-28 14:30:10 +02001343 int drm_fd = backend->drm.fd;
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001344 drmModeConnector *conn = head->connector.conn;
Pekka Paalaneneee580b2014-06-04 16:43:06 +03001345 drmModeEncoder *encoder;
1346 drmModeCrtc *crtc;
1347
1348 /* Get the current mode on the crtc that's currently driving
1349 * this connector. */
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001350 encoder = drmModeGetEncoder(drm_fd, conn->encoder_id);
Pekka Paalaneneee580b2014-06-04 16:43:06 +03001351 if (encoder != NULL) {
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001352 head->inherited_crtc_id = encoder->crtc_id;
1353
Pekka Paalaneneee580b2014-06-04 16:43:06 +03001354 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
1355 drmModeFreeEncoder(encoder);
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001356
Pekka Paalaneneee580b2014-06-04 16:43:06 +03001357 if (crtc == NULL)
1358 return -1;
1359 if (crtc->mode_valid)
Pekka Paalanen6fae2be2017-11-28 14:33:52 +02001360 head->inherited_mode = crtc->mode;
Pekka Paalaneneee580b2014-06-04 16:43:06 +03001361 drmModeFreeCrtc(crtc);
1362 }
1363
1364 return 0;
1365}
1366
Armin Krezović08368132016-09-30 14:11:05 +02001367static void
1368drm_output_set_gbm_format(struct weston_output *base,
1369 const char *gbm_format)
1370{
1371 struct drm_output *output = to_drm_output(base);
1372 struct drm_backend *b = to_drm_backend(base->compositor);
1373
1374 if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1)
1375 output->gbm_format = b->gbm_format;
1376}
1377
1378static void
1379drm_output_set_seat(struct weston_output *base,
1380 const char *seat)
1381{
1382 struct drm_output *output = to_drm_output(base);
1383 struct drm_backend *b = to_drm_backend(base->compositor);
1384
1385 setup_output_seat_constraint(b, &output->base,
1386 seat ? seat : "");
1387}
1388
1389static int
Pekka Paalanenc4db6f72017-09-05 16:37:03 +03001390drm_output_init_gamma_size(struct drm_output *output)
1391{
1392 struct drm_backend *backend = to_drm_backend(output->base.compositor);
1393 drmModeCrtc *crtc;
1394
1395 assert(output->base.compositor);
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001396 assert(output->crtc);
1397 crtc = drmModeGetCrtc(backend->drm.fd, output->crtc->crtc_id);
Pekka Paalanenc4db6f72017-09-05 16:37:03 +03001398 if (!crtc)
1399 return -1;
1400
1401 output->base.gamma_size = crtc->gamma_size;
1402
1403 drmModeFreeCrtc(crtc);
1404
1405 return 0;
1406}
1407
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001408static uint32_t
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001409drm_connector_get_possible_crtcs_mask(struct drm_connector *connector)
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001410{
1411 uint32_t possible_crtcs = 0;
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001412 drmModeConnector *conn = connector->conn;
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001413 drmModeEncoder *encoder;
1414 int i;
1415
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001416 for (i = 0; i < conn->count_encoders; i++) {
1417 encoder = drmModeGetEncoder(connector->backend->drm.fd,
1418 conn->encoders[i]);
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001419 if (!encoder)
1420 continue;
1421
1422 possible_crtcs |= encoder->possible_crtcs;
1423 drmModeFreeEncoder(encoder);
1424 }
1425
1426 return possible_crtcs;
1427}
1428
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001429/** Pick a CRTC that might be able to drive all attached connectors
1430 *
1431 * @param output The output whose attached heads to include.
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001432 * @return CRTC object to pick, or NULL on failure or not found.
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001433 */
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001434static struct drm_crtc *
1435drm_output_pick_crtc(struct drm_output *output)
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001436{
1437 struct drm_backend *backend;
1438 struct weston_head *base;
1439 struct drm_head *head;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001440 struct drm_crtc *crtc;
1441 struct drm_crtc *best_crtc = NULL;
1442 struct drm_crtc *fallback_crtc = NULL;
1443 struct drm_crtc *existing_crtc[32];
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001444 uint32_t possible_crtcs = 0xffffffff;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001445 unsigned n = 0;
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001446 uint32_t crtc_id;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001447 unsigned int i;
Pekka Paalanendb4c7d72017-11-28 16:11:00 +02001448 bool match;
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001449
1450 backend = to_drm_backend(output->base.compositor);
1451
1452 /* This algorithm ignores drmModeEncoder::possible_clones restriction,
1453 * because it is more often set wrong than not in the kernel. */
1454
1455 /* Accumulate a mask of possible crtcs and find existing routings. */
1456 wl_list_for_each(base, &output->base.head_list, output_link) {
1457 head = to_drm_head(base);
1458
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001459 possible_crtcs &=
1460 drm_connector_get_possible_crtcs_mask(&head->connector);
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001461
1462 crtc_id = head->inherited_crtc_id;
1463 if (crtc_id > 0 && n < ARRAY_LENGTH(existing_crtc))
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001464 existing_crtc[n++] = drm_crtc_find(backend, crtc_id);
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001465 }
1466
1467 /* Find a crtc that could drive each connector individually at least,
1468 * and prefer existing routings. */
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001469 wl_list_for_each(crtc, &backend->crtc_list, link) {
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001470
1471 /* Could the crtc not drive each connector? */
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001472 if (!(possible_crtcs & (1 << crtc->pipe)))
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001473 continue;
1474
1475 /* Is the crtc already in use? */
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001476 if (crtc->output)
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001477 continue;
1478
1479 /* Try to preserve the existing CRTC -> connector routing;
1480 * it makes initialisation faster, and also since we have a
1481 * very dumb picking algorithm, may preserve a better
1482 * choice. */
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001483 for (i = 0; i < n; i++) {
1484 if (existing_crtc[i] == crtc)
1485 return crtc;
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001486 }
1487
Pekka Paalanendb4c7d72017-11-28 16:11:00 +02001488 /* Check if any other head had existing routing to this CRTC.
1489 * If they did, this is not the best CRTC as it might be needed
1490 * for another output we haven't enabled yet. */
1491 match = false;
1492 wl_list_for_each(base, &backend->compositor->head_list,
1493 compositor_link) {
1494 head = to_drm_head(base);
1495
1496 if (head->base.output == &output->base)
1497 continue;
1498
1499 if (weston_head_is_enabled(&head->base))
1500 continue;
1501
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001502 if (head->inherited_crtc_id == crtc->crtc_id) {
Pekka Paalanendb4c7d72017-11-28 16:11:00 +02001503 match = true;
1504 break;
1505 }
1506 }
1507 if (!match)
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001508 best_crtc = crtc;
Pekka Paalanendb4c7d72017-11-28 16:11:00 +02001509
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001510 fallback_crtc = crtc;
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001511 }
1512
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001513 if (best_crtc)
1514 return best_crtc;
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001515
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001516 if (fallback_crtc)
1517 return fallback_crtc;
Pekka Paalanendb4c7d72017-11-28 16:11:00 +02001518
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001519 /* Likely possible_crtcs was empty due to asking for clones,
1520 * but since the DRM documentation says the kernel lies, let's
1521 * pick one crtc anyway. Trial and error is the only way to
1522 * be sure if something doesn't work. */
1523
1524 /* First pick any existing assignment. */
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001525 for (i = 0; i < n; i++) {
1526 crtc = existing_crtc[i];
1527 if (!crtc->output)
1528 return crtc;
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001529 }
1530
1531 /* Otherwise pick any available crtc. */
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001532 wl_list_for_each(crtc, &backend->crtc_list, link) {
1533 if (!crtc->output)
1534 return crtc;
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001535 }
1536
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001537 return NULL;
1538}
1539
1540/** Create an "empty" drm_crtc. It will only set its ID, pipe and props. After
1541 * all, it adds the object to the DRM-backend CRTC list.
1542 */
1543static struct drm_crtc *
1544drm_crtc_create(struct drm_backend *b, uint32_t crtc_id, uint32_t pipe)
1545{
1546 struct drm_crtc *crtc;
1547 drmModeObjectPropertiesPtr props;
1548
1549 props = drmModeObjectGetProperties(b->drm.fd, crtc_id,
1550 DRM_MODE_OBJECT_CRTC);
1551 if (!props) {
1552 weston_log("failed to get CRTC properties\n");
1553 return NULL;
1554 }
1555
1556 crtc = zalloc(sizeof(*crtc));
1557 if (!crtc)
1558 goto ret;
1559
1560 drm_property_info_populate(b, crtc_props, crtc->props_crtc,
1561 WDRM_CRTC__COUNT, props);
1562 crtc->backend = b;
1563 crtc->crtc_id = crtc_id;
1564 crtc->pipe = pipe;
1565 crtc->output = NULL;
1566
1567 /* Add it to the last position of the DRM-backend CRTC list */
1568 wl_list_insert(b->crtc_list.prev, &crtc->link);
1569
1570ret:
1571 drmModeFreeObjectProperties(props);
1572 return crtc;
1573}
1574
1575/** Destroy a drm_crtc object that was created with drm_crtc_create(). It will
1576 * also remove it from the DRM-backend CRTC list.
1577 */
1578static void
1579drm_crtc_destroy(struct drm_crtc *crtc)
1580{
1581 /* TODO: address the issue below to be able to remove the comment
1582 * from the assert.
1583 *
1584 * https://gitlab.freedesktop.org/wayland/weston/-/issues/421
1585 */
1586
1587 //assert(!crtc->output);
1588
1589 wl_list_remove(&crtc->link);
1590 drm_property_info_free(crtc->props_crtc, WDRM_CRTC__COUNT);
1591 free(crtc);
1592}
1593
1594/** Find all CRTCs of the fd and create drm_crtc objects for them.
1595 *
1596 * The CRTCs are saved in a list of the drm_backend and will keep there until
1597 * the fd gets closed.
1598 *
1599 * @param b The DRM-backend structure.
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03001600 * @param resources The DRM resources, it is taken with drmModeGetResources
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001601 * @return 0 on success (at least one CRTC in the list), -1 on failure.
1602 */
1603static int
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03001604drm_backend_create_crtc_list(struct drm_backend *b, drmModeRes *resources)
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001605{
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001606 struct drm_crtc *crtc, *crtc_tmp;
1607 int i;
1608
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001609 /* Iterate through all CRTCs */
1610 for (i = 0; i < resources->count_crtcs; i++) {
1611
1612 /* Let's create an object for the CRTC and add it to the list */
1613 crtc = drm_crtc_create(b, resources->crtcs[i], i);
1614 if (!crtc)
1615 goto err;
1616 }
1617
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001618 return 0;
1619
1620err:
1621 wl_list_for_each_safe(crtc, crtc_tmp, &b->crtc_list, link)
1622 drm_crtc_destroy(crtc);
Pekka Paalanen27cc4812017-11-20 13:31:06 +02001623 return -1;
1624}
1625
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001626
1627/** Populates scanout and cursor planes for the output. Also sets the topology
1628 * of the planes by adding them to the plane stacking list.
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001629 */
1630static int
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001631drm_output_init_planes(struct drm_output *output)
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001632{
1633 struct drm_backend *b = to_drm_backend(output->base.compositor);
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001634
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001635 output->scanout_plane =
1636 drm_output_find_special_plane(b, output,
1637 WDRM_PLANE_TYPE_PRIMARY);
1638 if (!output->scanout_plane) {
1639 weston_log("Failed to find primary plane for output %s\n",
1640 output->base.name);
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001641 return -1;
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001642 }
1643
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001644 weston_compositor_stack_plane(b->compositor,
1645 &output->scanout_plane->base,
1646 &b->compositor->primary_plane);
1647
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001648 /* Failing to find a cursor plane is not fatal, as we'll fall back
1649 * to software cursor. */
1650 output->cursor_plane =
1651 drm_output_find_special_plane(b, output,
1652 WDRM_PLANE_TYPE_CURSOR);
1653
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001654 if (output->cursor_plane)
1655 weston_compositor_stack_plane(b->compositor,
1656 &output->cursor_plane->base,
1657 NULL);
1658 else
1659 b->cursors_are_broken = true;
Pekka Paalanen663d5e92017-09-08 13:32:40 +03001660
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001661 return 0;
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001662}
1663
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001664/** The opposite of drm_output_init_planes(). First of all it removes the planes
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -03001665 * from the plane stacking list. After all it sets the planes of the output as NULL.
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001666 */
1667static void
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001668drm_output_deinit_planes(struct drm_output *output)
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001669{
1670 struct drm_backend *b = to_drm_backend(output->base.compositor);
1671
Alexandros Frantzis53a71cb2020-05-18 15:26:01 +03001672 /* If the compositor is already shutting down, the planes have already
1673 * been destroyed. */
1674 if (!b->shutting_down) {
Pekka Paalanen3d6721e2020-09-16 15:04:04 +03001675 wl_list_remove(&output->scanout_plane->base.link);
1676 wl_list_init(&output->scanout_plane->base.link);
1677
1678 if (output->cursor_plane) {
1679 wl_list_remove(&output->cursor_plane->base.link);
1680 wl_list_init(&output->cursor_plane->base.link);
1681 /* Turn off hardware cursor */
1682 drmModeSetCursor(b->drm.fd, output->crtc->crtc_id, 0, 0, 0);
1683 }
1684
Igor Matheus Andrade Torrentebfcb1ad2020-10-12 13:37:07 -03001685 /* With universal planes, the planes are allocated at startup,
1686 * freed at shutdown, and live on the plane list in between.
1687 * We want the planes to continue to exist and be freed up
1688 * for other outputs.
1689 */
1690 if (output->cursor_plane)
1691 drm_plane_reset_state(output->cursor_plane);
1692 if (output->scanout_plane)
1693 drm_plane_reset_state(output->scanout_plane);
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001694 }
1695
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001696 output->cursor_plane = NULL;
1697 output->scanout_plane = NULL;
1698}
1699
Leandro Ribeiro54293022021-10-12 14:48:36 -03001700static struct weston_drm_format_array *
1701get_scanout_formats(struct drm_backend *b)
1702{
1703 struct weston_compositor *ec = b->compositor;
1704 const struct weston_drm_format_array *renderer_formats;
1705 struct weston_drm_format_array *scanout_formats, union_planes_formats;
1706 struct drm_plane *plane;
1707 int ret;
1708
1709 /* If we got here it means that dma-buf feedback is supported and that
1710 * the renderer has formats/modifiers to expose. */
1711 assert(ec->renderer->get_supported_formats != NULL);
1712 renderer_formats = ec->renderer->get_supported_formats(ec);
1713
1714 scanout_formats = zalloc(sizeof(*scanout_formats));
1715 if (!scanout_formats) {
1716 weston_log("%s: out of memory\n", __func__);
1717 return NULL;
1718 }
1719
1720 weston_drm_format_array_init(&union_planes_formats);
1721 weston_drm_format_array_init(scanout_formats);
1722
1723 /* Compute the union of the format/modifiers of the KMS planes */
1724 wl_list_for_each(plane, &b->plane_list, link) {
1725 /* The scanout formats are used by the dma-buf feedback. But for
1726 * now cursor planes do not support dma-buf buffers, only wl_shm
1727 * buffers. So we skip cursor planes here. */
1728 if (plane->type == WDRM_PLANE_TYPE_CURSOR)
1729 continue;
1730
1731 ret = weston_drm_format_array_join(&union_planes_formats,
1732 &plane->formats);
1733 if (ret < 0)
1734 goto err;
1735 }
1736
1737 /* Compute the intersection between the union of format/modifiers of KMS
1738 * planes and the formats supported by the renderer */
1739 ret = weston_drm_format_array_replace(scanout_formats,
1740 renderer_formats);
1741 if (ret < 0)
1742 goto err;
1743
1744 ret = weston_drm_format_array_intersect(scanout_formats,
1745 &union_planes_formats);
1746 if (ret < 0)
1747 goto err;
1748
1749 weston_drm_format_array_fini(&union_planes_formats);
1750
1751 return scanout_formats;
1752
1753err:
1754 weston_drm_format_array_fini(&union_planes_formats);
1755 weston_drm_format_array_fini(scanout_formats);
Daniel Stonef5086032021-12-06 16:52:19 +00001756 free(scanout_formats);
Leandro Ribeiro54293022021-10-12 14:48:36 -03001757 return NULL;
1758}
1759
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001760/** Pick a CRTC and reserve it for the output.
1761 *
1762 * On failure, the output remains without a CRTC.
1763 *
1764 * @param output The output with no CRTC associated.
1765 * @return 0 on success, -1 on failure.
1766 */
1767static int
1768drm_output_attach_crtc(struct drm_output *output)
1769{
1770 output->crtc = drm_output_pick_crtc(output);
1771 if (!output->crtc) {
1772 weston_log("Output '%s': No available CRTCs.\n",
1773 output->base.name);
1774 return -1;
1775 }
1776
1777 /* Reserve the CRTC for the output */
1778 output->crtc->output = output;
1779
1780 return 0;
1781}
1782
1783/** Release reservation of the CRTC.
1784 *
1785 * Make the CRTC free to be reserved and used by another output.
1786 *
1787 * @param output The output that will release its CRTC.
1788 */
1789static void
1790drm_output_detach_crtc(struct drm_output *output)
1791{
1792 struct drm_backend *b = output->backend;
1793 struct drm_crtc *crtc = output->crtc;
Pekka Paalanen663d5e92017-09-08 13:32:40 +03001794
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001795 crtc->output = NULL;
1796 output->crtc = NULL;
1797
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001798 /* Force resetting unused CRTCs */
1799 b->state_invalid = true;
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03001800}
1801
Pekka Paalanenc4db6f72017-09-05 16:37:03 +03001802static int
Armin Krezović08368132016-09-30 14:11:05 +02001803drm_output_enable(struct weston_output *base)
1804{
1805 struct drm_output *output = to_drm_output(base);
1806 struct drm_backend *b = to_drm_backend(base->compositor);
Pekka Paalanen663d5e92017-09-08 13:32:40 +03001807 int ret;
1808
Tomohito Esakib1fb00d2018-01-31 17:50:48 +09001809 assert(!output->virtual);
1810
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001811 ret = drm_output_attach_crtc(output);
Pekka Paalanen663d5e92017-09-08 13:32:40 +03001812 if (ret < 0)
1813 return -1;
1814
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001815 ret = drm_output_init_planes(output);
1816 if (ret < 0)
1817 goto err_crtc;
1818
Pekka Paalanen663d5e92017-09-08 13:32:40 +03001819 if (drm_output_init_gamma_size(output) < 0)
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001820 goto err_planes;
Armin Krezović08368132016-09-30 14:11:05 +02001821
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001822 if (b->pageflip_timeout)
1823 drm_output_pageflip_timer_create(output);
1824
Giulio Camuffo954f1832014-10-11 18:27:30 +03001825 if (b->use_pixman) {
1826 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001827 weston_log("Failed to init output pixman state\n");
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001828 goto err_planes;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001829 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03001830 } else if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001831 weston_log("Failed to init output gl state\n");
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001832 goto err_planes;
Kristian Høgsberg1d1e0a52012-10-21 13:29:26 -04001833 }
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04001834
leng.fang32af9fc2024-06-13 11:22:15 +08001835 drm_enable_aml_output_config(output, base, b);
Pekka Paalanenf8b850d2017-11-15 12:51:01 +02001836 drm_output_init_backlight(output);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001837
Jonas Ådahle5a12252013-04-05 23:07:11 +02001838 output->base.start_repaint_loop = drm_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05001839 output->base.repaint = drm_output_repaint;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001840 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001841 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08001842 output->base.switch_mode = drm_output_switch_mode;
Richard Hughese7299962013-05-01 21:52:12 +01001843 output->base.set_gamma = drm_output_set_gamma;
1844
Pekka Paalanenc0eb2542017-11-15 13:37:18 +02001845 weston_log("Output %s (crtc %d) video modes:\n",
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001846 output->base.name, output->crtc->crtc_id);
Pekka Paalanenc0eb2542017-11-15 13:37:18 +02001847 drm_output_print_modes(output);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001848
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001849 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01001850
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001851err_planes:
1852 drm_output_deinit_planes(output);
1853err_crtc:
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001854 drm_output_detach_crtc(output);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001855 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001856}
1857
Jesse Barnes58ef3792012-02-23 09:45:49 -05001858static void
Armin Krezović08368132016-09-30 14:11:05 +02001859drm_output_deinit(struct weston_output *base)
1860{
1861 struct drm_output *output = to_drm_output(base);
1862 struct drm_backend *b = to_drm_backend(base->compositor);
1863
Daniel Stone3e661f72016-11-04 17:24:06 +00001864 if (b->use_pixman)
Armin Krezović08368132016-09-30 14:11:05 +02001865 drm_output_fini_pixman(output);
Daniel Stone3e661f72016-11-04 17:24:06 +00001866 else
1867 drm_output_fini_egl(output);
Armin Krezović08368132016-09-30 14:11:05 +02001868
Leandro Ribeiro3be23ef2020-08-13 16:44:02 -03001869 drm_output_deinit_planes(output);
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001870 drm_output_detach_crtc(output);
Armin Krezović08368132016-09-30 14:11:05 +02001871}
1872
1873static void
Pekka Paalanenc112f002017-08-28 16:27:20 +03001874drm_head_destroy(struct drm_head *head);
1875
1876static void
Armin Krezović08368132016-09-30 14:11:05 +02001877drm_output_destroy(struct weston_output *base)
1878{
1879 struct drm_output *output = to_drm_output(base);
1880 struct drm_backend *b = to_drm_backend(base->compositor);
Armin Krezović08368132016-09-30 14:11:05 +02001881
Tomohito Esakib1fb00d2018-01-31 17:50:48 +09001882 assert(!output->virtual);
1883
Daniel Stone31838bf2019-06-17 11:23:25 +01001884 if (output->page_flip_pending || output->atomic_complete_pending) {
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01001885 output->destroy_pending = true;
Armin Krezović08368132016-09-30 14:11:05 +02001886 weston_log("destroy output while page flip pending\n");
1887 return;
1888 }
1889
Alexandros Frantzis10937fe2021-06-14 13:09:44 +03001890 drm_output_set_cursor_view(output, NULL);
1891
Armin Krezović08368132016-09-30 14:11:05 +02001892 if (output->base.enabled)
1893 drm_output_deinit(&output->base);
1894
Pekka Paalanen383b3af2017-09-11 14:40:48 +03001895 drm_mode_list_destroy(b, &output->base.mode_list);
Armin Krezović445b41b2016-10-09 23:48:16 +02001896
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001897 if (output->pageflip_timer)
1898 wl_event_source_remove(output->pageflip_timer);
1899
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03001900 weston_output_release(&output->base);
Armin Krezović08368132016-09-30 14:11:05 +02001901
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001902 assert(!output->state_last);
1903 drm_output_state_free(output->state_cur);
1904
Armin Krezović08368132016-09-30 14:11:05 +02001905 free(output);
1906}
1907
1908static int
1909drm_output_disable(struct weston_output *base)
1910{
1911 struct drm_output *output = to_drm_output(base);
Armin Krezović08368132016-09-30 14:11:05 +02001912
Tomohito Esakib1fb00d2018-01-31 17:50:48 +09001913 assert(!output->virtual);
leng.fang32af9fc2024-06-13 11:22:15 +08001914 drm_output_disable_aml(output, base);
Daniel Stonea08512f2016-11-08 17:46:10 +00001915
Armin Krezović08368132016-09-30 14:11:05 +02001916 if (output->base.enabled)
1917 drm_output_deinit(&output->base);
1918
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01001919 output->disable_pending = false;
Armin Krezović08368132016-09-30 14:11:05 +02001920
Armin Krezović08368132016-09-30 14:11:05 +02001921 return 0;
1922}
1923
Ankit Nautiyala344fe32019-05-14 18:36:08 +05301924/*
1925 * This function converts the protection status from drm values to
1926 * weston_hdcp_protection status. The drm values as read from the connector
1927 * properties "Content Protection" and "HDCP Content Type" need to be converted
1928 * to appropriate weston values, that can be sent to a client application.
1929 */
1930static int
1931get_weston_protection_from_drm(enum wdrm_content_protection_state protection,
1932 enum wdrm_hdcp_content_type type,
1933 enum weston_hdcp_protection *weston_protection)
1934
1935{
1936 if (protection >= WDRM_CONTENT_PROTECTION__COUNT)
1937 return -1;
1938 if (protection == WDRM_CONTENT_PROTECTION_DESIRED ||
1939 protection == WDRM_CONTENT_PROTECTION_UNDESIRED) {
1940 *weston_protection = WESTON_HDCP_DISABLE;
1941 return 0;
1942 }
1943 if (type >= WDRM_HDCP_CONTENT_TYPE__COUNT)
1944 return -1;
1945 if (type == WDRM_HDCP_CONTENT_TYPE0) {
1946 *weston_protection = WESTON_HDCP_ENABLE_TYPE_0;
1947 return 0;
1948 }
1949 if (type == WDRM_HDCP_CONTENT_TYPE1) {
1950 *weston_protection = WESTON_HDCP_ENABLE_TYPE_1;
1951 return 0;
1952 }
1953 return -1;
1954}
1955
1956/**
1957 * Get current content-protection status for a given head.
1958 *
1959 * @param head drm_head, whose protection is to be retrieved
Ankit Nautiyala344fe32019-05-14 18:36:08 +05301960 * @return protection status in case of success, -1 otherwise
1961 */
1962static enum weston_hdcp_protection
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03001963drm_head_get_current_protection(struct drm_head *head)
Ankit Nautiyala344fe32019-05-14 18:36:08 +05301964{
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03001965 drmModeObjectProperties *props = head->connector.props_drm;
Ankit Nautiyala344fe32019-05-14 18:36:08 +05301966 struct drm_property_info *info;
1967 enum wdrm_content_protection_state protection;
1968 enum wdrm_hdcp_content_type type;
1969 enum weston_hdcp_protection weston_hdcp = WESTON_HDCP_DISABLE;
1970
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001971 info = &head->connector.props[WDRM_CONNECTOR_CONTENT_PROTECTION];
Ankit Nautiyala344fe32019-05-14 18:36:08 +05301972 protection = drm_property_get_value(info, props,
1973 WDRM_CONTENT_PROTECTION__COUNT);
1974
1975 if (protection == WDRM_CONTENT_PROTECTION__COUNT)
1976 return WESTON_HDCP_DISABLE;
1977
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001978 info = &head->connector.props[WDRM_CONNECTOR_HDCP_CONTENT_TYPE];
Ankit Nautiyala344fe32019-05-14 18:36:08 +05301979 type = drm_property_get_value(info, props,
1980 WDRM_HDCP_CONTENT_TYPE__COUNT);
1981
1982 /*
1983 * In case of platforms supporting HDCP1.4, only property
1984 * 'Content Protection' is exposed and not the 'HDCP Content Type'
1985 * for such cases HDCP Type 0 should be considered as the content-type.
1986 */
1987
1988 if (type == WDRM_HDCP_CONTENT_TYPE__COUNT)
1989 type = WDRM_HDCP_CONTENT_TYPE0;
1990
1991 if (get_weston_protection_from_drm(protection, type,
1992 &weston_hdcp) == -1) {
1993 weston_log("Invalid drm protection:%d type:%d, for head:%s connector-id:%d\n",
1994 protection, type, head->base.name,
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001995 head->connector.connector_id);
Ankit Nautiyala344fe32019-05-14 18:36:08 +05301996 return WESTON_HDCP_DISABLE;
1997 }
1998
1999 return weston_hdcp;
2000}
2001
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03002002static int
2003drm_connector_update_properties(struct drm_connector *connector)
2004{
2005 drmModeObjectProperties *props;
2006
2007 props = drmModeObjectGetProperties(connector->backend->drm.fd,
2008 connector->connector_id,
2009 DRM_MODE_OBJECT_CONNECTOR);
2010 if (!props) {
2011 weston_log("Error: failed to get connector properties\n");
2012 return -1;
2013 }
2014
2015 if (connector->props_drm)
2016 drmModeFreeObjectProperties(connector->props_drm);
2017 connector->props_drm = props;
2018
2019 return 0;
2020}
2021
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002022/** Replace connector data and monitor information
2023 *
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002024 * @param connector The drm_connector object to be updated.
2025 * @param conn The connector data to be owned by the drm_connector, must match
2026 * the current drm_connector ID.
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002027 * @return 0 on success, -1 on failure.
2028 *
2029 * Takes ownership of @c connector on success, not on failure.
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002030 */
2031static int
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002032drm_connector_assign_connector_info(struct drm_connector *connector,
2033 drmModeConnector *conn)
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002034{
Leandro Ribeiro70863962020-09-09 13:12:35 -03002035 assert(connector->conn != conn);
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002036 assert(connector->connector_id == conn->connector_id);
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002037
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03002038 if (drm_connector_update_properties(connector) < 0)
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002039 return -1;
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002040
leng.fang32af9fc2024-06-13 11:22:15 +08002041 drm_update_helper_connector(connector, conn);
2042
Leandro Ribeiro70863962020-09-09 13:12:35 -03002043 if (connector->conn)
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002044 drmModeFreeConnector(connector->conn);
2045 connector->conn = conn;
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002046
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002047 drm_property_info_free(connector->props, WDRM_CONNECTOR__COUNT);
2048 drm_property_info_populate(connector->backend, connector_props,
2049 connector->props,
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03002050 WDRM_CONNECTOR__COUNT, connector->props_drm);
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002051 return 0;
2052}
2053
Leandro Ribeiro70863962020-09-09 13:12:35 -03002054static void
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002055drm_connector_init(struct drm_backend *b, struct drm_connector *connector,
2056 uint32_t connector_id)
2057{
2058 connector->backend = b;
2059 connector->connector_id = connector_id;
Leandro Ribeiro70863962020-09-09 13:12:35 -03002060 connector->conn = NULL;
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03002061 connector->props_drm = NULL;
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002062}
2063
2064static void
2065drm_connector_fini(struct drm_connector *connector)
2066{
2067 drmModeFreeConnector(connector->conn);
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03002068 drmModeFreeObjectProperties(connector->props_drm);
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002069 drm_property_info_free(connector->props, WDRM_CONNECTOR__COUNT);
2070}
2071
Pekka Paalanen456dc732017-11-09 15:10:11 +02002072static void
2073drm_head_log_info(struct drm_head *head, const char *msg)
2074{
2075 if (head->base.connected) {
2076 weston_log("DRM: head '%s' %s, connector %d is connected, "
2077 "EDID make '%s', model '%s', serial '%s'\n",
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002078 head->base.name, msg, head->connector.connector_id,
Pekka Paalanen456dc732017-11-09 15:10:11 +02002079 head->base.make, head->base.model,
2080 head->base.serial_number ?: "");
2081 } else {
2082 weston_log("DRM: head '%s' %s, connector %d is disconnected.\n",
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002083 head->base.name, msg, head->connector.connector_id);
Pekka Paalanen456dc732017-11-09 15:10:11 +02002084 }
2085}
2086
Pekka Paalanend2e62422017-09-08 15:48:07 +03002087/** Update connector and monitor information
2088 *
2089 * @param head The head to update.
Leandro Ribeiro70863962020-09-09 13:12:35 -03002090 * @param conn The DRM connector object.
2091 * @returns 0 on success, -1 on failure.
Pekka Paalanend2e62422017-09-08 15:48:07 +03002092 *
Leandro Ribeiro70863962020-09-09 13:12:35 -03002093 * Updates monitor information and connection status. This may schedule a
2094 * heads changed call to the user.
Leandro Ribeiro4a5b67a2020-09-15 15:30:35 -03002095 *
2096 * Takes ownership of @c connector on success, not on failure.
Pekka Paalanend2e62422017-09-08 15:48:07 +03002097 */
Leandro Ribeiro70863962020-09-09 13:12:35 -03002098static int
2099drm_head_update_info(struct drm_head *head, drmModeConnector *conn)
Pekka Paalanend2e62422017-09-08 15:48:07 +03002100{
Leandro Ribeiro70863962020-09-09 13:12:35 -03002101 int ret;
Pekka Paalanend2e62422017-09-08 15:48:07 +03002102
Leandro Ribeiro70863962020-09-09 13:12:35 -03002103 ret = drm_connector_assign_connector_info(&head->connector, conn);
Pekka Paalanen456dc732017-11-09 15:10:11 +02002104
Leandro Ribeiro67941642020-09-15 00:11:02 -03002105 update_head_from_connector(head);
2106 weston_head_set_content_protection_status(&head->base,
2107 drm_head_get_current_protection(head));
2108
leng.fang91856072024-06-07 14:12:54 +08002109#ifdef ENABLE_MODE_POLICY
leng.fang69a93e32024-08-08 16:31:01 +08002110 init_mode_policy_without_mode(&head->base,
2111 head->backend->drm.fd, head->connector.conn);
2112
leng.fang3e8a4b52024-07-24 14:51:30 +08002113 if (head->base.device_changed) {
2114 mode_policy_set_head(&head->base);
leng.fang69a93e32024-08-08 16:31:01 +08002115 mode_policy_set_state(&head->base, head->base.connected ?
leng.fang13fd3982024-07-03 19:09:58 +08002116 AML_WESTON_HOTPLUG_PLUG : AML_WESTON_HOTPLUG_UNPLUG, false);
leng.fang3e8a4b52024-07-24 14:51:30 +08002117 }
leng.fang91856072024-06-07 14:12:54 +08002118#endif
2119
Pekka Paalanen456dc732017-11-09 15:10:11 +02002120 if (head->base.device_changed)
2121 drm_head_log_info(head, "updated");
Leandro Ribeiro70863962020-09-09 13:12:35 -03002122
2123 return ret;
Pekka Paalanend2e62422017-09-08 15:48:07 +03002124}
2125
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002126/** Update writeback connector
2127 *
2128 * @param writeback The writeback to update.
2129 * @param conn DRM connector object.
2130 * @returns 0 on success, -1 on failure.
2131 *
2132 * Takes ownership of @c connector on success, not on failure.
2133 */
2134static int
2135drm_writeback_update_info(struct drm_writeback *writeback, drmModeConnector *conn)
2136{
2137 int ret;
2138
2139 ret = drm_connector_assign_connector_info(&writeback->connector, conn);
2140
2141 return ret;
2142}
2143
Daniel Stone087ddf02017-02-14 17:51:30 +00002144/**
Pekka Paalanenc112f002017-08-28 16:27:20 +03002145 * Create a Weston head for a connector
2146 *
2147 * Given a DRM connector, create a matching drm_head structure and add it
2148 * to Weston's head list.
2149 *
Marius Vlada2dace22019-06-12 16:05:44 +03002150 * @param backend Weston backend structure
Leandro Ribeiro70863962020-09-09 13:12:35 -03002151 * @param conn DRM connector object
Pekka Paalanenc112f002017-08-28 16:27:20 +03002152 * @param drm_device udev device pointer
Leandro Ribeiro70863962020-09-09 13:12:35 -03002153 * @returns 0 on success, -1 on failure
Leandro Ribeiro4a5b67a2020-09-15 15:30:35 -03002154 *
2155 * Takes ownership of @c connector on success, not on failure.
Pekka Paalanenc112f002017-08-28 16:27:20 +03002156 */
Leandro Ribeiro70863962020-09-09 13:12:35 -03002157static int
2158drm_head_create(struct drm_backend *backend, drmModeConnector *conn,
Pekka Paalanenc112f002017-08-28 16:27:20 +03002159 struct udev_device *drm_device)
2160{
2161 struct drm_head *head;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002162 char *name;
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002163 int ret;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002164
2165 head = zalloc(sizeof *head);
2166 if (!head)
Leandro Ribeiro70863962020-09-09 13:12:35 -03002167 return -1;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002168
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002169 head->backend = backend;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002170
Leandro Ribeiro70863962020-09-09 13:12:35 -03002171 drm_connector_init(backend, &head->connector, conn->connector_id);
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002172
2173 name = make_connector_name(conn);
Pekka Paalanenc112f002017-08-28 16:27:20 +03002174 if (!name)
Leandro Ribeiro70863962020-09-09 13:12:35 -03002175 goto err;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002176
2177 weston_head_init(&head->base, name);
2178 free(name);
2179
Leandro Ribeiro67941642020-09-15 00:11:02 -03002180 ret = drm_head_update_info(head, conn);
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002181 if (ret < 0)
Leandro Ribeiro67941642020-09-15 00:11:02 -03002182 goto err_update;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002183
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002184 head->backlight = backlight_init(drm_device, conn->connector_type);
Pekka Paalanence724242017-09-04 12:21:24 +03002185
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002186 if (conn->connector_type == DRM_MODE_CONNECTOR_LVDS ||
2187 conn->connector_type == DRM_MODE_CONNECTOR_eDP)
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002188 weston_head_set_internal(&head->base);
Pekka Paalanenc112f002017-08-28 16:27:20 +03002189
Pekka Paalanen9c03a7c2017-11-28 14:30:10 +02002190 if (drm_head_read_current_setup(head, backend) < 0) {
Pekka Paalanen13d233e2017-09-11 14:06:11 +03002191 weston_log("Failed to retrieve current mode from connector %d.\n",
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002192 head->connector.connector_id);
Pekka Paalanen6fae2be2017-11-28 14:33:52 +02002193 /* Not fatal. */
Pekka Paalanen13d233e2017-09-11 14:06:11 +03002194 }
2195
Pekka Paalanenc112f002017-08-28 16:27:20 +03002196 weston_compositor_add_head(backend->compositor, &head->base);
Pekka Paalanen456dc732017-11-09 15:10:11 +02002197 drm_head_log_info(head, "found");
leng.fang32af9fc2024-06-13 11:22:15 +08002198 drm_append_helper_connector(head);
Pekka Paalanenc112f002017-08-28 16:27:20 +03002199
Leandro Ribeiro70863962020-09-09 13:12:35 -03002200 return 0;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002201
Leandro Ribeiro67941642020-09-15 00:11:02 -03002202err_update:
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002203 weston_head_release(&head->base);
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002204err:
Leandro Ribeiro70863962020-09-09 13:12:35 -03002205 drm_connector_fini(&head->connector);
Pekka Paalanenc112f002017-08-28 16:27:20 +03002206 free(head);
Leandro Ribeiro70863962020-09-09 13:12:35 -03002207 return -1;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002208}
2209
2210static void
2211drm_head_destroy(struct drm_head *head)
2212{
leng.fang32af9fc2024-06-13 11:22:15 +08002213 drm_delete_helper_connector(head);
Pekka Paalanenc112f002017-08-28 16:27:20 +03002214 weston_head_release(&head->base);
Pekka Paalanence724242017-09-04 12:21:24 +03002215
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002216 drm_connector_fini(&head->connector);
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002217
Pekka Paalanence724242017-09-04 12:21:24 +03002218 if (head->backlight)
2219 backlight_destroy(head->backlight);
2220
Pekka Paalanenc112f002017-08-28 16:27:20 +03002221 free(head);
2222}
2223
2224/**
Armin Krezović08368132016-09-30 14:11:05 +02002225 * Create a Weston output structure
2226 *
Pekka Paalanend2e62422017-09-08 15:48:07 +03002227 * Create an "empty" drm_output. This is the implementation of
2228 * weston_backend::create_output.
Armin Krezović08368132016-09-30 14:11:05 +02002229 *
Pekka Paalanend2e62422017-09-08 15:48:07 +03002230 * Creating an output is usually followed by drm_output_attach_head()
2231 * and drm_output_enable() to make use of it.
2232 *
2233 * @param compositor The compositor instance.
2234 * @param name Name for the new output.
2235 * @returns The output, or NULL on failure.
Armin Krezović08368132016-09-30 14:11:05 +02002236 */
Pekka Paalanend2e62422017-09-08 15:48:07 +03002237static struct weston_output *
2238drm_output_create(struct weston_compositor *compositor, const char *name)
Armin Krezović08368132016-09-30 14:11:05 +02002239{
Pekka Paalanend2e62422017-09-08 15:48:07 +03002240 struct drm_backend *b = to_drm_backend(compositor);
Armin Krezović08368132016-09-30 14:11:05 +02002241 struct drm_output *output;
Armin Krezović08368132016-09-30 14:11:05 +02002242
Armin Krezović08368132016-09-30 14:11:05 +02002243 output = zalloc(sizeof *output);
2244 if (output == NULL)
Pekka Paalanend2e62422017-09-08 15:48:07 +03002245 return NULL;
Armin Krezović08368132016-09-30 14:11:05 +02002246
Daniel Stone64dbbee2018-07-20 19:00:06 +01002247 output->backend = b;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002248 output->crtc = NULL;
2249
Stefan Agnerccf24072019-07-09 22:02:00 +02002250#ifdef BUILD_DRM_GBM
Tomohito Esaki718a40b2018-01-31 17:50:15 +09002251 output->gbm_bo_flags = GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING;
Stefan Agnerccf24072019-07-09 22:02:00 +02002252#endif
Daniel Stone64dbbee2018-07-20 19:00:06 +01002253
Pekka Paalanend2e62422017-09-08 15:48:07 +03002254 weston_output_init(&output->base, compositor, name);
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002255
Armin Krezović08368132016-09-30 14:11:05 +02002256 output->base.enable = drm_output_enable;
2257 output->base.destroy = drm_output_destroy;
2258 output->base.disable = drm_output_disable;
Pekka Paalanenc112f002017-08-28 16:27:20 +03002259 output->base.attach_head = drm_output_attach_head;
Pekka Paalanen7f853792017-11-29 14:33:33 +02002260 output->base.detach_head = drm_output_detach_head;
Armin Krezović08368132016-09-30 14:11:05 +02002261
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01002262 output->destroy_pending = false;
2263 output->disable_pending = false;
Armin Krezović08368132016-09-30 14:11:05 +02002264
Pekka Paalanen01f60212017-03-24 15:39:24 +02002265 output->state_cur = drm_output_state_alloc(output, NULL);
Pekka Paalanena0bfedc2017-04-03 14:42:51 +03002266
leng.fang32af9fc2024-06-13 11:22:15 +08002267 drm_init_aml_output_config(compositor, output);
Armin Krezović08368132016-09-30 14:11:05 +02002268 weston_compositor_add_pending_output(&output->base, b->compositor);
2269
Pekka Paalanend2e62422017-09-08 15:48:07 +03002270 return &output->base;
Armin Krezović08368132016-09-30 14:11:05 +02002271}
2272
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002273/**
2274 * Create a Weston writeback for a writeback connector
Leandro Ribeiro72430222020-09-14 17:41:41 -03002275 *
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002276 * Given a DRM connector of type writeback, create a matching drm_writeback
2277 * structure and add it to Weston's writeback list.
2278 *
2279 * @param b Weston backend structure
2280 * @param conn DRM connector object of type writeback
2281 * @returns 0 on success, -1 on failure
2282 *
2283 * Takes ownership of @c connector on success, not on failure.
2284 */
2285static int
2286drm_writeback_create(struct drm_backend *b, drmModeConnector *conn)
2287{
2288 struct drm_writeback *writeback;
2289 int ret;
2290
2291 writeback = zalloc(sizeof *writeback);
2292 assert(writeback);
2293
2294 writeback->backend = b;
2295
2296 drm_connector_init(b, &writeback->connector, conn->connector_id);
2297
2298 ret = drm_writeback_update_info(writeback, conn);
2299 if (ret < 0)
2300 goto err;
2301
2302 wl_list_insert(&b->writeback_connector_list, &writeback->link);
2303 return 0;
2304
2305err:
2306 drm_connector_fini(&writeback->connector);
2307 free(writeback);
2308 return -1;
2309}
2310
2311static void
2312drm_writeback_destroy(struct drm_writeback *writeback)
2313{
2314 drm_connector_fini(&writeback->connector);
2315 wl_list_remove(&writeback->link);
2316
2317 free(writeback);
2318}
2319
2320/** Given the DRM connector object of a connector, create drm_head or
2321 * drm_writeback object (depending on the type of connector) for it.
2322 *
2323 * The object is then added to the DRM-backend list of heads or writebacks.
Leandro Ribeiro72430222020-09-14 17:41:41 -03002324 *
2325 * @param b The DRM-backend structure
2326 * @param conn The DRM connector object
2327 * @param drm_device udev device pointer
2328 * @return 0 on success, -1 on failure
2329 */
2330static int
2331drm_backend_add_connector(struct drm_backend *b, drmModeConnector *conn,
2332 struct udev_device *drm_device)
2333{
2334 int ret;
2335
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002336 if (conn->connector_type == DRM_MODE_CONNECTOR_WRITEBACK) {
2337 ret = drm_writeback_create(b, conn);
2338 if (ret < 0)
2339 weston_log("DRM: failed to create writeback for connector %d.\n",
2340 conn->connector_id);
2341 } else {
2342 ret = drm_head_create(b, conn, drm_device);
2343 if (ret < 0)
2344 weston_log("DRM: failed to create head for connector %d.\n",
2345 conn->connector_id);
2346 }
Leandro Ribeiro72430222020-09-14 17:41:41 -03002347
2348 return ret;
2349}
2350
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002351/** Find all connectors of the fd and create drm_head or drm_writeback objects
2352 * (depending on the type of connector they are) for each of them
2353 *
2354 * These objects are added to the DRM-backend lists of heads and writebacks.
2355 *
2356 * @param b The DRM-backend structure
2357 * @param drm_device udev device pointer
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03002358 * @param resources The DRM resources, it is taken with drmModeGetResources
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002359 * @return 0 on success, -1 on failure
2360 */
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002361static int
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03002362drm_backend_discover_connectors(struct drm_backend *b, struct udev_device *drm_device,
2363 drmModeRes *resources)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002364{
Leandro Ribeiro70863962020-09-09 13:12:35 -03002365 drmModeConnector *conn;
2366 int i, ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002367
Giulio Camuffo954f1832014-10-11 18:27:30 +03002368 b->min_width = resources->min_width;
2369 b->max_width = resources->max_width;
2370 b->min_height = resources->min_height;
2371 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05002372
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002373 for (i = 0; i < resources->count_connectors; i++) {
Pekka Paalanend2e62422017-09-08 15:48:07 +03002374 uint32_t connector_id = resources->connectors[i];
Daniel Stone02cf4662017-03-03 16:19:39 +00002375
Leandro Ribeiro70863962020-09-09 13:12:35 -03002376 conn = drmModeGetConnector(b->drm.fd, connector_id);
2377 if (!conn)
2378 continue;
2379
Leandro Ribeiro72430222020-09-14 17:41:41 -03002380 ret = drm_backend_add_connector(b, conn, drm_device);
2381 if (ret < 0)
Leandro Ribeiro70863962020-09-09 13:12:35 -03002382 drmModeFreeConnector(conn);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002383 }
2384
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002385 return 0;
2386}
2387
Leandro Ribeiro99611c82020-09-14 17:19:12 -03002388static bool
2389resources_has_connector(drmModeRes *resources, uint32_t connector_id)
2390{
2391 for (int i = 0; i < resources->count_connectors; i++) {
2392 if (resources->connectors[i] == connector_id)
2393 return true;
2394 }
2395
2396 return false;
2397}
2398
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002399static void
Leandro Ribeiro993920d2020-09-14 17:01:01 -03002400drm_backend_update_connectors(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002401{
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002402 drmModeRes *resources;
Leandro Ribeiro70863962020-09-09 13:12:35 -03002403 drmModeConnector *conn;
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002404 struct weston_head *base, *base_next;
Pekka Paalanena0a37462017-08-31 15:41:57 +03002405 struct drm_head *head;
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002406 struct drm_writeback *writeback, *writeback_next;
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002407 uint32_t connector_id;
Leandro Ribeiro70863962020-09-09 13:12:35 -03002408 int i, ret;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002409
Giulio Camuffo954f1832014-10-11 18:27:30 +03002410 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002411 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002412 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002413 return;
2414 }
2415
Pekka Paalanend2e62422017-09-08 15:48:07 +03002416 /* collect new connectors that have appeared, e.g. MST */
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002417 for (i = 0; i < resources->count_connectors; i++) {
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002418 connector_id = resources->connectors[i];
Benjamin Franzke117483d2011-08-30 11:38:26 +02002419
Leandro Ribeiro70863962020-09-09 13:12:35 -03002420 conn = drmModeGetConnector(b->drm.fd, connector_id);
2421 if (!conn)
2422 continue;
2423
Pekka Paalanend2e62422017-09-08 15:48:07 +03002424 head = drm_head_find_by_connector(b, connector_id);
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002425 writeback = drm_writeback_find_by_connector(b, connector_id);
2426
2427 /* Connector can't be owned by both a head and a writeback, so
2428 * one of the searches must fail. */
2429 assert(head == NULL || writeback == NULL);
2430
Leandro Ribeiro72430222020-09-14 17:41:41 -03002431 if (head)
Leandro Ribeiro70863962020-09-09 13:12:35 -03002432 ret = drm_head_update_info(head, conn);
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002433 else if (writeback)
2434 ret = drm_writeback_update_info(writeback, conn);
Leandro Ribeiro72430222020-09-14 17:41:41 -03002435 else
2436 ret = drm_backend_add_connector(b, conn, drm_device);
2437
2438 if (ret < 0)
2439 drmModeFreeConnector(conn);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002440 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002441
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002442 /* Destroy head objects of connectors (except writeback connectors) that
2443 * have disappeared. */
2444 wl_list_for_each_safe(base, base_next,
Pekka Paalanena0a37462017-08-31 15:41:57 +03002445 &b->compositor->head_list, compositor_link) {
Pekka Paalanena0a37462017-08-31 15:41:57 +03002446 head = to_drm_head(base);
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002447 connector_id = head->connector.connector_id;
Pekka Paalanena0a37462017-08-31 15:41:57 +03002448
Leandro Ribeiro99611c82020-09-14 17:19:12 -03002449 if (resources_has_connector(resources, connector_id))
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002450 continue;
2451
Pekka Paalanend2e62422017-09-08 15:48:07 +03002452 weston_log("DRM: head '%s' (connector %d) disappeared.\n",
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002453 head->base.name, connector_id);
Pekka Paalanend2e62422017-09-08 15:48:07 +03002454 drm_head_destroy(head);
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002455 }
2456
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002457 /* Destroy writeback objects of writeback connectors that have
2458 * disappeared. */
2459 wl_list_for_each_safe(writeback, writeback_next,
2460 &b->writeback_connector_list, link) {
2461 connector_id = writeback->connector.connector_id;
2462
2463 if (resources_has_connector(resources, connector_id))
2464 continue;
2465
2466 weston_log("DRM: writeback connector (connector %d) disappeared.\n",
2467 connector_id);
2468 drm_writeback_destroy(writeback);
2469 }
2470
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002471 drmModeFreeResources(resources);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002472}
2473
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302474static enum wdrm_connector_property
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002475drm_connector_find_property_by_id(struct drm_connector *connector,
2476 uint32_t property_id)
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302477{
2478 int i;
2479 enum wdrm_connector_property prop = WDRM_CONNECTOR__COUNT;
2480
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002481 if (!connector || !property_id)
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302482 return WDRM_CONNECTOR__COUNT;
2483
2484 for (i = 0; i < WDRM_CONNECTOR__COUNT; i++)
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002485 if (connector->props[i].prop_id == property_id) {
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302486 prop = (enum wdrm_connector_property) i;
2487 break;
2488 }
2489 return prop;
2490}
2491
2492static void
2493drm_backend_update_conn_props(struct drm_backend *b,
2494 uint32_t connector_id,
2495 uint32_t property_id)
2496{
2497 struct drm_head *head;
2498 enum wdrm_connector_property conn_prop;
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302499
2500 head = drm_head_find_by_connector(b, connector_id);
2501 if (!head) {
2502 weston_log("DRM: failed to find head for connector id: %d.\n",
2503 connector_id);
2504 return;
2505 }
2506
Leandro Ribeiroe6369902020-06-17 11:09:47 -03002507 conn_prop = drm_connector_find_property_by_id(&head->connector, property_id);
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302508 if (conn_prop >= WDRM_CONNECTOR__COUNT)
2509 return;
2510
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03002511 if (drm_connector_update_properties(&head->connector) < 0)
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302512 return;
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03002513
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302514 if (conn_prop == WDRM_CONNECTOR_CONTENT_PROTECTION) {
2515 weston_head_set_content_protection_status(&head->base,
Leandro Ribeiro702fbf72020-08-18 17:35:05 -03002516 drm_head_get_current_protection(head));
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302517 }
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302518}
2519
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002520static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002521udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002522{
David Herrmannd7488c22012-03-11 20:05:21 +01002523 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01002524 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01002525
2526 sysnum = udev_device_get_sysnum(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002527 if (!sysnum || atoi(sysnum) != b->drm.id)
David Herrmannd7488c22012-03-11 20:05:21 +01002528 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02002529
David Herrmann6ac52db2012-03-11 20:05:22 +01002530 val = udev_device_get_property_value(device, "HOTPLUG");
2531 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002532 return 0;
2533
David Herrmann6ac52db2012-03-11 20:05:22 +01002534 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002535}
2536
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002537static int
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302538udev_event_is_conn_prop_change(struct drm_backend *b,
2539 struct udev_device *device,
2540 uint32_t *connector_id,
2541 uint32_t *property_id)
2542
2543{
2544 const char *val;
2545 int id;
2546
2547 val = udev_device_get_property_value(device, "CONNECTOR");
2548 if (!val || !safe_strtoint(val, &id))
2549 return 0;
2550 else
2551 *connector_id = id;
2552
2553 val = udev_device_get_property_value(device, "PROPERTY");
2554 if (!val || !safe_strtoint(val, &id))
2555 return 0;
2556 else
2557 *property_id = id;
2558
2559 return 1;
2560}
2561
2562static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002563udev_drm_event(int fd, uint32_t mask, void *data)
2564{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002565 struct drm_backend *b = data;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002566 struct udev_device *event;
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302567 uint32_t conn_id, prop_id;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002568
Giulio Camuffo954f1832014-10-11 18:27:30 +03002569 event = udev_monitor_receive_device(b->udev_monitor);
limin.tian930711f2024-11-08 08:11:08 +00002570 udev_event_is_resume(b, event);
leng.fang9cf09e22024-07-17 20:01:11 +08002571 if (udev_event_is_hotplug(b, event) && b->display_enable) {
leng.fang91856072024-06-07 14:12:54 +08002572#ifdef ENABLE_MODE_POLICY
leng.fang13fd3982024-07-03 19:09:58 +08002573 mode_policy_set_hotplug(AML_WESTON_HOTPLUG_PLUG | AML_WESTON_HOTPLUG_UNPLUG, true);
leng.fang91856072024-06-07 14:12:54 +08002574#endif
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302575 if (udev_event_is_conn_prop_change(b, event, &conn_id, &prop_id))
2576 drm_backend_update_conn_props(b, conn_id, prop_id);
2577 else
Leandro Ribeiro993920d2020-09-14 17:01:01 -03002578 drm_backend_update_connectors(b, event);
Ankit Nautiyala344fe32019-05-14 18:36:08 +05302579 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002580
leng.fang32af9fc2024-06-13 11:22:15 +08002581#ifdef BUILD_AML_TV
2582 drm_backend_afr_update(b, udev_event_frame_rate_hint(event),
2583 udev_event_is_vdin_event(event));
2584#endif
2585
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002586 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002587
2588 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002589}
2590
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002591static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002592drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002593{
Armin Krezović545dba62016-08-05 15:54:18 +02002594 struct drm_backend *b = to_drm_backend(ec);
Pekka Paalanenc112f002017-08-28 16:27:20 +03002595 struct weston_head *base, *next;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002596 struct drm_crtc *crtc, *crtc_tmp;
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002597 struct drm_writeback *writeback, *writeback_tmp;
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002598
leng.fang32af9fc2024-06-13 11:22:15 +08002599#ifdef ENABLE_DRM_HELP
2600 stop_help_worker();
2601#endif
2602
Giulio Camuffo954f1832014-10-11 18:27:30 +03002603 udev_input_destroy(&b->input);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002604
Giulio Camuffo954f1832014-10-11 18:27:30 +03002605 wl_event_source_remove(b->udev_drm_source);
2606 wl_event_source_remove(b->drm_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002607
Daniel Stoneb57c6a02017-10-05 16:27:21 +01002608 b->shutting_down = true;
2609
Giulio Camuffo954f1832014-10-11 18:27:30 +03002610 destroy_sprites(b);
Kristian Høgsberg3d64a3e2013-05-10 12:36:04 -04002611
Leandro Ribeirof0149642019-12-18 15:52:18 -03002612 weston_log_scope_destroy(b->debug);
Daniel Stone1cbe1f92018-07-20 10:21:28 +01002613 b->debug = NULL;
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03002614 weston_compositor_shutdown(ec);
2615
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002616 wl_list_for_each_safe(crtc, crtc_tmp, &b->crtc_list, link)
2617 drm_crtc_destroy(crtc);
2618
Pekka Paalanenc112f002017-08-28 16:27:20 +03002619 wl_list_for_each_safe(base, next, &ec->head_list, compositor_link)
2620 drm_head_destroy(to_drm_head(base));
2621
Leandro Ribeiro96bef052020-09-09 15:23:49 -03002622 wl_list_for_each_safe(writeback, writeback_tmp,
2623 &b->writeback_connector_list, link)
2624 drm_writeback_destroy(writeback);
2625
Stefan Agnerccf24072019-07-09 22:02:00 +02002626#ifdef BUILD_DRM_GBM
Giulio Camuffo954f1832014-10-11 18:27:30 +03002627 if (b->gbm)
2628 gbm_device_destroy(b->gbm);
Stefan Agnerccf24072019-07-09 22:02:00 +02002629#endif
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002630
Pekka Paalanen5b0aa552017-12-07 16:06:05 +02002631 udev_monitor_unref(b->udev_monitor);
Pekka Paalanen2a0c6c32017-09-13 16:48:01 +03002632 udev_unref(b->udev);
2633
Marius Vlad7a44ee72021-06-23 16:02:35 +03002634 weston_launcher_close(ec->launcher, b->drm.fd);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002635 weston_launcher_destroy(ec->launcher);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002636
Pekka Paalanen9bf4f372017-12-07 16:05:29 +02002637 free(b->drm.filename);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002638 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002639}
2640
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002641static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002642session_notify(struct wl_listener *listener, void *data)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002643{
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002644 struct weston_compositor *compositor = data;
Armin Krezović545dba62016-08-05 15:54:18 +02002645 struct drm_backend *b = to_drm_backend(compositor);
Daniel Stone085d2b92015-05-21 00:00:57 +01002646 struct drm_plane *plane;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002647 struct drm_output *output;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002648 struct drm_crtc *crtc;
leng.fang3e8a4b52024-07-24 14:51:30 +08002649 struct weston_head *head;
limin.tian930711f2024-11-08 08:11:08 +00002650 bool found = false;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002651 if (compositor->session_active) {
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002652 weston_log("activating session\n");
limin.tian930711f2024-11-08 08:11:08 +00002653 udev_input_enable(&b->input);
leng.fang3e8a4b52024-07-24 14:51:30 +08002654 wl_list_for_each(output, &compositor->output_list, base.link) {
2655 head = weston_output_get_first_head(&output->base);
limin.tian930711f2024-11-08 08:11:08 +00002656 weston_log("activating session, output:%s head:%s :%d\n", output->base.name, head->name, head->connected);
2657 if (strcmp("HDMI-A-1", head->name) == 0 && head->connected ) {
2658 found = true;
leng.fang0d88a8f2024-08-05 15:14:52 +08002659 }
leng.fang3e8a4b52024-07-24 14:51:30 +08002660 }
limin.tian930711f2024-11-08 08:11:08 +00002661 if (!found) {
2662 weston_log("activating session, no HDMI\n");
2663 resume_display(b);
2664 }
2665 weston_compositor_resume_focus(compositor);
Daniel Stonef33e1042016-11-05 08:10:13 +00002666 weston_compositor_wake(compositor);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002667 weston_compositor_damage_all(compositor);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002668 } else {
2669 weston_log("deactivating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002670 udev_input_disable(&b->input);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04002671
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002672 weston_compositor_offscreen(compositor);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04002673
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002674 /* If we have a repaint scheduled (either from a
2675 * pending pageflip or the idle handler), make sure we
2676 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002677 * vt switched away. The OFFSCREEN state will prevent
Abdur Rehman4dca0e12017-01-01 19:46:35 +05002678 * further attempts at repainting. When we switch
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002679 * back, we schedule a repaint, which will process
2680 * pending frame callbacks. */
2681
Giulio Camuffo954f1832014-10-11 18:27:30 +03002682 wl_list_for_each(output, &compositor->output_list, base.link) {
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002683 crtc = output->crtc;
Daniel Stone09a97e22017-03-01 11:34:06 +00002684 output->base.repaint_needed = false;
Daniel Stone2ba17f42015-05-19 20:02:41 +01002685 if (output->cursor_plane)
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002686 drmModeSetCursor(b->drm.fd, crtc->crtc_id,
Daniel Stone2ba17f42015-05-19 20:02:41 +01002687 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002688 }
2689
leng.fang5ec9b6c2024-07-29 14:25:00 +08002690 if (wl_list_empty(&compositor->output_list)) {
2691 wl_list_for_each(crtc, &b->crtc_list, link)
2692 break;
2693 } else {
2694 output = container_of(compositor->output_list.next,
2695 struct drm_output, base.link);
2696 crtc = output->crtc;
2697 }
2698
2699 if (!crtc)
2700 return;
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002701
Daniel Stone085d2b92015-05-21 00:00:57 +01002702 wl_list_for_each(plane, &b->plane_list, link) {
2703 if (plane->type != WDRM_PLANE_TYPE_OVERLAY)
2704 continue;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03002705 drmModeSetPlane(b->drm.fd, plane->plane_id, crtc->crtc_id,
2706 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
Daniel Stone085d2b92015-05-21 00:00:57 +01002707 }
2708 }
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002709}
2710
Robert Beckett8d23ab72019-06-13 16:55:44 +01002711
2712/**
2713 * Handle KMS GPU being added/removed
2714 *
2715 * If the device being added/removed is the KMS device, we activate/deactivate
2716 * the compositor session.
2717 *
2718 * @param compositor The compositor instance.
2719 * @param device The device being added/removed.
2720 * @param added Whether the device is being added (or removed)
2721 */
2722static void
2723drm_device_changed(struct weston_compositor *compositor,
2724 dev_t device, bool added)
2725{
2726 struct drm_backend *b = to_drm_backend(compositor);
2727
Robert Beckett49dc3202019-07-02 16:31:22 +01002728 if (b->drm.fd < 0 || b->drm.devnum != device ||
2729 compositor->session_active == added)
Robert Beckett8d23ab72019-06-13 16:55:44 +01002730 return;
2731
2732 compositor->session_active = added;
2733 wl_signal_emit(&compositor->session_signal, compositor);
2734}
2735
Daniel Stoneefa504f2016-12-19 16:48:20 +00002736/**
2737 * Determines whether or not a device is capable of modesetting. If successful,
2738 * sets b->drm.fd and b->drm.filename to the opened device.
2739 */
2740static bool
2741drm_device_is_kms(struct drm_backend *b, struct udev_device *device)
2742{
2743 const char *filename = udev_device_get_devnode(device);
2744 const char *sysnum = udev_device_get_sysnum(device);
Robert Beckett8d23ab72019-06-13 16:55:44 +01002745 dev_t devnum = udev_device_get_devnum(device);
Daniel Stoneefa504f2016-12-19 16:48:20 +00002746 drmModeRes *res;
Marius Vlad7d070ca2018-11-23 14:02:07 +02002747 int id = -1, fd;
Daniel Stoneefa504f2016-12-19 16:48:20 +00002748
2749 if (!filename)
2750 return false;
2751
2752 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
2753 if (fd < 0)
2754 return false;
2755
2756 res = drmModeGetResources(fd);
2757 if (!res)
2758 goto out_fd;
2759
2760 if (res->count_crtcs <= 0 || res->count_connectors <= 0 ||
2761 res->count_encoders <= 0)
2762 goto out_res;
2763
2764 if (sysnum)
2765 id = atoi(sysnum);
2766 if (!sysnum || id < 0) {
2767 weston_log("couldn't get sysnum for device %s\n", filename);
2768 goto out_res;
2769 }
2770
2771 /* We can be called successfully on multiple devices; if we have,
2772 * clean up old entries. */
2773 if (b->drm.fd >= 0)
2774 weston_launcher_close(b->compositor->launcher, b->drm.fd);
2775 free(b->drm.filename);
2776
2777 b->drm.fd = fd;
2778 b->drm.id = id;
2779 b->drm.filename = strdup(filename);
Robert Beckett8d23ab72019-06-13 16:55:44 +01002780 b->drm.devnum = devnum;
Daniel Stoneefa504f2016-12-19 16:48:20 +00002781
Sergi Granellceb59812017-03-28 12:44:04 +02002782 drmModeFreeResources(res);
2783
Daniel Stoneefa504f2016-12-19 16:48:20 +00002784 return true;
2785
2786out_res:
2787 drmModeFreeResources(res);
2788out_fd:
2789 weston_launcher_close(b->compositor->launcher, fd);
2790 return false;
2791}
2792
David Herrmann0af066f2012-10-29 19:21:16 +01002793/*
2794 * Find primary GPU
2795 * Some systems may have multiple DRM devices attached to a single seat. This
2796 * function loops over all devices and tries to find a PCI device with the
2797 * boot_vga sysfs attribute set to 1.
2798 * If no such device is found, the first DRM device reported by udev is used.
Daniel Stoneefa504f2016-12-19 16:48:20 +00002799 * Devices are also vetted to make sure they are are capable of modesetting,
2800 * rather than pure render nodes (GPU with no display), or pure
2801 * memory-allocation devices (VGEM).
David Herrmann0af066f2012-10-29 19:21:16 +01002802 */
2803static struct udev_device*
Giulio Camuffo954f1832014-10-11 18:27:30 +03002804find_primary_gpu(struct drm_backend *b, const char *seat)
David Herrmann0af066f2012-10-29 19:21:16 +01002805{
2806 struct udev_enumerate *e;
2807 struct udev_list_entry *entry;
2808 const char *path, *device_seat, *id;
2809 struct udev_device *device, *drm_device, *pci;
2810
Giulio Camuffo954f1832014-10-11 18:27:30 +03002811 e = udev_enumerate_new(b->udev);
David Herrmann0af066f2012-10-29 19:21:16 +01002812 udev_enumerate_add_match_subsystem(e, "drm");
2813 udev_enumerate_add_match_sysname(e, "card[0-9]*");
2814
2815 udev_enumerate_scan_devices(e);
2816 drm_device = NULL;
2817 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Daniel Stoneefa504f2016-12-19 16:48:20 +00002818 bool is_boot_vga = false;
2819
David Herrmann0af066f2012-10-29 19:21:16 +01002820 path = udev_list_entry_get_name(entry);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002821 device = udev_device_new_from_syspath(b->udev, path);
David Herrmann0af066f2012-10-29 19:21:16 +01002822 if (!device)
2823 continue;
2824 device_seat = udev_device_get_property_value(device, "ID_SEAT");
2825 if (!device_seat)
2826 device_seat = default_seat;
2827 if (strcmp(device_seat, seat)) {
2828 udev_device_unref(device);
2829 continue;
2830 }
2831
2832 pci = udev_device_get_parent_with_subsystem_devtype(device,
2833 "pci", NULL);
2834 if (pci) {
2835 id = udev_device_get_sysattr_value(pci, "boot_vga");
Daniel Stoneefa504f2016-12-19 16:48:20 +00002836 if (id && !strcmp(id, "1"))
2837 is_boot_vga = true;
David Herrmann0af066f2012-10-29 19:21:16 +01002838 }
2839
Daniel Stoneefa504f2016-12-19 16:48:20 +00002840 /* If we already have a modesetting-capable device, and this
2841 * device isn't our boot-VGA device, we aren't going to use
2842 * it. */
2843 if (!is_boot_vga && drm_device) {
David Herrmann0af066f2012-10-29 19:21:16 +01002844 udev_device_unref(device);
Daniel Stoneefa504f2016-12-19 16:48:20 +00002845 continue;
2846 }
2847
2848 /* Make sure this device is actually capable of modesetting;
2849 * if this call succeeds, b->drm.{fd,filename} will be set,
2850 * and any old values freed. */
2851 if (!drm_device_is_kms(b, device)) {
2852 udev_device_unref(device);
2853 continue;
2854 }
2855
2856 /* There can only be one boot_vga device, and we try to use it
2857 * at all costs. */
2858 if (is_boot_vga) {
2859 if (drm_device)
2860 udev_device_unref(drm_device);
2861 drm_device = device;
2862 break;
2863 }
2864
2865 /* Per the (!is_boot_vga && drm_device) test above, we only
2866 * trump existing saved devices with boot-VGA devices, so if
2867 * we end up here, this must be the first device we've seen. */
2868 assert(!drm_device);
2869 drm_device = device;
David Herrmann0af066f2012-10-29 19:21:16 +01002870 }
2871
Daniel Stoneefa504f2016-12-19 16:48:20 +00002872 /* If we're returning a device to use, we must have an open FD for
2873 * it. */
2874 assert(!!drm_device == (b->drm.fd >= 0));
2875
David Herrmann0af066f2012-10-29 19:21:16 +01002876 udev_enumerate_unref(e);
2877 return drm_device;
2878}
2879
Pekka Paalanenb45ed8b2017-03-28 18:04:27 +03002880static struct udev_device *
2881open_specific_drm_device(struct drm_backend *b, const char *name)
2882{
2883 struct udev_device *device;
2884
2885 device = udev_device_new_from_subsystem_sysname(b->udev, "drm", name);
2886 if (!device) {
2887 weston_log("ERROR: could not open DRM device '%s'\n", name);
2888 return NULL;
2889 }
2890
2891 if (!drm_device_is_kms(b, device)) {
2892 udev_device_unref(device);
2893 weston_log("ERROR: DRM device '%s' is not a KMS device.\n", name);
2894 return NULL;
2895 }
2896
2897 /* If we're returning a device to use, we must have an open FD for
2898 * it. */
2899 assert(b->drm.fd >= 0);
2900
2901 return device;
2902}
2903
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002904static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002905planes_binding(struct weston_keyboard *keyboard, const struct timespec *time,
2906 uint32_t key, void *data)
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002907{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002908 struct drm_backend *b = data;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002909
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002910 switch (key) {
2911 case KEY_C:
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01002912 b->cursors_are_broken ^= true;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002913 break;
2914 case KEY_V:
Daniel Stone87fab1c2019-06-17 11:13:20 +01002915 /* We don't support overlay-plane usage with legacy KMS. */
2916 if (b->atomic_modeset)
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01002917 b->sprites_are_broken ^= true;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002918 break;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002919 default:
2920 break;
2921 }
Ander Conselvan de Oliveira180f42a2012-11-21 15:11:37 +02002922}
2923
Kristian Høgsberg0eac34a2013-08-30 14:28:22 -07002924#ifdef BUILD_VAAPI_RECORDER
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002925static void
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002926recorder_destroy(struct drm_output *output)
2927{
2928 vaapi_recorder_destroy(output->recorder);
2929 output->recorder = NULL;
2930
Ankit Nautiyal93dde242019-07-08 11:46:42 +05302931 weston_output_disable_planes_decr(&output->base);
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002932
2933 wl_list_remove(&output->recorder_frame_listener.link);
2934 weston_log("[libva recorder] done\n");
2935}
2936
2937static void
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002938recorder_frame_notify(struct wl_listener *listener, void *data)
2939{
2940 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002941 struct drm_backend *b;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002942 int fd, ret;
2943
2944 output = container_of(listener, struct drm_output,
2945 recorder_frame_listener);
Armin Krezović545dba62016-08-05 15:54:18 +02002946 b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002947
2948 if (!output->recorder)
2949 return;
2950
Daniel Stonee2e80132018-01-16 15:37:33 +00002951 ret = drmPrimeHandleToFD(b->drm.fd,
Daniel Stone8eece0c2016-11-17 17:54:00 +00002952 output->scanout_plane->state_cur->fb->handles[0],
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002953 DRM_CLOEXEC, &fd);
2954 if (ret) {
2955 weston_log("[libva recorder] "
2956 "failed to create prime fd for front buffer\n");
2957 return;
2958 }
2959
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002960 ret = vaapi_recorder_frame(output->recorder, fd,
Daniel Stone8eece0c2016-11-17 17:54:00 +00002961 output->scanout_plane->state_cur->fb->strides[0]);
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002962 if (ret < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +02002963 weston_log("[libva recorder] aborted: %s\n", strerror(errno));
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002964 recorder_destroy(output);
2965 }
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002966}
2967
2968static void *
Giulio Camuffo954f1832014-10-11 18:27:30 +03002969create_recorder(struct drm_backend *b, int width, int height,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002970 const char *filename)
2971{
2972 int fd;
2973 drm_magic_t magic;
2974
Giulio Camuffo954f1832014-10-11 18:27:30 +03002975 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002976 if (fd < 0)
2977 return NULL;
2978
2979 drmGetMagic(fd, &magic);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002980 drmAuthMagic(b->drm.fd, magic);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002981
2982 return vaapi_recorder_create(fd, width, height, filename);
2983}
2984
2985static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002986recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time,
2987 uint32_t key, void *data)
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002988{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002989 struct drm_backend *b = data;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002990 struct drm_output *output;
2991 int width, height;
2992
Giulio Camuffo954f1832014-10-11 18:27:30 +03002993 output = container_of(b->compositor->output_list.next,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002994 struct drm_output, base.link);
2995
2996 if (!output->recorder) {
Stefan Agner4a18f302019-10-20 18:25:42 +02002997 if (output->gbm_format != DRM_FORMAT_XRGB8888) {
Ander Conselvan de Oliveira2ef1cd12014-05-06 16:49:06 +03002998 weston_log("failed to start vaapi recorder: "
2999 "output format not supported\n");
3000 return;
3001 }
3002
Hardeningff39efa2013-09-18 23:56:35 +02003003 width = output->base.current_mode->width;
3004 height = output->base.current_mode->height;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003005
3006 output->recorder =
Giulio Camuffo954f1832014-10-11 18:27:30 +03003007 create_recorder(b, width, height, "capture.h264");
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003008 if (!output->recorder) {
3009 weston_log("failed to create vaapi recorder\n");
3010 return;
3011 }
3012
Ankit Nautiyal93dde242019-07-08 11:46:42 +05303013 weston_output_disable_planes_incr(&output->base);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003014
3015 output->recorder_frame_listener.notify = recorder_frame_notify;
3016 wl_signal_add(&output->base.frame_signal,
3017 &output->recorder_frame_listener);
3018
3019 weston_output_schedule_repaint(&output->base);
3020
3021 weston_log("[libva recorder] initialized\n");
3022 } else {
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003023 recorder_destroy(output);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003024 }
3025}
3026#else
3027static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003028recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time,
3029 uint32_t key, void *data)
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003030{
3031 weston_log("Compiled without libva support\n");
3032}
3033#endif
3034
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003035
Armin Krezović08368132016-09-30 14:11:05 +02003036static const struct weston_drm_output_api api = {
3037 drm_output_set_mode,
3038 drm_output_set_gbm_format,
3039 drm_output_set_seat,
3040};
3041
Giulio Camuffo954f1832014-10-11 18:27:30 +03003042static struct drm_backend *
3043drm_backend_create(struct weston_compositor *compositor,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003044 struct weston_drm_backend_config *config)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003045{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003046 struct drm_backend *b;
David Herrmann0af066f2012-10-29 19:21:16 +01003047 struct udev_device *drm_device;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003048 struct wl_event_loop *loop;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003049 const char *seat_id = default_seat;
nerdopolisb16c4ac2018-06-29 08:17:46 -04003050 const char *session_seat;
Leandro Ribeiro54293022021-10-12 14:48:36 -03003051 struct weston_drm_format_array *scanout_formats;
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03003052 drmModeRes *res;
Armin Krezović08368132016-09-30 14:11:05 +02003053 int ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003054
nerdopolisb16c4ac2018-06-29 08:17:46 -04003055 session_seat = getenv("XDG_SEAT");
3056 if (session_seat)
3057 seat_id = session_seat;
3058
3059 if (config->seat_id)
3060 seat_id = config->seat_id;
3061
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003062 weston_log("initializing drm backend\n");
3063
Giulio Camuffo954f1832014-10-11 18:27:30 +03003064 b = zalloc(sizeof *b);
3065 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003066 return NULL;
Daniel Stone725c2c32012-06-22 14:04:36 +01003067
Daniel Stone6020f472018-02-05 15:46:20 +00003068 b->state_invalid = true;
leng.fang32af9fc2024-06-13 11:22:15 +08003069 b->allow_modeset = true;
Daniel Stoneefa504f2016-12-19 16:48:20 +00003070 b->drm.fd = -1;
leng.fang9cf09e22024-07-17 20:01:11 +08003071 b->display_enable = 1;
Daniel Stoneefa504f2016-12-19 16:48:20 +00003072
leng.fang32af9fc2024-06-13 11:22:15 +08003073#ifdef BUILD_AML_TV
3074 b->vdin_detect_fd = -1;
3075#endif
3076
Giulio Camuffo954f1832014-10-11 18:27:30 +03003077 b->compositor = compositor;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003078 b->use_pixman = config->use_pixman;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00003079 b->pageflip_timeout = config->pageflip_timeout;
Pekka Paalanendee412d2018-04-23 11:44:58 +02003080 b->use_pixman_shadow = config->use_pixman_shadow;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003081
Leandro Ribeiro172afc22019-12-26 16:23:43 -03003082 b->debug = weston_compositor_add_log_scope(compositor, "drm-backend",
3083 "Debug messages from DRM/KMS backend\n",
3084 NULL, NULL, NULL);
Daniel Stone1cbe1f92018-07-20 10:21:28 +01003085
Pekka Paalanen7da9a382017-08-30 11:29:49 +03003086 compositor->backend = &b->base;
3087
Stefan Agner0bfebeb2019-07-08 00:30:44 +02003088 if (parse_gbm_format(config->gbm_format, DRM_FORMAT_XRGB8888, &b->gbm_format) < 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003089 goto err_compositor;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003090
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003091 /* Check if we run drm-backend using weston-launch */
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003092 compositor->launcher = weston_launcher_connect(compositor, config->tty,
3093 seat_id, true);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003094 if (compositor->launcher == NULL) {
Pekka Paalanena453f4d2017-10-31 10:19:48 +02003095 weston_log("fatal: drm backend should be run using "
3096 "weston-launch binary, or your system should "
3097 "provide the logind D-Bus API.\n");
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003098 goto err_compositor;
3099 }
3100
Giulio Camuffo954f1832014-10-11 18:27:30 +03003101 b->udev = udev_new();
3102 if (b->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003103 weston_log("failed to initialize udev context\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003104 goto err_launcher;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003105 }
3106
Giulio Camuffo954f1832014-10-11 18:27:30 +03003107 b->session_listener.notify = session_notify;
3108 wl_signal_add(&compositor->session_signal, &b->session_listener);
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05003109
Pekka Paalanenb45ed8b2017-03-28 18:04:27 +03003110 if (config->specific_device)
3111 drm_device = open_specific_drm_device(b, config->specific_device);
3112 else
3113 drm_device = find_primary_gpu(b, seat_id);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04003114 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003115 weston_log("no drm device found\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003116 goto err_udev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003117 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003118
Daniel Stoneefa504f2016-12-19 16:48:20 +00003119 if (init_kms_caps(b) < 0) {
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003120 weston_log("failed to initialize kms\n");
3121 goto err_udev_dev;
3122 }
3123
Giulio Camuffo954f1832014-10-11 18:27:30 +03003124 if (b->use_pixman) {
3125 if (init_pixman(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003126 weston_log("failed to initialize pixman renderer\n");
3127 goto err_udev_dev;
3128 }
3129 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +03003130 if (init_egl(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003131 weston_log("failed to initialize egl\n");
3132 goto err_udev_dev;
3133 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003134 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05003135
Giulio Camuffo954f1832014-10-11 18:27:30 +03003136 b->base.destroy = drm_destroy;
Daniel Stoneeedf84c2017-02-10 18:06:04 +00003137 b->base.repaint_begin = drm_repaint_begin;
3138 b->base.repaint_flush = drm_repaint_flush;
3139 b->base.repaint_cancel = drm_repaint_cancel;
Pekka Paalanenc112f002017-08-28 16:27:20 +03003140 b->base.create_output = drm_output_create;
Robert Beckett8d23ab72019-06-13 16:55:44 +01003141 b->base.device_changed = drm_device_changed;
Marius Vlad81bada52019-11-11 00:27:17 +02003142 b->base.can_scanout_dmabuf = drm_can_scanout_dmabuf;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02003143
leng.fang32af9fc2024-06-13 11:22:15 +08003144 drm_add_aml_callback(b);
Bob Ham91880f12016-01-12 10:21:47 +00003145 weston_setup_vt_switch_bindings(compositor);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04003146
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03003147 res = drmModeGetResources(b->drm.fd);
3148 if (!res) {
3149 weston_log("Failed to get drmModeRes\n");
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03003150 goto err_udev_dev;
3151 }
3152
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03003153 wl_list_init(&b->crtc_list);
3154 if (drm_backend_create_crtc_list(b, res) == -1) {
3155 weston_log("Failed to create CRTC list for DRM-backend\n");
3156 goto err_create_crtc_list;
3157 }
3158
Daniel Stone085d2b92015-05-21 00:00:57 +01003159 wl_list_init(&b->plane_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003160 create_sprites(b);
Jesse Barnes58ef3792012-02-23 09:45:49 -05003161
Giulio Camuffo954f1832014-10-11 18:27:30 +03003162 if (udev_input_init(&b->input,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03003163 compositor, b->udev, seat_id,
3164 config->configure_device) < 0) {
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003165 weston_log("failed to create input devices\n");
3166 goto err_sprite;
3167 }
3168
leng.fang32af9fc2024-06-13 11:22:15 +08003169#ifdef ENABLE_DRM_HELP
3170 weston_log("drm start_help_worker \n");
3171 start_help_worker(b->drm.fd, b->atomic_modeset);
3172#endif
3173
leng.fang91856072024-06-07 14:12:54 +08003174#ifdef ENABLE_MODE_POLICY
leng.fang69a93e32024-08-08 16:31:01 +08003175 mode_policy_set_state(NULL, AML_WESTON_HOTPLUG_BOOT, true);
leng.fang91856072024-06-07 14:12:54 +08003176#endif
3177
Leandro Ribeiro96bef052020-09-09 15:23:49 -03003178 wl_list_init(&b->writeback_connector_list);
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03003179 if (drm_backend_discover_connectors(b, drm_device, res) < 0) {
Pekka Paalanend2e62422017-09-08 15:48:07 +03003180 weston_log("Failed to create heads for %s\n", b->drm.filename);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003181 goto err_udev_input;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003182 }
3183
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03003184 drmModeFreeResources(res);
3185
Marius Vlade83e7502019-10-29 17:29:37 +02003186 /* 'compute' faked zpos values in case HW doesn't expose any */
3187 drm_backend_create_faked_zpos(b);
3188
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003189 /* A this point we have some idea of whether or not we have a working
3190 * cursor plane. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003191 if (!b->cursors_are_broken)
3192 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003193
Giulio Camuffo954f1832014-10-11 18:27:30 +03003194 loop = wl_display_get_event_loop(compositor->wl_display);
3195 b->drm_source =
3196 wl_event_loop_add_fd(loop, b->drm.fd,
3197 WL_EVENT_READABLE, on_drm_input, b);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003198
Giulio Camuffo954f1832014-10-11 18:27:30 +03003199 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
3200 if (b->udev_monitor == NULL) {
Abdur Rehman4dca0e12017-01-01 19:46:35 +05003201 weston_log("failed to initialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003202 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003203 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03003204 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003205 "drm", NULL);
leng.fang32af9fc2024-06-13 11:22:15 +08003206#ifdef BUILD_AML_TV
3207 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
3208 "vdin", NULL);
3209 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
3210 "framerate_adapter", NULL);
3211#endif
limin.tian930711f2024-11-08 08:11:08 +00003212 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
3213 "amhdmitx", NULL);
leng.fang32af9fc2024-06-13 11:22:15 +08003214
Giulio Camuffo954f1832014-10-11 18:27:30 +03003215 b->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02003216 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03003217 udev_monitor_get_fd(b->udev_monitor),
3218 WL_EVENT_READABLE, udev_drm_event, b);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003219
Giulio Camuffo954f1832014-10-11 18:27:30 +03003220 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003221 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003222 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003223 }
3224
Daniel Stonea96b93c2012-06-22 14:04:37 +01003225 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003226
Giulio Camuffo954f1832014-10-11 18:27:30 +03003227 weston_compositor_add_debug_binding(compositor, KEY_O,
3228 planes_binding, b);
3229 weston_compositor_add_debug_binding(compositor, KEY_C,
3230 planes_binding, b);
3231 weston_compositor_add_debug_binding(compositor, KEY_V,
3232 planes_binding, b);
3233 weston_compositor_add_debug_binding(compositor, KEY_Q,
3234 recorder_binding, b);
3235 weston_compositor_add_debug_binding(compositor, KEY_W,
3236 renderer_switch_binding, b);
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003237
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003238 if (compositor->renderer->import_dmabuf) {
3239 if (linux_dmabuf_setup(compositor) < 0)
3240 weston_log("Error: initializing dmabuf "
3241 "support failed.\n");
Leandro Ribeiro54293022021-10-12 14:48:36 -03003242 if (compositor->default_dmabuf_feedback) {
3243 /* We were able to create the compositor's default
3244 * dma-buf feedback in the renderer, that means that the
3245 * table was already created and populated with
3246 * renderer's format/modifier pairs. So now we must
3247 * compute the scanout formats indices in the table */
3248 scanout_formats = get_scanout_formats(b);
3249 if (!scanout_formats)
3250 goto err_udev_monitor;
3251 ret = weston_dmabuf_feedback_format_table_set_scanout_indices(compositor->dmabuf_feedback_format_table,
3252 scanout_formats);
3253 weston_drm_format_array_fini(scanout_formats);
Daniel Stonef5086032021-12-06 16:52:19 +00003254 free(scanout_formats);
Leandro Ribeiro54293022021-10-12 14:48:36 -03003255 if (ret < 0)
3256 goto err_udev_monitor;
3257 }
Marius Vladebd10e52019-11-16 19:22:48 +02003258 if (weston_direct_display_setup(compositor) < 0)
3259 weston_log("Error: initializing direct-display "
3260 "support failed.\n");
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003261 }
3262
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003263 if (compositor->capabilities & WESTON_CAP_EXPLICIT_SYNC) {
3264 if (linux_explicit_synchronization_setup(compositor) < 0)
3265 weston_log("Error: initializing explicit "
3266 " synchronization support failed.\n");
3267 }
3268
Ankit Nautiyala344fe32019-05-14 18:36:08 +05303269 if (b->atomic_modeset)
3270 if (weston_compositor_enable_content_protection(compositor) < 0)
3271 weston_log("Error: initializing content-protection "
3272 "support failed.\n");
3273
Armin Krezović08368132016-09-30 14:11:05 +02003274 ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME,
3275 &api, sizeof(api));
3276
3277 if (ret < 0) {
3278 weston_log("Failed to register output API.\n");
3279 goto err_udev_monitor;
3280 }
3281
Stefan Agner3654c672019-07-09 00:50:30 +02003282 ret = drm_backend_init_virtual_output_api(compositor);
Tomohito Esakib1fb00d2018-01-31 17:50:48 +09003283 if (ret < 0) {
3284 weston_log("Failed to register virtual output API.\n");
3285 goto err_udev_monitor;
3286 }
leng.fang91856072024-06-07 14:12:54 +08003287
leng.fang32af9fc2024-06-13 11:22:15 +08003288#ifdef ENABLE_DRM_HELP
leng.fangc0af1fa2024-08-13 17:54:42 +08003289 aml_drm_backend_create(b);
leng.fang32af9fc2024-06-13 11:22:15 +08003290 help_set_printinfo_function(compositor, weston_print_info);
3291#endif
Tomohito Esakib1fb00d2018-01-31 17:50:48 +09003292
leng.fang91856072024-06-07 14:12:54 +08003293#ifdef ENABLE_MODE_POLICY
3294 weston_start_update_env_thread();
3295#endif
3296
Giulio Camuffo954f1832014-10-11 18:27:30 +03003297 return b;
Daniel Stonea96b93c2012-06-22 14:04:37 +01003298
3299err_udev_monitor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003300 wl_event_source_remove(b->udev_drm_source);
3301 udev_monitor_unref(b->udev_monitor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003302err_drm_source:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003303 wl_event_source_remove(b->drm_source);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003304err_udev_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003305 udev_input_destroy(&b->input);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003306err_sprite:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003307 destroy_sprites(b);
Igor Matheus Andrade Torrente63544552020-10-13 11:25:20 -03003308err_create_crtc_list:
3309 drmModeFreeResources(res);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003310err_udev_dev:
3311 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003312err_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003313 udev_unref(b->udev);
Marius Vlad5130a8c2020-07-30 14:47:32 +03003314err_launcher:
3315 weston_launcher_destroy(compositor->launcher);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003316err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003317 weston_compositor_shutdown(compositor);
Marius Vladd171c7b2021-04-01 00:12:00 +03003318#ifdef BUILD_DRM_GBM
3319 if (b->gbm)
3320 gbm_device_destroy(b->gbm);
3321#endif
Giulio Camuffo954f1832014-10-11 18:27:30 +03003322 free(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003323 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003324}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003325
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003326static void
3327config_init_to_defaults(struct weston_drm_backend_config *config)
3328{
Pekka Paalanendee412d2018-04-23 11:44:58 +02003329 config->use_pixman_shadow = true;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003330}
3331
Giulio Camuffo954f1832014-10-11 18:27:30 +03003332WL_EXPORT int
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01003333weston_backend_init(struct weston_compositor *compositor,
3334 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003335{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003336 struct drm_backend *b;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003337 struct weston_drm_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003338
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003339 if (config_base == NULL ||
3340 config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION ||
3341 config_base->struct_size > sizeof(struct weston_drm_backend_config)) {
3342 weston_log("drm backend config structure is invalid\n");
3343 return -1;
3344 }
Benjamin Franzke117483d2011-08-30 11:38:26 +02003345
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003346 config_init_to_defaults(&config);
3347 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003348
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003349 b = drm_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003350 if (b == NULL)
3351 return -1;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003352
Giulio Camuffo954f1832014-10-11 18:27:30 +03003353 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003354}