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 |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 4 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 5 | * Permission is hereby granted, free of charge, to any person obtaining |
| 6 | * a copy of this software and associated documentation files (the |
| 7 | * "Software"), to deal in the Software without restriction, including |
| 8 | * without limitation the rights to use, copy, modify, merge, publish, |
| 9 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | * permit persons to whom the Software is furnished to do so, subject to |
| 11 | * the following conditions: |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 12 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 13 | * The above copyright notice and this permission notice (including the |
| 14 | * next paragraph) shall be included in all copies or substantial |
| 15 | * portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | * SOFTWARE. |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 25 | */ |
| 26 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 27 | #include "config.h" |
Kristian Høgsberg | 0b9334a | 2011-04-12 11:34:32 -0400 | [diff] [blame] | 28 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 29 | #include <errno.h> |
Jussi Kukkonen | 649bbce | 2016-07-19 14:16:27 +0300 | [diff] [blame] | 30 | #include <stdint.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 31 | #include <stdlib.h> |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 32 | #include <ctype.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 33 | #include <string.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <unistd.h> |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 36 | #include <linux/input.h> |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 37 | #include <linux/vt.h> |
Ander Conselvan de Oliveira | fd1f4c6 | 2012-06-26 17:09:14 +0300 | [diff] [blame] | 38 | #include <assert.h> |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 39 | #include <sys/mman.h> |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 40 | #include <dlfcn.h> |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 41 | #include <time.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 42 | |
Benjamin Franzke | c649a92 | 2011-03-02 11:56:04 +0100 | [diff] [blame] | 43 | #include <xf86drm.h> |
| 44 | #include <xf86drmMode.h> |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 45 | #include <drm_fourcc.h> |
Benjamin Franzke | c649a92 | 2011-03-02 11:56:04 +0100 | [diff] [blame] | 46 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 47 | #include <gbm.h> |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 48 | #include <libudev.h> |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 49 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 50 | #include "compositor.h" |
| 51 | #include "compositor-drm.h" |
Jon Cruz | 35b2eaa | 2015-06-15 15:37:08 -0700 | [diff] [blame] | 52 | #include "shared/helpers.h" |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 53 | #include "shared/timespec-util.h" |
John Kåre Alsaker | 30d2b1f | 2012-11-13 19:10:28 +0100 | [diff] [blame] | 54 | #include "gl-renderer.h" |
Vincent Abriou | c950667 | 2016-10-05 16:14:07 +0200 | [diff] [blame] | 55 | #include "weston-egl-ext.h" |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 56 | #include "pixman-renderer.h" |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 57 | #include "pixel-formats.h" |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 58 | #include "libbacklight.h" |
Peter Hutterer | 823ad33 | 2014-11-26 07:06:31 +1000 | [diff] [blame] | 59 | #include "libinput-seat.h" |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 60 | #include "launcher-util.h" |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 61 | #include "vaapi-recorder.h" |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 62 | #include "presentation-time-server-protocol.h" |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 63 | #include "linux-dmabuf.h" |
Micah Fedke | c889012 | 2017-02-01 15:28:23 -0500 | [diff] [blame] | 64 | #include "linux-dmabuf-unstable-v1-server-protocol.h" |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 65 | |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 66 | #ifndef DRM_CAP_TIMESTAMP_MONOTONIC |
| 67 | #define DRM_CAP_TIMESTAMP_MONOTONIC 0x6 |
| 68 | #endif |
| 69 | |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 70 | #ifndef DRM_CAP_CURSOR_WIDTH |
| 71 | #define DRM_CAP_CURSOR_WIDTH 0x8 |
| 72 | #endif |
| 73 | |
| 74 | #ifndef DRM_CAP_CURSOR_HEIGHT |
| 75 | #define DRM_CAP_CURSOR_HEIGHT 0x9 |
| 76 | #endif |
| 77 | |
| 78 | #ifndef GBM_BO_USE_CURSOR |
| 79 | #define GBM_BO_USE_CURSOR GBM_BO_USE_CURSOR_64X64 |
| 80 | #endif |
| 81 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 82 | struct drm_backend { |
| 83 | struct weston_backend base; |
| 84 | struct weston_compositor *compositor; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 85 | |
| 86 | struct udev *udev; |
| 87 | struct wl_event_source *drm_source; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 88 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 89 | struct udev_monitor *udev_monitor; |
| 90 | struct wl_event_source *udev_drm_source; |
| 91 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 92 | struct { |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 93 | int id; |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 94 | int fd; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 95 | char *filename; |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 96 | } drm; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 97 | struct gbm_device *gbm; |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 98 | struct wl_listener session_listener; |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 99 | uint32_t gbm_format; |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 100 | |
Rob Clark | 4339add | 2012-08-09 14:18:28 -0500 | [diff] [blame] | 101 | /* we need these parameters in order to not fail drmModeAddFB2() |
| 102 | * due to out of bounds dimensions, and then mistakenly set |
| 103 | * sprites_are_broken: |
| 104 | */ |
Daniel Stone | f214fdc | 2016-11-14 17:43:57 +0000 | [diff] [blame] | 105 | int min_width, max_width; |
| 106 | int min_height, max_height; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 107 | int no_addfb2; |
Rob Clark | 4339add | 2012-08-09 14:18:28 -0500 | [diff] [blame] | 108 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 109 | struct wl_list sprite_list; |
Kristian Høgsberg | 65bec24 | 2012-03-05 19:57:35 -0500 | [diff] [blame] | 110 | int sprites_are_broken; |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 111 | int sprites_hidden; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 112 | |
Rob Clark | ab5b1e3 | 2012-08-09 13:24:45 -0500 | [diff] [blame] | 113 | int cursors_are_broken; |
| 114 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 115 | int use_pixman; |
| 116 | |
Rob Bradford | d355b80 | 2013-05-31 18:09:55 +0100 | [diff] [blame] | 117 | struct udev_input input; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 118 | |
Daniel Stone | 70d337d | 2015-06-16 18:42:23 +0100 | [diff] [blame] | 119 | int32_t cursor_width; |
| 120 | int32_t cursor_height; |
Ucan, Emre (ADITG/SW1) | 21e4944 | 2017-02-02 14:06:55 +0000 | [diff] [blame] | 121 | |
| 122 | uint32_t connector; |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 123 | uint32_t pageflip_timeout; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 124 | }; |
| 125 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 126 | struct drm_mode { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 127 | struct weston_mode base; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 128 | drmModeModeInfo mode_info; |
| 129 | }; |
| 130 | |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 131 | enum drm_fb_type { |
| 132 | BUFFER_INVALID = 0, /**< never used */ |
| 133 | BUFFER_CLIENT, /**< directly sourced from client */ |
| 134 | BUFFER_PIXMAN_DUMB, /**< internal Pixman rendering */ |
| 135 | BUFFER_GBM_SURFACE, /**< internal EGL rendering */ |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 136 | BUFFER_CURSOR, /**< internal cursor buffer */ |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 137 | }; |
| 138 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 139 | struct drm_fb { |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 140 | enum drm_fb_type type; |
| 141 | |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 142 | int refcnt; |
| 143 | |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 144 | uint32_t fb_id, stride, handle, size; |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 145 | const struct pixel_format_info *format; |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 146 | int width, height; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 147 | int fd; |
Pekka Paalanen | de685b8 | 2012-12-04 15:58:12 +0200 | [diff] [blame] | 148 | struct weston_buffer_reference buffer_ref; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 149 | |
| 150 | /* Used by gbm fbs */ |
| 151 | struct gbm_bo *bo; |
Daniel Stone | 05a5ac2 | 2017-04-04 17:54:25 +0100 | [diff] [blame] | 152 | struct gbm_surface *gbm_surface; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 153 | |
| 154 | /* Used by dumb fbs */ |
| 155 | void *map; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 156 | }; |
| 157 | |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 158 | struct drm_edid { |
| 159 | char eisa_id[13]; |
| 160 | char monitor_name[13]; |
| 161 | char pnp_id[5]; |
| 162 | char serial_number[13]; |
| 163 | }; |
| 164 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 165 | /** |
| 166 | * A plane represents one buffer, positioned within a CRTC, and stacked |
| 167 | * relative to other planes on the same CRTC. |
| 168 | * |
| 169 | * Each CRTC has a 'primary plane', which use used to display the classic |
| 170 | * framebuffer contents, as accessed through the legacy drmModeSetCrtc |
| 171 | * call (which combines setting the CRTC's actual physical mode, and the |
| 172 | * properties of the primary plane). |
| 173 | * |
| 174 | * The cursor plane also has its own alternate legacy API. |
| 175 | * |
| 176 | * Other planes are used opportunistically to display content we do not |
| 177 | * wish to blit into the primary plane. These non-primary/cursor planes |
| 178 | * are referred to as 'sprites'. |
| 179 | */ |
| 180 | struct drm_plane { |
| 181 | struct wl_list link; |
| 182 | |
| 183 | struct weston_plane base; |
| 184 | |
| 185 | struct drm_output *output; |
| 186 | struct drm_backend *backend; |
| 187 | |
| 188 | uint32_t possible_crtcs; |
| 189 | uint32_t plane_id; |
| 190 | uint32_t count_formats; |
| 191 | |
| 192 | /* The last framebuffer submitted to the kernel for this plane. */ |
| 193 | struct drm_fb *fb_current; |
| 194 | /* The previously-submitted framebuffer, where the hardware has not |
| 195 | * yet acknowledged display of fb_current. */ |
| 196 | struct drm_fb *fb_last; |
| 197 | /* Framebuffer we are going to submit to the kernel when the current |
| 198 | * repaint is flushed. */ |
| 199 | struct drm_fb *fb_pending; |
| 200 | |
| 201 | int32_t src_x, src_y; |
| 202 | uint32_t src_w, src_h; |
| 203 | uint32_t dest_x, dest_y; |
| 204 | uint32_t dest_w, dest_h; |
| 205 | |
| 206 | uint32_t formats[]; |
| 207 | }; |
| 208 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 209 | struct drm_output { |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 210 | struct weston_output base; |
| 211 | drmModeConnector *connector; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 212 | |
Daniel Stone | 1733923 | 2015-11-28 12:09:47 +0000 | [diff] [blame] | 213 | uint32_t crtc_id; /* object ID to pass to DRM functions */ |
| 214 | int pipe; /* index of CRTC in resource array / bitmasks */ |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 215 | uint32_t connector_id; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 216 | drmModeCrtcPtr original_crtc; |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 217 | struct drm_edid edid; |
Ander Conselvan de Oliveira | a0a433a | 2013-06-04 16:24:04 +0300 | [diff] [blame] | 218 | drmModePropertyPtr dpms_prop; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 219 | |
Daniel Stone | 36609c7 | 2015-06-18 07:49:02 +0100 | [diff] [blame] | 220 | enum dpms_enum dpms; |
Daniel Stone | 5bb8f58 | 2017-04-04 17:54:28 +0100 | [diff] [blame] | 221 | struct backlight *backlight; |
Daniel Stone | 36609c7 | 2015-06-18 07:49:02 +0100 | [diff] [blame] | 222 | |
Pekka Paalanen | 6b65d8f | 2017-07-27 13:44:32 +0300 | [diff] [blame] | 223 | bool state_invalid; |
| 224 | |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 225 | int vblank_pending; |
| 226 | int page_flip_pending; |
Xiong Zhang | abd5d47 | 2013-10-11 14:43:07 +0800 | [diff] [blame] | 227 | int destroy_pending; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 228 | int disable_pending; |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 229 | |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 230 | struct drm_fb *gbm_cursor_fb[2]; |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 231 | struct weston_plane cursor_plane; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 232 | struct weston_view *cursor_view; |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 233 | int current_cursor; |
Daniel Stone | 5bb8f58 | 2017-04-04 17:54:28 +0100 | [diff] [blame] | 234 | |
| 235 | struct gbm_surface *gbm_surface; |
| 236 | uint32_t gbm_format; |
| 237 | |
| 238 | struct weston_plane fb_plane; |
Daniel Stone | f30a18c | 2017-04-04 17:54:31 +0100 | [diff] [blame] | 239 | |
| 240 | /* The last framebuffer submitted to the kernel for this CRTC. */ |
| 241 | struct drm_fb *fb_current; |
| 242 | /* The previously-submitted framebuffer, where the hardware has not |
| 243 | * yet acknowledged display of fb_current. */ |
| 244 | struct drm_fb *fb_last; |
| 245 | /* Framebuffer we are going to submit to the kernel when the current |
| 246 | * repaint is flushed. */ |
| 247 | struct drm_fb *fb_pending; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 248 | |
| 249 | struct drm_fb *dumb[2]; |
| 250 | pixman_image_t *image[2]; |
| 251 | int current_image; |
| 252 | pixman_region32_t previous_damage; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 253 | |
| 254 | struct vaapi_recorder *recorder; |
| 255 | struct wl_listener recorder_frame_listener; |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 256 | |
| 257 | struct wl_event_source *pageflip_timer; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 258 | }; |
| 259 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 260 | static struct gl_renderer_interface *gl_renderer; |
| 261 | |
Kristian Høgsberg | 98cfea6 | 2013-02-18 16:15:53 -0500 | [diff] [blame] | 262 | static const char default_seat[] = "seat0"; |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 263 | |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 264 | static inline struct drm_output * |
| 265 | to_drm_output(struct weston_output *base) |
| 266 | { |
| 267 | return container_of(base, struct drm_output, base); |
| 268 | } |
| 269 | |
| 270 | static inline struct drm_backend * |
| 271 | to_drm_backend(struct weston_compositor *base) |
| 272 | { |
| 273 | return container_of(base->backend, struct drm_backend, base); |
| 274 | } |
| 275 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 276 | static int |
| 277 | pageflip_timeout(void *data) { |
| 278 | /* |
| 279 | * Our timer just went off, that means we're not receiving drm |
| 280 | * page flip events anymore for that output. Let's gracefully exit |
| 281 | * weston with a return value so devs can debug what's going on. |
| 282 | */ |
| 283 | struct drm_output *output = data; |
| 284 | struct weston_compositor *compositor = output->base.compositor; |
| 285 | |
| 286 | weston_log("Pageflip timeout reached on output %s, your " |
| 287 | "driver is probably buggy! Exiting.\n", |
| 288 | output->base.name); |
| 289 | weston_compositor_exit_with_code(compositor, EXIT_FAILURE); |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | /* Creates the pageflip timer. Note that it isn't armed by default */ |
| 295 | static int |
| 296 | drm_output_pageflip_timer_create(struct drm_output *output) |
| 297 | { |
| 298 | struct wl_event_loop *loop = NULL; |
| 299 | struct weston_compositor *ec = output->base.compositor; |
| 300 | |
| 301 | loop = wl_display_get_event_loop(ec->wl_display); |
| 302 | assert(loop); |
| 303 | output->pageflip_timer = wl_event_loop_add_timer(loop, |
| 304 | pageflip_timeout, |
| 305 | output); |
| 306 | |
| 307 | if (output->pageflip_timer == NULL) { |
| 308 | weston_log("creating drm pageflip timer failed: %m\n"); |
| 309 | return -1; |
| 310 | } |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 315 | static void |
| 316 | drm_output_set_cursor(struct drm_output *output); |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 317 | |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 318 | static void |
| 319 | drm_output_update_msc(struct drm_output *output, unsigned int seq); |
| 320 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 321 | static int |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 322 | drm_plane_crtc_supported(struct drm_output *output, struct drm_plane *plane) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 323 | { |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 324 | return !!(plane->possible_crtcs & (1 << output->pipe)); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 325 | } |
| 326 | |
Daniel Stone | 72c0e1b | 2017-02-09 13:49:15 +0000 | [diff] [blame] | 327 | static struct drm_output * |
| 328 | drm_output_find_by_crtc(struct drm_backend *b, uint32_t crtc_id) |
| 329 | { |
| 330 | struct drm_output *output; |
| 331 | |
| 332 | wl_list_for_each(output, &b->compositor->output_list, base.link) { |
| 333 | if (output->crtc_id == crtc_id) |
| 334 | return output; |
| 335 | } |
| 336 | |
| 337 | wl_list_for_each(output, &b->compositor->pending_output_list, |
| 338 | base.link) { |
| 339 | if (output->crtc_id == crtc_id) |
| 340 | return output; |
| 341 | } |
| 342 | |
| 343 | return NULL; |
| 344 | } |
| 345 | |
Daniel Stone | c0ec759 | 2017-02-09 13:58:35 +0000 | [diff] [blame] | 346 | static struct drm_output * |
| 347 | drm_output_find_by_connector(struct drm_backend *b, uint32_t connector_id) |
| 348 | { |
| 349 | struct drm_output *output; |
| 350 | |
| 351 | wl_list_for_each(output, &b->compositor->output_list, base.link) { |
| 352 | if (output->connector_id == connector_id) |
| 353 | return output; |
| 354 | } |
| 355 | |
| 356 | wl_list_for_each(output, &b->compositor->pending_output_list, |
| 357 | base.link) { |
| 358 | if (output->connector_id == connector_id) |
| 359 | return output; |
| 360 | } |
| 361 | |
| 362 | return NULL; |
| 363 | } |
| 364 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 365 | static void |
Tomohito Esaki | 576f42e | 2017-04-04 17:54:24 +0100 | [diff] [blame] | 366 | drm_fb_destroy(struct drm_fb *fb) |
| 367 | { |
| 368 | if (fb->fb_id != 0) |
| 369 | drmModeRmFB(fb->fd, fb->fb_id); |
| 370 | weston_buffer_reference(&fb->buffer_ref, NULL); |
| 371 | free(fb); |
| 372 | } |
| 373 | |
| 374 | static void |
| 375 | drm_fb_destroy_dumb(struct drm_fb *fb) |
| 376 | { |
| 377 | struct drm_mode_destroy_dumb destroy_arg; |
| 378 | |
| 379 | assert(fb->type == BUFFER_PIXMAN_DUMB); |
| 380 | |
| 381 | if (fb->map && fb->size > 0) |
| 382 | munmap(fb->map, fb->size); |
| 383 | |
| 384 | memset(&destroy_arg, 0, sizeof(destroy_arg)); |
| 385 | destroy_arg.handle = fb->handle; |
| 386 | drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg); |
| 387 | |
| 388 | drm_fb_destroy(fb); |
| 389 | } |
| 390 | |
| 391 | static void |
| 392 | drm_fb_destroy_gbm(struct gbm_bo *bo, void *data) |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 393 | { |
| 394 | struct drm_fb *fb = data; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 395 | |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 396 | assert(fb->type == BUFFER_GBM_SURFACE || fb->type == BUFFER_CLIENT || |
| 397 | fb->type == BUFFER_CURSOR); |
Tomohito Esaki | 576f42e | 2017-04-04 17:54:24 +0100 | [diff] [blame] | 398 | drm_fb_destroy(fb); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | static struct drm_fb * |
Daniel Stone | f214fdc | 2016-11-14 17:43:57 +0000 | [diff] [blame] | 402 | drm_fb_create_dumb(struct drm_backend *b, int width, int height, |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 403 | uint32_t format) |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 404 | { |
| 405 | struct drm_fb *fb; |
| 406 | int ret; |
| 407 | |
| 408 | struct drm_mode_create_dumb create_arg; |
| 409 | struct drm_mode_destroy_dumb destroy_arg; |
| 410 | struct drm_mode_map_dumb map_arg; |
| 411 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 412 | fb = zalloc(sizeof *fb); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 413 | if (!fb) |
| 414 | return NULL; |
| 415 | |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 416 | fb->refcnt = 1; |
| 417 | |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 418 | fb->format = pixel_format_get_info(format); |
| 419 | if (!fb->format) { |
| 420 | weston_log("failed to look up format 0x%lx\n", |
| 421 | (unsigned long) format); |
| 422 | goto err_fb; |
| 423 | } |
| 424 | |
| 425 | if (!fb->format->depth || !fb->format->bpp) { |
| 426 | weston_log("format 0x%lx is not compatible with dumb buffers\n", |
| 427 | (unsigned long) format); |
| 428 | goto err_fb; |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 429 | } |
| 430 | |
Kristian Høgsberg | ac6104e | 2013-08-21 22:14:14 -0700 | [diff] [blame] | 431 | memset(&create_arg, 0, sizeof create_arg); |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 432 | create_arg.bpp = fb->format->bpp; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 433 | create_arg.width = width; |
| 434 | create_arg.height = height; |
| 435 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 436 | ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 437 | if (ret) |
| 438 | goto err_fb; |
| 439 | |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 440 | fb->type = BUFFER_PIXMAN_DUMB; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 441 | fb->handle = create_arg.handle; |
| 442 | fb->stride = create_arg.pitch; |
| 443 | fb->size = create_arg.size; |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 444 | fb->width = width; |
| 445 | fb->height = height; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 446 | fb->fd = b->drm.fd; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 447 | |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 448 | ret = -1; |
| 449 | |
| 450 | if (!b->no_addfb2) { |
Yong Bakos | 4b6321f | 2016-08-17 17:37:55 -0700 | [diff] [blame] | 451 | uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 }; |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 452 | |
| 453 | handles[0] = fb->handle; |
| 454 | pitches[0] = fb->stride; |
| 455 | offsets[0] = 0; |
| 456 | |
| 457 | ret = drmModeAddFB2(b->drm.fd, width, height, |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 458 | fb->format->format, |
| 459 | handles, pitches, offsets, |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 460 | &fb->fb_id, 0); |
| 461 | if (ret) { |
| 462 | weston_log("addfb2 failed: %m\n"); |
| 463 | b->no_addfb2 = 1; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | if (ret) { |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 468 | ret = drmModeAddFB(b->drm.fd, width, height, |
| 469 | fb->format->depth, fb->format->bpp, |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 470 | fb->stride, fb->handle, &fb->fb_id); |
| 471 | } |
| 472 | |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 473 | if (ret) |
| 474 | goto err_bo; |
| 475 | |
Kristian Høgsberg | ac6104e | 2013-08-21 22:14:14 -0700 | [diff] [blame] | 476 | memset(&map_arg, 0, sizeof map_arg); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 477 | map_arg.handle = fb->handle; |
Chris Michael | eb2074a | 2013-05-01 21:26:02 -0400 | [diff] [blame] | 478 | ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 479 | if (ret) |
| 480 | goto err_add_fb; |
| 481 | |
Chris Michael | 4a7ce1f | 2015-11-10 10:40:37 -0500 | [diff] [blame] | 482 | fb->map = mmap(NULL, fb->size, PROT_WRITE, |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 483 | MAP_SHARED, b->drm.fd, map_arg.offset); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 484 | if (fb->map == MAP_FAILED) |
| 485 | goto err_add_fb; |
| 486 | |
| 487 | return fb; |
| 488 | |
| 489 | err_add_fb: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 490 | drmModeRmFB(b->drm.fd, fb->fb_id); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 491 | err_bo: |
| 492 | memset(&destroy_arg, 0, sizeof(destroy_arg)); |
| 493 | destroy_arg.handle = create_arg.handle; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 494 | drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 495 | err_fb: |
| 496 | free(fb); |
| 497 | return NULL; |
| 498 | } |
| 499 | |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 500 | static struct drm_fb * |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 501 | drm_fb_ref(struct drm_fb *fb) |
| 502 | { |
| 503 | fb->refcnt++; |
| 504 | return fb; |
| 505 | } |
| 506 | |
| 507 | static struct drm_fb * |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 508 | drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend, |
| 509 | uint32_t format, enum drm_fb_type type) |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 510 | { |
| 511 | struct drm_fb *fb = gbm_bo_get_user_data(bo); |
Derek Foreman | 482ffdf | 2016-07-08 12:50:57 -0500 | [diff] [blame] | 512 | uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 }; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 513 | int ret; |
| 514 | |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 515 | if (fb) { |
| 516 | assert(fb->type == type); |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 517 | return drm_fb_ref(fb); |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 518 | } |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 519 | |
Bryce Harrington | de16d89 | 2014-11-20 22:21:57 -0800 | [diff] [blame] | 520 | fb = zalloc(sizeof *fb); |
| 521 | if (fb == NULL) |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 522 | return NULL; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 523 | |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 524 | fb->type = type; |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 525 | fb->refcnt = 1; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 526 | fb->bo = bo; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 527 | |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 528 | fb->width = gbm_bo_get_width(bo); |
| 529 | fb->height = gbm_bo_get_height(bo); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 530 | fb->stride = gbm_bo_get_stride(bo); |
| 531 | fb->handle = gbm_bo_get_handle(bo).u32; |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 532 | fb->format = pixel_format_get_info(format); |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 533 | fb->size = fb->stride * fb->height; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 534 | fb->fd = backend->drm.fd; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 535 | |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 536 | if (!fb->format) { |
| 537 | weston_log("couldn't look up format 0x%lx\n", |
| 538 | (unsigned long) format); |
| 539 | goto err_free; |
| 540 | } |
| 541 | |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 542 | if (backend->min_width > fb->width || |
| 543 | fb->width > backend->max_width || |
| 544 | backend->min_height > fb->height || |
| 545 | fb->height > backend->max_height) { |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 546 | weston_log("bo geometry out of bounds\n"); |
| 547 | goto err_free; |
| 548 | } |
| 549 | |
| 550 | ret = -1; |
| 551 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 552 | if (format && !backend->no_addfb2) { |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 553 | handles[0] = fb->handle; |
| 554 | pitches[0] = fb->stride; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 555 | offsets[0] = 0; |
| 556 | |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 557 | ret = drmModeAddFB2(backend->drm.fd, fb->width, fb->height, |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 558 | format, handles, pitches, offsets, |
| 559 | &fb->fb_id, 0); |
| 560 | if (ret) { |
| 561 | weston_log("addfb2 failed: %m\n"); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 562 | backend->no_addfb2 = 1; |
| 563 | backend->sprites_are_broken = 1; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 567 | if (ret && fb->format->depth && fb->format->bpp) |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 568 | ret = drmModeAddFB(backend->drm.fd, fb->width, fb->height, |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 569 | fb->format->depth, fb->format->bpp, |
| 570 | fb->stride, fb->handle, &fb->fb_id); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 571 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 572 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 573 | weston_log("failed to create kms fb: %m\n"); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 574 | goto err_free; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 575 | } |
| 576 | |
Tomohito Esaki | 576f42e | 2017-04-04 17:54:24 +0100 | [diff] [blame] | 577 | gbm_bo_set_user_data(bo, fb, drm_fb_destroy_gbm); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 578 | |
| 579 | return fb; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 580 | |
| 581 | err_free: |
| 582 | free(fb); |
| 583 | return NULL; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | static void |
Jason Ekstrand | 6bd6294 | 2013-06-20 20:38:23 -0500 | [diff] [blame] | 587 | drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer) |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 588 | { |
Pekka Paalanen | de685b8 | 2012-12-04 15:58:12 +0200 | [diff] [blame] | 589 | assert(fb->buffer_ref.buffer == NULL); |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 590 | assert(fb->type == BUFFER_CLIENT); |
Pekka Paalanen | de685b8 | 2012-12-04 15:58:12 +0200 | [diff] [blame] | 591 | weston_buffer_reference(&fb->buffer_ref, buffer); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 592 | } |
| 593 | |
Ander Conselvan de Oliveira | 526d461 | 2013-01-25 15:13:03 +0200 | [diff] [blame] | 594 | static void |
Daniel Stone | 05a5ac2 | 2017-04-04 17:54:25 +0100 | [diff] [blame] | 595 | drm_fb_unref(struct drm_fb *fb) |
Ander Conselvan de Oliveira | 526d461 | 2013-01-25 15:13:03 +0200 | [diff] [blame] | 596 | { |
| 597 | if (!fb) |
| 598 | return; |
| 599 | |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 600 | assert(fb->refcnt > 0); |
| 601 | if (--fb->refcnt > 0) |
| 602 | return; |
| 603 | |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 604 | switch (fb->type) { |
| 605 | case BUFFER_PIXMAN_DUMB: |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 606 | drm_fb_destroy_dumb(fb); |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 607 | break; |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 608 | case BUFFER_CURSOR: |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 609 | case BUFFER_CLIENT: |
| 610 | gbm_bo_destroy(fb->bo); |
| 611 | break; |
| 612 | case BUFFER_GBM_SURFACE: |
Daniel Stone | 05a5ac2 | 2017-04-04 17:54:25 +0100 | [diff] [blame] | 613 | gbm_surface_release_buffer(fb->gbm_surface, fb->bo); |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 614 | break; |
| 615 | default: |
| 616 | assert(NULL); |
| 617 | break; |
Ander Conselvan de Oliveira | 526d461 | 2013-01-25 15:13:03 +0200 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 621 | static int |
| 622 | drm_view_transform_supported(struct weston_view *ev) |
| 623 | { |
| 624 | return !ev->transform.enabled || |
| 625 | (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE); |
| 626 | } |
| 627 | |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 628 | static uint32_t |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 629 | drm_output_check_scanout_format(struct drm_output *output, |
| 630 | struct weston_surface *es, struct gbm_bo *bo) |
| 631 | { |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 632 | uint32_t format; |
| 633 | pixman_region32_t r; |
| 634 | |
| 635 | format = gbm_bo_get_format(bo); |
| 636 | |
Kristian Høgsberg | 3f97b34 | 2013-10-16 16:08:57 -0700 | [diff] [blame] | 637 | if (format == GBM_FORMAT_ARGB8888) { |
| 638 | /* We can scanout an ARGB buffer if the surface's |
| 639 | * opaque region covers the whole output, but we have |
| 640 | * to use XRGB as the KMS format code. */ |
Kristian Høgsberg | 1be87e3 | 2014-01-17 14:22:41 -0800 | [diff] [blame] | 641 | pixman_region32_init_rect(&r, 0, 0, |
| 642 | output->base.width, |
| 643 | output->base.height); |
| 644 | pixman_region32_subtract(&r, &r, &es->opaque); |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 645 | |
| 646 | if (!pixman_region32_not_empty(&r)) |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 647 | format = GBM_FORMAT_XRGB8888; |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 648 | |
| 649 | pixman_region32_fini(&r); |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 650 | } |
Kristian Høgsberg | 3f97b34 | 2013-10-16 16:08:57 -0700 | [diff] [blame] | 651 | |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 652 | if (output->gbm_format == format) |
Kristian Høgsberg | 3f97b34 | 2013-10-16 16:08:57 -0700 | [diff] [blame] | 653 | return format; |
| 654 | |
| 655 | return 0; |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 656 | } |
| 657 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 658 | static struct weston_plane * |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 659 | drm_output_prepare_scanout_view(struct drm_output *output, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 660 | struct weston_view *ev) |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 661 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 662 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 663 | struct weston_buffer *buffer = ev->surface->buffer_ref.buffer; |
Pekka Paalanen | 952b6c8 | 2014-03-14 14:38:15 +0200 | [diff] [blame] | 664 | struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 665 | struct gbm_bo *bo; |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 666 | uint32_t format; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 667 | |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 668 | /* Don't import buffers which span multiple outputs. */ |
| 669 | if (ev->output_mask != (1u << output->base.id)) |
| 670 | return NULL; |
| 671 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 672 | /* We use GBM to import buffers. */ |
| 673 | if (b->gbm == NULL) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 674 | return NULL; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 675 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 676 | if (buffer == NULL) |
| 677 | return NULL; |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 678 | if (wl_shm_buffer_get(buffer->resource)) |
| 679 | return NULL; |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 680 | |
| 681 | /* Make sure our view is exactly compatible with the output. */ |
| 682 | if (ev->geometry.x != output->base.x || |
| 683 | ev->geometry.y != output->base.y) |
| 684 | return NULL; |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 685 | if (buffer->width != output->base.current_mode->width || |
| 686 | buffer->height != output->base.current_mode->height) |
| 687 | return NULL; |
| 688 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 689 | if (ev->transform.enabled) |
| 690 | return NULL; |
Pekka Paalanen | 5580f22 | 2015-02-17 16:33:18 +0200 | [diff] [blame] | 691 | if (ev->geometry.scissor_enabled) |
| 692 | return NULL; |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 693 | if (viewport->buffer.transform != output->base.transform) |
| 694 | return NULL; |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 695 | if (viewport->buffer.scale != output->base.current_scale) |
| 696 | return NULL; |
| 697 | if (!drm_view_transform_supported(ev)) |
| 698 | return NULL; |
| 699 | |
| 700 | if (ev->alpha != 1.0f) |
| 701 | return NULL; |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 702 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 703 | bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER, |
Kristian Høgsberg | 6399646 | 2013-09-03 22:27:08 -0700 | [diff] [blame] | 704 | buffer->resource, GBM_BO_USE_SCANOUT); |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 705 | |
Rob Bradford | 9b10187 | 2012-09-14 23:25:41 +0100 | [diff] [blame] | 706 | /* Unable to use the buffer for scanout */ |
| 707 | if (!bo) |
| 708 | return NULL; |
| 709 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 710 | format = drm_output_check_scanout_format(output, ev->surface, bo); |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 711 | if (format == 0) { |
Ander Conselvan de Oliveira | a64b15d | 2012-05-02 16:42:22 +0300 | [diff] [blame] | 712 | gbm_bo_destroy(bo); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 713 | return NULL; |
Ander Conselvan de Oliveira | a64b15d | 2012-05-02 16:42:22 +0300 | [diff] [blame] | 714 | } |
| 715 | |
Daniel Stone | 5bb8f58 | 2017-04-04 17:54:28 +0100 | [diff] [blame] | 716 | output->fb_pending = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT); |
| 717 | if (!output->fb_pending) { |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 718 | gbm_bo_destroy(bo); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 719 | return NULL; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 720 | } |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 721 | |
Daniel Stone | 5bb8f58 | 2017-04-04 17:54:28 +0100 | [diff] [blame] | 722 | drm_fb_set_buffer(output->fb_pending, buffer); |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 723 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 724 | return &output->fb_plane; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 725 | } |
| 726 | |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 727 | static struct drm_fb * |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 728 | drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage) |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 729 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 730 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 731 | struct gbm_bo *bo; |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 732 | struct drm_fb *ret; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 733 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 734 | output->base.compositor->renderer->repaint_output(&output->base, |
| 735 | damage); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 736 | |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 737 | bo = gbm_surface_lock_front_buffer(output->gbm_surface); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 738 | if (!bo) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 739 | weston_log("failed to lock front buffer: %m\n"); |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 740 | return NULL; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 741 | } |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 742 | |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 743 | ret = drm_fb_get_from_bo(bo, b, output->gbm_format, BUFFER_GBM_SURFACE); |
| 744 | if (!ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 745 | weston_log("failed to get drm_fb for bo\n"); |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 746 | gbm_surface_release_buffer(output->gbm_surface, bo); |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 747 | return NULL; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 748 | } |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 749 | ret->gbm_surface = output->gbm_surface; |
| 750 | |
| 751 | return ret; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 752 | } |
| 753 | |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 754 | static struct drm_fb * |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 755 | drm_output_render_pixman(struct drm_output *output, pixman_region32_t *damage) |
| 756 | { |
| 757 | struct weston_compositor *ec = output->base.compositor; |
| 758 | pixman_region32_t total_damage, previous_damage; |
| 759 | |
| 760 | pixman_region32_init(&total_damage); |
| 761 | pixman_region32_init(&previous_damage); |
| 762 | |
| 763 | pixman_region32_copy(&previous_damage, damage); |
| 764 | |
| 765 | pixman_region32_union(&total_damage, damage, &output->previous_damage); |
| 766 | pixman_region32_copy(&output->previous_damage, &previous_damage); |
| 767 | |
| 768 | output->current_image ^= 1; |
| 769 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 770 | pixman_renderer_output_set_buffer(&output->base, |
| 771 | output->image[output->current_image]); |
| 772 | |
| 773 | ec->renderer->repaint_output(&output->base, &total_damage); |
| 774 | |
| 775 | pixman_region32_fini(&total_damage); |
| 776 | pixman_region32_fini(&previous_damage); |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 777 | |
| 778 | return drm_fb_ref(output->dumb[output->current_image]); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | static void |
| 782 | drm_output_render(struct drm_output *output, pixman_region32_t *damage) |
| 783 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 784 | struct weston_compositor *c = output->base.compositor; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 785 | struct drm_backend *b = to_drm_backend(c); |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 786 | struct drm_fb *fb; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 787 | |
Daniel Stone | 4e84f7d | 2017-04-04 17:54:29 +0100 | [diff] [blame] | 788 | /* If we already have a client buffer promoted to scanout, then we don't |
| 789 | * want to render. */ |
| 790 | if (output->fb_pending) |
| 791 | return; |
| 792 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 793 | if (b->use_pixman) |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 794 | fb = drm_output_render_pixman(output, damage); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 795 | else |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 796 | fb = drm_output_render_gl(output, damage); |
| 797 | |
| 798 | if (!fb) |
| 799 | return; |
| 800 | output->fb_pending = fb; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 801 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 802 | pixman_region32_subtract(&c->primary_plane.damage, |
| 803 | &c->primary_plane.damage, damage); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | static void |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 807 | drm_output_set_gamma(struct weston_output *output_base, |
| 808 | uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b) |
| 809 | { |
| 810 | int rc; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 811 | struct drm_output *output = to_drm_output(output_base); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 812 | struct drm_backend *backend = |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 813 | to_drm_backend(output->base.compositor); |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 814 | |
| 815 | /* check */ |
| 816 | if (output_base->gamma_size != size) |
| 817 | return; |
| 818 | if (!output->original_crtc) |
| 819 | return; |
| 820 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 821 | rc = drmModeCrtcSetGamma(backend->drm.fd, |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 822 | output->crtc_id, |
| 823 | size, r, g, b); |
| 824 | if (rc) |
| 825 | weston_log("set gamma failed: %m\n"); |
| 826 | } |
| 827 | |
Bryce Harrington | ada4f07 | 2015-06-30 13:25:46 -0700 | [diff] [blame] | 828 | /* Determine the type of vblank synchronization to use for the output. |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 829 | * |
Bryce Harrington | ada4f07 | 2015-06-30 13:25:46 -0700 | [diff] [blame] | 830 | * The pipe parameter indicates which CRTC is in use. Knowing this, we |
| 831 | * can determine which vblank sequence type to use for it. Traditional |
| 832 | * cards had only two CRTCs, with CRTC 0 using no special flags, and |
| 833 | * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe |
| 834 | * parameter indicates this. |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 835 | * |
Bryce Harrington | ada4f07 | 2015-06-30 13:25:46 -0700 | [diff] [blame] | 836 | * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between |
| 837 | * 0-31. If this is non-zero it indicates we're dealing with a |
| 838 | * multi-gpu situation and we need to calculate the vblank sync |
| 839 | * using DRM_BLANK_HIGH_CRTC_MASK. |
| 840 | */ |
Pekka Paalanen | c8a1ff0 | 2015-07-02 15:06:08 +0300 | [diff] [blame] | 841 | static unsigned int |
| 842 | drm_waitvblank_pipe(struct drm_output *output) |
Mario Kleiner | 2ab4f4e | 2015-06-21 21:25:13 +0200 | [diff] [blame] | 843 | { |
| 844 | if (output->pipe > 1) |
| 845 | return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) & |
| 846 | DRM_VBLANK_HIGH_CRTC_MASK; |
| 847 | else if (output->pipe > 0) |
| 848 | return DRM_VBLANK_SECONDARY; |
| 849 | else |
| 850 | return 0; |
| 851 | } |
| 852 | |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 853 | static int |
Kristian Høgsberg | 6ddcdae | 2012-02-28 22:31:58 -0500 | [diff] [blame] | 854 | drm_output_repaint(struct weston_output *output_base, |
Daniel Stone | b1f166d | 2017-03-01 11:34:10 +0000 | [diff] [blame] | 855 | pixman_region32_t *damage, |
| 856 | void *repaint_data) |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 857 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 858 | struct drm_output *output = to_drm_output(output_base); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 859 | struct drm_backend *backend = |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 860 | to_drm_backend(output->base.compositor); |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 861 | struct drm_plane *s; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 862 | struct drm_mode *mode; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 863 | int ret = 0; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 864 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 865 | if (output->disable_pending || output->destroy_pending) |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 866 | return -1; |
Xiong Zhang | abd5d47 | 2013-10-11 14:43:07 +0800 | [diff] [blame] | 867 | |
Daniel Stone | f30a18c | 2017-04-04 17:54:31 +0100 | [diff] [blame] | 868 | assert(!output->fb_last); |
| 869 | |
Derek Foreman | 2cd87fe | 2017-04-13 13:48:48 -0500 | [diff] [blame] | 870 | /* If disable_planes is set then assign_planes() wasn't |
| 871 | * called for this render, so we could still have a stale |
| 872 | * cursor plane set up. |
| 873 | */ |
| 874 | if (output->base.disable_planes) { |
| 875 | output->cursor_view = NULL; |
| 876 | output->cursor_plane.x = INT32_MIN; |
| 877 | output->cursor_plane.y = INT32_MIN; |
| 878 | } |
| 879 | |
Daniel Stone | 4e84f7d | 2017-04-04 17:54:29 +0100 | [diff] [blame] | 880 | drm_output_render(output, damage); |
Daniel Stone | 5bb8f58 | 2017-04-04 17:54:28 +0100 | [diff] [blame] | 881 | if (!output->fb_pending) |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 882 | return -1; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 883 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 884 | mode = container_of(output->base.current_mode, struct drm_mode, base); |
Pekka Paalanen | 6b65d8f | 2017-07-27 13:44:32 +0300 | [diff] [blame] | 885 | if (output->state_invalid || !output->fb_current || |
Daniel Stone | 5bb8f58 | 2017-04-04 17:54:28 +0100 | [diff] [blame] | 886 | output->fb_current->stride != output->fb_pending->stride) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 887 | ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id, |
Daniel Stone | 5bb8f58 | 2017-04-04 17:54:28 +0100 | [diff] [blame] | 888 | output->fb_pending->fb_id, 0, 0, |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 889 | &output->connector_id, 1, |
| 890 | &mode->mode_info); |
| 891 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 892 | weston_log("set mode failed: %m\n"); |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 893 | goto err_pageflip; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 894 | } |
Ander Conselvan de Oliveira | 722a2d5 | 2013-06-04 16:24:05 +0300 | [diff] [blame] | 895 | output_base->set_dpms(output_base, WESTON_DPMS_ON); |
Pekka Paalanen | 6b65d8f | 2017-07-27 13:44:32 +0300 | [diff] [blame] | 896 | |
| 897 | output->state_invalid = false; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 898 | } |
| 899 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 900 | if (drmModePageFlip(backend->drm.fd, output->crtc_id, |
Daniel Stone | 5bb8f58 | 2017-04-04 17:54:28 +0100 | [diff] [blame] | 901 | output->fb_pending->fb_id, |
Kristian Høgsberg | 54f14c3 | 2012-01-18 11:47:41 -0500 | [diff] [blame] | 902 | DRM_MODE_PAGE_FLIP_EVENT, output) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 903 | weston_log("queueing pageflip failed: %m\n"); |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 904 | goto err_pageflip; |
Kristian Høgsberg | 54f14c3 | 2012-01-18 11:47:41 -0500 | [diff] [blame] | 905 | } |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 906 | |
Daniel Stone | f30a18c | 2017-04-04 17:54:31 +0100 | [diff] [blame] | 907 | output->fb_last = output->fb_current; |
| 908 | output->fb_current = output->fb_pending; |
| 909 | output->fb_pending = NULL; |
| 910 | |
Daniel Stone | 205c0a0 | 2017-04-04 17:54:33 +0100 | [diff] [blame] | 911 | assert(!output->page_flip_pending); |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 912 | output->page_flip_pending = 1; |
| 913 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 914 | if (output->pageflip_timer) |
| 915 | wl_event_source_timer_update(output->pageflip_timer, |
| 916 | backend->pageflip_timeout); |
| 917 | |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 918 | drm_output_set_cursor(output); |
| 919 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 920 | /* |
| 921 | * Now, update all the sprite surfaces |
| 922 | */ |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 923 | wl_list_for_each(s, &backend->sprite_list, link) { |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 924 | uint32_t flags = 0, fb_id = 0; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 925 | drmVBlank vbl = { |
| 926 | .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT, |
| 927 | .request.sequence = 1, |
| 928 | }; |
| 929 | |
Daniel Stone | f30a18c | 2017-04-04 17:54:31 +0100 | [diff] [blame] | 930 | /* XXX: Set output much earlier, so we don't attempt to place |
| 931 | * planes on entirely the wrong output. */ |
Daniel Stone | 5bb8f58 | 2017-04-04 17:54:28 +0100 | [diff] [blame] | 932 | if ((!s->fb_current && !s->fb_pending) || |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 933 | !drm_plane_crtc_supported(output, s)) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 934 | continue; |
| 935 | |
Daniel Stone | 5bb8f58 | 2017-04-04 17:54:28 +0100 | [diff] [blame] | 936 | if (s->fb_pending && !backend->sprites_hidden) |
| 937 | fb_id = s->fb_pending->fb_id; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 938 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 939 | ret = drmModeSetPlane(backend->drm.fd, s->plane_id, |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 940 | output->crtc_id, fb_id, flags, |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 941 | s->dest_x, s->dest_y, |
| 942 | s->dest_w, s->dest_h, |
| 943 | s->src_x, s->src_y, |
| 944 | s->src_w, s->src_h); |
| 945 | if (ret) |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 946 | weston_log("setplane failed: %d: %s\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 947 | ret, strerror(errno)); |
| 948 | |
Mario Kleiner | 2ab4f4e | 2015-06-21 21:25:13 +0200 | [diff] [blame] | 949 | vbl.request.type |= drm_waitvblank_pipe(output); |
Rob Clark | 5ca1a47 | 2012-08-08 20:27:37 -0500 | [diff] [blame] | 950 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 951 | /* |
| 952 | * Queue a vblank signal so we know when the surface |
| 953 | * becomes active on the display or has been replaced. |
| 954 | */ |
| 955 | vbl.request.signal = (unsigned long)s; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 956 | ret = drmWaitVBlank(backend->drm.fd, &vbl); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 957 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 958 | weston_log("vblank event request failed: %d: %s\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 959 | ret, strerror(errno)); |
| 960 | } |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 961 | |
| 962 | s->output = output; |
Daniel Stone | f30a18c | 2017-04-04 17:54:31 +0100 | [diff] [blame] | 963 | s->fb_last = s->fb_current; |
| 964 | s->fb_current = s->fb_pending; |
| 965 | s->fb_pending = NULL; |
Daniel Stone | 65d87d0 | 2017-04-04 17:54:32 +0100 | [diff] [blame] | 966 | output->vblank_pending++; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 967 | } |
| 968 | |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 969 | return 0; |
| 970 | |
| 971 | err_pageflip: |
Kristian Høgsberg | b3955b0 | 2014-01-23 16:25:06 -0800 | [diff] [blame] | 972 | output->cursor_view = NULL; |
Daniel Stone | 5bb8f58 | 2017-04-04 17:54:28 +0100 | [diff] [blame] | 973 | if (output->fb_pending) { |
| 974 | drm_fb_unref(output->fb_pending); |
| 975 | output->fb_pending = NULL; |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | return -1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | static void |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 982 | drm_output_start_repaint_loop(struct weston_output *output_base) |
| 983 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 984 | struct drm_output *output = to_drm_output(output_base); |
| 985 | struct drm_backend *backend = |
| 986 | to_drm_backend(output_base->compositor); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 987 | uint32_t fb_id; |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 988 | struct timespec ts, tnow; |
| 989 | struct timespec vbl2now; |
| 990 | int64_t refresh_nsec; |
| 991 | int ret; |
| 992 | drmVBlank vbl = { |
| 993 | .request.type = DRM_VBLANK_RELATIVE, |
| 994 | .request.sequence = 0, |
| 995 | .request.signal = 0, |
| 996 | }; |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 997 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 998 | if (output->disable_pending || output->destroy_pending) |
Xiong Zhang | abd5d47 | 2013-10-11 14:43:07 +0800 | [diff] [blame] | 999 | return; |
| 1000 | |
Daniel Stone | 5bb8f58 | 2017-04-04 17:54:28 +0100 | [diff] [blame] | 1001 | if (!output->fb_current) { |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 1002 | /* We can't page flip if there's no mode set */ |
David Herrmann | 3c688c5 | 2013-10-22 17:11:25 +0200 | [diff] [blame] | 1003 | goto finish_frame; |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 1004 | } |
| 1005 | |
Pekka Paalanen | 6b65d8f | 2017-07-27 13:44:32 +0300 | [diff] [blame] | 1006 | /* Need to smash all state in from scratch; current timings might not |
| 1007 | * be what we want, page flip might not work, etc. |
| 1008 | */ |
| 1009 | if (output->state_invalid) |
| 1010 | goto finish_frame; |
| 1011 | |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 1012 | /* Try to get current msc and timestamp via instant query */ |
| 1013 | vbl.request.type |= drm_waitvblank_pipe(output); |
| 1014 | ret = drmWaitVBlank(backend->drm.fd, &vbl); |
| 1015 | |
| 1016 | /* Error ret or zero timestamp means failure to get valid timestamp */ |
| 1017 | if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) { |
| 1018 | ts.tv_sec = vbl.reply.tval_sec; |
| 1019 | ts.tv_nsec = vbl.reply.tval_usec * 1000; |
| 1020 | |
| 1021 | /* Valid timestamp for most recent vblank - not stale? |
| 1022 | * Stale ts could happen on Linux 3.17+, so make sure it |
| 1023 | * is not older than 1 refresh duration since now. |
| 1024 | */ |
| 1025 | weston_compositor_read_presentation_clock(backend->compositor, |
| 1026 | &tnow); |
| 1027 | timespec_sub(&vbl2now, &tnow, &ts); |
| 1028 | refresh_nsec = |
| 1029 | millihz_to_nsec(output->base.current_mode->refresh); |
| 1030 | if (timespec_to_nsec(&vbl2now) < refresh_nsec) { |
| 1031 | drm_output_update_msc(output, vbl.reply.sequence); |
| 1032 | weston_output_finish_frame(output_base, &ts, |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 1033 | WP_PRESENTATION_FEEDBACK_INVALID); |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 1034 | return; |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | /* Immediate query didn't provide valid timestamp. |
| 1039 | * Use pageflip fallback. |
| 1040 | */ |
Daniel Stone | 5bb8f58 | 2017-04-04 17:54:28 +0100 | [diff] [blame] | 1041 | fb_id = output->fb_current->fb_id; |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 1042 | |
Daniel Stone | 205c0a0 | 2017-04-04 17:54:33 +0100 | [diff] [blame] | 1043 | assert(!output->page_flip_pending); |
| 1044 | assert(!output->fb_last); |
| 1045 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1046 | if (drmModePageFlip(backend->drm.fd, output->crtc_id, fb_id, |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 1047 | DRM_MODE_PAGE_FLIP_EVENT, output) < 0) { |
| 1048 | weston_log("queueing pageflip failed: %m\n"); |
David Herrmann | 3c688c5 | 2013-10-22 17:11:25 +0200 | [diff] [blame] | 1049 | goto finish_frame; |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 1050 | } |
David Herrmann | 3c688c5 | 2013-10-22 17:11:25 +0200 | [diff] [blame] | 1051 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 1052 | if (output->pageflip_timer) |
| 1053 | wl_event_source_timer_update(output->pageflip_timer, |
| 1054 | backend->pageflip_timeout); |
| 1055 | |
Daniel Stone | 205c0a0 | 2017-04-04 17:54:33 +0100 | [diff] [blame] | 1056 | output->fb_last = drm_fb_ref(output->fb_current); |
| 1057 | output->page_flip_pending = 1; |
| 1058 | |
David Herrmann | 3c688c5 | 2013-10-22 17:11:25 +0200 | [diff] [blame] | 1059 | return; |
| 1060 | |
| 1061 | finish_frame: |
| 1062 | /* if we cannot page-flip, immediately finish frame */ |
Daniel Stone | 3615ce1 | 2017-03-01 11:34:05 +0000 | [diff] [blame] | 1063 | weston_output_finish_frame(output_base, NULL, |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 1064 | WP_PRESENTATION_FEEDBACK_INVALID); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | static void |
Pekka Paalanen | 641307c | 2014-09-23 22:08:47 -0400 | [diff] [blame] | 1068 | drm_output_update_msc(struct drm_output *output, unsigned int seq) |
| 1069 | { |
| 1070 | uint64_t msc_hi = output->base.msc >> 32; |
| 1071 | |
| 1072 | if (seq < (output->base.msc & 0xffffffff)) |
| 1073 | msc_hi++; |
| 1074 | |
| 1075 | output->base.msc = (msc_hi << 32) + seq; |
| 1076 | } |
| 1077 | |
| 1078 | static void |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1079 | vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec, |
| 1080 | void *data) |
| 1081 | { |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 1082 | struct drm_plane *s = (struct drm_plane *)data; |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 1083 | struct drm_output *output = s->output; |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 1084 | struct timespec ts; |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 1085 | uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION | |
| 1086 | WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK; |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 1087 | |
Pekka Paalanen | 641307c | 2014-09-23 22:08:47 -0400 | [diff] [blame] | 1088 | drm_output_update_msc(output, frame); |
Daniel Stone | 65d87d0 | 2017-04-04 17:54:32 +0100 | [diff] [blame] | 1089 | output->vblank_pending--; |
| 1090 | assert(output->vblank_pending >= 0); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1091 | |
Daniel Stone | f30a18c | 2017-04-04 17:54:31 +0100 | [diff] [blame] | 1092 | assert(s->fb_last || s->fb_current); |
| 1093 | drm_fb_unref(s->fb_last); |
| 1094 | s->fb_last = NULL; |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 1095 | |
Daniel Stone | 65d87d0 | 2017-04-04 17:54:32 +0100 | [diff] [blame] | 1096 | if (!output->page_flip_pending && !output->vblank_pending) { |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 1097 | /* Stop the pageflip timer instead of rearming it here */ |
| 1098 | if (output->pageflip_timer) |
| 1099 | wl_event_source_timer_update(output->pageflip_timer, 0); |
| 1100 | |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 1101 | ts.tv_sec = sec; |
| 1102 | ts.tv_nsec = usec * 1000; |
Pekka Paalanen | 363aa7b | 2014-12-17 16:20:40 +0200 | [diff] [blame] | 1103 | weston_output_finish_frame(&output->base, &ts, flags); |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 1104 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | static void |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 1108 | drm_output_destroy(struct weston_output *base); |
Xiong Zhang | abd5d47 | 2013-10-11 14:43:07 +0800 | [diff] [blame] | 1109 | |
| 1110 | static void |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1111 | page_flip_handler(int fd, unsigned int frame, |
| 1112 | unsigned int sec, unsigned int usec, void *data) |
| 1113 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1114 | struct drm_output *output = data; |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 1115 | struct timespec ts; |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 1116 | uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC | |
| 1117 | WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION | |
| 1118 | WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1119 | |
Pekka Paalanen | 641307c | 2014-09-23 22:08:47 -0400 | [diff] [blame] | 1120 | drm_output_update_msc(output, frame); |
| 1121 | |
Daniel Stone | 205c0a0 | 2017-04-04 17:54:33 +0100 | [diff] [blame] | 1122 | assert(output->page_flip_pending); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 1123 | output->page_flip_pending = 0; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1124 | |
Daniel Stone | 205c0a0 | 2017-04-04 17:54:33 +0100 | [diff] [blame] | 1125 | drm_fb_unref(output->fb_last); |
| 1126 | output->fb_last = NULL; |
| 1127 | |
Xiong Zhang | abd5d47 | 2013-10-11 14:43:07 +0800 | [diff] [blame] | 1128 | if (output->destroy_pending) |
| 1129 | drm_output_destroy(&output->base); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 1130 | else if (output->disable_pending) |
| 1131 | weston_output_disable(&output->base); |
Xiong Zhang | abd5d47 | 2013-10-11 14:43:07 +0800 | [diff] [blame] | 1132 | else if (!output->vblank_pending) { |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 1133 | /* Stop the pageflip timer instead of rearming it here */ |
| 1134 | if (output->pageflip_timer) |
| 1135 | wl_event_source_timer_update(output->pageflip_timer, 0); |
| 1136 | |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 1137 | ts.tv_sec = sec; |
| 1138 | ts.tv_nsec = usec * 1000; |
Pekka Paalanen | 363aa7b | 2014-12-17 16:20:40 +0200 | [diff] [blame] | 1139 | weston_output_finish_frame(&output->base, &ts, flags); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 1140 | |
| 1141 | /* We can't call this from frame_notify, because the output's |
| 1142 | * repaint needed flag is cleared just after that */ |
| 1143 | if (output->recorder) |
| 1144 | weston_output_schedule_repaint(&output->base); |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 1145 | } |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 1146 | } |
| 1147 | |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 1148 | static uint32_t |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 1149 | drm_output_check_plane_format(struct drm_plane *p, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1150 | struct weston_view *ev, struct gbm_bo *bo) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1151 | { |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 1152 | uint32_t i, format; |
| 1153 | |
| 1154 | format = gbm_bo_get_format(bo); |
| 1155 | |
| 1156 | if (format == GBM_FORMAT_ARGB8888) { |
| 1157 | pixman_region32_t r; |
| 1158 | |
Kristian Høgsberg | 63093a3 | 2013-03-01 14:29:16 -0500 | [diff] [blame] | 1159 | pixman_region32_init_rect(&r, 0, 0, |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 1160 | ev->surface->width, |
| 1161 | ev->surface->height); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1162 | pixman_region32_subtract(&r, &r, &ev->surface->opaque); |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 1163 | |
| 1164 | if (!pixman_region32_not_empty(&r)) |
| 1165 | format = GBM_FORMAT_XRGB8888; |
| 1166 | |
| 1167 | pixman_region32_fini(&r); |
| 1168 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1169 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 1170 | for (i = 0; i < p->count_formats; i++) |
| 1171 | if (p->formats[i] == format) |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 1172 | return format; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1173 | |
| 1174 | return 0; |
| 1175 | } |
| 1176 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1177 | static struct weston_plane * |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 1178 | drm_output_prepare_overlay_view(struct drm_output *output, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1179 | struct weston_view *ev) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1180 | { |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 1181 | struct weston_compositor *ec = output->base.compositor; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1182 | struct drm_backend *b = to_drm_backend(ec); |
Pekka Paalanen | 952b6c8 | 2014-03-14 14:38:15 +0200 | [diff] [blame] | 1183 | struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport; |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 1184 | struct wl_resource *buffer_resource; |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 1185 | struct drm_plane *p; |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 1186 | struct linux_dmabuf_buffer *dmabuf; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1187 | int found = 0; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1188 | struct gbm_bo *bo; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1189 | pixman_region32_t dest_rect, src_rect; |
Ander Conselvan de Oliveira | 409eebf | 2012-12-05 15:14:04 +0200 | [diff] [blame] | 1190 | pixman_box32_t *box, tbox; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1191 | uint32_t format; |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 1192 | wl_fixed_t sx1, sy1, sx2, sy2; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1193 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1194 | if (b->sprites_are_broken) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1195 | return NULL; |
Kristian Høgsberg | 65bec24 | 2012-03-05 19:57:35 -0500 | [diff] [blame] | 1196 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1197 | /* Don't import buffers which span multiple outputs. */ |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 1198 | if (ev->output_mask != (1u << output->base.id)) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1199 | return NULL; |
Ander Conselvan de Oliveira | d450b19 | 2012-06-26 17:09:12 +0300 | [diff] [blame] | 1200 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1201 | /* We can only import GBM buffers. */ |
| 1202 | if (b->gbm == NULL) |
| 1203 | return NULL; |
| 1204 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1205 | if (ev->surface->buffer_ref.buffer == NULL) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1206 | return NULL; |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 1207 | buffer_resource = ev->surface->buffer_ref.buffer->resource; |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 1208 | if (wl_shm_buffer_get(buffer_resource)) |
Rob Clark | 702ffae | 2012-08-09 14:18:27 -0500 | [diff] [blame] | 1209 | return NULL; |
| 1210 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1211 | if (viewport->buffer.transform != output->base.transform) |
| 1212 | return NULL; |
| 1213 | if (viewport->buffer.scale != output->base.current_scale) |
| 1214 | return NULL; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1215 | if (!drm_view_transform_supported(ev)) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1216 | return NULL; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1217 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1218 | if (ev->alpha != 1.0f) |
| 1219 | return NULL; |
| 1220 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 1221 | wl_list_for_each(p, &b->sprite_list, link) { |
| 1222 | if (!drm_plane_crtc_supported(output, p)) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1223 | continue; |
| 1224 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 1225 | if (!p->fb_pending) { |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1226 | found = 1; |
| 1227 | break; |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | /* No sprites available */ |
| 1232 | if (!found) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1233 | return NULL; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1234 | |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 1235 | if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) { |
Bryce Harrington | a358207 | 2015-08-14 12:23:13 -0700 | [diff] [blame] | 1236 | #ifdef HAVE_GBM_FD_IMPORT |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 1237 | /* XXX: TODO: |
| 1238 | * |
| 1239 | * Use AddFB2 directly, do not go via GBM. |
| 1240 | * Add support for multiplanar formats. |
| 1241 | * Both require refactoring in the DRM-backend to |
| 1242 | * support a mix of gbm_bos and drmfbs. |
| 1243 | */ |
| 1244 | struct gbm_import_fd_data gbm_dmabuf = { |
Emmanuel Gil Peyrot | c399692 | 2015-11-24 19:28:24 +0000 | [diff] [blame] | 1245 | .fd = dmabuf->attributes.fd[0], |
| 1246 | .width = dmabuf->attributes.width, |
| 1247 | .height = dmabuf->attributes.height, |
| 1248 | .stride = dmabuf->attributes.stride[0], |
| 1249 | .format = dmabuf->attributes.format |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 1250 | }; |
| 1251 | |
Micah Fedke | c889012 | 2017-02-01 15:28:23 -0500 | [diff] [blame] | 1252 | /* XXX: TODO: |
| 1253 | * |
| 1254 | * Currently the buffer is rejected if any dmabuf attribute |
| 1255 | * flag is set. This keeps us from passing an inverted / |
| 1256 | * interlaced / bottom-first buffer (or any other type that may |
| 1257 | * be added in the future) through to an overlay. Ultimately, |
| 1258 | * these types of buffers should be handled through buffer |
| 1259 | * transforms and not as spot-checks requiring specific |
| 1260 | * knowledge. */ |
| 1261 | if (dmabuf->attributes.n_planes != 1 || |
| 1262 | dmabuf->attributes.offset[0] != 0 || |
| 1263 | dmabuf->attributes.flags) |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 1264 | return NULL; |
| 1265 | |
| 1266 | bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf, |
| 1267 | GBM_BO_USE_SCANOUT); |
Bryce Harrington | a358207 | 2015-08-14 12:23:13 -0700 | [diff] [blame] | 1268 | #else |
| 1269 | return NULL; |
| 1270 | #endif |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 1271 | } else { |
| 1272 | bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER, |
| 1273 | buffer_resource, GBM_BO_USE_SCANOUT); |
| 1274 | } |
Kristian Høgsberg | 2763a2e | 2012-07-13 22:54:43 -0400 | [diff] [blame] | 1275 | if (!bo) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1276 | return NULL; |
Kristian Høgsberg | 2763a2e | 2012-07-13 22:54:43 -0400 | [diff] [blame] | 1277 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 1278 | format = drm_output_check_plane_format(p, ev, bo); |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 1279 | if (format == 0) { |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1280 | gbm_bo_destroy(bo); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1281 | return NULL; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1282 | } |
| 1283 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 1284 | p->fb_pending = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT); |
| 1285 | if (!p->fb_pending) { |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1286 | gbm_bo_destroy(bo); |
| 1287 | return NULL; |
| 1288 | } |
| 1289 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 1290 | drm_fb_set_buffer(p->fb_pending, ev->surface->buffer_ref.buffer); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1291 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1292 | box = pixman_region32_extents(&ev->transform.boundingbox); |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 1293 | p->base.x = box->x1; |
| 1294 | p->base.y = box->y1; |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1295 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1296 | /* |
| 1297 | * Calculate the source & dest rects properly based on actual |
Derek Foreman | 4b1a0a1 | 2014-09-10 15:37:33 -0500 | [diff] [blame] | 1298 | * position (note the caller has called weston_view_update_transform() |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1299 | * for us already). |
| 1300 | */ |
| 1301 | pixman_region32_init(&dest_rect); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1302 | pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox, |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 1303 | &output->base.region); |
| 1304 | pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1305 | box = pixman_region32_extents(&dest_rect); |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 1306 | tbox = weston_transformed_rect(output->base.width, |
| 1307 | output->base.height, |
| 1308 | output->base.transform, |
| 1309 | output->base.current_scale, |
Alexander Larsson | d9a7bb7 | 2013-05-22 14:41:39 +0200 | [diff] [blame] | 1310 | *box); |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 1311 | p->dest_x = tbox.x1; |
| 1312 | p->dest_y = tbox.y1; |
| 1313 | p->dest_w = tbox.x2 - tbox.x1; |
| 1314 | p->dest_h = tbox.y2 - tbox.y1; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1315 | pixman_region32_fini(&dest_rect); |
| 1316 | |
| 1317 | pixman_region32_init(&src_rect); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1318 | pixman_region32_intersect(&src_rect, &ev->transform.boundingbox, |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 1319 | &output->base.region); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1320 | box = pixman_region32_extents(&src_rect); |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 1321 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1322 | weston_view_from_global_fixed(ev, |
| 1323 | wl_fixed_from_int(box->x1), |
| 1324 | wl_fixed_from_int(box->y1), |
| 1325 | &sx1, &sy1); |
| 1326 | weston_view_from_global_fixed(ev, |
| 1327 | wl_fixed_from_int(box->x2), |
| 1328 | wl_fixed_from_int(box->y2), |
| 1329 | &sx2, &sy2); |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 1330 | |
| 1331 | if (sx1 < 0) |
| 1332 | sx1 = 0; |
| 1333 | if (sy1 < 0) |
| 1334 | sy1 = 0; |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 1335 | if (sx2 > wl_fixed_from_int(ev->surface->width)) |
| 1336 | sx2 = wl_fixed_from_int(ev->surface->width); |
| 1337 | if (sy2 > wl_fixed_from_int(ev->surface->height)) |
| 1338 | sy2 = wl_fixed_from_int(ev->surface->height); |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 1339 | |
Ander Conselvan de Oliveira | 409eebf | 2012-12-05 15:14:04 +0200 | [diff] [blame] | 1340 | tbox.x1 = sx1; |
| 1341 | tbox.y1 = sy1; |
| 1342 | tbox.x2 = sx2; |
| 1343 | tbox.y2 = sy2; |
| 1344 | |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 1345 | tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width), |
| 1346 | wl_fixed_from_int(ev->surface->height), |
Pekka Paalanen | 952b6c8 | 2014-03-14 14:38:15 +0200 | [diff] [blame] | 1347 | viewport->buffer.transform, |
| 1348 | viewport->buffer.scale, |
Pekka Paalanen | 1fd9c0f | 2013-11-26 18:19:41 +0100 | [diff] [blame] | 1349 | tbox); |
Ander Conselvan de Oliveira | 409eebf | 2012-12-05 15:14:04 +0200 | [diff] [blame] | 1350 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 1351 | p->src_x = tbox.x1 << 8; |
| 1352 | p->src_y = tbox.y1 << 8; |
| 1353 | p->src_w = (tbox.x2 - tbox.x1) << 8; |
| 1354 | p->src_h = (tbox.y2 - tbox.y1) << 8; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1355 | pixman_region32_fini(&src_rect); |
| 1356 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 1357 | return &p->base; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1358 | } |
| 1359 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1360 | static struct weston_plane * |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 1361 | drm_output_prepare_cursor_view(struct drm_output *output, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1362 | struct weston_view *ev) |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 1363 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1364 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Neil Roberts | f37f82c | 2014-05-01 18:00:41 +0100 | [diff] [blame] | 1365 | struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport; |
Derek Foreman | 6c19b69 | 2015-12-03 14:07:12 -0600 | [diff] [blame] | 1366 | struct wl_shm_buffer *shmbuf; |
Daniel Stone | a7cba1d | 2017-04-04 17:54:21 +0100 | [diff] [blame] | 1367 | float x, y; |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 1368 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1369 | if (b->cursors_are_broken) |
Kristian Høgsberg | 8a01580 | 2012-08-09 17:19:23 -0400 | [diff] [blame] | 1370 | return NULL; |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1371 | |
| 1372 | if (output->cursor_view) |
Pekka Paalanen | 5580f22 | 2015-02-17 16:33:18 +0200 | [diff] [blame] | 1373 | return NULL; |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1374 | |
| 1375 | /* Don't import buffers which span multiple outputs. */ |
| 1376 | if (ev->output_mask != (1u << output->base.id)) |
| 1377 | return NULL; |
| 1378 | |
| 1379 | /* We use GBM to import SHM buffers. */ |
| 1380 | if (b->gbm == NULL) |
| 1381 | return NULL; |
| 1382 | |
Derek Foreman | 6c19b69 | 2015-12-03 14:07:12 -0600 | [diff] [blame] | 1383 | if (ev->surface->buffer_ref.buffer == NULL) |
| 1384 | return NULL; |
| 1385 | shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource); |
| 1386 | if (!shmbuf) |
| 1387 | return NULL; |
| 1388 | if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888) |
| 1389 | return NULL; |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1390 | |
| 1391 | if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL) |
| 1392 | return NULL; |
| 1393 | if (ev->transform.enabled && |
| 1394 | (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE)) |
| 1395 | return NULL; |
| 1396 | if (viewport->buffer.scale != output->base.current_scale) |
| 1397 | return NULL; |
| 1398 | if (ev->geometry.scissor_enabled) |
| 1399 | return NULL; |
| 1400 | |
Derek Foreman | 6c19b69 | 2015-12-03 14:07:12 -0600 | [diff] [blame] | 1401 | if (ev->surface->width > b->cursor_width || |
Daniel Stone | 70d337d | 2015-06-16 18:42:23 +0100 | [diff] [blame] | 1402 | ev->surface->height > b->cursor_height) |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 1403 | return NULL; |
| 1404 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1405 | output->cursor_view = ev; |
Daniel Stone | a7cba1d | 2017-04-04 17:54:21 +0100 | [diff] [blame] | 1406 | weston_view_to_global_float(ev, 0, 0, &x, &y); |
| 1407 | output->cursor_plane.x = x; |
| 1408 | output->cursor_plane.y = y; |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 1409 | |
| 1410 | return &output->cursor_plane; |
| 1411 | } |
| 1412 | |
Pekka Paalanen | d0ead48 | 2014-06-16 12:05:40 +0300 | [diff] [blame] | 1413 | /** |
| 1414 | * Update the image for the current cursor surface |
| 1415 | * |
| 1416 | * @param b DRM backend structure |
| 1417 | * @param bo GBM buffer object to write into |
| 1418 | * @param ev View to use for cursor image |
| 1419 | */ |
| 1420 | static void |
| 1421 | cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo, |
| 1422 | struct weston_view *ev) |
| 1423 | { |
| 1424 | struct weston_buffer *buffer = ev->surface->buffer_ref.buffer; |
| 1425 | uint32_t buf[b->cursor_width * b->cursor_height]; |
| 1426 | int32_t stride; |
| 1427 | uint8_t *s; |
| 1428 | int i; |
| 1429 | |
| 1430 | assert(buffer && buffer->shm_buffer); |
| 1431 | assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource)); |
| 1432 | assert(ev->surface->width <= b->cursor_width); |
| 1433 | assert(ev->surface->height <= b->cursor_height); |
| 1434 | |
| 1435 | memset(buf, 0, sizeof buf); |
| 1436 | stride = wl_shm_buffer_get_stride(buffer->shm_buffer); |
| 1437 | s = wl_shm_buffer_get_data(buffer->shm_buffer); |
| 1438 | |
| 1439 | wl_shm_buffer_begin_access(buffer->shm_buffer); |
| 1440 | for (i = 0; i < ev->surface->height; i++) |
| 1441 | memcpy(buf + i * b->cursor_width, |
| 1442 | s + i * stride, |
| 1443 | ev->surface->width * 4); |
| 1444 | wl_shm_buffer_end_access(buffer->shm_buffer); |
| 1445 | |
| 1446 | if (gbm_bo_write(bo, buf, sizeof buf) < 0) |
| 1447 | weston_log("failed update cursor: %m\n"); |
| 1448 | } |
| 1449 | |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 1450 | static void |
| 1451 | drm_output_set_cursor(struct drm_output *output) |
| 1452 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1453 | struct weston_view *ev = output->cursor_view; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1454 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Pekka Paalanen | d0ead48 | 2014-06-16 12:05:40 +0300 | [diff] [blame] | 1455 | EGLint handle; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 1456 | struct gbm_bo *bo; |
Derek Foreman | be428b3 | 2015-11-24 11:39:38 -0600 | [diff] [blame] | 1457 | float x, y; |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 1458 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1459 | if (ev == NULL) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1460 | drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0); |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 1461 | return; |
| 1462 | } |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 1463 | |
Daniel Stone | a7cba1d | 2017-04-04 17:54:21 +0100 | [diff] [blame] | 1464 | if (pixman_region32_not_empty(&output->cursor_plane.damage)) { |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 1465 | pixman_region32_fini(&output->cursor_plane.damage); |
| 1466 | pixman_region32_init(&output->cursor_plane.damage); |
Kristian Høgsberg | 1f5de35 | 2012-07-18 12:09:58 -0400 | [diff] [blame] | 1467 | output->current_cursor ^= 1; |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 1468 | bo = output->gbm_cursor_fb[output->current_cursor]->bo; |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 1469 | |
Pekka Paalanen | d0ead48 | 2014-06-16 12:05:40 +0300 | [diff] [blame] | 1470 | cursor_bo_update(b, bo, ev); |
Kristian Høgsberg | 1f5de35 | 2012-07-18 12:09:58 -0400 | [diff] [blame] | 1471 | handle = gbm_bo_get_handle(bo).s32; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1472 | if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle, |
| 1473 | b->cursor_width, b->cursor_height)) { |
Pekka Paalanen | ae29da2 | 2012-08-06 14:57:05 +0300 | [diff] [blame] | 1474 | weston_log("failed to set cursor: %m\n"); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1475 | b->cursors_are_broken = 1; |
Rob Clark | ab5b1e3 | 2012-08-09 13:24:45 -0500 | [diff] [blame] | 1476 | } |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 1477 | } |
| 1478 | |
Daniel Stone | a7cba1d | 2017-04-04 17:54:21 +0100 | [diff] [blame] | 1479 | x = (output->cursor_plane.x - output->base.x) * |
| 1480 | output->base.current_scale; |
| 1481 | y = (output->cursor_plane.y - output->base.y) * |
| 1482 | output->base.current_scale; |
Pekka Paalanen | 7eaed40 | 2015-11-27 14:20:58 +0200 | [diff] [blame] | 1483 | |
Daniel Stone | a7cba1d | 2017-04-04 17:54:21 +0100 | [diff] [blame] | 1484 | if (drmModeMoveCursor(b->drm.fd, output->crtc_id, x, y)) { |
| 1485 | weston_log("failed to move cursor: %m\n"); |
| 1486 | b->cursors_are_broken = 1; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 1487 | } |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 1488 | } |
| 1489 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1490 | static void |
Daniel Stone | b1f166d | 2017-03-01 11:34:10 +0000 | [diff] [blame] | 1491 | drm_assign_planes(struct weston_output *output_base, void *repaint_data) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1492 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1493 | struct drm_backend *b = to_drm_backend(output_base->compositor); |
| 1494 | struct drm_output *output = to_drm_output(output_base); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1495 | struct weston_view *ev, *next; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1496 | pixman_region32_t overlap, surface_overlap; |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1497 | struct weston_plane *primary, *next_plane; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1498 | |
| 1499 | /* |
| 1500 | * Find a surface for each sprite in the output using some heuristics: |
| 1501 | * 1) size |
| 1502 | * 2) frequency of update |
| 1503 | * 3) opacity (though some hw might support alpha blending) |
| 1504 | * 4) clipping (this can be fixed with color keys) |
| 1505 | * |
| 1506 | * The idea is to save on blitting since this should save power. |
| 1507 | * If we can get a large video surface on the sprite for example, |
| 1508 | * the main display surface may not need to update at all, and |
| 1509 | * the client buffer can be used directly for the sprite surface |
| 1510 | * as we do for flipping full screen surfaces. |
| 1511 | */ |
| 1512 | pixman_region32_init(&overlap); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1513 | primary = &output_base->compositor->primary_plane; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1514 | |
Daniel Stone | a7cba1d | 2017-04-04 17:54:21 +0100 | [diff] [blame] | 1515 | output->cursor_view = NULL; |
| 1516 | output->cursor_plane.x = INT32_MIN; |
| 1517 | output->cursor_plane.y = INT32_MIN; |
| 1518 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1519 | wl_list_for_each_safe(ev, next, &output_base->compositor->view_list, link) { |
Ander Conselvan de Oliveira | 895b1fd | 2013-11-19 15:22:05 +0200 | [diff] [blame] | 1520 | struct weston_surface *es = ev->surface; |
| 1521 | |
| 1522 | /* Test whether this buffer can ever go into a plane: |
| 1523 | * non-shm, or small enough to be a cursor. |
| 1524 | * |
| 1525 | * Also, keep a reference when using the pixman renderer. |
| 1526 | * That makes it possible to do a seamless switch to the GL |
| 1527 | * renderer and since the pixman renderer keeps a reference |
| 1528 | * to the buffer anyway, there is no side effects. |
Pekka Paalanen | ccfeae2 | 2012-12-04 15:58:14 +0200 | [diff] [blame] | 1529 | */ |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1530 | if (b->use_pixman || |
Ander Conselvan de Oliveira | 895b1fd | 2013-11-19 15:22:05 +0200 | [diff] [blame] | 1531 | (es->buffer_ref.buffer && |
| 1532 | (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) || |
Derek Foreman | 8743047 | 2015-10-15 10:24:48 -0500 | [diff] [blame] | 1533 | (ev->surface->width <= b->cursor_width && |
| 1534 | ev->surface->height <= b->cursor_height)))) |
Derek Foreman | 0fd6d4e | 2014-10-10 09:36:45 -0500 | [diff] [blame] | 1535 | es->keep_buffer = true; |
Ander Conselvan de Oliveira | 895b1fd | 2013-11-19 15:22:05 +0200 | [diff] [blame] | 1536 | else |
Derek Foreman | 0fd6d4e | 2014-10-10 09:36:45 -0500 | [diff] [blame] | 1537 | es->keep_buffer = false; |
Pekka Paalanen | ccfeae2 | 2012-12-04 15:58:14 +0200 | [diff] [blame] | 1538 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1539 | pixman_region32_init(&surface_overlap); |
| 1540 | pixman_region32_intersect(&surface_overlap, &overlap, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1541 | &ev->transform.boundingbox); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1542 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1543 | next_plane = NULL; |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 1544 | if (pixman_region32_not_empty(&surface_overlap)) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1545 | next_plane = primary; |
| 1546 | if (next_plane == NULL) |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1547 | next_plane = drm_output_prepare_cursor_view(output, ev); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1548 | if (next_plane == NULL) |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1549 | next_plane = drm_output_prepare_scanout_view(output, ev); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1550 | if (next_plane == NULL) |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1551 | next_plane = drm_output_prepare_overlay_view(output, ev); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1552 | if (next_plane == NULL) |
| 1553 | next_plane = primary; |
Pekka Paalanen | bf0e031 | 2014-12-17 16:20:41 +0200 | [diff] [blame] | 1554 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1555 | weston_view_move_to_plane(ev, next_plane); |
Pekka Paalanen | bf0e031 | 2014-12-17 16:20:41 +0200 | [diff] [blame] | 1556 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1557 | if (next_plane == primary) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1558 | pixman_region32_union(&overlap, &overlap, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1559 | &ev->transform.boundingbox); |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 1560 | |
Pekka Paalanen | bf0e031 | 2014-12-17 16:20:41 +0200 | [diff] [blame] | 1561 | if (next_plane == primary || |
| 1562 | next_plane == &output->cursor_plane) { |
| 1563 | /* cursor plane involves a copy */ |
| 1564 | ev->psf_flags = 0; |
| 1565 | } else { |
| 1566 | /* All other planes are a direct scanout of a |
| 1567 | * single client buffer. |
| 1568 | */ |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 1569 | ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY; |
Pekka Paalanen | bf0e031 | 2014-12-17 16:20:41 +0200 | [diff] [blame] | 1570 | } |
| 1571 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1572 | pixman_region32_fini(&surface_overlap); |
| 1573 | } |
| 1574 | pixman_region32_fini(&overlap); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1575 | } |
| 1576 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 1577 | /** |
| 1578 | * Find the closest-matching mode for a given target |
| 1579 | * |
| 1580 | * Given a target mode, find the most suitable mode amongst the output's |
| 1581 | * current mode list to use, preferring the current mode if possible, to |
| 1582 | * avoid an expensive mode switch. |
| 1583 | * |
| 1584 | * @param output DRM output |
| 1585 | * @param target_mode Mode to attempt to match |
| 1586 | * @returns Pointer to a mode from the output's mode list |
| 1587 | */ |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1588 | static struct drm_mode * |
| 1589 | choose_mode (struct drm_output *output, struct weston_mode *target_mode) |
| 1590 | { |
| 1591 | struct drm_mode *tmp_mode = NULL, *mode; |
| 1592 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 1593 | if (output->base.current_mode->width == target_mode->width && |
| 1594 | output->base.current_mode->height == target_mode->height && |
| 1595 | (output->base.current_mode->refresh == target_mode->refresh || |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1596 | target_mode->refresh == 0)) |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 1597 | return (struct drm_mode *)output->base.current_mode; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1598 | |
| 1599 | wl_list_for_each(mode, &output->base.mode_list, base.link) { |
| 1600 | if (mode->mode_info.hdisplay == target_mode->width && |
| 1601 | mode->mode_info.vdisplay == target_mode->height) { |
Mario Kleiner | 872797c | 2015-06-21 21:25:09 +0200 | [diff] [blame] | 1602 | if (mode->base.refresh == target_mode->refresh || |
| 1603 | target_mode->refresh == 0) { |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1604 | return mode; |
Daniel Stone | f556ebe | 2015-05-21 08:28:58 +0100 | [diff] [blame] | 1605 | } else if (!tmp_mode) |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1606 | tmp_mode = mode; |
| 1607 | } |
| 1608 | } |
| 1609 | |
| 1610 | return tmp_mode; |
| 1611 | } |
| 1612 | |
| 1613 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1614 | drm_output_init_egl(struct drm_output *output, struct drm_backend *b); |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 1615 | static void |
| 1616 | drm_output_fini_egl(struct drm_output *output); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1617 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1618 | drm_output_init_pixman(struct drm_output *output, struct drm_backend *b); |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 1619 | static void |
| 1620 | drm_output_fini_pixman(struct drm_output *output); |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 1621 | |
| 1622 | static int |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1623 | drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode) |
| 1624 | { |
| 1625 | struct drm_output *output; |
| 1626 | struct drm_mode *drm_mode; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1627 | struct drm_backend *b; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1628 | |
| 1629 | if (output_base == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1630 | weston_log("output is NULL.\n"); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1631 | return -1; |
| 1632 | } |
| 1633 | |
| 1634 | if (mode == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1635 | weston_log("mode is NULL.\n"); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1636 | return -1; |
| 1637 | } |
| 1638 | |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1639 | b = to_drm_backend(output_base->compositor); |
| 1640 | output = to_drm_output(output_base); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1641 | drm_mode = choose_mode (output, mode); |
| 1642 | |
| 1643 | if (!drm_mode) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1644 | weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1645 | return -1; |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 1646 | } |
| 1647 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 1648 | if (&drm_mode->base == output->base.current_mode) |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1649 | return 0; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1650 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 1651 | output->base.current_mode->flags = 0; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1652 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 1653 | output->base.current_mode = &drm_mode->base; |
| 1654 | output->base.current_mode->flags = |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1655 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 1656 | |
Daniel Stone | f30a18c | 2017-04-04 17:54:31 +0100 | [diff] [blame] | 1657 | /* XXX: This drops our current buffer too early, before we've started |
| 1658 | * displaying it. Ideally this should be much more atomic and |
| 1659 | * integrated with a full repaint cycle, rather than doing a |
| 1660 | * sledgehammer modeswitch first, and only later showing new |
| 1661 | * content. |
| 1662 | */ |
Daniel Stone | 5bb8f58 | 2017-04-04 17:54:28 +0100 | [diff] [blame] | 1663 | drm_fb_unref(output->fb_current); |
Daniel Stone | f30a18c | 2017-04-04 17:54:31 +0100 | [diff] [blame] | 1664 | assert(!output->fb_last); |
| 1665 | assert(!output->fb_pending); |
| 1666 | output->fb_last = output->fb_current = NULL; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1667 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1668 | if (b->use_pixman) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1669 | drm_output_fini_pixman(output); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1670 | if (drm_output_init_pixman(output, b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1671 | weston_log("failed to init output pixman state with " |
| 1672 | "new mode\n"); |
| 1673 | return -1; |
| 1674 | } |
| 1675 | } else { |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 1676 | drm_output_fini_egl(output); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1677 | if (drm_output_init_egl(output, b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1678 | weston_log("failed to init output egl state with " |
| 1679 | "new mode"); |
| 1680 | return -1; |
| 1681 | } |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 1682 | } |
| 1683 | |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1684 | return 0; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1685 | } |
| 1686 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 1687 | static int |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1688 | on_drm_input(int fd, uint32_t mask, void *data) |
| 1689 | { |
| 1690 | drmEventContext evctx; |
| 1691 | |
| 1692 | memset(&evctx, 0, sizeof evctx); |
Emil Velikov | 863e66b | 2017-04-04 18:07:34 +0100 | [diff] [blame] | 1693 | evctx.version = 2; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1694 | evctx.page_flip_handler = page_flip_handler; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1695 | evctx.vblank_handler = vblank_handler; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1696 | drmHandleEvent(fd, &evctx); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 1697 | |
| 1698 | return 1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1699 | } |
| 1700 | |
| 1701 | static int |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 1702 | init_kms_caps(struct drm_backend *b) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1703 | { |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 1704 | uint64_t cap; |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 1705 | int ret; |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 1706 | clockid_t clk_id; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1707 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 1708 | weston_log("using %s\n", b->drm.filename); |
Kristian Høgsberg | b71302e | 2012-05-10 12:28:35 -0400 | [diff] [blame] | 1709 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 1710 | ret = drmGetCap(b->drm.fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap); |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 1711 | if (ret == 0 && cap == 1) |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 1712 | clk_id = CLOCK_MONOTONIC; |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 1713 | else |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 1714 | clk_id = CLOCK_REALTIME; |
| 1715 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1716 | if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) { |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 1717 | weston_log("Error: failed to set presentation clock %d.\n", |
| 1718 | clk_id); |
| 1719 | return -1; |
| 1720 | } |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 1721 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 1722 | ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_WIDTH, &cap); |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 1723 | if (ret == 0) |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1724 | b->cursor_width = cap; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 1725 | else |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1726 | b->cursor_width = 64; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 1727 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 1728 | ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_HEIGHT, &cap); |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 1729 | if (ret == 0) |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1730 | b->cursor_height = cap; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 1731 | else |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1732 | b->cursor_height = 64; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 1733 | |
Ander Conselvan de Oliveira | 2292917 | 2013-01-25 15:13:02 +0200 | [diff] [blame] | 1734 | return 0; |
| 1735 | } |
| 1736 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1737 | static struct gbm_device * |
| 1738 | create_gbm_device(int fd) |
Ander Conselvan de Oliveira | 2292917 | 2013-01-25 15:13:02 +0200 | [diff] [blame] | 1739 | { |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1740 | struct gbm_device *gbm; |
Alexandru DAMIAN | be0ac5b | 2013-10-02 17:51:05 +0100 | [diff] [blame] | 1741 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 1742 | gl_renderer = weston_load_module("gl-renderer.so", |
| 1743 | "gl_renderer_interface"); |
| 1744 | if (!gl_renderer) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1745 | return NULL; |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 1746 | |
| 1747 | /* GBM will load a dri driver, but even though they need symbols from |
| 1748 | * libglapi, in some version of Mesa they are not linked to it. Since |
| 1749 | * only the gl-renderer module links to it, the call above won't make |
| 1750 | * these symbols globally available, and loading the DRI driver fails. |
| 1751 | * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */ |
| 1752 | dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL); |
| 1753 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1754 | gbm = gbm_create_device(fd); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1755 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1756 | return gbm; |
| 1757 | } |
| 1758 | |
Bryce Harrington | c056a98 | 2015-05-19 15:25:18 -0700 | [diff] [blame] | 1759 | /* When initializing EGL, if the preferred buffer format isn't available |
Bryce Harrington | b993998 | 2016-04-15 20:28:26 -0700 | [diff] [blame] | 1760 | * 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] | 1761 | * |
| 1762 | * This returns 0 if substitution isn't possible, but 0 might be a |
| 1763 | * legitimate format for other EGL platforms, so the caller is |
| 1764 | * responsible for checking for 0 before calling gl_renderer->create(). |
| 1765 | * |
| 1766 | * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689 |
| 1767 | * but it's entirely possible we'll see this again on other implementations. |
| 1768 | */ |
| 1769 | static int |
| 1770 | fallback_format_for(uint32_t format) |
| 1771 | { |
| 1772 | switch (format) { |
| 1773 | case GBM_FORMAT_XRGB8888: |
| 1774 | return GBM_FORMAT_ARGB8888; |
| 1775 | case GBM_FORMAT_XRGB2101010: |
| 1776 | return GBM_FORMAT_ARGB2101010; |
| 1777 | default: |
| 1778 | return 0; |
| 1779 | } |
| 1780 | } |
| 1781 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1782 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1783 | drm_backend_create_gl_renderer(struct drm_backend *b) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1784 | { |
Derek Foreman | 6d55637 | 2015-11-04 14:47:33 -0600 | [diff] [blame] | 1785 | EGLint format[3] = { |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 1786 | b->gbm_format, |
| 1787 | fallback_format_for(b->gbm_format), |
Derek Foreman | 6d55637 | 2015-11-04 14:47:33 -0600 | [diff] [blame] | 1788 | 0, |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 1789 | }; |
Derek Foreman | 6d55637 | 2015-11-04 14:47:33 -0600 | [diff] [blame] | 1790 | int n_formats = 2; |
John Kåre Alsaker | ef591aa | 2013-03-02 12:27:39 +0100 | [diff] [blame] | 1791 | |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 1792 | if (format[1]) |
Derek Foreman | 6d55637 | 2015-11-04 14:47:33 -0600 | [diff] [blame] | 1793 | n_formats = 3; |
Miguel A. Vico | dddc670 | 2016-05-18 17:41:07 +0200 | [diff] [blame] | 1794 | if (gl_renderer->display_create(b->compositor, |
| 1795 | EGL_PLATFORM_GBM_KHR, |
| 1796 | (void *)b->gbm, |
Miguel A. Vico | 41700e3 | 2016-05-18 17:47:59 +0200 | [diff] [blame] | 1797 | NULL, |
Miguel A. Vico | dddc670 | 2016-05-18 17:41:07 +0200 | [diff] [blame] | 1798 | gl_renderer->opaque_attribs, |
| 1799 | format, |
| 1800 | n_formats) < 0) { |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1801 | return -1; |
| 1802 | } |
| 1803 | |
| 1804 | return 0; |
| 1805 | } |
| 1806 | |
| 1807 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1808 | init_egl(struct drm_backend *b) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1809 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1810 | b->gbm = create_gbm_device(b->drm.fd); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1811 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1812 | if (!b->gbm) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1813 | return -1; |
| 1814 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1815 | if (drm_backend_create_gl_renderer(b) < 0) { |
| 1816 | gbm_device_destroy(b->gbm); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1817 | return -1; |
| 1818 | } |
| 1819 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1820 | return 0; |
| 1821 | } |
| 1822 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1823 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1824 | init_pixman(struct drm_backend *b) |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1825 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1826 | return pixman_renderer_init(b->compositor); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1827 | } |
| 1828 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 1829 | /** |
| 1830 | * Add a mode to output's mode list |
| 1831 | * |
| 1832 | * Copy the supplied DRM mode into a Weston mode structure, and add it to the |
| 1833 | * output's mode list. |
| 1834 | * |
| 1835 | * @param output DRM output to add mode to |
| 1836 | * @param info DRM mode structure to add |
| 1837 | * @returns Newly-allocated Weston/DRM mode structure |
| 1838 | */ |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 1839 | static struct drm_mode * |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 1840 | drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info) |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1841 | { |
| 1842 | struct drm_mode *mode; |
Kristian Høgsberg | c4621b0 | 2012-05-10 12:23:53 -0400 | [diff] [blame] | 1843 | uint64_t refresh; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1844 | |
| 1845 | mode = malloc(sizeof *mode); |
| 1846 | if (mode == NULL) |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 1847 | return NULL; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1848 | |
| 1849 | mode->base.flags = 0; |
Alexander Larsson | 0b13506 | 2013-05-28 16:23:36 +0200 | [diff] [blame] | 1850 | mode->base.width = info->hdisplay; |
| 1851 | mode->base.height = info->vdisplay; |
Kristian Høgsberg | c4621b0 | 2012-05-10 12:23:53 -0400 | [diff] [blame] | 1852 | |
| 1853 | /* Calculate higher precision (mHz) refresh rate */ |
| 1854 | refresh = (info->clock * 1000000LL / info->htotal + |
| 1855 | info->vtotal / 2) / info->vtotal; |
| 1856 | |
| 1857 | if (info->flags & DRM_MODE_FLAG_INTERLACE) |
| 1858 | refresh *= 2; |
| 1859 | if (info->flags & DRM_MODE_FLAG_DBLSCAN) |
| 1860 | refresh /= 2; |
| 1861 | if (info->vscan > 1) |
| 1862 | refresh /= info->vscan; |
| 1863 | |
| 1864 | mode->base.refresh = refresh; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1865 | mode->mode_info = *info; |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1866 | |
| 1867 | if (info->type & DRM_MODE_TYPE_PREFERRED) |
| 1868 | mode->base.flags |= WL_OUTPUT_MODE_PREFERRED; |
| 1869 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1870 | wl_list_insert(output->base.mode_list.prev, &mode->base.link); |
| 1871 | |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 1872 | return mode; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1873 | } |
| 1874 | |
| 1875 | static int |
| 1876 | drm_subpixel_to_wayland(int drm_value) |
| 1877 | { |
| 1878 | switch (drm_value) { |
| 1879 | default: |
| 1880 | case DRM_MODE_SUBPIXEL_UNKNOWN: |
| 1881 | return WL_OUTPUT_SUBPIXEL_UNKNOWN; |
| 1882 | case DRM_MODE_SUBPIXEL_NONE: |
| 1883 | return WL_OUTPUT_SUBPIXEL_NONE; |
| 1884 | case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB: |
| 1885 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB; |
| 1886 | case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR: |
| 1887 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR; |
| 1888 | case DRM_MODE_SUBPIXEL_VERTICAL_RGB: |
| 1889 | return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB; |
| 1890 | case DRM_MODE_SUBPIXEL_VERTICAL_BGR: |
| 1891 | return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR; |
| 1892 | } |
| 1893 | } |
| 1894 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 1895 | /* returns a value between 0-255 range, where higher is brighter */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1896 | static uint32_t |
| 1897 | drm_get_backlight(struct drm_output *output) |
| 1898 | { |
| 1899 | long brightness, max_brightness, norm; |
| 1900 | |
| 1901 | brightness = backlight_get_brightness(output->backlight); |
| 1902 | max_brightness = backlight_get_max_brightness(output->backlight); |
| 1903 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 1904 | /* convert it on a scale of 0 to 255 */ |
| 1905 | norm = (brightness * 255)/(max_brightness); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1906 | |
| 1907 | return (uint32_t) norm; |
| 1908 | } |
| 1909 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 1910 | /* values accepted are between 0-255 range */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1911 | static void |
| 1912 | drm_set_backlight(struct weston_output *output_base, uint32_t value) |
| 1913 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1914 | struct drm_output *output = to_drm_output(output_base); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1915 | long max_brightness, new_brightness; |
| 1916 | |
| 1917 | if (!output->backlight) |
| 1918 | return; |
| 1919 | |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 1920 | if (value > 255) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1921 | return; |
| 1922 | |
| 1923 | max_brightness = backlight_get_max_brightness(output->backlight); |
| 1924 | |
| 1925 | /* get denormalized value */ |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 1926 | new_brightness = (value * max_brightness) / 255; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1927 | |
| 1928 | backlight_set_brightness(output->backlight, new_brightness); |
| 1929 | } |
| 1930 | |
| 1931 | static drmModePropertyPtr |
| 1932 | drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name) |
| 1933 | { |
| 1934 | drmModePropertyPtr props; |
| 1935 | int i; |
| 1936 | |
| 1937 | for (i = 0; i < connector->count_props; i++) { |
| 1938 | props = drmModeGetProperty(fd, connector->props[i]); |
| 1939 | if (!props) |
| 1940 | continue; |
| 1941 | |
| 1942 | if (!strcmp(props->name, name)) |
| 1943 | return props; |
| 1944 | |
| 1945 | drmModeFreeProperty(props); |
| 1946 | } |
| 1947 | |
| 1948 | return NULL; |
| 1949 | } |
| 1950 | |
| 1951 | static void |
| 1952 | drm_set_dpms(struct weston_output *output_base, enum dpms_enum level) |
| 1953 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1954 | struct drm_output *output = to_drm_output(output_base); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1955 | struct weston_compositor *ec = output_base->compositor; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1956 | struct drm_backend *b = to_drm_backend(ec); |
Daniel Stone | 36609c7 | 2015-06-18 07:49:02 +0100 | [diff] [blame] | 1957 | int ret; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1958 | |
Ander Conselvan de Oliveira | a0a433a | 2013-06-04 16:24:04 +0300 | [diff] [blame] | 1959 | if (!output->dpms_prop) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1960 | return; |
| 1961 | |
Daniel Stone | 36609c7 | 2015-06-18 07:49:02 +0100 | [diff] [blame] | 1962 | ret = drmModeConnectorSetProperty(b->drm.fd, output->connector_id, |
| 1963 | output->dpms_prop->prop_id, level); |
| 1964 | if (ret) { |
| 1965 | weston_log("DRM: DPMS: failed property set for %s\n", |
| 1966 | output->base.name); |
| 1967 | return; |
| 1968 | } |
| 1969 | |
| 1970 | output->dpms = level; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1971 | } |
| 1972 | |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 1973 | static const char * const connector_type_names[] = { |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 1974 | [DRM_MODE_CONNECTOR_Unknown] = "Unknown", |
| 1975 | [DRM_MODE_CONNECTOR_VGA] = "VGA", |
| 1976 | [DRM_MODE_CONNECTOR_DVII] = "DVI-I", |
| 1977 | [DRM_MODE_CONNECTOR_DVID] = "DVI-D", |
| 1978 | [DRM_MODE_CONNECTOR_DVIA] = "DVI-A", |
| 1979 | [DRM_MODE_CONNECTOR_Composite] = "Composite", |
| 1980 | [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO", |
| 1981 | [DRM_MODE_CONNECTOR_LVDS] = "LVDS", |
| 1982 | [DRM_MODE_CONNECTOR_Component] = "Component", |
| 1983 | [DRM_MODE_CONNECTOR_9PinDIN] = "DIN", |
| 1984 | [DRM_MODE_CONNECTOR_DisplayPort] = "DP", |
| 1985 | [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A", |
| 1986 | [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B", |
| 1987 | [DRM_MODE_CONNECTOR_TV] = "TV", |
| 1988 | [DRM_MODE_CONNECTOR_eDP] = "eDP", |
Pekka Paalanen | ab81f15 | 2015-08-24 14:27:07 +0300 | [diff] [blame] | 1989 | #ifdef DRM_MODE_CONNECTOR_DSI |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 1990 | [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual", |
| 1991 | [DRM_MODE_CONNECTOR_DSI] = "DSI", |
Pekka Paalanen | ab81f15 | 2015-08-24 14:27:07 +0300 | [diff] [blame] | 1992 | #endif |
Kristian Høgsberg | 2f9ed71 | 2012-07-26 17:57:15 -0400 | [diff] [blame] | 1993 | }; |
| 1994 | |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 1995 | static char * |
| 1996 | make_connector_name(const drmModeConnector *con) |
| 1997 | { |
| 1998 | char name[32]; |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 1999 | const char *type_name = NULL; |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 2000 | |
| 2001 | if (con->connector_type < ARRAY_LENGTH(connector_type_names)) |
| 2002 | type_name = connector_type_names[con->connector_type]; |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 2003 | |
| 2004 | if (!type_name) |
| 2005 | type_name = "UNNAMED"; |
| 2006 | |
| 2007 | snprintf(name, sizeof name, "%s-%d", type_name, con->connector_type_id); |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 2008 | |
| 2009 | return strdup(name); |
| 2010 | } |
| 2011 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2012 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2013 | find_crtc_for_connector(struct drm_backend *b, |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 2014 | drmModeRes *resources, drmModeConnector *connector) |
| 2015 | { |
| 2016 | drmModeEncoder *encoder; |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 2017 | int i, j; |
Daniel Stone | 75487c2 | 2017-01-16 14:33:38 +0000 | [diff] [blame] | 2018 | int ret = -1; |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 2019 | |
| 2020 | for (j = 0; j < connector->count_encoders; j++) { |
Daniel Stone | 75487c2 | 2017-01-16 14:33:38 +0000 | [diff] [blame] | 2021 | uint32_t possible_crtcs, encoder_id, crtc_id; |
| 2022 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2023 | encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]); |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 2024 | if (encoder == NULL) { |
| 2025 | weston_log("Failed to get encoder.\n"); |
Daniel Stone | 75487c2 | 2017-01-16 14:33:38 +0000 | [diff] [blame] | 2026 | continue; |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 2027 | } |
Daniel Stone | 75487c2 | 2017-01-16 14:33:38 +0000 | [diff] [blame] | 2028 | encoder_id = encoder->encoder_id; |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 2029 | possible_crtcs = encoder->possible_crtcs; |
Daniel Stone | 75487c2 | 2017-01-16 14:33:38 +0000 | [diff] [blame] | 2030 | crtc_id = encoder->crtc_id; |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 2031 | drmModeFreeEncoder(encoder); |
| 2032 | |
| 2033 | for (i = 0; i < resources->count_crtcs; i++) { |
Daniel Stone | 72c0e1b | 2017-02-09 13:49:15 +0000 | [diff] [blame] | 2034 | if (!(possible_crtcs & (1 << i))) |
| 2035 | continue; |
| 2036 | |
| 2037 | if (drm_output_find_by_crtc(b, resources->crtcs[i])) |
| 2038 | continue; |
| 2039 | |
Daniel Stone | 75487c2 | 2017-01-16 14:33:38 +0000 | [diff] [blame] | 2040 | /* Try to preserve the existing |
| 2041 | * CRTC -> encoder -> connector routing; it makes |
| 2042 | * initialisation faster, and also since we have a |
| 2043 | * very dumb picking algorithm, may preserve a better |
| 2044 | * choice. */ |
| 2045 | if (!connector->encoder_id || |
| 2046 | (encoder_id == connector->encoder_id && |
| 2047 | crtc_id == resources->crtcs[i])) |
| 2048 | return i; |
| 2049 | |
| 2050 | ret = i; |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 2051 | } |
| 2052 | } |
| 2053 | |
Daniel Stone | 75487c2 | 2017-01-16 14:33:38 +0000 | [diff] [blame] | 2054 | return ret; |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 2055 | } |
| 2056 | |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 2057 | static void drm_output_fini_cursor_egl(struct drm_output *output) |
| 2058 | { |
| 2059 | unsigned int i; |
| 2060 | |
| 2061 | for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) { |
| 2062 | drm_fb_unref(output->gbm_cursor_fb[i]); |
| 2063 | output->gbm_cursor_fb[i] = NULL; |
| 2064 | } |
| 2065 | } |
| 2066 | |
| 2067 | static int |
| 2068 | drm_output_init_cursor_egl(struct drm_output *output, struct drm_backend *b) |
| 2069 | { |
| 2070 | unsigned int i; |
| 2071 | |
| 2072 | for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) { |
| 2073 | struct gbm_bo *bo; |
| 2074 | |
| 2075 | bo = gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height, |
| 2076 | GBM_FORMAT_ARGB8888, |
| 2077 | GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE); |
| 2078 | if (!bo) |
| 2079 | goto err; |
| 2080 | |
| 2081 | output->gbm_cursor_fb[i] = |
| 2082 | drm_fb_get_from_bo(bo, b, GBM_FORMAT_ARGB8888, |
| 2083 | BUFFER_CURSOR); |
| 2084 | if (!output->gbm_cursor_fb[i]) { |
| 2085 | gbm_bo_destroy(bo); |
| 2086 | goto err; |
| 2087 | } |
| 2088 | } |
| 2089 | |
| 2090 | return 0; |
| 2091 | |
| 2092 | err: |
| 2093 | weston_log("cursor buffers unavailable, using gl cursors\n"); |
| 2094 | b->cursors_are_broken = 1; |
| 2095 | drm_output_fini_cursor_egl(output); |
| 2096 | return -1; |
| 2097 | } |
| 2098 | |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 2099 | /* Init output state that depends on gl or gbm */ |
| 2100 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2101 | 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] | 2102 | { |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 2103 | EGLint format[2] = { |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 2104 | output->gbm_format, |
| 2105 | fallback_format_for(output->gbm_format), |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 2106 | }; |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 2107 | int n_formats = 1; |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 2108 | |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 2109 | output->gbm_surface = gbm_surface_create(b->gbm, |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 2110 | output->base.current_mode->width, |
| 2111 | output->base.current_mode->height, |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 2112 | format[0], |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 2113 | GBM_BO_USE_SCANOUT | |
| 2114 | GBM_BO_USE_RENDERING); |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 2115 | if (!output->gbm_surface) { |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 2116 | weston_log("failed to create gbm surface\n"); |
| 2117 | return -1; |
| 2118 | } |
| 2119 | |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 2120 | if (format[1]) |
| 2121 | n_formats = 2; |
Miguel A. Vico | c095cde | 2016-05-18 17:43:00 +0200 | [diff] [blame] | 2122 | if (gl_renderer->output_window_create(&output->base, |
| 2123 | (EGLNativeWindowType)output->gbm_surface, |
| 2124 | output->gbm_surface, |
| 2125 | gl_renderer->opaque_attribs, |
| 2126 | format, |
| 2127 | n_formats) < 0) { |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 2128 | weston_log("failed to create gl renderer output state\n"); |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 2129 | gbm_surface_destroy(output->gbm_surface); |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 2130 | return -1; |
| 2131 | } |
| 2132 | |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 2133 | drm_output_init_cursor_egl(output, b); |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 2134 | |
| 2135 | return 0; |
| 2136 | } |
| 2137 | |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 2138 | static void |
| 2139 | drm_output_fini_egl(struct drm_output *output) |
| 2140 | { |
| 2141 | gl_renderer->output_destroy(&output->base); |
| 2142 | gbm_surface_destroy(output->gbm_surface); |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 2143 | drm_output_fini_cursor_egl(output); |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 2144 | } |
| 2145 | |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 2146 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2147 | 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] | 2148 | { |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 2149 | int w = output->base.current_mode->width; |
| 2150 | int h = output->base.current_mode->height; |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 2151 | uint32_t format = output->gbm_format; |
| 2152 | uint32_t pixman_format; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2153 | unsigned int i; |
| 2154 | |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 2155 | switch (format) { |
| 2156 | case GBM_FORMAT_XRGB8888: |
| 2157 | pixman_format = PIXMAN_x8r8g8b8; |
| 2158 | break; |
| 2159 | case GBM_FORMAT_RGB565: |
| 2160 | pixman_format = PIXMAN_r5g6b5; |
| 2161 | break; |
| 2162 | default: |
| 2163 | weston_log("Unsupported pixman format 0x%x\n", format); |
| 2164 | return -1; |
| 2165 | } |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2166 | |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 2167 | /* FIXME error checking */ |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2168 | for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) { |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 2169 | 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] | 2170 | if (!output->dumb[i]) |
| 2171 | goto err; |
| 2172 | |
| 2173 | output->image[i] = |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 2174 | pixman_image_create_bits(pixman_format, w, h, |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2175 | output->dumb[i]->map, |
| 2176 | output->dumb[i]->stride); |
| 2177 | if (!output->image[i]) |
| 2178 | goto err; |
| 2179 | } |
| 2180 | |
| 2181 | if (pixman_renderer_output_create(&output->base) < 0) |
| 2182 | goto err; |
| 2183 | |
| 2184 | pixman_region32_init_rect(&output->previous_damage, |
Alexander Larsson | 0b13506 | 2013-05-28 16:23:36 +0200 | [diff] [blame] | 2185 | 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] | 2186 | |
| 2187 | return 0; |
| 2188 | |
| 2189 | err: |
| 2190 | for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) { |
| 2191 | if (output->dumb[i]) |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 2192 | drm_fb_unref(output->dumb[i]); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2193 | if (output->image[i]) |
| 2194 | pixman_image_unref(output->image[i]); |
| 2195 | |
| 2196 | output->dumb[i] = NULL; |
| 2197 | output->image[i] = NULL; |
| 2198 | } |
| 2199 | |
| 2200 | return -1; |
| 2201 | } |
| 2202 | |
| 2203 | static void |
| 2204 | drm_output_fini_pixman(struct drm_output *output) |
| 2205 | { |
| 2206 | unsigned int i; |
| 2207 | |
| 2208 | pixman_renderer_output_destroy(&output->base); |
| 2209 | pixman_region32_fini(&output->previous_damage); |
| 2210 | |
| 2211 | for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2212 | pixman_image_unref(output->image[i]); |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 2213 | drm_fb_unref(output->dumb[i]); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2214 | output->dumb[i] = NULL; |
| 2215 | output->image[i] = NULL; |
| 2216 | } |
| 2217 | } |
| 2218 | |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 2219 | static void |
| 2220 | edid_parse_string(const uint8_t *data, char text[]) |
| 2221 | { |
| 2222 | int i; |
| 2223 | int replaced = 0; |
| 2224 | |
| 2225 | /* this is always 12 bytes, but we can't guarantee it's null |
| 2226 | * terminated or not junk. */ |
| 2227 | strncpy(text, (const char *) data, 12); |
| 2228 | |
Bryce Harrington | 9c7de16 | 2015-08-28 13:04:26 -0700 | [diff] [blame] | 2229 | /* guarantee our new string is null-terminated */ |
| 2230 | text[12] = '\0'; |
| 2231 | |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 2232 | /* remove insane chars */ |
| 2233 | for (i = 0; text[i] != '\0'; i++) { |
| 2234 | if (text[i] == '\n' || |
| 2235 | text[i] == '\r') { |
| 2236 | text[i] = '\0'; |
| 2237 | break; |
| 2238 | } |
| 2239 | } |
| 2240 | |
| 2241 | /* ensure string is printable */ |
| 2242 | for (i = 0; text[i] != '\0'; i++) { |
| 2243 | if (!isprint(text[i])) { |
| 2244 | text[i] = '-'; |
| 2245 | replaced++; |
| 2246 | } |
| 2247 | } |
| 2248 | |
| 2249 | /* if the string is random junk, ignore the string */ |
| 2250 | if (replaced > 4) |
| 2251 | text[0] = '\0'; |
| 2252 | } |
| 2253 | |
| 2254 | #define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe |
| 2255 | #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc |
| 2256 | #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff |
| 2257 | #define EDID_OFFSET_DATA_BLOCKS 0x36 |
| 2258 | #define EDID_OFFSET_LAST_BLOCK 0x6c |
| 2259 | #define EDID_OFFSET_PNPID 0x08 |
| 2260 | #define EDID_OFFSET_SERIAL 0x0c |
| 2261 | |
| 2262 | static int |
| 2263 | edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length) |
| 2264 | { |
| 2265 | int i; |
| 2266 | uint32_t serial_number; |
| 2267 | |
| 2268 | /* check header */ |
| 2269 | if (length < 128) |
| 2270 | return -1; |
| 2271 | if (data[0] != 0x00 || data[1] != 0xff) |
| 2272 | return -1; |
| 2273 | |
| 2274 | /* decode the PNP ID from three 5 bit words packed into 2 bytes |
| 2275 | * /--08--\/--09--\ |
| 2276 | * 7654321076543210 |
| 2277 | * |\---/\---/\---/ |
| 2278 | * R C1 C2 C3 */ |
| 2279 | edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1; |
| 2280 | edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1; |
| 2281 | edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1; |
| 2282 | edid->pnp_id[3] = '\0'; |
| 2283 | |
| 2284 | /* maybe there isn't a ASCII serial number descriptor, so use this instead */ |
| 2285 | serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0]; |
| 2286 | serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100; |
| 2287 | serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000; |
| 2288 | serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000; |
| 2289 | if (serial_number > 0) |
| 2290 | sprintf(edid->serial_number, "%lu", (unsigned long) serial_number); |
| 2291 | |
| 2292 | /* parse EDID data */ |
| 2293 | for (i = EDID_OFFSET_DATA_BLOCKS; |
| 2294 | i <= EDID_OFFSET_LAST_BLOCK; |
| 2295 | i += 18) { |
| 2296 | /* ignore pixel clock data */ |
| 2297 | if (data[i] != 0) |
| 2298 | continue; |
| 2299 | if (data[i+2] != 0) |
| 2300 | continue; |
| 2301 | |
| 2302 | /* any useful blocks? */ |
| 2303 | if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) { |
| 2304 | edid_parse_string(&data[i+5], |
| 2305 | edid->monitor_name); |
| 2306 | } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) { |
| 2307 | edid_parse_string(&data[i+5], |
| 2308 | edid->serial_number); |
| 2309 | } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) { |
| 2310 | edid_parse_string(&data[i+5], |
| 2311 | edid->eisa_id); |
| 2312 | } |
| 2313 | } |
| 2314 | return 0; |
| 2315 | } |
| 2316 | |
| 2317 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2318 | find_and_parse_output_edid(struct drm_backend *b, |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 2319 | struct drm_output *output, |
| 2320 | drmModeConnector *connector) |
| 2321 | { |
| 2322 | drmModePropertyBlobPtr edid_blob = NULL; |
| 2323 | drmModePropertyPtr property; |
| 2324 | int i; |
| 2325 | int rc; |
| 2326 | |
| 2327 | for (i = 0; i < connector->count_props && !edid_blob; i++) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2328 | property = drmModeGetProperty(b->drm.fd, connector->props[i]); |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 2329 | if (!property) |
| 2330 | continue; |
| 2331 | if ((property->flags & DRM_MODE_PROP_BLOB) && |
| 2332 | !strcmp(property->name, "EDID")) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2333 | edid_blob = drmModeGetPropertyBlob(b->drm.fd, |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 2334 | connector->prop_values[i]); |
| 2335 | } |
| 2336 | drmModeFreeProperty(property); |
| 2337 | } |
| 2338 | if (!edid_blob) |
| 2339 | return; |
| 2340 | |
| 2341 | rc = edid_parse(&output->edid, |
| 2342 | edid_blob->data, |
| 2343 | edid_blob->length); |
| 2344 | if (!rc) { |
| 2345 | weston_log("EDID data '%s', '%s', '%s'\n", |
| 2346 | output->edid.pnp_id, |
| 2347 | output->edid.monitor_name, |
| 2348 | output->edid.serial_number); |
| 2349 | if (output->edid.pnp_id[0] != '\0') |
| 2350 | output->base.make = output->edid.pnp_id; |
| 2351 | if (output->edid.monitor_name[0] != '\0') |
| 2352 | output->base.model = output->edid.monitor_name; |
| 2353 | if (output->edid.serial_number[0] != '\0') |
| 2354 | output->base.serial_number = output->edid.serial_number; |
| 2355 | } |
| 2356 | drmModeFreePropertyBlob(edid_blob); |
| 2357 | } |
| 2358 | |
Kristian Høgsberg | a30989a | 2013-05-23 17:23:15 -0400 | [diff] [blame] | 2359 | |
| 2360 | |
| 2361 | static int |
| 2362 | parse_modeline(const char *s, drmModeModeInfo *mode) |
| 2363 | { |
| 2364 | char hsync[16]; |
| 2365 | char vsync[16]; |
| 2366 | float fclock; |
| 2367 | |
| 2368 | mode->type = DRM_MODE_TYPE_USERDEF; |
| 2369 | mode->hskew = 0; |
| 2370 | mode->vscan = 0; |
| 2371 | mode->vrefresh = 0; |
| 2372 | mode->flags = 0; |
| 2373 | |
Rob Bradford | 307e09e | 2013-07-26 16:29:40 +0100 | [diff] [blame] | 2374 | if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s", |
Kristian Høgsberg | a30989a | 2013-05-23 17:23:15 -0400 | [diff] [blame] | 2375 | &fclock, |
| 2376 | &mode->hdisplay, |
| 2377 | &mode->hsync_start, |
| 2378 | &mode->hsync_end, |
| 2379 | &mode->htotal, |
| 2380 | &mode->vdisplay, |
| 2381 | &mode->vsync_start, |
| 2382 | &mode->vsync_end, |
| 2383 | &mode->vtotal, hsync, vsync) != 11) |
| 2384 | return -1; |
| 2385 | |
| 2386 | mode->clock = fclock * 1000; |
| 2387 | if (strcmp(hsync, "+hsync") == 0) |
| 2388 | mode->flags |= DRM_MODE_FLAG_PHSYNC; |
| 2389 | else if (strcmp(hsync, "-hsync") == 0) |
| 2390 | mode->flags |= DRM_MODE_FLAG_NHSYNC; |
| 2391 | else |
| 2392 | return -1; |
| 2393 | |
| 2394 | if (strcmp(vsync, "+vsync") == 0) |
| 2395 | mode->flags |= DRM_MODE_FLAG_PVSYNC; |
| 2396 | else if (strcmp(vsync, "-vsync") == 0) |
| 2397 | mode->flags |= DRM_MODE_FLAG_NVSYNC; |
| 2398 | else |
| 2399 | return -1; |
| 2400 | |
Emmanuel Gil Peyrot | a62138b | 2016-05-02 22:40:11 +0100 | [diff] [blame] | 2401 | snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f", |
| 2402 | mode->hdisplay, mode->vdisplay, fclock); |
| 2403 | |
Kristian Høgsberg | a30989a | 2013-05-23 17:23:15 -0400 | [diff] [blame] | 2404 | return 0; |
| 2405 | } |
| 2406 | |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 2407 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2408 | setup_output_seat_constraint(struct drm_backend *b, |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 2409 | struct weston_output *output, |
| 2410 | const char *s) |
| 2411 | { |
| 2412 | if (strcmp(s, "") != 0) { |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2413 | struct weston_pointer *pointer; |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 2414 | struct udev_seat *seat; |
| 2415 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2416 | seat = udev_seat_get_named(&b->input, s); |
Derek Foreman | 0720ea3 | 2015-07-15 13:00:35 -0500 | [diff] [blame] | 2417 | if (!seat) |
| 2418 | return; |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 2419 | |
Derek Foreman | 0720ea3 | 2015-07-15 13:00:35 -0500 | [diff] [blame] | 2420 | seat->base.output = output; |
| 2421 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2422 | pointer = weston_seat_get_pointer(&seat->base); |
| 2423 | if (pointer) |
| 2424 | weston_pointer_clamp(pointer, |
| 2425 | &pointer->x, |
| 2426 | &pointer->y); |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 2427 | } |
| 2428 | } |
| 2429 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2430 | static int |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2431 | 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] | 2432 | { |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2433 | int ret = 0; |
| 2434 | |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2435 | if (s == NULL) |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2436 | *gbm_format = default_value; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2437 | else if (strcmp(s, "xrgb8888") == 0) |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2438 | *gbm_format = GBM_FORMAT_XRGB8888; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2439 | else if (strcmp(s, "rgb565") == 0) |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2440 | *gbm_format = GBM_FORMAT_RGB565; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2441 | else if (strcmp(s, "xrgb2101010") == 0) |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2442 | *gbm_format = GBM_FORMAT_XRGB2101010; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2443 | else { |
| 2444 | weston_log("fatal: unrecognized pixel format: %s\n", s); |
| 2445 | ret = -1; |
| 2446 | } |
| 2447 | |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2448 | return ret; |
| 2449 | } |
| 2450 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2451 | /** |
| 2452 | * Choose suitable mode for an output |
| 2453 | * |
| 2454 | * Find the most suitable mode to use for initial setup (or reconfiguration on |
| 2455 | * hotplug etc) for a DRM output. |
| 2456 | * |
| 2457 | * @param output DRM output to choose mode for |
| 2458 | * @param kind Strategy and preference to use when choosing mode |
| 2459 | * @param width Desired width for this output |
| 2460 | * @param height Desired height for this output |
| 2461 | * @param current_mode Mode currently being displayed on this output |
| 2462 | * @param modeline Manually-entered mode (may be NULL) |
| 2463 | * @returns A mode from the output's mode list, or NULL if none available |
| 2464 | */ |
| 2465 | static struct drm_mode * |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2466 | drm_output_choose_initial_mode(struct drm_backend *backend, |
| 2467 | struct drm_output *output, |
| 2468 | enum weston_drm_backend_output_mode mode, |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2469 | const char *modeline, |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2470 | const drmModeModeInfo *current_mode) |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2471 | { |
| 2472 | struct drm_mode *preferred = NULL; |
| 2473 | struct drm_mode *current = NULL; |
| 2474 | struct drm_mode *configured = NULL; |
| 2475 | struct drm_mode *best = NULL; |
| 2476 | struct drm_mode *drm_mode; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2477 | drmModeModeInfo drm_modeline; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2478 | int32_t width = 0; |
| 2479 | int32_t height = 0; |
Fabien Dessenne | 2d66a7d | 2017-01-17 17:17:21 +0100 | [diff] [blame] | 2480 | uint32_t refresh = 0; |
| 2481 | int n; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2482 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2483 | if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) { |
Fabien Dessenne | 2d66a7d | 2017-01-17 17:17:21 +0100 | [diff] [blame] | 2484 | n = sscanf(modeline, "%dx%d@%d", &width, &height, &refresh); |
| 2485 | if (n != 2 && n != 3) { |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2486 | width = -1; |
| 2487 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2488 | if (parse_modeline(modeline, &drm_modeline) == 0) { |
| 2489 | configured = drm_output_add_mode(output, &drm_modeline); |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2490 | if (!configured) |
| 2491 | return NULL; |
| 2492 | } else { |
| 2493 | weston_log("Invalid modeline \"%s\" for output %s\n", |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2494 | modeline, output->base.name); |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2495 | } |
| 2496 | } |
| 2497 | } |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2498 | |
| 2499 | wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) { |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2500 | if (width == drm_mode->base.width && |
Fabien Dessenne | 2d66a7d | 2017-01-17 17:17:21 +0100 | [diff] [blame] | 2501 | height == drm_mode->base.height && |
| 2502 | (refresh == 0 || refresh == drm_mode->mode_info.vrefresh)) |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2503 | configured = drm_mode; |
| 2504 | |
comic fans | 7a5c562 | 2016-03-17 14:29:27 +0200 | [diff] [blame] | 2505 | if (memcmp(current_mode, &drm_mode->mode_info, |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2506 | sizeof *current_mode) == 0) |
| 2507 | current = drm_mode; |
| 2508 | |
| 2509 | if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED) |
| 2510 | preferred = drm_mode; |
| 2511 | |
| 2512 | best = drm_mode; |
| 2513 | } |
| 2514 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2515 | if (current == NULL && current_mode->clock != 0) { |
| 2516 | current = drm_output_add_mode(output, current_mode); |
| 2517 | if (!current) |
| 2518 | return NULL; |
| 2519 | } |
| 2520 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2521 | if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT) |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2522 | configured = current; |
| 2523 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2524 | if (configured) |
| 2525 | return configured; |
| 2526 | |
| 2527 | if (preferred) |
| 2528 | return preferred; |
| 2529 | |
| 2530 | if (current) |
| 2531 | return current; |
| 2532 | |
| 2533 | if (best) |
| 2534 | return best; |
| 2535 | |
| 2536 | weston_log("no available modes for %s\n", output->base.name); |
| 2537 | return NULL; |
| 2538 | } |
| 2539 | |
Pekka Paalanen | eee580b | 2014-06-04 16:43:06 +0300 | [diff] [blame] | 2540 | static int |
| 2541 | connector_get_current_mode(drmModeConnector *connector, int drm_fd, |
| 2542 | drmModeModeInfo *mode) |
| 2543 | { |
| 2544 | drmModeEncoder *encoder; |
| 2545 | drmModeCrtc *crtc; |
| 2546 | |
| 2547 | /* Get the current mode on the crtc that's currently driving |
| 2548 | * this connector. */ |
| 2549 | encoder = drmModeGetEncoder(drm_fd, connector->encoder_id); |
| 2550 | memset(mode, 0, sizeof *mode); |
| 2551 | if (encoder != NULL) { |
| 2552 | crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id); |
| 2553 | drmModeFreeEncoder(encoder); |
| 2554 | if (crtc == NULL) |
| 2555 | return -1; |
| 2556 | if (crtc->mode_valid) |
| 2557 | *mode = crtc->mode; |
| 2558 | drmModeFreeCrtc(crtc); |
| 2559 | } |
| 2560 | |
| 2561 | return 0; |
| 2562 | } |
| 2563 | |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2564 | static int |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2565 | drm_output_set_mode(struct weston_output *base, |
| 2566 | enum weston_drm_backend_output_mode mode, |
| 2567 | const char *modeline) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2568 | { |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2569 | struct drm_output *output = to_drm_output(base); |
| 2570 | struct drm_backend *b = to_drm_backend(base->compositor); |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2571 | |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 2572 | struct drm_mode *current; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2573 | drmModeModeInfo crtc_mode; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2574 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 2575 | output->base.make = "unknown"; |
| 2576 | output->base.model = "unknown"; |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 2577 | output->base.serial_number = "unknown"; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 2578 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2579 | if (connector_get_current_mode(output->connector, b->drm.fd, &crtc_mode) < 0) |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 2580 | return -1; |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 2581 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2582 | current = drm_output_choose_initial_mode(b, output, mode, modeline, &crtc_mode); |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2583 | if (!current) |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 2584 | return -1; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2585 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2586 | output->base.current_mode = ¤t->base; |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 2587 | output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT; |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame] | 2588 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2589 | /* Set native_ fields, so weston_output_mode_switch_to_native() works */ |
| 2590 | output->base.native_mode = output->base.current_mode; |
| 2591 | output->base.native_scale = output->base.current_scale; |
| 2592 | |
| 2593 | output->base.mm_width = output->connector->mmWidth; |
| 2594 | output->base.mm_height = output->connector->mmHeight; |
| 2595 | |
| 2596 | return 0; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2597 | } |
| 2598 | |
| 2599 | static void |
| 2600 | drm_output_set_gbm_format(struct weston_output *base, |
| 2601 | const char *gbm_format) |
| 2602 | { |
| 2603 | struct drm_output *output = to_drm_output(base); |
| 2604 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 2605 | |
| 2606 | if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1) |
| 2607 | output->gbm_format = b->gbm_format; |
| 2608 | } |
| 2609 | |
| 2610 | static void |
| 2611 | drm_output_set_seat(struct weston_output *base, |
| 2612 | const char *seat) |
| 2613 | { |
| 2614 | struct drm_output *output = to_drm_output(base); |
| 2615 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 2616 | |
| 2617 | setup_output_seat_constraint(b, &output->base, |
| 2618 | seat ? seat : ""); |
| 2619 | } |
| 2620 | |
| 2621 | static int |
| 2622 | drm_output_enable(struct weston_output *base) |
| 2623 | { |
| 2624 | struct drm_output *output = to_drm_output(base); |
| 2625 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 2626 | struct weston_mode *m; |
| 2627 | |
| 2628 | output->dpms_prop = drm_get_prop(b->drm.fd, output->connector, "DPMS"); |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 2629 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 2630 | if (b->pageflip_timeout) |
| 2631 | drm_output_pageflip_timer_create(output); |
| 2632 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2633 | if (b->use_pixman) { |
| 2634 | if (drm_output_init_pixman(output, b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2635 | weston_log("Failed to init output pixman state\n"); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2636 | goto err_free; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2637 | } |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2638 | } else if (drm_output_init_egl(output, b) < 0) { |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 2639 | weston_log("Failed to init output gl state\n"); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2640 | goto err_free; |
Kristian Høgsberg | 1d1e0a5 | 2012-10-21 13:29:26 -0400 | [diff] [blame] | 2641 | } |
Kristian Høgsberg | 8e1f77f | 2012-05-03 11:39:35 -0400 | [diff] [blame] | 2642 | |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 2643 | if (output->backlight) { |
Kristian Høgsberg | 220819f | 2013-05-23 20:29:40 -0400 | [diff] [blame] | 2644 | weston_log("Initialized backlight, device %s\n", |
| 2645 | output->backlight->path); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 2646 | output->base.set_backlight = drm_set_backlight; |
| 2647 | output->base.backlight_current = drm_get_backlight(output); |
Kristian Høgsberg | 220819f | 2013-05-23 20:29:40 -0400 | [diff] [blame] | 2648 | } else { |
| 2649 | weston_log("Failed to initialize backlight\n"); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 2650 | } |
| 2651 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2652 | output->base.start_repaint_loop = drm_output_start_repaint_loop; |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 2653 | output->base.repaint = drm_output_repaint; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2654 | output->base.assign_planes = drm_assign_planes; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 2655 | output->base.set_dpms = drm_set_dpms; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 2656 | output->base.switch_mode = drm_output_switch_mode; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 2657 | |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 2658 | output->base.gamma_size = output->original_crtc->gamma_size; |
| 2659 | output->base.set_gamma = drm_output_set_gamma; |
| 2660 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2661 | output->base.subpixel = drm_subpixel_to_wayland(output->connector->subpixel); |
| 2662 | |
| 2663 | find_and_parse_output_edid(b, output, output->connector); |
Armin Krezović | 9bcf4c5 | 2017-02-08 13:55:26 +0100 | [diff] [blame] | 2664 | if (output->connector->connector_type == DRM_MODE_CONNECTOR_LVDS || |
| 2665 | output->connector->connector_type == DRM_MODE_CONNECTOR_eDP) |
Pekka Paalanen | 0079a94 | 2017-03-24 15:46:23 +0200 | [diff] [blame] | 2666 | output->base.connection_internal = true; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2667 | |
Derek Foreman | b042756 | 2016-02-05 15:55:20 -0600 | [diff] [blame] | 2668 | weston_plane_init(&output->cursor_plane, b->compositor, |
| 2669 | INT32_MIN, INT32_MIN); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2670 | weston_plane_init(&output->fb_plane, b->compositor, 0, 0); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 2671 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2672 | weston_compositor_stack_plane(b->compositor, &output->cursor_plane, NULL); |
| 2673 | weston_compositor_stack_plane(b->compositor, &output->fb_plane, |
| 2674 | &b->compositor->primary_plane); |
Ander Conselvan de Oliveira | 8ad1982 | 2013-03-05 17:30:27 +0200 | [diff] [blame] | 2675 | |
Kristian Høgsberg | 2f9ed71 | 2012-07-26 17:57:15 -0400 | [diff] [blame] | 2676 | weston_log("Output %s, (connector %d, crtc %d)\n", |
Richard Hughes | afe690c | 2013-05-02 10:10:04 +0100 | [diff] [blame] | 2677 | output->base.name, output->connector_id, output->crtc_id); |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 2678 | wl_list_for_each(m, &output->base.mode_list, link) |
U. Artie Eoff | d3ed6cb | 2014-01-10 10:15:17 -0800 | [diff] [blame] | 2679 | weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n", |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 2680 | m->width, m->height, m->refresh / 1000.0, |
| 2681 | m->flags & WL_OUTPUT_MODE_PREFERRED ? |
| 2682 | ", preferred" : "", |
| 2683 | m->flags & WL_OUTPUT_MODE_CURRENT ? |
| 2684 | ", current" : "", |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2685 | output->connector->count_modes == 0 ? |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 2686 | ", built-in" : ""); |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 2687 | |
Pekka Paalanen | 6b65d8f | 2017-07-27 13:44:32 +0300 | [diff] [blame] | 2688 | output->state_invalid = true; |
| 2689 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2690 | return 0; |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 2691 | |
| 2692 | err_free: |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2693 | drmModeFreeProperty(output->dpms_prop); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 2694 | |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 2695 | return -1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2696 | } |
| 2697 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2698 | static void |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2699 | drm_output_deinit(struct weston_output *base) |
| 2700 | { |
| 2701 | struct drm_output *output = to_drm_output(base); |
| 2702 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 2703 | |
Daniel Stone | f30a18c | 2017-04-04 17:54:31 +0100 | [diff] [blame] | 2704 | /* output->fb_last and output->fb_pending must not be set here; |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 2705 | * destroy_pending/disable_pending exist to guarantee exactly this. */ |
Daniel Stone | f30a18c | 2017-04-04 17:54:31 +0100 | [diff] [blame] | 2706 | assert(!output->fb_last); |
Daniel Stone | 5bb8f58 | 2017-04-04 17:54:28 +0100 | [diff] [blame] | 2707 | assert(!output->fb_pending); |
| 2708 | drm_fb_unref(output->fb_current); |
| 2709 | output->fb_current = NULL; |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 2710 | |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 2711 | if (b->use_pixman) |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2712 | drm_output_fini_pixman(output); |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 2713 | else |
| 2714 | drm_output_fini_egl(output); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2715 | |
| 2716 | weston_plane_release(&output->fb_plane); |
| 2717 | weston_plane_release(&output->cursor_plane); |
| 2718 | |
| 2719 | drmModeFreeProperty(output->dpms_prop); |
| 2720 | |
| 2721 | /* Turn off hardware cursor */ |
| 2722 | drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0); |
| 2723 | } |
| 2724 | |
| 2725 | static void |
| 2726 | drm_output_destroy(struct weston_output *base) |
| 2727 | { |
| 2728 | struct drm_output *output = to_drm_output(base); |
| 2729 | struct drm_backend *b = to_drm_backend(base->compositor); |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 2730 | struct drm_mode *drm_mode, *next; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2731 | drmModeCrtcPtr origcrtc = output->original_crtc; |
| 2732 | |
| 2733 | if (output->page_flip_pending) { |
| 2734 | output->destroy_pending = 1; |
| 2735 | weston_log("destroy output while page flip pending\n"); |
| 2736 | return; |
| 2737 | } |
| 2738 | |
| 2739 | if (output->base.enabled) |
| 2740 | drm_output_deinit(&output->base); |
| 2741 | |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 2742 | wl_list_for_each_safe(drm_mode, next, &output->base.mode_list, |
| 2743 | base.link) { |
| 2744 | wl_list_remove(&drm_mode->base.link); |
| 2745 | free(drm_mode); |
| 2746 | } |
| 2747 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2748 | if (origcrtc) { |
| 2749 | /* Restore original CRTC state */ |
| 2750 | drmModeSetCrtc(b->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id, |
| 2751 | origcrtc->x, origcrtc->y, |
| 2752 | &output->connector_id, 1, &origcrtc->mode); |
| 2753 | drmModeFreeCrtc(origcrtc); |
| 2754 | } |
| 2755 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 2756 | if (output->pageflip_timer) |
| 2757 | wl_event_source_remove(output->pageflip_timer); |
| 2758 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2759 | weston_output_destroy(&output->base); |
| 2760 | |
| 2761 | drmModeFreeConnector(output->connector); |
| 2762 | |
| 2763 | if (output->backlight) |
| 2764 | backlight_destroy(output->backlight); |
| 2765 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2766 | free(output); |
| 2767 | } |
| 2768 | |
| 2769 | static int |
| 2770 | drm_output_disable(struct weston_output *base) |
| 2771 | { |
| 2772 | struct drm_output *output = to_drm_output(base); |
| 2773 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 2774 | |
| 2775 | if (output->page_flip_pending) { |
| 2776 | output->disable_pending = 1; |
| 2777 | return -1; |
| 2778 | } |
| 2779 | |
| 2780 | if (output->base.enabled) |
| 2781 | drm_output_deinit(&output->base); |
| 2782 | |
| 2783 | output->disable_pending = 0; |
| 2784 | |
| 2785 | weston_log("Disabling output %s\n", output->base.name); |
| 2786 | drmModeSetCrtc(b->drm.fd, output->crtc_id, |
| 2787 | 0, 0, 0, 0, 0, NULL); |
| 2788 | |
| 2789 | return 0; |
| 2790 | } |
| 2791 | |
| 2792 | /** |
| 2793 | * Create a Weston output structure |
| 2794 | * |
| 2795 | * Given a DRM connector, create a matching drm_output structure and add it |
| 2796 | * to Weston's output list. It also takes ownership of the connector, which |
| 2797 | * is released when output is destroyed. |
| 2798 | * |
| 2799 | * @param b Weston backend structure |
| 2800 | * @param resources DRM resources for this device |
| 2801 | * @param connector DRM connector to use for this new output |
| 2802 | * @param drm_device udev device pointer |
| 2803 | * @returns 0 on success, or -1 on failure |
| 2804 | */ |
| 2805 | static int |
| 2806 | create_output_for_connector(struct drm_backend *b, |
| 2807 | drmModeRes *resources, |
| 2808 | drmModeConnector *connector, |
| 2809 | struct udev_device *drm_device) |
| 2810 | { |
| 2811 | struct drm_output *output; |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 2812 | struct drm_mode *drm_mode; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2813 | int i; |
| 2814 | |
| 2815 | i = find_crtc_for_connector(b, resources, connector); |
| 2816 | if (i < 0) { |
| 2817 | weston_log("No usable crtc/encoder pair for connector.\n"); |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 2818 | goto err; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2819 | } |
| 2820 | |
| 2821 | output = zalloc(sizeof *output); |
| 2822 | if (output == NULL) |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 2823 | goto err; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2824 | |
| 2825 | output->connector = connector; |
| 2826 | output->crtc_id = resources->crtcs[i]; |
| 2827 | output->pipe = i; |
| 2828 | output->connector_id = connector->connector_id; |
| 2829 | |
| 2830 | output->backlight = backlight_init(drm_device, |
| 2831 | connector->connector_type); |
| 2832 | |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 2833 | output->original_crtc = drmModeGetCrtc(b->drm.fd, output->crtc_id); |
| 2834 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2835 | output->base.enable = drm_output_enable; |
| 2836 | output->base.destroy = drm_output_destroy; |
| 2837 | output->base.disable = drm_output_disable; |
| 2838 | output->base.name = make_connector_name(connector); |
| 2839 | |
| 2840 | output->destroy_pending = 0; |
| 2841 | output->disable_pending = 0; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2842 | |
Armin Krezović | 4008740 | 2016-09-30 14:11:12 +0200 | [diff] [blame] | 2843 | weston_output_init(&output->base, b->compositor); |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 2844 | |
| 2845 | wl_list_init(&output->base.mode_list); |
| 2846 | |
| 2847 | for (i = 0; i < output->connector->count_modes; i++) { |
| 2848 | drm_mode = drm_output_add_mode(output, &output->connector->modes[i]); |
| 2849 | if (!drm_mode) { |
| 2850 | drm_output_destroy(&output->base); |
| 2851 | return -1; |
| 2852 | } |
| 2853 | } |
| 2854 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2855 | weston_compositor_add_pending_output(&output->base, b->compositor); |
| 2856 | |
| 2857 | return 0; |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 2858 | |
| 2859 | err: |
| 2860 | drmModeFreeConnector(connector); |
| 2861 | |
| 2862 | return -1; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2863 | } |
| 2864 | |
| 2865 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2866 | create_sprites(struct drm_backend *b) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2867 | { |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2868 | struct drm_plane *plane; |
| 2869 | drmModePlaneRes *kplane_res; |
| 2870 | drmModePlane *kplane; |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 2871 | uint32_t i; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2872 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2873 | kplane_res = drmModeGetPlaneResources(b->drm.fd); |
| 2874 | if (!kplane_res) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2875 | weston_log("failed to get plane resources: %s\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2876 | strerror(errno)); |
| 2877 | return; |
| 2878 | } |
| 2879 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2880 | for (i = 0; i < kplane_res->count_planes; i++) { |
| 2881 | kplane = drmModeGetPlane(b->drm.fd, kplane_res->planes[i]); |
| 2882 | if (!kplane) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2883 | continue; |
| 2884 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2885 | plane = zalloc(sizeof(*plane) + ((sizeof(uint32_t)) * |
| 2886 | kplane->count_formats)); |
| 2887 | if (!plane) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2888 | weston_log("%s: out of memory\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2889 | __func__); |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2890 | drmModeFreePlane(kplane); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2891 | continue; |
| 2892 | } |
| 2893 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2894 | plane->possible_crtcs = kplane->possible_crtcs; |
| 2895 | plane->plane_id = kplane->plane_id; |
| 2896 | plane->fb_last = NULL; |
| 2897 | plane->fb_current = NULL; |
| 2898 | plane->fb_pending = NULL; |
| 2899 | plane->backend = b; |
| 2900 | plane->count_formats = kplane->count_formats; |
| 2901 | memcpy(plane->formats, kplane->formats, |
| 2902 | kplane->count_formats * sizeof(kplane->formats[0])); |
| 2903 | drmModeFreePlane(kplane); |
| 2904 | weston_plane_init(&plane->base, b->compositor, 0, 0); |
| 2905 | weston_compositor_stack_plane(b->compositor, &plane->base, |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2906 | &b->compositor->primary_plane); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2907 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2908 | wl_list_insert(&b->sprite_list, &plane->link); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2909 | } |
| 2910 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2911 | drmModeFreePlaneResources(kplane_res); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2912 | } |
| 2913 | |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 2914 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2915 | destroy_sprites(struct drm_backend *backend) |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 2916 | { |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2917 | struct drm_plane *plane, *next; |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 2918 | struct drm_output *output; |
| 2919 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2920 | output = container_of(backend->compositor->output_list.next, |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 2921 | struct drm_output, base.link); |
| 2922 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2923 | wl_list_for_each_safe(plane, next, &backend->sprite_list, link) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2924 | drmModeSetPlane(backend->drm.fd, |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2925 | plane->plane_id, |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 2926 | output->crtc_id, 0, 0, |
| 2927 | 0, 0, 0, 0, 0, 0, 0, 0); |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2928 | assert(!plane->fb_last); |
| 2929 | assert(!plane->fb_pending); |
| 2930 | drm_fb_unref(plane->fb_current); |
| 2931 | weston_plane_release(&plane->base); |
| 2932 | free(plane); |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 2933 | } |
| 2934 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2935 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2936 | static int |
Ucan, Emre (ADITG/SW1) | 9a200d7 | 2017-02-02 14:06:56 +0000 | [diff] [blame] | 2937 | create_outputs(struct drm_backend *b, struct udev_device *drm_device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2938 | { |
| 2939 | drmModeConnector *connector; |
| 2940 | drmModeRes *resources; |
| 2941 | int i; |
| 2942 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2943 | resources = drmModeGetResources(b->drm.fd); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2944 | if (!resources) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2945 | weston_log("drmModeGetResources failed\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2946 | return -1; |
| 2947 | } |
| 2948 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2949 | b->min_width = resources->min_width; |
| 2950 | b->max_width = resources->max_width; |
| 2951 | b->min_height = resources->min_height; |
| 2952 | b->max_height = resources->max_height; |
Rob Clark | 4339add | 2012-08-09 14:18:28 -0500 | [diff] [blame] | 2953 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2954 | for (i = 0; i < resources->count_connectors; i++) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2955 | connector = drmModeGetConnector(b->drm.fd, |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 2956 | resources->connectors[i]); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2957 | if (connector == NULL) |
| 2958 | continue; |
| 2959 | |
| 2960 | if (connector->connection == DRM_MODE_CONNECTED && |
Ucan, Emre (ADITG/SW1) | 9a200d7 | 2017-02-02 14:06:56 +0000 | [diff] [blame] | 2961 | (b->connector == 0 || |
| 2962 | connector->connector_id == b->connector)) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2963 | if (create_output_for_connector(b, resources, |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 2964 | connector, drm_device) < 0) |
Benjamin Franzke | 439d986 | 2011-10-07 08:20:53 +0200 | [diff] [blame] | 2965 | continue; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2966 | } else { |
| 2967 | drmModeFreeConnector(connector); |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 2968 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2969 | } |
| 2970 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2971 | if (wl_list_empty(&b->compositor->output_list) && |
| 2972 | wl_list_empty(&b->compositor->pending_output_list)) |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2973 | weston_log("No currently active connector found.\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2974 | |
| 2975 | drmModeFreeResources(resources); |
| 2976 | |
| 2977 | return 0; |
| 2978 | } |
| 2979 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2980 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2981 | update_outputs(struct drm_backend *b, struct udev_device *drm_device) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2982 | { |
| 2983 | drmModeConnector *connector; |
| 2984 | drmModeRes *resources; |
| 2985 | struct drm_output *output, *next; |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 2986 | uint32_t *connected; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2987 | int i; |
| 2988 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2989 | resources = drmModeGetResources(b->drm.fd); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2990 | if (!resources) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2991 | weston_log("drmModeGetResources failed\n"); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2992 | return; |
| 2993 | } |
| 2994 | |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 2995 | connected = calloc(resources->count_connectors, sizeof(uint32_t)); |
| 2996 | if (!connected) { |
| 2997 | drmModeFreeResources(resources); |
| 2998 | return; |
| 2999 | } |
| 3000 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3001 | /* collect new connects */ |
| 3002 | for (i = 0; i < resources->count_connectors; i++) { |
Ucan, Emre (ADITG/SW1) | 21e4944 | 2017-02-02 14:06:55 +0000 | [diff] [blame] | 3003 | uint32_t connector_id = resources->connectors[i]; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 3004 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3005 | connector = drmModeGetConnector(b->drm.fd, connector_id); |
David Herrmann | 7551cff | 2011-12-08 17:05:43 +0100 | [diff] [blame] | 3006 | if (connector == NULL) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3007 | continue; |
| 3008 | |
David Herrmann | 7551cff | 2011-12-08 17:05:43 +0100 | [diff] [blame] | 3009 | if (connector->connection != DRM_MODE_CONNECTED) { |
| 3010 | drmModeFreeConnector(connector); |
| 3011 | continue; |
| 3012 | } |
| 3013 | |
Ucan, Emre (ADITG/SW1) | 21e4944 | 2017-02-02 14:06:55 +0000 | [diff] [blame] | 3014 | if (b->connector && (b->connector != connector_id)) { |
| 3015 | drmModeFreeConnector(connector); |
| 3016 | continue; |
| 3017 | } |
| 3018 | |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 3019 | connected[i] = connector_id; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 3020 | |
Daniel Stone | c0ec759 | 2017-02-09 13:58:35 +0000 | [diff] [blame] | 3021 | if (drm_output_find_by_connector(b, connector_id)) { |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 3022 | drmModeFreeConnector(connector); |
Daniel Stone | c0ec759 | 2017-02-09 13:58:35 +0000 | [diff] [blame] | 3023 | continue; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3024 | } |
Daniel Stone | c0ec759 | 2017-02-09 13:58:35 +0000 | [diff] [blame] | 3025 | |
| 3026 | create_output_for_connector(b, resources, |
| 3027 | connector, drm_device); |
| 3028 | weston_log("connector %d connected\n", connector_id); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3029 | } |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3030 | |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 3031 | wl_list_for_each_safe(output, next, &b->compositor->output_list, |
| 3032 | base.link) { |
| 3033 | bool disconnected = true; |
| 3034 | |
| 3035 | for (i = 0; i < resources->count_connectors; i++) { |
| 3036 | if (connected[i] == output->connector_id) { |
| 3037 | disconnected = false; |
| 3038 | break; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3039 | } |
| 3040 | } |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 3041 | |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 3042 | if (!disconnected) |
| 3043 | continue; |
| 3044 | |
| 3045 | weston_log("connector %d disconnected\n", output->connector_id); |
| 3046 | drm_output_destroy(&output->base); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3047 | } |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 3048 | |
| 3049 | wl_list_for_each_safe(output, next, &b->compositor->pending_output_list, |
| 3050 | base.link) { |
| 3051 | bool disconnected = true; |
| 3052 | |
| 3053 | for (i = 0; i < resources->count_connectors; i++) { |
| 3054 | if (connected[i] == output->connector_id) { |
| 3055 | disconnected = false; |
| 3056 | break; |
| 3057 | } |
| 3058 | } |
| 3059 | |
| 3060 | if (!disconnected) |
| 3061 | continue; |
| 3062 | |
| 3063 | weston_log("connector %d disconnected\n", output->connector_id); |
| 3064 | drm_output_destroy(&output->base); |
| 3065 | } |
| 3066 | |
| 3067 | free(connected); |
| 3068 | drmModeFreeResources(resources); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3069 | } |
| 3070 | |
| 3071 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3072 | udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3073 | { |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 3074 | const char *sysnum; |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 3075 | const char *val; |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 3076 | |
| 3077 | sysnum = udev_device_get_sysnum(device); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3078 | if (!sysnum || atoi(sysnum) != b->drm.id) |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 3079 | return 0; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 3080 | |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 3081 | val = udev_device_get_property_value(device, "HOTPLUG"); |
| 3082 | if (!val) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3083 | return 0; |
| 3084 | |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 3085 | return strcmp(val, "1") == 0; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3086 | } |
| 3087 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 3088 | static int |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3089 | udev_drm_event(int fd, uint32_t mask, void *data) |
| 3090 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3091 | struct drm_backend *b = data; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3092 | struct udev_device *event; |
| 3093 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3094 | event = udev_monitor_receive_device(b->udev_monitor); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 3095 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3096 | if (udev_event_is_hotplug(b, event)) |
| 3097 | update_outputs(b, event); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3098 | |
| 3099 | udev_device_unref(event); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 3100 | |
| 3101 | return 1; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3102 | } |
| 3103 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 3104 | static void |
Kristian Høgsberg | 7b884bc | 2012-07-31 14:32:01 -0400 | [diff] [blame] | 3105 | drm_restore(struct weston_compositor *ec) |
| 3106 | { |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 3107 | weston_launcher_restore(ec->launcher); |
Kristian Høgsberg | 7b884bc | 2012-07-31 14:32:01 -0400 | [diff] [blame] | 3108 | } |
| 3109 | |
| 3110 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 3111 | drm_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 3112 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 3113 | struct drm_backend *b = to_drm_backend(ec); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 3114 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3115 | udev_input_destroy(&b->input); |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 3116 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3117 | wl_event_source_remove(b->udev_drm_source); |
| 3118 | wl_event_source_remove(b->drm_source); |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 3119 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3120 | destroy_sprites(b); |
Kristian Høgsberg | 3d64a3e | 2013-05-10 12:36:04 -0400 | [diff] [blame] | 3121 | |
Ander Conselvan de Oliveira | 6b16214 | 2013-10-25 16:26:32 +0300 | [diff] [blame] | 3122 | weston_compositor_shutdown(ec); |
| 3123 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3124 | if (b->gbm) |
| 3125 | gbm_device_destroy(b->gbm); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3126 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3127 | weston_launcher_destroy(ec->launcher); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 3128 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3129 | close(b->drm.fd); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3130 | free(b); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 3131 | } |
| 3132 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 3133 | static void |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 3134 | session_notify(struct wl_listener *listener, void *data) |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 3135 | { |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 3136 | struct weston_compositor *compositor = data; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 3137 | struct drm_backend *b = to_drm_backend(compositor); |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 3138 | struct drm_plane *sprite; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 3139 | struct drm_output *output; |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 3140 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3141 | if (compositor->session_active) { |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 3142 | weston_log("activating session\n"); |
Daniel Stone | f33e104 | 2016-11-05 08:10:13 +0000 | [diff] [blame] | 3143 | weston_compositor_wake(compositor); |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 3144 | weston_compositor_damage_all(compositor); |
Pekka Paalanen | 6b65d8f | 2017-07-27 13:44:32 +0300 | [diff] [blame] | 3145 | |
| 3146 | wl_list_for_each(output, &compositor->output_list, base.link) |
| 3147 | output->state_invalid = true; |
| 3148 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3149 | udev_input_enable(&b->input); |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 3150 | } else { |
| 3151 | weston_log("deactivating session\n"); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3152 | udev_input_disable(&b->input); |
Kristian Høgsberg | 4014a6b | 2012-04-10 00:08:45 -0400 | [diff] [blame] | 3153 | |
Philipp Brüschweiler | 57edf7f | 2013-03-29 13:01:56 +0100 | [diff] [blame] | 3154 | weston_compositor_offscreen(compositor); |
Kristian Høgsberg | d8e181b | 2011-05-06 15:38:28 -0400 | [diff] [blame] | 3155 | |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 3156 | /* If we have a repaint scheduled (either from a |
| 3157 | * pending pageflip or the idle handler), make sure we |
| 3158 | * 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] | 3159 | * vt switched away. The OFFSCREEN state will prevent |
Abdur Rehman | 4dca0e1 | 2017-01-01 19:46:35 +0500 | [diff] [blame] | 3160 | * further attempts at repainting. When we switch |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 3161 | * back, we schedule a repaint, which will process |
| 3162 | * pending frame callbacks. */ |
| 3163 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3164 | wl_list_for_each(output, &compositor->output_list, base.link) { |
Daniel Stone | 09a97e2 | 2017-03-01 11:34:06 +0000 | [diff] [blame] | 3165 | output->base.repaint_needed = false; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3166 | drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0); |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 3167 | } |
| 3168 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3169 | output = container_of(compositor->output_list.next, |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 3170 | struct drm_output, base.link); |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 3171 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3172 | wl_list_for_each(sprite, &b->sprite_list, link) |
| 3173 | drmModeSetPlane(b->drm.fd, |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 3174 | sprite->plane_id, |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 3175 | output->crtc_id, 0, 0, |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 3176 | 0, 0, 0, 0, 0, 0, 0, 0); |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 3177 | }; |
| 3178 | } |
| 3179 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3180 | /** |
| 3181 | * Determines whether or not a device is capable of modesetting. If successful, |
| 3182 | * sets b->drm.fd and b->drm.filename to the opened device. |
| 3183 | */ |
| 3184 | static bool |
| 3185 | drm_device_is_kms(struct drm_backend *b, struct udev_device *device) |
| 3186 | { |
| 3187 | const char *filename = udev_device_get_devnode(device); |
| 3188 | const char *sysnum = udev_device_get_sysnum(device); |
| 3189 | drmModeRes *res; |
| 3190 | int id, fd; |
| 3191 | |
| 3192 | if (!filename) |
| 3193 | return false; |
| 3194 | |
| 3195 | fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR); |
| 3196 | if (fd < 0) |
| 3197 | return false; |
| 3198 | |
| 3199 | res = drmModeGetResources(fd); |
| 3200 | if (!res) |
| 3201 | goto out_fd; |
| 3202 | |
| 3203 | if (res->count_crtcs <= 0 || res->count_connectors <= 0 || |
| 3204 | res->count_encoders <= 0) |
| 3205 | goto out_res; |
| 3206 | |
| 3207 | if (sysnum) |
| 3208 | id = atoi(sysnum); |
| 3209 | if (!sysnum || id < 0) { |
| 3210 | weston_log("couldn't get sysnum for device %s\n", filename); |
| 3211 | goto out_res; |
| 3212 | } |
| 3213 | |
| 3214 | /* We can be called successfully on multiple devices; if we have, |
| 3215 | * clean up old entries. */ |
| 3216 | if (b->drm.fd >= 0) |
| 3217 | weston_launcher_close(b->compositor->launcher, b->drm.fd); |
| 3218 | free(b->drm.filename); |
| 3219 | |
| 3220 | b->drm.fd = fd; |
| 3221 | b->drm.id = id; |
| 3222 | b->drm.filename = strdup(filename); |
| 3223 | |
Sergi Granell | ceb5981 | 2017-03-28 12:44:04 +0200 | [diff] [blame] | 3224 | drmModeFreeResources(res); |
| 3225 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3226 | return true; |
| 3227 | |
| 3228 | out_res: |
| 3229 | drmModeFreeResources(res); |
| 3230 | out_fd: |
| 3231 | weston_launcher_close(b->compositor->launcher, fd); |
| 3232 | return false; |
| 3233 | } |
| 3234 | |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 3235 | /* |
| 3236 | * Find primary GPU |
| 3237 | * Some systems may have multiple DRM devices attached to a single seat. This |
| 3238 | * function loops over all devices and tries to find a PCI device with the |
| 3239 | * boot_vga sysfs attribute set to 1. |
| 3240 | * 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] | 3241 | * Devices are also vetted to make sure they are are capable of modesetting, |
| 3242 | * rather than pure render nodes (GPU with no display), or pure |
| 3243 | * memory-allocation devices (VGEM). |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 3244 | */ |
| 3245 | static struct udev_device* |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3246 | find_primary_gpu(struct drm_backend *b, const char *seat) |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 3247 | { |
| 3248 | struct udev_enumerate *e; |
| 3249 | struct udev_list_entry *entry; |
| 3250 | const char *path, *device_seat, *id; |
| 3251 | struct udev_device *device, *drm_device, *pci; |
| 3252 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3253 | e = udev_enumerate_new(b->udev); |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 3254 | udev_enumerate_add_match_subsystem(e, "drm"); |
| 3255 | udev_enumerate_add_match_sysname(e, "card[0-9]*"); |
| 3256 | |
| 3257 | udev_enumerate_scan_devices(e); |
| 3258 | drm_device = NULL; |
| 3259 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3260 | bool is_boot_vga = false; |
| 3261 | |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 3262 | path = udev_list_entry_get_name(entry); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3263 | device = udev_device_new_from_syspath(b->udev, path); |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 3264 | if (!device) |
| 3265 | continue; |
| 3266 | device_seat = udev_device_get_property_value(device, "ID_SEAT"); |
| 3267 | if (!device_seat) |
| 3268 | device_seat = default_seat; |
| 3269 | if (strcmp(device_seat, seat)) { |
| 3270 | udev_device_unref(device); |
| 3271 | continue; |
| 3272 | } |
| 3273 | |
| 3274 | pci = udev_device_get_parent_with_subsystem_devtype(device, |
| 3275 | "pci", NULL); |
| 3276 | if (pci) { |
| 3277 | id = udev_device_get_sysattr_value(pci, "boot_vga"); |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3278 | if (id && !strcmp(id, "1")) |
| 3279 | is_boot_vga = true; |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 3280 | } |
| 3281 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3282 | /* If we already have a modesetting-capable device, and this |
| 3283 | * device isn't our boot-VGA device, we aren't going to use |
| 3284 | * it. */ |
| 3285 | if (!is_boot_vga && drm_device) { |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 3286 | udev_device_unref(device); |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3287 | continue; |
| 3288 | } |
| 3289 | |
| 3290 | /* Make sure this device is actually capable of modesetting; |
| 3291 | * if this call succeeds, b->drm.{fd,filename} will be set, |
| 3292 | * and any old values freed. */ |
| 3293 | if (!drm_device_is_kms(b, device)) { |
| 3294 | udev_device_unref(device); |
| 3295 | continue; |
| 3296 | } |
| 3297 | |
| 3298 | /* There can only be one boot_vga device, and we try to use it |
| 3299 | * at all costs. */ |
| 3300 | if (is_boot_vga) { |
| 3301 | if (drm_device) |
| 3302 | udev_device_unref(drm_device); |
| 3303 | drm_device = device; |
| 3304 | break; |
| 3305 | } |
| 3306 | |
| 3307 | /* Per the (!is_boot_vga && drm_device) test above, we only |
| 3308 | * trump existing saved devices with boot-VGA devices, so if |
| 3309 | * we end up here, this must be the first device we've seen. */ |
| 3310 | assert(!drm_device); |
| 3311 | drm_device = device; |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 3312 | } |
| 3313 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3314 | /* If we're returning a device to use, we must have an open FD for |
| 3315 | * it. */ |
| 3316 | assert(!!drm_device == (b->drm.fd >= 0)); |
| 3317 | |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 3318 | udev_enumerate_unref(e); |
| 3319 | return drm_device; |
| 3320 | } |
| 3321 | |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 3322 | static void |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 3323 | planes_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key, |
| 3324 | void *data) |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 3325 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3326 | struct drm_backend *b = data; |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 3327 | |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 3328 | switch (key) { |
| 3329 | case KEY_C: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3330 | b->cursors_are_broken ^= 1; |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 3331 | break; |
| 3332 | case KEY_V: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3333 | b->sprites_are_broken ^= 1; |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 3334 | break; |
| 3335 | case KEY_O: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3336 | b->sprites_hidden ^= 1; |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 3337 | break; |
| 3338 | default: |
| 3339 | break; |
| 3340 | } |
Ander Conselvan de Oliveira | 180f42a | 2012-11-21 15:11:37 +0200 | [diff] [blame] | 3341 | } |
| 3342 | |
Kristian Høgsberg | 0eac34a | 2013-08-30 14:28:22 -0700 | [diff] [blame] | 3343 | #ifdef BUILD_VAAPI_RECORDER |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3344 | static void |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 3345 | recorder_destroy(struct drm_output *output) |
| 3346 | { |
| 3347 | vaapi_recorder_destroy(output->recorder); |
| 3348 | output->recorder = NULL; |
| 3349 | |
| 3350 | output->base.disable_planes--; |
| 3351 | |
| 3352 | wl_list_remove(&output->recorder_frame_listener.link); |
| 3353 | weston_log("[libva recorder] done\n"); |
| 3354 | } |
| 3355 | |
| 3356 | static void |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3357 | recorder_frame_notify(struct wl_listener *listener, void *data) |
| 3358 | { |
| 3359 | struct drm_output *output; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3360 | struct drm_backend *b; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3361 | int fd, ret; |
| 3362 | |
| 3363 | output = container_of(listener, struct drm_output, |
| 3364 | recorder_frame_listener); |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 3365 | b = to_drm_backend(output->base.compositor); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3366 | |
| 3367 | if (!output->recorder) |
| 3368 | return; |
| 3369 | |
Daniel Stone | 5bb8f58 | 2017-04-04 17:54:28 +0100 | [diff] [blame] | 3370 | ret = drmPrimeHandleToFD(b->drm.fd, output->fb_current->handle, |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3371 | DRM_CLOEXEC, &fd); |
| 3372 | if (ret) { |
| 3373 | weston_log("[libva recorder] " |
| 3374 | "failed to create prime fd for front buffer\n"); |
| 3375 | return; |
| 3376 | } |
| 3377 | |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 3378 | ret = vaapi_recorder_frame(output->recorder, fd, |
Daniel Stone | 5bb8f58 | 2017-04-04 17:54:28 +0100 | [diff] [blame] | 3379 | output->fb_current->stride); |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 3380 | if (ret < 0) { |
| 3381 | weston_log("[libva recorder] aborted: %m\n"); |
| 3382 | recorder_destroy(output); |
| 3383 | } |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3384 | } |
| 3385 | |
| 3386 | static void * |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3387 | create_recorder(struct drm_backend *b, int width, int height, |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3388 | const char *filename) |
| 3389 | { |
| 3390 | int fd; |
| 3391 | drm_magic_t magic; |
| 3392 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3393 | fd = open(b->drm.filename, O_RDWR | O_CLOEXEC); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3394 | if (fd < 0) |
| 3395 | return NULL; |
| 3396 | |
| 3397 | drmGetMagic(fd, &magic); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3398 | drmAuthMagic(b->drm.fd, magic); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3399 | |
| 3400 | return vaapi_recorder_create(fd, width, height, filename); |
| 3401 | } |
| 3402 | |
| 3403 | static void |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 3404 | recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key, |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3405 | void *data) |
| 3406 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3407 | struct drm_backend *b = data; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3408 | struct drm_output *output; |
| 3409 | int width, height; |
| 3410 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3411 | output = container_of(b->compositor->output_list.next, |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3412 | struct drm_output, base.link); |
| 3413 | |
| 3414 | if (!output->recorder) { |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 3415 | if (output->gbm_format != GBM_FORMAT_XRGB8888) { |
Ander Conselvan de Oliveira | 2ef1cd1 | 2014-05-06 16:49:06 +0300 | [diff] [blame] | 3416 | weston_log("failed to start vaapi recorder: " |
| 3417 | "output format not supported\n"); |
| 3418 | return; |
| 3419 | } |
| 3420 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 3421 | width = output->base.current_mode->width; |
| 3422 | height = output->base.current_mode->height; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3423 | |
| 3424 | output->recorder = |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3425 | create_recorder(b, width, height, "capture.h264"); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3426 | if (!output->recorder) { |
| 3427 | weston_log("failed to create vaapi recorder\n"); |
| 3428 | return; |
| 3429 | } |
| 3430 | |
| 3431 | output->base.disable_planes++; |
| 3432 | |
| 3433 | output->recorder_frame_listener.notify = recorder_frame_notify; |
| 3434 | wl_signal_add(&output->base.frame_signal, |
| 3435 | &output->recorder_frame_listener); |
| 3436 | |
| 3437 | weston_output_schedule_repaint(&output->base); |
| 3438 | |
| 3439 | weston_log("[libva recorder] initialized\n"); |
| 3440 | } else { |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 3441 | recorder_destroy(output); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3442 | } |
| 3443 | } |
| 3444 | #else |
| 3445 | static void |
Bryce Harrington | 4a8a3a1 | 2015-07-16 19:12:26 -0700 | [diff] [blame] | 3446 | recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key, |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3447 | void *data) |
| 3448 | { |
| 3449 | weston_log("Compiled without libva support\n"); |
| 3450 | } |
| 3451 | #endif |
| 3452 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3453 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3454 | switch_to_gl_renderer(struct drm_backend *b) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3455 | { |
| 3456 | struct drm_output *output; |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 3457 | bool dmabuf_support_inited; |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3458 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3459 | if (!b->use_pixman) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3460 | return; |
| 3461 | |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 3462 | dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf; |
| 3463 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3464 | weston_log("Switching to GL renderer\n"); |
| 3465 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3466 | b->gbm = create_gbm_device(b->drm.fd); |
| 3467 | if (!b->gbm) { |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3468 | weston_log("Failed to create gbm device. " |
| 3469 | "Aborting renderer switch\n"); |
| 3470 | return; |
| 3471 | } |
| 3472 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3473 | 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] | 3474 | pixman_renderer_output_destroy(&output->base); |
| 3475 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3476 | b->compositor->renderer->destroy(b->compositor); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3477 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3478 | if (drm_backend_create_gl_renderer(b) < 0) { |
| 3479 | gbm_device_destroy(b->gbm); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3480 | weston_log("Failed to create GL renderer. Quitting.\n"); |
| 3481 | /* FIXME: we need a function to shutdown cleanly */ |
| 3482 | assert(0); |
| 3483 | } |
| 3484 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3485 | wl_list_for_each(output, &b->compositor->output_list, base.link) |
| 3486 | drm_output_init_egl(output, b); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3487 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3488 | b->use_pixman = 0; |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 3489 | |
| 3490 | if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) { |
| 3491 | if (linux_dmabuf_setup(b->compositor) < 0) |
| 3492 | weston_log("Error: initializing dmabuf " |
| 3493 | "support failed.\n"); |
| 3494 | } |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3495 | } |
| 3496 | |
| 3497 | static void |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 3498 | renderer_switch_binding(struct weston_keyboard *keyboard, uint32_t time, |
| 3499 | uint32_t key, void *data) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3500 | { |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 3501 | struct drm_backend *b = |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 3502 | to_drm_backend(keyboard->seat->compositor); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3503 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3504 | switch_to_gl_renderer(b); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3505 | } |
| 3506 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 3507 | static const struct weston_drm_output_api api = { |
| 3508 | drm_output_set_mode, |
| 3509 | drm_output_set_gbm_format, |
| 3510 | drm_output_set_seat, |
| 3511 | }; |
| 3512 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3513 | static struct drm_backend * |
| 3514 | drm_backend_create(struct weston_compositor *compositor, |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3515 | struct weston_drm_backend_config *config) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3516 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3517 | struct drm_backend *b; |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 3518 | struct udev_device *drm_device; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3519 | struct wl_event_loop *loop; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3520 | const char *seat_id = default_seat; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 3521 | int ret; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3522 | |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 3523 | weston_log("initializing drm backend\n"); |
| 3524 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3525 | b = zalloc(sizeof *b); |
| 3526 | if (b == NULL) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 3527 | return NULL; |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 3528 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3529 | b->drm.fd = -1; |
| 3530 | |
Pekka Paalanen | 6858383 | 2015-05-19 09:53:16 +0300 | [diff] [blame] | 3531 | /* |
| 3532 | * KMS support for hardware planes cannot properly synchronize |
| 3533 | * without nuclear page flip. Without nuclear/atomic, hw plane |
| 3534 | * and cursor plane updates would either tear or cause extra |
| 3535 | * waits for vblanks which means dropping the compositor framerate |
Pekka Paalanen | 3f32a13 | 2015-09-07 15:38:43 +0300 | [diff] [blame] | 3536 | * to a fraction. For cursors, it's not so bad, so they are |
| 3537 | * enabled. |
Pekka Paalanen | 6858383 | 2015-05-19 09:53:16 +0300 | [diff] [blame] | 3538 | * |
| 3539 | * These can be enabled again when nuclear/atomic support lands. |
| 3540 | */ |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3541 | b->sprites_are_broken = 1; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3542 | b->compositor = compositor; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3543 | b->use_pixman = config->use_pixman; |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 3544 | b->pageflip_timeout = config->pageflip_timeout; |
Kristian Høgsberg | 8e6f376 | 2013-10-16 16:31:42 -0700 | [diff] [blame] | 3545 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3546 | if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0) |
| 3547 | goto err_compositor; |
Kristian Høgsberg | 8e6f376 | 2013-10-16 16:31:42 -0700 | [diff] [blame] | 3548 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3549 | if (config->seat_id) |
| 3550 | seat_id = config->seat_id; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3551 | |
Rafal Mielniczuk | 32069c0 | 2013-03-27 18:39:28 +0100 | [diff] [blame] | 3552 | /* Check if we run drm-backend using weston-launch */ |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3553 | compositor->launcher = weston_launcher_connect(compositor, config->tty, |
| 3554 | seat_id, true); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3555 | if (compositor->launcher == NULL) { |
Rafal Mielniczuk | 32069c0 | 2013-03-27 18:39:28 +0100 | [diff] [blame] | 3556 | weston_log("fatal: drm backend should be run " |
Kristian Høgsberg | b76237e | 2013-04-04 21:36:20 -0400 | [diff] [blame] | 3557 | "using weston-launch binary or as root\n"); |
Rafal Mielniczuk | 32069c0 | 2013-03-27 18:39:28 +0100 | [diff] [blame] | 3558 | goto err_compositor; |
| 3559 | } |
| 3560 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3561 | b->udev = udev_new(); |
| 3562 | if (b->udev == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 3563 | weston_log("failed to initialize udev context\n"); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 3564 | goto err_launcher; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3565 | } |
| 3566 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3567 | b->session_listener.notify = session_notify; |
| 3568 | wl_signal_add(&compositor->session_signal, &b->session_listener); |
Kristian Høgsberg | c5b9ddb | 2012-01-15 14:29:09 -0500 | [diff] [blame] | 3569 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3570 | drm_device = find_primary_gpu(b, seat_id); |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 3571 | if (drm_device == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 3572 | weston_log("no drm device found\n"); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 3573 | goto err_udev; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 3574 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3575 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3576 | if (init_kms_caps(b) < 0) { |
Ander Conselvan de Oliveira | 2292917 | 2013-01-25 15:13:02 +0200 | [diff] [blame] | 3577 | weston_log("failed to initialize kms\n"); |
| 3578 | goto err_udev_dev; |
| 3579 | } |
| 3580 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3581 | if (b->use_pixman) { |
| 3582 | if (init_pixman(b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3583 | weston_log("failed to initialize pixman renderer\n"); |
| 3584 | goto err_udev_dev; |
| 3585 | } |
| 3586 | } else { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3587 | if (init_egl(b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3588 | weston_log("failed to initialize egl\n"); |
| 3589 | goto err_udev_dev; |
| 3590 | } |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 3591 | } |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 3592 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3593 | b->base.destroy = drm_destroy; |
| 3594 | b->base.restore = drm_restore; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 3595 | |
Bob Ham | 91880f1 | 2016-01-12 10:21:47 +0000 | [diff] [blame] | 3596 | weston_setup_vt_switch_bindings(compositor); |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 3597 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3598 | wl_list_init(&b->sprite_list); |
| 3599 | create_sprites(b); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3600 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3601 | if (udev_input_init(&b->input, |
Giulio Camuffo | 8aedf7b | 2016-06-02 21:48:12 +0300 | [diff] [blame] | 3602 | compositor, b->udev, seat_id, |
| 3603 | config->configure_device) < 0) { |
Ander Conselvan de Oliveira | 4ade0e4 | 2014-04-17 13:08:45 +0300 | [diff] [blame] | 3604 | weston_log("failed to create input devices\n"); |
| 3605 | goto err_sprite; |
| 3606 | } |
| 3607 | |
Ucan, Emre (ADITG/SW1) | 21e4944 | 2017-02-02 14:06:55 +0000 | [diff] [blame] | 3608 | b->connector = config->connector; |
| 3609 | |
Ucan, Emre (ADITG/SW1) | 9a200d7 | 2017-02-02 14:06:56 +0000 | [diff] [blame] | 3610 | if (create_outputs(b, drm_device) < 0) { |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3611 | weston_log("failed to create output for %s\n", b->drm.filename); |
Ander Conselvan de Oliveira | 4ade0e4 | 2014-04-17 13:08:45 +0300 | [diff] [blame] | 3612 | goto err_udev_input; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 3613 | } |
| 3614 | |
Jason Ekstrand | 9fc7151 | 2014-04-02 19:53:46 -0500 | [diff] [blame] | 3615 | /* A this point we have some idea of whether or not we have a working |
| 3616 | * cursor plane. */ |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3617 | if (!b->cursors_are_broken) |
| 3618 | compositor->capabilities |= WESTON_CAP_CURSOR_PLANE; |
Jason Ekstrand | 9fc7151 | 2014-04-02 19:53:46 -0500 | [diff] [blame] | 3619 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3620 | loop = wl_display_get_event_loop(compositor->wl_display); |
| 3621 | b->drm_source = |
| 3622 | wl_event_loop_add_fd(loop, b->drm.fd, |
| 3623 | WL_EVENT_READABLE, on_drm_input, b); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 3624 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3625 | b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev"); |
| 3626 | if (b->udev_monitor == NULL) { |
Abdur Rehman | 4dca0e1 | 2017-01-01 19:46:35 +0500 | [diff] [blame] | 3627 | weston_log("failed to initialize udev monitor\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3628 | goto err_drm_source; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3629 | } |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3630 | udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor, |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3631 | "drm", NULL); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3632 | b->udev_drm_source = |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 3633 | wl_event_loop_add_fd(loop, |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3634 | udev_monitor_get_fd(b->udev_monitor), |
| 3635 | WL_EVENT_READABLE, udev_drm_event, b); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3636 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3637 | if (udev_monitor_enable_receiving(b->udev_monitor) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 3638 | weston_log("failed to enable udev-monitor receiving\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3639 | goto err_udev_monitor; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3640 | } |
| 3641 | |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3642 | udev_device_unref(drm_device); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3643 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3644 | weston_compositor_add_debug_binding(compositor, KEY_O, |
| 3645 | planes_binding, b); |
| 3646 | weston_compositor_add_debug_binding(compositor, KEY_C, |
| 3647 | planes_binding, b); |
| 3648 | weston_compositor_add_debug_binding(compositor, KEY_V, |
| 3649 | planes_binding, b); |
| 3650 | weston_compositor_add_debug_binding(compositor, KEY_Q, |
| 3651 | recorder_binding, b); |
| 3652 | weston_compositor_add_debug_binding(compositor, KEY_W, |
| 3653 | renderer_switch_binding, b); |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 3654 | |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 3655 | if (compositor->renderer->import_dmabuf) { |
| 3656 | if (linux_dmabuf_setup(compositor) < 0) |
| 3657 | weston_log("Error: initializing dmabuf " |
| 3658 | "support failed.\n"); |
| 3659 | } |
| 3660 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3661 | compositor->backend = &b->base; |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 3662 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 3663 | ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME, |
| 3664 | &api, sizeof(api)); |
| 3665 | |
| 3666 | if (ret < 0) { |
| 3667 | weston_log("Failed to register output API.\n"); |
| 3668 | goto err_udev_monitor; |
| 3669 | } |
| 3670 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3671 | return b; |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3672 | |
| 3673 | err_udev_monitor: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3674 | wl_event_source_remove(b->udev_drm_source); |
| 3675 | udev_monitor_unref(b->udev_monitor); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3676 | err_drm_source: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3677 | wl_event_source_remove(b->drm_source); |
Ander Conselvan de Oliveira | 4ade0e4 | 2014-04-17 13:08:45 +0300 | [diff] [blame] | 3678 | err_udev_input: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3679 | udev_input_destroy(&b->input); |
Kristian Høgsberg | 2bc5e8e | 2012-09-06 20:51:00 -0400 | [diff] [blame] | 3680 | err_sprite: |
Emmanuel Gil Peyrot | b8347e3 | 2016-05-02 22:40:13 +0100 | [diff] [blame] | 3681 | if (b->gbm) |
| 3682 | gbm_device_destroy(b->gbm); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3683 | destroy_sprites(b); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3684 | err_udev_dev: |
| 3685 | udev_device_unref(drm_device); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 3686 | err_launcher: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3687 | weston_launcher_destroy(compositor->launcher); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3688 | err_udev: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3689 | udev_unref(b->udev); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3690 | err_compositor: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3691 | weston_compositor_shutdown(compositor); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3692 | free(b); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3693 | return NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3694 | } |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 3695 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3696 | static void |
| 3697 | config_init_to_defaults(struct weston_drm_backend_config *config) |
| 3698 | { |
| 3699 | } |
| 3700 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3701 | WL_EXPORT int |
Quentin Glidic | 23e1d6f | 2016-12-02 14:08:44 +0100 | [diff] [blame] | 3702 | weston_backend_init(struct weston_compositor *compositor, |
| 3703 | struct weston_backend_config *config_base) |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 3704 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3705 | struct drm_backend *b; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3706 | struct weston_drm_backend_config config = {{ 0, }}; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 3707 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3708 | if (config_base == NULL || |
| 3709 | config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION || |
| 3710 | config_base->struct_size > sizeof(struct weston_drm_backend_config)) { |
| 3711 | weston_log("drm backend config structure is invalid\n"); |
| 3712 | return -1; |
| 3713 | } |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 3714 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3715 | config_init_to_defaults(&config); |
| 3716 | memcpy(&config, config_base, config_base->struct_size); |
Kristian Høgsberg | d8e9833 | 2013-10-16 16:15:11 -0700 | [diff] [blame] | 3717 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3718 | b = drm_backend_create(compositor, &config); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3719 | if (b == NULL) |
| 3720 | return -1; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3721 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3722 | return 0; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 3723 | } |