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