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