Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1 | /* |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 2 | * Copyright © 2008-2011 Kristian Høgsberg |
| 3 | * Copyright © 2011 Intel Corporation |
Pekka Paalanen | 925788f | 2018-04-19 14:20:01 +0300 | [diff] [blame] | 4 | * Copyright © 2017, 2018 Collabora, Ltd. |
| 5 | * Copyright © 2017, 2018 General Electric Company |
Pekka Paalanen | 62a9436 | 2018-09-26 14:33:36 +0300 | [diff] [blame] | 6 | * Copyright (c) 2018 DisplayLink (UK) Ltd. |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 7 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 8 | * 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øgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 15 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 16 | * 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øgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 28 | */ |
| 29 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 30 | #include "config.h" |
Kristian Høgsberg | 0b9334a | 2011-04-12 11:34:32 -0400 | [diff] [blame] | 31 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 32 | #include <errno.h> |
Jussi Kukkonen | 649bbce | 2016-07-19 14:16:27 +0300 | [diff] [blame] | 33 | #include <stdint.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 34 | #include <stdlib.h> |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 35 | #include <ctype.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 36 | #include <string.h> |
| 37 | #include <fcntl.h> |
| 38 | #include <unistd.h> |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 39 | #include <linux/input.h> |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 40 | #include <linux/vt.h> |
Ander Conselvan de Oliveira | fd1f4c6 | 2012-06-26 17:09:14 +0300 | [diff] [blame] | 41 | #include <assert.h> |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 42 | #include <sys/mman.h> |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 43 | #include <time.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 44 | |
Benjamin Franzke | c649a92 | 2011-03-02 11:56:04 +0100 | [diff] [blame] | 45 | #include <xf86drm.h> |
| 46 | #include <xf86drmMode.h> |
| 47 | |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 48 | #include <libudev.h> |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 49 | |
Pekka Paalanen | 3d5d947 | 2019-03-28 16:28:47 +0200 | [diff] [blame] | 50 | #include <libweston/libweston.h> |
Pekka Paalanen | 7571027 | 2019-03-29 16:39:12 +0200 | [diff] [blame] | 51 | #include <libweston/backend-drm.h> |
Marius Vlad | c901e89 | 2019-06-21 22:49:18 +0300 | [diff] [blame] | 52 | #include <libweston/weston-log.h> |
Daniel Stone | dd1bc50 | 2019-06-17 12:13:46 +0100 | [diff] [blame] | 53 | #include "drm-internal.h" |
Jon Cruz | 35b2eaa | 2015-06-15 15:37:08 -0700 | [diff] [blame] | 54 | #include "shared/helpers.h" |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 55 | #include "shared/timespec-util.h" |
Ankit Nautiyal | a344fe3 | 2019-05-14 18:36:08 +0530 | [diff] [blame] | 56 | #include "shared/string-helpers.h" |
Pekka Paalanen | 4b301fe | 2021-02-04 17:39:45 +0200 | [diff] [blame] | 57 | #include "shared/weston-drm-fourcc.h" |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 58 | #include "pixman-renderer.h" |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 59 | #include "pixel-formats.h" |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 60 | #include "libbacklight.h" |
Peter Hutterer | 823ad33 | 2014-11-26 07:06:31 +1000 | [diff] [blame] | 61 | #include "libinput-seat.h" |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 62 | #include "launcher-util.h" |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 63 | #include "vaapi-recorder.h" |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 64 | #include "presentation-time-server-protocol.h" |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 65 | #include "linux-dmabuf.h" |
Micah Fedke | c889012 | 2017-02-01 15:28:23 -0500 | [diff] [blame] | 66 | #include "linux-dmabuf-unstable-v1-server-protocol.h" |
Alexandros Frantzis | acff29b | 2018-10-19 12:14:11 +0300 | [diff] [blame] | 67 | #include "linux-explicit-synchronization.h" |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 68 | |
leng.fang | 32af9fc | 2024-06-13 11:22:15 +0800 | [diff] [blame] | 69 | #include "aml-weston/aml-backend.h" |
| 70 | |
Kristian Høgsberg | 98cfea6 | 2013-02-18 16:15:53 -0500 | [diff] [blame] | 71 | static const char default_seat[] = "seat0"; |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 72 | |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 73 | static void |
Marius Vlad | e83e750 | 2019-10-29 17:29:37 +0200 | [diff] [blame] | 74 | drm_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 Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 128 | static int |
| 129 | pageflip_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 */ |
| 147 | static int |
| 148 | drm_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 Borneo | 3957863 | 2019-04-26 23:57:31 +0200 | [diff] [blame] | 160 | weston_log("creating drm pageflip timer failed: %s\n", |
| 161 | strerror(errno)); |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 162 | return -1; |
| 163 | } |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 168 | static void |
| 169 | drm_output_destroy(struct weston_output *output_base); |
| 170 | |
Daniel Stone | 5ff289a | 2017-10-07 12:59:02 +0100 | [diff] [blame] | 171 | /** |
| 172 | * Returns true if the plane can be used on the given output for its current |
| 173 | * repaint cycle. |
| 174 | */ |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 175 | bool |
Daniel Stone | 5ff289a | 2017-10-07 12:59:02 +0100 | [diff] [blame] | 176 | drm_plane_is_available(struct drm_plane *plane, struct drm_output *output) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 177 | { |
Daniel Stone | 5ff289a | 2017-10-07 12:59:02 +0100 | [diff] [blame] | 178 | assert(plane->state_cur); |
| 179 | |
Tomohito Esaki | b1fb00d | 2018-01-31 17:50:48 +0900 | [diff] [blame] | 180 | if (output->virtual) |
| 181 | return false; |
| 182 | |
Daniel Stone | 5ff289a | 2017-10-07 12:59:02 +0100 | [diff] [blame] | 183 | /* 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.fang | 32af9fc | 2024-06-13 11:22:15 +0800 | [diff] [blame] | 188 | if (plane->state_cur->output && plane->state_cur->output != output) { |
| 189 | //plane->state_cur->output = output; |
| 190 | //return false; |
| 191 | } |
Daniel Stone | 5ff289a | 2017-10-07 12:59:02 +0100 | [diff] [blame] | 192 | /* 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 Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 194 | return !!(plane->possible_crtcs & (1 << output->crtc->pipe)); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 195 | } |
| 196 | |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 197 | struct drm_crtc * |
| 198 | drm_crtc_find(struct drm_backend *b, uint32_t crtc_id) |
Daniel Stone | 72c0e1b | 2017-02-09 13:49:15 +0000 | [diff] [blame] | 199 | { |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 200 | struct drm_crtc *crtc; |
Daniel Stone | 72c0e1b | 2017-02-09 13:49:15 +0000 | [diff] [blame] | 201 | |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 202 | wl_list_for_each(crtc, &b->crtc_list, link) { |
| 203 | if (crtc->crtc_id == crtc_id) |
| 204 | return crtc; |
Daniel Stone | 72c0e1b | 2017-02-09 13:49:15 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Daniel Stone | 72c0e1b | 2017-02-09 13:49:15 +0000 | [diff] [blame] | 207 | return NULL; |
| 208 | } |
| 209 | |
Daniel Stone | 4c2fc70 | 2019-06-18 11:12:07 +0100 | [diff] [blame] | 210 | struct drm_head * |
Pekka Paalanen | 54cc47c | 2017-08-31 11:58:41 +0300 | [diff] [blame] | 211 | drm_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 Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 219 | if (head->connector.connector_id == connector_id) |
Pekka Paalanen | 54cc47c | 2017-08-31 11:58:41 +0300 | [diff] [blame] | 220 | return head; |
| 221 | } |
| 222 | |
| 223 | return NULL; |
| 224 | } |
| 225 | |
Leandro Ribeiro | 96bef05 | 2020-09-09 15:23:49 -0300 | [diff] [blame] | 226 | static struct drm_writeback * |
| 227 | drm_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 Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 239 | /** |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 240 | * 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 | */ |
| 249 | static struct drm_output_state * |
| 250 | drm_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 Nautiyal | a344fe3 | 2019-05-14 18:36:08 +0530 | [diff] [blame] | 260 | output_state->protection = WESTON_HDCP_DISABLE; |
| 261 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 262 | return output_state; |
| 263 | } |
| 264 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 265 | |
| 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 Stone | 4c2fc70 | 2019-06-18 11:12:07 +0100 | [diff] [blame] | 271 | void |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 272 | drm_output_update_complete(struct drm_output *output, uint32_t flags, |
| 273 | unsigned int sec, unsigned int usec) |
| 274 | { |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 275 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 276 | struct drm_plane_state *ps; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 277 | 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 Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 283 | wl_list_for_each(ps, &output->state_cur->plane_list, link) |
| 284 | ps->complete = true; |
| 285 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 286 | drm_output_state_free(output->state_last); |
| 287 | output->state_last = NULL; |
| 288 | |
| 289 | if (output->destroy_pending) { |
Emmanuel Gil Peyrot | 1b3ad09 | 2019-12-09 02:50:55 +0100 | [diff] [blame] | 290 | output->destroy_pending = false; |
| 291 | output->disable_pending = false; |
| 292 | output->dpms_off_pending = false; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 293 | drm_output_destroy(&output->base); |
| 294 | return; |
| 295 | } else if (output->disable_pending) { |
Emmanuel Gil Peyrot | 1b3ad09 | 2019-12-09 02:50:55 +0100 | [diff] [blame] | 296 | output->disable_pending = false; |
| 297 | output->dpms_off_pending = false; |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 298 | weston_output_disable(&output->base); |
| 299 | return; |
| 300 | } else if (output->dpms_off_pending) { |
| 301 | struct drm_pending_state *pending = drm_pending_state_alloc(b); |
Emmanuel Gil Peyrot | 1b3ad09 | 2019-12-09 02:50:55 +0100 | [diff] [blame] | 302 | output->dpms_off_pending = false; |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 303 | drm_output_get_disable_state(pending, output); |
| 304 | drm_pending_state_apply_sync(pending); |
Michael Olbrich | d70e712 | 2020-08-06 09:57:54 +0200 | [diff] [blame] | 305 | } |
| 306 | if (output->state_cur->dpms == WESTON_DPMS_OFF && |
| 307 | output->base.repaint_status != REPAINT_AWAITING_COMPLETION) { |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 308 | /* DPMS can happen to us either in the middle of a repaint |
| 309 | * cycle (when we have painted fresh content, only to throw it |
| 310 | * away for DPMS off), or at any other random point. If the |
| 311 | * latter is true, then we cannot go through finish_frame, |
| 312 | * because the repaint machinery does not expect this. */ |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 313 | return; |
| 314 | } |
| 315 | |
| 316 | ts.tv_sec = sec; |
| 317 | ts.tv_nsec = usec * 1000; |
Derek Foreman | 74bdb35 | 2021-07-29 09:02:04 -0500 | [diff] [blame] | 318 | |
| 319 | if (output->state_cur->dpms != WESTON_DPMS_OFF) |
| 320 | weston_output_finish_frame(&output->base, &ts, flags); |
| 321 | else |
| 322 | weston_output_finish_frame(&output->base, NULL, |
| 323 | WP_PRESENTATION_FEEDBACK_INVALID); |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 324 | |
| 325 | /* We can't call this from frame_notify, because the output's |
| 326 | * repaint needed flag is cleared just after that */ |
| 327 | if (output->recorder) |
| 328 | weston_output_schedule_repaint(&output->base); |
| 329 | } |
| 330 | |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 331 | static struct drm_fb * |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 332 | drm_output_render_pixman(struct drm_output_state *state, |
| 333 | pixman_region32_t *damage) |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 334 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 335 | struct drm_output *output = state->output; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 336 | struct weston_compositor *ec = output->base.compositor; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 337 | |
| 338 | output->current_image ^= 1; |
| 339 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 340 | pixman_renderer_output_set_buffer(&output->base, |
| 341 | output->image[output->current_image]); |
Pekka Paalanen | acf50c3 | 2018-04-23 11:44:56 +0200 | [diff] [blame] | 342 | pixman_renderer_output_set_hw_extra_damage(&output->base, |
| 343 | &output->previous_damage); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 344 | |
Pekka Paalanen | acf50c3 | 2018-04-23 11:44:56 +0200 | [diff] [blame] | 345 | ec->renderer->repaint_output(&output->base, damage); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 346 | |
Pekka Paalanen | acf50c3 | 2018-04-23 11:44:56 +0200 | [diff] [blame] | 347 | pixman_region32_copy(&output->previous_damage, damage); |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 348 | |
| 349 | return drm_fb_ref(output->dumb[output->current_image]); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 350 | } |
| 351 | |
Stefan Agner | 3654c67 | 2019-07-09 00:50:30 +0200 | [diff] [blame] | 352 | void |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 353 | drm_output_render(struct drm_output_state *state, pixman_region32_t *damage) |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 354 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 355 | struct drm_output *output = state->output; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 356 | struct weston_compositor *c = output->base.compositor; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 357 | struct drm_plane_state *scanout_state; |
Daniel Stone | e95169b | 2016-11-14 17:46:59 +0000 | [diff] [blame] | 358 | struct drm_plane *scanout_plane = output->scanout_plane; |
Scott Anderson | 15c603c | 2020-06-02 17:39:43 +1200 | [diff] [blame] | 359 | struct drm_property_info *damage_info = |
| 360 | &scanout_plane->props[WDRM_PLANE_FB_DAMAGE_CLIPS]; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 361 | struct drm_backend *b = to_drm_backend(c); |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 362 | struct drm_fb *fb; |
Scott Anderson | 15c603c | 2020-06-02 17:39:43 +1200 | [diff] [blame] | 363 | pixman_region32_t scanout_damage; |
| 364 | pixman_box32_t *rects; |
| 365 | int n_rects; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 366 | |
Daniel Stone | 4e84f7d | 2017-04-04 17:54:29 +0100 | [diff] [blame] | 367 | /* If we already have a client buffer promoted to scanout, then we don't |
| 368 | * want to render. */ |
Leandro Ribeiro | 05cecc8 | 2020-08-13 17:34:58 -0300 | [diff] [blame] | 369 | scanout_state = drm_output_state_get_plane(state, scanout_plane); |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 370 | if (scanout_state->fb) |
Daniel Stone | 4e84f7d | 2017-04-04 17:54:29 +0100 | [diff] [blame] | 371 | return; |
| 372 | |
Daniel Stone | 98d75e1 | 2020-03-06 11:03:14 +0000 | [diff] [blame] | 373 | /* |
| 374 | * If we don't have any damage on the primary plane, and we already |
| 375 | * have a renderer buffer active, we can reuse it; else we pass |
| 376 | * the damaged region into the renderer to re-render the affected |
Pekka Paalanen | 7f1a113 | 2020-12-07 14:59:44 +0200 | [diff] [blame] | 377 | * area. But, we still have to call the renderer anyway if any screen |
| 378 | * capture is pending, otherwise the capture will not complete. |
Daniel Stone | 98d75e1 | 2020-03-06 11:03:14 +0000 | [diff] [blame] | 379 | */ |
Daniel Stone | e95169b | 2016-11-14 17:46:59 +0000 | [diff] [blame] | 380 | if (!pixman_region32_not_empty(damage) && |
Pekka Paalanen | 7f1a113 | 2020-12-07 14:59:44 +0200 | [diff] [blame] | 381 | wl_list_empty(&output->base.frame_signal.listener_list) && |
Daniel Stone | e95169b | 2016-11-14 17:46:59 +0000 | [diff] [blame] | 382 | scanout_plane->state_cur->fb && |
| 383 | (scanout_plane->state_cur->fb->type == BUFFER_GBM_SURFACE || |
Daniel Stone | 98d75e1 | 2020-03-06 11:03:14 +0000 | [diff] [blame] | 384 | scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB)) { |
Daniel Stone | e95169b | 2016-11-14 17:46:59 +0000 | [diff] [blame] | 385 | fb = drm_fb_ref(scanout_plane->state_cur->fb); |
| 386 | } else if (b->use_pixman) { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 387 | fb = drm_output_render_pixman(state, damage); |
Daniel Stone | e95169b | 2016-11-14 17:46:59 +0000 | [diff] [blame] | 388 | } else { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 389 | fb = drm_output_render_gl(state, damage); |
Daniel Stone | e95169b | 2016-11-14 17:46:59 +0000 | [diff] [blame] | 390 | } |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 391 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 392 | if (!fb) { |
| 393 | drm_plane_state_put_back(scanout_state); |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 394 | return; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | scanout_state->fb = fb; |
| 398 | scanout_state->output = output; |
| 399 | |
| 400 | scanout_state->src_x = 0; |
| 401 | scanout_state->src_y = 0; |
Daniel Stone | f9a6162 | 2020-03-06 11:07:15 +0000 | [diff] [blame] | 402 | scanout_state->src_w = fb->width << 16; |
| 403 | scanout_state->src_h = fb->height << 16; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 404 | |
| 405 | scanout_state->dest_x = 0; |
| 406 | scanout_state->dest_y = 0; |
Daniel Stone | f9a6162 | 2020-03-06 11:07:15 +0000 | [diff] [blame] | 407 | scanout_state->dest_w = output->base.current_mode->width; |
| 408 | scanout_state->dest_h = output->base.current_mode->height; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 409 | |
leng.fang | 32af9fc | 2024-06-13 11:22:15 +0800 | [diff] [blame] | 410 | drm_output_render_aml_config(output, scanout_state); |
| 411 | |
Scott Anderson | 15c603c | 2020-06-02 17:39:43 +1200 | [diff] [blame] | 412 | pixman_region32_subtract(&c->primary_plane.damage, |
| 413 | &c->primary_plane.damage, damage); |
| 414 | |
| 415 | /* Don't bother calculating plane damage if the plane doesn't support it */ |
| 416 | if (damage_info->prop_id == 0) |
| 417 | return; |
| 418 | |
| 419 | pixman_region32_init(&scanout_damage); |
| 420 | pixman_region32_copy(&scanout_damage, damage); |
| 421 | |
Deepak Rawat | 46a1c72 | 2018-07-24 14:13:34 -0700 | [diff] [blame] | 422 | if (output->base.zoom.active) { |
Derek Foreman | db7e85d | 2022-01-20 10:27:17 -0600 | [diff] [blame] | 423 | pixman_region32_t clip; |
| 424 | |
Scott Anderson | 15c603c | 2020-06-02 17:39:43 +1200 | [diff] [blame] | 425 | weston_matrix_transform_region(&scanout_damage, |
Deepak Rawat | 46a1c72 | 2018-07-24 14:13:34 -0700 | [diff] [blame] | 426 | &output->base.matrix, |
Scott Anderson | 15c603c | 2020-06-02 17:39:43 +1200 | [diff] [blame] | 427 | &scanout_damage); |
Derek Foreman | db7e85d | 2022-01-20 10:27:17 -0600 | [diff] [blame] | 428 | pixman_region32_init_rect(&clip, 0, 0, |
| 429 | output->base.width, |
| 430 | output->base.height); |
| 431 | pixman_region32_intersect(&scanout_damage, &scanout_damage, &clip); |
| 432 | pixman_region32_fini(&clip); |
Deepak Rawat | 46a1c72 | 2018-07-24 14:13:34 -0700 | [diff] [blame] | 433 | } else { |
Scott Anderson | 15c603c | 2020-06-02 17:39:43 +1200 | [diff] [blame] | 434 | pixman_region32_translate(&scanout_damage, |
Deepak Rawat | 46a1c72 | 2018-07-24 14:13:34 -0700 | [diff] [blame] | 435 | -output->base.x, -output->base.y); |
| 436 | weston_transformed_region(output->base.width, |
| 437 | output->base.height, |
| 438 | output->base.transform, |
| 439 | output->base.current_scale, |
Scott Anderson | 15c603c | 2020-06-02 17:39:43 +1200 | [diff] [blame] | 440 | &scanout_damage, |
| 441 | &scanout_damage); |
Deepak Rawat | 46a1c72 | 2018-07-24 14:13:34 -0700 | [diff] [blame] | 442 | } |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 443 | |
Scott Anderson | 15c603c | 2020-06-02 17:39:43 +1200 | [diff] [blame] | 444 | assert(scanout_state->damage_blob_id == 0); |
| 445 | |
| 446 | rects = pixman_region32_rectangles(&scanout_damage, &n_rects); |
| 447 | |
| 448 | /* |
| 449 | * If this function fails, the blob id should still be 0. |
| 450 | * This tells the kernel there is no damage information, which means |
| 451 | * that it will consider the whole plane damaged. While this may |
| 452 | * affect efficiency, it should still produce correct results. |
| 453 | */ |
| 454 | drmModeCreatePropertyBlob(b->drm.fd, rects, |
| 455 | sizeof(*rects) * n_rects, |
| 456 | &scanout_state->damage_blob_id); |
| 457 | |
| 458 | pixman_region32_fini(&scanout_damage); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 459 | } |
| 460 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 461 | static int |
| 462 | drm_output_repaint(struct weston_output *output_base, |
| 463 | pixman_region32_t *damage, |
| 464 | void *repaint_data) |
| 465 | { |
| 466 | struct drm_pending_state *pending_state = repaint_data; |
| 467 | struct drm_output *output = to_drm_output(output_base); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 468 | struct drm_output_state *state = NULL; |
| 469 | struct drm_plane_state *scanout_state; |
| 470 | |
Tomohito Esaki | b1fb00d | 2018-01-31 17:50:48 +0900 | [diff] [blame] | 471 | assert(!output->virtual); |
| 472 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 473 | if (output->disable_pending || output->destroy_pending) |
| 474 | goto err; |
| 475 | |
| 476 | assert(!output->state_last); |
| 477 | |
| 478 | /* If planes have been disabled in the core, we might not have |
| 479 | * hit assign_planes at all, so might not have valid output state |
| 480 | * here. */ |
| 481 | state = drm_pending_state_get_output(pending_state, output); |
| 482 | if (!state) |
| 483 | state = drm_output_state_duplicate(output->state_cur, |
| 484 | pending_state, |
| 485 | DRM_OUTPUT_STATE_CLEAR_PLANES); |
| 486 | state->dpms = WESTON_DPMS_ON; |
| 487 | |
Ankit Nautiyal | a344fe3 | 2019-05-14 18:36:08 +0530 | [diff] [blame] | 488 | if (output_base->allow_protection) |
| 489 | state->protection = output_base->desired_protection; |
| 490 | else |
| 491 | state->protection = WESTON_HDCP_DISABLE; |
| 492 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 493 | drm_output_render(state, damage); |
| 494 | scanout_state = drm_output_state_get_plane(state, |
| 495 | output->scanout_plane); |
| 496 | if (!scanout_state || !scanout_state->fb) |
| 497 | goto err; |
| 498 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 499 | return 0; |
| 500 | |
| 501 | err: |
| 502 | drm_output_state_free(state); |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 503 | return -1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 504 | } |
| 505 | |
Daniel Stone | 4c2fc70 | 2019-06-18 11:12:07 +0100 | [diff] [blame] | 506 | /* Determine the type of vblank synchronization to use for the output. |
| 507 | * |
| 508 | * The pipe parameter indicates which CRTC is in use. Knowing this, we |
| 509 | * can determine which vblank sequence type to use for it. Traditional |
| 510 | * cards had only two CRTCs, with CRTC 0 using no special flags, and |
| 511 | * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe |
| 512 | * parameter indicates this. |
| 513 | * |
| 514 | * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between |
| 515 | * 0-31. If this is non-zero it indicates we're dealing with a |
| 516 | * multi-gpu situation and we need to calculate the vblank sync |
| 517 | * using DRM_BLANK_HIGH_CRTC_MASK. |
| 518 | */ |
| 519 | static unsigned int |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 520 | drm_waitvblank_pipe(struct drm_crtc *crtc) |
Daniel Stone | 4c2fc70 | 2019-06-18 11:12:07 +0100 | [diff] [blame] | 521 | { |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 522 | if (crtc->pipe > 1) |
| 523 | return (crtc->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) & |
Daniel Stone | 4c2fc70 | 2019-06-18 11:12:07 +0100 | [diff] [blame] | 524 | DRM_VBLANK_HIGH_CRTC_MASK; |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 525 | else if (crtc->pipe > 0) |
Daniel Stone | 4c2fc70 | 2019-06-18 11:12:07 +0100 | [diff] [blame] | 526 | return DRM_VBLANK_SECONDARY; |
| 527 | else |
| 528 | return 0; |
| 529 | } |
| 530 | |
Antonio Borneo | c90fccc | 2019-06-30 15:51:10 +0200 | [diff] [blame] | 531 | static int |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 532 | drm_output_start_repaint_loop(struct weston_output *output_base) |
| 533 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 534 | struct drm_output *output = to_drm_output(output_base); |
Daniel Stone | 8747f95 | 2016-11-29 20:17:32 +0000 | [diff] [blame] | 535 | struct drm_pending_state *pending_state; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 536 | struct drm_plane *scanout_plane = output->scanout_plane; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 537 | struct drm_backend *backend = |
| 538 | to_drm_backend(output_base->compositor); |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 539 | struct timespec ts, tnow; |
| 540 | struct timespec vbl2now; |
| 541 | int64_t refresh_nsec; |
| 542 | int ret; |
| 543 | drmVBlank vbl = { |
| 544 | .request.type = DRM_VBLANK_RELATIVE, |
| 545 | .request.sequence = 0, |
| 546 | .request.signal = 0, |
| 547 | }; |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 548 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 549 | if (output->disable_pending || output->destroy_pending) |
Antonio Borneo | c90fccc | 2019-06-30 15:51:10 +0200 | [diff] [blame] | 550 | return 0; |
Xiong Zhang | abd5d47 | 2013-10-11 14:43:07 +0800 | [diff] [blame] | 551 | |
Leandro Ribeiro | 05cecc8 | 2020-08-13 17:34:58 -0300 | [diff] [blame] | 552 | if (!scanout_plane->state_cur->fb) { |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 553 | /* We can't page flip if there's no mode set */ |
David Herrmann | 3c688c5 | 2013-10-22 17:11:25 +0200 | [diff] [blame] | 554 | goto finish_frame; |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 555 | } |
| 556 | |
Pekka Paalanen | 6b65d8f | 2017-07-27 13:44:32 +0300 | [diff] [blame] | 557 | /* Need to smash all state in from scratch; current timings might not |
| 558 | * be what we want, page flip might not work, etc. |
| 559 | */ |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 560 | if (backend->state_invalid) |
Pekka Paalanen | 6b65d8f | 2017-07-27 13:44:32 +0300 | [diff] [blame] | 561 | goto finish_frame; |
| 562 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 563 | assert(scanout_plane->state_cur->output == output); |
| 564 | |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 565 | /* Try to get current msc and timestamp via instant query */ |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 566 | vbl.request.type |= drm_waitvblank_pipe(output->crtc); |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 567 | ret = drmWaitVBlank(backend->drm.fd, &vbl); |
| 568 | |
| 569 | /* Error ret or zero timestamp means failure to get valid timestamp */ |
| 570 | if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) { |
| 571 | ts.tv_sec = vbl.reply.tval_sec; |
| 572 | ts.tv_nsec = vbl.reply.tval_usec * 1000; |
| 573 | |
| 574 | /* Valid timestamp for most recent vblank - not stale? |
| 575 | * Stale ts could happen on Linux 3.17+, so make sure it |
| 576 | * is not older than 1 refresh duration since now. |
| 577 | */ |
| 578 | weston_compositor_read_presentation_clock(backend->compositor, |
| 579 | &tnow); |
| 580 | timespec_sub(&vbl2now, &tnow, &ts); |
| 581 | refresh_nsec = |
| 582 | millihz_to_nsec(output->base.current_mode->refresh); |
| 583 | if (timespec_to_nsec(&vbl2now) < refresh_nsec) { |
| 584 | drm_output_update_msc(output, vbl.reply.sequence); |
| 585 | weston_output_finish_frame(output_base, &ts, |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 586 | WP_PRESENTATION_FEEDBACK_INVALID); |
Antonio Borneo | c90fccc | 2019-06-30 15:51:10 +0200 | [diff] [blame] | 587 | return 0; |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | |
| 591 | /* Immediate query didn't provide valid timestamp. |
| 592 | * Use pageflip fallback. |
| 593 | */ |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 594 | |
Daniel Stone | 205c0a0 | 2017-04-04 17:54:33 +0100 | [diff] [blame] | 595 | assert(!output->page_flip_pending); |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 596 | assert(!output->state_last); |
| 597 | |
| 598 | pending_state = drm_pending_state_alloc(backend); |
Daniel Stone | 8747f95 | 2016-11-29 20:17:32 +0000 | [diff] [blame] | 599 | drm_output_state_duplicate(output->state_cur, pending_state, |
| 600 | DRM_OUTPUT_STATE_PRESERVE_PLANES); |
Daniel Stone | 205c0a0 | 2017-04-04 17:54:33 +0100 | [diff] [blame] | 601 | |
Daniel Stone | 8747f95 | 2016-11-29 20:17:32 +0000 | [diff] [blame] | 602 | ret = drm_pending_state_apply(pending_state); |
| 603 | if (ret != 0) { |
Antonio Borneo | 3957863 | 2019-04-26 23:57:31 +0200 | [diff] [blame] | 604 | weston_log("applying repaint-start state failed: %s\n", |
| 605 | strerror(errno)); |
Antonio Borneo | c90fccc | 2019-06-30 15:51:10 +0200 | [diff] [blame] | 606 | if (ret == -EACCES) |
| 607 | return -1; |
David Herrmann | 3c688c5 | 2013-10-22 17:11:25 +0200 | [diff] [blame] | 608 | goto finish_frame; |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 609 | } |
David Herrmann | 3c688c5 | 2013-10-22 17:11:25 +0200 | [diff] [blame] | 610 | |
Antonio Borneo | c90fccc | 2019-06-30 15:51:10 +0200 | [diff] [blame] | 611 | return 0; |
David Herrmann | 3c688c5 | 2013-10-22 17:11:25 +0200 | [diff] [blame] | 612 | |
| 613 | finish_frame: |
| 614 | /* if we cannot page-flip, immediately finish frame */ |
Daniel Stone | 3615ce1 | 2017-03-01 11:34:05 +0000 | [diff] [blame] | 615 | weston_output_finish_frame(output_base, NULL, |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 616 | WP_PRESENTATION_FEEDBACK_INVALID); |
Antonio Borneo | c90fccc | 2019-06-30 15:51:10 +0200 | [diff] [blame] | 617 | return 0; |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 618 | } |
| 619 | |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 620 | /** |
| 621 | * Begin a new repaint cycle |
| 622 | * |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 623 | * Called by the core compositor at the beginning of a repaint cycle. Creates |
| 624 | * a new pending_state structure to own any output state created by individual |
| 625 | * output repaint functions until the repaint is flushed or cancelled. |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 626 | */ |
| 627 | static void * |
| 628 | drm_repaint_begin(struct weston_compositor *compositor) |
| 629 | { |
| 630 | struct drm_backend *b = to_drm_backend(compositor); |
| 631 | struct drm_pending_state *ret; |
| 632 | |
| 633 | ret = drm_pending_state_alloc(b); |
| 634 | b->repaint_data = ret; |
| 635 | |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 636 | if (weston_log_scope_is_enabled(b->debug)) { |
Daniel Stone | 1cbe1f9 | 2018-07-20 10:21:28 +0100 | [diff] [blame] | 637 | char *dbg = weston_compositor_print_scene_graph(compositor); |
| 638 | drm_debug(b, "[repaint] Beginning repaint; pending_state %p\n", |
| 639 | ret); |
| 640 | drm_debug(b, "%s", dbg); |
| 641 | free(dbg); |
| 642 | } |
| 643 | |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 644 | return ret; |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * Flush a repaint set |
| 649 | * |
| 650 | * Called by the core compositor when a repaint cycle has been completed |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 651 | * and should be flushed. Frees the pending state, transitioning ownership |
| 652 | * of the output state from the pending state, to the update itself. When |
| 653 | * the update completes (see drm_output_update_complete), the output |
| 654 | * state will be freed. |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 655 | */ |
Antonio Borneo | c90fccc | 2019-06-30 15:51:10 +0200 | [diff] [blame] | 656 | static int |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 657 | drm_repaint_flush(struct weston_compositor *compositor, void *repaint_data) |
| 658 | { |
| 659 | struct drm_backend *b = to_drm_backend(compositor); |
| 660 | struct drm_pending_state *pending_state = repaint_data; |
Antonio Borneo | c90fccc | 2019-06-30 15:51:10 +0200 | [diff] [blame] | 661 | int ret; |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 662 | |
Antonio Borneo | c90fccc | 2019-06-30 15:51:10 +0200 | [diff] [blame] | 663 | ret = drm_pending_state_apply(pending_state); |
| 664 | if (ret != 0) |
| 665 | weston_log("repaint-flush failed: %s\n", strerror(errno)); |
| 666 | |
Daniel Stone | 1cbe1f9 | 2018-07-20 10:21:28 +0100 | [diff] [blame] | 667 | drm_debug(b, "[repaint] flushed pending_state %p\n", pending_state); |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 668 | b->repaint_data = NULL; |
leng.fang | 32af9fc | 2024-06-13 11:22:15 +0800 | [diff] [blame] | 669 | #ifdef ENABLE_DRM_HELP |
| 670 | help_do_repaint_cycle_completed(); |
| 671 | #endif |
Antonio Borneo | c90fccc | 2019-06-30 15:51:10 +0200 | [diff] [blame] | 672 | |
| 673 | return (ret == -EACCES) ? -1 : 0; |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | /** |
| 677 | * Cancel a repaint set |
| 678 | * |
| 679 | * Called by the core compositor when a repaint has finished, so the data |
| 680 | * held across the repaint cycle should be discarded. |
| 681 | */ |
| 682 | static void |
| 683 | drm_repaint_cancel(struct weston_compositor *compositor, void *repaint_data) |
| 684 | { |
| 685 | struct drm_backend *b = to_drm_backend(compositor); |
| 686 | struct drm_pending_state *pending_state = repaint_data; |
| 687 | |
| 688 | drm_pending_state_free(pending_state); |
Daniel Stone | 1cbe1f9 | 2018-07-20 10:21:28 +0100 | [diff] [blame] | 689 | drm_debug(b, "[repaint] cancel pending_state %p\n", pending_state); |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 690 | b->repaint_data = NULL; |
| 691 | } |
| 692 | |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 693 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 694 | drm_output_init_pixman(struct drm_output *output, struct drm_backend *b); |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 695 | static void |
| 696 | drm_output_fini_pixman(struct drm_output *output); |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 697 | |
| 698 | static int |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 699 | drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode) |
| 700 | { |
Daniel Stone | 02d487a | 2017-10-07 14:01:45 +0100 | [diff] [blame] | 701 | struct drm_output *output = to_drm_output(output_base); |
| 702 | struct drm_backend *b = to_drm_backend(output_base->compositor); |
Daniel Stone | fbe6c1d | 2019-06-17 16:04:26 +0100 | [diff] [blame] | 703 | struct drm_mode *drm_mode = drm_output_choose_mode(output, mode); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 704 | |
| 705 | if (!drm_mode) { |
Daniel Stone | 02d487a | 2017-10-07 14:01:45 +0100 | [diff] [blame] | 706 | weston_log("%s: invalid resolution %dx%d\n", |
| 707 | output_base->name, mode->width, mode->height); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 708 | return -1; |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 709 | } |
| 710 | |
limin.tian | c616dad | 2024-07-15 11:35:38 +0000 | [diff] [blame^] | 711 | if (&drm_mode->base == output->base.current_mode) { |
| 712 | weston_log("\n drm_output_switch_mode same mode return \n"); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 713 | return 0; |
limin.tian | c616dad | 2024-07-15 11:35:38 +0000 | [diff] [blame^] | 714 | } |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 715 | output->base.current_mode->flags = 0; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 716 | |
leng.fang | 32af9fc | 2024-06-13 11:22:15 +0800 | [diff] [blame] | 717 | drm_switch_mode_aml_config_ui_size(output, output_base, drm_mode); |
| 718 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 719 | output->base.current_mode = &drm_mode->base; |
| 720 | output->base.current_mode->flags = |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 721 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 722 | |
leng.fang | 32af9fc | 2024-06-13 11:22:15 +0800 | [diff] [blame] | 723 | drm_switch_mode_aml_config_help(output, output_base, drm_mode); |
| 724 | |
Daniel Stone | f30a18c | 2017-04-04 17:54:31 +0100 | [diff] [blame] | 725 | /* XXX: This drops our current buffer too early, before we've started |
| 726 | * displaying it. Ideally this should be much more atomic and |
| 727 | * integrated with a full repaint cycle, rather than doing a |
| 728 | * sledgehammer modeswitch first, and only later showing new |
| 729 | * content. |
| 730 | */ |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 731 | b->state_invalid = true; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 732 | |
leng.fang | 32af9fc | 2024-06-13 11:22:15 +0800 | [diff] [blame] | 733 | b->allow_modeset = true; |
| 734 | #ifdef MESON_DRM_FIX_UI_SIZE |
| 735 | // fixed ui mode needn't reset egl. |
| 736 | return 0; |
| 737 | #endif |
| 738 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 739 | if (b->use_pixman) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 740 | drm_output_fini_pixman(output); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 741 | if (drm_output_init_pixman(output, b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 742 | weston_log("failed to init output pixman state with " |
| 743 | "new mode\n"); |
| 744 | return -1; |
| 745 | } |
| 746 | } else { |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 747 | drm_output_fini_egl(output); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 748 | if (drm_output_init_egl(output, b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 749 | weston_log("failed to init output egl state with " |
| 750 | "new mode"); |
| 751 | return -1; |
| 752 | } |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 753 | } |
| 754 | |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 755 | return 0; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 756 | } |
| 757 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 758 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 759 | init_pixman(struct drm_backend *b) |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 760 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 761 | return pixman_renderer_init(b->compositor); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 762 | } |
| 763 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 764 | /** |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 765 | * Create a drm_plane for a hardware plane |
| 766 | * |
| 767 | * Creates one drm_plane structure for a hardware plane, and initialises its |
| 768 | * properties and formats. |
| 769 | * |
| 770 | * This function does not add the plane to the list of usable planes in Weston |
| 771 | * itself; the caller is responsible for this. |
| 772 | * |
| 773 | * Call drm_plane_destroy to clean up the plane. |
| 774 | * |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 775 | * @sa drm_output_find_special_plane |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 776 | * @param b DRM compositor backend |
Igor Matheus Andrade Torrente | bfcb1ad | 2020-10-12 13:37:07 -0300 | [diff] [blame] | 777 | * @param kplane DRM plane to create |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 778 | */ |
| 779 | static struct drm_plane * |
Igor Matheus Andrade Torrente | bfcb1ad | 2020-10-12 13:37:07 -0300 | [diff] [blame] | 780 | drm_plane_create(struct drm_backend *b, const drmModePlane *kplane) |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 781 | { |
| 782 | struct drm_plane *plane; |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 783 | drmModeObjectProperties *props; |
Marius Vlad | cdd6fa2 | 2019-08-29 20:42:00 +0300 | [diff] [blame] | 784 | uint64_t *zpos_range_values; |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 785 | |
Scott Anderson | 7466309 | 2021-02-01 15:46:33 -0300 | [diff] [blame] | 786 | plane = zalloc(sizeof(*plane)); |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 787 | if (!plane) { |
| 788 | weston_log("%s: out of memory\n", __func__); |
| 789 | return NULL; |
| 790 | } |
| 791 | |
| 792 | plane->backend = b; |
Daniel Stone | 57d609a | 2021-11-16 18:56:09 +0000 | [diff] [blame] | 793 | plane->plane_idx = b->next_plane_idx++; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 794 | plane->state_cur = drm_plane_state_alloc(NULL, plane); |
| 795 | plane->state_cur->complete = true; |
Igor Matheus Andrade Torrente | bfcb1ad | 2020-10-12 13:37:07 -0300 | [diff] [blame] | 796 | plane->possible_crtcs = kplane->possible_crtcs; |
| 797 | plane->plane_id = kplane->plane_id; |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 798 | |
Scott Anderson | 7466309 | 2021-02-01 15:46:33 -0300 | [diff] [blame] | 799 | weston_drm_format_array_init(&plane->formats); |
| 800 | |
Igor Matheus Andrade Torrente | bfcb1ad | 2020-10-12 13:37:07 -0300 | [diff] [blame] | 801 | props = drmModeObjectGetProperties(b->drm.fd, kplane->plane_id, |
| 802 | DRM_MODE_OBJECT_PLANE); |
| 803 | if (!props) { |
| 804 | weston_log("couldn't get plane properties\n"); |
| 805 | goto err; |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 806 | } |
Igor Matheus Andrade Torrente | bfcb1ad | 2020-10-12 13:37:07 -0300 | [diff] [blame] | 807 | |
| 808 | drm_property_info_populate(b, plane_props, plane->props, |
| 809 | WDRM_PLANE__COUNT, props); |
| 810 | plane->type = |
| 811 | drm_property_get_value(&plane->props[WDRM_PLANE_TYPE], |
| 812 | props, |
| 813 | WDRM_PLANE_TYPE__COUNT); |
| 814 | |
| 815 | zpos_range_values = |
| 816 | drm_property_get_range_values(&plane->props[WDRM_PLANE_ZPOS], |
| 817 | props); |
| 818 | |
| 819 | if (zpos_range_values) { |
| 820 | plane->zpos_min = zpos_range_values[0]; |
| 821 | plane->zpos_max = zpos_range_values[1]; |
| 822 | } else { |
Marius Vlad | cdd6fa2 | 2019-08-29 20:42:00 +0300 | [diff] [blame] | 823 | plane->zpos_min = DRM_PLANE_ZPOS_INVALID_PLANE; |
Igor Matheus Andrade Torrente | bfcb1ad | 2020-10-12 13:37:07 -0300 | [diff] [blame] | 824 | plane->zpos_max = DRM_PLANE_ZPOS_INVALID_PLANE; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 825 | } |
| 826 | |
Igor Matheus Andrade Torrente | bfcb1ad | 2020-10-12 13:37:07 -0300 | [diff] [blame] | 827 | if (drm_plane_populate_formats(plane, kplane, props, |
| 828 | b->fb_modifiers) < 0) { |
| 829 | drmModeFreeObjectProperties(props); |
| 830 | goto err; |
| 831 | } |
| 832 | |
| 833 | drmModeFreeObjectProperties(props); |
| 834 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 835 | if (plane->type == WDRM_PLANE_TYPE__COUNT) |
| 836 | goto err_props; |
| 837 | |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 838 | weston_plane_init(&plane->base, b->compositor, 0, 0); |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 839 | wl_list_insert(&b->plane_list, &plane->link); |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 840 | |
| 841 | return plane; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 842 | |
| 843 | err_props: |
| 844 | drm_property_info_free(plane->props, WDRM_PLANE__COUNT); |
| 845 | err: |
Scott Anderson | 7466309 | 2021-02-01 15:46:33 -0300 | [diff] [blame] | 846 | weston_drm_format_array_fini(&plane->formats); |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 847 | drm_plane_state_free(plane->state_cur, true); |
| 848 | free(plane); |
| 849 | return NULL; |
| 850 | } |
| 851 | |
| 852 | /** |
| 853 | * Find, or create, a special-purpose plane |
| 854 | * |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 855 | * @param b DRM backend |
| 856 | * @param output Output to use for plane |
| 857 | * @param type Type of plane |
| 858 | */ |
| 859 | static struct drm_plane * |
| 860 | drm_output_find_special_plane(struct drm_backend *b, struct drm_output *output, |
| 861 | enum wdrm_plane_type type) |
| 862 | { |
| 863 | struct drm_plane *plane; |
| 864 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 865 | wl_list_for_each(plane, &b->plane_list, link) { |
| 866 | struct drm_output *tmp; |
| 867 | bool found_elsewhere = false; |
| 868 | |
| 869 | if (plane->type != type) |
| 870 | continue; |
| 871 | if (!drm_plane_is_available(plane, output)) |
| 872 | continue; |
| 873 | |
| 874 | /* On some platforms, primary/cursor planes can roam |
| 875 | * between different CRTCs, so make sure we don't claim the |
| 876 | * same plane for two outputs. */ |
Leandro Ribeiro | 05cecc8 | 2020-08-13 17:34:58 -0300 | [diff] [blame] | 877 | wl_list_for_each(tmp, &b->compositor->output_list, base.link) { |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 878 | if (tmp->cursor_plane == plane || |
| 879 | tmp->scanout_plane == plane) { |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 880 | found_elsewhere = true; |
| 881 | break; |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | if (found_elsewhere) |
| 886 | continue; |
| 887 | |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 888 | plane->possible_crtcs = (1 << output->crtc->pipe); |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 889 | return plane; |
| 890 | } |
| 891 | |
| 892 | return NULL; |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | /** |
| 896 | * Destroy one DRM plane |
| 897 | * |
| 898 | * Destroy a DRM plane, removing it from screen and releasing its retained |
| 899 | * buffers in the process. The counterpart to drm_plane_create. |
| 900 | * |
| 901 | * @param plane Plane to deallocate (will be freed) |
| 902 | */ |
| 903 | static void |
| 904 | drm_plane_destroy(struct drm_plane *plane) |
| 905 | { |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 906 | if (plane->type == WDRM_PLANE_TYPE_OVERLAY) |
| 907 | drmModeSetPlane(plane->backend->drm.fd, plane->plane_id, |
| 908 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 909 | drm_plane_state_free(plane->state_cur, true); |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 910 | drm_property_info_free(plane->props, WDRM_PLANE__COUNT); |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 911 | weston_plane_release(&plane->base); |
Scott Anderson | 7466309 | 2021-02-01 15:46:33 -0300 | [diff] [blame] | 912 | weston_drm_format_array_fini(&plane->formats); |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 913 | wl_list_remove(&plane->link); |
| 914 | free(plane); |
| 915 | } |
| 916 | |
| 917 | /** |
| 918 | * Initialise sprites (overlay planes) |
| 919 | * |
| 920 | * Walk the list of provided DRM planes, and add overlay planes. |
| 921 | * |
| 922 | * Call destroy_sprites to free these planes. |
| 923 | * |
| 924 | * @param b DRM compositor backend |
| 925 | */ |
| 926 | static void |
| 927 | create_sprites(struct drm_backend *b) |
| 928 | { |
| 929 | drmModePlaneRes *kplane_res; |
| 930 | drmModePlane *kplane; |
| 931 | struct drm_plane *drm_plane; |
| 932 | uint32_t i; |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 933 | kplane_res = drmModeGetPlaneResources(b->drm.fd); |
| 934 | if (!kplane_res) { |
| 935 | weston_log("failed to get plane resources: %s\n", |
| 936 | strerror(errno)); |
| 937 | return; |
| 938 | } |
leng.fang | 32af9fc | 2024-06-13 11:22:15 +0800 | [diff] [blame] | 939 | for (i = kplane_res->count_planes; i >0; i--) { |
| 940 | kplane = drmModeGetPlane(b->drm.fd, kplane_res->planes[i-1]); |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 941 | |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 942 | if (!kplane) |
| 943 | continue; |
| 944 | |
Igor Matheus Andrade Torrente | bfcb1ad | 2020-10-12 13:37:07 -0300 | [diff] [blame] | 945 | drm_plane = drm_plane_create(b, kplane); |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 946 | drmModeFreePlane(kplane); |
| 947 | if (!drm_plane) |
| 948 | continue; |
| 949 | |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 950 | if (drm_plane->type == WDRM_PLANE_TYPE_OVERLAY) |
| 951 | weston_compositor_stack_plane(b->compositor, |
| 952 | &drm_plane->base, |
| 953 | &b->compositor->primary_plane); |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | drmModeFreePlaneResources(kplane_res); |
| 957 | } |
| 958 | |
| 959 | /** |
| 960 | * Clean up sprites (overlay planes) |
| 961 | * |
| 962 | * The counterpart to create_sprites. |
| 963 | * |
| 964 | * @param b DRM compositor backend |
| 965 | */ |
| 966 | static void |
| 967 | destroy_sprites(struct drm_backend *b) |
| 968 | { |
| 969 | struct drm_plane *plane, *next; |
| 970 | |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 971 | wl_list_for_each_safe(plane, next, &b->plane_list, link) |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 972 | drm_plane_destroy(plane); |
| 973 | } |
| 974 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 975 | /* returns a value between 0-255 range, where higher is brighter */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 976 | static uint32_t |
Pekka Paalanen | ce72424 | 2017-09-04 12:21:24 +0300 | [diff] [blame] | 977 | drm_get_backlight(struct drm_head *head) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 978 | { |
| 979 | long brightness, max_brightness, norm; |
| 980 | |
Pekka Paalanen | ce72424 | 2017-09-04 12:21:24 +0300 | [diff] [blame] | 981 | brightness = backlight_get_brightness(head->backlight); |
| 982 | max_brightness = backlight_get_max_brightness(head->backlight); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 983 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 984 | /* convert it on a scale of 0 to 255 */ |
| 985 | norm = (brightness * 255)/(max_brightness); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 986 | |
| 987 | return (uint32_t) norm; |
| 988 | } |
| 989 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 990 | /* values accepted are between 0-255 range */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 991 | static void |
| 992 | drm_set_backlight(struct weston_output *output_base, uint32_t value) |
| 993 | { |
Pekka Paalanen | ecc8cce | 2017-09-12 16:14:31 +0300 | [diff] [blame] | 994 | struct drm_output *output = to_drm_output(output_base); |
| 995 | struct drm_head *head; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 996 | long max_brightness, new_brightness; |
| 997 | |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 998 | if (value > 255) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 999 | return; |
| 1000 | |
Pekka Paalanen | ecc8cce | 2017-09-12 16:14:31 +0300 | [diff] [blame] | 1001 | wl_list_for_each(head, &output->base.head_list, base.output_link) { |
| 1002 | if (!head->backlight) |
| 1003 | return; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1004 | |
Pekka Paalanen | ecc8cce | 2017-09-12 16:14:31 +0300 | [diff] [blame] | 1005 | max_brightness = backlight_get_max_brightness(head->backlight); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1006 | |
Pekka Paalanen | ecc8cce | 2017-09-12 16:14:31 +0300 | [diff] [blame] | 1007 | /* get denormalized value */ |
| 1008 | new_brightness = (value * max_brightness) / 255; |
| 1009 | |
| 1010 | backlight_set_brightness(head->backlight, new_brightness); |
| 1011 | } |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1012 | } |
| 1013 | |
Pekka Paalanen | f8b850d | 2017-11-15 12:51:01 +0200 | [diff] [blame] | 1014 | static void |
| 1015 | drm_output_init_backlight(struct drm_output *output) |
| 1016 | { |
| 1017 | struct weston_head *base; |
| 1018 | struct drm_head *head; |
| 1019 | |
| 1020 | output->base.set_backlight = NULL; |
| 1021 | |
| 1022 | wl_list_for_each(base, &output->base.head_list, output_link) { |
| 1023 | head = to_drm_head(base); |
| 1024 | |
| 1025 | if (head->backlight) { |
| 1026 | weston_log("Initialized backlight for head '%s', device %s\n", |
| 1027 | head->base.name, head->backlight->path); |
| 1028 | |
| 1029 | if (!output->base.set_backlight) { |
| 1030 | output->base.set_backlight = drm_set_backlight; |
| 1031 | output->base.backlight_current = |
| 1032 | drm_get_backlight(head); |
| 1033 | } |
| 1034 | } |
| 1035 | } |
Pekka Paalanen | f8b850d | 2017-11-15 12:51:01 +0200 | [diff] [blame] | 1036 | } |
| 1037 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1038 | /** |
| 1039 | * Power output on or off |
| 1040 | * |
| 1041 | * The DPMS/power level of an output is used to switch it on or off. This |
| 1042 | * is DRM's hook for doing so, which can called either as part of repaint, |
| 1043 | * or independently of the repaint loop. |
| 1044 | * |
| 1045 | * If we are called as part of repaint, we simply set the relevant bit in |
| 1046 | * state and return. |
Tomohito Esaki | b1fb00d | 2018-01-31 17:50:48 +0900 | [diff] [blame] | 1047 | * |
| 1048 | * This function is never called on a virtual output. |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1049 | */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1050 | static void |
| 1051 | drm_set_dpms(struct weston_output *output_base, enum dpms_enum level) |
| 1052 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1053 | struct drm_output *output = to_drm_output(output_base); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1054 | struct drm_backend *b = to_drm_backend(output_base->compositor); |
| 1055 | struct drm_pending_state *pending_state = b->repaint_data; |
| 1056 | struct drm_output_state *state; |
Daniel Stone | 36609c7 | 2015-06-18 07:49:02 +0100 | [diff] [blame] | 1057 | int ret; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1058 | |
Tomohito Esaki | b1fb00d | 2018-01-31 17:50:48 +0900 | [diff] [blame] | 1059 | assert(!output->virtual); |
| 1060 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1061 | if (output->state_cur->dpms == level) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1062 | return; |
| 1063 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1064 | /* If we're being called during the repaint loop, then this is |
| 1065 | * simple: discard any previously-generated state, and create a new |
| 1066 | * state where we disable everything. When we come to flush, this |
| 1067 | * will be applied. |
| 1068 | * |
| 1069 | * However, we need to be careful: we can be called whilst another |
| 1070 | * output is in its repaint cycle (pending_state exists), but our |
| 1071 | * output still has an incomplete state application outstanding. |
| 1072 | * In that case, we need to wait until that completes. */ |
| 1073 | if (pending_state && !output->state_last) { |
| 1074 | /* The repaint loop already sets DPMS on; we don't need to |
| 1075 | * explicitly set it on here, as it will already happen |
| 1076 | * whilst applying the repaint state. */ |
| 1077 | if (level == WESTON_DPMS_ON) |
| 1078 | return; |
| 1079 | |
| 1080 | state = drm_pending_state_get_output(pending_state, output); |
| 1081 | if (state) |
| 1082 | drm_output_state_free(state); |
| 1083 | state = drm_output_get_disable_state(pending_state, output); |
Daniel Stone | 36609c7 | 2015-06-18 07:49:02 +0100 | [diff] [blame] | 1084 | return; |
| 1085 | } |
| 1086 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1087 | /* As we throw everything away when disabling, just send us back through |
| 1088 | * a repaint cycle. */ |
| 1089 | if (level == WESTON_DPMS_ON) { |
| 1090 | if (output->dpms_off_pending) |
Emmanuel Gil Peyrot | 1b3ad09 | 2019-12-09 02:50:55 +0100 | [diff] [blame] | 1091 | output->dpms_off_pending = false; |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1092 | weston_output_schedule_repaint(output_base); |
| 1093 | return; |
| 1094 | } |
| 1095 | |
| 1096 | /* If we've already got a request in the pipeline, then we need to |
| 1097 | * park our DPMS request until that request has quiesced. */ |
| 1098 | if (output->state_last) { |
Emmanuel Gil Peyrot | 1b3ad09 | 2019-12-09 02:50:55 +0100 | [diff] [blame] | 1099 | output->dpms_off_pending = true; |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1100 | return; |
| 1101 | } |
| 1102 | |
| 1103 | pending_state = drm_pending_state_alloc(b); |
| 1104 | drm_output_get_disable_state(pending_state, output); |
| 1105 | ret = drm_pending_state_apply_sync(pending_state); |
| 1106 | if (ret != 0) |
| 1107 | weston_log("drm_set_dpms: couldn't disable output?\n"); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1108 | } |
| 1109 | |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 1110 | static const char * const connector_type_names[] = { |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 1111 | [DRM_MODE_CONNECTOR_Unknown] = "Unknown", |
| 1112 | [DRM_MODE_CONNECTOR_VGA] = "VGA", |
| 1113 | [DRM_MODE_CONNECTOR_DVII] = "DVI-I", |
| 1114 | [DRM_MODE_CONNECTOR_DVID] = "DVI-D", |
| 1115 | [DRM_MODE_CONNECTOR_DVIA] = "DVI-A", |
| 1116 | [DRM_MODE_CONNECTOR_Composite] = "Composite", |
| 1117 | [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO", |
| 1118 | [DRM_MODE_CONNECTOR_LVDS] = "LVDS", |
| 1119 | [DRM_MODE_CONNECTOR_Component] = "Component", |
| 1120 | [DRM_MODE_CONNECTOR_9PinDIN] = "DIN", |
| 1121 | [DRM_MODE_CONNECTOR_DisplayPort] = "DP", |
| 1122 | [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A", |
| 1123 | [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B", |
| 1124 | [DRM_MODE_CONNECTOR_TV] = "TV", |
| 1125 | [DRM_MODE_CONNECTOR_eDP] = "eDP", |
| 1126 | [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual", |
| 1127 | [DRM_MODE_CONNECTOR_DSI] = "DSI", |
Stefan Agner | 30e283d | 2018-08-20 17:11:38 +0200 | [diff] [blame] | 1128 | [DRM_MODE_CONNECTOR_DPI] = "DPI", |
Kristian Høgsberg | 2f9ed71 | 2012-07-26 17:57:15 -0400 | [diff] [blame] | 1129 | }; |
| 1130 | |
Pekka Paalanen | 1f21ef1 | 2017-04-03 13:33:26 +0300 | [diff] [blame] | 1131 | /** Create a name given a DRM connector |
| 1132 | * |
| 1133 | * \param con The DRM connector whose type and id form the name. |
| 1134 | * \return A newly allocate string, or NULL on error. Must be free()'d |
| 1135 | * after use. |
| 1136 | * |
| 1137 | * The name does not identify the DRM display device. |
| 1138 | */ |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 1139 | static char * |
| 1140 | make_connector_name(const drmModeConnector *con) |
| 1141 | { |
Pekka Paalanen | 1f21ef1 | 2017-04-03 13:33:26 +0300 | [diff] [blame] | 1142 | char *name; |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 1143 | const char *type_name = NULL; |
Pekka Paalanen | 1f21ef1 | 2017-04-03 13:33:26 +0300 | [diff] [blame] | 1144 | int ret; |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 1145 | |
| 1146 | if (con->connector_type < ARRAY_LENGTH(connector_type_names)) |
| 1147 | type_name = connector_type_names[con->connector_type]; |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 1148 | |
| 1149 | if (!type_name) |
| 1150 | type_name = "UNNAMED"; |
| 1151 | |
Pekka Paalanen | 1f21ef1 | 2017-04-03 13:33:26 +0300 | [diff] [blame] | 1152 | ret = asprintf(&name, "%s-%d", type_name, con->connector_type_id); |
| 1153 | if (ret < 0) |
| 1154 | return NULL; |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 1155 | |
Pekka Paalanen | 1f21ef1 | 2017-04-03 13:33:26 +0300 | [diff] [blame] | 1156 | return name; |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 1157 | } |
| 1158 | |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 1159 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1160 | drm_output_init_pixman(struct drm_output *output, struct drm_backend *b) |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1161 | { |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 1162 | int w = output->base.current_mode->width; |
| 1163 | int h = output->base.current_mode->height; |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 1164 | uint32_t format = output->gbm_format; |
| 1165 | uint32_t pixman_format; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1166 | unsigned int i; |
Daniel Stone | 61abf35 | 2020-03-06 12:46:30 +0000 | [diff] [blame] | 1167 | const struct pixman_renderer_output_options options = { |
| 1168 | .use_shadow = b->use_pixman_shadow, |
| 1169 | }; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1170 | |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 1171 | switch (format) { |
Stefan Agner | 0bfebeb | 2019-07-08 00:30:44 +0200 | [diff] [blame] | 1172 | case DRM_FORMAT_XRGB8888: |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 1173 | pixman_format = PIXMAN_x8r8g8b8; |
| 1174 | break; |
Stefan Agner | 0bfebeb | 2019-07-08 00:30:44 +0200 | [diff] [blame] | 1175 | case DRM_FORMAT_RGB565: |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 1176 | pixman_format = PIXMAN_r5g6b5; |
| 1177 | break; |
| 1178 | default: |
| 1179 | weston_log("Unsupported pixman format 0x%x\n", format); |
| 1180 | return -1; |
| 1181 | } |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1182 | |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 1183 | /* FIXME error checking */ |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1184 | for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) { |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 1185 | output->dumb[i] = drm_fb_create_dumb(b, w, h, format); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1186 | if (!output->dumb[i]) |
| 1187 | goto err; |
| 1188 | |
| 1189 | output->image[i] = |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 1190 | pixman_image_create_bits(pixman_format, w, h, |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1191 | output->dumb[i]->map, |
Daniel Stone | 8eece0c | 2016-11-17 17:54:00 +0000 | [diff] [blame] | 1192 | output->dumb[i]->strides[0]); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1193 | if (!output->image[i]) |
| 1194 | goto err; |
| 1195 | } |
| 1196 | |
Daniel Stone | 61abf35 | 2020-03-06 12:46:30 +0000 | [diff] [blame] | 1197 | if (pixman_renderer_output_create(&output->base, &options) < 0) |
Dongjin Kim | ba89f00 | 2024-09-30 17:49:36 +0900 | [diff] [blame] | 1198 | goto err; |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 1199 | |
Pekka Paalanen | dee412d | 2018-04-23 11:44:58 +0200 | [diff] [blame] | 1200 | weston_log("DRM: output %s %s shadow framebuffer.\n", output->base.name, |
| 1201 | b->use_pixman_shadow ? "uses" : "does not use"); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1202 | |
| 1203 | pixman_region32_init_rect(&output->previous_damage, |
Alexander Larsson | 0b13506 | 2013-05-28 16:23:36 +0200 | [diff] [blame] | 1204 | output->base.x, output->base.y, output->base.width, output->base.height); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1205 | |
| 1206 | return 0; |
| 1207 | |
| 1208 | err: |
| 1209 | for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) { |
| 1210 | if (output->dumb[i]) |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 1211 | drm_fb_unref(output->dumb[i]); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1212 | if (output->image[i]) |
| 1213 | pixman_image_unref(output->image[i]); |
| 1214 | |
| 1215 | output->dumb[i] = NULL; |
| 1216 | output->image[i] = NULL; |
| 1217 | } |
| 1218 | |
| 1219 | return -1; |
| 1220 | } |
| 1221 | |
| 1222 | static void |
| 1223 | drm_output_fini_pixman(struct drm_output *output) |
| 1224 | { |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1225 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1226 | unsigned int i; |
| 1227 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1228 | /* Destroying the Pixman surface will destroy all our buffers, |
| 1229 | * regardless of refcount. Ensure we destroy them here. */ |
| 1230 | if (!b->shutting_down && |
| 1231 | output->scanout_plane->state_cur->fb && |
| 1232 | output->scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB) { |
Alexandros Frantzis | 9975134 | 2020-05-18 15:22:49 +0300 | [diff] [blame] | 1233 | drm_plane_reset_state(output->scanout_plane); |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1236 | pixman_renderer_output_destroy(&output->base); |
| 1237 | pixman_region32_fini(&output->previous_damage); |
| 1238 | |
| 1239 | for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1240 | pixman_image_unref(output->image[i]); |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 1241 | drm_fb_unref(output->dumb[i]); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1242 | output->dumb[i] = NULL; |
| 1243 | output->image[i] = NULL; |
| 1244 | } |
| 1245 | } |
| 1246 | |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 1247 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1248 | setup_output_seat_constraint(struct drm_backend *b, |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 1249 | struct weston_output *output, |
| 1250 | const char *s) |
| 1251 | { |
| 1252 | if (strcmp(s, "") != 0) { |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1253 | struct weston_pointer *pointer; |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 1254 | struct udev_seat *seat; |
| 1255 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1256 | seat = udev_seat_get_named(&b->input, s); |
Derek Foreman | 0720ea3 | 2015-07-15 13:00:35 -0500 | [diff] [blame] | 1257 | if (!seat) |
| 1258 | return; |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 1259 | |
Derek Foreman | 0720ea3 | 2015-07-15 13:00:35 -0500 | [diff] [blame] | 1260 | seat->base.output = output; |
| 1261 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1262 | pointer = weston_seat_get_pointer(&seat->base); |
| 1263 | if (pointer) |
| 1264 | weston_pointer_clamp(pointer, |
| 1265 | &pointer->x, |
| 1266 | &pointer->y); |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 1267 | } |
| 1268 | } |
| 1269 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1270 | static int |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 1271 | drm_output_attach_head(struct weston_output *output_base, |
| 1272 | struct weston_head *head_base) |
| 1273 | { |
Pekka Paalanen | d5f98d8 | 2017-12-08 14:45:00 +0200 | [diff] [blame] | 1274 | struct drm_backend *b = to_drm_backend(output_base->compositor); |
| 1275 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 1276 | if (wl_list_length(&output_base->head_list) >= MAX_CLONED_CONNECTORS) |
| 1277 | return -1; |
| 1278 | |
Pekka Paalanen | d5f98d8 | 2017-12-08 14:45:00 +0200 | [diff] [blame] | 1279 | if (!output_base->enabled) |
| 1280 | return 0; |
| 1281 | |
| 1282 | /* XXX: ensure the configuration will work. |
| 1283 | * This is actually impossible without major infrastructure |
| 1284 | * work. */ |
| 1285 | |
| 1286 | /* Need to go through modeset to add connectors. */ |
| 1287 | /* XXX: Ideally we'd do this per-output, not globally. */ |
| 1288 | /* XXX: Doing it globally, what guarantees another output's update |
| 1289 | * will not clear the flag before this output is updated? |
| 1290 | */ |
| 1291 | b->state_invalid = true; |
| 1292 | |
| 1293 | weston_output_schedule_repaint(output_base); |
| 1294 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 1295 | return 0; |
| 1296 | } |
| 1297 | |
Pekka Paalanen | 7f85379 | 2017-11-29 14:33:33 +0200 | [diff] [blame] | 1298 | static void |
| 1299 | drm_output_detach_head(struct weston_output *output_base, |
| 1300 | struct weston_head *head_base) |
| 1301 | { |
| 1302 | struct drm_backend *b = to_drm_backend(output_base->compositor); |
| 1303 | |
| 1304 | if (!output_base->enabled) |
| 1305 | return; |
| 1306 | |
| 1307 | /* Need to go through modeset to drop connectors that should no longer |
| 1308 | * be driven. */ |
| 1309 | /* XXX: Ideally we'd do this per-output, not globally. */ |
| 1310 | b->state_invalid = true; |
| 1311 | |
| 1312 | weston_output_schedule_repaint(output_base); |
| 1313 | } |
| 1314 | |
Stefan Agner | 3654c67 | 2019-07-09 00:50:30 +0200 | [diff] [blame] | 1315 | int |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 1316 | parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format) |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 1317 | { |
Pekka Paalanen | 62a9436 | 2018-09-26 14:33:36 +0300 | [diff] [blame] | 1318 | const struct pixel_format_info *pinfo; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 1319 | |
Pekka Paalanen | 62a9436 | 2018-09-26 14:33:36 +0300 | [diff] [blame] | 1320 | if (s == NULL) { |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 1321 | *gbm_format = default_value; |
Pekka Paalanen | 62a9436 | 2018-09-26 14:33:36 +0300 | [diff] [blame] | 1322 | |
| 1323 | return 0; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
Pekka Paalanen | 62a9436 | 2018-09-26 14:33:36 +0300 | [diff] [blame] | 1326 | pinfo = pixel_format_get_info_by_drm_name(s); |
| 1327 | if (!pinfo) { |
| 1328 | weston_log("fatal: unrecognized pixel format: %s\n", s); |
| 1329 | |
| 1330 | return -1; |
| 1331 | } |
| 1332 | |
| 1333 | /* GBM formats and DRM formats are identical. */ |
| 1334 | *gbm_format = pinfo->format; |
| 1335 | |
| 1336 | return 0; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
Pekka Paalanen | eee580b | 2014-06-04 16:43:06 +0300 | [diff] [blame] | 1339 | static int |
Pekka Paalanen | 9c03a7c | 2017-11-28 14:30:10 +0200 | [diff] [blame] | 1340 | drm_head_read_current_setup(struct drm_head *head, struct drm_backend *backend) |
Pekka Paalanen | eee580b | 2014-06-04 16:43:06 +0300 | [diff] [blame] | 1341 | { |
Pekka Paalanen | 9c03a7c | 2017-11-28 14:30:10 +0200 | [diff] [blame] | 1342 | int drm_fd = backend->drm.fd; |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 1343 | drmModeConnector *conn = head->connector.conn; |
Pekka Paalanen | eee580b | 2014-06-04 16:43:06 +0300 | [diff] [blame] | 1344 | drmModeEncoder *encoder; |
| 1345 | drmModeCrtc *crtc; |
| 1346 | |
| 1347 | /* Get the current mode on the crtc that's currently driving |
| 1348 | * this connector. */ |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 1349 | encoder = drmModeGetEncoder(drm_fd, conn->encoder_id); |
Pekka Paalanen | eee580b | 2014-06-04 16:43:06 +0300 | [diff] [blame] | 1350 | if (encoder != NULL) { |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 1351 | head->inherited_crtc_id = encoder->crtc_id; |
| 1352 | |
Pekka Paalanen | eee580b | 2014-06-04 16:43:06 +0300 | [diff] [blame] | 1353 | crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id); |
| 1354 | drmModeFreeEncoder(encoder); |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 1355 | |
Pekka Paalanen | eee580b | 2014-06-04 16:43:06 +0300 | [diff] [blame] | 1356 | if (crtc == NULL) |
| 1357 | return -1; |
| 1358 | if (crtc->mode_valid) |
Pekka Paalanen | 6fae2be | 2017-11-28 14:33:52 +0200 | [diff] [blame] | 1359 | head->inherited_mode = crtc->mode; |
Pekka Paalanen | eee580b | 2014-06-04 16:43:06 +0300 | [diff] [blame] | 1360 | drmModeFreeCrtc(crtc); |
| 1361 | } |
| 1362 | |
| 1363 | return 0; |
| 1364 | } |
| 1365 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 1366 | static void |
| 1367 | drm_output_set_gbm_format(struct weston_output *base, |
| 1368 | const char *gbm_format) |
| 1369 | { |
| 1370 | struct drm_output *output = to_drm_output(base); |
| 1371 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 1372 | |
| 1373 | if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1) |
| 1374 | output->gbm_format = b->gbm_format; |
| 1375 | } |
| 1376 | |
| 1377 | static void |
| 1378 | drm_output_set_seat(struct weston_output *base, |
| 1379 | const char *seat) |
| 1380 | { |
| 1381 | struct drm_output *output = to_drm_output(base); |
| 1382 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 1383 | |
| 1384 | setup_output_seat_constraint(b, &output->base, |
| 1385 | seat ? seat : ""); |
| 1386 | } |
| 1387 | |
| 1388 | static int |
Pekka Paalanen | c4db6f7 | 2017-09-05 16:37:03 +0300 | [diff] [blame] | 1389 | drm_output_init_gamma_size(struct drm_output *output) |
| 1390 | { |
| 1391 | struct drm_backend *backend = to_drm_backend(output->base.compositor); |
| 1392 | drmModeCrtc *crtc; |
| 1393 | |
| 1394 | assert(output->base.compositor); |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1395 | assert(output->crtc); |
| 1396 | crtc = drmModeGetCrtc(backend->drm.fd, output->crtc->crtc_id); |
Pekka Paalanen | c4db6f7 | 2017-09-05 16:37:03 +0300 | [diff] [blame] | 1397 | if (!crtc) |
| 1398 | return -1; |
| 1399 | |
| 1400 | output->base.gamma_size = crtc->gamma_size; |
| 1401 | |
| 1402 | drmModeFreeCrtc(crtc); |
| 1403 | |
| 1404 | return 0; |
| 1405 | } |
| 1406 | |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 1407 | static uint32_t |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 1408 | drm_connector_get_possible_crtcs_mask(struct drm_connector *connector) |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 1409 | { |
| 1410 | uint32_t possible_crtcs = 0; |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 1411 | drmModeConnector *conn = connector->conn; |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 1412 | drmModeEncoder *encoder; |
| 1413 | int i; |
| 1414 | |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 1415 | for (i = 0; i < conn->count_encoders; i++) { |
| 1416 | encoder = drmModeGetEncoder(connector->backend->drm.fd, |
| 1417 | conn->encoders[i]); |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 1418 | if (!encoder) |
| 1419 | continue; |
| 1420 | |
| 1421 | possible_crtcs |= encoder->possible_crtcs; |
| 1422 | drmModeFreeEncoder(encoder); |
| 1423 | } |
| 1424 | |
| 1425 | return possible_crtcs; |
| 1426 | } |
| 1427 | |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 1428 | /** Pick a CRTC that might be able to drive all attached connectors |
| 1429 | * |
| 1430 | * @param output The output whose attached heads to include. |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1431 | * @return CRTC object to pick, or NULL on failure or not found. |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 1432 | */ |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1433 | static struct drm_crtc * |
| 1434 | drm_output_pick_crtc(struct drm_output *output) |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 1435 | { |
| 1436 | struct drm_backend *backend; |
| 1437 | struct weston_head *base; |
| 1438 | struct drm_head *head; |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1439 | struct drm_crtc *crtc; |
| 1440 | struct drm_crtc *best_crtc = NULL; |
| 1441 | struct drm_crtc *fallback_crtc = NULL; |
| 1442 | struct drm_crtc *existing_crtc[32]; |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 1443 | uint32_t possible_crtcs = 0xffffffff; |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1444 | unsigned n = 0; |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 1445 | uint32_t crtc_id; |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1446 | unsigned int i; |
Pekka Paalanen | db4c7d7 | 2017-11-28 16:11:00 +0200 | [diff] [blame] | 1447 | bool match; |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 1448 | |
| 1449 | backend = to_drm_backend(output->base.compositor); |
| 1450 | |
| 1451 | /* This algorithm ignores drmModeEncoder::possible_clones restriction, |
| 1452 | * because it is more often set wrong than not in the kernel. */ |
| 1453 | |
| 1454 | /* Accumulate a mask of possible crtcs and find existing routings. */ |
| 1455 | wl_list_for_each(base, &output->base.head_list, output_link) { |
| 1456 | head = to_drm_head(base); |
| 1457 | |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 1458 | possible_crtcs &= |
| 1459 | drm_connector_get_possible_crtcs_mask(&head->connector); |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 1460 | |
| 1461 | crtc_id = head->inherited_crtc_id; |
| 1462 | if (crtc_id > 0 && n < ARRAY_LENGTH(existing_crtc)) |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1463 | existing_crtc[n++] = drm_crtc_find(backend, crtc_id); |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | /* Find a crtc that could drive each connector individually at least, |
| 1467 | * and prefer existing routings. */ |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1468 | wl_list_for_each(crtc, &backend->crtc_list, link) { |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 1469 | |
| 1470 | /* Could the crtc not drive each connector? */ |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1471 | if (!(possible_crtcs & (1 << crtc->pipe))) |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 1472 | continue; |
| 1473 | |
| 1474 | /* Is the crtc already in use? */ |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1475 | if (crtc->output) |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 1476 | continue; |
| 1477 | |
| 1478 | /* Try to preserve the existing CRTC -> connector routing; |
| 1479 | * it makes initialisation faster, and also since we have a |
| 1480 | * very dumb picking algorithm, may preserve a better |
| 1481 | * choice. */ |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1482 | for (i = 0; i < n; i++) { |
| 1483 | if (existing_crtc[i] == crtc) |
| 1484 | return crtc; |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 1485 | } |
| 1486 | |
Pekka Paalanen | db4c7d7 | 2017-11-28 16:11:00 +0200 | [diff] [blame] | 1487 | /* Check if any other head had existing routing to this CRTC. |
| 1488 | * If they did, this is not the best CRTC as it might be needed |
| 1489 | * for another output we haven't enabled yet. */ |
| 1490 | match = false; |
| 1491 | wl_list_for_each(base, &backend->compositor->head_list, |
| 1492 | compositor_link) { |
| 1493 | head = to_drm_head(base); |
| 1494 | |
| 1495 | if (head->base.output == &output->base) |
| 1496 | continue; |
| 1497 | |
| 1498 | if (weston_head_is_enabled(&head->base)) |
| 1499 | continue; |
| 1500 | |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1501 | if (head->inherited_crtc_id == crtc->crtc_id) { |
Pekka Paalanen | db4c7d7 | 2017-11-28 16:11:00 +0200 | [diff] [blame] | 1502 | match = true; |
| 1503 | break; |
| 1504 | } |
| 1505 | } |
| 1506 | if (!match) |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1507 | best_crtc = crtc; |
Pekka Paalanen | db4c7d7 | 2017-11-28 16:11:00 +0200 | [diff] [blame] | 1508 | |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1509 | fallback_crtc = crtc; |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 1510 | } |
| 1511 | |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1512 | if (best_crtc) |
| 1513 | return best_crtc; |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 1514 | |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1515 | if (fallback_crtc) |
| 1516 | return fallback_crtc; |
Pekka Paalanen | db4c7d7 | 2017-11-28 16:11:00 +0200 | [diff] [blame] | 1517 | |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 1518 | /* Likely possible_crtcs was empty due to asking for clones, |
| 1519 | * but since the DRM documentation says the kernel lies, let's |
| 1520 | * pick one crtc anyway. Trial and error is the only way to |
| 1521 | * be sure if something doesn't work. */ |
| 1522 | |
| 1523 | /* First pick any existing assignment. */ |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1524 | for (i = 0; i < n; i++) { |
| 1525 | crtc = existing_crtc[i]; |
| 1526 | if (!crtc->output) |
| 1527 | return crtc; |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | /* Otherwise pick any available crtc. */ |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1531 | wl_list_for_each(crtc, &backend->crtc_list, link) { |
| 1532 | if (!crtc->output) |
| 1533 | return crtc; |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 1534 | } |
| 1535 | |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1536 | return NULL; |
| 1537 | } |
| 1538 | |
| 1539 | /** Create an "empty" drm_crtc. It will only set its ID, pipe and props. After |
| 1540 | * all, it adds the object to the DRM-backend CRTC list. |
| 1541 | */ |
| 1542 | static struct drm_crtc * |
| 1543 | drm_crtc_create(struct drm_backend *b, uint32_t crtc_id, uint32_t pipe) |
| 1544 | { |
| 1545 | struct drm_crtc *crtc; |
| 1546 | drmModeObjectPropertiesPtr props; |
| 1547 | |
| 1548 | props = drmModeObjectGetProperties(b->drm.fd, crtc_id, |
| 1549 | DRM_MODE_OBJECT_CRTC); |
| 1550 | if (!props) { |
| 1551 | weston_log("failed to get CRTC properties\n"); |
| 1552 | return NULL; |
| 1553 | } |
| 1554 | |
| 1555 | crtc = zalloc(sizeof(*crtc)); |
| 1556 | if (!crtc) |
| 1557 | goto ret; |
| 1558 | |
| 1559 | drm_property_info_populate(b, crtc_props, crtc->props_crtc, |
| 1560 | WDRM_CRTC__COUNT, props); |
| 1561 | crtc->backend = b; |
| 1562 | crtc->crtc_id = crtc_id; |
| 1563 | crtc->pipe = pipe; |
| 1564 | crtc->output = NULL; |
| 1565 | |
| 1566 | /* Add it to the last position of the DRM-backend CRTC list */ |
| 1567 | wl_list_insert(b->crtc_list.prev, &crtc->link); |
| 1568 | |
| 1569 | ret: |
| 1570 | drmModeFreeObjectProperties(props); |
| 1571 | return crtc; |
| 1572 | } |
| 1573 | |
| 1574 | /** Destroy a drm_crtc object that was created with drm_crtc_create(). It will |
| 1575 | * also remove it from the DRM-backend CRTC list. |
| 1576 | */ |
| 1577 | static void |
| 1578 | drm_crtc_destroy(struct drm_crtc *crtc) |
| 1579 | { |
| 1580 | /* TODO: address the issue below to be able to remove the comment |
| 1581 | * from the assert. |
| 1582 | * |
| 1583 | * https://gitlab.freedesktop.org/wayland/weston/-/issues/421 |
| 1584 | */ |
| 1585 | |
| 1586 | //assert(!crtc->output); |
| 1587 | |
| 1588 | wl_list_remove(&crtc->link); |
| 1589 | drm_property_info_free(crtc->props_crtc, WDRM_CRTC__COUNT); |
| 1590 | free(crtc); |
| 1591 | } |
| 1592 | |
| 1593 | /** Find all CRTCs of the fd and create drm_crtc objects for them. |
| 1594 | * |
| 1595 | * The CRTCs are saved in a list of the drm_backend and will keep there until |
| 1596 | * the fd gets closed. |
| 1597 | * |
| 1598 | * @param b The DRM-backend structure. |
Igor Matheus Andrade Torrente | 6354455 | 2020-10-13 11:25:20 -0300 | [diff] [blame] | 1599 | * @param resources The DRM resources, it is taken with drmModeGetResources |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1600 | * @return 0 on success (at least one CRTC in the list), -1 on failure. |
| 1601 | */ |
| 1602 | static int |
Igor Matheus Andrade Torrente | 6354455 | 2020-10-13 11:25:20 -0300 | [diff] [blame] | 1603 | drm_backend_create_crtc_list(struct drm_backend *b, drmModeRes *resources) |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1604 | { |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1605 | struct drm_crtc *crtc, *crtc_tmp; |
| 1606 | int i; |
| 1607 | |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1608 | /* Iterate through all CRTCs */ |
| 1609 | for (i = 0; i < resources->count_crtcs; i++) { |
| 1610 | |
| 1611 | /* Let's create an object for the CRTC and add it to the list */ |
| 1612 | crtc = drm_crtc_create(b, resources->crtcs[i], i); |
| 1613 | if (!crtc) |
| 1614 | goto err; |
| 1615 | } |
| 1616 | |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1617 | return 0; |
| 1618 | |
| 1619 | err: |
| 1620 | wl_list_for_each_safe(crtc, crtc_tmp, &b->crtc_list, link) |
| 1621 | drm_crtc_destroy(crtc); |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 1622 | return -1; |
| 1623 | } |
| 1624 | |
Leandro Ribeiro | 3be23ef | 2020-08-13 16:44:02 -0300 | [diff] [blame] | 1625 | |
| 1626 | /** Populates scanout and cursor planes for the output. Also sets the topology |
| 1627 | * of the planes by adding them to the plane stacking list. |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 1628 | */ |
| 1629 | static int |
Leandro Ribeiro | 3be23ef | 2020-08-13 16:44:02 -0300 | [diff] [blame] | 1630 | drm_output_init_planes(struct drm_output *output) |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 1631 | { |
| 1632 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 1633 | |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 1634 | output->scanout_plane = |
| 1635 | drm_output_find_special_plane(b, output, |
| 1636 | WDRM_PLANE_TYPE_PRIMARY); |
| 1637 | if (!output->scanout_plane) { |
| 1638 | weston_log("Failed to find primary plane for output %s\n", |
| 1639 | output->base.name); |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1640 | return -1; |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 1641 | } |
| 1642 | |
Leandro Ribeiro | 3be23ef | 2020-08-13 16:44:02 -0300 | [diff] [blame] | 1643 | weston_compositor_stack_plane(b->compositor, |
| 1644 | &output->scanout_plane->base, |
| 1645 | &b->compositor->primary_plane); |
| 1646 | |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 1647 | /* Failing to find a cursor plane is not fatal, as we'll fall back |
| 1648 | * to software cursor. */ |
| 1649 | output->cursor_plane = |
| 1650 | drm_output_find_special_plane(b, output, |
| 1651 | WDRM_PLANE_TYPE_CURSOR); |
| 1652 | |
Leandro Ribeiro | 3be23ef | 2020-08-13 16:44:02 -0300 | [diff] [blame] | 1653 | if (output->cursor_plane) |
| 1654 | weston_compositor_stack_plane(b->compositor, |
| 1655 | &output->cursor_plane->base, |
| 1656 | NULL); |
| 1657 | else |
| 1658 | b->cursors_are_broken = true; |
Pekka Paalanen | 663d5e9 | 2017-09-08 13:32:40 +0300 | [diff] [blame] | 1659 | |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 1660 | return 0; |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 1661 | } |
| 1662 | |
Leandro Ribeiro | 3be23ef | 2020-08-13 16:44:02 -0300 | [diff] [blame] | 1663 | /** The opposite of drm_output_init_planes(). First of all it removes the planes |
Igor Matheus Andrade Torrente | bfcb1ad | 2020-10-12 13:37:07 -0300 | [diff] [blame] | 1664 | * from the plane stacking list. After all it sets the planes of the output as NULL. |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 1665 | */ |
| 1666 | static void |
Leandro Ribeiro | 3be23ef | 2020-08-13 16:44:02 -0300 | [diff] [blame] | 1667 | drm_output_deinit_planes(struct drm_output *output) |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 1668 | { |
| 1669 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
| 1670 | |
Alexandros Frantzis | 53a71cb | 2020-05-18 15:26:01 +0300 | [diff] [blame] | 1671 | /* If the compositor is already shutting down, the planes have already |
| 1672 | * been destroyed. */ |
| 1673 | if (!b->shutting_down) { |
Pekka Paalanen | 3d6721e | 2020-09-16 15:04:04 +0300 | [diff] [blame] | 1674 | wl_list_remove(&output->scanout_plane->base.link); |
| 1675 | wl_list_init(&output->scanout_plane->base.link); |
| 1676 | |
| 1677 | if (output->cursor_plane) { |
| 1678 | wl_list_remove(&output->cursor_plane->base.link); |
| 1679 | wl_list_init(&output->cursor_plane->base.link); |
| 1680 | /* Turn off hardware cursor */ |
| 1681 | drmModeSetCursor(b->drm.fd, output->crtc->crtc_id, 0, 0, 0); |
| 1682 | } |
| 1683 | |
Igor Matheus Andrade Torrente | bfcb1ad | 2020-10-12 13:37:07 -0300 | [diff] [blame] | 1684 | /* With universal planes, the planes are allocated at startup, |
| 1685 | * freed at shutdown, and live on the plane list in between. |
| 1686 | * We want the planes to continue to exist and be freed up |
| 1687 | * for other outputs. |
| 1688 | */ |
| 1689 | if (output->cursor_plane) |
| 1690 | drm_plane_reset_state(output->cursor_plane); |
| 1691 | if (output->scanout_plane) |
| 1692 | drm_plane_reset_state(output->scanout_plane); |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 1693 | } |
| 1694 | |
Leandro Ribeiro | 3be23ef | 2020-08-13 16:44:02 -0300 | [diff] [blame] | 1695 | output->cursor_plane = NULL; |
| 1696 | output->scanout_plane = NULL; |
| 1697 | } |
| 1698 | |
Leandro Ribeiro | 5429302 | 2021-10-12 14:48:36 -0300 | [diff] [blame] | 1699 | static struct weston_drm_format_array * |
| 1700 | get_scanout_formats(struct drm_backend *b) |
| 1701 | { |
| 1702 | struct weston_compositor *ec = b->compositor; |
| 1703 | const struct weston_drm_format_array *renderer_formats; |
| 1704 | struct weston_drm_format_array *scanout_formats, union_planes_formats; |
| 1705 | struct drm_plane *plane; |
| 1706 | int ret; |
| 1707 | |
| 1708 | /* If we got here it means that dma-buf feedback is supported and that |
| 1709 | * the renderer has formats/modifiers to expose. */ |
| 1710 | assert(ec->renderer->get_supported_formats != NULL); |
| 1711 | renderer_formats = ec->renderer->get_supported_formats(ec); |
| 1712 | |
| 1713 | scanout_formats = zalloc(sizeof(*scanout_formats)); |
| 1714 | if (!scanout_formats) { |
| 1715 | weston_log("%s: out of memory\n", __func__); |
| 1716 | return NULL; |
| 1717 | } |
| 1718 | |
| 1719 | weston_drm_format_array_init(&union_planes_formats); |
| 1720 | weston_drm_format_array_init(scanout_formats); |
| 1721 | |
| 1722 | /* Compute the union of the format/modifiers of the KMS planes */ |
| 1723 | wl_list_for_each(plane, &b->plane_list, link) { |
| 1724 | /* The scanout formats are used by the dma-buf feedback. But for |
| 1725 | * now cursor planes do not support dma-buf buffers, only wl_shm |
| 1726 | * buffers. So we skip cursor planes here. */ |
| 1727 | if (plane->type == WDRM_PLANE_TYPE_CURSOR) |
| 1728 | continue; |
| 1729 | |
| 1730 | ret = weston_drm_format_array_join(&union_planes_formats, |
| 1731 | &plane->formats); |
| 1732 | if (ret < 0) |
| 1733 | goto err; |
| 1734 | } |
| 1735 | |
| 1736 | /* Compute the intersection between the union of format/modifiers of KMS |
| 1737 | * planes and the formats supported by the renderer */ |
| 1738 | ret = weston_drm_format_array_replace(scanout_formats, |
| 1739 | renderer_formats); |
| 1740 | if (ret < 0) |
| 1741 | goto err; |
| 1742 | |
| 1743 | ret = weston_drm_format_array_intersect(scanout_formats, |
| 1744 | &union_planes_formats); |
| 1745 | if (ret < 0) |
| 1746 | goto err; |
| 1747 | |
| 1748 | weston_drm_format_array_fini(&union_planes_formats); |
| 1749 | |
| 1750 | return scanout_formats; |
| 1751 | |
| 1752 | err: |
| 1753 | weston_drm_format_array_fini(&union_planes_formats); |
| 1754 | weston_drm_format_array_fini(scanout_formats); |
Daniel Stone | f508603 | 2021-12-06 16:52:19 +0000 | [diff] [blame] | 1755 | free(scanout_formats); |
Leandro Ribeiro | 5429302 | 2021-10-12 14:48:36 -0300 | [diff] [blame] | 1756 | return NULL; |
| 1757 | } |
| 1758 | |
Leandro Ribeiro | 3be23ef | 2020-08-13 16:44:02 -0300 | [diff] [blame] | 1759 | /** Pick a CRTC and reserve it for the output. |
| 1760 | * |
| 1761 | * On failure, the output remains without a CRTC. |
| 1762 | * |
| 1763 | * @param output The output with no CRTC associated. |
| 1764 | * @return 0 on success, -1 on failure. |
| 1765 | */ |
| 1766 | static int |
| 1767 | drm_output_attach_crtc(struct drm_output *output) |
| 1768 | { |
| 1769 | output->crtc = drm_output_pick_crtc(output); |
| 1770 | if (!output->crtc) { |
| 1771 | weston_log("Output '%s': No available CRTCs.\n", |
| 1772 | output->base.name); |
| 1773 | return -1; |
| 1774 | } |
| 1775 | |
| 1776 | /* Reserve the CRTC for the output */ |
| 1777 | output->crtc->output = output; |
| 1778 | |
| 1779 | return 0; |
| 1780 | } |
| 1781 | |
| 1782 | /** Release reservation of the CRTC. |
| 1783 | * |
| 1784 | * Make the CRTC free to be reserved and used by another output. |
| 1785 | * |
| 1786 | * @param output The output that will release its CRTC. |
| 1787 | */ |
| 1788 | static void |
| 1789 | drm_output_detach_crtc(struct drm_output *output) |
| 1790 | { |
| 1791 | struct drm_backend *b = output->backend; |
| 1792 | struct drm_crtc *crtc = output->crtc; |
Pekka Paalanen | 663d5e9 | 2017-09-08 13:32:40 +0300 | [diff] [blame] | 1793 | |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1794 | crtc->output = NULL; |
| 1795 | output->crtc = NULL; |
| 1796 | |
Leandro Ribeiro | 3be23ef | 2020-08-13 16:44:02 -0300 | [diff] [blame] | 1797 | /* Force resetting unused CRTCs */ |
| 1798 | b->state_invalid = true; |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 1799 | } |
| 1800 | |
Pekka Paalanen | c4db6f7 | 2017-09-05 16:37:03 +0300 | [diff] [blame] | 1801 | static int |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 1802 | drm_output_enable(struct weston_output *base) |
| 1803 | { |
| 1804 | struct drm_output *output = to_drm_output(base); |
| 1805 | struct drm_backend *b = to_drm_backend(base->compositor); |
Pekka Paalanen | 663d5e9 | 2017-09-08 13:32:40 +0300 | [diff] [blame] | 1806 | int ret; |
| 1807 | |
Tomohito Esaki | b1fb00d | 2018-01-31 17:50:48 +0900 | [diff] [blame] | 1808 | assert(!output->virtual); |
| 1809 | |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1810 | ret = drm_output_attach_crtc(output); |
Pekka Paalanen | 663d5e9 | 2017-09-08 13:32:40 +0300 | [diff] [blame] | 1811 | if (ret < 0) |
| 1812 | return -1; |
| 1813 | |
Leandro Ribeiro | 3be23ef | 2020-08-13 16:44:02 -0300 | [diff] [blame] | 1814 | ret = drm_output_init_planes(output); |
| 1815 | if (ret < 0) |
| 1816 | goto err_crtc; |
| 1817 | |
Pekka Paalanen | 663d5e9 | 2017-09-08 13:32:40 +0300 | [diff] [blame] | 1818 | if (drm_output_init_gamma_size(output) < 0) |
Leandro Ribeiro | 3be23ef | 2020-08-13 16:44:02 -0300 | [diff] [blame] | 1819 | goto err_planes; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 1820 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 1821 | if (b->pageflip_timeout) |
| 1822 | drm_output_pageflip_timer_create(output); |
| 1823 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1824 | if (b->use_pixman) { |
| 1825 | if (drm_output_init_pixman(output, b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1826 | weston_log("Failed to init output pixman state\n"); |
Leandro Ribeiro | 3be23ef | 2020-08-13 16:44:02 -0300 | [diff] [blame] | 1827 | goto err_planes; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1828 | } |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1829 | } else if (drm_output_init_egl(output, b) < 0) { |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 1830 | weston_log("Failed to init output gl state\n"); |
Leandro Ribeiro | 3be23ef | 2020-08-13 16:44:02 -0300 | [diff] [blame] | 1831 | goto err_planes; |
Kristian Høgsberg | 1d1e0a5 | 2012-10-21 13:29:26 -0400 | [diff] [blame] | 1832 | } |
Kristian Høgsberg | 8e1f77f | 2012-05-03 11:39:35 -0400 | [diff] [blame] | 1833 | |
leng.fang | 32af9fc | 2024-06-13 11:22:15 +0800 | [diff] [blame] | 1834 | drm_enable_aml_output_config(output, base, b); |
Pekka Paalanen | f8b850d | 2017-11-15 12:51:01 +0200 | [diff] [blame] | 1835 | drm_output_init_backlight(output); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1836 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 1837 | output->base.start_repaint_loop = drm_output_start_repaint_loop; |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 1838 | output->base.repaint = drm_output_repaint; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1839 | output->base.assign_planes = drm_assign_planes; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1840 | output->base.set_dpms = drm_set_dpms; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1841 | output->base.switch_mode = drm_output_switch_mode; |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 1842 | output->base.set_gamma = drm_output_set_gamma; |
| 1843 | |
Pekka Paalanen | c0eb254 | 2017-11-15 13:37:18 +0200 | [diff] [blame] | 1844 | weston_log("Output %s (crtc %d) video modes:\n", |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1845 | output->base.name, output->crtc->crtc_id); |
Pekka Paalanen | c0eb254 | 2017-11-15 13:37:18 +0200 | [diff] [blame] | 1846 | drm_output_print_modes(output); |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 1847 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1848 | return 0; |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1849 | |
Leandro Ribeiro | 3be23ef | 2020-08-13 16:44:02 -0300 | [diff] [blame] | 1850 | err_planes: |
| 1851 | drm_output_deinit_planes(output); |
| 1852 | err_crtc: |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1853 | drm_output_detach_crtc(output); |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1854 | return -1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1855 | } |
| 1856 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1857 | static void |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 1858 | drm_output_deinit(struct weston_output *base) |
| 1859 | { |
| 1860 | struct drm_output *output = to_drm_output(base); |
| 1861 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 1862 | |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 1863 | if (b->use_pixman) |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 1864 | drm_output_fini_pixman(output); |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 1865 | else |
| 1866 | drm_output_fini_egl(output); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 1867 | |
Leandro Ribeiro | 3be23ef | 2020-08-13 16:44:02 -0300 | [diff] [blame] | 1868 | drm_output_deinit_planes(output); |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 1869 | drm_output_detach_crtc(output); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | static void |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 1873 | drm_head_destroy(struct drm_head *head); |
| 1874 | |
| 1875 | static void |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 1876 | drm_output_destroy(struct weston_output *base) |
| 1877 | { |
| 1878 | struct drm_output *output = to_drm_output(base); |
| 1879 | struct drm_backend *b = to_drm_backend(base->compositor); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 1880 | |
Tomohito Esaki | b1fb00d | 2018-01-31 17:50:48 +0900 | [diff] [blame] | 1881 | assert(!output->virtual); |
| 1882 | |
Daniel Stone | 31838bf | 2019-06-17 11:23:25 +0100 | [diff] [blame] | 1883 | if (output->page_flip_pending || output->atomic_complete_pending) { |
Emmanuel Gil Peyrot | 1b3ad09 | 2019-12-09 02:50:55 +0100 | [diff] [blame] | 1884 | output->destroy_pending = true; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 1885 | weston_log("destroy output while page flip pending\n"); |
| 1886 | return; |
| 1887 | } |
| 1888 | |
Alexandros Frantzis | 10937fe | 2021-06-14 13:09:44 +0300 | [diff] [blame] | 1889 | drm_output_set_cursor_view(output, NULL); |
| 1890 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 1891 | if (output->base.enabled) |
| 1892 | drm_output_deinit(&output->base); |
| 1893 | |
Pekka Paalanen | 383b3af | 2017-09-11 14:40:48 +0300 | [diff] [blame] | 1894 | drm_mode_list_destroy(b, &output->base.mode_list); |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 1895 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 1896 | if (output->pageflip_timer) |
| 1897 | wl_event_source_remove(output->pageflip_timer); |
| 1898 | |
Pekka Paalanen | ae6d35d | 2017-08-16 12:07:14 +0300 | [diff] [blame] | 1899 | weston_output_release(&output->base); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 1900 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1901 | assert(!output->state_last); |
| 1902 | drm_output_state_free(output->state_cur); |
| 1903 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 1904 | free(output); |
| 1905 | } |
| 1906 | |
| 1907 | static int |
| 1908 | drm_output_disable(struct weston_output *base) |
| 1909 | { |
| 1910 | struct drm_output *output = to_drm_output(base); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 1911 | |
Tomohito Esaki | b1fb00d | 2018-01-31 17:50:48 +0900 | [diff] [blame] | 1912 | assert(!output->virtual); |
leng.fang | 32af9fc | 2024-06-13 11:22:15 +0800 | [diff] [blame] | 1913 | drm_output_disable_aml(output, base); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1914 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 1915 | if (output->base.enabled) |
| 1916 | drm_output_deinit(&output->base); |
| 1917 | |
Emmanuel Gil Peyrot | 1b3ad09 | 2019-12-09 02:50:55 +0100 | [diff] [blame] | 1918 | output->disable_pending = false; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 1919 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 1920 | return 0; |
| 1921 | } |
| 1922 | |
Ankit Nautiyal | a344fe3 | 2019-05-14 18:36:08 +0530 | [diff] [blame] | 1923 | /* |
| 1924 | * This function converts the protection status from drm values to |
| 1925 | * weston_hdcp_protection status. The drm values as read from the connector |
| 1926 | * properties "Content Protection" and "HDCP Content Type" need to be converted |
| 1927 | * to appropriate weston values, that can be sent to a client application. |
| 1928 | */ |
| 1929 | static int |
| 1930 | get_weston_protection_from_drm(enum wdrm_content_protection_state protection, |
| 1931 | enum wdrm_hdcp_content_type type, |
| 1932 | enum weston_hdcp_protection *weston_protection) |
| 1933 | |
| 1934 | { |
| 1935 | if (protection >= WDRM_CONTENT_PROTECTION__COUNT) |
| 1936 | return -1; |
| 1937 | if (protection == WDRM_CONTENT_PROTECTION_DESIRED || |
| 1938 | protection == WDRM_CONTENT_PROTECTION_UNDESIRED) { |
| 1939 | *weston_protection = WESTON_HDCP_DISABLE; |
| 1940 | return 0; |
| 1941 | } |
| 1942 | if (type >= WDRM_HDCP_CONTENT_TYPE__COUNT) |
| 1943 | return -1; |
| 1944 | if (type == WDRM_HDCP_CONTENT_TYPE0) { |
| 1945 | *weston_protection = WESTON_HDCP_ENABLE_TYPE_0; |
| 1946 | return 0; |
| 1947 | } |
| 1948 | if (type == WDRM_HDCP_CONTENT_TYPE1) { |
| 1949 | *weston_protection = WESTON_HDCP_ENABLE_TYPE_1; |
| 1950 | return 0; |
| 1951 | } |
| 1952 | return -1; |
| 1953 | } |
| 1954 | |
| 1955 | /** |
| 1956 | * Get current content-protection status for a given head. |
| 1957 | * |
| 1958 | * @param head drm_head, whose protection is to be retrieved |
Ankit Nautiyal | a344fe3 | 2019-05-14 18:36:08 +0530 | [diff] [blame] | 1959 | * @return protection status in case of success, -1 otherwise |
| 1960 | */ |
| 1961 | static enum weston_hdcp_protection |
Leandro Ribeiro | 702fbf7 | 2020-08-18 17:35:05 -0300 | [diff] [blame] | 1962 | drm_head_get_current_protection(struct drm_head *head) |
Ankit Nautiyal | a344fe3 | 2019-05-14 18:36:08 +0530 | [diff] [blame] | 1963 | { |
Leandro Ribeiro | 702fbf7 | 2020-08-18 17:35:05 -0300 | [diff] [blame] | 1964 | drmModeObjectProperties *props = head->connector.props_drm; |
Ankit Nautiyal | a344fe3 | 2019-05-14 18:36:08 +0530 | [diff] [blame] | 1965 | struct drm_property_info *info; |
| 1966 | enum wdrm_content_protection_state protection; |
| 1967 | enum wdrm_hdcp_content_type type; |
| 1968 | enum weston_hdcp_protection weston_hdcp = WESTON_HDCP_DISABLE; |
| 1969 | |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 1970 | info = &head->connector.props[WDRM_CONNECTOR_CONTENT_PROTECTION]; |
Ankit Nautiyal | a344fe3 | 2019-05-14 18:36:08 +0530 | [diff] [blame] | 1971 | protection = drm_property_get_value(info, props, |
| 1972 | WDRM_CONTENT_PROTECTION__COUNT); |
| 1973 | |
| 1974 | if (protection == WDRM_CONTENT_PROTECTION__COUNT) |
| 1975 | return WESTON_HDCP_DISABLE; |
| 1976 | |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 1977 | info = &head->connector.props[WDRM_CONNECTOR_HDCP_CONTENT_TYPE]; |
Ankit Nautiyal | a344fe3 | 2019-05-14 18:36:08 +0530 | [diff] [blame] | 1978 | type = drm_property_get_value(info, props, |
| 1979 | WDRM_HDCP_CONTENT_TYPE__COUNT); |
| 1980 | |
| 1981 | /* |
| 1982 | * In case of platforms supporting HDCP1.4, only property |
| 1983 | * 'Content Protection' is exposed and not the 'HDCP Content Type' |
| 1984 | * for such cases HDCP Type 0 should be considered as the content-type. |
| 1985 | */ |
| 1986 | |
| 1987 | if (type == WDRM_HDCP_CONTENT_TYPE__COUNT) |
| 1988 | type = WDRM_HDCP_CONTENT_TYPE0; |
| 1989 | |
| 1990 | if (get_weston_protection_from_drm(protection, type, |
| 1991 | &weston_hdcp) == -1) { |
| 1992 | weston_log("Invalid drm protection:%d type:%d, for head:%s connector-id:%d\n", |
| 1993 | protection, type, head->base.name, |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 1994 | head->connector.connector_id); |
Ankit Nautiyal | a344fe3 | 2019-05-14 18:36:08 +0530 | [diff] [blame] | 1995 | return WESTON_HDCP_DISABLE; |
| 1996 | } |
| 1997 | |
| 1998 | return weston_hdcp; |
| 1999 | } |
| 2000 | |
Leandro Ribeiro | 702fbf7 | 2020-08-18 17:35:05 -0300 | [diff] [blame] | 2001 | static int |
| 2002 | drm_connector_update_properties(struct drm_connector *connector) |
| 2003 | { |
| 2004 | drmModeObjectProperties *props; |
| 2005 | |
| 2006 | props = drmModeObjectGetProperties(connector->backend->drm.fd, |
| 2007 | connector->connector_id, |
| 2008 | DRM_MODE_OBJECT_CONNECTOR); |
| 2009 | if (!props) { |
| 2010 | weston_log("Error: failed to get connector properties\n"); |
| 2011 | return -1; |
| 2012 | } |
| 2013 | |
| 2014 | if (connector->props_drm) |
| 2015 | drmModeFreeObjectProperties(connector->props_drm); |
| 2016 | connector->props_drm = props; |
| 2017 | |
| 2018 | return 0; |
| 2019 | } |
| 2020 | |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 2021 | /** Replace connector data and monitor information |
| 2022 | * |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 2023 | * @param connector The drm_connector object to be updated. |
| 2024 | * @param conn The connector data to be owned by the drm_connector, must match |
| 2025 | * the current drm_connector ID. |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 2026 | * @return 0 on success, -1 on failure. |
| 2027 | * |
| 2028 | * Takes ownership of @c connector on success, not on failure. |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 2029 | */ |
| 2030 | static int |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 2031 | drm_connector_assign_connector_info(struct drm_connector *connector, |
| 2032 | drmModeConnector *conn) |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 2033 | { |
Leandro Ribeiro | 7086396 | 2020-09-09 13:12:35 -0300 | [diff] [blame] | 2034 | assert(connector->conn != conn); |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 2035 | assert(connector->connector_id == conn->connector_id); |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 2036 | |
Leandro Ribeiro | 702fbf7 | 2020-08-18 17:35:05 -0300 | [diff] [blame] | 2037 | if (drm_connector_update_properties(connector) < 0) |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 2038 | return -1; |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 2039 | |
leng.fang | 32af9fc | 2024-06-13 11:22:15 +0800 | [diff] [blame] | 2040 | drm_update_helper_connector(connector, conn); |
| 2041 | |
Leandro Ribeiro | 7086396 | 2020-09-09 13:12:35 -0300 | [diff] [blame] | 2042 | if (connector->conn) |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 2043 | drmModeFreeConnector(connector->conn); |
| 2044 | connector->conn = conn; |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 2045 | |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 2046 | drm_property_info_free(connector->props, WDRM_CONNECTOR__COUNT); |
| 2047 | drm_property_info_populate(connector->backend, connector_props, |
| 2048 | connector->props, |
Leandro Ribeiro | 702fbf7 | 2020-08-18 17:35:05 -0300 | [diff] [blame] | 2049 | WDRM_CONNECTOR__COUNT, connector->props_drm); |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 2050 | return 0; |
| 2051 | } |
| 2052 | |
Leandro Ribeiro | 7086396 | 2020-09-09 13:12:35 -0300 | [diff] [blame] | 2053 | static void |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 2054 | drm_connector_init(struct drm_backend *b, struct drm_connector *connector, |
| 2055 | uint32_t connector_id) |
| 2056 | { |
| 2057 | connector->backend = b; |
| 2058 | connector->connector_id = connector_id; |
Leandro Ribeiro | 7086396 | 2020-09-09 13:12:35 -0300 | [diff] [blame] | 2059 | connector->conn = NULL; |
Leandro Ribeiro | 702fbf7 | 2020-08-18 17:35:05 -0300 | [diff] [blame] | 2060 | connector->props_drm = NULL; |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 2061 | } |
| 2062 | |
| 2063 | static void |
| 2064 | drm_connector_fini(struct drm_connector *connector) |
| 2065 | { |
| 2066 | drmModeFreeConnector(connector->conn); |
Leandro Ribeiro | 702fbf7 | 2020-08-18 17:35:05 -0300 | [diff] [blame] | 2067 | drmModeFreeObjectProperties(connector->props_drm); |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 2068 | drm_property_info_free(connector->props, WDRM_CONNECTOR__COUNT); |
| 2069 | } |
| 2070 | |
Pekka Paalanen | 456dc73 | 2017-11-09 15:10:11 +0200 | [diff] [blame] | 2071 | static void |
| 2072 | drm_head_log_info(struct drm_head *head, const char *msg) |
| 2073 | { |
| 2074 | if (head->base.connected) { |
| 2075 | weston_log("DRM: head '%s' %s, connector %d is connected, " |
| 2076 | "EDID make '%s', model '%s', serial '%s'\n", |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 2077 | head->base.name, msg, head->connector.connector_id, |
Pekka Paalanen | 456dc73 | 2017-11-09 15:10:11 +0200 | [diff] [blame] | 2078 | head->base.make, head->base.model, |
| 2079 | head->base.serial_number ?: ""); |
| 2080 | } else { |
| 2081 | weston_log("DRM: head '%s' %s, connector %d is disconnected.\n", |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 2082 | head->base.name, msg, head->connector.connector_id); |
Pekka Paalanen | 456dc73 | 2017-11-09 15:10:11 +0200 | [diff] [blame] | 2083 | } |
| 2084 | } |
| 2085 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 2086 | /** Update connector and monitor information |
| 2087 | * |
| 2088 | * @param head The head to update. |
Leandro Ribeiro | 7086396 | 2020-09-09 13:12:35 -0300 | [diff] [blame] | 2089 | * @param conn The DRM connector object. |
| 2090 | * @returns 0 on success, -1 on failure. |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 2091 | * |
Leandro Ribeiro | 7086396 | 2020-09-09 13:12:35 -0300 | [diff] [blame] | 2092 | * Updates monitor information and connection status. This may schedule a |
| 2093 | * heads changed call to the user. |
Leandro Ribeiro | 4a5b67a | 2020-09-15 15:30:35 -0300 | [diff] [blame] | 2094 | * |
| 2095 | * Takes ownership of @c connector on success, not on failure. |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 2096 | */ |
Leandro Ribeiro | 7086396 | 2020-09-09 13:12:35 -0300 | [diff] [blame] | 2097 | static int |
| 2098 | drm_head_update_info(struct drm_head *head, drmModeConnector *conn) |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 2099 | { |
Leandro Ribeiro | 7086396 | 2020-09-09 13:12:35 -0300 | [diff] [blame] | 2100 | int ret; |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 2101 | |
Leandro Ribeiro | 7086396 | 2020-09-09 13:12:35 -0300 | [diff] [blame] | 2102 | ret = drm_connector_assign_connector_info(&head->connector, conn); |
Pekka Paalanen | 456dc73 | 2017-11-09 15:10:11 +0200 | [diff] [blame] | 2103 | |
Leandro Ribeiro | 6794164 | 2020-09-15 00:11:02 -0300 | [diff] [blame] | 2104 | update_head_from_connector(head); |
| 2105 | weston_head_set_content_protection_status(&head->base, |
| 2106 | drm_head_get_current_protection(head)); |
| 2107 | |
leng.fang | 9185607 | 2024-06-07 14:12:54 +0800 | [diff] [blame] | 2108 | #ifdef ENABLE_MODE_POLICY |
| 2109 | if (head->base.connected) |
| 2110 | init_mode_policy_without_mode(&head->base, |
| 2111 | head->backend->drm.fd, head->connector.conn); |
| 2112 | if (head->base.device_changed) |
| 2113 | mode_policy_set_hotplug(head->base.connected ? |
leng.fang | 13fd398 | 2024-07-03 19:09:58 +0800 | [diff] [blame] | 2114 | AML_WESTON_HOTPLUG_PLUG : AML_WESTON_HOTPLUG_UNPLUG, false); |
leng.fang | 9185607 | 2024-06-07 14:12:54 +0800 | [diff] [blame] | 2115 | #endif |
| 2116 | |
Pekka Paalanen | 456dc73 | 2017-11-09 15:10:11 +0200 | [diff] [blame] | 2117 | if (head->base.device_changed) |
| 2118 | drm_head_log_info(head, "updated"); |
Leandro Ribeiro | 7086396 | 2020-09-09 13:12:35 -0300 | [diff] [blame] | 2119 | |
| 2120 | return ret; |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 2121 | } |
| 2122 | |
Leandro Ribeiro | 96bef05 | 2020-09-09 15:23:49 -0300 | [diff] [blame] | 2123 | /** Update writeback connector |
| 2124 | * |
| 2125 | * @param writeback The writeback to update. |
| 2126 | * @param conn DRM connector object. |
| 2127 | * @returns 0 on success, -1 on failure. |
| 2128 | * |
| 2129 | * Takes ownership of @c connector on success, not on failure. |
| 2130 | */ |
| 2131 | static int |
| 2132 | drm_writeback_update_info(struct drm_writeback *writeback, drmModeConnector *conn) |
| 2133 | { |
| 2134 | int ret; |
| 2135 | |
| 2136 | ret = drm_connector_assign_connector_info(&writeback->connector, conn); |
| 2137 | |
| 2138 | return ret; |
| 2139 | } |
| 2140 | |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 2141 | /** |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 2142 | * Create a Weston head for a connector |
| 2143 | * |
| 2144 | * Given a DRM connector, create a matching drm_head structure and add it |
| 2145 | * to Weston's head list. |
| 2146 | * |
Marius Vlad | a2dace2 | 2019-06-12 16:05:44 +0300 | [diff] [blame] | 2147 | * @param backend Weston backend structure |
Leandro Ribeiro | 7086396 | 2020-09-09 13:12:35 -0300 | [diff] [blame] | 2148 | * @param conn DRM connector object |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 2149 | * @param drm_device udev device pointer |
Leandro Ribeiro | 7086396 | 2020-09-09 13:12:35 -0300 | [diff] [blame] | 2150 | * @returns 0 on success, -1 on failure |
Leandro Ribeiro | 4a5b67a | 2020-09-15 15:30:35 -0300 | [diff] [blame] | 2151 | * |
| 2152 | * Takes ownership of @c connector on success, not on failure. |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 2153 | */ |
Leandro Ribeiro | 7086396 | 2020-09-09 13:12:35 -0300 | [diff] [blame] | 2154 | static int |
| 2155 | drm_head_create(struct drm_backend *backend, drmModeConnector *conn, |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 2156 | struct udev_device *drm_device) |
| 2157 | { |
| 2158 | struct drm_head *head; |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 2159 | char *name; |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 2160 | int ret; |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 2161 | |
| 2162 | head = zalloc(sizeof *head); |
| 2163 | if (!head) |
Leandro Ribeiro | 7086396 | 2020-09-09 13:12:35 -0300 | [diff] [blame] | 2164 | return -1; |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 2165 | |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 2166 | head->backend = backend; |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 2167 | |
Leandro Ribeiro | 7086396 | 2020-09-09 13:12:35 -0300 | [diff] [blame] | 2168 | drm_connector_init(backend, &head->connector, conn->connector_id); |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 2169 | |
| 2170 | name = make_connector_name(conn); |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 2171 | if (!name) |
Leandro Ribeiro | 7086396 | 2020-09-09 13:12:35 -0300 | [diff] [blame] | 2172 | goto err; |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 2173 | |
| 2174 | weston_head_init(&head->base, name); |
| 2175 | free(name); |
| 2176 | |
Leandro Ribeiro | 6794164 | 2020-09-15 00:11:02 -0300 | [diff] [blame] | 2177 | ret = drm_head_update_info(head, conn); |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 2178 | if (ret < 0) |
Leandro Ribeiro | 6794164 | 2020-09-15 00:11:02 -0300 | [diff] [blame] | 2179 | goto err_update; |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 2180 | |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 2181 | head->backlight = backlight_init(drm_device, conn->connector_type); |
Pekka Paalanen | ce72424 | 2017-09-04 12:21:24 +0300 | [diff] [blame] | 2182 | |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 2183 | if (conn->connector_type == DRM_MODE_CONNECTOR_LVDS || |
| 2184 | conn->connector_type == DRM_MODE_CONNECTOR_eDP) |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 2185 | weston_head_set_internal(&head->base); |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 2186 | |
Pekka Paalanen | 9c03a7c | 2017-11-28 14:30:10 +0200 | [diff] [blame] | 2187 | if (drm_head_read_current_setup(head, backend) < 0) { |
Pekka Paalanen | 13d233e | 2017-09-11 14:06:11 +0300 | [diff] [blame] | 2188 | weston_log("Failed to retrieve current mode from connector %d.\n", |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 2189 | head->connector.connector_id); |
Pekka Paalanen | 6fae2be | 2017-11-28 14:33:52 +0200 | [diff] [blame] | 2190 | /* Not fatal. */ |
Pekka Paalanen | 13d233e | 2017-09-11 14:06:11 +0300 | [diff] [blame] | 2191 | } |
| 2192 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 2193 | weston_compositor_add_head(backend->compositor, &head->base); |
Pekka Paalanen | 456dc73 | 2017-11-09 15:10:11 +0200 | [diff] [blame] | 2194 | drm_head_log_info(head, "found"); |
leng.fang | 32af9fc | 2024-06-13 11:22:15 +0800 | [diff] [blame] | 2195 | drm_append_helper_connector(head); |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 2196 | |
Leandro Ribeiro | 7086396 | 2020-09-09 13:12:35 -0300 | [diff] [blame] | 2197 | return 0; |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 2198 | |
Leandro Ribeiro | 6794164 | 2020-09-15 00:11:02 -0300 | [diff] [blame] | 2199 | err_update: |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 2200 | weston_head_release(&head->base); |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 2201 | err: |
Leandro Ribeiro | 7086396 | 2020-09-09 13:12:35 -0300 | [diff] [blame] | 2202 | drm_connector_fini(&head->connector); |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 2203 | free(head); |
Leandro Ribeiro | 7086396 | 2020-09-09 13:12:35 -0300 | [diff] [blame] | 2204 | return -1; |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 2205 | } |
| 2206 | |
| 2207 | static void |
| 2208 | drm_head_destroy(struct drm_head *head) |
| 2209 | { |
leng.fang | 32af9fc | 2024-06-13 11:22:15 +0800 | [diff] [blame] | 2210 | drm_delete_helper_connector(head); |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 2211 | weston_head_release(&head->base); |
Pekka Paalanen | ce72424 | 2017-09-04 12:21:24 +0300 | [diff] [blame] | 2212 | |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 2213 | drm_connector_fini(&head->connector); |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 2214 | |
Pekka Paalanen | ce72424 | 2017-09-04 12:21:24 +0300 | [diff] [blame] | 2215 | if (head->backlight) |
| 2216 | backlight_destroy(head->backlight); |
| 2217 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 2218 | free(head); |
| 2219 | } |
| 2220 | |
| 2221 | /** |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2222 | * Create a Weston output structure |
| 2223 | * |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 2224 | * Create an "empty" drm_output. This is the implementation of |
| 2225 | * weston_backend::create_output. |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2226 | * |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 2227 | * Creating an output is usually followed by drm_output_attach_head() |
| 2228 | * and drm_output_enable() to make use of it. |
| 2229 | * |
| 2230 | * @param compositor The compositor instance. |
| 2231 | * @param name Name for the new output. |
| 2232 | * @returns The output, or NULL on failure. |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2233 | */ |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 2234 | static struct weston_output * |
| 2235 | drm_output_create(struct weston_compositor *compositor, const char *name) |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2236 | { |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 2237 | struct drm_backend *b = to_drm_backend(compositor); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2238 | struct drm_output *output; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2239 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2240 | output = zalloc(sizeof *output); |
| 2241 | if (output == NULL) |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 2242 | return NULL; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2243 | |
Daniel Stone | 64dbbee | 2018-07-20 19:00:06 +0100 | [diff] [blame] | 2244 | output->backend = b; |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 2245 | output->crtc = NULL; |
| 2246 | |
Stefan Agner | ccf2407 | 2019-07-09 22:02:00 +0200 | [diff] [blame] | 2247 | #ifdef BUILD_DRM_GBM |
Tomohito Esaki | 718a40b | 2018-01-31 17:50:15 +0900 | [diff] [blame] | 2248 | output->gbm_bo_flags = GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING; |
Stefan Agner | ccf2407 | 2019-07-09 22:02:00 +0200 | [diff] [blame] | 2249 | #endif |
Daniel Stone | 64dbbee | 2018-07-20 19:00:06 +0100 | [diff] [blame] | 2250 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 2251 | weston_output_init(&output->base, compositor, name); |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 2252 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2253 | output->base.enable = drm_output_enable; |
| 2254 | output->base.destroy = drm_output_destroy; |
| 2255 | output->base.disable = drm_output_disable; |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 2256 | output->base.attach_head = drm_output_attach_head; |
Pekka Paalanen | 7f85379 | 2017-11-29 14:33:33 +0200 | [diff] [blame] | 2257 | output->base.detach_head = drm_output_detach_head; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2258 | |
Emmanuel Gil Peyrot | 1b3ad09 | 2019-12-09 02:50:55 +0100 | [diff] [blame] | 2259 | output->destroy_pending = false; |
| 2260 | output->disable_pending = false; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2261 | |
Pekka Paalanen | 01f6021 | 2017-03-24 15:39:24 +0200 | [diff] [blame] | 2262 | output->state_cur = drm_output_state_alloc(output, NULL); |
Pekka Paalanen | a0bfedc | 2017-04-03 14:42:51 +0300 | [diff] [blame] | 2263 | |
leng.fang | 32af9fc | 2024-06-13 11:22:15 +0800 | [diff] [blame] | 2264 | drm_init_aml_output_config(compositor, output); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2265 | weston_compositor_add_pending_output(&output->base, b->compositor); |
| 2266 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 2267 | return &output->base; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2268 | } |
| 2269 | |
Leandro Ribeiro | 96bef05 | 2020-09-09 15:23:49 -0300 | [diff] [blame] | 2270 | /** |
| 2271 | * Create a Weston writeback for a writeback connector |
Leandro Ribeiro | 7243022 | 2020-09-14 17:41:41 -0300 | [diff] [blame] | 2272 | * |
Leandro Ribeiro | 96bef05 | 2020-09-09 15:23:49 -0300 | [diff] [blame] | 2273 | * Given a DRM connector of type writeback, create a matching drm_writeback |
| 2274 | * structure and add it to Weston's writeback list. |
| 2275 | * |
| 2276 | * @param b Weston backend structure |
| 2277 | * @param conn DRM connector object of type writeback |
| 2278 | * @returns 0 on success, -1 on failure |
| 2279 | * |
| 2280 | * Takes ownership of @c connector on success, not on failure. |
| 2281 | */ |
| 2282 | static int |
| 2283 | drm_writeback_create(struct drm_backend *b, drmModeConnector *conn) |
| 2284 | { |
| 2285 | struct drm_writeback *writeback; |
| 2286 | int ret; |
| 2287 | |
| 2288 | writeback = zalloc(sizeof *writeback); |
| 2289 | assert(writeback); |
| 2290 | |
| 2291 | writeback->backend = b; |
| 2292 | |
| 2293 | drm_connector_init(b, &writeback->connector, conn->connector_id); |
| 2294 | |
| 2295 | ret = drm_writeback_update_info(writeback, conn); |
| 2296 | if (ret < 0) |
| 2297 | goto err; |
| 2298 | |
| 2299 | wl_list_insert(&b->writeback_connector_list, &writeback->link); |
| 2300 | return 0; |
| 2301 | |
| 2302 | err: |
| 2303 | drm_connector_fini(&writeback->connector); |
| 2304 | free(writeback); |
| 2305 | return -1; |
| 2306 | } |
| 2307 | |
| 2308 | static void |
| 2309 | drm_writeback_destroy(struct drm_writeback *writeback) |
| 2310 | { |
| 2311 | drm_connector_fini(&writeback->connector); |
| 2312 | wl_list_remove(&writeback->link); |
| 2313 | |
| 2314 | free(writeback); |
| 2315 | } |
| 2316 | |
| 2317 | /** Given the DRM connector object of a connector, create drm_head or |
| 2318 | * drm_writeback object (depending on the type of connector) for it. |
| 2319 | * |
| 2320 | * The object is then added to the DRM-backend list of heads or writebacks. |
Leandro Ribeiro | 7243022 | 2020-09-14 17:41:41 -0300 | [diff] [blame] | 2321 | * |
| 2322 | * @param b The DRM-backend structure |
| 2323 | * @param conn The DRM connector object |
| 2324 | * @param drm_device udev device pointer |
| 2325 | * @return 0 on success, -1 on failure |
| 2326 | */ |
| 2327 | static int |
| 2328 | drm_backend_add_connector(struct drm_backend *b, drmModeConnector *conn, |
| 2329 | struct udev_device *drm_device) |
| 2330 | { |
| 2331 | int ret; |
| 2332 | |
Leandro Ribeiro | 96bef05 | 2020-09-09 15:23:49 -0300 | [diff] [blame] | 2333 | if (conn->connector_type == DRM_MODE_CONNECTOR_WRITEBACK) { |
| 2334 | ret = drm_writeback_create(b, conn); |
| 2335 | if (ret < 0) |
| 2336 | weston_log("DRM: failed to create writeback for connector %d.\n", |
| 2337 | conn->connector_id); |
| 2338 | } else { |
| 2339 | ret = drm_head_create(b, conn, drm_device); |
| 2340 | if (ret < 0) |
| 2341 | weston_log("DRM: failed to create head for connector %d.\n", |
| 2342 | conn->connector_id); |
| 2343 | } |
Leandro Ribeiro | 7243022 | 2020-09-14 17:41:41 -0300 | [diff] [blame] | 2344 | |
| 2345 | return ret; |
| 2346 | } |
| 2347 | |
Leandro Ribeiro | 96bef05 | 2020-09-09 15:23:49 -0300 | [diff] [blame] | 2348 | /** Find all connectors of the fd and create drm_head or drm_writeback objects |
| 2349 | * (depending on the type of connector they are) for each of them |
| 2350 | * |
| 2351 | * These objects are added to the DRM-backend lists of heads and writebacks. |
| 2352 | * |
| 2353 | * @param b The DRM-backend structure |
| 2354 | * @param drm_device udev device pointer |
Igor Matheus Andrade Torrente | 6354455 | 2020-10-13 11:25:20 -0300 | [diff] [blame] | 2355 | * @param resources The DRM resources, it is taken with drmModeGetResources |
Leandro Ribeiro | 96bef05 | 2020-09-09 15:23:49 -0300 | [diff] [blame] | 2356 | * @return 0 on success, -1 on failure |
| 2357 | */ |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2358 | static int |
Igor Matheus Andrade Torrente | 6354455 | 2020-10-13 11:25:20 -0300 | [diff] [blame] | 2359 | drm_backend_discover_connectors(struct drm_backend *b, struct udev_device *drm_device, |
| 2360 | drmModeRes *resources) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2361 | { |
Leandro Ribeiro | 7086396 | 2020-09-09 13:12:35 -0300 | [diff] [blame] | 2362 | drmModeConnector *conn; |
| 2363 | int i, ret; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2364 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2365 | b->min_width = resources->min_width; |
| 2366 | b->max_width = resources->max_width; |
| 2367 | b->min_height = resources->min_height; |
| 2368 | b->max_height = resources->max_height; |
Rob Clark | 4339add | 2012-08-09 14:18:28 -0500 | [diff] [blame] | 2369 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2370 | for (i = 0; i < resources->count_connectors; i++) { |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 2371 | uint32_t connector_id = resources->connectors[i]; |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 2372 | |
Leandro Ribeiro | 7086396 | 2020-09-09 13:12:35 -0300 | [diff] [blame] | 2373 | conn = drmModeGetConnector(b->drm.fd, connector_id); |
| 2374 | if (!conn) |
| 2375 | continue; |
| 2376 | |
Leandro Ribeiro | 7243022 | 2020-09-14 17:41:41 -0300 | [diff] [blame] | 2377 | ret = drm_backend_add_connector(b, conn, drm_device); |
| 2378 | if (ret < 0) |
Leandro Ribeiro | 7086396 | 2020-09-09 13:12:35 -0300 | [diff] [blame] | 2379 | drmModeFreeConnector(conn); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2380 | } |
| 2381 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2382 | return 0; |
| 2383 | } |
| 2384 | |
Leandro Ribeiro | 99611c8 | 2020-09-14 17:19:12 -0300 | [diff] [blame] | 2385 | static bool |
| 2386 | resources_has_connector(drmModeRes *resources, uint32_t connector_id) |
| 2387 | { |
| 2388 | for (int i = 0; i < resources->count_connectors; i++) { |
| 2389 | if (resources->connectors[i] == connector_id) |
| 2390 | return true; |
| 2391 | } |
| 2392 | |
| 2393 | return false; |
| 2394 | } |
| 2395 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2396 | static void |
Leandro Ribeiro | 993920d | 2020-09-14 17:01:01 -0300 | [diff] [blame] | 2397 | drm_backend_update_connectors(struct drm_backend *b, struct udev_device *drm_device) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2398 | { |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2399 | drmModeRes *resources; |
Leandro Ribeiro | 7086396 | 2020-09-09 13:12:35 -0300 | [diff] [blame] | 2400 | drmModeConnector *conn; |
Leandro Ribeiro | 96bef05 | 2020-09-09 15:23:49 -0300 | [diff] [blame] | 2401 | struct weston_head *base, *base_next; |
Pekka Paalanen | a0a3746 | 2017-08-31 15:41:57 +0300 | [diff] [blame] | 2402 | struct drm_head *head; |
Leandro Ribeiro | 96bef05 | 2020-09-09 15:23:49 -0300 | [diff] [blame] | 2403 | struct drm_writeback *writeback, *writeback_next; |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 2404 | uint32_t connector_id; |
Leandro Ribeiro | 7086396 | 2020-09-09 13:12:35 -0300 | [diff] [blame] | 2405 | int i, ret; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2406 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2407 | resources = drmModeGetResources(b->drm.fd); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2408 | if (!resources) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2409 | weston_log("drmModeGetResources failed\n"); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2410 | return; |
| 2411 | } |
| 2412 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 2413 | /* collect new connectors that have appeared, e.g. MST */ |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2414 | for (i = 0; i < resources->count_connectors; i++) { |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 2415 | connector_id = resources->connectors[i]; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 2416 | |
Leandro Ribeiro | 7086396 | 2020-09-09 13:12:35 -0300 | [diff] [blame] | 2417 | conn = drmModeGetConnector(b->drm.fd, connector_id); |
| 2418 | if (!conn) |
| 2419 | continue; |
| 2420 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 2421 | head = drm_head_find_by_connector(b, connector_id); |
Leandro Ribeiro | 96bef05 | 2020-09-09 15:23:49 -0300 | [diff] [blame] | 2422 | writeback = drm_writeback_find_by_connector(b, connector_id); |
| 2423 | |
| 2424 | /* Connector can't be owned by both a head and a writeback, so |
| 2425 | * one of the searches must fail. */ |
| 2426 | assert(head == NULL || writeback == NULL); |
| 2427 | |
Leandro Ribeiro | 7243022 | 2020-09-14 17:41:41 -0300 | [diff] [blame] | 2428 | if (head) |
Leandro Ribeiro | 7086396 | 2020-09-09 13:12:35 -0300 | [diff] [blame] | 2429 | ret = drm_head_update_info(head, conn); |
Leandro Ribeiro | 96bef05 | 2020-09-09 15:23:49 -0300 | [diff] [blame] | 2430 | else if (writeback) |
| 2431 | ret = drm_writeback_update_info(writeback, conn); |
Leandro Ribeiro | 7243022 | 2020-09-14 17:41:41 -0300 | [diff] [blame] | 2432 | else |
| 2433 | ret = drm_backend_add_connector(b, conn, drm_device); |
| 2434 | |
| 2435 | if (ret < 0) |
| 2436 | drmModeFreeConnector(conn); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2437 | } |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2438 | |
Leandro Ribeiro | 96bef05 | 2020-09-09 15:23:49 -0300 | [diff] [blame] | 2439 | /* Destroy head objects of connectors (except writeback connectors) that |
| 2440 | * have disappeared. */ |
| 2441 | wl_list_for_each_safe(base, base_next, |
Pekka Paalanen | a0a3746 | 2017-08-31 15:41:57 +0300 | [diff] [blame] | 2442 | &b->compositor->head_list, compositor_link) { |
Pekka Paalanen | a0a3746 | 2017-08-31 15:41:57 +0300 | [diff] [blame] | 2443 | head = to_drm_head(base); |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 2444 | connector_id = head->connector.connector_id; |
Pekka Paalanen | a0a3746 | 2017-08-31 15:41:57 +0300 | [diff] [blame] | 2445 | |
Leandro Ribeiro | 99611c8 | 2020-09-14 17:19:12 -0300 | [diff] [blame] | 2446 | if (resources_has_connector(resources, connector_id)) |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 2447 | continue; |
| 2448 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 2449 | weston_log("DRM: head '%s' (connector %d) disappeared.\n", |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 2450 | head->base.name, connector_id); |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 2451 | drm_head_destroy(head); |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 2452 | } |
| 2453 | |
Leandro Ribeiro | 96bef05 | 2020-09-09 15:23:49 -0300 | [diff] [blame] | 2454 | /* Destroy writeback objects of writeback connectors that have |
| 2455 | * disappeared. */ |
| 2456 | wl_list_for_each_safe(writeback, writeback_next, |
| 2457 | &b->writeback_connector_list, link) { |
| 2458 | connector_id = writeback->connector.connector_id; |
| 2459 | |
| 2460 | if (resources_has_connector(resources, connector_id)) |
| 2461 | continue; |
| 2462 | |
| 2463 | weston_log("DRM: writeback connector (connector %d) disappeared.\n", |
| 2464 | connector_id); |
| 2465 | drm_writeback_destroy(writeback); |
| 2466 | } |
| 2467 | |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 2468 | drmModeFreeResources(resources); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2469 | } |
| 2470 | |
Ankit Nautiyal | a344fe3 | 2019-05-14 18:36:08 +0530 | [diff] [blame] | 2471 | static enum wdrm_connector_property |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 2472 | drm_connector_find_property_by_id(struct drm_connector *connector, |
| 2473 | uint32_t property_id) |
Ankit Nautiyal | a344fe3 | 2019-05-14 18:36:08 +0530 | [diff] [blame] | 2474 | { |
| 2475 | int i; |
| 2476 | enum wdrm_connector_property prop = WDRM_CONNECTOR__COUNT; |
| 2477 | |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 2478 | if (!connector || !property_id) |
Ankit Nautiyal | a344fe3 | 2019-05-14 18:36:08 +0530 | [diff] [blame] | 2479 | return WDRM_CONNECTOR__COUNT; |
| 2480 | |
| 2481 | for (i = 0; i < WDRM_CONNECTOR__COUNT; i++) |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 2482 | if (connector->props[i].prop_id == property_id) { |
Ankit Nautiyal | a344fe3 | 2019-05-14 18:36:08 +0530 | [diff] [blame] | 2483 | prop = (enum wdrm_connector_property) i; |
| 2484 | break; |
| 2485 | } |
| 2486 | return prop; |
| 2487 | } |
| 2488 | |
| 2489 | static void |
| 2490 | drm_backend_update_conn_props(struct drm_backend *b, |
| 2491 | uint32_t connector_id, |
| 2492 | uint32_t property_id) |
| 2493 | { |
| 2494 | struct drm_head *head; |
| 2495 | enum wdrm_connector_property conn_prop; |
Ankit Nautiyal | a344fe3 | 2019-05-14 18:36:08 +0530 | [diff] [blame] | 2496 | |
| 2497 | head = drm_head_find_by_connector(b, connector_id); |
| 2498 | if (!head) { |
| 2499 | weston_log("DRM: failed to find head for connector id: %d.\n", |
| 2500 | connector_id); |
| 2501 | return; |
| 2502 | } |
| 2503 | |
Leandro Ribeiro | e636990 | 2020-06-17 11:09:47 -0300 | [diff] [blame] | 2504 | conn_prop = drm_connector_find_property_by_id(&head->connector, property_id); |
Ankit Nautiyal | a344fe3 | 2019-05-14 18:36:08 +0530 | [diff] [blame] | 2505 | if (conn_prop >= WDRM_CONNECTOR__COUNT) |
| 2506 | return; |
| 2507 | |
Leandro Ribeiro | 702fbf7 | 2020-08-18 17:35:05 -0300 | [diff] [blame] | 2508 | if (drm_connector_update_properties(&head->connector) < 0) |
Ankit Nautiyal | a344fe3 | 2019-05-14 18:36:08 +0530 | [diff] [blame] | 2509 | return; |
Leandro Ribeiro | 702fbf7 | 2020-08-18 17:35:05 -0300 | [diff] [blame] | 2510 | |
Ankit Nautiyal | a344fe3 | 2019-05-14 18:36:08 +0530 | [diff] [blame] | 2511 | if (conn_prop == WDRM_CONNECTOR_CONTENT_PROTECTION) { |
| 2512 | weston_head_set_content_protection_status(&head->base, |
Leandro Ribeiro | 702fbf7 | 2020-08-18 17:35:05 -0300 | [diff] [blame] | 2513 | drm_head_get_current_protection(head)); |
Ankit Nautiyal | a344fe3 | 2019-05-14 18:36:08 +0530 | [diff] [blame] | 2514 | } |
Ankit Nautiyal | a344fe3 | 2019-05-14 18:36:08 +0530 | [diff] [blame] | 2515 | } |
| 2516 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2517 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2518 | udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2519 | { |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 2520 | const char *sysnum; |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 2521 | const char *val; |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 2522 | |
| 2523 | sysnum = udev_device_get_sysnum(device); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2524 | if (!sysnum || atoi(sysnum) != b->drm.id) |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 2525 | return 0; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 2526 | |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 2527 | val = udev_device_get_property_value(device, "HOTPLUG"); |
| 2528 | if (!val) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2529 | return 0; |
| 2530 | |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 2531 | return strcmp(val, "1") == 0; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2532 | } |
| 2533 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 2534 | static int |
Ankit Nautiyal | a344fe3 | 2019-05-14 18:36:08 +0530 | [diff] [blame] | 2535 | udev_event_is_conn_prop_change(struct drm_backend *b, |
| 2536 | struct udev_device *device, |
| 2537 | uint32_t *connector_id, |
| 2538 | uint32_t *property_id) |
| 2539 | |
| 2540 | { |
| 2541 | const char *val; |
| 2542 | int id; |
| 2543 | |
| 2544 | val = udev_device_get_property_value(device, "CONNECTOR"); |
| 2545 | if (!val || !safe_strtoint(val, &id)) |
| 2546 | return 0; |
| 2547 | else |
| 2548 | *connector_id = id; |
| 2549 | |
| 2550 | val = udev_device_get_property_value(device, "PROPERTY"); |
| 2551 | if (!val || !safe_strtoint(val, &id)) |
| 2552 | return 0; |
| 2553 | else |
| 2554 | *property_id = id; |
| 2555 | |
| 2556 | return 1; |
| 2557 | } |
| 2558 | |
| 2559 | static int |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2560 | udev_drm_event(int fd, uint32_t mask, void *data) |
| 2561 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2562 | struct drm_backend *b = data; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2563 | struct udev_device *event; |
Ankit Nautiyal | a344fe3 | 2019-05-14 18:36:08 +0530 | [diff] [blame] | 2564 | uint32_t conn_id, prop_id; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2565 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2566 | event = udev_monitor_receive_device(b->udev_monitor); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 2567 | |
leng.fang | 9cf09e2 | 2024-07-17 20:01:11 +0800 | [diff] [blame] | 2568 | if (udev_event_is_hotplug(b, event) && b->display_enable) { |
leng.fang | 9185607 | 2024-06-07 14:12:54 +0800 | [diff] [blame] | 2569 | #ifdef ENABLE_MODE_POLICY |
leng.fang | 13fd398 | 2024-07-03 19:09:58 +0800 | [diff] [blame] | 2570 | mode_policy_set_hotplug(AML_WESTON_HOTPLUG_PLUG | AML_WESTON_HOTPLUG_UNPLUG, true); |
leng.fang | 9185607 | 2024-06-07 14:12:54 +0800 | [diff] [blame] | 2571 | #endif |
Ankit Nautiyal | a344fe3 | 2019-05-14 18:36:08 +0530 | [diff] [blame] | 2572 | if (udev_event_is_conn_prop_change(b, event, &conn_id, &prop_id)) |
| 2573 | drm_backend_update_conn_props(b, conn_id, prop_id); |
| 2574 | else |
Leandro Ribeiro | 993920d | 2020-09-14 17:01:01 -0300 | [diff] [blame] | 2575 | drm_backend_update_connectors(b, event); |
Ankit Nautiyal | a344fe3 | 2019-05-14 18:36:08 +0530 | [diff] [blame] | 2576 | } |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2577 | |
leng.fang | 32af9fc | 2024-06-13 11:22:15 +0800 | [diff] [blame] | 2578 | #ifdef BUILD_AML_TV |
| 2579 | drm_backend_afr_update(b, udev_event_frame_rate_hint(event), |
| 2580 | udev_event_is_vdin_event(event)); |
| 2581 | #endif |
| 2582 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2583 | udev_device_unref(event); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 2584 | |
| 2585 | return 1; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2586 | } |
| 2587 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 2588 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 2589 | drm_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 2590 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2591 | struct drm_backend *b = to_drm_backend(ec); |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 2592 | struct weston_head *base, *next; |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 2593 | struct drm_crtc *crtc, *crtc_tmp; |
Leandro Ribeiro | 96bef05 | 2020-09-09 15:23:49 -0300 | [diff] [blame] | 2594 | struct drm_writeback *writeback, *writeback_tmp; |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 2595 | |
leng.fang | 32af9fc | 2024-06-13 11:22:15 +0800 | [diff] [blame] | 2596 | #ifdef ENABLE_DRM_HELP |
| 2597 | stop_help_worker(); |
| 2598 | #endif |
| 2599 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2600 | udev_input_destroy(&b->input); |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 2601 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2602 | wl_event_source_remove(b->udev_drm_source); |
| 2603 | wl_event_source_remove(b->drm_source); |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 2604 | |
Daniel Stone | b57c6a0 | 2017-10-05 16:27:21 +0100 | [diff] [blame] | 2605 | b->shutting_down = true; |
| 2606 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2607 | destroy_sprites(b); |
Kristian Høgsberg | 3d64a3e | 2013-05-10 12:36:04 -0400 | [diff] [blame] | 2608 | |
Leandro Ribeiro | f014964 | 2019-12-18 15:52:18 -0300 | [diff] [blame] | 2609 | weston_log_scope_destroy(b->debug); |
Daniel Stone | 1cbe1f9 | 2018-07-20 10:21:28 +0100 | [diff] [blame] | 2610 | b->debug = NULL; |
Ander Conselvan de Oliveira | 6b16214 | 2013-10-25 16:26:32 +0300 | [diff] [blame] | 2611 | weston_compositor_shutdown(ec); |
| 2612 | |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 2613 | wl_list_for_each_safe(crtc, crtc_tmp, &b->crtc_list, link) |
| 2614 | drm_crtc_destroy(crtc); |
| 2615 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 2616 | wl_list_for_each_safe(base, next, &ec->head_list, compositor_link) |
| 2617 | drm_head_destroy(to_drm_head(base)); |
| 2618 | |
Leandro Ribeiro | 96bef05 | 2020-09-09 15:23:49 -0300 | [diff] [blame] | 2619 | wl_list_for_each_safe(writeback, writeback_tmp, |
| 2620 | &b->writeback_connector_list, link) |
| 2621 | drm_writeback_destroy(writeback); |
| 2622 | |
Stefan Agner | ccf2407 | 2019-07-09 22:02:00 +0200 | [diff] [blame] | 2623 | #ifdef BUILD_DRM_GBM |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2624 | if (b->gbm) |
| 2625 | gbm_device_destroy(b->gbm); |
Stefan Agner | ccf2407 | 2019-07-09 22:02:00 +0200 | [diff] [blame] | 2626 | #endif |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2627 | |
Pekka Paalanen | 5b0aa55 | 2017-12-07 16:06:05 +0200 | [diff] [blame] | 2628 | udev_monitor_unref(b->udev_monitor); |
Pekka Paalanen | 2a0c6c3 | 2017-09-13 16:48:01 +0300 | [diff] [blame] | 2629 | udev_unref(b->udev); |
| 2630 | |
Marius Vlad | 7a44ee7 | 2021-06-23 16:02:35 +0300 | [diff] [blame] | 2631 | weston_launcher_close(ec->launcher, b->drm.fd); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2632 | weston_launcher_destroy(ec->launcher); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 2633 | |
Pekka Paalanen | 9bf4f37 | 2017-12-07 16:05:29 +0200 | [diff] [blame] | 2634 | free(b->drm.filename); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2635 | free(b); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 2636 | } |
| 2637 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 2638 | static void |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 2639 | session_notify(struct wl_listener *listener, void *data) |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 2640 | { |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 2641 | struct weston_compositor *compositor = data; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2642 | struct drm_backend *b = to_drm_backend(compositor); |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 2643 | struct drm_plane *plane; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 2644 | struct drm_output *output; |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 2645 | struct drm_crtc *crtc; |
leng.fang | e9d800e | 2024-07-12 13:31:59 +0800 | [diff] [blame] | 2646 | struct weston_mode *wst_mode; |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 2647 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2648 | if (compositor->session_active) { |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 2649 | weston_log("activating session\n"); |
leng.fang | e9d800e | 2024-07-12 13:31:59 +0800 | [diff] [blame] | 2650 | |
| 2651 | mode_policy_resume(); |
| 2652 | wst_mode = mode_policy_choose_mode(NULL); |
| 2653 | wl_list_for_each(output, &compositor->output_list, base.link) |
| 2654 | weston_output_mode_set_native(&output->base, |
| 2655 | wst_mode, output->base.current_scale); |
| 2656 | |
Daniel Stone | f33e104 | 2016-11-05 08:10:13 +0000 | [diff] [blame] | 2657 | weston_compositor_wake(compositor); |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 2658 | weston_compositor_damage_all(compositor); |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 2659 | b->state_invalid = true; |
leng.fang | 32af9fc | 2024-06-13 11:22:15 +0800 | [diff] [blame] | 2660 | b->allow_modeset = true; |
leng.fang | 9cf09e2 | 2024-07-17 20:01:11 +0800 | [diff] [blame] | 2661 | b->display_enable = 1; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2662 | udev_input_enable(&b->input); |
leng.fang | e9d800e | 2024-07-12 13:31:59 +0800 | [diff] [blame] | 2663 | weston_compositor_resume_focus(compositor); |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 2664 | } else { |
| 2665 | weston_log("deactivating session\n"); |
leng.fang | 32af9fc | 2024-06-13 11:22:15 +0800 | [diff] [blame] | 2666 | drm_change_to_dummy_mode(b->compositor); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2667 | udev_input_disable(&b->input); |
Kristian Høgsberg | 4014a6b | 2012-04-10 00:08:45 -0400 | [diff] [blame] | 2668 | |
Philipp Brüschweiler | 57edf7f | 2013-03-29 13:01:56 +0100 | [diff] [blame] | 2669 | weston_compositor_offscreen(compositor); |
Kristian Høgsberg | d8e181b | 2011-05-06 15:38:28 -0400 | [diff] [blame] | 2670 | |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 2671 | /* If we have a repaint scheduled (either from a |
| 2672 | * pending pageflip or the idle handler), make sure we |
| 2673 | * cancel that so we don't try to pageflip when we're |
Philipp Brüschweiler | 57edf7f | 2013-03-29 13:01:56 +0100 | [diff] [blame] | 2674 | * vt switched away. The OFFSCREEN state will prevent |
Abdur Rehman | 4dca0e1 | 2017-01-01 19:46:35 +0500 | [diff] [blame] | 2675 | * further attempts at repainting. When we switch |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 2676 | * back, we schedule a repaint, which will process |
| 2677 | * pending frame callbacks. */ |
| 2678 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2679 | wl_list_for_each(output, &compositor->output_list, base.link) { |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 2680 | crtc = output->crtc; |
Daniel Stone | 09a97e2 | 2017-03-01 11:34:06 +0000 | [diff] [blame] | 2681 | output->base.repaint_needed = false; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 2682 | if (output->cursor_plane) |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 2683 | drmModeSetCursor(b->drm.fd, crtc->crtc_id, |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 2684 | 0, 0, 0); |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 2685 | } |
| 2686 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2687 | output = container_of(compositor->output_list.next, |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 2688 | struct drm_output, base.link); |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 2689 | crtc = output->crtc; |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 2690 | |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 2691 | wl_list_for_each(plane, &b->plane_list, link) { |
| 2692 | if (plane->type != WDRM_PLANE_TYPE_OVERLAY) |
| 2693 | continue; |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 2694 | drmModeSetPlane(b->drm.fd, plane->plane_id, crtc->crtc_id, |
| 2695 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 2696 | } |
| 2697 | } |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 2698 | } |
| 2699 | |
Robert Beckett | 8d23ab7 | 2019-06-13 16:55:44 +0100 | [diff] [blame] | 2700 | |
| 2701 | /** |
| 2702 | * Handle KMS GPU being added/removed |
| 2703 | * |
| 2704 | * If the device being added/removed is the KMS device, we activate/deactivate |
| 2705 | * the compositor session. |
| 2706 | * |
| 2707 | * @param compositor The compositor instance. |
| 2708 | * @param device The device being added/removed. |
| 2709 | * @param added Whether the device is being added (or removed) |
| 2710 | */ |
| 2711 | static void |
| 2712 | drm_device_changed(struct weston_compositor *compositor, |
| 2713 | dev_t device, bool added) |
| 2714 | { |
| 2715 | struct drm_backend *b = to_drm_backend(compositor); |
| 2716 | |
Robert Beckett | 49dc320 | 2019-07-02 16:31:22 +0100 | [diff] [blame] | 2717 | if (b->drm.fd < 0 || b->drm.devnum != device || |
| 2718 | compositor->session_active == added) |
Robert Beckett | 8d23ab7 | 2019-06-13 16:55:44 +0100 | [diff] [blame] | 2719 | return; |
| 2720 | |
| 2721 | compositor->session_active = added; |
| 2722 | wl_signal_emit(&compositor->session_signal, compositor); |
| 2723 | } |
| 2724 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 2725 | /** |
| 2726 | * Determines whether or not a device is capable of modesetting. If successful, |
| 2727 | * sets b->drm.fd and b->drm.filename to the opened device. |
| 2728 | */ |
| 2729 | static bool |
| 2730 | drm_device_is_kms(struct drm_backend *b, struct udev_device *device) |
| 2731 | { |
| 2732 | const char *filename = udev_device_get_devnode(device); |
| 2733 | const char *sysnum = udev_device_get_sysnum(device); |
Robert Beckett | 8d23ab7 | 2019-06-13 16:55:44 +0100 | [diff] [blame] | 2734 | dev_t devnum = udev_device_get_devnum(device); |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 2735 | drmModeRes *res; |
Marius Vlad | 7d070ca | 2018-11-23 14:02:07 +0200 | [diff] [blame] | 2736 | int id = -1, fd; |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 2737 | |
| 2738 | if (!filename) |
| 2739 | return false; |
| 2740 | |
| 2741 | fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR); |
| 2742 | if (fd < 0) |
| 2743 | return false; |
| 2744 | |
| 2745 | res = drmModeGetResources(fd); |
| 2746 | if (!res) |
| 2747 | goto out_fd; |
| 2748 | |
| 2749 | if (res->count_crtcs <= 0 || res->count_connectors <= 0 || |
| 2750 | res->count_encoders <= 0) |
| 2751 | goto out_res; |
| 2752 | |
| 2753 | if (sysnum) |
| 2754 | id = atoi(sysnum); |
| 2755 | if (!sysnum || id < 0) { |
| 2756 | weston_log("couldn't get sysnum for device %s\n", filename); |
| 2757 | goto out_res; |
| 2758 | } |
| 2759 | |
| 2760 | /* We can be called successfully on multiple devices; if we have, |
| 2761 | * clean up old entries. */ |
| 2762 | if (b->drm.fd >= 0) |
| 2763 | weston_launcher_close(b->compositor->launcher, b->drm.fd); |
| 2764 | free(b->drm.filename); |
| 2765 | |
| 2766 | b->drm.fd = fd; |
| 2767 | b->drm.id = id; |
| 2768 | b->drm.filename = strdup(filename); |
Robert Beckett | 8d23ab7 | 2019-06-13 16:55:44 +0100 | [diff] [blame] | 2769 | b->drm.devnum = devnum; |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 2770 | |
Sergi Granell | ceb5981 | 2017-03-28 12:44:04 +0200 | [diff] [blame] | 2771 | drmModeFreeResources(res); |
| 2772 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 2773 | return true; |
| 2774 | |
| 2775 | out_res: |
| 2776 | drmModeFreeResources(res); |
| 2777 | out_fd: |
| 2778 | weston_launcher_close(b->compositor->launcher, fd); |
| 2779 | return false; |
| 2780 | } |
| 2781 | |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 2782 | /* |
| 2783 | * Find primary GPU |
| 2784 | * Some systems may have multiple DRM devices attached to a single seat. This |
| 2785 | * function loops over all devices and tries to find a PCI device with the |
| 2786 | * boot_vga sysfs attribute set to 1. |
| 2787 | * If no such device is found, the first DRM device reported by udev is used. |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 2788 | * Devices are also vetted to make sure they are are capable of modesetting, |
| 2789 | * rather than pure render nodes (GPU with no display), or pure |
| 2790 | * memory-allocation devices (VGEM). |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 2791 | */ |
| 2792 | static struct udev_device* |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2793 | find_primary_gpu(struct drm_backend *b, const char *seat) |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 2794 | { |
| 2795 | struct udev_enumerate *e; |
| 2796 | struct udev_list_entry *entry; |
| 2797 | const char *path, *device_seat, *id; |
| 2798 | struct udev_device *device, *drm_device, *pci; |
| 2799 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2800 | e = udev_enumerate_new(b->udev); |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 2801 | udev_enumerate_add_match_subsystem(e, "drm"); |
| 2802 | udev_enumerate_add_match_sysname(e, "card[0-9]*"); |
| 2803 | |
| 2804 | udev_enumerate_scan_devices(e); |
| 2805 | drm_device = NULL; |
| 2806 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 2807 | bool is_boot_vga = false; |
| 2808 | |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 2809 | path = udev_list_entry_get_name(entry); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2810 | device = udev_device_new_from_syspath(b->udev, path); |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 2811 | if (!device) |
| 2812 | continue; |
| 2813 | device_seat = udev_device_get_property_value(device, "ID_SEAT"); |
| 2814 | if (!device_seat) |
| 2815 | device_seat = default_seat; |
| 2816 | if (strcmp(device_seat, seat)) { |
| 2817 | udev_device_unref(device); |
| 2818 | continue; |
| 2819 | } |
| 2820 | |
| 2821 | pci = udev_device_get_parent_with_subsystem_devtype(device, |
| 2822 | "pci", NULL); |
| 2823 | if (pci) { |
| 2824 | id = udev_device_get_sysattr_value(pci, "boot_vga"); |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 2825 | if (id && !strcmp(id, "1")) |
| 2826 | is_boot_vga = true; |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 2827 | } |
| 2828 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 2829 | /* If we already have a modesetting-capable device, and this |
| 2830 | * device isn't our boot-VGA device, we aren't going to use |
| 2831 | * it. */ |
| 2832 | if (!is_boot_vga && drm_device) { |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 2833 | udev_device_unref(device); |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 2834 | continue; |
| 2835 | } |
| 2836 | |
| 2837 | /* Make sure this device is actually capable of modesetting; |
| 2838 | * if this call succeeds, b->drm.{fd,filename} will be set, |
| 2839 | * and any old values freed. */ |
| 2840 | if (!drm_device_is_kms(b, device)) { |
| 2841 | udev_device_unref(device); |
| 2842 | continue; |
| 2843 | } |
| 2844 | |
| 2845 | /* There can only be one boot_vga device, and we try to use it |
| 2846 | * at all costs. */ |
| 2847 | if (is_boot_vga) { |
| 2848 | if (drm_device) |
| 2849 | udev_device_unref(drm_device); |
| 2850 | drm_device = device; |
| 2851 | break; |
| 2852 | } |
| 2853 | |
| 2854 | /* Per the (!is_boot_vga && drm_device) test above, we only |
| 2855 | * trump existing saved devices with boot-VGA devices, so if |
| 2856 | * we end up here, this must be the first device we've seen. */ |
| 2857 | assert(!drm_device); |
| 2858 | drm_device = device; |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 2859 | } |
| 2860 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 2861 | /* If we're returning a device to use, we must have an open FD for |
| 2862 | * it. */ |
| 2863 | assert(!!drm_device == (b->drm.fd >= 0)); |
| 2864 | |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 2865 | udev_enumerate_unref(e); |
| 2866 | return drm_device; |
| 2867 | } |
| 2868 | |
Pekka Paalanen | b45ed8b | 2017-03-28 18:04:27 +0300 | [diff] [blame] | 2869 | static struct udev_device * |
| 2870 | open_specific_drm_device(struct drm_backend *b, const char *name) |
| 2871 | { |
| 2872 | struct udev_device *device; |
| 2873 | |
| 2874 | device = udev_device_new_from_subsystem_sysname(b->udev, "drm", name); |
| 2875 | if (!device) { |
| 2876 | weston_log("ERROR: could not open DRM device '%s'\n", name); |
| 2877 | return NULL; |
| 2878 | } |
| 2879 | |
| 2880 | if (!drm_device_is_kms(b, device)) { |
| 2881 | udev_device_unref(device); |
| 2882 | weston_log("ERROR: DRM device '%s' is not a KMS device.\n", name); |
| 2883 | return NULL; |
| 2884 | } |
| 2885 | |
| 2886 | /* If we're returning a device to use, we must have an open FD for |
| 2887 | * it. */ |
| 2888 | assert(b->drm.fd >= 0); |
| 2889 | |
| 2890 | return device; |
| 2891 | } |
| 2892 | |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 2893 | static void |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 2894 | planes_binding(struct weston_keyboard *keyboard, const struct timespec *time, |
| 2895 | uint32_t key, void *data) |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 2896 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2897 | struct drm_backend *b = data; |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 2898 | |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 2899 | switch (key) { |
| 2900 | case KEY_C: |
Emmanuel Gil Peyrot | 1b3ad09 | 2019-12-09 02:50:55 +0100 | [diff] [blame] | 2901 | b->cursors_are_broken ^= true; |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 2902 | break; |
| 2903 | case KEY_V: |
Daniel Stone | 87fab1c | 2019-06-17 11:13:20 +0100 | [diff] [blame] | 2904 | /* We don't support overlay-plane usage with legacy KMS. */ |
| 2905 | if (b->atomic_modeset) |
Emmanuel Gil Peyrot | 1b3ad09 | 2019-12-09 02:50:55 +0100 | [diff] [blame] | 2906 | b->sprites_are_broken ^= true; |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 2907 | break; |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 2908 | default: |
| 2909 | break; |
| 2910 | } |
Ander Conselvan de Oliveira | 180f42a | 2012-11-21 15:11:37 +0200 | [diff] [blame] | 2911 | } |
| 2912 | |
Kristian Høgsberg | 0eac34a | 2013-08-30 14:28:22 -0700 | [diff] [blame] | 2913 | #ifdef BUILD_VAAPI_RECORDER |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 2914 | static void |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 2915 | recorder_destroy(struct drm_output *output) |
| 2916 | { |
| 2917 | vaapi_recorder_destroy(output->recorder); |
| 2918 | output->recorder = NULL; |
| 2919 | |
Ankit Nautiyal | 93dde24 | 2019-07-08 11:46:42 +0530 | [diff] [blame] | 2920 | weston_output_disable_planes_decr(&output->base); |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 2921 | |
| 2922 | wl_list_remove(&output->recorder_frame_listener.link); |
| 2923 | weston_log("[libva recorder] done\n"); |
| 2924 | } |
| 2925 | |
| 2926 | static void |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 2927 | recorder_frame_notify(struct wl_listener *listener, void *data) |
| 2928 | { |
| 2929 | struct drm_output *output; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2930 | struct drm_backend *b; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 2931 | int fd, ret; |
| 2932 | |
| 2933 | output = container_of(listener, struct drm_output, |
| 2934 | recorder_frame_listener); |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2935 | b = to_drm_backend(output->base.compositor); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 2936 | |
| 2937 | if (!output->recorder) |
| 2938 | return; |
| 2939 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 2940 | ret = drmPrimeHandleToFD(b->drm.fd, |
Daniel Stone | 8eece0c | 2016-11-17 17:54:00 +0000 | [diff] [blame] | 2941 | output->scanout_plane->state_cur->fb->handles[0], |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 2942 | DRM_CLOEXEC, &fd); |
| 2943 | if (ret) { |
| 2944 | weston_log("[libva recorder] " |
| 2945 | "failed to create prime fd for front buffer\n"); |
| 2946 | return; |
| 2947 | } |
| 2948 | |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 2949 | ret = vaapi_recorder_frame(output->recorder, fd, |
Daniel Stone | 8eece0c | 2016-11-17 17:54:00 +0000 | [diff] [blame] | 2950 | output->scanout_plane->state_cur->fb->strides[0]); |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 2951 | if (ret < 0) { |
Antonio Borneo | 3957863 | 2019-04-26 23:57:31 +0200 | [diff] [blame] | 2952 | weston_log("[libva recorder] aborted: %s\n", strerror(errno)); |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 2953 | recorder_destroy(output); |
| 2954 | } |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 2955 | } |
| 2956 | |
| 2957 | static void * |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2958 | create_recorder(struct drm_backend *b, int width, int height, |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 2959 | const char *filename) |
| 2960 | { |
| 2961 | int fd; |
| 2962 | drm_magic_t magic; |
| 2963 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2964 | fd = open(b->drm.filename, O_RDWR | O_CLOEXEC); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 2965 | if (fd < 0) |
| 2966 | return NULL; |
| 2967 | |
| 2968 | drmGetMagic(fd, &magic); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2969 | drmAuthMagic(b->drm.fd, magic); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 2970 | |
| 2971 | return vaapi_recorder_create(fd, width, height, filename); |
| 2972 | } |
| 2973 | |
| 2974 | static void |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 2975 | recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time, |
| 2976 | uint32_t key, void *data) |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 2977 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2978 | struct drm_backend *b = data; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 2979 | struct drm_output *output; |
| 2980 | int width, height; |
| 2981 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2982 | output = container_of(b->compositor->output_list.next, |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 2983 | struct drm_output, base.link); |
| 2984 | |
| 2985 | if (!output->recorder) { |
Stefan Agner | 4a18f30 | 2019-10-20 18:25:42 +0200 | [diff] [blame] | 2986 | if (output->gbm_format != DRM_FORMAT_XRGB8888) { |
Ander Conselvan de Oliveira | 2ef1cd1 | 2014-05-06 16:49:06 +0300 | [diff] [blame] | 2987 | weston_log("failed to start vaapi recorder: " |
| 2988 | "output format not supported\n"); |
| 2989 | return; |
| 2990 | } |
| 2991 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 2992 | width = output->base.current_mode->width; |
| 2993 | height = output->base.current_mode->height; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 2994 | |
| 2995 | output->recorder = |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2996 | create_recorder(b, width, height, "capture.h264"); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 2997 | if (!output->recorder) { |
| 2998 | weston_log("failed to create vaapi recorder\n"); |
| 2999 | return; |
| 3000 | } |
| 3001 | |
Ankit Nautiyal | 93dde24 | 2019-07-08 11:46:42 +0530 | [diff] [blame] | 3002 | weston_output_disable_planes_incr(&output->base); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3003 | |
| 3004 | output->recorder_frame_listener.notify = recorder_frame_notify; |
| 3005 | wl_signal_add(&output->base.frame_signal, |
| 3006 | &output->recorder_frame_listener); |
| 3007 | |
| 3008 | weston_output_schedule_repaint(&output->base); |
| 3009 | |
| 3010 | weston_log("[libva recorder] initialized\n"); |
| 3011 | } else { |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 3012 | recorder_destroy(output); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3013 | } |
| 3014 | } |
| 3015 | #else |
| 3016 | static void |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 3017 | recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time, |
| 3018 | uint32_t key, void *data) |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3019 | { |
| 3020 | weston_log("Compiled without libva support\n"); |
| 3021 | } |
| 3022 | #endif |
| 3023 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3024 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 3025 | static const struct weston_drm_output_api api = { |
| 3026 | drm_output_set_mode, |
| 3027 | drm_output_set_gbm_format, |
| 3028 | drm_output_set_seat, |
| 3029 | }; |
| 3030 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3031 | static struct drm_backend * |
| 3032 | drm_backend_create(struct weston_compositor *compositor, |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3033 | struct weston_drm_backend_config *config) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3034 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3035 | struct drm_backend *b; |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 3036 | struct udev_device *drm_device; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3037 | struct wl_event_loop *loop; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3038 | const char *seat_id = default_seat; |
nerdopolis | b16c4ac | 2018-06-29 08:17:46 -0400 | [diff] [blame] | 3039 | const char *session_seat; |
Leandro Ribeiro | 5429302 | 2021-10-12 14:48:36 -0300 | [diff] [blame] | 3040 | struct weston_drm_format_array *scanout_formats; |
Igor Matheus Andrade Torrente | 6354455 | 2020-10-13 11:25:20 -0300 | [diff] [blame] | 3041 | drmModeRes *res; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 3042 | int ret; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3043 | |
nerdopolis | b16c4ac | 2018-06-29 08:17:46 -0400 | [diff] [blame] | 3044 | session_seat = getenv("XDG_SEAT"); |
| 3045 | if (session_seat) |
| 3046 | seat_id = session_seat; |
| 3047 | |
| 3048 | if (config->seat_id) |
| 3049 | seat_id = config->seat_id; |
| 3050 | |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 3051 | weston_log("initializing drm backend\n"); |
| 3052 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3053 | b = zalloc(sizeof *b); |
| 3054 | if (b == NULL) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 3055 | return NULL; |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 3056 | |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 3057 | b->state_invalid = true; |
leng.fang | 32af9fc | 2024-06-13 11:22:15 +0800 | [diff] [blame] | 3058 | b->allow_modeset = true; |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3059 | b->drm.fd = -1; |
leng.fang | 9cf09e2 | 2024-07-17 20:01:11 +0800 | [diff] [blame] | 3060 | b->display_enable = 1; |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3061 | |
leng.fang | 32af9fc | 2024-06-13 11:22:15 +0800 | [diff] [blame] | 3062 | #ifdef BUILD_AML_TV |
| 3063 | b->vdin_detect_fd = -1; |
| 3064 | #endif |
| 3065 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3066 | b->compositor = compositor; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3067 | b->use_pixman = config->use_pixman; |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 3068 | b->pageflip_timeout = config->pageflip_timeout; |
Pekka Paalanen | dee412d | 2018-04-23 11:44:58 +0200 | [diff] [blame] | 3069 | b->use_pixman_shadow = config->use_pixman_shadow; |
Kristian Høgsberg | 8e6f376 | 2013-10-16 16:31:42 -0700 | [diff] [blame] | 3070 | |
Leandro Ribeiro | 172afc2 | 2019-12-26 16:23:43 -0300 | [diff] [blame] | 3071 | b->debug = weston_compositor_add_log_scope(compositor, "drm-backend", |
| 3072 | "Debug messages from DRM/KMS backend\n", |
| 3073 | NULL, NULL, NULL); |
Daniel Stone | 1cbe1f9 | 2018-07-20 10:21:28 +0100 | [diff] [blame] | 3074 | |
Pekka Paalanen | 7da9a38 | 2017-08-30 11:29:49 +0300 | [diff] [blame] | 3075 | compositor->backend = &b->base; |
| 3076 | |
Stefan Agner | 0bfebeb | 2019-07-08 00:30:44 +0200 | [diff] [blame] | 3077 | if (parse_gbm_format(config->gbm_format, DRM_FORMAT_XRGB8888, &b->gbm_format) < 0) |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3078 | goto err_compositor; |
Kristian Høgsberg | 8e6f376 | 2013-10-16 16:31:42 -0700 | [diff] [blame] | 3079 | |
Rafal Mielniczuk | 32069c0 | 2013-03-27 18:39:28 +0100 | [diff] [blame] | 3080 | /* Check if we run drm-backend using weston-launch */ |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3081 | compositor->launcher = weston_launcher_connect(compositor, config->tty, |
| 3082 | seat_id, true); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3083 | if (compositor->launcher == NULL) { |
Pekka Paalanen | a453f4d | 2017-10-31 10:19:48 +0200 | [diff] [blame] | 3084 | weston_log("fatal: drm backend should be run using " |
| 3085 | "weston-launch binary, or your system should " |
| 3086 | "provide the logind D-Bus API.\n"); |
Rafal Mielniczuk | 32069c0 | 2013-03-27 18:39:28 +0100 | [diff] [blame] | 3087 | goto err_compositor; |
| 3088 | } |
| 3089 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3090 | b->udev = udev_new(); |
| 3091 | if (b->udev == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 3092 | weston_log("failed to initialize udev context\n"); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 3093 | goto err_launcher; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3094 | } |
| 3095 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3096 | b->session_listener.notify = session_notify; |
| 3097 | wl_signal_add(&compositor->session_signal, &b->session_listener); |
Kristian Høgsberg | c5b9ddb | 2012-01-15 14:29:09 -0500 | [diff] [blame] | 3098 | |
leng.fang | 32af9fc | 2024-06-13 11:22:15 +0800 | [diff] [blame] | 3099 | drm_config_fixed_UIsize(b, config); |
| 3100 | |
Pekka Paalanen | b45ed8b | 2017-03-28 18:04:27 +0300 | [diff] [blame] | 3101 | if (config->specific_device) |
| 3102 | drm_device = open_specific_drm_device(b, config->specific_device); |
| 3103 | else |
| 3104 | drm_device = find_primary_gpu(b, seat_id); |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 3105 | if (drm_device == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 3106 | weston_log("no drm device found\n"); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 3107 | goto err_udev; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 3108 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3109 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3110 | if (init_kms_caps(b) < 0) { |
Ander Conselvan de Oliveira | 2292917 | 2013-01-25 15:13:02 +0200 | [diff] [blame] | 3111 | weston_log("failed to initialize kms\n"); |
| 3112 | goto err_udev_dev; |
| 3113 | } |
| 3114 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3115 | if (b->use_pixman) { |
| 3116 | if (init_pixman(b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3117 | weston_log("failed to initialize pixman renderer\n"); |
| 3118 | goto err_udev_dev; |
| 3119 | } |
| 3120 | } else { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3121 | if (init_egl(b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3122 | weston_log("failed to initialize egl\n"); |
| 3123 | goto err_udev_dev; |
| 3124 | } |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 3125 | } |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 3126 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3127 | b->base.destroy = drm_destroy; |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 3128 | b->base.repaint_begin = drm_repaint_begin; |
| 3129 | b->base.repaint_flush = drm_repaint_flush; |
| 3130 | b->base.repaint_cancel = drm_repaint_cancel; |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 3131 | b->base.create_output = drm_output_create; |
Robert Beckett | 8d23ab7 | 2019-06-13 16:55:44 +0100 | [diff] [blame] | 3132 | b->base.device_changed = drm_device_changed; |
Marius Vlad | 81bada5 | 2019-11-11 00:27:17 +0200 | [diff] [blame] | 3133 | b->base.can_scanout_dmabuf = drm_can_scanout_dmabuf; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 3134 | |
leng.fang | 32af9fc | 2024-06-13 11:22:15 +0800 | [diff] [blame] | 3135 | drm_add_aml_callback(b); |
Bob Ham | 91880f1 | 2016-01-12 10:21:47 +0000 | [diff] [blame] | 3136 | weston_setup_vt_switch_bindings(compositor); |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 3137 | |
Igor Matheus Andrade Torrente | 6354455 | 2020-10-13 11:25:20 -0300 | [diff] [blame] | 3138 | res = drmModeGetResources(b->drm.fd); |
| 3139 | if (!res) { |
| 3140 | weston_log("Failed to get drmModeRes\n"); |
Leandro Ribeiro | b00d1a2 | 2020-08-13 14:12:28 -0300 | [diff] [blame] | 3141 | goto err_udev_dev; |
| 3142 | } |
| 3143 | |
Igor Matheus Andrade Torrente | 6354455 | 2020-10-13 11:25:20 -0300 | [diff] [blame] | 3144 | wl_list_init(&b->crtc_list); |
| 3145 | if (drm_backend_create_crtc_list(b, res) == -1) { |
| 3146 | weston_log("Failed to create CRTC list for DRM-backend\n"); |
| 3147 | goto err_create_crtc_list; |
| 3148 | } |
| 3149 | |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 3150 | wl_list_init(&b->plane_list); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3151 | create_sprites(b); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3152 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3153 | if (udev_input_init(&b->input, |
Giulio Camuffo | 8aedf7b | 2016-06-02 21:48:12 +0300 | [diff] [blame] | 3154 | compositor, b->udev, seat_id, |
| 3155 | config->configure_device) < 0) { |
Ander Conselvan de Oliveira | 4ade0e4 | 2014-04-17 13:08:45 +0300 | [diff] [blame] | 3156 | weston_log("failed to create input devices\n"); |
| 3157 | goto err_sprite; |
| 3158 | } |
| 3159 | |
leng.fang | 32af9fc | 2024-06-13 11:22:15 +0800 | [diff] [blame] | 3160 | #ifdef ENABLE_DRM_HELP |
| 3161 | weston_log("drm start_help_worker \n"); |
| 3162 | start_help_worker(b->drm.fd, b->atomic_modeset); |
| 3163 | #endif |
| 3164 | |
leng.fang | 9185607 | 2024-06-07 14:12:54 +0800 | [diff] [blame] | 3165 | #ifdef ENABLE_MODE_POLICY |
leng.fang | 13fd398 | 2024-07-03 19:09:58 +0800 | [diff] [blame] | 3166 | mode_policy_set_hotplug(AML_WESTON_HOTPLUG_BOOT, true); |
leng.fang | 9185607 | 2024-06-07 14:12:54 +0800 | [diff] [blame] | 3167 | #endif |
| 3168 | |
Leandro Ribeiro | 96bef05 | 2020-09-09 15:23:49 -0300 | [diff] [blame] | 3169 | wl_list_init(&b->writeback_connector_list); |
Igor Matheus Andrade Torrente | 6354455 | 2020-10-13 11:25:20 -0300 | [diff] [blame] | 3170 | if (drm_backend_discover_connectors(b, drm_device, res) < 0) { |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 3171 | weston_log("Failed to create heads for %s\n", b->drm.filename); |
Ander Conselvan de Oliveira | 4ade0e4 | 2014-04-17 13:08:45 +0300 | [diff] [blame] | 3172 | goto err_udev_input; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 3173 | } |
| 3174 | |
Igor Matheus Andrade Torrente | 6354455 | 2020-10-13 11:25:20 -0300 | [diff] [blame] | 3175 | drmModeFreeResources(res); |
| 3176 | |
Marius Vlad | e83e750 | 2019-10-29 17:29:37 +0200 | [diff] [blame] | 3177 | /* 'compute' faked zpos values in case HW doesn't expose any */ |
| 3178 | drm_backend_create_faked_zpos(b); |
| 3179 | |
Jason Ekstrand | 9fc7151 | 2014-04-02 19:53:46 -0500 | [diff] [blame] | 3180 | /* A this point we have some idea of whether or not we have a working |
| 3181 | * cursor plane. */ |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3182 | if (!b->cursors_are_broken) |
| 3183 | compositor->capabilities |= WESTON_CAP_CURSOR_PLANE; |
Jason Ekstrand | 9fc7151 | 2014-04-02 19:53:46 -0500 | [diff] [blame] | 3184 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3185 | loop = wl_display_get_event_loop(compositor->wl_display); |
| 3186 | b->drm_source = |
| 3187 | wl_event_loop_add_fd(loop, b->drm.fd, |
| 3188 | WL_EVENT_READABLE, on_drm_input, b); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 3189 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3190 | b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev"); |
| 3191 | if (b->udev_monitor == NULL) { |
Abdur Rehman | 4dca0e1 | 2017-01-01 19:46:35 +0500 | [diff] [blame] | 3192 | weston_log("failed to initialize udev monitor\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3193 | goto err_drm_source; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3194 | } |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3195 | udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor, |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3196 | "drm", NULL); |
leng.fang | 32af9fc | 2024-06-13 11:22:15 +0800 | [diff] [blame] | 3197 | #ifdef BUILD_AML_TV |
| 3198 | udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor, |
| 3199 | "vdin", NULL); |
| 3200 | udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor, |
| 3201 | "framerate_adapter", NULL); |
| 3202 | #endif |
| 3203 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3204 | b->udev_drm_source = |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 3205 | wl_event_loop_add_fd(loop, |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3206 | udev_monitor_get_fd(b->udev_monitor), |
| 3207 | WL_EVENT_READABLE, udev_drm_event, b); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3208 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3209 | if (udev_monitor_enable_receiving(b->udev_monitor) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 3210 | weston_log("failed to enable udev-monitor receiving\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3211 | goto err_udev_monitor; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3212 | } |
| 3213 | |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3214 | udev_device_unref(drm_device); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3215 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3216 | weston_compositor_add_debug_binding(compositor, KEY_O, |
| 3217 | planes_binding, b); |
| 3218 | weston_compositor_add_debug_binding(compositor, KEY_C, |
| 3219 | planes_binding, b); |
| 3220 | weston_compositor_add_debug_binding(compositor, KEY_V, |
| 3221 | planes_binding, b); |
| 3222 | weston_compositor_add_debug_binding(compositor, KEY_Q, |
| 3223 | recorder_binding, b); |
| 3224 | weston_compositor_add_debug_binding(compositor, KEY_W, |
| 3225 | renderer_switch_binding, b); |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 3226 | |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 3227 | if (compositor->renderer->import_dmabuf) { |
| 3228 | if (linux_dmabuf_setup(compositor) < 0) |
| 3229 | weston_log("Error: initializing dmabuf " |
| 3230 | "support failed.\n"); |
Leandro Ribeiro | 5429302 | 2021-10-12 14:48:36 -0300 | [diff] [blame] | 3231 | if (compositor->default_dmabuf_feedback) { |
| 3232 | /* We were able to create the compositor's default |
| 3233 | * dma-buf feedback in the renderer, that means that the |
| 3234 | * table was already created and populated with |
| 3235 | * renderer's format/modifier pairs. So now we must |
| 3236 | * compute the scanout formats indices in the table */ |
| 3237 | scanout_formats = get_scanout_formats(b); |
| 3238 | if (!scanout_formats) |
| 3239 | goto err_udev_monitor; |
| 3240 | ret = weston_dmabuf_feedback_format_table_set_scanout_indices(compositor->dmabuf_feedback_format_table, |
| 3241 | scanout_formats); |
| 3242 | weston_drm_format_array_fini(scanout_formats); |
Daniel Stone | f508603 | 2021-12-06 16:52:19 +0000 | [diff] [blame] | 3243 | free(scanout_formats); |
Leandro Ribeiro | 5429302 | 2021-10-12 14:48:36 -0300 | [diff] [blame] | 3244 | if (ret < 0) |
| 3245 | goto err_udev_monitor; |
| 3246 | } |
Marius Vlad | ebd10e5 | 2019-11-16 19:22:48 +0200 | [diff] [blame] | 3247 | if (weston_direct_display_setup(compositor) < 0) |
| 3248 | weston_log("Error: initializing direct-display " |
| 3249 | "support failed.\n"); |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 3250 | } |
| 3251 | |
Alexandros Frantzis | acff29b | 2018-10-19 12:14:11 +0300 | [diff] [blame] | 3252 | if (compositor->capabilities & WESTON_CAP_EXPLICIT_SYNC) { |
| 3253 | if (linux_explicit_synchronization_setup(compositor) < 0) |
| 3254 | weston_log("Error: initializing explicit " |
| 3255 | " synchronization support failed.\n"); |
| 3256 | } |
| 3257 | |
Ankit Nautiyal | a344fe3 | 2019-05-14 18:36:08 +0530 | [diff] [blame] | 3258 | if (b->atomic_modeset) |
| 3259 | if (weston_compositor_enable_content_protection(compositor) < 0) |
| 3260 | weston_log("Error: initializing content-protection " |
| 3261 | "support failed.\n"); |
| 3262 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 3263 | ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME, |
| 3264 | &api, sizeof(api)); |
| 3265 | |
| 3266 | if (ret < 0) { |
| 3267 | weston_log("Failed to register output API.\n"); |
| 3268 | goto err_udev_monitor; |
| 3269 | } |
| 3270 | |
Stefan Agner | 3654c67 | 2019-07-09 00:50:30 +0200 | [diff] [blame] | 3271 | ret = drm_backend_init_virtual_output_api(compositor); |
Tomohito Esaki | b1fb00d | 2018-01-31 17:50:48 +0900 | [diff] [blame] | 3272 | if (ret < 0) { |
| 3273 | weston_log("Failed to register virtual output API.\n"); |
| 3274 | goto err_udev_monitor; |
| 3275 | } |
leng.fang | 9185607 | 2024-06-07 14:12:54 +0800 | [diff] [blame] | 3276 | |
leng.fang | 32af9fc | 2024-06-13 11:22:15 +0800 | [diff] [blame] | 3277 | #ifdef ENABLE_DRM_HELP |
| 3278 | help_set_printinfo_function(compositor, weston_print_info); |
| 3279 | #endif |
Tomohito Esaki | b1fb00d | 2018-01-31 17:50:48 +0900 | [diff] [blame] | 3280 | |
leng.fang | 9185607 | 2024-06-07 14:12:54 +0800 | [diff] [blame] | 3281 | #ifdef ENABLE_MODE_POLICY |
| 3282 | weston_start_update_env_thread(); |
| 3283 | #endif |
| 3284 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3285 | return b; |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3286 | |
| 3287 | err_udev_monitor: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3288 | wl_event_source_remove(b->udev_drm_source); |
| 3289 | udev_monitor_unref(b->udev_monitor); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3290 | err_drm_source: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3291 | wl_event_source_remove(b->drm_source); |
Ander Conselvan de Oliveira | 4ade0e4 | 2014-04-17 13:08:45 +0300 | [diff] [blame] | 3292 | err_udev_input: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3293 | udev_input_destroy(&b->input); |
Kristian Høgsberg | 2bc5e8e | 2012-09-06 20:51:00 -0400 | [diff] [blame] | 3294 | err_sprite: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3295 | destroy_sprites(b); |
Igor Matheus Andrade Torrente | 6354455 | 2020-10-13 11:25:20 -0300 | [diff] [blame] | 3296 | err_create_crtc_list: |
| 3297 | drmModeFreeResources(res); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3298 | err_udev_dev: |
| 3299 | udev_device_unref(drm_device); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3300 | err_udev: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3301 | udev_unref(b->udev); |
Marius Vlad | 5130a8c | 2020-07-30 14:47:32 +0300 | [diff] [blame] | 3302 | err_launcher: |
| 3303 | weston_launcher_destroy(compositor->launcher); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3304 | err_compositor: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3305 | weston_compositor_shutdown(compositor); |
Marius Vlad | d171c7b | 2021-04-01 00:12:00 +0300 | [diff] [blame] | 3306 | #ifdef BUILD_DRM_GBM |
| 3307 | if (b->gbm) |
| 3308 | gbm_device_destroy(b->gbm); |
| 3309 | #endif |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3310 | free(b); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3311 | return NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3312 | } |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 3313 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3314 | static void |
| 3315 | config_init_to_defaults(struct weston_drm_backend_config *config) |
| 3316 | { |
Pekka Paalanen | dee412d | 2018-04-23 11:44:58 +0200 | [diff] [blame] | 3317 | config->use_pixman_shadow = true; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3318 | } |
| 3319 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3320 | WL_EXPORT int |
Quentin Glidic | 23e1d6f | 2016-12-02 14:08:44 +0100 | [diff] [blame] | 3321 | weston_backend_init(struct weston_compositor *compositor, |
| 3322 | struct weston_backend_config *config_base) |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 3323 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3324 | struct drm_backend *b; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3325 | struct weston_drm_backend_config config = {{ 0, }}; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 3326 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3327 | if (config_base == NULL || |
| 3328 | config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION || |
| 3329 | config_base->struct_size > sizeof(struct weston_drm_backend_config)) { |
| 3330 | weston_log("drm backend config structure is invalid\n"); |
| 3331 | return -1; |
| 3332 | } |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 3333 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3334 | config_init_to_defaults(&config); |
| 3335 | memcpy(&config, config_base, config_base->struct_size); |
Kristian Høgsberg | d8e9833 | 2013-10-16 16:15:11 -0700 | [diff] [blame] | 3336 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3337 | b = drm_backend_create(compositor, &config); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3338 | if (b == NULL) |
| 3339 | return -1; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3340 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3341 | return 0; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 3342 | } |