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