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