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