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