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