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