Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1 | /* |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 2 | * Copyright © 2008-2011 Kristian Høgsberg |
| 3 | * Copyright © 2011 Intel Corporation |
Pekka Paalanen | 925788f | 2018-04-19 14:20:01 +0300 | [diff] [blame] | 4 | * Copyright © 2017, 2018 Collabora, Ltd. |
| 5 | * Copyright © 2017, 2018 General Electric Company |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 6 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 7 | * Permission is hereby granted, free of charge, to any person obtaining |
| 8 | * a copy of this software and associated documentation files (the |
| 9 | * "Software"), to deal in the Software without restriction, including |
| 10 | * without limitation the rights to use, copy, modify, merge, publish, |
| 11 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 12 | * permit persons to whom the Software is furnished to do so, subject to |
| 13 | * the following conditions: |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 14 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 15 | * The above copyright notice and this permission notice (including the |
| 16 | * next paragraph) shall be included in all copies or substantial |
| 17 | * portions of the Software. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 21 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 22 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 23 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 24 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 25 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 | * SOFTWARE. |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 27 | */ |
| 28 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 29 | #include "config.h" |
Kristian Høgsberg | 0b9334a | 2011-04-12 11:34:32 -0400 | [diff] [blame] | 30 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 31 | #include <errno.h> |
Jussi Kukkonen | 649bbce | 2016-07-19 14:16:27 +0300 | [diff] [blame] | 32 | #include <stdint.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 33 | #include <stdlib.h> |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 34 | #include <ctype.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 35 | #include <string.h> |
| 36 | #include <fcntl.h> |
| 37 | #include <unistd.h> |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 38 | #include <linux/input.h> |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 39 | #include <linux/vt.h> |
Ander Conselvan de Oliveira | fd1f4c6 | 2012-06-26 17:09:14 +0300 | [diff] [blame] | 40 | #include <assert.h> |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 41 | #include <sys/mman.h> |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 42 | #include <dlfcn.h> |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 43 | #include <time.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 44 | |
Benjamin Franzke | c649a92 | 2011-03-02 11:56:04 +0100 | [diff] [blame] | 45 | #include <xf86drm.h> |
| 46 | #include <xf86drmMode.h> |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 47 | #include <drm_fourcc.h> |
Benjamin Franzke | c649a92 | 2011-03-02 11:56:04 +0100 | [diff] [blame] | 48 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 49 | #include <gbm.h> |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 50 | #include <libudev.h> |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 51 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 52 | #include "compositor.h" |
| 53 | #include "compositor-drm.h" |
Jon Cruz | 35b2eaa | 2015-06-15 15:37:08 -0700 | [diff] [blame] | 54 | #include "shared/helpers.h" |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 55 | #include "shared/timespec-util.h" |
John Kåre Alsaker | 30d2b1f | 2012-11-13 19:10:28 +0100 | [diff] [blame] | 56 | #include "gl-renderer.h" |
Vincent Abriou | c950667 | 2016-10-05 16:14:07 +0200 | [diff] [blame] | 57 | #include "weston-egl-ext.h" |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 58 | #include "pixman-renderer.h" |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 59 | #include "pixel-formats.h" |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 60 | #include "libbacklight.h" |
Peter Hutterer | 823ad33 | 2014-11-26 07:06:31 +1000 | [diff] [blame] | 61 | #include "libinput-seat.h" |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 62 | #include "launcher-util.h" |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 63 | #include "vaapi-recorder.h" |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 64 | #include "presentation-time-server-protocol.h" |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 65 | #include "linux-dmabuf.h" |
Micah Fedke | c889012 | 2017-02-01 15:28:23 -0500 | [diff] [blame] | 66 | #include "linux-dmabuf-unstable-v1-server-protocol.h" |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 67 | |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 68 | #ifndef DRM_CAP_TIMESTAMP_MONOTONIC |
| 69 | #define DRM_CAP_TIMESTAMP_MONOTONIC 0x6 |
| 70 | #endif |
| 71 | |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 72 | #ifndef DRM_CLIENT_CAP_UNIVERSAL_PLANES |
| 73 | #define DRM_CLIENT_CAP_UNIVERSAL_PLANES 2 |
| 74 | #endif |
| 75 | |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 76 | #ifndef DRM_CLIENT_CAP_ASPECT_RATIO |
| 77 | #define DRM_CLIENT_CAP_ASPECT_RATIO 4 |
| 78 | #endif |
| 79 | |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 80 | #ifndef DRM_CAP_CURSOR_WIDTH |
| 81 | #define DRM_CAP_CURSOR_WIDTH 0x8 |
| 82 | #endif |
| 83 | |
| 84 | #ifndef DRM_CAP_CURSOR_HEIGHT |
| 85 | #define DRM_CAP_CURSOR_HEIGHT 0x9 |
| 86 | #endif |
| 87 | |
| 88 | #ifndef GBM_BO_USE_CURSOR |
| 89 | #define GBM_BO_USE_CURSOR GBM_BO_USE_CURSOR_64X64 |
| 90 | #endif |
| 91 | |
Pekka Paalanen | dfc0683 | 2017-11-20 14:04:38 +0200 | [diff] [blame] | 92 | #define MAX_CLONED_CONNECTORS 4 |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 93 | |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 94 | /** |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 95 | * aspect ratio info taken from the drmModeModeInfo flag bits 19-22, |
| 96 | * which should be used to fill the aspect ratio field in weston_mode. |
| 97 | */ |
| 98 | #define DRM_MODE_FLAG_PIC_AR_BITS_POS 19 |
| 99 | #ifndef DRM_MODE_FLAG_PIC_AR_MASK |
| 100 | #define DRM_MODE_FLAG_PIC_AR_MASK (0xF << DRM_MODE_FLAG_PIC_AR_BITS_POS) |
| 101 | #endif |
| 102 | |
| 103 | /** |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 104 | * Represents the values of an enum-type KMS property |
| 105 | */ |
| 106 | struct drm_property_enum_info { |
| 107 | const char *name; /**< name as string (static, not freed) */ |
| 108 | bool valid; /**< true if value is supported; ignore if false */ |
| 109 | uint64_t value; /**< raw value */ |
| 110 | }; |
| 111 | |
| 112 | /** |
| 113 | * Holds information on a DRM property, including its ID and the enum |
| 114 | * values it holds. |
| 115 | * |
| 116 | * DRM properties are allocated dynamically, and maintained as DRM objects |
| 117 | * within the normal object ID space; they thus do not have a stable ID |
| 118 | * to refer to. This includes enum values, which must be referred to by |
| 119 | * integer values, but these are not stable. |
| 120 | * |
| 121 | * drm_property_info allows a cache to be maintained where Weston can use |
| 122 | * enum values internally to refer to properties, with the mapping to DRM |
| 123 | * ID values being maintained internally. |
| 124 | */ |
| 125 | struct drm_property_info { |
| 126 | const char *name; /**< name as string (static, not freed) */ |
| 127 | uint32_t prop_id; /**< KMS property object ID */ |
| 128 | unsigned int num_enum_values; /**< number of enum values */ |
| 129 | struct drm_property_enum_info *enum_values; /**< array of enum values */ |
| 130 | }; |
| 131 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 132 | /** |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 133 | * List of properties attached to DRM planes |
| 134 | */ |
| 135 | enum wdrm_plane_property { |
| 136 | WDRM_PLANE_TYPE = 0, |
| 137 | WDRM_PLANE_SRC_X, |
| 138 | WDRM_PLANE_SRC_Y, |
| 139 | WDRM_PLANE_SRC_W, |
| 140 | WDRM_PLANE_SRC_H, |
| 141 | WDRM_PLANE_CRTC_X, |
| 142 | WDRM_PLANE_CRTC_Y, |
| 143 | WDRM_PLANE_CRTC_W, |
| 144 | WDRM_PLANE_CRTC_H, |
| 145 | WDRM_PLANE_FB_ID, |
| 146 | WDRM_PLANE_CRTC_ID, |
| 147 | WDRM_PLANE__COUNT |
| 148 | }; |
| 149 | |
| 150 | /** |
| 151 | * Possible values for the WDRM_PLANE_TYPE property. |
| 152 | */ |
| 153 | enum wdrm_plane_type { |
| 154 | WDRM_PLANE_TYPE_PRIMARY = 0, |
| 155 | WDRM_PLANE_TYPE_CURSOR, |
| 156 | WDRM_PLANE_TYPE_OVERLAY, |
| 157 | WDRM_PLANE_TYPE__COUNT |
| 158 | }; |
| 159 | |
| 160 | static struct drm_property_enum_info plane_type_enums[] = { |
| 161 | [WDRM_PLANE_TYPE_PRIMARY] = { |
| 162 | .name = "Primary", |
| 163 | }, |
| 164 | [WDRM_PLANE_TYPE_OVERLAY] = { |
| 165 | .name = "Overlay", |
| 166 | }, |
| 167 | [WDRM_PLANE_TYPE_CURSOR] = { |
| 168 | .name = "Cursor", |
| 169 | }, |
| 170 | }; |
| 171 | |
| 172 | static const struct drm_property_info plane_props[] = { |
| 173 | [WDRM_PLANE_TYPE] = { |
| 174 | .name = "type", |
| 175 | .enum_values = plane_type_enums, |
| 176 | .num_enum_values = WDRM_PLANE_TYPE__COUNT, |
| 177 | }, |
| 178 | [WDRM_PLANE_SRC_X] = { .name = "SRC_X", }, |
| 179 | [WDRM_PLANE_SRC_Y] = { .name = "SRC_Y", }, |
| 180 | [WDRM_PLANE_SRC_W] = { .name = "SRC_W", }, |
| 181 | [WDRM_PLANE_SRC_H] = { .name = "SRC_H", }, |
| 182 | [WDRM_PLANE_CRTC_X] = { .name = "CRTC_X", }, |
| 183 | [WDRM_PLANE_CRTC_Y] = { .name = "CRTC_Y", }, |
| 184 | [WDRM_PLANE_CRTC_W] = { .name = "CRTC_W", }, |
| 185 | [WDRM_PLANE_CRTC_H] = { .name = "CRTC_H", }, |
| 186 | [WDRM_PLANE_FB_ID] = { .name = "FB_ID", }, |
| 187 | [WDRM_PLANE_CRTC_ID] = { .name = "CRTC_ID", }, |
| 188 | }; |
| 189 | |
| 190 | /** |
| 191 | * List of properties attached to a DRM connector |
| 192 | */ |
| 193 | enum wdrm_connector_property { |
| 194 | WDRM_CONNECTOR_EDID = 0, |
| 195 | WDRM_CONNECTOR_DPMS, |
| 196 | WDRM_CONNECTOR_CRTC_ID, |
| 197 | WDRM_CONNECTOR__COUNT |
| 198 | }; |
| 199 | |
| 200 | static const struct drm_property_info connector_props[] = { |
| 201 | [WDRM_CONNECTOR_EDID] = { .name = "EDID" }, |
| 202 | [WDRM_CONNECTOR_DPMS] = { .name = "DPMS" }, |
| 203 | [WDRM_CONNECTOR_CRTC_ID] = { .name = "CRTC_ID", }, |
| 204 | }; |
| 205 | |
| 206 | /** |
Pekka Paalanen | cd011a6 | 2016-11-15 22:07:49 +0000 | [diff] [blame] | 207 | * List of properties attached to DRM CRTCs |
| 208 | */ |
| 209 | enum wdrm_crtc_property { |
| 210 | WDRM_CRTC_MODE_ID = 0, |
| 211 | WDRM_CRTC_ACTIVE, |
| 212 | WDRM_CRTC__COUNT |
| 213 | }; |
| 214 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 215 | static const struct drm_property_info crtc_props[] = { |
| 216 | [WDRM_CRTC_MODE_ID] = { .name = "MODE_ID", }, |
| 217 | [WDRM_CRTC_ACTIVE] = { .name = "ACTIVE", }, |
| 218 | }; |
| 219 | |
Pekka Paalanen | cd011a6 | 2016-11-15 22:07:49 +0000 | [diff] [blame] | 220 | /** |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 221 | * Mode for drm_output_state_duplicate. |
| 222 | */ |
| 223 | enum drm_output_state_duplicate_mode { |
| 224 | DRM_OUTPUT_STATE_CLEAR_PLANES, /**< reset all planes to off */ |
| 225 | DRM_OUTPUT_STATE_PRESERVE_PLANES, /**< preserve plane state */ |
| 226 | }; |
| 227 | |
| 228 | /** |
| 229 | * Mode for drm_pending_state_apply and co. |
| 230 | */ |
| 231 | enum drm_state_apply_mode { |
| 232 | DRM_STATE_APPLY_SYNC, /**< state fully processed */ |
| 233 | DRM_STATE_APPLY_ASYNC, /**< state pending event delivery */ |
| 234 | }; |
| 235 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 236 | struct drm_backend { |
| 237 | struct weston_backend base; |
| 238 | struct weston_compositor *compositor; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 239 | |
| 240 | struct udev *udev; |
| 241 | struct wl_event_source *drm_source; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 242 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 243 | struct udev_monitor *udev_monitor; |
| 244 | struct wl_event_source *udev_drm_source; |
| 245 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 246 | struct { |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 247 | int id; |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 248 | int fd; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 249 | char *filename; |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 250 | } drm; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 251 | struct gbm_device *gbm; |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 252 | struct wl_listener session_listener; |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 253 | uint32_t gbm_format; |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 254 | |
Rob Clark | 4339add | 2012-08-09 14:18:28 -0500 | [diff] [blame] | 255 | /* we need these parameters in order to not fail drmModeAddFB2() |
| 256 | * due to out of bounds dimensions, and then mistakenly set |
| 257 | * sprites_are_broken: |
| 258 | */ |
Daniel Stone | f214fdc | 2016-11-14 17:43:57 +0000 | [diff] [blame] | 259 | int min_width, max_width; |
| 260 | int min_height, max_height; |
Rob Clark | 4339add | 2012-08-09 14:18:28 -0500 | [diff] [blame] | 261 | |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 262 | struct wl_list plane_list; |
Kristian Høgsberg | 65bec24 | 2012-03-05 19:57:35 -0500 | [diff] [blame] | 263 | int sprites_are_broken; |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 264 | int sprites_hidden; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 265 | |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 266 | void *repaint_data; |
| 267 | |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 268 | bool state_invalid; |
| 269 | |
Pekka Paalanen | eacec81 | 2017-09-12 13:43:51 +0300 | [diff] [blame] | 270 | /* CRTC IDs not used by any enabled output. */ |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 271 | struct wl_array unused_crtcs; |
| 272 | |
Rob Clark | ab5b1e3 | 2012-08-09 13:24:45 -0500 | [diff] [blame] | 273 | int cursors_are_broken; |
| 274 | |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 275 | bool universal_planes; |
Pekka Paalanen | cd011a6 | 2016-11-15 22:07:49 +0000 | [diff] [blame] | 276 | bool atomic_modeset; |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 277 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 278 | int use_pixman; |
Pekka Paalanen | dee412d | 2018-04-23 11:44:58 +0200 | [diff] [blame] | 279 | bool use_pixman_shadow; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 280 | |
Rob Bradford | d355b80 | 2013-05-31 18:09:55 +0100 | [diff] [blame] | 281 | struct udev_input input; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 282 | |
Daniel Stone | 70d337d | 2015-06-16 18:42:23 +0100 | [diff] [blame] | 283 | int32_t cursor_width; |
| 284 | int32_t cursor_height; |
Ucan, Emre (ADITG/SW1) | 21e4944 | 2017-02-02 14:06:55 +0000 | [diff] [blame] | 285 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 286 | uint32_t pageflip_timeout; |
Daniel Stone | b57c6a0 | 2017-10-05 16:27:21 +0100 | [diff] [blame] | 287 | |
| 288 | bool shutting_down; |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 289 | |
| 290 | bool aspect_ratio_supported; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 291 | }; |
| 292 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 293 | struct drm_mode { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 294 | struct weston_mode base; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 295 | drmModeModeInfo mode_info; |
Daniel Stone | d5526cb | 2016-11-16 10:54:10 +0000 | [diff] [blame] | 296 | uint32_t blob_id; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 297 | }; |
| 298 | |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 299 | enum drm_fb_type { |
| 300 | BUFFER_INVALID = 0, /**< never used */ |
| 301 | BUFFER_CLIENT, /**< directly sourced from client */ |
| 302 | BUFFER_PIXMAN_DUMB, /**< internal Pixman rendering */ |
| 303 | BUFFER_GBM_SURFACE, /**< internal EGL rendering */ |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 304 | BUFFER_CURSOR, /**< internal cursor buffer */ |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 305 | }; |
| 306 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 307 | struct drm_fb { |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 308 | enum drm_fb_type type; |
| 309 | |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 310 | int refcnt; |
| 311 | |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 312 | uint32_t fb_id, stride, handle, size; |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 313 | const struct pixel_format_info *format; |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 314 | int width, height; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 315 | int fd; |
Pekka Paalanen | de685b8 | 2012-12-04 15:58:12 +0200 | [diff] [blame] | 316 | struct weston_buffer_reference buffer_ref; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 317 | |
| 318 | /* Used by gbm fbs */ |
| 319 | struct gbm_bo *bo; |
Daniel Stone | 05a5ac2 | 2017-04-04 17:54:25 +0100 | [diff] [blame] | 320 | struct gbm_surface *gbm_surface; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 321 | |
| 322 | /* Used by dumb fbs */ |
| 323 | void *map; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 324 | }; |
| 325 | |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 326 | struct drm_edid { |
| 327 | char eisa_id[13]; |
| 328 | char monitor_name[13]; |
| 329 | char pnp_id[5]; |
| 330 | char serial_number[13]; |
| 331 | }; |
| 332 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 333 | /** |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 334 | * Pending state holds one or more drm_output_state structures, collected from |
| 335 | * performing repaint. This pending state is transient, and only lives between |
| 336 | * beginning a repaint group and flushing the results: after flush, each |
| 337 | * output state will complete and be retired separately. |
| 338 | */ |
| 339 | struct drm_pending_state { |
| 340 | struct drm_backend *backend; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 341 | struct wl_list output_list; |
| 342 | }; |
| 343 | |
| 344 | /* |
| 345 | * Output state holds the dynamic state for one Weston output, i.e. a KMS CRTC, |
| 346 | * plus >= 1 each of encoder/connector/plane. Since everything but the planes |
| 347 | * is currently statically assigned per-output, we mainly use this to track |
| 348 | * plane state. |
| 349 | * |
| 350 | * pending_state is set when the output state is owned by a pending_state, |
| 351 | * i.e. when it is being constructed and has not yet been applied. When the |
| 352 | * output state has been applied, the owning pending_state is freed. |
| 353 | */ |
| 354 | struct drm_output_state { |
| 355 | struct drm_pending_state *pending_state; |
| 356 | struct drm_output *output; |
| 357 | struct wl_list link; |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 358 | enum dpms_enum dpms; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 359 | struct wl_list plane_list; |
| 360 | }; |
| 361 | |
| 362 | /** |
| 363 | * Plane state holds the dynamic state for a plane: where it is positioned, |
| 364 | * and which buffer it is currently displaying. |
| 365 | * |
| 366 | * The plane state is owned by an output state, except when setting an initial |
| 367 | * state. See drm_output_state for notes on state object lifetime. |
| 368 | */ |
| 369 | struct drm_plane_state { |
| 370 | struct drm_plane *plane; |
| 371 | struct drm_output *output; |
| 372 | struct drm_output_state *output_state; |
| 373 | |
| 374 | struct drm_fb *fb; |
| 375 | |
| 376 | int32_t src_x, src_y; |
| 377 | uint32_t src_w, src_h; |
| 378 | int32_t dest_x, dest_y; |
| 379 | uint32_t dest_w, dest_h; |
| 380 | |
| 381 | bool complete; |
| 382 | |
| 383 | struct wl_list link; /* drm_output_state::plane_list */ |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 384 | }; |
| 385 | |
| 386 | /** |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 387 | * A plane represents one buffer, positioned within a CRTC, and stacked |
| 388 | * relative to other planes on the same CRTC. |
| 389 | * |
| 390 | * Each CRTC has a 'primary plane', which use used to display the classic |
| 391 | * framebuffer contents, as accessed through the legacy drmModeSetCrtc |
| 392 | * call (which combines setting the CRTC's actual physical mode, and the |
| 393 | * properties of the primary plane). |
| 394 | * |
| 395 | * The cursor plane also has its own alternate legacy API. |
| 396 | * |
| 397 | * Other planes are used opportunistically to display content we do not |
| 398 | * wish to blit into the primary plane. These non-primary/cursor planes |
| 399 | * are referred to as 'sprites'. |
| 400 | */ |
| 401 | struct drm_plane { |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 402 | struct weston_plane base; |
| 403 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 404 | struct drm_backend *backend; |
| 405 | |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 406 | enum wdrm_plane_type type; |
| 407 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 408 | uint32_t possible_crtcs; |
| 409 | uint32_t plane_id; |
| 410 | uint32_t count_formats; |
| 411 | |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 412 | struct drm_property_info props[WDRM_PLANE__COUNT]; |
| 413 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 414 | /* The last state submitted to the kernel for this plane. */ |
| 415 | struct drm_plane_state *state_cur; |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 416 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 417 | struct wl_list link; |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 418 | |
| 419 | uint32_t formats[]; |
| 420 | }; |
| 421 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 422 | struct drm_head { |
| 423 | struct weston_head base; |
| 424 | struct drm_backend *backend; |
| 425 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 426 | drmModeConnector *connector; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 427 | uint32_t connector_id; |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 428 | struct drm_edid edid; |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 429 | |
| 430 | /* Holds the properties for the connector */ |
| 431 | struct drm_property_info props_conn[WDRM_CONNECTOR__COUNT]; |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 432 | |
| 433 | struct backlight *backlight; |
Pekka Paalanen | 13d233e | 2017-09-11 14:06:11 +0300 | [diff] [blame] | 434 | |
| 435 | drmModeModeInfo inherited_mode; /**< Original mode on the connector */ |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 436 | uint32_t inherited_crtc_id; /**< Original CRTC assignment */ |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 437 | }; |
| 438 | |
| 439 | struct drm_output { |
| 440 | struct weston_output base; |
| 441 | |
| 442 | uint32_t crtc_id; /* object ID to pass to DRM functions */ |
| 443 | int pipe; /* index of CRTC in resource array / bitmasks */ |
| 444 | |
Pekka Paalanen | cd011a6 | 2016-11-15 22:07:49 +0000 | [diff] [blame] | 445 | /* Holds the properties for the CRTC */ |
| 446 | struct drm_property_info props_crtc[WDRM_CRTC__COUNT]; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 447 | |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 448 | int vblank_pending; |
| 449 | int page_flip_pending; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 450 | int atomic_complete_pending; |
Xiong Zhang | abd5d47 | 2013-10-11 14:43:07 +0800 | [diff] [blame] | 451 | int destroy_pending; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 452 | int disable_pending; |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 453 | int dpms_off_pending; |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 454 | |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 455 | struct drm_fb *gbm_cursor_fb[2]; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 456 | struct drm_plane *cursor_plane; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 457 | struct weston_view *cursor_view; |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 458 | int current_cursor; |
Daniel Stone | 5bb8f58 | 2017-04-04 17:54:28 +0100 | [diff] [blame] | 459 | |
| 460 | struct gbm_surface *gbm_surface; |
| 461 | uint32_t gbm_format; |
| 462 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 463 | /* Plane being displayed directly on the CRTC */ |
| 464 | struct drm_plane *scanout_plane; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 465 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 466 | /* The last state submitted to the kernel for this CRTC. */ |
| 467 | struct drm_output_state *state_cur; |
| 468 | /* The previously-submitted state, where the hardware has not |
| 469 | * yet acknowledged completion of state_cur. */ |
| 470 | struct drm_output_state *state_last; |
| 471 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 472 | struct drm_fb *dumb[2]; |
| 473 | pixman_image_t *image[2]; |
| 474 | int current_image; |
| 475 | pixman_region32_t previous_damage; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 476 | |
| 477 | struct vaapi_recorder *recorder; |
| 478 | struct wl_listener recorder_frame_listener; |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 479 | |
| 480 | struct wl_event_source *pageflip_timer; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 481 | }; |
| 482 | |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 483 | static const char *const aspect_ratio_as_string[] = { |
| 484 | [WESTON_MODE_PIC_AR_NONE] = "", |
| 485 | [WESTON_MODE_PIC_AR_4_3] = " 4:3", |
| 486 | [WESTON_MODE_PIC_AR_16_9] = " 16:9", |
| 487 | [WESTON_MODE_PIC_AR_64_27] = " 64:27", |
| 488 | [WESTON_MODE_PIC_AR_256_135] = " 256:135", |
| 489 | }; |
| 490 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 491 | static struct gl_renderer_interface *gl_renderer; |
| 492 | |
Kristian Høgsberg | 98cfea6 | 2013-02-18 16:15:53 -0500 | [diff] [blame] | 493 | static const char default_seat[] = "seat0"; |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 494 | |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 495 | static void |
| 496 | wl_array_remove_uint32(struct wl_array *array, uint32_t elm) |
| 497 | { |
| 498 | uint32_t *pos, *end; |
| 499 | |
| 500 | end = (uint32_t *) ((char *) array->data + array->size); |
| 501 | |
| 502 | wl_array_for_each(pos, array) { |
| 503 | if (*pos != elm) |
| 504 | continue; |
| 505 | |
| 506 | array->size -= sizeof(*pos); |
| 507 | if (pos + 1 == end) |
| 508 | break; |
| 509 | |
| 510 | memmove(pos, pos + 1, (char *) end - (char *) (pos + 1)); |
| 511 | break; |
| 512 | } |
| 513 | } |
| 514 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 515 | static inline struct drm_head * |
| 516 | to_drm_head(struct weston_head *base) |
| 517 | { |
| 518 | return container_of(base, struct drm_head, base); |
| 519 | } |
| 520 | |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 521 | static inline struct drm_output * |
| 522 | to_drm_output(struct weston_output *base) |
| 523 | { |
| 524 | return container_of(base, struct drm_output, base); |
| 525 | } |
| 526 | |
| 527 | static inline struct drm_backend * |
| 528 | to_drm_backend(struct weston_compositor *base) |
| 529 | { |
| 530 | return container_of(base->backend, struct drm_backend, base); |
| 531 | } |
| 532 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 533 | static int |
| 534 | pageflip_timeout(void *data) { |
| 535 | /* |
| 536 | * Our timer just went off, that means we're not receiving drm |
| 537 | * page flip events anymore for that output. Let's gracefully exit |
| 538 | * weston with a return value so devs can debug what's going on. |
| 539 | */ |
| 540 | struct drm_output *output = data; |
| 541 | struct weston_compositor *compositor = output->base.compositor; |
| 542 | |
| 543 | weston_log("Pageflip timeout reached on output %s, your " |
| 544 | "driver is probably buggy! Exiting.\n", |
| 545 | output->base.name); |
| 546 | weston_compositor_exit_with_code(compositor, EXIT_FAILURE); |
| 547 | |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | /* Creates the pageflip timer. Note that it isn't armed by default */ |
| 552 | static int |
| 553 | drm_output_pageflip_timer_create(struct drm_output *output) |
| 554 | { |
| 555 | struct wl_event_loop *loop = NULL; |
| 556 | struct weston_compositor *ec = output->base.compositor; |
| 557 | |
| 558 | loop = wl_display_get_event_loop(ec->wl_display); |
| 559 | assert(loop); |
| 560 | output->pageflip_timer = wl_event_loop_add_timer(loop, |
| 561 | pageflip_timeout, |
| 562 | output); |
| 563 | |
| 564 | if (output->pageflip_timer == NULL) { |
| 565 | weston_log("creating drm pageflip timer failed: %m\n"); |
| 566 | return -1; |
| 567 | } |
| 568 | |
| 569 | return 0; |
| 570 | } |
| 571 | |
Daniel Stone | cb04cc4 | 2016-11-16 11:51:27 +0000 | [diff] [blame] | 572 | static inline struct drm_mode * |
| 573 | to_drm_mode(struct weston_mode *base) |
| 574 | { |
| 575 | return container_of(base, struct drm_mode, base); |
| 576 | } |
| 577 | |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 578 | /** |
| 579 | * Get the current value of a KMS property |
| 580 | * |
| 581 | * Given a drmModeObjectGetProperties return, as well as the drm_property_info |
| 582 | * for the target property, return the current value of that property, |
| 583 | * with an optional default. If the property is a KMS enum type, the return |
| 584 | * value will be translated into the appropriate internal enum. |
| 585 | * |
| 586 | * If the property is not present, the default value will be returned. |
| 587 | * |
| 588 | * @param info Internal structure for property to look up |
| 589 | * @param props Raw KMS properties for the target object |
| 590 | * @param def Value to return if property is not found |
| 591 | */ |
| 592 | static uint64_t |
| 593 | drm_property_get_value(struct drm_property_info *info, |
Daniel Stone | 85eebdf | 2018-07-05 17:55:43 +0100 | [diff] [blame^] | 594 | const drmModeObjectProperties *props, |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 595 | uint64_t def) |
| 596 | { |
| 597 | unsigned int i; |
| 598 | |
| 599 | if (info->prop_id == 0) |
| 600 | return def; |
| 601 | |
| 602 | for (i = 0; i < props->count_props; i++) { |
| 603 | unsigned int j; |
| 604 | |
| 605 | if (props->props[i] != info->prop_id) |
| 606 | continue; |
| 607 | |
| 608 | /* Simple (non-enum) types can return the value directly */ |
| 609 | if (info->num_enum_values == 0) |
| 610 | return props->prop_values[i]; |
| 611 | |
| 612 | /* Map from raw value to enum value */ |
| 613 | for (j = 0; j < info->num_enum_values; j++) { |
| 614 | if (!info->enum_values[j].valid) |
| 615 | continue; |
| 616 | if (info->enum_values[j].value != props->prop_values[i]) |
| 617 | continue; |
| 618 | |
| 619 | return j; |
| 620 | } |
| 621 | |
| 622 | /* We don't have a mapping for this enum; return default. */ |
| 623 | break; |
| 624 | } |
| 625 | |
| 626 | return def; |
| 627 | } |
| 628 | |
| 629 | /** |
| 630 | * Cache DRM property values |
| 631 | * |
| 632 | * Update a per-object array of drm_property_info structures, given the |
| 633 | * DRM properties of the object. |
| 634 | * |
| 635 | * Call this every time an object newly appears (note that only connectors |
| 636 | * can be hotplugged), the first time it is seen, or when its status changes |
| 637 | * in a way which invalidates the potential property values (currently, the |
| 638 | * only case for this is connector hotplug). |
| 639 | * |
| 640 | * This updates the property IDs and enum values within the drm_property_info |
| 641 | * array. |
| 642 | * |
| 643 | * DRM property enum values are dynamic at runtime; the user must query the |
| 644 | * property to find out the desired runtime value for a requested string |
| 645 | * name. Using the 'type' field on planes as an example, there is no single |
| 646 | * hardcoded constant for primary plane types; instead, the property must be |
| 647 | * queried at runtime to find the value associated with the string "Primary". |
| 648 | * |
| 649 | * This helper queries and caches the enum values, to allow us to use a set |
| 650 | * of compile-time-constant enums portably across various implementations. |
| 651 | * The values given in enum_names are searched for, and stored in the |
| 652 | * same-indexed field of the map array. |
| 653 | * |
| 654 | * @param b DRM backend object |
| 655 | * @param src DRM property info array to source from |
| 656 | * @param info DRM property info array to copy into |
| 657 | * @param num_infos Number of entries in the source array |
| 658 | * @param props DRM object properties for the object |
| 659 | */ |
| 660 | static void |
| 661 | drm_property_info_populate(struct drm_backend *b, |
| 662 | const struct drm_property_info *src, |
| 663 | struct drm_property_info *info, |
| 664 | unsigned int num_infos, |
| 665 | drmModeObjectProperties *props) |
| 666 | { |
| 667 | drmModePropertyRes *prop; |
| 668 | unsigned i, j; |
| 669 | |
| 670 | for (i = 0; i < num_infos; i++) { |
| 671 | unsigned int j; |
| 672 | |
| 673 | info[i].name = src[i].name; |
| 674 | info[i].prop_id = 0; |
| 675 | info[i].num_enum_values = src[i].num_enum_values; |
| 676 | |
| 677 | if (src[i].num_enum_values == 0) |
| 678 | continue; |
| 679 | |
| 680 | info[i].enum_values = |
| 681 | malloc(src[i].num_enum_values * |
| 682 | sizeof(*info[i].enum_values)); |
| 683 | assert(info[i].enum_values); |
| 684 | for (j = 0; j < info[i].num_enum_values; j++) { |
| 685 | info[i].enum_values[j].name = src[i].enum_values[j].name; |
| 686 | info[i].enum_values[j].valid = false; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | for (i = 0; i < props->count_props; i++) { |
| 691 | unsigned int k; |
| 692 | |
| 693 | prop = drmModeGetProperty(b->drm.fd, props->props[i]); |
| 694 | if (!prop) |
| 695 | continue; |
| 696 | |
| 697 | for (j = 0; j < num_infos; j++) { |
| 698 | if (!strcmp(prop->name, info[j].name)) |
| 699 | break; |
| 700 | } |
| 701 | |
| 702 | /* We don't know/care about this property. */ |
| 703 | if (j == num_infos) { |
| 704 | #ifdef DEBUG |
| 705 | weston_log("DRM debug: unrecognized property %u '%s'\n", |
| 706 | prop->prop_id, prop->name); |
| 707 | #endif |
| 708 | drmModeFreeProperty(prop); |
| 709 | continue; |
| 710 | } |
| 711 | |
| 712 | if (info[j].num_enum_values == 0 && |
| 713 | (prop->flags & DRM_MODE_PROP_ENUM)) { |
| 714 | weston_log("DRM: expected property %s to not be an" |
| 715 | " enum, but it is; ignoring\n", prop->name); |
| 716 | drmModeFreeProperty(prop); |
| 717 | continue; |
| 718 | } |
| 719 | |
| 720 | info[j].prop_id = props->props[i]; |
| 721 | |
| 722 | if (info[j].num_enum_values == 0) { |
| 723 | drmModeFreeProperty(prop); |
| 724 | continue; |
| 725 | } |
| 726 | |
| 727 | if (!(prop->flags & DRM_MODE_PROP_ENUM)) { |
| 728 | weston_log("DRM: expected property %s to be an enum," |
| 729 | " but it is not; ignoring\n", prop->name); |
| 730 | drmModeFreeProperty(prop); |
| 731 | info[j].prop_id = 0; |
| 732 | continue; |
| 733 | } |
| 734 | |
| 735 | for (k = 0; k < info[j].num_enum_values; k++) { |
| 736 | int l; |
| 737 | |
| 738 | for (l = 0; l < prop->count_enums; l++) { |
| 739 | if (!strcmp(prop->enums[l].name, |
| 740 | info[j].enum_values[k].name)) |
| 741 | break; |
| 742 | } |
| 743 | |
| 744 | if (l == prop->count_enums) |
| 745 | continue; |
| 746 | |
| 747 | info[j].enum_values[k].valid = true; |
| 748 | info[j].enum_values[k].value = prop->enums[l].value; |
| 749 | } |
| 750 | |
| 751 | drmModeFreeProperty(prop); |
| 752 | } |
| 753 | |
| 754 | #ifdef DEBUG |
| 755 | for (i = 0; i < num_infos; i++) { |
| 756 | if (info[i].prop_id == 0) |
| 757 | weston_log("DRM warning: property '%s' missing\n", |
| 758 | info[i].name); |
| 759 | } |
| 760 | #endif |
| 761 | } |
| 762 | |
| 763 | /** |
| 764 | * Free DRM property information |
| 765 | * |
Pekka Paalanen | 46e4f97 | 2017-09-07 15:32:01 +0300 | [diff] [blame] | 766 | * Frees all memory associated with a DRM property info array and zeroes |
| 767 | * it out, leaving it usable for a further drm_property_info_update() or |
| 768 | * drm_property_info_free(). |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 769 | * |
| 770 | * @param info DRM property info array |
| 771 | * @param num_props Number of entries in array to free |
| 772 | */ |
| 773 | static void |
| 774 | drm_property_info_free(struct drm_property_info *info, int num_props) |
| 775 | { |
| 776 | int i; |
| 777 | |
| 778 | for (i = 0; i < num_props; i++) |
| 779 | free(info[i].enum_values); |
Pekka Paalanen | 46e4f97 | 2017-09-07 15:32:01 +0300 | [diff] [blame] | 780 | |
| 781 | memset(info, 0, sizeof(*info) * num_props); |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 784 | static void |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 785 | drm_output_set_cursor(struct drm_output_state *output_state); |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 786 | |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 787 | static void |
| 788 | drm_output_update_msc(struct drm_output *output, unsigned int seq); |
| 789 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 790 | static void |
| 791 | drm_output_destroy(struct weston_output *output_base); |
| 792 | |
Daniel Stone | 5ff289a | 2017-10-07 12:59:02 +0100 | [diff] [blame] | 793 | /** |
| 794 | * Returns true if the plane can be used on the given output for its current |
| 795 | * repaint cycle. |
| 796 | */ |
| 797 | static bool |
| 798 | drm_plane_is_available(struct drm_plane *plane, struct drm_output *output) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 799 | { |
Daniel Stone | 5ff289a | 2017-10-07 12:59:02 +0100 | [diff] [blame] | 800 | assert(plane->state_cur); |
| 801 | |
| 802 | /* The plane still has a request not yet completed by the kernel. */ |
| 803 | if (!plane->state_cur->complete) |
| 804 | return false; |
| 805 | |
| 806 | /* The plane is still active on another output. */ |
| 807 | if (plane->state_cur->output && plane->state_cur->output != output) |
| 808 | return false; |
| 809 | |
| 810 | /* Check whether the plane can be used with this CRTC; possible_crtcs |
| 811 | * is a bitmask of CRTC indices (pipe), rather than CRTC object ID. */ |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 812 | return !!(plane->possible_crtcs & (1 << output->pipe)); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 813 | } |
| 814 | |
Daniel Stone | 72c0e1b | 2017-02-09 13:49:15 +0000 | [diff] [blame] | 815 | static struct drm_output * |
| 816 | drm_output_find_by_crtc(struct drm_backend *b, uint32_t crtc_id) |
| 817 | { |
| 818 | struct drm_output *output; |
| 819 | |
| 820 | wl_list_for_each(output, &b->compositor->output_list, base.link) { |
| 821 | if (output->crtc_id == crtc_id) |
| 822 | return output; |
| 823 | } |
| 824 | |
Daniel Stone | 72c0e1b | 2017-02-09 13:49:15 +0000 | [diff] [blame] | 825 | return NULL; |
| 826 | } |
| 827 | |
Pekka Paalanen | 54cc47c | 2017-08-31 11:58:41 +0300 | [diff] [blame] | 828 | static struct drm_head * |
| 829 | drm_head_find_by_connector(struct drm_backend *backend, uint32_t connector_id) |
| 830 | { |
| 831 | struct weston_head *base; |
| 832 | struct drm_head *head; |
| 833 | |
| 834 | wl_list_for_each(base, |
| 835 | &backend->compositor->head_list, compositor_link) { |
| 836 | head = to_drm_head(base); |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 837 | if (head->connector_id == connector_id) |
Pekka Paalanen | 54cc47c | 2017-08-31 11:58:41 +0300 | [diff] [blame] | 838 | return head; |
| 839 | } |
| 840 | |
| 841 | return NULL; |
| 842 | } |
| 843 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 844 | static void |
Tomohito Esaki | 576f42e | 2017-04-04 17:54:24 +0100 | [diff] [blame] | 845 | drm_fb_destroy(struct drm_fb *fb) |
| 846 | { |
| 847 | if (fb->fb_id != 0) |
| 848 | drmModeRmFB(fb->fd, fb->fb_id); |
| 849 | weston_buffer_reference(&fb->buffer_ref, NULL); |
| 850 | free(fb); |
| 851 | } |
| 852 | |
| 853 | static void |
| 854 | drm_fb_destroy_dumb(struct drm_fb *fb) |
| 855 | { |
| 856 | struct drm_mode_destroy_dumb destroy_arg; |
| 857 | |
| 858 | assert(fb->type == BUFFER_PIXMAN_DUMB); |
| 859 | |
| 860 | if (fb->map && fb->size > 0) |
| 861 | munmap(fb->map, fb->size); |
| 862 | |
| 863 | memset(&destroy_arg, 0, sizeof(destroy_arg)); |
| 864 | destroy_arg.handle = fb->handle; |
| 865 | drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg); |
| 866 | |
| 867 | drm_fb_destroy(fb); |
| 868 | } |
| 869 | |
| 870 | static void |
| 871 | drm_fb_destroy_gbm(struct gbm_bo *bo, void *data) |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 872 | { |
| 873 | struct drm_fb *fb = data; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 874 | |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 875 | assert(fb->type == BUFFER_GBM_SURFACE || fb->type == BUFFER_CLIENT || |
| 876 | fb->type == BUFFER_CURSOR); |
Tomohito Esaki | 576f42e | 2017-04-04 17:54:24 +0100 | [diff] [blame] | 877 | drm_fb_destroy(fb); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | static struct drm_fb * |
Daniel Stone | f214fdc | 2016-11-14 17:43:57 +0000 | [diff] [blame] | 881 | drm_fb_create_dumb(struct drm_backend *b, int width, int height, |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 882 | uint32_t format) |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 883 | { |
| 884 | struct drm_fb *fb; |
| 885 | int ret; |
| 886 | |
| 887 | struct drm_mode_create_dumb create_arg; |
| 888 | struct drm_mode_destroy_dumb destroy_arg; |
| 889 | struct drm_mode_map_dumb map_arg; |
Daniel Stone | 1de4252 | 2016-11-17 18:17:16 +0000 | [diff] [blame] | 890 | uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 }; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 891 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 892 | fb = zalloc(sizeof *fb); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 893 | if (!fb) |
| 894 | return NULL; |
| 895 | |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 896 | fb->refcnt = 1; |
| 897 | |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 898 | fb->format = pixel_format_get_info(format); |
| 899 | if (!fb->format) { |
| 900 | weston_log("failed to look up format 0x%lx\n", |
| 901 | (unsigned long) format); |
| 902 | goto err_fb; |
| 903 | } |
| 904 | |
| 905 | if (!fb->format->depth || !fb->format->bpp) { |
| 906 | weston_log("format 0x%lx is not compatible with dumb buffers\n", |
| 907 | (unsigned long) format); |
| 908 | goto err_fb; |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 909 | } |
| 910 | |
Kristian Høgsberg | ac6104e | 2013-08-21 22:14:14 -0700 | [diff] [blame] | 911 | memset(&create_arg, 0, sizeof create_arg); |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 912 | create_arg.bpp = fb->format->bpp; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 913 | create_arg.width = width; |
| 914 | create_arg.height = height; |
| 915 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 916 | 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] | 917 | if (ret) |
| 918 | goto err_fb; |
| 919 | |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 920 | fb->type = BUFFER_PIXMAN_DUMB; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 921 | fb->handle = create_arg.handle; |
| 922 | fb->stride = create_arg.pitch; |
| 923 | fb->size = create_arg.size; |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 924 | fb->width = width; |
| 925 | fb->height = height; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 926 | fb->fd = b->drm.fd; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 927 | |
Daniel Stone | 1de4252 | 2016-11-17 18:17:16 +0000 | [diff] [blame] | 928 | handles[0] = fb->handle; |
| 929 | pitches[0] = fb->stride; |
| 930 | offsets[0] = 0; |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 931 | |
Daniel Stone | 1de4252 | 2016-11-17 18:17:16 +0000 | [diff] [blame] | 932 | ret = drmModeAddFB2(b->drm.fd, width, height, fb->format->format, |
| 933 | handles, pitches, offsets, &fb->fb_id, 0); |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 934 | if (ret) { |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 935 | ret = drmModeAddFB(b->drm.fd, width, height, |
| 936 | fb->format->depth, fb->format->bpp, |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 937 | fb->stride, fb->handle, &fb->fb_id); |
| 938 | } |
| 939 | |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 940 | if (ret) |
| 941 | goto err_bo; |
| 942 | |
Kristian Høgsberg | ac6104e | 2013-08-21 22:14:14 -0700 | [diff] [blame] | 943 | memset(&map_arg, 0, sizeof map_arg); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 944 | map_arg.handle = fb->handle; |
Chris Michael | eb2074a | 2013-05-01 21:26:02 -0400 | [diff] [blame] | 945 | 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] | 946 | if (ret) |
| 947 | goto err_add_fb; |
| 948 | |
Chris Michael | 4a7ce1f | 2015-11-10 10:40:37 -0500 | [diff] [blame] | 949 | fb->map = mmap(NULL, fb->size, PROT_WRITE, |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 950 | MAP_SHARED, b->drm.fd, map_arg.offset); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 951 | if (fb->map == MAP_FAILED) |
| 952 | goto err_add_fb; |
| 953 | |
| 954 | return fb; |
| 955 | |
| 956 | err_add_fb: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 957 | drmModeRmFB(b->drm.fd, fb->fb_id); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 958 | err_bo: |
| 959 | memset(&destroy_arg, 0, sizeof(destroy_arg)); |
| 960 | destroy_arg.handle = create_arg.handle; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 961 | 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] | 962 | err_fb: |
| 963 | free(fb); |
| 964 | return NULL; |
| 965 | } |
| 966 | |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 967 | static struct drm_fb * |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 968 | drm_fb_ref(struct drm_fb *fb) |
| 969 | { |
| 970 | fb->refcnt++; |
| 971 | return fb; |
| 972 | } |
| 973 | |
| 974 | static struct drm_fb * |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 975 | drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend, |
| 976 | uint32_t format, enum drm_fb_type type) |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 977 | { |
| 978 | struct drm_fb *fb = gbm_bo_get_user_data(bo); |
Derek Foreman | 482ffdf | 2016-07-08 12:50:57 -0500 | [diff] [blame] | 979 | 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] | 980 | int ret; |
| 981 | |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 982 | if (fb) { |
| 983 | assert(fb->type == type); |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 984 | return drm_fb_ref(fb); |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 985 | } |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 986 | |
Daniel Stone | 1de4252 | 2016-11-17 18:17:16 +0000 | [diff] [blame] | 987 | assert(format != 0); |
| 988 | |
Bryce Harrington | de16d89 | 2014-11-20 22:21:57 -0800 | [diff] [blame] | 989 | fb = zalloc(sizeof *fb); |
| 990 | if (fb == NULL) |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 991 | return NULL; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 992 | |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 993 | fb->type = type; |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 994 | fb->refcnt = 1; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 995 | fb->bo = bo; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 996 | |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 997 | fb->width = gbm_bo_get_width(bo); |
| 998 | fb->height = gbm_bo_get_height(bo); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 999 | fb->stride = gbm_bo_get_stride(bo); |
| 1000 | fb->handle = gbm_bo_get_handle(bo).u32; |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 1001 | fb->format = pixel_format_get_info(format); |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 1002 | fb->size = fb->stride * fb->height; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1003 | fb->fd = backend->drm.fd; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1004 | |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 1005 | if (!fb->format) { |
| 1006 | weston_log("couldn't look up format 0x%lx\n", |
| 1007 | (unsigned long) format); |
| 1008 | goto err_free; |
| 1009 | } |
| 1010 | |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 1011 | if (backend->min_width > fb->width || |
| 1012 | fb->width > backend->max_width || |
| 1013 | backend->min_height > fb->height || |
| 1014 | fb->height > backend->max_height) { |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1015 | weston_log("bo geometry out of bounds\n"); |
| 1016 | goto err_free; |
| 1017 | } |
| 1018 | |
Daniel Stone | 1de4252 | 2016-11-17 18:17:16 +0000 | [diff] [blame] | 1019 | handles[0] = fb->handle; |
| 1020 | pitches[0] = fb->stride; |
| 1021 | offsets[0] = 0; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1022 | |
Daniel Stone | 1de4252 | 2016-11-17 18:17:16 +0000 | [diff] [blame] | 1023 | ret = drmModeAddFB2(backend->drm.fd, fb->width, fb->height, |
| 1024 | fb->format->format, handles, pitches, offsets, |
| 1025 | &fb->fb_id, 0); |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 1026 | if (ret && fb->format->depth && fb->format->bpp) |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 1027 | ret = drmModeAddFB(backend->drm.fd, fb->width, fb->height, |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 1028 | fb->format->depth, fb->format->bpp, |
| 1029 | fb->stride, fb->handle, &fb->fb_id); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1030 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1031 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1032 | weston_log("failed to create kms fb: %m\n"); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1033 | goto err_free; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1034 | } |
| 1035 | |
Tomohito Esaki | 576f42e | 2017-04-04 17:54:24 +0100 | [diff] [blame] | 1036 | 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] | 1037 | |
| 1038 | return fb; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1039 | |
| 1040 | err_free: |
| 1041 | free(fb); |
| 1042 | return NULL; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | static void |
Jason Ekstrand | 6bd6294 | 2013-06-20 20:38:23 -0500 | [diff] [blame] | 1046 | 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] | 1047 | { |
Pekka Paalanen | de685b8 | 2012-12-04 15:58:12 +0200 | [diff] [blame] | 1048 | assert(fb->buffer_ref.buffer == NULL); |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 1049 | assert(fb->type == BUFFER_CLIENT); |
Pekka Paalanen | de685b8 | 2012-12-04 15:58:12 +0200 | [diff] [blame] | 1050 | weston_buffer_reference(&fb->buffer_ref, buffer); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1051 | } |
| 1052 | |
Ander Conselvan de Oliveira | 526d461 | 2013-01-25 15:13:03 +0200 | [diff] [blame] | 1053 | static void |
Daniel Stone | 05a5ac2 | 2017-04-04 17:54:25 +0100 | [diff] [blame] | 1054 | drm_fb_unref(struct drm_fb *fb) |
Ander Conselvan de Oliveira | 526d461 | 2013-01-25 15:13:03 +0200 | [diff] [blame] | 1055 | { |
| 1056 | if (!fb) |
| 1057 | return; |
| 1058 | |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 1059 | assert(fb->refcnt > 0); |
| 1060 | if (--fb->refcnt > 0) |
| 1061 | return; |
| 1062 | |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 1063 | switch (fb->type) { |
| 1064 | case BUFFER_PIXMAN_DUMB: |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 1065 | drm_fb_destroy_dumb(fb); |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 1066 | break; |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 1067 | case BUFFER_CURSOR: |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 1068 | case BUFFER_CLIENT: |
| 1069 | gbm_bo_destroy(fb->bo); |
| 1070 | break; |
| 1071 | case BUFFER_GBM_SURFACE: |
Daniel Stone | 05a5ac2 | 2017-04-04 17:54:25 +0100 | [diff] [blame] | 1072 | gbm_surface_release_buffer(fb->gbm_surface, fb->bo); |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 1073 | break; |
| 1074 | default: |
| 1075 | assert(NULL); |
| 1076 | break; |
Ander Conselvan de Oliveira | 526d461 | 2013-01-25 15:13:03 +0200 | [diff] [blame] | 1077 | } |
| 1078 | } |
| 1079 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1080 | /** |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1081 | * Allocate a new, empty, plane state. |
| 1082 | */ |
| 1083 | static struct drm_plane_state * |
| 1084 | drm_plane_state_alloc(struct drm_output_state *state_output, |
| 1085 | struct drm_plane *plane) |
| 1086 | { |
| 1087 | struct drm_plane_state *state = zalloc(sizeof(*state)); |
| 1088 | |
| 1089 | assert(state); |
| 1090 | state->output_state = state_output; |
| 1091 | state->plane = plane; |
| 1092 | |
| 1093 | /* Here we only add the plane state to the desired link, and not |
| 1094 | * set the member. Having an output pointer set means that the |
| 1095 | * plane will be displayed on the output; this won't be the case |
| 1096 | * when we go to disable a plane. In this case, it must be part of |
| 1097 | * the commit (and thus the output state), but the member must be |
| 1098 | * NULL, as it will not be on any output when the state takes |
| 1099 | * effect. |
| 1100 | */ |
| 1101 | if (state_output) |
| 1102 | wl_list_insert(&state_output->plane_list, &state->link); |
| 1103 | else |
| 1104 | wl_list_init(&state->link); |
| 1105 | |
| 1106 | return state; |
| 1107 | } |
| 1108 | |
| 1109 | /** |
| 1110 | * Free an existing plane state. As a special case, the state will not |
| 1111 | * normally be freed if it is the current state; see drm_plane_set_state. |
| 1112 | */ |
| 1113 | static void |
| 1114 | drm_plane_state_free(struct drm_plane_state *state, bool force) |
| 1115 | { |
| 1116 | if (!state) |
| 1117 | return; |
| 1118 | |
| 1119 | wl_list_remove(&state->link); |
| 1120 | wl_list_init(&state->link); |
| 1121 | state->output_state = NULL; |
| 1122 | |
| 1123 | if (force || state != state->plane->state_cur) { |
| 1124 | drm_fb_unref(state->fb); |
| 1125 | free(state); |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | /** |
| 1130 | * Duplicate an existing plane state into a new plane state, storing it within |
| 1131 | * the given output state. If the output state already contains a plane state |
| 1132 | * for the drm_plane referenced by 'src', that plane state is freed first. |
| 1133 | */ |
| 1134 | static struct drm_plane_state * |
| 1135 | drm_plane_state_duplicate(struct drm_output_state *state_output, |
| 1136 | struct drm_plane_state *src) |
| 1137 | { |
| 1138 | struct drm_plane_state *dst = malloc(sizeof(*dst)); |
| 1139 | struct drm_plane_state *old, *tmp; |
| 1140 | |
| 1141 | assert(src); |
| 1142 | assert(dst); |
| 1143 | *dst = *src; |
| 1144 | wl_list_init(&dst->link); |
| 1145 | |
| 1146 | wl_list_for_each_safe(old, tmp, &state_output->plane_list, link) { |
| 1147 | /* Duplicating a plane state into the same output state, so |
| 1148 | * it can replace itself with an identical copy of itself, |
| 1149 | * makes no sense. */ |
| 1150 | assert(old != src); |
| 1151 | if (old->plane == dst->plane) |
| 1152 | drm_plane_state_free(old, false); |
| 1153 | } |
| 1154 | |
| 1155 | wl_list_insert(&state_output->plane_list, &dst->link); |
| 1156 | if (src->fb) |
| 1157 | dst->fb = drm_fb_ref(src->fb); |
| 1158 | dst->output_state = state_output; |
| 1159 | dst->complete = false; |
| 1160 | |
| 1161 | return dst; |
| 1162 | } |
| 1163 | |
| 1164 | /** |
| 1165 | * Remove a plane state from an output state; if the plane was previously |
| 1166 | * enabled, then replace it with a disabling state. This ensures that the |
| 1167 | * output state was untouched from it was before the plane state was |
| 1168 | * modified by the caller of this function. |
| 1169 | * |
| 1170 | * This is required as drm_output_state_get_plane may either allocate a |
| 1171 | * new plane state, in which case this function will just perform a matching |
| 1172 | * drm_plane_state_free, or it may instead repurpose an existing disabling |
| 1173 | * state (if the plane was previously active), in which case this function |
| 1174 | * will reset it. |
| 1175 | */ |
| 1176 | static void |
| 1177 | drm_plane_state_put_back(struct drm_plane_state *state) |
| 1178 | { |
| 1179 | struct drm_output_state *state_output; |
| 1180 | struct drm_plane *plane; |
| 1181 | |
| 1182 | if (!state) |
| 1183 | return; |
| 1184 | |
| 1185 | state_output = state->output_state; |
| 1186 | plane = state->plane; |
| 1187 | drm_plane_state_free(state, false); |
| 1188 | |
| 1189 | /* Plane was previously disabled; no need to keep this temporary |
| 1190 | * state around. */ |
| 1191 | if (!plane->state_cur->fb) |
| 1192 | return; |
| 1193 | |
| 1194 | (void) drm_plane_state_alloc(state_output, plane); |
| 1195 | } |
| 1196 | |
| 1197 | /** |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 1198 | * Return a plane state from a drm_output_state. |
| 1199 | */ |
| 1200 | static struct drm_plane_state * |
| 1201 | drm_output_state_get_existing_plane(struct drm_output_state *state_output, |
| 1202 | struct drm_plane *plane) |
| 1203 | { |
| 1204 | struct drm_plane_state *ps; |
| 1205 | |
| 1206 | wl_list_for_each(ps, &state_output->plane_list, link) { |
| 1207 | if (ps->plane == plane) |
| 1208 | return ps; |
| 1209 | } |
| 1210 | |
| 1211 | return NULL; |
| 1212 | } |
| 1213 | |
| 1214 | /** |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1215 | * Return a plane state from a drm_output_state, either existing or |
| 1216 | * freshly allocated. |
| 1217 | */ |
| 1218 | static struct drm_plane_state * |
| 1219 | drm_output_state_get_plane(struct drm_output_state *state_output, |
| 1220 | struct drm_plane *plane) |
| 1221 | { |
| 1222 | struct drm_plane_state *ps; |
| 1223 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 1224 | ps = drm_output_state_get_existing_plane(state_output, plane); |
| 1225 | if (ps) |
| 1226 | return ps; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1227 | |
| 1228 | return drm_plane_state_alloc(state_output, plane); |
| 1229 | } |
| 1230 | |
| 1231 | /** |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1232 | * Allocate a new, empty drm_output_state. This should not generally be used |
| 1233 | * in the repaint cycle; see drm_output_state_duplicate. |
| 1234 | */ |
| 1235 | static struct drm_output_state * |
| 1236 | drm_output_state_alloc(struct drm_output *output, |
| 1237 | struct drm_pending_state *pending_state) |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 1238 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1239 | struct drm_output_state *state = zalloc(sizeof(*state)); |
| 1240 | |
| 1241 | assert(state); |
| 1242 | state->output = output; |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1243 | state->dpms = WESTON_DPMS_OFF; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1244 | state->pending_state = pending_state; |
| 1245 | if (pending_state) |
| 1246 | wl_list_insert(&pending_state->output_list, &state->link); |
| 1247 | else |
| 1248 | wl_list_init(&state->link); |
| 1249 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1250 | wl_list_init(&state->plane_list); |
| 1251 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1252 | return state; |
| 1253 | } |
| 1254 | |
| 1255 | /** |
| 1256 | * Duplicate an existing drm_output_state into a new one. This is generally |
| 1257 | * used during the repaint cycle, to capture the existing state of an output |
| 1258 | * and modify it to create a new state to be used. |
| 1259 | * |
| 1260 | * The mode determines whether the output will be reset to an a blank state, |
| 1261 | * or an exact mirror of the current state. |
| 1262 | */ |
| 1263 | static struct drm_output_state * |
| 1264 | drm_output_state_duplicate(struct drm_output_state *src, |
| 1265 | struct drm_pending_state *pending_state, |
| 1266 | enum drm_output_state_duplicate_mode plane_mode) |
| 1267 | { |
| 1268 | struct drm_output_state *dst = malloc(sizeof(*dst)); |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1269 | struct drm_plane_state *ps; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1270 | |
| 1271 | assert(dst); |
| 1272 | |
| 1273 | /* Copy the whole structure, then individually modify the |
| 1274 | * pending_state, as well as the list link into our pending |
| 1275 | * state. */ |
| 1276 | *dst = *src; |
| 1277 | |
| 1278 | dst->pending_state = pending_state; |
| 1279 | if (pending_state) |
| 1280 | wl_list_insert(&pending_state->output_list, &dst->link); |
| 1281 | else |
| 1282 | wl_list_init(&dst->link); |
| 1283 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1284 | wl_list_init(&dst->plane_list); |
| 1285 | |
| 1286 | wl_list_for_each(ps, &src->plane_list, link) { |
| 1287 | /* Don't carry planes which are now disabled; these should be |
| 1288 | * free for other outputs to reuse. */ |
| 1289 | if (!ps->output) |
| 1290 | continue; |
| 1291 | |
| 1292 | if (plane_mode == DRM_OUTPUT_STATE_CLEAR_PLANES) |
| 1293 | (void) drm_plane_state_alloc(dst, ps->plane); |
| 1294 | else |
| 1295 | (void) drm_plane_state_duplicate(dst, ps); |
| 1296 | } |
| 1297 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1298 | return dst; |
| 1299 | } |
| 1300 | |
| 1301 | /** |
| 1302 | * Free an unused drm_output_state. |
| 1303 | */ |
| 1304 | static void |
| 1305 | drm_output_state_free(struct drm_output_state *state) |
| 1306 | { |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1307 | struct drm_plane_state *ps, *next; |
| 1308 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1309 | if (!state) |
| 1310 | return; |
| 1311 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1312 | wl_list_for_each_safe(ps, next, &state->plane_list, link) |
| 1313 | drm_plane_state_free(ps, false); |
| 1314 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1315 | wl_list_remove(&state->link); |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1316 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1317 | free(state); |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 1318 | } |
| 1319 | |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 1320 | /** |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1321 | * Get output state to disable output |
| 1322 | * |
| 1323 | * Returns a pointer to an output_state object which can be used to disable |
| 1324 | * an output (e.g. DPMS off). |
| 1325 | * |
| 1326 | * @param pending_state The pending state object owning this update |
| 1327 | * @param output The output to disable |
| 1328 | * @returns A drm_output_state to disable the output |
| 1329 | */ |
| 1330 | static struct drm_output_state * |
| 1331 | drm_output_get_disable_state(struct drm_pending_state *pending_state, |
| 1332 | struct drm_output *output) |
| 1333 | { |
| 1334 | struct drm_output_state *output_state; |
| 1335 | |
| 1336 | output_state = drm_output_state_duplicate(output->state_cur, |
| 1337 | pending_state, |
| 1338 | DRM_OUTPUT_STATE_CLEAR_PLANES); |
| 1339 | output_state->dpms = WESTON_DPMS_OFF; |
| 1340 | |
| 1341 | return output_state; |
| 1342 | } |
| 1343 | |
| 1344 | /** |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 1345 | * Allocate a new drm_pending_state |
| 1346 | * |
| 1347 | * Allocate a new, empty, 'pending state' structure to be used across a |
| 1348 | * repaint cycle or similar. |
| 1349 | * |
| 1350 | * @param backend DRM backend |
| 1351 | * @returns Newly-allocated pending state structure |
| 1352 | */ |
| 1353 | static struct drm_pending_state * |
| 1354 | drm_pending_state_alloc(struct drm_backend *backend) |
| 1355 | { |
| 1356 | struct drm_pending_state *ret; |
| 1357 | |
| 1358 | ret = calloc(1, sizeof(*ret)); |
| 1359 | if (!ret) |
| 1360 | return NULL; |
| 1361 | |
| 1362 | ret->backend = backend; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1363 | wl_list_init(&ret->output_list); |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 1364 | |
| 1365 | return ret; |
| 1366 | } |
| 1367 | |
| 1368 | /** |
| 1369 | * Free a drm_pending_state structure |
| 1370 | * |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1371 | * Frees a pending_state structure, as well as any output_states connected |
| 1372 | * to this pending state. |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 1373 | * |
| 1374 | * @param pending_state Pending state structure to free |
| 1375 | */ |
| 1376 | static void |
| 1377 | drm_pending_state_free(struct drm_pending_state *pending_state) |
| 1378 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1379 | struct drm_output_state *output_state, *tmp; |
| 1380 | |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 1381 | if (!pending_state) |
| 1382 | return; |
| 1383 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1384 | wl_list_for_each_safe(output_state, tmp, &pending_state->output_list, |
| 1385 | link) { |
| 1386 | drm_output_state_free(output_state); |
| 1387 | } |
| 1388 | |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 1389 | free(pending_state); |
| 1390 | } |
| 1391 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1392 | /** |
| 1393 | * Find an output state in a pending state |
| 1394 | * |
| 1395 | * Given a pending_state structure, find the output_state for a particular |
| 1396 | * output. |
| 1397 | * |
| 1398 | * @param pending_state Pending state structure to search |
| 1399 | * @param output Output to find state for |
| 1400 | * @returns Output state if present, or NULL if not |
| 1401 | */ |
| 1402 | static struct drm_output_state * |
| 1403 | drm_pending_state_get_output(struct drm_pending_state *pending_state, |
| 1404 | struct drm_output *output) |
| 1405 | { |
| 1406 | struct drm_output_state *output_state; |
| 1407 | |
| 1408 | wl_list_for_each(output_state, &pending_state->output_list, link) { |
| 1409 | if (output_state->output == output) |
| 1410 | return output_state; |
| 1411 | } |
| 1412 | |
| 1413 | return NULL; |
| 1414 | } |
| 1415 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1416 | static int drm_pending_state_apply_sync(struct drm_pending_state *state); |
| 1417 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1418 | /** |
| 1419 | * Mark a drm_output_state (the output's last state) as complete. This handles |
| 1420 | * any post-completion actions such as updating the repaint timer, disabling the |
| 1421 | * output, and finally freeing the state. |
| 1422 | */ |
| 1423 | static void |
| 1424 | drm_output_update_complete(struct drm_output *output, uint32_t flags, |
| 1425 | unsigned int sec, unsigned int usec) |
| 1426 | { |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1427 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1428 | struct drm_plane_state *ps; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1429 | struct timespec ts; |
| 1430 | |
| 1431 | /* Stop the pageflip timer instead of rearming it here */ |
| 1432 | if (output->pageflip_timer) |
| 1433 | wl_event_source_timer_update(output->pageflip_timer, 0); |
| 1434 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1435 | wl_list_for_each(ps, &output->state_cur->plane_list, link) |
| 1436 | ps->complete = true; |
| 1437 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1438 | drm_output_state_free(output->state_last); |
| 1439 | output->state_last = NULL; |
| 1440 | |
| 1441 | if (output->destroy_pending) { |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1442 | output->destroy_pending = 0; |
| 1443 | output->disable_pending = 0; |
| 1444 | output->dpms_off_pending = 0; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1445 | drm_output_destroy(&output->base); |
| 1446 | return; |
| 1447 | } else if (output->disable_pending) { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1448 | output->disable_pending = 0; |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1449 | output->dpms_off_pending = 0; |
| 1450 | weston_output_disable(&output->base); |
| 1451 | return; |
| 1452 | } else if (output->dpms_off_pending) { |
| 1453 | struct drm_pending_state *pending = drm_pending_state_alloc(b); |
| 1454 | output->dpms_off_pending = 0; |
| 1455 | drm_output_get_disable_state(pending, output); |
| 1456 | drm_pending_state_apply_sync(pending); |
| 1457 | return; |
| 1458 | } else if (output->state_cur->dpms == WESTON_DPMS_OFF && |
| 1459 | output->base.repaint_status != REPAINT_AWAITING_COMPLETION) { |
| 1460 | /* DPMS can happen to us either in the middle of a repaint |
| 1461 | * cycle (when we have painted fresh content, only to throw it |
| 1462 | * away for DPMS off), or at any other random point. If the |
| 1463 | * latter is true, then we cannot go through finish_frame, |
| 1464 | * because the repaint machinery does not expect this. */ |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1465 | return; |
| 1466 | } |
| 1467 | |
| 1468 | ts.tv_sec = sec; |
| 1469 | ts.tv_nsec = usec * 1000; |
| 1470 | weston_output_finish_frame(&output->base, &ts, flags); |
| 1471 | |
| 1472 | /* We can't call this from frame_notify, because the output's |
| 1473 | * repaint needed flag is cleared just after that */ |
| 1474 | if (output->recorder) |
| 1475 | weston_output_schedule_repaint(&output->base); |
| 1476 | } |
| 1477 | |
| 1478 | /** |
| 1479 | * Mark an output state as current on the output, i.e. it has been |
| 1480 | * submitted to the kernel. The mode argument determines whether this |
| 1481 | * update will be applied synchronously (e.g. when calling drmModeSetCrtc), |
| 1482 | * or asynchronously (in which case we wait for events to complete). |
| 1483 | */ |
| 1484 | static void |
| 1485 | drm_output_assign_state(struct drm_output_state *state, |
| 1486 | enum drm_state_apply_mode mode) |
| 1487 | { |
| 1488 | struct drm_output *output = state->output; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 1489 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1490 | struct drm_plane_state *plane_state; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1491 | |
| 1492 | assert(!output->state_last); |
| 1493 | |
| 1494 | if (mode == DRM_STATE_APPLY_ASYNC) |
| 1495 | output->state_last = output->state_cur; |
| 1496 | else |
| 1497 | drm_output_state_free(output->state_cur); |
| 1498 | |
| 1499 | wl_list_remove(&state->link); |
| 1500 | wl_list_init(&state->link); |
| 1501 | state->pending_state = NULL; |
| 1502 | |
| 1503 | output->state_cur = state; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1504 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 1505 | if (b->atomic_modeset && mode == DRM_STATE_APPLY_ASYNC) |
| 1506 | output->atomic_complete_pending = 1; |
| 1507 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1508 | /* Replace state_cur on each affected plane with the new state, being |
| 1509 | * careful to dispose of orphaned (but only orphaned) previous state. |
| 1510 | * If the previous state is not orphaned (still has an output_state |
| 1511 | * attached), it will be disposed of by freeing the output_state. */ |
| 1512 | wl_list_for_each(plane_state, &state->plane_list, link) { |
| 1513 | struct drm_plane *plane = plane_state->plane; |
| 1514 | |
| 1515 | if (plane->state_cur && !plane->state_cur->output_state) |
| 1516 | drm_plane_state_free(plane->state_cur, true); |
| 1517 | plane->state_cur = plane_state; |
| 1518 | |
| 1519 | if (mode != DRM_STATE_APPLY_ASYNC) { |
| 1520 | plane_state->complete = true; |
| 1521 | continue; |
| 1522 | } |
| 1523 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 1524 | if (b->atomic_modeset) |
| 1525 | continue; |
| 1526 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1527 | if (plane->type == WDRM_PLANE_TYPE_OVERLAY) |
| 1528 | output->vblank_pending++; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1529 | else if (plane->type == WDRM_PLANE_TYPE_PRIMARY) |
| 1530 | output->page_flip_pending = 1; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1531 | } |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1534 | static int |
| 1535 | drm_view_transform_supported(struct weston_view *ev) |
| 1536 | { |
| 1537 | return !ev->transform.enabled || |
| 1538 | (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE); |
| 1539 | } |
| 1540 | |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 1541 | static uint32_t |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 1542 | drm_output_check_scanout_format(struct drm_output *output, |
| 1543 | struct weston_surface *es, struct gbm_bo *bo) |
| 1544 | { |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 1545 | uint32_t format; |
| 1546 | pixman_region32_t r; |
| 1547 | |
| 1548 | format = gbm_bo_get_format(bo); |
| 1549 | |
Kristian Høgsberg | 3f97b34 | 2013-10-16 16:08:57 -0700 | [diff] [blame] | 1550 | if (format == GBM_FORMAT_ARGB8888) { |
| 1551 | /* We can scanout an ARGB buffer if the surface's |
| 1552 | * opaque region covers the whole output, but we have |
| 1553 | * to use XRGB as the KMS format code. */ |
Kristian Høgsberg | 1be87e3 | 2014-01-17 14:22:41 -0800 | [diff] [blame] | 1554 | pixman_region32_init_rect(&r, 0, 0, |
| 1555 | output->base.width, |
| 1556 | output->base.height); |
| 1557 | pixman_region32_subtract(&r, &r, &es->opaque); |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 1558 | |
| 1559 | if (!pixman_region32_not_empty(&r)) |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 1560 | format = GBM_FORMAT_XRGB8888; |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 1561 | |
| 1562 | pixman_region32_fini(&r); |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 1563 | } |
Kristian Høgsberg | 3f97b34 | 2013-10-16 16:08:57 -0700 | [diff] [blame] | 1564 | |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 1565 | if (output->gbm_format == format) |
Kristian Høgsberg | 3f97b34 | 2013-10-16 16:08:57 -0700 | [diff] [blame] | 1566 | return format; |
| 1567 | |
| 1568 | return 0; |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 1569 | } |
| 1570 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1571 | static struct weston_plane * |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1572 | drm_output_prepare_scanout_view(struct drm_output_state *output_state, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1573 | struct weston_view *ev) |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 1574 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1575 | struct drm_output *output = output_state->output; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1576 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1577 | struct drm_plane *scanout_plane = output->scanout_plane; |
| 1578 | struct drm_plane_state *state; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1579 | struct weston_buffer *buffer = ev->surface->buffer_ref.buffer; |
Pekka Paalanen | 952b6c8 | 2014-03-14 14:38:15 +0200 | [diff] [blame] | 1580 | struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1581 | struct gbm_bo *bo; |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 1582 | uint32_t format; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 1583 | |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 1584 | /* Don't import buffers which span multiple outputs. */ |
| 1585 | if (ev->output_mask != (1u << output->base.id)) |
| 1586 | return NULL; |
| 1587 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1588 | /* We use GBM to import buffers. */ |
| 1589 | if (b->gbm == NULL) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1590 | return NULL; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 1591 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1592 | if (buffer == NULL) |
| 1593 | return NULL; |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 1594 | if (wl_shm_buffer_get(buffer->resource)) |
| 1595 | return NULL; |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1596 | |
| 1597 | /* Make sure our view is exactly compatible with the output. */ |
| 1598 | if (ev->geometry.x != output->base.x || |
| 1599 | ev->geometry.y != output->base.y) |
| 1600 | return NULL; |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 1601 | if (buffer->width != output->base.current_mode->width || |
| 1602 | buffer->height != output->base.current_mode->height) |
| 1603 | return NULL; |
| 1604 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1605 | if (ev->transform.enabled) |
| 1606 | return NULL; |
Pekka Paalanen | 5580f22 | 2015-02-17 16:33:18 +0200 | [diff] [blame] | 1607 | if (ev->geometry.scissor_enabled) |
| 1608 | return NULL; |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1609 | if (viewport->buffer.transform != output->base.transform) |
| 1610 | return NULL; |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 1611 | if (viewport->buffer.scale != output->base.current_scale) |
| 1612 | return NULL; |
| 1613 | if (!drm_view_transform_supported(ev)) |
| 1614 | return NULL; |
| 1615 | |
| 1616 | if (ev->alpha != 1.0f) |
| 1617 | return NULL; |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1618 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1619 | state = drm_output_state_get_plane(output_state, scanout_plane); |
| 1620 | if (state->fb) { |
| 1621 | /* If there is already a framebuffer on the scanout plane, |
| 1622 | * a client view has already been placed on the scanout |
| 1623 | * view. In that case, do not free or put back the state, |
| 1624 | * but just leave it in place and quietly exit. */ |
| 1625 | return NULL; |
| 1626 | } |
| 1627 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1628 | bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER, |
Kristian Høgsberg | 6399646 | 2013-09-03 22:27:08 -0700 | [diff] [blame] | 1629 | buffer->resource, GBM_BO_USE_SCANOUT); |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 1630 | |
Rob Bradford | 9b10187 | 2012-09-14 23:25:41 +0100 | [diff] [blame] | 1631 | /* Unable to use the buffer for scanout */ |
| 1632 | if (!bo) |
| 1633 | return NULL; |
| 1634 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1635 | format = drm_output_check_scanout_format(output, ev->surface, bo); |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 1636 | if (format == 0) { |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1637 | drm_plane_state_put_back(state); |
Ander Conselvan de Oliveira | a64b15d | 2012-05-02 16:42:22 +0300 | [diff] [blame] | 1638 | gbm_bo_destroy(bo); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1639 | return NULL; |
Ander Conselvan de Oliveira | a64b15d | 2012-05-02 16:42:22 +0300 | [diff] [blame] | 1640 | } |
| 1641 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1642 | state->fb = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT); |
| 1643 | if (!state->fb) { |
| 1644 | drm_plane_state_put_back(state); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1645 | gbm_bo_destroy(bo); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1646 | return NULL; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1647 | } |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 1648 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1649 | drm_fb_set_buffer(state->fb, buffer); |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 1650 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1651 | state->output = output; |
| 1652 | |
| 1653 | state->src_x = 0; |
| 1654 | state->src_y = 0; |
| 1655 | state->src_w = state->fb->width << 16; |
| 1656 | state->src_h = state->fb->height << 16; |
| 1657 | |
| 1658 | state->dest_x = 0; |
| 1659 | state->dest_y = 0; |
| 1660 | state->dest_w = output->base.current_mode->width; |
| 1661 | state->dest_h = output->base.current_mode->height; |
| 1662 | |
| 1663 | return &scanout_plane->base; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 1664 | } |
| 1665 | |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1666 | static struct drm_fb * |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1667 | drm_output_render_gl(struct drm_output_state *state, pixman_region32_t *damage) |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1668 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1669 | struct drm_output *output = state->output; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1670 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1671 | struct gbm_bo *bo; |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1672 | struct drm_fb *ret; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1673 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1674 | output->base.compositor->renderer->repaint_output(&output->base, |
| 1675 | damage); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1676 | |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 1677 | bo = gbm_surface_lock_front_buffer(output->gbm_surface); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1678 | if (!bo) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1679 | weston_log("failed to lock front buffer: %m\n"); |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1680 | return NULL; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1681 | } |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1682 | |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1683 | ret = drm_fb_get_from_bo(bo, b, output->gbm_format, BUFFER_GBM_SURFACE); |
| 1684 | if (!ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1685 | weston_log("failed to get drm_fb for bo\n"); |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 1686 | gbm_surface_release_buffer(output->gbm_surface, bo); |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1687 | return NULL; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1688 | } |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1689 | ret->gbm_surface = output->gbm_surface; |
| 1690 | |
| 1691 | return ret; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1692 | } |
| 1693 | |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1694 | static struct drm_fb * |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1695 | drm_output_render_pixman(struct drm_output_state *state, |
| 1696 | pixman_region32_t *damage) |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1697 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1698 | struct drm_output *output = state->output; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1699 | struct weston_compositor *ec = output->base.compositor; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1700 | |
| 1701 | output->current_image ^= 1; |
| 1702 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1703 | pixman_renderer_output_set_buffer(&output->base, |
| 1704 | output->image[output->current_image]); |
Pekka Paalanen | acf50c3 | 2018-04-23 11:44:56 +0200 | [diff] [blame] | 1705 | pixman_renderer_output_set_hw_extra_damage(&output->base, |
| 1706 | &output->previous_damage); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1707 | |
Pekka Paalanen | acf50c3 | 2018-04-23 11:44:56 +0200 | [diff] [blame] | 1708 | ec->renderer->repaint_output(&output->base, damage); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1709 | |
Pekka Paalanen | acf50c3 | 2018-04-23 11:44:56 +0200 | [diff] [blame] | 1710 | pixman_region32_copy(&output->previous_damage, damage); |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1711 | |
| 1712 | return drm_fb_ref(output->dumb[output->current_image]); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1713 | } |
| 1714 | |
| 1715 | static void |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1716 | drm_output_render(struct drm_output_state *state, pixman_region32_t *damage) |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1717 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1718 | struct drm_output *output = state->output; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1719 | struct weston_compositor *c = output->base.compositor; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1720 | struct drm_plane_state *scanout_state; |
Daniel Stone | e95169b | 2016-11-14 17:46:59 +0000 | [diff] [blame] | 1721 | struct drm_plane *scanout_plane = output->scanout_plane; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1722 | struct drm_backend *b = to_drm_backend(c); |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1723 | struct drm_fb *fb; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1724 | |
Daniel Stone | 4e84f7d | 2017-04-04 17:54:29 +0100 | [diff] [blame] | 1725 | /* If we already have a client buffer promoted to scanout, then we don't |
| 1726 | * want to render. */ |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1727 | scanout_state = drm_output_state_get_plane(state, |
| 1728 | output->scanout_plane); |
| 1729 | if (scanout_state->fb) |
Daniel Stone | 4e84f7d | 2017-04-04 17:54:29 +0100 | [diff] [blame] | 1730 | return; |
| 1731 | |
Daniel Stone | e95169b | 2016-11-14 17:46:59 +0000 | [diff] [blame] | 1732 | if (!pixman_region32_not_empty(damage) && |
| 1733 | scanout_plane->state_cur->fb && |
| 1734 | (scanout_plane->state_cur->fb->type == BUFFER_GBM_SURFACE || |
| 1735 | scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB) && |
| 1736 | scanout_plane->state_cur->fb->width == |
| 1737 | output->base.current_mode->width && |
| 1738 | scanout_plane->state_cur->fb->height == |
| 1739 | output->base.current_mode->height) { |
| 1740 | fb = drm_fb_ref(scanout_plane->state_cur->fb); |
| 1741 | } else if (b->use_pixman) { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1742 | fb = drm_output_render_pixman(state, damage); |
Daniel Stone | e95169b | 2016-11-14 17:46:59 +0000 | [diff] [blame] | 1743 | } else { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1744 | fb = drm_output_render_gl(state, damage); |
Daniel Stone | e95169b | 2016-11-14 17:46:59 +0000 | [diff] [blame] | 1745 | } |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1746 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1747 | if (!fb) { |
| 1748 | drm_plane_state_put_back(scanout_state); |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1749 | return; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
| 1752 | scanout_state->fb = fb; |
| 1753 | scanout_state->output = output; |
| 1754 | |
| 1755 | scanout_state->src_x = 0; |
| 1756 | scanout_state->src_y = 0; |
| 1757 | scanout_state->src_w = output->base.current_mode->width << 16; |
| 1758 | scanout_state->src_h = output->base.current_mode->height << 16; |
| 1759 | |
| 1760 | scanout_state->dest_x = 0; |
| 1761 | scanout_state->dest_y = 0; |
| 1762 | scanout_state->dest_w = scanout_state->src_w >> 16; |
| 1763 | scanout_state->dest_h = scanout_state->src_h >> 16; |
| 1764 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1765 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1766 | pixman_region32_subtract(&c->primary_plane.damage, |
| 1767 | &c->primary_plane.damage, damage); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | static void |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 1771 | drm_output_set_gamma(struct weston_output *output_base, |
| 1772 | uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b) |
| 1773 | { |
| 1774 | int rc; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1775 | struct drm_output *output = to_drm_output(output_base); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1776 | struct drm_backend *backend = |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1777 | to_drm_backend(output->base.compositor); |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 1778 | |
| 1779 | /* check */ |
| 1780 | if (output_base->gamma_size != size) |
| 1781 | return; |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 1782 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1783 | rc = drmModeCrtcSetGamma(backend->drm.fd, |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 1784 | output->crtc_id, |
| 1785 | size, r, g, b); |
| 1786 | if (rc) |
| 1787 | weston_log("set gamma failed: %m\n"); |
| 1788 | } |
| 1789 | |
Bryce Harrington | ada4f07 | 2015-06-30 13:25:46 -0700 | [diff] [blame] | 1790 | /* Determine the type of vblank synchronization to use for the output. |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 1791 | * |
Bryce Harrington | ada4f07 | 2015-06-30 13:25:46 -0700 | [diff] [blame] | 1792 | * The pipe parameter indicates which CRTC is in use. Knowing this, we |
| 1793 | * can determine which vblank sequence type to use for it. Traditional |
| 1794 | * cards had only two CRTCs, with CRTC 0 using no special flags, and |
| 1795 | * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe |
| 1796 | * parameter indicates this. |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 1797 | * |
Bryce Harrington | ada4f07 | 2015-06-30 13:25:46 -0700 | [diff] [blame] | 1798 | * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between |
| 1799 | * 0-31. If this is non-zero it indicates we're dealing with a |
| 1800 | * multi-gpu situation and we need to calculate the vblank sync |
| 1801 | * using DRM_BLANK_HIGH_CRTC_MASK. |
| 1802 | */ |
Pekka Paalanen | c8a1ff0 | 2015-07-02 15:06:08 +0300 | [diff] [blame] | 1803 | static unsigned int |
| 1804 | drm_waitvblank_pipe(struct drm_output *output) |
Mario Kleiner | 2ab4f4e | 2015-06-21 21:25:13 +0200 | [diff] [blame] | 1805 | { |
| 1806 | if (output->pipe > 1) |
| 1807 | return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) & |
| 1808 | DRM_VBLANK_HIGH_CRTC_MASK; |
| 1809 | else if (output->pipe > 0) |
| 1810 | return DRM_VBLANK_SECONDARY; |
| 1811 | else |
| 1812 | return 0; |
| 1813 | } |
| 1814 | |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 1815 | static int |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 1816 | drm_output_apply_state_legacy(struct drm_output_state *state) |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1817 | { |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1818 | struct drm_output *output = state->output; |
| 1819 | struct drm_backend *backend = to_drm_backend(output->base.compositor); |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1820 | struct drm_plane *scanout_plane = output->scanout_plane; |
Pekka Paalanen | 02aeb5c | 2017-09-12 16:02:01 +0300 | [diff] [blame] | 1821 | struct drm_property_info *dpms_prop; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1822 | struct drm_plane_state *scanout_state; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1823 | struct drm_plane_state *ps; |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 1824 | struct drm_plane *p; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1825 | struct drm_mode *mode; |
Pekka Paalanen | 02aeb5c | 2017-09-12 16:02:01 +0300 | [diff] [blame] | 1826 | struct drm_head *head; |
| 1827 | uint32_t connectors[MAX_CLONED_CONNECTORS]; |
| 1828 | int n_conn = 0; |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1829 | struct timespec now; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1830 | int ret = 0; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1831 | |
Pekka Paalanen | 02aeb5c | 2017-09-12 16:02:01 +0300 | [diff] [blame] | 1832 | wl_list_for_each(head, &output->base.head_list, base.output_link) { |
| 1833 | assert(n_conn < MAX_CLONED_CONNECTORS); |
| 1834 | connectors[n_conn++] = head->connector_id; |
| 1835 | } |
| 1836 | |
Derek Foreman | 2cd87fe | 2017-04-13 13:48:48 -0500 | [diff] [blame] | 1837 | /* If disable_planes is set then assign_planes() wasn't |
| 1838 | * called for this render, so we could still have a stale |
| 1839 | * cursor plane set up. |
| 1840 | */ |
| 1841 | if (output->base.disable_planes) { |
| 1842 | output->cursor_view = NULL; |
Greg V | 1f78176 | 2018-02-19 17:59:42 +0300 | [diff] [blame] | 1843 | if (output->cursor_plane) { |
| 1844 | output->cursor_plane->base.x = INT32_MIN; |
| 1845 | output->cursor_plane->base.y = INT32_MIN; |
| 1846 | } |
Derek Foreman | 2cd87fe | 2017-04-13 13:48:48 -0500 | [diff] [blame] | 1847 | } |
| 1848 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1849 | if (state->dpms != WESTON_DPMS_ON) { |
| 1850 | wl_list_for_each(ps, &state->plane_list, link) { |
| 1851 | p = ps->plane; |
| 1852 | assert(ps->fb == NULL); |
| 1853 | assert(ps->output == NULL); |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1854 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1855 | if (p->type != WDRM_PLANE_TYPE_OVERLAY) |
| 1856 | continue; |
| 1857 | |
| 1858 | ret = drmModeSetPlane(backend->drm.fd, p->plane_id, |
| 1859 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
| 1860 | if (ret) |
| 1861 | weston_log("drmModeSetPlane failed disable: %m\n"); |
| 1862 | } |
| 1863 | |
| 1864 | if (output->cursor_plane) { |
| 1865 | ret = drmModeSetCursor(backend->drm.fd, output->crtc_id, |
| 1866 | 0, 0, 0); |
| 1867 | if (ret) |
| 1868 | weston_log("drmModeSetCursor failed disable: %m\n"); |
| 1869 | } |
| 1870 | |
| 1871 | ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id, 0, 0, 0, |
Pekka Paalanen | 02aeb5c | 2017-09-12 16:02:01 +0300 | [diff] [blame] | 1872 | NULL, 0, NULL); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1873 | if (ret) |
| 1874 | weston_log("drmModeSetCrtc failed disabling: %m\n"); |
| 1875 | |
| 1876 | drm_output_assign_state(state, DRM_STATE_APPLY_SYNC); |
| 1877 | weston_compositor_read_presentation_clock(output->base.compositor, &now); |
| 1878 | drm_output_update_complete(output, |
| 1879 | WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION, |
| 1880 | now.tv_sec, now.tv_nsec / 1000); |
| 1881 | |
| 1882 | return 0; |
| 1883 | } |
| 1884 | |
| 1885 | scanout_state = |
| 1886 | drm_output_state_get_existing_plane(state, scanout_plane); |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 1887 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1888 | /* The legacy SetCrtc API doesn't allow us to do scaling, and the |
| 1889 | * legacy PageFlip API doesn't allow us to do clipping either. */ |
| 1890 | assert(scanout_state->src_x == 0); |
| 1891 | assert(scanout_state->src_y == 0); |
| 1892 | assert(scanout_state->src_w == |
| 1893 | (unsigned) (output->base.current_mode->width << 16)); |
| 1894 | assert(scanout_state->src_h == |
| 1895 | (unsigned) (output->base.current_mode->height << 16)); |
| 1896 | assert(scanout_state->dest_x == 0); |
| 1897 | assert(scanout_state->dest_y == 0); |
| 1898 | assert(scanout_state->dest_w == scanout_state->src_w >> 16); |
| 1899 | assert(scanout_state->dest_h == scanout_state->src_h >> 16); |
| 1900 | |
Daniel Stone | cb04cc4 | 2016-11-16 11:51:27 +0000 | [diff] [blame] | 1901 | mode = to_drm_mode(output->base.current_mode); |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 1902 | if (backend->state_invalid || !scanout_plane->state_cur->fb || |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1903 | scanout_plane->state_cur->fb->stride != scanout_state->fb->stride) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1904 | ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id, |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1905 | scanout_state->fb->fb_id, |
| 1906 | 0, 0, |
Pekka Paalanen | 02aeb5c | 2017-09-12 16:02:01 +0300 | [diff] [blame] | 1907 | connectors, n_conn, |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1908 | &mode->mode_info); |
| 1909 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1910 | weston_log("set mode failed: %m\n"); |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1911 | goto err; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1912 | } |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 1913 | } |
| 1914 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1915 | if (drmModePageFlip(backend->drm.fd, output->crtc_id, |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1916 | scanout_state->fb->fb_id, |
Kristian Høgsberg | 54f14c3 | 2012-01-18 11:47:41 -0500 | [diff] [blame] | 1917 | DRM_MODE_PAGE_FLIP_EVENT, output) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1918 | weston_log("queueing pageflip failed: %m\n"); |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1919 | goto err; |
Kristian Høgsberg | 54f14c3 | 2012-01-18 11:47:41 -0500 | [diff] [blame] | 1920 | } |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 1921 | |
Daniel Stone | 205c0a0 | 2017-04-04 17:54:33 +0100 | [diff] [blame] | 1922 | assert(!output->page_flip_pending); |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 1923 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 1924 | if (output->pageflip_timer) |
| 1925 | wl_event_source_timer_update(output->pageflip_timer, |
| 1926 | backend->pageflip_timeout); |
| 1927 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 1928 | drm_output_set_cursor(state); |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 1929 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1930 | /* |
| 1931 | * Now, update all the sprite surfaces |
| 1932 | */ |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1933 | wl_list_for_each(ps, &state->plane_list, link) { |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1934 | uint32_t flags = 0, fb_id = 0; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1935 | drmVBlank vbl = { |
| 1936 | .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT, |
| 1937 | .request.sequence = 1, |
| 1938 | }; |
| 1939 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1940 | p = ps->plane; |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 1941 | if (p->type != WDRM_PLANE_TYPE_OVERLAY) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1942 | continue; |
| 1943 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1944 | assert(p->state_cur->complete); |
| 1945 | assert(!!p->state_cur->output == !!p->state_cur->fb); |
| 1946 | assert(!p->state_cur->output || p->state_cur->output == output); |
| 1947 | assert(!ps->complete); |
| 1948 | assert(!ps->output || ps->output == output); |
| 1949 | assert(!!ps->output == !!ps->fb); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1950 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1951 | if (ps->fb && !backend->sprites_hidden) |
| 1952 | fb_id = ps->fb->fb_id; |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 1953 | |
| 1954 | ret = drmModeSetPlane(backend->drm.fd, p->plane_id, |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1955 | output->crtc_id, fb_id, flags, |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1956 | ps->dest_x, ps->dest_y, |
| 1957 | ps->dest_w, ps->dest_h, |
| 1958 | ps->src_x, ps->src_y, |
| 1959 | ps->src_w, ps->src_h); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1960 | if (ret) |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1961 | weston_log("setplane failed: %d: %s\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1962 | ret, strerror(errno)); |
| 1963 | |
Mario Kleiner | 2ab4f4e | 2015-06-21 21:25:13 +0200 | [diff] [blame] | 1964 | vbl.request.type |= drm_waitvblank_pipe(output); |
Rob Clark | 5ca1a47 | 2012-08-08 20:27:37 -0500 | [diff] [blame] | 1965 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1966 | /* |
| 1967 | * Queue a vblank signal so we know when the surface |
| 1968 | * becomes active on the display or has been replaced. |
| 1969 | */ |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1970 | vbl.request.signal = (unsigned long) ps; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1971 | ret = drmWaitVBlank(backend->drm.fd, &vbl); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1972 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1973 | weston_log("vblank event request failed: %d: %s\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1974 | ret, strerror(errno)); |
| 1975 | } |
| 1976 | } |
| 1977 | |
Pekka Paalanen | 02aeb5c | 2017-09-12 16:02:01 +0300 | [diff] [blame] | 1978 | if (state->dpms != output->state_cur->dpms) { |
| 1979 | wl_list_for_each(head, &output->base.head_list, base.output_link) { |
| 1980 | dpms_prop = &head->props_conn[WDRM_CONNECTOR_DPMS]; |
| 1981 | if (dpms_prop->prop_id == 0) |
| 1982 | continue; |
| 1983 | |
| 1984 | ret = drmModeConnectorSetProperty(backend->drm.fd, |
| 1985 | head->connector_id, |
| 1986 | dpms_prop->prop_id, |
| 1987 | state->dpms); |
| 1988 | if (ret) { |
| 1989 | weston_log("DRM: DPMS: failed property set for %s\n", |
| 1990 | head->base.name); |
| 1991 | } |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1992 | } |
| 1993 | } |
| 1994 | |
| 1995 | drm_output_assign_state(state, DRM_STATE_APPLY_ASYNC); |
| 1996 | |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 1997 | return 0; |
| 1998 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1999 | err: |
Kristian Høgsberg | b3955b0 | 2014-01-23 16:25:06 -0800 | [diff] [blame] | 2000 | output->cursor_view = NULL; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2001 | drm_output_state_free(state); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2002 | return -1; |
| 2003 | } |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 2004 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2005 | #ifdef HAVE_DRM_ATOMIC |
| 2006 | static int |
| 2007 | crtc_add_prop(drmModeAtomicReq *req, struct drm_output *output, |
| 2008 | enum wdrm_crtc_property prop, uint64_t val) |
| 2009 | { |
| 2010 | struct drm_property_info *info = &output->props_crtc[prop]; |
| 2011 | int ret; |
| 2012 | |
| 2013 | if (info->prop_id == 0) |
| 2014 | return -1; |
| 2015 | |
| 2016 | ret = drmModeAtomicAddProperty(req, output->crtc_id, info->prop_id, |
| 2017 | val); |
| 2018 | return (ret <= 0) ? -1 : 0; |
| 2019 | } |
| 2020 | |
| 2021 | static int |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 2022 | connector_add_prop(drmModeAtomicReq *req, struct drm_head *head, |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2023 | enum wdrm_connector_property prop, uint64_t val) |
| 2024 | { |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 2025 | struct drm_property_info *info = &head->props_conn[prop]; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2026 | int ret; |
| 2027 | |
| 2028 | if (info->prop_id == 0) |
| 2029 | return -1; |
| 2030 | |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 2031 | ret = drmModeAtomicAddProperty(req, head->connector_id, |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2032 | info->prop_id, val); |
| 2033 | return (ret <= 0) ? -1 : 0; |
| 2034 | } |
| 2035 | |
| 2036 | static int |
| 2037 | plane_add_prop(drmModeAtomicReq *req, struct drm_plane *plane, |
| 2038 | enum wdrm_plane_property prop, uint64_t val) |
| 2039 | { |
| 2040 | struct drm_property_info *info = &plane->props[prop]; |
| 2041 | int ret; |
| 2042 | |
| 2043 | if (info->prop_id == 0) |
| 2044 | return -1; |
| 2045 | |
| 2046 | ret = drmModeAtomicAddProperty(req, plane->plane_id, info->prop_id, |
| 2047 | val); |
| 2048 | return (ret <= 0) ? -1 : 0; |
| 2049 | } |
| 2050 | |
| 2051 | static int |
| 2052 | drm_mode_ensure_blob(struct drm_backend *backend, struct drm_mode *mode) |
| 2053 | { |
| 2054 | int ret; |
| 2055 | |
| 2056 | if (mode->blob_id) |
| 2057 | return 0; |
| 2058 | |
| 2059 | ret = drmModeCreatePropertyBlob(backend->drm.fd, |
| 2060 | &mode->mode_info, |
| 2061 | sizeof(mode->mode_info), |
| 2062 | &mode->blob_id); |
| 2063 | if (ret != 0) |
| 2064 | weston_log("failed to create mode property blob: %m\n"); |
| 2065 | |
| 2066 | return ret; |
| 2067 | } |
| 2068 | |
| 2069 | static int |
| 2070 | drm_output_apply_state_atomic(struct drm_output_state *state, |
| 2071 | drmModeAtomicReq *req, |
| 2072 | uint32_t *flags) |
| 2073 | { |
| 2074 | struct drm_output *output = state->output; |
| 2075 | struct drm_backend *backend = to_drm_backend(output->base.compositor); |
| 2076 | struct drm_plane_state *plane_state; |
| 2077 | struct drm_mode *current_mode = to_drm_mode(output->base.current_mode); |
Pekka Paalanen | 2f66130 | 2017-09-12 16:07:32 +0300 | [diff] [blame] | 2078 | struct drm_head *head; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2079 | int ret = 0; |
| 2080 | |
| 2081 | if (state->dpms != output->state_cur->dpms) |
| 2082 | *flags |= DRM_MODE_ATOMIC_ALLOW_MODESET; |
| 2083 | |
| 2084 | if (state->dpms == WESTON_DPMS_ON) { |
| 2085 | ret = drm_mode_ensure_blob(backend, current_mode); |
| 2086 | if (ret != 0) |
| 2087 | return ret; |
| 2088 | |
| 2089 | ret |= crtc_add_prop(req, output, WDRM_CRTC_MODE_ID, |
| 2090 | current_mode->blob_id); |
| 2091 | ret |= crtc_add_prop(req, output, WDRM_CRTC_ACTIVE, 1); |
Pekka Paalanen | 2f66130 | 2017-09-12 16:07:32 +0300 | [diff] [blame] | 2092 | |
| 2093 | wl_list_for_each(head, &output->base.head_list, base.output_link) { |
| 2094 | ret |= connector_add_prop(req, head, WDRM_CONNECTOR_CRTC_ID, |
| 2095 | output->crtc_id); |
| 2096 | } |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2097 | } else { |
| 2098 | ret |= crtc_add_prop(req, output, WDRM_CRTC_MODE_ID, 0); |
| 2099 | ret |= crtc_add_prop(req, output, WDRM_CRTC_ACTIVE, 0); |
Pekka Paalanen | 2f66130 | 2017-09-12 16:07:32 +0300 | [diff] [blame] | 2100 | |
| 2101 | wl_list_for_each(head, &output->base.head_list, base.output_link) |
| 2102 | ret |= connector_add_prop(req, head, WDRM_CONNECTOR_CRTC_ID, 0); |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2103 | } |
| 2104 | |
| 2105 | if (ret != 0) { |
| 2106 | weston_log("couldn't set atomic CRTC/connector state\n"); |
| 2107 | return ret; |
| 2108 | } |
| 2109 | |
| 2110 | wl_list_for_each(plane_state, &state->plane_list, link) { |
| 2111 | struct drm_plane *plane = plane_state->plane; |
| 2112 | |
| 2113 | ret |= plane_add_prop(req, plane, WDRM_PLANE_FB_ID, |
| 2114 | plane_state->fb ? plane_state->fb->fb_id : 0); |
| 2115 | ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_ID, |
| 2116 | plane_state->fb ? output->crtc_id : 0); |
| 2117 | ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_X, |
| 2118 | plane_state->src_x); |
| 2119 | ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_Y, |
| 2120 | plane_state->src_y); |
| 2121 | ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_W, |
| 2122 | plane_state->src_w); |
| 2123 | ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_H, |
| 2124 | plane_state->src_h); |
| 2125 | ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_X, |
| 2126 | plane_state->dest_x); |
| 2127 | ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_Y, |
| 2128 | plane_state->dest_y); |
| 2129 | ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_W, |
| 2130 | plane_state->dest_w); |
| 2131 | ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_H, |
| 2132 | plane_state->dest_h); |
| 2133 | |
| 2134 | if (ret != 0) { |
| 2135 | weston_log("couldn't set plane state\n"); |
| 2136 | return ret; |
| 2137 | } |
| 2138 | } |
| 2139 | |
| 2140 | return 0; |
| 2141 | } |
| 2142 | |
| 2143 | /** |
| 2144 | * Helper function used only by drm_pending_state_apply, with the same |
| 2145 | * guarantees and constraints as that function. |
| 2146 | */ |
| 2147 | static int |
| 2148 | drm_pending_state_apply_atomic(struct drm_pending_state *pending_state, |
| 2149 | enum drm_state_apply_mode mode) |
| 2150 | { |
| 2151 | struct drm_backend *b = pending_state->backend; |
| 2152 | struct drm_output_state *output_state, *tmp; |
| 2153 | struct drm_plane *plane; |
| 2154 | drmModeAtomicReq *req = drmModeAtomicAlloc(); |
| 2155 | uint32_t flags = 0; |
| 2156 | int ret = 0; |
| 2157 | |
| 2158 | if (!req) |
| 2159 | return -1; |
| 2160 | |
| 2161 | if (b->state_invalid) { |
Pekka Paalanen | eacec81 | 2017-09-12 13:43:51 +0300 | [diff] [blame] | 2162 | struct weston_head *head_base; |
| 2163 | struct drm_head *head; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2164 | uint32_t *unused; |
| 2165 | int err; |
| 2166 | |
| 2167 | /* If we need to reset all our state (e.g. because we've |
| 2168 | * just started, or just been VT-switched in), explicitly |
| 2169 | * disable all the CRTCs and connectors we aren't using. */ |
Pekka Paalanen | eacec81 | 2017-09-12 13:43:51 +0300 | [diff] [blame] | 2170 | wl_list_for_each(head_base, |
| 2171 | &b->compositor->head_list, compositor_link) { |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2172 | struct drm_property_info *info; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2173 | |
Pekka Paalanen | eacec81 | 2017-09-12 13:43:51 +0300 | [diff] [blame] | 2174 | if (weston_head_is_enabled(head_base)) |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2175 | continue; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2176 | |
Pekka Paalanen | eacec81 | 2017-09-12 13:43:51 +0300 | [diff] [blame] | 2177 | head = to_drm_head(head_base); |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2178 | |
Pekka Paalanen | eacec81 | 2017-09-12 13:43:51 +0300 | [diff] [blame] | 2179 | info = &head->props_conn[WDRM_CONNECTOR_CRTC_ID]; |
| 2180 | err = drmModeAtomicAddProperty(req, head->connector_id, |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2181 | info->prop_id, 0); |
| 2182 | if (err <= 0) |
| 2183 | ret = -1; |
| 2184 | |
Pekka Paalanen | eacec81 | 2017-09-12 13:43:51 +0300 | [diff] [blame] | 2185 | info = &head->props_conn[WDRM_CONNECTOR_DPMS]; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2186 | if (info->prop_id > 0) |
Pekka Paalanen | eacec81 | 2017-09-12 13:43:51 +0300 | [diff] [blame] | 2187 | err = drmModeAtomicAddProperty(req, head->connector_id, |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2188 | info->prop_id, |
| 2189 | DRM_MODE_DPMS_OFF); |
| 2190 | if (err <= 0) |
| 2191 | ret = -1; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
| 2194 | wl_array_for_each(unused, &b->unused_crtcs) { |
| 2195 | struct drm_property_info infos[WDRM_CRTC__COUNT]; |
| 2196 | struct drm_property_info *info; |
| 2197 | drmModeObjectProperties *props; |
| 2198 | uint64_t active; |
| 2199 | |
| 2200 | memset(infos, 0, sizeof(infos)); |
| 2201 | |
| 2202 | /* We can't emit a disable on a CRTC that's already |
| 2203 | * off, as the kernel will refuse to generate an event |
| 2204 | * for an off->off state and fail the commit. |
| 2205 | */ |
| 2206 | props = drmModeObjectGetProperties(b->drm.fd, |
| 2207 | *unused, |
| 2208 | DRM_MODE_OBJECT_CRTC); |
| 2209 | if (!props) { |
| 2210 | ret = -1; |
| 2211 | continue; |
| 2212 | } |
| 2213 | |
| 2214 | drm_property_info_populate(b, crtc_props, infos, |
| 2215 | WDRM_CRTC__COUNT, |
| 2216 | props); |
| 2217 | |
| 2218 | info = &infos[WDRM_CRTC_ACTIVE]; |
| 2219 | active = drm_property_get_value(info, props, 0); |
| 2220 | drmModeFreeObjectProperties(props); |
| 2221 | if (active == 0) { |
| 2222 | drm_property_info_free(infos, WDRM_CRTC__COUNT); |
| 2223 | continue; |
| 2224 | } |
| 2225 | |
| 2226 | err = drmModeAtomicAddProperty(req, *unused, |
| 2227 | info->prop_id, 0); |
| 2228 | if (err <= 0) |
| 2229 | ret = -1; |
| 2230 | |
| 2231 | info = &infos[WDRM_CRTC_MODE_ID]; |
| 2232 | err = drmModeAtomicAddProperty(req, *unused, |
| 2233 | info->prop_id, 0); |
| 2234 | if (err <= 0) |
| 2235 | ret = -1; |
| 2236 | |
| 2237 | drm_property_info_free(infos, WDRM_CRTC__COUNT); |
| 2238 | } |
| 2239 | |
| 2240 | /* Disable all the planes; planes which are being used will |
| 2241 | * override this state in the output-state application. */ |
| 2242 | wl_list_for_each(plane, &b->plane_list, link) { |
| 2243 | plane_add_prop(req, plane, WDRM_PLANE_CRTC_ID, 0); |
| 2244 | plane_add_prop(req, plane, WDRM_PLANE_FB_ID, 0); |
| 2245 | } |
| 2246 | |
| 2247 | flags |= DRM_MODE_ATOMIC_ALLOW_MODESET; |
| 2248 | } |
| 2249 | |
| 2250 | wl_list_for_each(output_state, &pending_state->output_list, link) { |
| 2251 | if (mode == DRM_STATE_APPLY_SYNC) |
| 2252 | assert(output_state->dpms == WESTON_DPMS_OFF); |
| 2253 | ret |= drm_output_apply_state_atomic(output_state, req, &flags); |
| 2254 | } |
| 2255 | |
| 2256 | if (ret != 0) { |
| 2257 | weston_log("atomic: couldn't compile atomic state\n"); |
| 2258 | goto out; |
| 2259 | } |
| 2260 | |
| 2261 | switch (mode) { |
| 2262 | case DRM_STATE_APPLY_SYNC: |
| 2263 | break; |
| 2264 | case DRM_STATE_APPLY_ASYNC: |
| 2265 | flags |= DRM_MODE_PAGE_FLIP_EVENT | DRM_MODE_ATOMIC_NONBLOCK; |
| 2266 | break; |
| 2267 | } |
| 2268 | |
| 2269 | ret = drmModeAtomicCommit(b->drm.fd, req, flags, b); |
| 2270 | if (ret != 0) { |
| 2271 | weston_log("atomic: couldn't commit new state: %m\n"); |
| 2272 | goto out; |
| 2273 | } |
| 2274 | |
| 2275 | wl_list_for_each_safe(output_state, tmp, &pending_state->output_list, |
| 2276 | link) |
| 2277 | drm_output_assign_state(output_state, mode); |
| 2278 | |
| 2279 | b->state_invalid = false; |
| 2280 | |
| 2281 | assert(wl_list_empty(&pending_state->output_list)); |
| 2282 | |
| 2283 | out: |
| 2284 | drmModeAtomicFree(req); |
| 2285 | drm_pending_state_free(pending_state); |
| 2286 | return ret; |
| 2287 | } |
| 2288 | #endif |
| 2289 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2290 | /** |
| 2291 | * Applies all of a pending_state asynchronously: the primary entry point for |
| 2292 | * applying KMS state to a device. Updates the state for all outputs in the |
| 2293 | * pending_state, as well as disabling any unclaimed outputs. |
| 2294 | * |
| 2295 | * Unconditionally takes ownership of pending_state, and clears state_invalid. |
| 2296 | */ |
| 2297 | static int |
| 2298 | drm_pending_state_apply(struct drm_pending_state *pending_state) |
| 2299 | { |
| 2300 | struct drm_backend *b = pending_state->backend; |
| 2301 | struct drm_output_state *output_state, *tmp; |
| 2302 | uint32_t *unused; |
| 2303 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2304 | #ifdef HAVE_DRM_ATOMIC |
| 2305 | if (b->atomic_modeset) |
| 2306 | return drm_pending_state_apply_atomic(pending_state, |
| 2307 | DRM_STATE_APPLY_ASYNC); |
| 2308 | #endif |
| 2309 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2310 | if (b->state_invalid) { |
| 2311 | /* If we need to reset all our state (e.g. because we've |
| 2312 | * just started, or just been VT-switched in), explicitly |
| 2313 | * disable all the CRTCs we aren't using. This also disables |
| 2314 | * all connectors on these CRTCs, so we don't need to do that |
| 2315 | * separately with the pre-atomic API. */ |
| 2316 | wl_array_for_each(unused, &b->unused_crtcs) |
| 2317 | drmModeSetCrtc(b->drm.fd, *unused, 0, 0, 0, NULL, 0, |
| 2318 | NULL); |
| 2319 | } |
| 2320 | |
| 2321 | wl_list_for_each_safe(output_state, tmp, &pending_state->output_list, |
| 2322 | link) { |
| 2323 | struct drm_output *output = output_state->output; |
| 2324 | int ret; |
| 2325 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2326 | ret = drm_output_apply_state_legacy(output_state); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2327 | if (ret != 0) { |
| 2328 | weston_log("Couldn't apply state for output %s\n", |
| 2329 | output->base.name); |
| 2330 | } |
| 2331 | } |
| 2332 | |
| 2333 | b->state_invalid = false; |
| 2334 | |
| 2335 | assert(wl_list_empty(&pending_state->output_list)); |
| 2336 | |
| 2337 | drm_pending_state_free(pending_state); |
| 2338 | |
| 2339 | return 0; |
| 2340 | } |
| 2341 | |
| 2342 | /** |
| 2343 | * The synchronous version of drm_pending_state_apply. May only be used to |
| 2344 | * disable outputs. Does so synchronously: the request is guaranteed to have |
| 2345 | * completed on return, and the output will not be touched afterwards. |
| 2346 | * |
| 2347 | * Unconditionally takes ownership of pending_state, and clears state_invalid. |
| 2348 | */ |
| 2349 | static int |
| 2350 | drm_pending_state_apply_sync(struct drm_pending_state *pending_state) |
| 2351 | { |
| 2352 | struct drm_backend *b = pending_state->backend; |
| 2353 | struct drm_output_state *output_state, *tmp; |
| 2354 | uint32_t *unused; |
| 2355 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2356 | #ifdef HAVE_DRM_ATOMIC |
| 2357 | if (b->atomic_modeset) |
| 2358 | return drm_pending_state_apply_atomic(pending_state, |
| 2359 | DRM_STATE_APPLY_SYNC); |
| 2360 | #endif |
| 2361 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2362 | if (b->state_invalid) { |
| 2363 | /* If we need to reset all our state (e.g. because we've |
| 2364 | * just started, or just been VT-switched in), explicitly |
| 2365 | * disable all the CRTCs we aren't using. This also disables |
| 2366 | * all connectors on these CRTCs, so we don't need to do that |
| 2367 | * separately with the pre-atomic API. */ |
| 2368 | wl_array_for_each(unused, &b->unused_crtcs) |
| 2369 | drmModeSetCrtc(b->drm.fd, *unused, 0, 0, 0, NULL, 0, |
| 2370 | NULL); |
| 2371 | } |
| 2372 | |
| 2373 | wl_list_for_each_safe(output_state, tmp, &pending_state->output_list, |
| 2374 | link) { |
| 2375 | int ret; |
| 2376 | |
| 2377 | assert(output_state->dpms == WESTON_DPMS_OFF); |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2378 | ret = drm_output_apply_state_legacy(output_state); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2379 | if (ret != 0) { |
| 2380 | weston_log("Couldn't apply state for output %s\n", |
| 2381 | output_state->output->base.name); |
| 2382 | } |
| 2383 | } |
| 2384 | |
| 2385 | b->state_invalid = false; |
| 2386 | |
| 2387 | assert(wl_list_empty(&pending_state->output_list)); |
| 2388 | |
| 2389 | drm_pending_state_free(pending_state); |
| 2390 | |
| 2391 | return 0; |
| 2392 | } |
| 2393 | |
| 2394 | static int |
| 2395 | drm_output_repaint(struct weston_output *output_base, |
| 2396 | pixman_region32_t *damage, |
| 2397 | void *repaint_data) |
| 2398 | { |
| 2399 | struct drm_pending_state *pending_state = repaint_data; |
| 2400 | struct drm_output *output = to_drm_output(output_base); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2401 | struct drm_output_state *state = NULL; |
| 2402 | struct drm_plane_state *scanout_state; |
| 2403 | |
| 2404 | if (output->disable_pending || output->destroy_pending) |
| 2405 | goto err; |
| 2406 | |
| 2407 | assert(!output->state_last); |
| 2408 | |
| 2409 | /* If planes have been disabled in the core, we might not have |
| 2410 | * hit assign_planes at all, so might not have valid output state |
| 2411 | * here. */ |
| 2412 | state = drm_pending_state_get_output(pending_state, output); |
| 2413 | if (!state) |
| 2414 | state = drm_output_state_duplicate(output->state_cur, |
| 2415 | pending_state, |
| 2416 | DRM_OUTPUT_STATE_CLEAR_PLANES); |
| 2417 | state->dpms = WESTON_DPMS_ON; |
| 2418 | |
| 2419 | drm_output_render(state, damage); |
| 2420 | scanout_state = drm_output_state_get_plane(state, |
| 2421 | output->scanout_plane); |
| 2422 | if (!scanout_state || !scanout_state->fb) |
| 2423 | goto err; |
| 2424 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2425 | return 0; |
| 2426 | |
| 2427 | err: |
| 2428 | drm_output_state_free(state); |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 2429 | return -1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2430 | } |
| 2431 | |
| 2432 | static void |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2433 | drm_output_start_repaint_loop(struct weston_output *output_base) |
| 2434 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2435 | struct drm_output *output = to_drm_output(output_base); |
Daniel Stone | 8747f95 | 2016-11-29 20:17:32 +0000 | [diff] [blame] | 2436 | struct drm_pending_state *pending_state; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 2437 | struct drm_plane *scanout_plane = output->scanout_plane; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2438 | struct drm_backend *backend = |
| 2439 | to_drm_backend(output_base->compositor); |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 2440 | struct timespec ts, tnow; |
| 2441 | struct timespec vbl2now; |
| 2442 | int64_t refresh_nsec; |
| 2443 | int ret; |
| 2444 | drmVBlank vbl = { |
| 2445 | .request.type = DRM_VBLANK_RELATIVE, |
| 2446 | .request.sequence = 0, |
| 2447 | .request.signal = 0, |
| 2448 | }; |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 2449 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2450 | if (output->disable_pending || output->destroy_pending) |
Xiong Zhang | abd5d47 | 2013-10-11 14:43:07 +0800 | [diff] [blame] | 2451 | return; |
| 2452 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 2453 | if (!output->scanout_plane->state_cur->fb) { |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 2454 | /* We can't page flip if there's no mode set */ |
David Herrmann | 3c688c5 | 2013-10-22 17:11:25 +0200 | [diff] [blame] | 2455 | goto finish_frame; |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 2456 | } |
| 2457 | |
Pekka Paalanen | 6b65d8f | 2017-07-27 13:44:32 +0300 | [diff] [blame] | 2458 | /* Need to smash all state in from scratch; current timings might not |
| 2459 | * be what we want, page flip might not work, etc. |
| 2460 | */ |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 2461 | if (backend->state_invalid) |
Pekka Paalanen | 6b65d8f | 2017-07-27 13:44:32 +0300 | [diff] [blame] | 2462 | goto finish_frame; |
| 2463 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 2464 | assert(scanout_plane->state_cur->output == output); |
| 2465 | |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 2466 | /* Try to get current msc and timestamp via instant query */ |
| 2467 | vbl.request.type |= drm_waitvblank_pipe(output); |
| 2468 | ret = drmWaitVBlank(backend->drm.fd, &vbl); |
| 2469 | |
| 2470 | /* Error ret or zero timestamp means failure to get valid timestamp */ |
| 2471 | if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) { |
| 2472 | ts.tv_sec = vbl.reply.tval_sec; |
| 2473 | ts.tv_nsec = vbl.reply.tval_usec * 1000; |
| 2474 | |
| 2475 | /* Valid timestamp for most recent vblank - not stale? |
| 2476 | * Stale ts could happen on Linux 3.17+, so make sure it |
| 2477 | * is not older than 1 refresh duration since now. |
| 2478 | */ |
| 2479 | weston_compositor_read_presentation_clock(backend->compositor, |
| 2480 | &tnow); |
| 2481 | timespec_sub(&vbl2now, &tnow, &ts); |
| 2482 | refresh_nsec = |
| 2483 | millihz_to_nsec(output->base.current_mode->refresh); |
| 2484 | if (timespec_to_nsec(&vbl2now) < refresh_nsec) { |
| 2485 | drm_output_update_msc(output, vbl.reply.sequence); |
| 2486 | weston_output_finish_frame(output_base, &ts, |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 2487 | WP_PRESENTATION_FEEDBACK_INVALID); |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 2488 | return; |
| 2489 | } |
| 2490 | } |
| 2491 | |
| 2492 | /* Immediate query didn't provide valid timestamp. |
| 2493 | * Use pageflip fallback. |
| 2494 | */ |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2495 | |
Daniel Stone | 205c0a0 | 2017-04-04 17:54:33 +0100 | [diff] [blame] | 2496 | assert(!output->page_flip_pending); |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2497 | assert(!output->state_last); |
| 2498 | |
| 2499 | pending_state = drm_pending_state_alloc(backend); |
Daniel Stone | 8747f95 | 2016-11-29 20:17:32 +0000 | [diff] [blame] | 2500 | drm_output_state_duplicate(output->state_cur, pending_state, |
| 2501 | DRM_OUTPUT_STATE_PRESERVE_PLANES); |
Daniel Stone | 205c0a0 | 2017-04-04 17:54:33 +0100 | [diff] [blame] | 2502 | |
Daniel Stone | 8747f95 | 2016-11-29 20:17:32 +0000 | [diff] [blame] | 2503 | ret = drm_pending_state_apply(pending_state); |
| 2504 | if (ret != 0) { |
| 2505 | weston_log("applying repaint-start state failed: %m\n"); |
David Herrmann | 3c688c5 | 2013-10-22 17:11:25 +0200 | [diff] [blame] | 2506 | goto finish_frame; |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2507 | } |
David Herrmann | 3c688c5 | 2013-10-22 17:11:25 +0200 | [diff] [blame] | 2508 | |
| 2509 | return; |
| 2510 | |
| 2511 | finish_frame: |
| 2512 | /* if we cannot page-flip, immediately finish frame */ |
Daniel Stone | 3615ce1 | 2017-03-01 11:34:05 +0000 | [diff] [blame] | 2513 | weston_output_finish_frame(output_base, NULL, |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 2514 | WP_PRESENTATION_FEEDBACK_INVALID); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2515 | } |
| 2516 | |
| 2517 | static void |
Pekka Paalanen | 641307c | 2014-09-23 22:08:47 -0400 | [diff] [blame] | 2518 | drm_output_update_msc(struct drm_output *output, unsigned int seq) |
| 2519 | { |
| 2520 | uint64_t msc_hi = output->base.msc >> 32; |
| 2521 | |
| 2522 | if (seq < (output->base.msc & 0xffffffff)) |
| 2523 | msc_hi++; |
| 2524 | |
| 2525 | output->base.msc = (msc_hi << 32) + seq; |
| 2526 | } |
| 2527 | |
| 2528 | static void |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2529 | vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec, |
| 2530 | void *data) |
| 2531 | { |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2532 | struct drm_plane_state *ps = (struct drm_plane_state *) data; |
| 2533 | struct drm_output_state *os = ps->output_state; |
| 2534 | struct drm_output *output = os->output; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2535 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 2536 | uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION | |
| 2537 | WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK; |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 2538 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2539 | assert(!b->atomic_modeset); |
| 2540 | |
Pekka Paalanen | 641307c | 2014-09-23 22:08:47 -0400 | [diff] [blame] | 2541 | drm_output_update_msc(output, frame); |
Daniel Stone | 65d87d0 | 2017-04-04 17:54:32 +0100 | [diff] [blame] | 2542 | output->vblank_pending--; |
| 2543 | assert(output->vblank_pending >= 0); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2544 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2545 | assert(ps->fb); |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 2546 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2547 | if (output->page_flip_pending || output->vblank_pending) |
| 2548 | return; |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 2549 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2550 | drm_output_update_complete(output, flags, sec, usec); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2551 | } |
| 2552 | |
| 2553 | static void |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2554 | page_flip_handler(int fd, unsigned int frame, |
| 2555 | unsigned int sec, unsigned int usec, void *data) |
| 2556 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2557 | struct drm_output *output = data; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2558 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 2559 | uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC | |
| 2560 | WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION | |
| 2561 | WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2562 | |
Pekka Paalanen | 641307c | 2014-09-23 22:08:47 -0400 | [diff] [blame] | 2563 | drm_output_update_msc(output, frame); |
| 2564 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2565 | assert(!b->atomic_modeset); |
Daniel Stone | 205c0a0 | 2017-04-04 17:54:33 +0100 | [diff] [blame] | 2566 | assert(output->page_flip_pending); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2567 | output->page_flip_pending = 0; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 2568 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2569 | if (output->vblank_pending) |
| 2570 | return; |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 2571 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2572 | drm_output_update_complete(output, flags, sec, usec); |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 2573 | } |
| 2574 | |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 2575 | /** |
| 2576 | * Begin a new repaint cycle |
| 2577 | * |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2578 | * Called by the core compositor at the beginning of a repaint cycle. Creates |
| 2579 | * a new pending_state structure to own any output state created by individual |
| 2580 | * output repaint functions until the repaint is flushed or cancelled. |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 2581 | */ |
| 2582 | static void * |
| 2583 | drm_repaint_begin(struct weston_compositor *compositor) |
| 2584 | { |
| 2585 | struct drm_backend *b = to_drm_backend(compositor); |
| 2586 | struct drm_pending_state *ret; |
| 2587 | |
| 2588 | ret = drm_pending_state_alloc(b); |
| 2589 | b->repaint_data = ret; |
| 2590 | |
| 2591 | return ret; |
| 2592 | } |
| 2593 | |
| 2594 | /** |
| 2595 | * Flush a repaint set |
| 2596 | * |
| 2597 | * Called by the core compositor when a repaint cycle has been completed |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2598 | * and should be flushed. Frees the pending state, transitioning ownership |
| 2599 | * of the output state from the pending state, to the update itself. When |
| 2600 | * the update completes (see drm_output_update_complete), the output |
| 2601 | * state will be freed. |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 2602 | */ |
| 2603 | static void |
| 2604 | drm_repaint_flush(struct weston_compositor *compositor, void *repaint_data) |
| 2605 | { |
| 2606 | struct drm_backend *b = to_drm_backend(compositor); |
| 2607 | struct drm_pending_state *pending_state = repaint_data; |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 2608 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2609 | drm_pending_state_apply(pending_state); |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 2610 | b->repaint_data = NULL; |
| 2611 | } |
| 2612 | |
| 2613 | /** |
| 2614 | * Cancel a repaint set |
| 2615 | * |
| 2616 | * Called by the core compositor when a repaint has finished, so the data |
| 2617 | * held across the repaint cycle should be discarded. |
| 2618 | */ |
| 2619 | static void |
| 2620 | drm_repaint_cancel(struct weston_compositor *compositor, void *repaint_data) |
| 2621 | { |
| 2622 | struct drm_backend *b = to_drm_backend(compositor); |
| 2623 | struct drm_pending_state *pending_state = repaint_data; |
| 2624 | |
| 2625 | drm_pending_state_free(pending_state); |
| 2626 | b->repaint_data = NULL; |
| 2627 | } |
| 2628 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2629 | #ifdef HAVE_DRM_ATOMIC |
| 2630 | static void |
| 2631 | atomic_flip_handler(int fd, unsigned int frame, unsigned int sec, |
| 2632 | unsigned int usec, unsigned int crtc_id, void *data) |
| 2633 | { |
| 2634 | struct drm_backend *b = data; |
| 2635 | struct drm_output *output = drm_output_find_by_crtc(b, crtc_id); |
| 2636 | uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC | |
| 2637 | WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION | |
| 2638 | WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK; |
| 2639 | |
| 2640 | /* During the initial modeset, we can disable CRTCs which we don't |
| 2641 | * actually handle during normal operation; this will give us events |
| 2642 | * for unknown outputs. Ignore them. */ |
| 2643 | if (!output || !output->base.enabled) |
| 2644 | return; |
| 2645 | |
| 2646 | drm_output_update_msc(output, frame); |
| 2647 | |
| 2648 | assert(b->atomic_modeset); |
| 2649 | assert(output->atomic_complete_pending); |
| 2650 | output->atomic_complete_pending = 0; |
| 2651 | |
| 2652 | drm_output_update_complete(output, flags, sec, usec); |
| 2653 | } |
| 2654 | #endif |
| 2655 | |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 2656 | static uint32_t |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2657 | drm_output_check_plane_format(struct drm_plane *p, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2658 | struct weston_view *ev, struct gbm_bo *bo) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2659 | { |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 2660 | uint32_t i, format; |
| 2661 | |
| 2662 | format = gbm_bo_get_format(bo); |
| 2663 | |
| 2664 | if (format == GBM_FORMAT_ARGB8888) { |
| 2665 | pixman_region32_t r; |
| 2666 | |
Kristian Høgsberg | 63093a3 | 2013-03-01 14:29:16 -0500 | [diff] [blame] | 2667 | pixman_region32_init_rect(&r, 0, 0, |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 2668 | ev->surface->width, |
| 2669 | ev->surface->height); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2670 | pixman_region32_subtract(&r, &r, &ev->surface->opaque); |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 2671 | |
| 2672 | if (!pixman_region32_not_empty(&r)) |
| 2673 | format = GBM_FORMAT_XRGB8888; |
| 2674 | |
| 2675 | pixman_region32_fini(&r); |
| 2676 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2677 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2678 | for (i = 0; i < p->count_formats; i++) |
| 2679 | if (p->formats[i] == format) |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 2680 | return format; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2681 | |
| 2682 | return 0; |
| 2683 | } |
| 2684 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 2685 | static struct weston_plane * |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2686 | drm_output_prepare_overlay_view(struct drm_output_state *output_state, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2687 | struct weston_view *ev) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2688 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2689 | struct drm_output *output = output_state->output; |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 2690 | struct weston_compositor *ec = output->base.compositor; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2691 | struct drm_backend *b = to_drm_backend(ec); |
Pekka Paalanen | 952b6c8 | 2014-03-14 14:38:15 +0200 | [diff] [blame] | 2692 | struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport; |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 2693 | struct wl_resource *buffer_resource; |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2694 | struct drm_plane *p; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2695 | struct drm_plane_state *state = NULL; |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 2696 | struct linux_dmabuf_buffer *dmabuf; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2697 | struct gbm_bo *bo; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2698 | pixman_region32_t dest_rect, src_rect; |
Ander Conselvan de Oliveira | 409eebf | 2012-12-05 15:14:04 +0200 | [diff] [blame] | 2699 | pixman_box32_t *box, tbox; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2700 | uint32_t format; |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 2701 | wl_fixed_t sx1, sy1, sx2, sy2; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2702 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2703 | if (b->sprites_are_broken) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 2704 | return NULL; |
Kristian Høgsberg | 65bec24 | 2012-03-05 19:57:35 -0500 | [diff] [blame] | 2705 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 2706 | /* Don't import buffers which span multiple outputs. */ |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 2707 | if (ev->output_mask != (1u << output->base.id)) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 2708 | return NULL; |
Ander Conselvan de Oliveira | d450b19 | 2012-06-26 17:09:12 +0300 | [diff] [blame] | 2709 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 2710 | /* We can only import GBM buffers. */ |
| 2711 | if (b->gbm == NULL) |
| 2712 | return NULL; |
| 2713 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2714 | if (ev->surface->buffer_ref.buffer == NULL) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 2715 | return NULL; |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 2716 | buffer_resource = ev->surface->buffer_ref.buffer->resource; |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 2717 | if (wl_shm_buffer_get(buffer_resource)) |
Rob Clark | 702ffae | 2012-08-09 14:18:27 -0500 | [diff] [blame] | 2718 | return NULL; |
| 2719 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 2720 | if (viewport->buffer.transform != output->base.transform) |
| 2721 | return NULL; |
| 2722 | if (viewport->buffer.scale != output->base.current_scale) |
| 2723 | return NULL; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2724 | if (!drm_view_transform_supported(ev)) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 2725 | return NULL; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2726 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 2727 | if (ev->alpha != 1.0f) |
| 2728 | return NULL; |
| 2729 | |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 2730 | wl_list_for_each(p, &b->plane_list, link) { |
| 2731 | if (p->type != WDRM_PLANE_TYPE_OVERLAY) |
| 2732 | continue; |
| 2733 | |
Daniel Stone | 5ff289a | 2017-10-07 12:59:02 +0100 | [diff] [blame] | 2734 | if (!drm_plane_is_available(p, output)) |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2735 | continue; |
| 2736 | |
| 2737 | state = drm_output_state_get_plane(output_state, p); |
| 2738 | if (state->fb) { |
| 2739 | state = NULL; |
| 2740 | continue; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2741 | } |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2742 | |
| 2743 | break; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2744 | } |
| 2745 | |
| 2746 | /* No sprites available */ |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2747 | if (!state) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 2748 | return NULL; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2749 | |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 2750 | if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) { |
Bryce Harrington | a358207 | 2015-08-14 12:23:13 -0700 | [diff] [blame] | 2751 | #ifdef HAVE_GBM_FD_IMPORT |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 2752 | /* XXX: TODO: |
| 2753 | * |
| 2754 | * Use AddFB2 directly, do not go via GBM. |
| 2755 | * Add support for multiplanar formats. |
| 2756 | * Both require refactoring in the DRM-backend to |
| 2757 | * support a mix of gbm_bos and drmfbs. |
| 2758 | */ |
| 2759 | struct gbm_import_fd_data gbm_dmabuf = { |
Emmanuel Gil Peyrot | c399692 | 2015-11-24 19:28:24 +0000 | [diff] [blame] | 2760 | .fd = dmabuf->attributes.fd[0], |
| 2761 | .width = dmabuf->attributes.width, |
| 2762 | .height = dmabuf->attributes.height, |
| 2763 | .stride = dmabuf->attributes.stride[0], |
| 2764 | .format = dmabuf->attributes.format |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 2765 | }; |
| 2766 | |
Micah Fedke | c889012 | 2017-02-01 15:28:23 -0500 | [diff] [blame] | 2767 | /* XXX: TODO: |
| 2768 | * |
| 2769 | * Currently the buffer is rejected if any dmabuf attribute |
| 2770 | * flag is set. This keeps us from passing an inverted / |
| 2771 | * interlaced / bottom-first buffer (or any other type that may |
| 2772 | * be added in the future) through to an overlay. Ultimately, |
| 2773 | * these types of buffers should be handled through buffer |
| 2774 | * transforms and not as spot-checks requiring specific |
| 2775 | * knowledge. */ |
| 2776 | if (dmabuf->attributes.n_planes != 1 || |
| 2777 | dmabuf->attributes.offset[0] != 0 || |
| 2778 | dmabuf->attributes.flags) |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 2779 | return NULL; |
| 2780 | |
| 2781 | bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf, |
| 2782 | GBM_BO_USE_SCANOUT); |
Bryce Harrington | a358207 | 2015-08-14 12:23:13 -0700 | [diff] [blame] | 2783 | #else |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2784 | goto err; |
Bryce Harrington | a358207 | 2015-08-14 12:23:13 -0700 | [diff] [blame] | 2785 | #endif |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 2786 | } else { |
| 2787 | bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER, |
| 2788 | buffer_resource, GBM_BO_USE_SCANOUT); |
| 2789 | } |
Kristian Høgsberg | 2763a2e | 2012-07-13 22:54:43 -0400 | [diff] [blame] | 2790 | if (!bo) |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2791 | goto err; |
Kristian Høgsberg | 2763a2e | 2012-07-13 22:54:43 -0400 | [diff] [blame] | 2792 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2793 | format = drm_output_check_plane_format(p, ev, bo); |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2794 | if (format == 0) |
| 2795 | goto err; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2796 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2797 | state->fb = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT); |
| 2798 | if (!state->fb) |
| 2799 | goto err; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 2800 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2801 | drm_fb_set_buffer(state->fb, ev->surface->buffer_ref.buffer); |
| 2802 | |
| 2803 | state->output = output; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2804 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2805 | box = pixman_region32_extents(&ev->transform.boundingbox); |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2806 | p->base.x = box->x1; |
| 2807 | p->base.y = box->y1; |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 2808 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2809 | /* |
| 2810 | * Calculate the source & dest rects properly based on actual |
Derek Foreman | 4b1a0a1 | 2014-09-10 15:37:33 -0500 | [diff] [blame] | 2811 | * position (note the caller has called weston_view_update_transform() |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2812 | * for us already). |
| 2813 | */ |
| 2814 | pixman_region32_init(&dest_rect); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2815 | pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox, |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 2816 | &output->base.region); |
| 2817 | pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2818 | box = pixman_region32_extents(&dest_rect); |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 2819 | tbox = weston_transformed_rect(output->base.width, |
| 2820 | output->base.height, |
| 2821 | output->base.transform, |
| 2822 | output->base.current_scale, |
Alexander Larsson | d9a7bb7 | 2013-05-22 14:41:39 +0200 | [diff] [blame] | 2823 | *box); |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2824 | state->dest_x = tbox.x1; |
| 2825 | state->dest_y = tbox.y1; |
| 2826 | state->dest_w = tbox.x2 - tbox.x1; |
| 2827 | state->dest_h = tbox.y2 - tbox.y1; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2828 | pixman_region32_fini(&dest_rect); |
| 2829 | |
| 2830 | pixman_region32_init(&src_rect); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2831 | pixman_region32_intersect(&src_rect, &ev->transform.boundingbox, |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 2832 | &output->base.region); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2833 | box = pixman_region32_extents(&src_rect); |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 2834 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2835 | weston_view_from_global_fixed(ev, |
| 2836 | wl_fixed_from_int(box->x1), |
| 2837 | wl_fixed_from_int(box->y1), |
| 2838 | &sx1, &sy1); |
| 2839 | weston_view_from_global_fixed(ev, |
| 2840 | wl_fixed_from_int(box->x2), |
| 2841 | wl_fixed_from_int(box->y2), |
| 2842 | &sx2, &sy2); |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 2843 | |
| 2844 | if (sx1 < 0) |
| 2845 | sx1 = 0; |
| 2846 | if (sy1 < 0) |
| 2847 | sy1 = 0; |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 2848 | if (sx2 > wl_fixed_from_int(ev->surface->width)) |
| 2849 | sx2 = wl_fixed_from_int(ev->surface->width); |
| 2850 | if (sy2 > wl_fixed_from_int(ev->surface->height)) |
| 2851 | sy2 = wl_fixed_from_int(ev->surface->height); |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 2852 | |
Ander Conselvan de Oliveira | 409eebf | 2012-12-05 15:14:04 +0200 | [diff] [blame] | 2853 | tbox.x1 = sx1; |
| 2854 | tbox.y1 = sy1; |
| 2855 | tbox.x2 = sx2; |
| 2856 | tbox.y2 = sy2; |
| 2857 | |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 2858 | tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width), |
| 2859 | wl_fixed_from_int(ev->surface->height), |
Pekka Paalanen | 952b6c8 | 2014-03-14 14:38:15 +0200 | [diff] [blame] | 2860 | viewport->buffer.transform, |
| 2861 | viewport->buffer.scale, |
Pekka Paalanen | 1fd9c0f | 2013-11-26 18:19:41 +0100 | [diff] [blame] | 2862 | tbox); |
Ander Conselvan de Oliveira | 409eebf | 2012-12-05 15:14:04 +0200 | [diff] [blame] | 2863 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2864 | state->src_x = tbox.x1 << 8; |
| 2865 | state->src_y = tbox.y1 << 8; |
| 2866 | state->src_w = (tbox.x2 - tbox.x1) << 8; |
| 2867 | state->src_h = (tbox.y2 - tbox.y1) << 8; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2868 | pixman_region32_fini(&src_rect); |
| 2869 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2870 | return &p->base; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2871 | |
| 2872 | err: |
| 2873 | drm_plane_state_put_back(state); |
| 2874 | if (bo) |
| 2875 | gbm_bo_destroy(bo); |
| 2876 | return NULL; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2877 | } |
| 2878 | |
Pekka Paalanen | d0ead48 | 2014-06-16 12:05:40 +0300 | [diff] [blame] | 2879 | /** |
| 2880 | * Update the image for the current cursor surface |
| 2881 | * |
| 2882 | * @param b DRM backend structure |
| 2883 | * @param bo GBM buffer object to write into |
| 2884 | * @param ev View to use for cursor image |
| 2885 | */ |
| 2886 | static void |
| 2887 | cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo, |
| 2888 | struct weston_view *ev) |
| 2889 | { |
| 2890 | struct weston_buffer *buffer = ev->surface->buffer_ref.buffer; |
| 2891 | uint32_t buf[b->cursor_width * b->cursor_height]; |
| 2892 | int32_t stride; |
| 2893 | uint8_t *s; |
| 2894 | int i; |
| 2895 | |
| 2896 | assert(buffer && buffer->shm_buffer); |
| 2897 | assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource)); |
| 2898 | assert(ev->surface->width <= b->cursor_width); |
| 2899 | assert(ev->surface->height <= b->cursor_height); |
| 2900 | |
| 2901 | memset(buf, 0, sizeof buf); |
| 2902 | stride = wl_shm_buffer_get_stride(buffer->shm_buffer); |
| 2903 | s = wl_shm_buffer_get_data(buffer->shm_buffer); |
| 2904 | |
| 2905 | wl_shm_buffer_begin_access(buffer->shm_buffer); |
| 2906 | for (i = 0; i < ev->surface->height; i++) |
| 2907 | memcpy(buf + i * b->cursor_width, |
| 2908 | s + i * stride, |
| 2909 | ev->surface->width * 4); |
| 2910 | wl_shm_buffer_end_access(buffer->shm_buffer); |
| 2911 | |
| 2912 | if (gbm_bo_write(bo, buf, sizeof buf) < 0) |
| 2913 | weston_log("failed update cursor: %m\n"); |
| 2914 | } |
| 2915 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 2916 | static struct weston_plane * |
| 2917 | drm_output_prepare_cursor_view(struct drm_output_state *output_state, |
| 2918 | struct weston_view *ev) |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 2919 | { |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 2920 | struct drm_output *output = output_state->output; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2921 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 2922 | struct drm_plane *plane = output->cursor_plane; |
| 2923 | struct drm_plane_state *plane_state; |
| 2924 | struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport; |
| 2925 | struct wl_shm_buffer *shmbuf; |
| 2926 | bool needs_update = false; |
Derek Foreman | be428b3 | 2015-11-24 11:39:38 -0600 | [diff] [blame] | 2927 | float x, y; |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 2928 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 2929 | if (!plane) |
| 2930 | return NULL; |
| 2931 | |
| 2932 | if (b->cursors_are_broken) |
| 2933 | return NULL; |
| 2934 | |
| 2935 | if (!plane->state_cur->complete) |
| 2936 | return NULL; |
| 2937 | |
| 2938 | if (plane->state_cur->output && plane->state_cur->output != output) |
| 2939 | return NULL; |
| 2940 | |
| 2941 | /* Don't import buffers which span multiple outputs. */ |
| 2942 | if (ev->output_mask != (1u << output->base.id)) |
| 2943 | return NULL; |
| 2944 | |
| 2945 | /* We use GBM to import SHM buffers. */ |
| 2946 | if (b->gbm == NULL) |
| 2947 | return NULL; |
| 2948 | |
| 2949 | if (ev->surface->buffer_ref.buffer == NULL) |
| 2950 | return NULL; |
| 2951 | shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource); |
| 2952 | if (!shmbuf) |
| 2953 | return NULL; |
| 2954 | if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888) |
| 2955 | return NULL; |
| 2956 | |
| 2957 | if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL) |
| 2958 | return NULL; |
| 2959 | if (ev->transform.enabled && |
| 2960 | (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE)) |
| 2961 | return NULL; |
| 2962 | if (viewport->buffer.scale != output->base.current_scale) |
| 2963 | return NULL; |
| 2964 | if (ev->geometry.scissor_enabled) |
| 2965 | return NULL; |
| 2966 | |
| 2967 | if (ev->surface->width > b->cursor_width || |
| 2968 | ev->surface->height > b->cursor_height) |
| 2969 | return NULL; |
| 2970 | |
| 2971 | plane_state = |
| 2972 | drm_output_state_get_plane(output_state, output->cursor_plane); |
| 2973 | |
| 2974 | if (plane_state && plane_state->fb) |
| 2975 | return NULL; |
| 2976 | |
| 2977 | /* Since we're setting plane state up front, we need to work out |
| 2978 | * whether or not we need to upload a new cursor. We can't use the |
| 2979 | * plane damage, since the planes haven't actually been calculated |
| 2980 | * yet: instead try to figure it out directly. KMS cursor planes are |
| 2981 | * pretty unique here, in that they lie partway between a Weston plane |
| 2982 | * (direct scanout) and a renderer. */ |
| 2983 | if (ev != output->cursor_view || |
| 2984 | pixman_region32_not_empty(&ev->surface->damage)) { |
| 2985 | output->current_cursor++; |
| 2986 | output->current_cursor = |
| 2987 | output->current_cursor % |
| 2988 | ARRAY_LENGTH(output->gbm_cursor_fb); |
| 2989 | needs_update = true; |
| 2990 | } |
| 2991 | |
| 2992 | output->cursor_view = ev; |
| 2993 | weston_view_to_global_float(ev, 0, 0, &x, &y); |
| 2994 | plane->base.x = x; |
| 2995 | plane->base.y = y; |
| 2996 | |
| 2997 | plane_state->fb = |
| 2998 | drm_fb_ref(output->gbm_cursor_fb[output->current_cursor]); |
| 2999 | plane_state->output = output; |
| 3000 | plane_state->src_x = 0; |
| 3001 | plane_state->src_y = 0; |
| 3002 | plane_state->src_w = b->cursor_width << 16; |
| 3003 | plane_state->src_h = b->cursor_height << 16; |
| 3004 | plane_state->dest_x = (x - output->base.x) * output->base.current_scale; |
| 3005 | plane_state->dest_y = (y - output->base.y) * output->base.current_scale; |
| 3006 | plane_state->dest_w = b->cursor_width; |
| 3007 | plane_state->dest_h = b->cursor_height; |
| 3008 | |
| 3009 | if (needs_update) |
| 3010 | cursor_bo_update(b, plane_state->fb->bo, ev); |
| 3011 | |
| 3012 | return &plane->base; |
| 3013 | } |
| 3014 | |
| 3015 | static void |
| 3016 | drm_output_set_cursor(struct drm_output_state *output_state) |
| 3017 | { |
| 3018 | struct drm_output *output = output_state->output; |
| 3019 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
| 3020 | struct drm_plane *plane = output->cursor_plane; |
| 3021 | struct drm_plane_state *state; |
| 3022 | EGLint handle; |
| 3023 | struct gbm_bo *bo; |
| 3024 | |
| 3025 | if (!plane) |
| 3026 | return; |
| 3027 | |
| 3028 | state = drm_output_state_get_existing_plane(output_state, plane); |
| 3029 | if (!state) |
| 3030 | return; |
| 3031 | |
| 3032 | if (!state->fb) { |
| 3033 | pixman_region32_fini(&plane->base.damage); |
| 3034 | pixman_region32_init(&plane->base.damage); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3035 | drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0); |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 3036 | return; |
| 3037 | } |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 3038 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3039 | assert(state->fb == output->gbm_cursor_fb[output->current_cursor]); |
| 3040 | assert(!plane->state_cur->output || plane->state_cur->output == output); |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 3041 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3042 | if (plane->state_cur->fb != state->fb) { |
| 3043 | bo = state->fb->bo; |
Kristian Høgsberg | 1f5de35 | 2012-07-18 12:09:58 -0400 | [diff] [blame] | 3044 | handle = gbm_bo_get_handle(bo).s32; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3045 | if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle, |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3046 | b->cursor_width, b->cursor_height)) { |
Pekka Paalanen | ae29da2 | 2012-08-06 14:57:05 +0300 | [diff] [blame] | 3047 | weston_log("failed to set cursor: %m\n"); |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3048 | goto err; |
Rob Clark | ab5b1e3 | 2012-08-09 13:24:45 -0500 | [diff] [blame] | 3049 | } |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 3050 | } |
| 3051 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3052 | pixman_region32_fini(&plane->base.damage); |
| 3053 | pixman_region32_init(&plane->base.damage); |
Pekka Paalanen | 7eaed40 | 2015-11-27 14:20:58 +0200 | [diff] [blame] | 3054 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3055 | if (drmModeMoveCursor(b->drm.fd, output->crtc_id, |
| 3056 | state->dest_x, state->dest_y)) { |
Daniel Stone | a7cba1d | 2017-04-04 17:54:21 +0100 | [diff] [blame] | 3057 | weston_log("failed to move cursor: %m\n"); |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3058 | goto err; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 3059 | } |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3060 | |
| 3061 | return; |
| 3062 | |
| 3063 | err: |
| 3064 | b->cursors_are_broken = 1; |
| 3065 | drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0); |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 3066 | } |
| 3067 | |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 3068 | /* |
| 3069 | * Get the aspect-ratio from drmModeModeInfo mode flags. |
| 3070 | * |
| 3071 | * @param drm_mode_flags- flags from drmModeModeInfo structure. |
| 3072 | * @returns aspect-ratio as encoded in enum 'weston_mode_aspect_ratio'. |
| 3073 | */ |
| 3074 | static enum weston_mode_aspect_ratio |
| 3075 | drm_to_weston_mode_aspect_ratio(uint32_t drm_mode_flags) |
| 3076 | { |
| 3077 | return (drm_mode_flags & DRM_MODE_FLAG_PIC_AR_MASK) >> |
| 3078 | DRM_MODE_FLAG_PIC_AR_BITS_POS; |
| 3079 | } |
| 3080 | |
| 3081 | static const char * |
| 3082 | aspect_ratio_to_string(enum weston_mode_aspect_ratio ratio) |
| 3083 | { |
| 3084 | if (ratio < 0 || ratio >= ARRAY_LENGTH(aspect_ratio_as_string) || |
| 3085 | !aspect_ratio_as_string[ratio]) |
| 3086 | return " (unknown aspect ratio)"; |
| 3087 | |
| 3088 | return aspect_ratio_as_string[ratio]; |
| 3089 | } |
| 3090 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3091 | static void |
Daniel Stone | b1f166d | 2017-03-01 11:34:10 +0000 | [diff] [blame] | 3092 | drm_assign_planes(struct weston_output *output_base, void *repaint_data) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3093 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 3094 | struct drm_backend *b = to_drm_backend(output_base->compositor); |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 3095 | struct drm_pending_state *pending_state = repaint_data; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 3096 | struct drm_output *output = to_drm_output(output_base); |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 3097 | struct drm_output_state *state; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3098 | struct drm_plane_state *plane_state; |
Daniel Stone | c3fcb5b | 2016-12-01 12:33:54 +0000 | [diff] [blame] | 3099 | struct weston_view *ev; |
Daniel Stone | 115ed2c | 2016-11-08 21:19:22 +0000 | [diff] [blame] | 3100 | pixman_region32_t surface_overlap, renderer_region; |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 3101 | struct weston_plane *primary, *next_plane; |
Matt Hoosier | df57303 | 2017-08-24 09:24:20 -0500 | [diff] [blame] | 3102 | bool picked_scanout = false; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3103 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 3104 | assert(!output->state_last); |
| 3105 | state = drm_output_state_duplicate(output->state_cur, |
| 3106 | pending_state, |
| 3107 | DRM_OUTPUT_STATE_CLEAR_PLANES); |
| 3108 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3109 | /* |
| 3110 | * Find a surface for each sprite in the output using some heuristics: |
| 3111 | * 1) size |
| 3112 | * 2) frequency of update |
| 3113 | * 3) opacity (though some hw might support alpha blending) |
| 3114 | * 4) clipping (this can be fixed with color keys) |
| 3115 | * |
| 3116 | * The idea is to save on blitting since this should save power. |
| 3117 | * If we can get a large video surface on the sprite for example, |
| 3118 | * the main display surface may not need to update at all, and |
| 3119 | * the client buffer can be used directly for the sprite surface |
| 3120 | * as we do for flipping full screen surfaces. |
| 3121 | */ |
Daniel Stone | 115ed2c | 2016-11-08 21:19:22 +0000 | [diff] [blame] | 3122 | pixman_region32_init(&renderer_region); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3123 | primary = &output_base->compositor->primary_plane; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 3124 | |
Daniel Stone | c3fcb5b | 2016-12-01 12:33:54 +0000 | [diff] [blame] | 3125 | wl_list_for_each(ev, &output_base->compositor->view_list, link) { |
Ander Conselvan de Oliveira | 895b1fd | 2013-11-19 15:22:05 +0200 | [diff] [blame] | 3126 | struct weston_surface *es = ev->surface; |
| 3127 | |
| 3128 | /* Test whether this buffer can ever go into a plane: |
| 3129 | * non-shm, or small enough to be a cursor. |
| 3130 | * |
| 3131 | * Also, keep a reference when using the pixman renderer. |
| 3132 | * That makes it possible to do a seamless switch to the GL |
| 3133 | * renderer and since the pixman renderer keeps a reference |
| 3134 | * to the buffer anyway, there is no side effects. |
Pekka Paalanen | ccfeae2 | 2012-12-04 15:58:14 +0200 | [diff] [blame] | 3135 | */ |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3136 | if (b->use_pixman || |
Ander Conselvan de Oliveira | 895b1fd | 2013-11-19 15:22:05 +0200 | [diff] [blame] | 3137 | (es->buffer_ref.buffer && |
| 3138 | (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) || |
Derek Foreman | 8743047 | 2015-10-15 10:24:48 -0500 | [diff] [blame] | 3139 | (ev->surface->width <= b->cursor_width && |
| 3140 | ev->surface->height <= b->cursor_height)))) |
Derek Foreman | 0fd6d4e | 2014-10-10 09:36:45 -0500 | [diff] [blame] | 3141 | es->keep_buffer = true; |
Ander Conselvan de Oliveira | 895b1fd | 2013-11-19 15:22:05 +0200 | [diff] [blame] | 3142 | else |
Derek Foreman | 0fd6d4e | 2014-10-10 09:36:45 -0500 | [diff] [blame] | 3143 | es->keep_buffer = false; |
Pekka Paalanen | ccfeae2 | 2012-12-04 15:58:14 +0200 | [diff] [blame] | 3144 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3145 | pixman_region32_init(&surface_overlap); |
Daniel Stone | 115ed2c | 2016-11-08 21:19:22 +0000 | [diff] [blame] | 3146 | pixman_region32_intersect(&surface_overlap, &renderer_region, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 3147 | &ev->transform.boundingbox); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3148 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 3149 | next_plane = NULL; |
Matt Hoosier | df57303 | 2017-08-24 09:24:20 -0500 | [diff] [blame] | 3150 | if (pixman_region32_not_empty(&surface_overlap) || picked_scanout) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 3151 | next_plane = primary; |
| 3152 | if (next_plane == NULL) |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 3153 | next_plane = drm_output_prepare_cursor_view(state, ev); |
Matt Hoosier | df57303 | 2017-08-24 09:24:20 -0500 | [diff] [blame] | 3154 | |
| 3155 | /* If a higher-stacked view already got assigned to scanout, it's incorrect to |
| 3156 | * assign a subsequent (lower-stacked) view to scanout. |
| 3157 | */ |
| 3158 | if (next_plane == NULL) { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 3159 | next_plane = drm_output_prepare_scanout_view(state, ev); |
Matt Hoosier | df57303 | 2017-08-24 09:24:20 -0500 | [diff] [blame] | 3160 | if (next_plane) |
| 3161 | picked_scanout = true; |
| 3162 | } |
| 3163 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 3164 | if (next_plane == NULL) |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 3165 | next_plane = drm_output_prepare_overlay_view(state, ev); |
Matt Hoosier | df57303 | 2017-08-24 09:24:20 -0500 | [diff] [blame] | 3166 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 3167 | if (next_plane == NULL) |
| 3168 | next_plane = primary; |
Pekka Paalanen | bf0e031 | 2014-12-17 16:20:41 +0200 | [diff] [blame] | 3169 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 3170 | weston_view_move_to_plane(ev, next_plane); |
Pekka Paalanen | bf0e031 | 2014-12-17 16:20:41 +0200 | [diff] [blame] | 3171 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 3172 | if (next_plane == primary) |
Daniel Stone | 115ed2c | 2016-11-08 21:19:22 +0000 | [diff] [blame] | 3173 | pixman_region32_union(&renderer_region, |
| 3174 | &renderer_region, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 3175 | &ev->transform.boundingbox); |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 3176 | |
Pekka Paalanen | bf0e031 | 2014-12-17 16:20:41 +0200 | [diff] [blame] | 3177 | if (next_plane == primary || |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3178 | (output->cursor_plane && |
| 3179 | next_plane == &output->cursor_plane->base)) { |
Pekka Paalanen | bf0e031 | 2014-12-17 16:20:41 +0200 | [diff] [blame] | 3180 | /* cursor plane involves a copy */ |
| 3181 | ev->psf_flags = 0; |
| 3182 | } else { |
| 3183 | /* All other planes are a direct scanout of a |
| 3184 | * single client buffer. |
| 3185 | */ |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 3186 | ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY; |
Pekka Paalanen | bf0e031 | 2014-12-17 16:20:41 +0200 | [diff] [blame] | 3187 | } |
| 3188 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3189 | pixman_region32_fini(&surface_overlap); |
| 3190 | } |
Daniel Stone | 115ed2c | 2016-11-08 21:19:22 +0000 | [diff] [blame] | 3191 | pixman_region32_fini(&renderer_region); |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3192 | |
| 3193 | /* We rely on output->cursor_view being both an accurate reflection of |
| 3194 | * the cursor plane's state, but also being maintained across repaints |
| 3195 | * to avoid unnecessary damage uploads, per the comment in |
| 3196 | * drm_output_prepare_cursor_view. In the event that we go from having |
| 3197 | * a cursor view to not having a cursor view, we need to clear it. */ |
| 3198 | if (output->cursor_view) { |
| 3199 | plane_state = |
| 3200 | drm_output_state_get_existing_plane(state, |
| 3201 | output->cursor_plane); |
| 3202 | if (!plane_state || !plane_state->fb) |
| 3203 | output->cursor_view = NULL; |
| 3204 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3205 | } |
| 3206 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 3207 | /** |
| 3208 | * Find the closest-matching mode for a given target |
| 3209 | * |
| 3210 | * Given a target mode, find the most suitable mode amongst the output's |
| 3211 | * current mode list to use, preferring the current mode if possible, to |
| 3212 | * avoid an expensive mode switch. |
| 3213 | * |
| 3214 | * @param output DRM output |
| 3215 | * @param target_mode Mode to attempt to match |
| 3216 | * @returns Pointer to a mode from the output's mode list |
| 3217 | */ |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3218 | static struct drm_mode * |
| 3219 | choose_mode (struct drm_output *output, struct weston_mode *target_mode) |
| 3220 | { |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 3221 | struct drm_mode *tmp_mode = NULL, *mode_fall_back = NULL, *mode; |
| 3222 | enum weston_mode_aspect_ratio src_aspect = WESTON_MODE_PIC_AR_NONE; |
| 3223 | enum weston_mode_aspect_ratio target_aspect = WESTON_MODE_PIC_AR_NONE; |
| 3224 | struct drm_backend *b; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3225 | |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 3226 | b = to_drm_backend(output->base.compositor); |
| 3227 | target_aspect = target_mode->aspect_ratio; |
| 3228 | src_aspect = output->base.current_mode->aspect_ratio; |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 3229 | if (output->base.current_mode->width == target_mode->width && |
| 3230 | output->base.current_mode->height == target_mode->height && |
| 3231 | (output->base.current_mode->refresh == target_mode->refresh || |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 3232 | target_mode->refresh == 0)) { |
| 3233 | if (!b->aspect_ratio_supported || src_aspect == target_aspect) |
| 3234 | return to_drm_mode(output->base.current_mode); |
| 3235 | } |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3236 | |
| 3237 | wl_list_for_each(mode, &output->base.mode_list, base.link) { |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 3238 | |
| 3239 | src_aspect = mode->base.aspect_ratio; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3240 | if (mode->mode_info.hdisplay == target_mode->width && |
| 3241 | mode->mode_info.vdisplay == target_mode->height) { |
Mario Kleiner | 872797c | 2015-06-21 21:25:09 +0200 | [diff] [blame] | 3242 | if (mode->base.refresh == target_mode->refresh || |
| 3243 | target_mode->refresh == 0) { |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 3244 | if (!b->aspect_ratio_supported || |
| 3245 | src_aspect == target_aspect) |
| 3246 | return mode; |
| 3247 | else if (!mode_fall_back) |
| 3248 | mode_fall_back = mode; |
| 3249 | } else if (!tmp_mode) { |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3250 | tmp_mode = mode; |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 3251 | } |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3252 | } |
| 3253 | } |
| 3254 | |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 3255 | if (mode_fall_back) |
| 3256 | return mode_fall_back; |
| 3257 | |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3258 | return tmp_mode; |
| 3259 | } |
| 3260 | |
| 3261 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3262 | drm_output_init_egl(struct drm_output *output, struct drm_backend *b); |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 3263 | static void |
| 3264 | drm_output_fini_egl(struct drm_output *output); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3265 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3266 | drm_output_init_pixman(struct drm_output *output, struct drm_backend *b); |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 3267 | static void |
| 3268 | drm_output_fini_pixman(struct drm_output *output); |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 3269 | |
| 3270 | static int |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3271 | drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode) |
| 3272 | { |
Daniel Stone | 02d487a | 2017-10-07 14:01:45 +0100 | [diff] [blame] | 3273 | struct drm_output *output = to_drm_output(output_base); |
| 3274 | struct drm_backend *b = to_drm_backend(output_base->compositor); |
| 3275 | struct drm_mode *drm_mode = choose_mode(output, mode); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3276 | |
| 3277 | if (!drm_mode) { |
Daniel Stone | 02d487a | 2017-10-07 14:01:45 +0100 | [diff] [blame] | 3278 | weston_log("%s: invalid resolution %dx%d\n", |
| 3279 | output_base->name, mode->width, mode->height); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3280 | return -1; |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 3281 | } |
| 3282 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 3283 | if (&drm_mode->base == output->base.current_mode) |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3284 | return 0; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3285 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 3286 | output->base.current_mode->flags = 0; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3287 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 3288 | output->base.current_mode = &drm_mode->base; |
| 3289 | output->base.current_mode->flags = |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3290 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 3291 | |
Daniel Stone | f30a18c | 2017-04-04 17:54:31 +0100 | [diff] [blame] | 3292 | /* XXX: This drops our current buffer too early, before we've started |
| 3293 | * displaying it. Ideally this should be much more atomic and |
| 3294 | * integrated with a full repaint cycle, rather than doing a |
| 3295 | * sledgehammer modeswitch first, and only later showing new |
| 3296 | * content. |
| 3297 | */ |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 3298 | b->state_invalid = true; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3299 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3300 | if (b->use_pixman) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3301 | drm_output_fini_pixman(output); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3302 | if (drm_output_init_pixman(output, b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3303 | weston_log("failed to init output pixman state with " |
| 3304 | "new mode\n"); |
| 3305 | return -1; |
| 3306 | } |
| 3307 | } else { |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 3308 | drm_output_fini_egl(output); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3309 | if (drm_output_init_egl(output, b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3310 | weston_log("failed to init output egl state with " |
| 3311 | "new mode"); |
| 3312 | return -1; |
| 3313 | } |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 3314 | } |
| 3315 | |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3316 | return 0; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3317 | } |
| 3318 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 3319 | static int |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3320 | on_drm_input(int fd, uint32_t mask, void *data) |
| 3321 | { |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 3322 | #ifdef HAVE_DRM_ATOMIC |
| 3323 | struct drm_backend *b = data; |
| 3324 | #endif |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3325 | drmEventContext evctx; |
| 3326 | |
| 3327 | memset(&evctx, 0, sizeof evctx); |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 3328 | #ifndef HAVE_DRM_ATOMIC |
Emil Velikov | 863e66b | 2017-04-04 18:07:34 +0100 | [diff] [blame] | 3329 | evctx.version = 2; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 3330 | #else |
| 3331 | evctx.version = 3; |
| 3332 | if (b->atomic_modeset) |
| 3333 | evctx.page_flip_handler2 = atomic_flip_handler; |
| 3334 | else |
| 3335 | #endif |
| 3336 | evctx.page_flip_handler = page_flip_handler; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3337 | evctx.vblank_handler = vblank_handler; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3338 | drmHandleEvent(fd, &evctx); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 3339 | |
| 3340 | return 1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3341 | } |
| 3342 | |
| 3343 | static int |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3344 | init_kms_caps(struct drm_backend *b) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3345 | { |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 3346 | uint64_t cap; |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3347 | int ret; |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 3348 | clockid_t clk_id; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 3349 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3350 | weston_log("using %s\n", b->drm.filename); |
Kristian Høgsberg | b71302e | 2012-05-10 12:28:35 -0400 | [diff] [blame] | 3351 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3352 | ret = drmGetCap(b->drm.fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap); |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 3353 | if (ret == 0 && cap == 1) |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 3354 | clk_id = CLOCK_MONOTONIC; |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 3355 | else |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 3356 | clk_id = CLOCK_REALTIME; |
| 3357 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3358 | if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) { |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 3359 | weston_log("Error: failed to set presentation clock %d.\n", |
| 3360 | clk_id); |
| 3361 | return -1; |
| 3362 | } |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 3363 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3364 | ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_WIDTH, &cap); |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 3365 | if (ret == 0) |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3366 | b->cursor_width = cap; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 3367 | else |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3368 | b->cursor_width = 64; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 3369 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3370 | ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_HEIGHT, &cap); |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 3371 | if (ret == 0) |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3372 | b->cursor_height = cap; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 3373 | else |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3374 | b->cursor_height = 64; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 3375 | |
Daniel Stone | be1090b | 2017-09-06 17:29:57 +0100 | [diff] [blame] | 3376 | if (!getenv("WESTON_DISABLE_UNIVERSAL_PLANES")) { |
| 3377 | ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1); |
| 3378 | b->universal_planes = (ret == 0); |
| 3379 | } |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 3380 | weston_log("DRM: %s universal planes\n", |
| 3381 | b->universal_planes ? "supports" : "does not support"); |
| 3382 | |
Pekka Paalanen | cd011a6 | 2016-11-15 22:07:49 +0000 | [diff] [blame] | 3383 | #ifdef HAVE_DRM_ATOMIC |
| 3384 | if (b->universal_planes && !getenv("WESTON_DISABLE_ATOMIC")) { |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 3385 | ret = drmGetCap(b->drm.fd, DRM_CAP_CRTC_IN_VBLANK_EVENT, &cap); |
| 3386 | if (ret != 0) |
| 3387 | cap = 0; |
Pekka Paalanen | cd011a6 | 2016-11-15 22:07:49 +0000 | [diff] [blame] | 3388 | ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_ATOMIC, 1); |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 3389 | b->atomic_modeset = ((ret == 0) && (cap == 1)); |
Pekka Paalanen | cd011a6 | 2016-11-15 22:07:49 +0000 | [diff] [blame] | 3390 | } |
| 3391 | #endif |
| 3392 | weston_log("DRM: %s atomic modesetting\n", |
| 3393 | b->atomic_modeset ? "supports" : "does not support"); |
| 3394 | |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 3395 | ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_ASPECT_RATIO, 1); |
| 3396 | b->aspect_ratio_supported = (ret == 0); |
| 3397 | weston_log("DRM: %s picture aspect ratio\n", |
| 3398 | b->aspect_ratio_supported ? "supports" : "does not support"); |
| 3399 | |
Ander Conselvan de Oliveira | 2292917 | 2013-01-25 15:13:02 +0200 | [diff] [blame] | 3400 | return 0; |
| 3401 | } |
| 3402 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3403 | static struct gbm_device * |
| 3404 | create_gbm_device(int fd) |
Ander Conselvan de Oliveira | 2292917 | 2013-01-25 15:13:02 +0200 | [diff] [blame] | 3405 | { |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3406 | struct gbm_device *gbm; |
Alexandru DAMIAN | be0ac5b | 2013-10-02 17:51:05 +0100 | [diff] [blame] | 3407 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 3408 | gl_renderer = weston_load_module("gl-renderer.so", |
| 3409 | "gl_renderer_interface"); |
| 3410 | if (!gl_renderer) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3411 | return NULL; |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 3412 | |
| 3413 | /* GBM will load a dri driver, but even though they need symbols from |
| 3414 | * libglapi, in some version of Mesa they are not linked to it. Since |
| 3415 | * only the gl-renderer module links to it, the call above won't make |
| 3416 | * these symbols globally available, and loading the DRI driver fails. |
| 3417 | * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */ |
| 3418 | dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL); |
| 3419 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3420 | gbm = gbm_create_device(fd); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3421 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3422 | return gbm; |
| 3423 | } |
| 3424 | |
Bryce Harrington | c056a98 | 2015-05-19 15:25:18 -0700 | [diff] [blame] | 3425 | /* When initializing EGL, if the preferred buffer format isn't available |
Bryce Harrington | b993998 | 2016-04-15 20:28:26 -0700 | [diff] [blame] | 3426 | * 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] | 3427 | * |
| 3428 | * This returns 0 if substitution isn't possible, but 0 might be a |
| 3429 | * legitimate format for other EGL platforms, so the caller is |
| 3430 | * responsible for checking for 0 before calling gl_renderer->create(). |
| 3431 | * |
| 3432 | * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689 |
| 3433 | * but it's entirely possible we'll see this again on other implementations. |
| 3434 | */ |
| 3435 | static int |
| 3436 | fallback_format_for(uint32_t format) |
| 3437 | { |
| 3438 | switch (format) { |
| 3439 | case GBM_FORMAT_XRGB8888: |
| 3440 | return GBM_FORMAT_ARGB8888; |
| 3441 | case GBM_FORMAT_XRGB2101010: |
| 3442 | return GBM_FORMAT_ARGB2101010; |
| 3443 | default: |
| 3444 | return 0; |
| 3445 | } |
| 3446 | } |
| 3447 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3448 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3449 | drm_backend_create_gl_renderer(struct drm_backend *b) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3450 | { |
Derek Foreman | 6d55637 | 2015-11-04 14:47:33 -0600 | [diff] [blame] | 3451 | EGLint format[3] = { |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 3452 | b->gbm_format, |
| 3453 | fallback_format_for(b->gbm_format), |
Derek Foreman | 6d55637 | 2015-11-04 14:47:33 -0600 | [diff] [blame] | 3454 | 0, |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 3455 | }; |
Derek Foreman | 6d55637 | 2015-11-04 14:47:33 -0600 | [diff] [blame] | 3456 | int n_formats = 2; |
John Kåre Alsaker | ef591aa | 2013-03-02 12:27:39 +0100 | [diff] [blame] | 3457 | |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 3458 | if (format[1]) |
Derek Foreman | 6d55637 | 2015-11-04 14:47:33 -0600 | [diff] [blame] | 3459 | n_formats = 3; |
Miguel A. Vico | dddc670 | 2016-05-18 17:41:07 +0200 | [diff] [blame] | 3460 | if (gl_renderer->display_create(b->compositor, |
| 3461 | EGL_PLATFORM_GBM_KHR, |
| 3462 | (void *)b->gbm, |
Miguel A. Vico | 41700e3 | 2016-05-18 17:47:59 +0200 | [diff] [blame] | 3463 | NULL, |
Miguel A. Vico | dddc670 | 2016-05-18 17:41:07 +0200 | [diff] [blame] | 3464 | gl_renderer->opaque_attribs, |
| 3465 | format, |
| 3466 | n_formats) < 0) { |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3467 | return -1; |
| 3468 | } |
| 3469 | |
| 3470 | return 0; |
| 3471 | } |
| 3472 | |
| 3473 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3474 | init_egl(struct drm_backend *b) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3475 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3476 | b->gbm = create_gbm_device(b->drm.fd); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3477 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3478 | if (!b->gbm) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3479 | return -1; |
| 3480 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3481 | if (drm_backend_create_gl_renderer(b) < 0) { |
| 3482 | gbm_device_destroy(b->gbm); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 3483 | return -1; |
| 3484 | } |
| 3485 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3486 | return 0; |
| 3487 | } |
| 3488 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3489 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3490 | init_pixman(struct drm_backend *b) |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3491 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3492 | return pixman_renderer_init(b->compositor); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3493 | } |
| 3494 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 3495 | /** |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3496 | * Create a drm_plane for a hardware plane |
| 3497 | * |
| 3498 | * Creates one drm_plane structure for a hardware plane, and initialises its |
| 3499 | * properties and formats. |
| 3500 | * |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3501 | * In the absence of universal plane support, where KMS does not explicitly |
| 3502 | * expose the primary and cursor planes to userspace, this may also create |
| 3503 | * an 'internal' plane for internal management. |
| 3504 | * |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3505 | * This function does not add the plane to the list of usable planes in Weston |
| 3506 | * itself; the caller is responsible for this. |
| 3507 | * |
| 3508 | * Call drm_plane_destroy to clean up the plane. |
| 3509 | * |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3510 | * @sa drm_output_find_special_plane |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3511 | * @param b DRM compositor backend |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3512 | * @param kplane DRM plane to create, or NULL if creating internal plane |
| 3513 | * @param output Output to create internal plane for, or NULL |
| 3514 | * @param type Type to use when creating internal plane, or invalid |
| 3515 | * @param format Format to use for internal planes, or 0 |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3516 | */ |
| 3517 | static struct drm_plane * |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3518 | drm_plane_create(struct drm_backend *b, const drmModePlane *kplane, |
| 3519 | struct drm_output *output, enum wdrm_plane_type type, |
| 3520 | uint32_t format) |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3521 | { |
| 3522 | struct drm_plane *plane; |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 3523 | drmModeObjectProperties *props; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3524 | int num_formats = (kplane) ? kplane->count_formats : 1; |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 3525 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3526 | plane = zalloc(sizeof(*plane) + |
| 3527 | (sizeof(uint32_t) * num_formats)); |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3528 | if (!plane) { |
| 3529 | weston_log("%s: out of memory\n", __func__); |
| 3530 | return NULL; |
| 3531 | } |
| 3532 | |
| 3533 | plane->backend = b; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 3534 | plane->state_cur = drm_plane_state_alloc(NULL, plane); |
| 3535 | plane->state_cur->complete = true; |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3536 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3537 | if (kplane) { |
| 3538 | plane->possible_crtcs = kplane->possible_crtcs; |
| 3539 | plane->plane_id = kplane->plane_id; |
| 3540 | plane->count_formats = kplane->count_formats; |
| 3541 | memcpy(plane->formats, kplane->formats, |
| 3542 | kplane->count_formats * sizeof(kplane->formats[0])); |
| 3543 | |
| 3544 | props = drmModeObjectGetProperties(b->drm.fd, kplane->plane_id, |
| 3545 | DRM_MODE_OBJECT_PLANE); |
| 3546 | if (!props) { |
| 3547 | weston_log("couldn't get plane properties\n"); |
| 3548 | goto err; |
| 3549 | } |
| 3550 | drm_property_info_populate(b, plane_props, plane->props, |
| 3551 | WDRM_PLANE__COUNT, props); |
| 3552 | plane->type = |
| 3553 | drm_property_get_value(&plane->props[WDRM_PLANE_TYPE], |
| 3554 | props, |
| 3555 | WDRM_PLANE_TYPE__COUNT); |
| 3556 | drmModeFreeObjectProperties(props); |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 3557 | } |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3558 | else { |
| 3559 | plane->possible_crtcs = (1 << output->pipe); |
| 3560 | plane->plane_id = 0; |
| 3561 | plane->count_formats = 1; |
| 3562 | plane->formats[0] = format; |
| 3563 | plane->type = type; |
| 3564 | } |
| 3565 | |
| 3566 | if (plane->type == WDRM_PLANE_TYPE__COUNT) |
| 3567 | goto err_props; |
| 3568 | |
| 3569 | /* With universal planes, everything is a DRM plane; without |
| 3570 | * universal planes, the only DRM planes are overlay planes. |
| 3571 | * Everything else is a fake plane. */ |
| 3572 | if (b->universal_planes) { |
| 3573 | assert(kplane); |
| 3574 | } else { |
| 3575 | if (kplane) |
| 3576 | assert(plane->type == WDRM_PLANE_TYPE_OVERLAY); |
| 3577 | else |
| 3578 | assert(plane->type != WDRM_PLANE_TYPE_OVERLAY && |
| 3579 | output); |
| 3580 | } |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 3581 | |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3582 | weston_plane_init(&plane->base, b->compositor, 0, 0); |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 3583 | wl_list_insert(&b->plane_list, &plane->link); |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3584 | |
| 3585 | return plane; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3586 | |
| 3587 | err_props: |
| 3588 | drm_property_info_free(plane->props, WDRM_PLANE__COUNT); |
| 3589 | err: |
| 3590 | drm_plane_state_free(plane->state_cur, true); |
| 3591 | free(plane); |
| 3592 | return NULL; |
| 3593 | } |
| 3594 | |
| 3595 | /** |
| 3596 | * Find, or create, a special-purpose plane |
| 3597 | * |
| 3598 | * Primary and cursor planes are a special case, in that before universal |
| 3599 | * planes, they are driven by non-plane API calls. Without universal plane |
| 3600 | * support, the only way to configure a primary plane is via drmModeSetCrtc, |
| 3601 | * and the only way to configure a cursor plane is drmModeSetCursor2. |
| 3602 | * |
| 3603 | * Although they may actually be regular planes in the hardware, without |
| 3604 | * universal plane support, these planes are not actually exposed to |
| 3605 | * userspace in the regular plane list. |
| 3606 | * |
| 3607 | * However, for ease of internal tracking, we want to manage all planes |
| 3608 | * through the same drm_plane structures. Therefore, when we are running |
| 3609 | * without universal plane support, we create fake drm_plane structures |
| 3610 | * to track these planes. |
| 3611 | * |
| 3612 | * @param b DRM backend |
| 3613 | * @param output Output to use for plane |
| 3614 | * @param type Type of plane |
| 3615 | */ |
| 3616 | static struct drm_plane * |
| 3617 | drm_output_find_special_plane(struct drm_backend *b, struct drm_output *output, |
| 3618 | enum wdrm_plane_type type) |
| 3619 | { |
| 3620 | struct drm_plane *plane; |
| 3621 | |
| 3622 | if (!b->universal_planes) { |
| 3623 | uint32_t format; |
| 3624 | |
| 3625 | switch (type) { |
| 3626 | case WDRM_PLANE_TYPE_CURSOR: |
| 3627 | format = GBM_FORMAT_ARGB8888; |
| 3628 | break; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 3629 | case WDRM_PLANE_TYPE_PRIMARY: |
| 3630 | /* We don't know what formats the primary plane supports |
| 3631 | * before universal planes, so we just assume that the |
| 3632 | * GBM format works; however, this isn't set until after |
| 3633 | * the output is created. */ |
| 3634 | format = 0; |
| 3635 | break; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3636 | default: |
| 3637 | assert(!"invalid type in drm_output_find_special_plane"); |
| 3638 | break; |
| 3639 | } |
| 3640 | |
| 3641 | return drm_plane_create(b, NULL, output, type, format); |
| 3642 | } |
| 3643 | |
| 3644 | wl_list_for_each(plane, &b->plane_list, link) { |
| 3645 | struct drm_output *tmp; |
| 3646 | bool found_elsewhere = false; |
| 3647 | |
| 3648 | if (plane->type != type) |
| 3649 | continue; |
| 3650 | if (!drm_plane_is_available(plane, output)) |
| 3651 | continue; |
| 3652 | |
| 3653 | /* On some platforms, primary/cursor planes can roam |
| 3654 | * between different CRTCs, so make sure we don't claim the |
| 3655 | * same plane for two outputs. */ |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3656 | wl_list_for_each(tmp, &b->compositor->output_list, |
| 3657 | base.link) { |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 3658 | if (tmp->cursor_plane == plane || |
| 3659 | tmp->scanout_plane == plane) { |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3660 | found_elsewhere = true; |
| 3661 | break; |
| 3662 | } |
| 3663 | } |
| 3664 | |
| 3665 | if (found_elsewhere) |
| 3666 | continue; |
| 3667 | |
| 3668 | plane->possible_crtcs = (1 << output->pipe); |
| 3669 | return plane; |
| 3670 | } |
| 3671 | |
| 3672 | return NULL; |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3673 | } |
| 3674 | |
| 3675 | /** |
| 3676 | * Destroy one DRM plane |
| 3677 | * |
| 3678 | * Destroy a DRM plane, removing it from screen and releasing its retained |
| 3679 | * buffers in the process. The counterpart to drm_plane_create. |
| 3680 | * |
| 3681 | * @param plane Plane to deallocate (will be freed) |
| 3682 | */ |
| 3683 | static void |
| 3684 | drm_plane_destroy(struct drm_plane *plane) |
| 3685 | { |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3686 | if (plane->type == WDRM_PLANE_TYPE_OVERLAY) |
| 3687 | drmModeSetPlane(plane->backend->drm.fd, plane->plane_id, |
| 3688 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 3689 | drm_plane_state_free(plane->state_cur, true); |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 3690 | drm_property_info_free(plane->props, WDRM_PLANE__COUNT); |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3691 | weston_plane_release(&plane->base); |
| 3692 | wl_list_remove(&plane->link); |
| 3693 | free(plane); |
| 3694 | } |
| 3695 | |
| 3696 | /** |
| 3697 | * Initialise sprites (overlay planes) |
| 3698 | * |
| 3699 | * Walk the list of provided DRM planes, and add overlay planes. |
| 3700 | * |
| 3701 | * Call destroy_sprites to free these planes. |
| 3702 | * |
| 3703 | * @param b DRM compositor backend |
| 3704 | */ |
| 3705 | static void |
| 3706 | create_sprites(struct drm_backend *b) |
| 3707 | { |
| 3708 | drmModePlaneRes *kplane_res; |
| 3709 | drmModePlane *kplane; |
| 3710 | struct drm_plane *drm_plane; |
| 3711 | uint32_t i; |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3712 | kplane_res = drmModeGetPlaneResources(b->drm.fd); |
| 3713 | if (!kplane_res) { |
| 3714 | weston_log("failed to get plane resources: %s\n", |
| 3715 | strerror(errno)); |
| 3716 | return; |
| 3717 | } |
| 3718 | |
| 3719 | for (i = 0; i < kplane_res->count_planes; i++) { |
| 3720 | kplane = drmModeGetPlane(b->drm.fd, kplane_res->planes[i]); |
| 3721 | if (!kplane) |
| 3722 | continue; |
| 3723 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3724 | drm_plane = drm_plane_create(b, kplane, NULL, |
| 3725 | WDRM_PLANE_TYPE__COUNT, 0); |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3726 | drmModeFreePlane(kplane); |
| 3727 | if (!drm_plane) |
| 3728 | continue; |
| 3729 | |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 3730 | if (drm_plane->type == WDRM_PLANE_TYPE_OVERLAY) |
| 3731 | weston_compositor_stack_plane(b->compositor, |
| 3732 | &drm_plane->base, |
| 3733 | &b->compositor->primary_plane); |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3734 | } |
| 3735 | |
| 3736 | drmModeFreePlaneResources(kplane_res); |
| 3737 | } |
| 3738 | |
| 3739 | /** |
| 3740 | * Clean up sprites (overlay planes) |
| 3741 | * |
| 3742 | * The counterpart to create_sprites. |
| 3743 | * |
| 3744 | * @param b DRM compositor backend |
| 3745 | */ |
| 3746 | static void |
| 3747 | destroy_sprites(struct drm_backend *b) |
| 3748 | { |
| 3749 | struct drm_plane *plane, *next; |
| 3750 | |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 3751 | wl_list_for_each_safe(plane, next, &b->plane_list, link) |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3752 | drm_plane_destroy(plane); |
| 3753 | } |
| 3754 | |
Pekka Paalanen | dc14fd4 | 2017-11-10 15:31:39 +0200 | [diff] [blame] | 3755 | static uint32_t |
| 3756 | drm_refresh_rate_mHz(const drmModeModeInfo *info) |
| 3757 | { |
| 3758 | uint64_t refresh; |
| 3759 | |
| 3760 | /* Calculate higher precision (mHz) refresh rate */ |
| 3761 | refresh = (info->clock * 1000000LL / info->htotal + |
| 3762 | info->vtotal / 2) / info->vtotal; |
| 3763 | |
| 3764 | if (info->flags & DRM_MODE_FLAG_INTERLACE) |
| 3765 | refresh *= 2; |
| 3766 | if (info->flags & DRM_MODE_FLAG_DBLSCAN) |
| 3767 | refresh /= 2; |
| 3768 | if (info->vscan > 1) |
| 3769 | refresh /= info->vscan; |
| 3770 | |
| 3771 | return refresh; |
| 3772 | } |
| 3773 | |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3774 | /** |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 3775 | * Add a mode to output's mode list |
| 3776 | * |
| 3777 | * Copy the supplied DRM mode into a Weston mode structure, and add it to the |
| 3778 | * output's mode list. |
| 3779 | * |
| 3780 | * @param output DRM output to add mode to |
| 3781 | * @param info DRM mode structure to add |
| 3782 | * @returns Newly-allocated Weston/DRM mode structure |
| 3783 | */ |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 3784 | static struct drm_mode * |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 3785 | drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info) |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 3786 | { |
| 3787 | struct drm_mode *mode; |
| 3788 | |
| 3789 | mode = malloc(sizeof *mode); |
| 3790 | if (mode == NULL) |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 3791 | return NULL; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 3792 | |
| 3793 | mode->base.flags = 0; |
Alexander Larsson | 0b13506 | 2013-05-28 16:23:36 +0200 | [diff] [blame] | 3794 | mode->base.width = info->hdisplay; |
| 3795 | mode->base.height = info->vdisplay; |
Kristian Høgsberg | c4621b0 | 2012-05-10 12:23:53 -0400 | [diff] [blame] | 3796 | |
Pekka Paalanen | dc14fd4 | 2017-11-10 15:31:39 +0200 | [diff] [blame] | 3797 | mode->base.refresh = drm_refresh_rate_mHz(info); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 3798 | mode->mode_info = *info; |
Daniel Stone | d5526cb | 2016-11-16 10:54:10 +0000 | [diff] [blame] | 3799 | mode->blob_id = 0; |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 3800 | |
| 3801 | if (info->type & DRM_MODE_TYPE_PREFERRED) |
| 3802 | mode->base.flags |= WL_OUTPUT_MODE_PREFERRED; |
| 3803 | |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 3804 | mode->base.aspect_ratio = drm_to_weston_mode_aspect_ratio(info->flags); |
| 3805 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 3806 | wl_list_insert(output->base.mode_list.prev, &mode->base.link); |
| 3807 | |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 3808 | return mode; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 3809 | } |
| 3810 | |
Daniel Stone | d5526cb | 2016-11-16 10:54:10 +0000 | [diff] [blame] | 3811 | /** |
| 3812 | * Destroys a mode, and removes it from the list. |
| 3813 | */ |
| 3814 | static void |
| 3815 | drm_output_destroy_mode(struct drm_backend *backend, struct drm_mode *mode) |
| 3816 | { |
| 3817 | if (mode->blob_id) |
| 3818 | drmModeDestroyPropertyBlob(backend->drm.fd, mode->blob_id); |
| 3819 | wl_list_remove(&mode->base.link); |
| 3820 | free(mode); |
| 3821 | } |
| 3822 | |
Pekka Paalanen | 383b3af | 2017-09-11 14:40:48 +0300 | [diff] [blame] | 3823 | /** Destroy a list of drm_modes |
| 3824 | * |
| 3825 | * @param backend The backend for releasing mode property blobs. |
| 3826 | * @param mode_list The list linked by drm_mode::base.link. |
| 3827 | */ |
| 3828 | static void |
| 3829 | drm_mode_list_destroy(struct drm_backend *backend, struct wl_list *mode_list) |
| 3830 | { |
| 3831 | struct drm_mode *mode, *next; |
| 3832 | |
| 3833 | wl_list_for_each_safe(mode, next, mode_list, base.link) |
| 3834 | drm_output_destroy_mode(backend, mode); |
| 3835 | } |
| 3836 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 3837 | static int |
| 3838 | drm_subpixel_to_wayland(int drm_value) |
| 3839 | { |
| 3840 | switch (drm_value) { |
| 3841 | default: |
| 3842 | case DRM_MODE_SUBPIXEL_UNKNOWN: |
| 3843 | return WL_OUTPUT_SUBPIXEL_UNKNOWN; |
| 3844 | case DRM_MODE_SUBPIXEL_NONE: |
| 3845 | return WL_OUTPUT_SUBPIXEL_NONE; |
| 3846 | case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB: |
| 3847 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB; |
| 3848 | case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR: |
| 3849 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR; |
| 3850 | case DRM_MODE_SUBPIXEL_VERTICAL_RGB: |
| 3851 | return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB; |
| 3852 | case DRM_MODE_SUBPIXEL_VERTICAL_BGR: |
| 3853 | return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR; |
| 3854 | } |
| 3855 | } |
| 3856 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 3857 | /* returns a value between 0-255 range, where higher is brighter */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3858 | static uint32_t |
Pekka Paalanen | ce72424 | 2017-09-04 12:21:24 +0300 | [diff] [blame] | 3859 | drm_get_backlight(struct drm_head *head) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3860 | { |
| 3861 | long brightness, max_brightness, norm; |
| 3862 | |
Pekka Paalanen | ce72424 | 2017-09-04 12:21:24 +0300 | [diff] [blame] | 3863 | brightness = backlight_get_brightness(head->backlight); |
| 3864 | max_brightness = backlight_get_max_brightness(head->backlight); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3865 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 3866 | /* convert it on a scale of 0 to 255 */ |
| 3867 | norm = (brightness * 255)/(max_brightness); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3868 | |
| 3869 | return (uint32_t) norm; |
| 3870 | } |
| 3871 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 3872 | /* values accepted are between 0-255 range */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3873 | static void |
| 3874 | drm_set_backlight(struct weston_output *output_base, uint32_t value) |
| 3875 | { |
Pekka Paalanen | ecc8cce | 2017-09-12 16:14:31 +0300 | [diff] [blame] | 3876 | struct drm_output *output = to_drm_output(output_base); |
| 3877 | struct drm_head *head; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3878 | long max_brightness, new_brightness; |
| 3879 | |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 3880 | if (value > 255) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3881 | return; |
| 3882 | |
Pekka Paalanen | ecc8cce | 2017-09-12 16:14:31 +0300 | [diff] [blame] | 3883 | wl_list_for_each(head, &output->base.head_list, base.output_link) { |
| 3884 | if (!head->backlight) |
| 3885 | return; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3886 | |
Pekka Paalanen | ecc8cce | 2017-09-12 16:14:31 +0300 | [diff] [blame] | 3887 | max_brightness = backlight_get_max_brightness(head->backlight); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3888 | |
Pekka Paalanen | ecc8cce | 2017-09-12 16:14:31 +0300 | [diff] [blame] | 3889 | /* get denormalized value */ |
| 3890 | new_brightness = (value * max_brightness) / 255; |
| 3891 | |
| 3892 | backlight_set_brightness(head->backlight, new_brightness); |
| 3893 | } |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3894 | } |
| 3895 | |
Pekka Paalanen | f8b850d | 2017-11-15 12:51:01 +0200 | [diff] [blame] | 3896 | static void |
| 3897 | drm_output_init_backlight(struct drm_output *output) |
| 3898 | { |
| 3899 | struct weston_head *base; |
| 3900 | struct drm_head *head; |
| 3901 | |
| 3902 | output->base.set_backlight = NULL; |
| 3903 | |
| 3904 | wl_list_for_each(base, &output->base.head_list, output_link) { |
| 3905 | head = to_drm_head(base); |
| 3906 | |
| 3907 | if (head->backlight) { |
| 3908 | weston_log("Initialized backlight for head '%s', device %s\n", |
| 3909 | head->base.name, head->backlight->path); |
| 3910 | |
| 3911 | if (!output->base.set_backlight) { |
| 3912 | output->base.set_backlight = drm_set_backlight; |
| 3913 | output->base.backlight_current = |
| 3914 | drm_get_backlight(head); |
| 3915 | } |
| 3916 | } |
| 3917 | } |
| 3918 | |
| 3919 | if (!output->base.set_backlight) { |
| 3920 | weston_log("No backlight control for output '%s'\n", |
| 3921 | output->base.name); |
| 3922 | } |
| 3923 | } |
| 3924 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 3925 | /** |
| 3926 | * Power output on or off |
| 3927 | * |
| 3928 | * The DPMS/power level of an output is used to switch it on or off. This |
| 3929 | * is DRM's hook for doing so, which can called either as part of repaint, |
| 3930 | * or independently of the repaint loop. |
| 3931 | * |
| 3932 | * If we are called as part of repaint, we simply set the relevant bit in |
| 3933 | * state and return. |
| 3934 | */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3935 | static void |
| 3936 | drm_set_dpms(struct weston_output *output_base, enum dpms_enum level) |
| 3937 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 3938 | struct drm_output *output = to_drm_output(output_base); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 3939 | struct drm_backend *b = to_drm_backend(output_base->compositor); |
| 3940 | struct drm_pending_state *pending_state = b->repaint_data; |
| 3941 | struct drm_output_state *state; |
Daniel Stone | 36609c7 | 2015-06-18 07:49:02 +0100 | [diff] [blame] | 3942 | int ret; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3943 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 3944 | if (output->state_cur->dpms == level) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3945 | return; |
| 3946 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 3947 | /* If we're being called during the repaint loop, then this is |
| 3948 | * simple: discard any previously-generated state, and create a new |
| 3949 | * state where we disable everything. When we come to flush, this |
| 3950 | * will be applied. |
| 3951 | * |
| 3952 | * However, we need to be careful: we can be called whilst another |
| 3953 | * output is in its repaint cycle (pending_state exists), but our |
| 3954 | * output still has an incomplete state application outstanding. |
| 3955 | * In that case, we need to wait until that completes. */ |
| 3956 | if (pending_state && !output->state_last) { |
| 3957 | /* The repaint loop already sets DPMS on; we don't need to |
| 3958 | * explicitly set it on here, as it will already happen |
| 3959 | * whilst applying the repaint state. */ |
| 3960 | if (level == WESTON_DPMS_ON) |
| 3961 | return; |
| 3962 | |
| 3963 | state = drm_pending_state_get_output(pending_state, output); |
| 3964 | if (state) |
| 3965 | drm_output_state_free(state); |
| 3966 | state = drm_output_get_disable_state(pending_state, output); |
Daniel Stone | 36609c7 | 2015-06-18 07:49:02 +0100 | [diff] [blame] | 3967 | return; |
| 3968 | } |
| 3969 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 3970 | /* As we throw everything away when disabling, just send us back through |
| 3971 | * a repaint cycle. */ |
| 3972 | if (level == WESTON_DPMS_ON) { |
| 3973 | if (output->dpms_off_pending) |
| 3974 | output->dpms_off_pending = 0; |
| 3975 | weston_output_schedule_repaint(output_base); |
| 3976 | return; |
| 3977 | } |
| 3978 | |
| 3979 | /* If we've already got a request in the pipeline, then we need to |
| 3980 | * park our DPMS request until that request has quiesced. */ |
| 3981 | if (output->state_last) { |
| 3982 | output->dpms_off_pending = 1; |
| 3983 | return; |
| 3984 | } |
| 3985 | |
| 3986 | pending_state = drm_pending_state_alloc(b); |
| 3987 | drm_output_get_disable_state(pending_state, output); |
| 3988 | ret = drm_pending_state_apply_sync(pending_state); |
| 3989 | if (ret != 0) |
| 3990 | weston_log("drm_set_dpms: couldn't disable output?\n"); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3991 | } |
| 3992 | |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 3993 | static const char * const connector_type_names[] = { |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 3994 | [DRM_MODE_CONNECTOR_Unknown] = "Unknown", |
| 3995 | [DRM_MODE_CONNECTOR_VGA] = "VGA", |
| 3996 | [DRM_MODE_CONNECTOR_DVII] = "DVI-I", |
| 3997 | [DRM_MODE_CONNECTOR_DVID] = "DVI-D", |
| 3998 | [DRM_MODE_CONNECTOR_DVIA] = "DVI-A", |
| 3999 | [DRM_MODE_CONNECTOR_Composite] = "Composite", |
| 4000 | [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO", |
| 4001 | [DRM_MODE_CONNECTOR_LVDS] = "LVDS", |
| 4002 | [DRM_MODE_CONNECTOR_Component] = "Component", |
| 4003 | [DRM_MODE_CONNECTOR_9PinDIN] = "DIN", |
| 4004 | [DRM_MODE_CONNECTOR_DisplayPort] = "DP", |
| 4005 | [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A", |
| 4006 | [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B", |
| 4007 | [DRM_MODE_CONNECTOR_TV] = "TV", |
| 4008 | [DRM_MODE_CONNECTOR_eDP] = "eDP", |
Pekka Paalanen | ab81f15 | 2015-08-24 14:27:07 +0300 | [diff] [blame] | 4009 | #ifdef DRM_MODE_CONNECTOR_DSI |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 4010 | [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual", |
| 4011 | [DRM_MODE_CONNECTOR_DSI] = "DSI", |
Pekka Paalanen | ab81f15 | 2015-08-24 14:27:07 +0300 | [diff] [blame] | 4012 | #endif |
Kristian Høgsberg | 2f9ed71 | 2012-07-26 17:57:15 -0400 | [diff] [blame] | 4013 | }; |
| 4014 | |
Pekka Paalanen | 1f21ef1 | 2017-04-03 13:33:26 +0300 | [diff] [blame] | 4015 | /** Create a name given a DRM connector |
| 4016 | * |
| 4017 | * \param con The DRM connector whose type and id form the name. |
| 4018 | * \return A newly allocate string, or NULL on error. Must be free()'d |
| 4019 | * after use. |
| 4020 | * |
| 4021 | * The name does not identify the DRM display device. |
| 4022 | */ |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 4023 | static char * |
| 4024 | make_connector_name(const drmModeConnector *con) |
| 4025 | { |
Pekka Paalanen | 1f21ef1 | 2017-04-03 13:33:26 +0300 | [diff] [blame] | 4026 | char *name; |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 4027 | const char *type_name = NULL; |
Pekka Paalanen | 1f21ef1 | 2017-04-03 13:33:26 +0300 | [diff] [blame] | 4028 | int ret; |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 4029 | |
| 4030 | if (con->connector_type < ARRAY_LENGTH(connector_type_names)) |
| 4031 | type_name = connector_type_names[con->connector_type]; |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 4032 | |
| 4033 | if (!type_name) |
| 4034 | type_name = "UNNAMED"; |
| 4035 | |
Pekka Paalanen | 1f21ef1 | 2017-04-03 13:33:26 +0300 | [diff] [blame] | 4036 | ret = asprintf(&name, "%s-%d", type_name, con->connector_type_id); |
| 4037 | if (ret < 0) |
| 4038 | return NULL; |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 4039 | |
Pekka Paalanen | 1f21ef1 | 2017-04-03 13:33:26 +0300 | [diff] [blame] | 4040 | return name; |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 4041 | } |
| 4042 | |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 4043 | static void drm_output_fini_cursor_egl(struct drm_output *output) |
| 4044 | { |
| 4045 | unsigned int i; |
| 4046 | |
| 4047 | for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) { |
| 4048 | drm_fb_unref(output->gbm_cursor_fb[i]); |
| 4049 | output->gbm_cursor_fb[i] = NULL; |
| 4050 | } |
| 4051 | } |
| 4052 | |
| 4053 | static int |
| 4054 | drm_output_init_cursor_egl(struct drm_output *output, struct drm_backend *b) |
| 4055 | { |
| 4056 | unsigned int i; |
| 4057 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 4058 | /* No point creating cursors if we don't have a plane for them. */ |
| 4059 | if (!output->cursor_plane) |
| 4060 | return 0; |
| 4061 | |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 4062 | for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) { |
| 4063 | struct gbm_bo *bo; |
| 4064 | |
| 4065 | bo = gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height, |
| 4066 | GBM_FORMAT_ARGB8888, |
| 4067 | GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE); |
| 4068 | if (!bo) |
| 4069 | goto err; |
| 4070 | |
| 4071 | output->gbm_cursor_fb[i] = |
| 4072 | drm_fb_get_from_bo(bo, b, GBM_FORMAT_ARGB8888, |
| 4073 | BUFFER_CURSOR); |
| 4074 | if (!output->gbm_cursor_fb[i]) { |
| 4075 | gbm_bo_destroy(bo); |
| 4076 | goto err; |
| 4077 | } |
| 4078 | } |
| 4079 | |
| 4080 | return 0; |
| 4081 | |
| 4082 | err: |
| 4083 | weston_log("cursor buffers unavailable, using gl cursors\n"); |
| 4084 | b->cursors_are_broken = 1; |
| 4085 | drm_output_fini_cursor_egl(output); |
| 4086 | return -1; |
| 4087 | } |
| 4088 | |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 4089 | /* Init output state that depends on gl or gbm */ |
| 4090 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 4091 | 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] | 4092 | { |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 4093 | EGLint format[2] = { |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 4094 | output->gbm_format, |
| 4095 | fallback_format_for(output->gbm_format), |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 4096 | }; |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 4097 | int n_formats = 1; |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 4098 | |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 4099 | output->gbm_surface = gbm_surface_create(b->gbm, |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 4100 | output->base.current_mode->width, |
| 4101 | output->base.current_mode->height, |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 4102 | format[0], |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 4103 | GBM_BO_USE_SCANOUT | |
| 4104 | GBM_BO_USE_RENDERING); |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 4105 | if (!output->gbm_surface) { |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 4106 | weston_log("failed to create gbm surface\n"); |
| 4107 | return -1; |
| 4108 | } |
| 4109 | |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 4110 | if (format[1]) |
| 4111 | n_formats = 2; |
Miguel A. Vico | c095cde | 2016-05-18 17:43:00 +0200 | [diff] [blame] | 4112 | if (gl_renderer->output_window_create(&output->base, |
| 4113 | (EGLNativeWindowType)output->gbm_surface, |
| 4114 | output->gbm_surface, |
| 4115 | gl_renderer->opaque_attribs, |
| 4116 | format, |
| 4117 | n_formats) < 0) { |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 4118 | weston_log("failed to create gl renderer output state\n"); |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 4119 | gbm_surface_destroy(output->gbm_surface); |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 4120 | return -1; |
| 4121 | } |
| 4122 | |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 4123 | drm_output_init_cursor_egl(output, b); |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 4124 | |
| 4125 | return 0; |
| 4126 | } |
| 4127 | |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 4128 | static void |
| 4129 | drm_output_fini_egl(struct drm_output *output) |
| 4130 | { |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 4131 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
| 4132 | |
| 4133 | /* Destroying the GBM surface will destroy all our GBM buffers, |
| 4134 | * regardless of refcount. Ensure we destroy them here. */ |
| 4135 | if (!b->shutting_down && |
| 4136 | output->scanout_plane->state_cur->fb && |
| 4137 | output->scanout_plane->state_cur->fb->type == BUFFER_GBM_SURFACE) { |
| 4138 | drm_plane_state_free(output->scanout_plane->state_cur, true); |
| 4139 | output->scanout_plane->state_cur = |
| 4140 | drm_plane_state_alloc(NULL, output->scanout_plane); |
| 4141 | output->scanout_plane->state_cur->complete = true; |
| 4142 | } |
| 4143 | |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 4144 | gl_renderer->output_destroy(&output->base); |
| 4145 | gbm_surface_destroy(output->gbm_surface); |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 4146 | drm_output_fini_cursor_egl(output); |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 4147 | } |
| 4148 | |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 4149 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 4150 | 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] | 4151 | { |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 4152 | int w = output->base.current_mode->width; |
| 4153 | int h = output->base.current_mode->height; |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 4154 | uint32_t format = output->gbm_format; |
| 4155 | uint32_t pixman_format; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4156 | unsigned int i; |
Pekka Paalanen | dee412d | 2018-04-23 11:44:58 +0200 | [diff] [blame] | 4157 | uint32_t flags = 0; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4158 | |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 4159 | switch (format) { |
| 4160 | case GBM_FORMAT_XRGB8888: |
| 4161 | pixman_format = PIXMAN_x8r8g8b8; |
| 4162 | break; |
| 4163 | case GBM_FORMAT_RGB565: |
| 4164 | pixman_format = PIXMAN_r5g6b5; |
| 4165 | break; |
| 4166 | default: |
| 4167 | weston_log("Unsupported pixman format 0x%x\n", format); |
| 4168 | return -1; |
| 4169 | } |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4170 | |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 4171 | /* FIXME error checking */ |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4172 | for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) { |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 4173 | 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] | 4174 | if (!output->dumb[i]) |
| 4175 | goto err; |
| 4176 | |
| 4177 | output->image[i] = |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 4178 | pixman_image_create_bits(pixman_format, w, h, |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4179 | output->dumb[i]->map, |
| 4180 | output->dumb[i]->stride); |
| 4181 | if (!output->image[i]) |
| 4182 | goto err; |
| 4183 | } |
| 4184 | |
Pekka Paalanen | dee412d | 2018-04-23 11:44:58 +0200 | [diff] [blame] | 4185 | if (b->use_pixman_shadow) |
| 4186 | flags |= PIXMAN_RENDERER_OUTPUT_USE_SHADOW; |
| 4187 | |
| 4188 | if (pixman_renderer_output_create(&output->base, flags) < 0) |
| 4189 | goto err; |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 4190 | |
Pekka Paalanen | dee412d | 2018-04-23 11:44:58 +0200 | [diff] [blame] | 4191 | weston_log("DRM: output %s %s shadow framebuffer.\n", output->base.name, |
| 4192 | b->use_pixman_shadow ? "uses" : "does not use"); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4193 | |
| 4194 | pixman_region32_init_rect(&output->previous_damage, |
Alexander Larsson | 0b13506 | 2013-05-28 16:23:36 +0200 | [diff] [blame] | 4195 | 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] | 4196 | |
| 4197 | return 0; |
| 4198 | |
| 4199 | err: |
| 4200 | for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) { |
| 4201 | if (output->dumb[i]) |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 4202 | drm_fb_unref(output->dumb[i]); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4203 | if (output->image[i]) |
| 4204 | pixman_image_unref(output->image[i]); |
| 4205 | |
| 4206 | output->dumb[i] = NULL; |
| 4207 | output->image[i] = NULL; |
| 4208 | } |
| 4209 | |
| 4210 | return -1; |
| 4211 | } |
| 4212 | |
| 4213 | static void |
| 4214 | drm_output_fini_pixman(struct drm_output *output) |
| 4215 | { |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 4216 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4217 | unsigned int i; |
| 4218 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 4219 | /* Destroying the Pixman surface will destroy all our buffers, |
| 4220 | * regardless of refcount. Ensure we destroy them here. */ |
| 4221 | if (!b->shutting_down && |
| 4222 | output->scanout_plane->state_cur->fb && |
| 4223 | output->scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB) { |
| 4224 | drm_plane_state_free(output->scanout_plane->state_cur, true); |
| 4225 | output->scanout_plane->state_cur = |
| 4226 | drm_plane_state_alloc(NULL, output->scanout_plane); |
| 4227 | output->scanout_plane->state_cur->complete = true; |
| 4228 | } |
| 4229 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4230 | pixman_renderer_output_destroy(&output->base); |
| 4231 | pixman_region32_fini(&output->previous_damage); |
| 4232 | |
| 4233 | for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4234 | pixman_image_unref(output->image[i]); |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 4235 | drm_fb_unref(output->dumb[i]); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4236 | output->dumb[i] = NULL; |
| 4237 | output->image[i] = NULL; |
| 4238 | } |
| 4239 | } |
| 4240 | |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4241 | static void |
| 4242 | edid_parse_string(const uint8_t *data, char text[]) |
| 4243 | { |
| 4244 | int i; |
| 4245 | int replaced = 0; |
| 4246 | |
| 4247 | /* this is always 12 bytes, but we can't guarantee it's null |
| 4248 | * terminated or not junk. */ |
| 4249 | strncpy(text, (const char *) data, 12); |
| 4250 | |
Bryce Harrington | 9c7de16 | 2015-08-28 13:04:26 -0700 | [diff] [blame] | 4251 | /* guarantee our new string is null-terminated */ |
| 4252 | text[12] = '\0'; |
| 4253 | |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4254 | /* remove insane chars */ |
| 4255 | for (i = 0; text[i] != '\0'; i++) { |
| 4256 | if (text[i] == '\n' || |
| 4257 | text[i] == '\r') { |
| 4258 | text[i] = '\0'; |
| 4259 | break; |
| 4260 | } |
| 4261 | } |
| 4262 | |
| 4263 | /* ensure string is printable */ |
| 4264 | for (i = 0; text[i] != '\0'; i++) { |
| 4265 | if (!isprint(text[i])) { |
| 4266 | text[i] = '-'; |
| 4267 | replaced++; |
| 4268 | } |
| 4269 | } |
| 4270 | |
| 4271 | /* if the string is random junk, ignore the string */ |
| 4272 | if (replaced > 4) |
| 4273 | text[0] = '\0'; |
| 4274 | } |
| 4275 | |
| 4276 | #define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe |
| 4277 | #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc |
| 4278 | #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff |
| 4279 | #define EDID_OFFSET_DATA_BLOCKS 0x36 |
| 4280 | #define EDID_OFFSET_LAST_BLOCK 0x6c |
| 4281 | #define EDID_OFFSET_PNPID 0x08 |
| 4282 | #define EDID_OFFSET_SERIAL 0x0c |
| 4283 | |
| 4284 | static int |
| 4285 | edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length) |
| 4286 | { |
| 4287 | int i; |
| 4288 | uint32_t serial_number; |
| 4289 | |
| 4290 | /* check header */ |
| 4291 | if (length < 128) |
| 4292 | return -1; |
| 4293 | if (data[0] != 0x00 || data[1] != 0xff) |
| 4294 | return -1; |
| 4295 | |
| 4296 | /* decode the PNP ID from three 5 bit words packed into 2 bytes |
| 4297 | * /--08--\/--09--\ |
| 4298 | * 7654321076543210 |
| 4299 | * |\---/\---/\---/ |
| 4300 | * R C1 C2 C3 */ |
| 4301 | edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1; |
| 4302 | edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1; |
| 4303 | edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1; |
| 4304 | edid->pnp_id[3] = '\0'; |
| 4305 | |
| 4306 | /* maybe there isn't a ASCII serial number descriptor, so use this instead */ |
| 4307 | serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0]; |
| 4308 | serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100; |
| 4309 | serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000; |
| 4310 | serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000; |
| 4311 | if (serial_number > 0) |
| 4312 | sprintf(edid->serial_number, "%lu", (unsigned long) serial_number); |
| 4313 | |
| 4314 | /* parse EDID data */ |
| 4315 | for (i = EDID_OFFSET_DATA_BLOCKS; |
| 4316 | i <= EDID_OFFSET_LAST_BLOCK; |
| 4317 | i += 18) { |
| 4318 | /* ignore pixel clock data */ |
| 4319 | if (data[i] != 0) |
| 4320 | continue; |
| 4321 | if (data[i+2] != 0) |
| 4322 | continue; |
| 4323 | |
| 4324 | /* any useful blocks? */ |
| 4325 | if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) { |
| 4326 | edid_parse_string(&data[i+5], |
| 4327 | edid->monitor_name); |
| 4328 | } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) { |
| 4329 | edid_parse_string(&data[i+5], |
| 4330 | edid->serial_number); |
| 4331 | } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) { |
| 4332 | edid_parse_string(&data[i+5], |
| 4333 | edid->eisa_id); |
| 4334 | } |
| 4335 | } |
| 4336 | return 0; |
| 4337 | } |
| 4338 | |
Pekka Paalanen | 6f1866b | 2017-04-03 14:22:51 +0300 | [diff] [blame] | 4339 | /** Parse monitor make, model and serial from EDID |
| 4340 | * |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 4341 | * \param head The head whose \c drm_edid to fill in. |
Pekka Paalanen | 6f1866b | 2017-04-03 14:22:51 +0300 | [diff] [blame] | 4342 | * \param props The DRM connector properties to get the EDID from. |
| 4343 | * \param make[out] The monitor make (PNP ID). |
| 4344 | * \param model[out] The monitor model (name). |
| 4345 | * \param serial_number[out] The monitor serial number. |
| 4346 | * |
| 4347 | * Each of \c *make, \c *model and \c *serial_number are set only if the |
| 4348 | * information is found in the EDID. The pointers they are set to must not |
| 4349 | * be free()'d explicitly, instead they get implicitly freed when the |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 4350 | * \c drm_head is destroyed. |
Pekka Paalanen | 6f1866b | 2017-04-03 14:22:51 +0300 | [diff] [blame] | 4351 | */ |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4352 | static void |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 4353 | find_and_parse_output_edid(struct drm_head *head, |
Pekka Paalanen | 6f1866b | 2017-04-03 14:22:51 +0300 | [diff] [blame] | 4354 | drmModeObjectPropertiesPtr props, |
| 4355 | const char **make, |
| 4356 | const char **model, |
| 4357 | const char **serial_number) |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4358 | { |
| 4359 | drmModePropertyBlobPtr edid_blob = NULL; |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 4360 | uint32_t blob_id; |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4361 | int rc; |
| 4362 | |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 4363 | blob_id = |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 4364 | drm_property_get_value(&head->props_conn[WDRM_CONNECTOR_EDID], |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 4365 | props, 0); |
| 4366 | if (!blob_id) |
| 4367 | return; |
| 4368 | |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 4369 | edid_blob = drmModeGetPropertyBlob(head->backend->drm.fd, blob_id); |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4370 | if (!edid_blob) |
| 4371 | return; |
| 4372 | |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 4373 | rc = edid_parse(&head->edid, |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4374 | edid_blob->data, |
| 4375 | edid_blob->length); |
| 4376 | if (!rc) { |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 4377 | if (head->edid.pnp_id[0] != '\0') |
| 4378 | *make = head->edid.pnp_id; |
| 4379 | if (head->edid.monitor_name[0] != '\0') |
| 4380 | *model = head->edid.monitor_name; |
| 4381 | if (head->edid.serial_number[0] != '\0') |
| 4382 | *serial_number = head->edid.serial_number; |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4383 | } |
| 4384 | drmModeFreePropertyBlob(edid_blob); |
| 4385 | } |
| 4386 | |
Kristian Høgsberg | a30989a | 2013-05-23 17:23:15 -0400 | [diff] [blame] | 4387 | static int |
| 4388 | parse_modeline(const char *s, drmModeModeInfo *mode) |
| 4389 | { |
| 4390 | char hsync[16]; |
| 4391 | char vsync[16]; |
| 4392 | float fclock; |
| 4393 | |
Pekka Paalanen | dc4e3c6 | 2017-12-05 15:37:41 +0200 | [diff] [blame] | 4394 | memset(mode, 0, sizeof *mode); |
| 4395 | |
Kristian Høgsberg | a30989a | 2013-05-23 17:23:15 -0400 | [diff] [blame] | 4396 | mode->type = DRM_MODE_TYPE_USERDEF; |
| 4397 | mode->hskew = 0; |
| 4398 | mode->vscan = 0; |
| 4399 | mode->vrefresh = 0; |
| 4400 | mode->flags = 0; |
| 4401 | |
Rob Bradford | 307e09e | 2013-07-26 16:29:40 +0100 | [diff] [blame] | 4402 | 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] | 4403 | &fclock, |
| 4404 | &mode->hdisplay, |
| 4405 | &mode->hsync_start, |
| 4406 | &mode->hsync_end, |
| 4407 | &mode->htotal, |
| 4408 | &mode->vdisplay, |
| 4409 | &mode->vsync_start, |
| 4410 | &mode->vsync_end, |
| 4411 | &mode->vtotal, hsync, vsync) != 11) |
| 4412 | return -1; |
| 4413 | |
| 4414 | mode->clock = fclock * 1000; |
Guido Günther | 92278e0 | 2018-06-26 20:40:08 +0200 | [diff] [blame] | 4415 | if (strcasecmp(hsync, "+hsync") == 0) |
Kristian Høgsberg | a30989a | 2013-05-23 17:23:15 -0400 | [diff] [blame] | 4416 | mode->flags |= DRM_MODE_FLAG_PHSYNC; |
Guido Günther | 92278e0 | 2018-06-26 20:40:08 +0200 | [diff] [blame] | 4417 | else if (strcasecmp(hsync, "-hsync") == 0) |
Kristian Høgsberg | a30989a | 2013-05-23 17:23:15 -0400 | [diff] [blame] | 4418 | mode->flags |= DRM_MODE_FLAG_NHSYNC; |
| 4419 | else |
| 4420 | return -1; |
| 4421 | |
Guido Günther | 92278e0 | 2018-06-26 20:40:08 +0200 | [diff] [blame] | 4422 | if (strcasecmp(vsync, "+vsync") == 0) |
Kristian Høgsberg | a30989a | 2013-05-23 17:23:15 -0400 | [diff] [blame] | 4423 | mode->flags |= DRM_MODE_FLAG_PVSYNC; |
Guido Günther | 92278e0 | 2018-06-26 20:40:08 +0200 | [diff] [blame] | 4424 | else if (strcasecmp(vsync, "-vsync") == 0) |
Kristian Høgsberg | a30989a | 2013-05-23 17:23:15 -0400 | [diff] [blame] | 4425 | mode->flags |= DRM_MODE_FLAG_NVSYNC; |
| 4426 | else |
| 4427 | return -1; |
| 4428 | |
Emmanuel Gil Peyrot | a62138b | 2016-05-02 22:40:11 +0100 | [diff] [blame] | 4429 | snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f", |
| 4430 | mode->hdisplay, mode->vdisplay, fclock); |
| 4431 | |
Kristian Høgsberg | a30989a | 2013-05-23 17:23:15 -0400 | [diff] [blame] | 4432 | return 0; |
| 4433 | } |
| 4434 | |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 4435 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 4436 | setup_output_seat_constraint(struct drm_backend *b, |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 4437 | struct weston_output *output, |
| 4438 | const char *s) |
| 4439 | { |
| 4440 | if (strcmp(s, "") != 0) { |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 4441 | struct weston_pointer *pointer; |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 4442 | struct udev_seat *seat; |
| 4443 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 4444 | seat = udev_seat_get_named(&b->input, s); |
Derek Foreman | 0720ea3 | 2015-07-15 13:00:35 -0500 | [diff] [blame] | 4445 | if (!seat) |
| 4446 | return; |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 4447 | |
Derek Foreman | 0720ea3 | 2015-07-15 13:00:35 -0500 | [diff] [blame] | 4448 | seat->base.output = output; |
| 4449 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 4450 | pointer = weston_seat_get_pointer(&seat->base); |
| 4451 | if (pointer) |
| 4452 | weston_pointer_clamp(pointer, |
| 4453 | &pointer->x, |
| 4454 | &pointer->y); |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 4455 | } |
| 4456 | } |
| 4457 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4458 | static int |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 4459 | drm_output_attach_head(struct weston_output *output_base, |
| 4460 | struct weston_head *head_base) |
| 4461 | { |
Pekka Paalanen | d5f98d8 | 2017-12-08 14:45:00 +0200 | [diff] [blame] | 4462 | struct drm_backend *b = to_drm_backend(output_base->compositor); |
| 4463 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 4464 | if (wl_list_length(&output_base->head_list) >= MAX_CLONED_CONNECTORS) |
| 4465 | return -1; |
| 4466 | |
Pekka Paalanen | d5f98d8 | 2017-12-08 14:45:00 +0200 | [diff] [blame] | 4467 | if (!output_base->enabled) |
| 4468 | return 0; |
| 4469 | |
| 4470 | /* XXX: ensure the configuration will work. |
| 4471 | * This is actually impossible without major infrastructure |
| 4472 | * work. */ |
| 4473 | |
| 4474 | /* Need to go through modeset to add connectors. */ |
| 4475 | /* XXX: Ideally we'd do this per-output, not globally. */ |
| 4476 | /* XXX: Doing it globally, what guarantees another output's update |
| 4477 | * will not clear the flag before this output is updated? |
| 4478 | */ |
| 4479 | b->state_invalid = true; |
| 4480 | |
| 4481 | weston_output_schedule_repaint(output_base); |
| 4482 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 4483 | return 0; |
| 4484 | } |
| 4485 | |
Pekka Paalanen | 7f85379 | 2017-11-29 14:33:33 +0200 | [diff] [blame] | 4486 | static void |
| 4487 | drm_output_detach_head(struct weston_output *output_base, |
| 4488 | struct weston_head *head_base) |
| 4489 | { |
| 4490 | struct drm_backend *b = to_drm_backend(output_base->compositor); |
| 4491 | |
| 4492 | if (!output_base->enabled) |
| 4493 | return; |
| 4494 | |
| 4495 | /* Need to go through modeset to drop connectors that should no longer |
| 4496 | * be driven. */ |
| 4497 | /* XXX: Ideally we'd do this per-output, not globally. */ |
| 4498 | b->state_invalid = true; |
| 4499 | |
| 4500 | weston_output_schedule_repaint(output_base); |
| 4501 | } |
| 4502 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 4503 | static int |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4504 | 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] | 4505 | { |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 4506 | int ret = 0; |
| 4507 | |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 4508 | if (s == NULL) |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4509 | *gbm_format = default_value; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 4510 | else if (strcmp(s, "xrgb8888") == 0) |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4511 | *gbm_format = GBM_FORMAT_XRGB8888; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 4512 | else if (strcmp(s, "rgb565") == 0) |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4513 | *gbm_format = GBM_FORMAT_RGB565; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 4514 | else if (strcmp(s, "xrgb2101010") == 0) |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4515 | *gbm_format = GBM_FORMAT_XRGB2101010; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 4516 | else { |
| 4517 | weston_log("fatal: unrecognized pixel format: %s\n", s); |
| 4518 | ret = -1; |
| 4519 | } |
| 4520 | |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 4521 | return ret; |
| 4522 | } |
| 4523 | |
Pekka Paalanen | f005f25 | 2017-11-10 16:34:39 +0200 | [diff] [blame] | 4524 | static uint32_t |
| 4525 | u32distance(uint32_t a, uint32_t b) |
| 4526 | { |
| 4527 | if (a < b) |
| 4528 | return b - a; |
| 4529 | else |
| 4530 | return a - b; |
| 4531 | } |
| 4532 | |
| 4533 | /** Choose equivalent mode |
| 4534 | * |
| 4535 | * If the two modes are not equivalent, return NULL. |
| 4536 | * Otherwise return the mode that is more likely to work in place of both. |
| 4537 | * |
| 4538 | * None of the fuzzy matching criteria in this function have any justification. |
| 4539 | * |
| 4540 | * typedef struct _drmModeModeInfo { |
| 4541 | * uint32_t clock; |
| 4542 | * uint16_t hdisplay, hsync_start, hsync_end, htotal, hskew; |
| 4543 | * uint16_t vdisplay, vsync_start, vsync_end, vtotal, vscan; |
| 4544 | * |
| 4545 | * uint32_t vrefresh; |
| 4546 | * |
| 4547 | * uint32_t flags; |
| 4548 | * uint32_t type; |
| 4549 | * char name[DRM_DISPLAY_MODE_LEN]; |
| 4550 | * } drmModeModeInfo, *drmModeModeInfoPtr; |
| 4551 | */ |
| 4552 | static const drmModeModeInfo * |
| 4553 | drm_mode_pick_equivalent(const drmModeModeInfo *a, const drmModeModeInfo *b) |
| 4554 | { |
| 4555 | uint32_t refresh_a, refresh_b; |
| 4556 | |
| 4557 | if (a->hdisplay != b->hdisplay || a->vdisplay != b->vdisplay) |
| 4558 | return NULL; |
| 4559 | |
| 4560 | if (a->flags != b->flags) |
| 4561 | return NULL; |
| 4562 | |
| 4563 | /* kHz */ |
| 4564 | if (u32distance(a->clock, b->clock) > 500) |
| 4565 | return NULL; |
| 4566 | |
| 4567 | refresh_a = drm_refresh_rate_mHz(a); |
| 4568 | refresh_b = drm_refresh_rate_mHz(b); |
| 4569 | if (u32distance(refresh_a, refresh_b) > 50) |
| 4570 | return NULL; |
| 4571 | |
| 4572 | if ((a->type ^ b->type) & DRM_MODE_TYPE_PREFERRED) { |
| 4573 | if (a->type & DRM_MODE_TYPE_PREFERRED) |
| 4574 | return a; |
| 4575 | else |
| 4576 | return b; |
| 4577 | } |
| 4578 | |
| 4579 | return a; |
| 4580 | } |
| 4581 | |
| 4582 | /* If the given mode info is not already in the list, add it. |
| 4583 | * If it is in the list, either keep the existing or replace it, |
| 4584 | * depending on which one is "better". |
| 4585 | */ |
| 4586 | static int |
| 4587 | drm_output_try_add_mode(struct drm_output *output, const drmModeModeInfo *info) |
| 4588 | { |
| 4589 | struct weston_mode *base; |
| 4590 | struct drm_mode *mode; |
| 4591 | struct drm_backend *backend; |
| 4592 | const drmModeModeInfo *chosen = NULL; |
| 4593 | |
| 4594 | assert(info); |
| 4595 | |
| 4596 | wl_list_for_each(base, &output->base.mode_list, link) { |
| 4597 | mode = to_drm_mode(base); |
| 4598 | chosen = drm_mode_pick_equivalent(&mode->mode_info, info); |
| 4599 | if (chosen) |
| 4600 | break; |
| 4601 | } |
| 4602 | |
| 4603 | if (chosen == info) { |
| 4604 | backend = to_drm_backend(output->base.compositor); |
| 4605 | drm_output_destroy_mode(backend, mode); |
| 4606 | chosen = NULL; |
| 4607 | } |
| 4608 | |
| 4609 | if (!chosen) { |
| 4610 | mode = drm_output_add_mode(output, info); |
| 4611 | if (!mode) |
| 4612 | return -1; |
| 4613 | } |
| 4614 | /* else { the equivalent mode is already in the list } */ |
| 4615 | |
| 4616 | return 0; |
| 4617 | } |
| 4618 | |
Pekka Paalanen | 4be2485 | 2017-09-11 15:01:12 +0300 | [diff] [blame] | 4619 | /** Rewrite the output's mode list |
| 4620 | * |
| 4621 | * @param output The output. |
| 4622 | * @return 0 on success, -1 on failure. |
| 4623 | * |
| 4624 | * Destroy all existing modes in the list, and reconstruct a new list from |
| 4625 | * scratch, based on the currently attached heads. |
| 4626 | * |
| 4627 | * On failure the output's mode list may contain some modes. |
| 4628 | */ |
| 4629 | static int |
| 4630 | drm_output_update_modelist_from_heads(struct drm_output *output) |
| 4631 | { |
| 4632 | struct drm_backend *backend = to_drm_backend(output->base.compositor); |
| 4633 | struct weston_head *head_base; |
| 4634 | struct drm_head *head; |
Pekka Paalanen | 4be2485 | 2017-09-11 15:01:12 +0300 | [diff] [blame] | 4635 | int i; |
Pekka Paalanen | f005f25 | 2017-11-10 16:34:39 +0200 | [diff] [blame] | 4636 | int ret; |
Pekka Paalanen | 4be2485 | 2017-09-11 15:01:12 +0300 | [diff] [blame] | 4637 | |
| 4638 | assert(!output->base.enabled); |
| 4639 | |
| 4640 | drm_mode_list_destroy(backend, &output->base.mode_list); |
| 4641 | |
Pekka Paalanen | f005f25 | 2017-11-10 16:34:39 +0200 | [diff] [blame] | 4642 | wl_list_for_each(head_base, &output->base.head_list, output_link) { |
| 4643 | head = to_drm_head(head_base); |
| 4644 | for (i = 0; i < head->connector->count_modes; i++) { |
| 4645 | ret = drm_output_try_add_mode(output, |
| 4646 | &head->connector->modes[i]); |
| 4647 | if (ret < 0) |
| 4648 | return -1; |
| 4649 | } |
Pekka Paalanen | 4be2485 | 2017-09-11 15:01:12 +0300 | [diff] [blame] | 4650 | } |
| 4651 | |
| 4652 | return 0; |
| 4653 | } |
| 4654 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4655 | /** |
| 4656 | * Choose suitable mode for an output |
| 4657 | * |
| 4658 | * Find the most suitable mode to use for initial setup (or reconfiguration on |
| 4659 | * hotplug etc) for a DRM output. |
| 4660 | * |
| 4661 | * @param output DRM output to choose mode for |
| 4662 | * @param kind Strategy and preference to use when choosing mode |
| 4663 | * @param width Desired width for this output |
| 4664 | * @param height Desired height for this output |
| 4665 | * @param current_mode Mode currently being displayed on this output |
| 4666 | * @param modeline Manually-entered mode (may be NULL) |
| 4667 | * @returns A mode from the output's mode list, or NULL if none available |
| 4668 | */ |
| 4669 | static struct drm_mode * |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4670 | drm_output_choose_initial_mode(struct drm_backend *backend, |
| 4671 | struct drm_output *output, |
| 4672 | enum weston_drm_backend_output_mode mode, |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4673 | const char *modeline, |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4674 | const drmModeModeInfo *current_mode) |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4675 | { |
| 4676 | struct drm_mode *preferred = NULL; |
| 4677 | struct drm_mode *current = NULL; |
| 4678 | struct drm_mode *configured = NULL; |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 4679 | struct drm_mode *config_fall_back = NULL; |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4680 | struct drm_mode *best = NULL; |
| 4681 | struct drm_mode *drm_mode; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4682 | drmModeModeInfo drm_modeline; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4683 | int32_t width = 0; |
| 4684 | int32_t height = 0; |
Fabien Dessenne | 2d66a7d | 2017-01-17 17:17:21 +0100 | [diff] [blame] | 4685 | uint32_t refresh = 0; |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 4686 | uint32_t aspect_width = 0; |
| 4687 | uint32_t aspect_height = 0; |
| 4688 | enum weston_mode_aspect_ratio aspect_ratio = WESTON_MODE_PIC_AR_NONE; |
Fabien Dessenne | 2d66a7d | 2017-01-17 17:17:21 +0100 | [diff] [blame] | 4689 | int n; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4690 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4691 | if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) { |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 4692 | n = sscanf(modeline, "%dx%d@%d %u:%u", &width, &height, |
| 4693 | &refresh, &aspect_width, &aspect_height); |
| 4694 | if (backend->aspect_ratio_supported && n == 5) { |
| 4695 | if (aspect_width == 4 && aspect_height == 3) |
| 4696 | aspect_ratio = WESTON_MODE_PIC_AR_4_3; |
| 4697 | else if (aspect_width == 16 && aspect_height == 9) |
| 4698 | aspect_ratio = WESTON_MODE_PIC_AR_16_9; |
| 4699 | else if (aspect_width == 64 && aspect_height == 27) |
| 4700 | aspect_ratio = WESTON_MODE_PIC_AR_64_27; |
| 4701 | else if (aspect_width == 256 && aspect_height == 135) |
| 4702 | aspect_ratio = WESTON_MODE_PIC_AR_256_135; |
| 4703 | else |
| 4704 | weston_log("Invalid modeline \"%s\" for output %s\n", |
| 4705 | modeline, output->base.name); |
| 4706 | } |
| 4707 | if (n != 2 && n != 3 && n != 5) { |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4708 | width = -1; |
| 4709 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4710 | if (parse_modeline(modeline, &drm_modeline) == 0) { |
| 4711 | configured = drm_output_add_mode(output, &drm_modeline); |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4712 | if (!configured) |
| 4713 | return NULL; |
| 4714 | } else { |
| 4715 | weston_log("Invalid modeline \"%s\" for output %s\n", |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4716 | modeline, output->base.name); |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4717 | } |
| 4718 | } |
| 4719 | } |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4720 | |
| 4721 | 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] | 4722 | if (width == drm_mode->base.width && |
Fabien Dessenne | 2d66a7d | 2017-01-17 17:17:21 +0100 | [diff] [blame] | 4723 | height == drm_mode->base.height && |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 4724 | (refresh == 0 || refresh == drm_mode->mode_info.vrefresh)) { |
| 4725 | if (!backend->aspect_ratio_supported || |
| 4726 | aspect_ratio == drm_mode->base.aspect_ratio) |
| 4727 | configured = drm_mode; |
| 4728 | else |
| 4729 | config_fall_back = drm_mode; |
| 4730 | } |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4731 | |
comic fans | 7a5c562 | 2016-03-17 14:29:27 +0200 | [diff] [blame] | 4732 | if (memcmp(current_mode, &drm_mode->mode_info, |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4733 | sizeof *current_mode) == 0) |
| 4734 | current = drm_mode; |
| 4735 | |
| 4736 | if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED) |
| 4737 | preferred = drm_mode; |
| 4738 | |
| 4739 | best = drm_mode; |
| 4740 | } |
| 4741 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4742 | if (current == NULL && current_mode->clock != 0) { |
| 4743 | current = drm_output_add_mode(output, current_mode); |
| 4744 | if (!current) |
| 4745 | return NULL; |
| 4746 | } |
| 4747 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4748 | if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT) |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4749 | configured = current; |
| 4750 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4751 | if (configured) |
| 4752 | return configured; |
| 4753 | |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 4754 | if (config_fall_back) |
| 4755 | return config_fall_back; |
| 4756 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4757 | if (preferred) |
| 4758 | return preferred; |
| 4759 | |
| 4760 | if (current) |
| 4761 | return current; |
| 4762 | |
| 4763 | if (best) |
| 4764 | return best; |
| 4765 | |
| 4766 | weston_log("no available modes for %s\n", output->base.name); |
| 4767 | return NULL; |
| 4768 | } |
| 4769 | |
Pekka Paalanen | eee580b | 2014-06-04 16:43:06 +0300 | [diff] [blame] | 4770 | static int |
Pekka Paalanen | 9c03a7c | 2017-11-28 14:30:10 +0200 | [diff] [blame] | 4771 | drm_head_read_current_setup(struct drm_head *head, struct drm_backend *backend) |
Pekka Paalanen | eee580b | 2014-06-04 16:43:06 +0300 | [diff] [blame] | 4772 | { |
Pekka Paalanen | 9c03a7c | 2017-11-28 14:30:10 +0200 | [diff] [blame] | 4773 | int drm_fd = backend->drm.fd; |
Pekka Paalanen | eee580b | 2014-06-04 16:43:06 +0300 | [diff] [blame] | 4774 | drmModeEncoder *encoder; |
| 4775 | drmModeCrtc *crtc; |
| 4776 | |
| 4777 | /* Get the current mode on the crtc that's currently driving |
| 4778 | * this connector. */ |
Pekka Paalanen | 9c03a7c | 2017-11-28 14:30:10 +0200 | [diff] [blame] | 4779 | encoder = drmModeGetEncoder(drm_fd, head->connector->encoder_id); |
Pekka Paalanen | eee580b | 2014-06-04 16:43:06 +0300 | [diff] [blame] | 4780 | if (encoder != NULL) { |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 4781 | head->inherited_crtc_id = encoder->crtc_id; |
| 4782 | |
Pekka Paalanen | eee580b | 2014-06-04 16:43:06 +0300 | [diff] [blame] | 4783 | crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id); |
| 4784 | drmModeFreeEncoder(encoder); |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 4785 | |
Pekka Paalanen | eee580b | 2014-06-04 16:43:06 +0300 | [diff] [blame] | 4786 | if (crtc == NULL) |
| 4787 | return -1; |
| 4788 | if (crtc->mode_valid) |
Pekka Paalanen | 6fae2be | 2017-11-28 14:33:52 +0200 | [diff] [blame] | 4789 | head->inherited_mode = crtc->mode; |
Pekka Paalanen | eee580b | 2014-06-04 16:43:06 +0300 | [diff] [blame] | 4790 | drmModeFreeCrtc(crtc); |
| 4791 | } |
| 4792 | |
| 4793 | return 0; |
| 4794 | } |
| 4795 | |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 4796 | static int |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4797 | drm_output_set_mode(struct weston_output *base, |
| 4798 | enum weston_drm_backend_output_mode mode, |
| 4799 | const char *modeline) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 4800 | { |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4801 | struct drm_output *output = to_drm_output(base); |
| 4802 | struct drm_backend *b = to_drm_backend(base->compositor); |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 4803 | struct drm_head *head = to_drm_head(weston_output_get_first_head(base)); |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4804 | |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 4805 | struct drm_mode *current; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 4806 | |
Pekka Paalanen | 4be2485 | 2017-09-11 15:01:12 +0300 | [diff] [blame] | 4807 | if (drm_output_update_modelist_from_heads(output) < 0) |
| 4808 | return -1; |
| 4809 | |
Pekka Paalanen | 13d233e | 2017-09-11 14:06:11 +0300 | [diff] [blame] | 4810 | current = drm_output_choose_initial_mode(b, output, mode, modeline, |
| 4811 | &head->inherited_mode); |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4812 | if (!current) |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 4813 | return -1; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4814 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4815 | output->base.current_mode = ¤t->base; |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 4816 | output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT; |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame] | 4817 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4818 | /* Set native_ fields, so weston_output_mode_switch_to_native() works */ |
| 4819 | output->base.native_mode = output->base.current_mode; |
| 4820 | output->base.native_scale = output->base.current_scale; |
| 4821 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4822 | return 0; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4823 | } |
| 4824 | |
| 4825 | static void |
| 4826 | drm_output_set_gbm_format(struct weston_output *base, |
| 4827 | const char *gbm_format) |
| 4828 | { |
| 4829 | struct drm_output *output = to_drm_output(base); |
| 4830 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 4831 | |
| 4832 | if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1) |
| 4833 | output->gbm_format = b->gbm_format; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 4834 | |
| 4835 | /* Without universal planes, we can't discover which formats are |
| 4836 | * supported by the primary plane; we just hope that the GBM format |
| 4837 | * works. */ |
| 4838 | if (!b->universal_planes) |
| 4839 | output->scanout_plane->formats[0] = output->gbm_format; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4840 | } |
| 4841 | |
| 4842 | static void |
| 4843 | drm_output_set_seat(struct weston_output *base, |
| 4844 | const char *seat) |
| 4845 | { |
| 4846 | struct drm_output *output = to_drm_output(base); |
| 4847 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 4848 | |
| 4849 | setup_output_seat_constraint(b, &output->base, |
| 4850 | seat ? seat : ""); |
| 4851 | } |
| 4852 | |
| 4853 | static int |
Pekka Paalanen | c4db6f7 | 2017-09-05 16:37:03 +0300 | [diff] [blame] | 4854 | drm_output_init_gamma_size(struct drm_output *output) |
| 4855 | { |
| 4856 | struct drm_backend *backend = to_drm_backend(output->base.compositor); |
| 4857 | drmModeCrtc *crtc; |
| 4858 | |
| 4859 | assert(output->base.compositor); |
| 4860 | assert(output->crtc_id != 0); |
| 4861 | crtc = drmModeGetCrtc(backend->drm.fd, output->crtc_id); |
| 4862 | if (!crtc) |
| 4863 | return -1; |
| 4864 | |
| 4865 | output->base.gamma_size = crtc->gamma_size; |
| 4866 | |
| 4867 | drmModeFreeCrtc(crtc); |
| 4868 | |
| 4869 | return 0; |
| 4870 | } |
| 4871 | |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 4872 | static uint32_t |
| 4873 | drm_head_get_possible_crtcs_mask(struct drm_head *head) |
| 4874 | { |
| 4875 | uint32_t possible_crtcs = 0; |
| 4876 | drmModeEncoder *encoder; |
| 4877 | int i; |
| 4878 | |
| 4879 | for (i = 0; i < head->connector->count_encoders; i++) { |
| 4880 | encoder = drmModeGetEncoder(head->backend->drm.fd, |
| 4881 | head->connector->encoders[i]); |
| 4882 | if (!encoder) |
| 4883 | continue; |
| 4884 | |
| 4885 | possible_crtcs |= encoder->possible_crtcs; |
| 4886 | drmModeFreeEncoder(encoder); |
| 4887 | } |
| 4888 | |
| 4889 | return possible_crtcs; |
| 4890 | } |
| 4891 | |
| 4892 | static int |
| 4893 | drm_crtc_get_index(drmModeRes *resources, uint32_t crtc_id) |
| 4894 | { |
| 4895 | int i; |
| 4896 | |
| 4897 | for (i = 0; i < resources->count_crtcs; i++) { |
| 4898 | if (resources->crtcs[i] == crtc_id) |
| 4899 | return i; |
| 4900 | } |
| 4901 | |
| 4902 | assert(0 && "unknown crtc id"); |
| 4903 | return -1; |
| 4904 | } |
| 4905 | |
| 4906 | /** Pick a CRTC that might be able to drive all attached connectors |
| 4907 | * |
| 4908 | * @param output The output whose attached heads to include. |
| 4909 | * @param resources The DRM KMS resources. |
| 4910 | * @return CRTC index, or -1 on failure or not found. |
| 4911 | */ |
| 4912 | static int |
| 4913 | drm_output_pick_crtc(struct drm_output *output, drmModeRes *resources) |
| 4914 | { |
| 4915 | struct drm_backend *backend; |
| 4916 | struct weston_head *base; |
| 4917 | struct drm_head *head; |
| 4918 | uint32_t possible_crtcs = 0xffffffff; |
| 4919 | int existing_crtc[32]; |
| 4920 | unsigned j, n = 0; |
| 4921 | uint32_t crtc_id; |
| 4922 | int best_crtc_index = -1; |
Pekka Paalanen | db4c7d7 | 2017-11-28 16:11:00 +0200 | [diff] [blame] | 4923 | int fallback_crtc_index = -1; |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 4924 | int i; |
Pekka Paalanen | db4c7d7 | 2017-11-28 16:11:00 +0200 | [diff] [blame] | 4925 | bool match; |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 4926 | |
| 4927 | backend = to_drm_backend(output->base.compositor); |
| 4928 | |
| 4929 | /* This algorithm ignores drmModeEncoder::possible_clones restriction, |
| 4930 | * because it is more often set wrong than not in the kernel. */ |
| 4931 | |
| 4932 | /* Accumulate a mask of possible crtcs and find existing routings. */ |
| 4933 | wl_list_for_each(base, &output->base.head_list, output_link) { |
| 4934 | head = to_drm_head(base); |
| 4935 | |
| 4936 | possible_crtcs &= drm_head_get_possible_crtcs_mask(head); |
| 4937 | |
| 4938 | crtc_id = head->inherited_crtc_id; |
| 4939 | if (crtc_id > 0 && n < ARRAY_LENGTH(existing_crtc)) |
| 4940 | existing_crtc[n++] = drm_crtc_get_index(resources, |
| 4941 | crtc_id); |
| 4942 | } |
| 4943 | |
| 4944 | /* Find a crtc that could drive each connector individually at least, |
| 4945 | * and prefer existing routings. */ |
| 4946 | for (i = 0; i < resources->count_crtcs; i++) { |
| 4947 | crtc_id = resources->crtcs[i]; |
| 4948 | |
| 4949 | /* Could the crtc not drive each connector? */ |
| 4950 | if (!(possible_crtcs & (1 << i))) |
| 4951 | continue; |
| 4952 | |
| 4953 | /* Is the crtc already in use? */ |
| 4954 | if (drm_output_find_by_crtc(backend, crtc_id)) |
| 4955 | continue; |
| 4956 | |
| 4957 | /* Try to preserve the existing CRTC -> connector routing; |
| 4958 | * it makes initialisation faster, and also since we have a |
| 4959 | * very dumb picking algorithm, may preserve a better |
| 4960 | * choice. */ |
| 4961 | for (j = 0; j < n; j++) { |
| 4962 | if (existing_crtc[j] == i) |
| 4963 | return i; |
| 4964 | } |
| 4965 | |
Pekka Paalanen | db4c7d7 | 2017-11-28 16:11:00 +0200 | [diff] [blame] | 4966 | /* Check if any other head had existing routing to this CRTC. |
| 4967 | * If they did, this is not the best CRTC as it might be needed |
| 4968 | * for another output we haven't enabled yet. */ |
| 4969 | match = false; |
| 4970 | wl_list_for_each(base, &backend->compositor->head_list, |
| 4971 | compositor_link) { |
| 4972 | head = to_drm_head(base); |
| 4973 | |
| 4974 | if (head->base.output == &output->base) |
| 4975 | continue; |
| 4976 | |
| 4977 | if (weston_head_is_enabled(&head->base)) |
| 4978 | continue; |
| 4979 | |
| 4980 | if (head->inherited_crtc_id == crtc_id) { |
| 4981 | match = true; |
| 4982 | break; |
| 4983 | } |
| 4984 | } |
| 4985 | if (!match) |
| 4986 | best_crtc_index = i; |
| 4987 | |
| 4988 | fallback_crtc_index = i; |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 4989 | } |
| 4990 | |
| 4991 | if (best_crtc_index != -1) |
| 4992 | return best_crtc_index; |
| 4993 | |
Pekka Paalanen | db4c7d7 | 2017-11-28 16:11:00 +0200 | [diff] [blame] | 4994 | if (fallback_crtc_index != -1) |
| 4995 | return fallback_crtc_index; |
| 4996 | |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 4997 | /* Likely possible_crtcs was empty due to asking for clones, |
| 4998 | * but since the DRM documentation says the kernel lies, let's |
| 4999 | * pick one crtc anyway. Trial and error is the only way to |
| 5000 | * be sure if something doesn't work. */ |
| 5001 | |
| 5002 | /* First pick any existing assignment. */ |
| 5003 | for (j = 0; j < n; j++) { |
| 5004 | crtc_id = resources->crtcs[existing_crtc[j]]; |
| 5005 | if (!drm_output_find_by_crtc(backend, crtc_id)) |
| 5006 | return existing_crtc[j]; |
| 5007 | } |
| 5008 | |
| 5009 | /* Otherwise pick any available crtc. */ |
| 5010 | for (i = 0; i < resources->count_crtcs; i++) { |
| 5011 | crtc_id = resources->crtcs[i]; |
| 5012 | |
| 5013 | if (!drm_output_find_by_crtc(backend, crtc_id)) |
| 5014 | return i; |
| 5015 | } |
| 5016 | |
| 5017 | return -1; |
| 5018 | } |
| 5019 | |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 5020 | /** Allocate a CRTC for the output |
| 5021 | * |
| 5022 | * @param output The output with no allocated CRTC. |
| 5023 | * @param resources DRM KMS resources. |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 5024 | * @return 0 on success, -1 on failure. |
| 5025 | * |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 5026 | * Finds a free CRTC that might drive the attached connectors, reserves the CRTC |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 5027 | * for the output, and loads the CRTC properties. |
| 5028 | * |
| 5029 | * Populates the cursor and scanout planes. |
| 5030 | * |
| 5031 | * On failure, the output remains without a CRTC. |
| 5032 | */ |
| 5033 | static int |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 5034 | drm_output_init_crtc(struct drm_output *output, drmModeRes *resources) |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 5035 | { |
| 5036 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
| 5037 | drmModeObjectPropertiesPtr props; |
| 5038 | int i; |
| 5039 | |
| 5040 | assert(output->crtc_id == 0); |
| 5041 | |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 5042 | i = drm_output_pick_crtc(output, resources); |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 5043 | if (i < 0) { |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 5044 | weston_log("Output '%s': No available CRTCs.\n", |
| 5045 | output->base.name); |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 5046 | return -1; |
| 5047 | } |
| 5048 | |
| 5049 | output->crtc_id = resources->crtcs[i]; |
| 5050 | output->pipe = i; |
| 5051 | |
| 5052 | props = drmModeObjectGetProperties(b->drm.fd, output->crtc_id, |
| 5053 | DRM_MODE_OBJECT_CRTC); |
| 5054 | if (!props) { |
| 5055 | weston_log("failed to get CRTC properties\n"); |
| 5056 | goto err_crtc; |
| 5057 | } |
| 5058 | drm_property_info_populate(b, crtc_props, output->props_crtc, |
| 5059 | WDRM_CRTC__COUNT, props); |
| 5060 | drmModeFreeObjectProperties(props); |
| 5061 | |
| 5062 | output->scanout_plane = |
| 5063 | drm_output_find_special_plane(b, output, |
| 5064 | WDRM_PLANE_TYPE_PRIMARY); |
| 5065 | if (!output->scanout_plane) { |
| 5066 | weston_log("Failed to find primary plane for output %s\n", |
| 5067 | output->base.name); |
| 5068 | goto err_crtc; |
| 5069 | } |
| 5070 | |
| 5071 | /* Failing to find a cursor plane is not fatal, as we'll fall back |
| 5072 | * to software cursor. */ |
| 5073 | output->cursor_plane = |
| 5074 | drm_output_find_special_plane(b, output, |
| 5075 | WDRM_PLANE_TYPE_CURSOR); |
| 5076 | |
Pekka Paalanen | 663d5e9 | 2017-09-08 13:32:40 +0300 | [diff] [blame] | 5077 | wl_array_remove_uint32(&b->unused_crtcs, output->crtc_id); |
| 5078 | |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 5079 | return 0; |
| 5080 | |
| 5081 | err_crtc: |
| 5082 | output->crtc_id = 0; |
| 5083 | output->pipe = 0; |
| 5084 | |
| 5085 | return -1; |
| 5086 | } |
| 5087 | |
| 5088 | /** Free the CRTC from the output |
| 5089 | * |
| 5090 | * @param output The output whose CRTC to deallocate. |
| 5091 | * |
| 5092 | * The CRTC reserved for the given output becomes free to use again. |
| 5093 | */ |
| 5094 | static void |
| 5095 | drm_output_fini_crtc(struct drm_output *output) |
| 5096 | { |
| 5097 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Pekka Paalanen | 663d5e9 | 2017-09-08 13:32:40 +0300 | [diff] [blame] | 5098 | uint32_t *unused; |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 5099 | |
| 5100 | if (!b->universal_planes && !b->shutting_down) { |
| 5101 | /* With universal planes, the 'special' planes are allocated at |
| 5102 | * startup, freed at shutdown, and live on the plane list in |
| 5103 | * between. We want the planes to continue to exist and be freed |
| 5104 | * up for other outputs. |
| 5105 | * |
| 5106 | * Without universal planes, our special planes are |
| 5107 | * pseudo-planes allocated at output creation, freed at output |
| 5108 | * destruction, and not usable by other outputs. |
| 5109 | * |
| 5110 | * On the other hand, if the compositor is already shutting down, |
| 5111 | * the plane has already been destroyed. |
| 5112 | */ |
| 5113 | if (output->cursor_plane) |
| 5114 | drm_plane_destroy(output->cursor_plane); |
| 5115 | if (output->scanout_plane) |
| 5116 | drm_plane_destroy(output->scanout_plane); |
| 5117 | } |
| 5118 | |
| 5119 | drm_property_info_free(output->props_crtc, WDRM_CRTC__COUNT); |
Pekka Paalanen | 663d5e9 | 2017-09-08 13:32:40 +0300 | [diff] [blame] | 5120 | |
| 5121 | assert(output->crtc_id != 0); |
| 5122 | |
| 5123 | unused = wl_array_add(&b->unused_crtcs, sizeof(*unused)); |
| 5124 | *unused = output->crtc_id; |
| 5125 | |
| 5126 | /* Force resetting unused CRTCs */ |
| 5127 | b->state_invalid = true; |
| 5128 | |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 5129 | output->crtc_id = 0; |
| 5130 | output->cursor_plane = NULL; |
| 5131 | output->scanout_plane = NULL; |
| 5132 | } |
| 5133 | |
Pekka Paalanen | c0eb254 | 2017-11-15 13:37:18 +0200 | [diff] [blame] | 5134 | static void |
| 5135 | drm_output_print_modes(struct drm_output *output) |
| 5136 | { |
| 5137 | struct weston_mode *m; |
| 5138 | struct drm_mode *dm; |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 5139 | const char *aspect_ratio; |
Pekka Paalanen | c0eb254 | 2017-11-15 13:37:18 +0200 | [diff] [blame] | 5140 | |
| 5141 | wl_list_for_each(m, &output->base.mode_list, link) { |
| 5142 | dm = to_drm_mode(m); |
| 5143 | |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 5144 | aspect_ratio = aspect_ratio_to_string(m->aspect_ratio); |
| 5145 | weston_log_continue(STAMP_SPACE "%dx%d@%.1f%s%s%s, %.1f MHz\n", |
Pekka Paalanen | c0eb254 | 2017-11-15 13:37:18 +0200 | [diff] [blame] | 5146 | m->width, m->height, m->refresh / 1000.0, |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 5147 | aspect_ratio, |
Pekka Paalanen | c0eb254 | 2017-11-15 13:37:18 +0200 | [diff] [blame] | 5148 | m->flags & WL_OUTPUT_MODE_PREFERRED ? |
| 5149 | ", preferred" : "", |
| 5150 | m->flags & WL_OUTPUT_MODE_CURRENT ? |
| 5151 | ", current" : "", |
| 5152 | dm->mode_info.clock / 1000.0); |
| 5153 | } |
| 5154 | } |
| 5155 | |
Pekka Paalanen | c4db6f7 | 2017-09-05 16:37:03 +0300 | [diff] [blame] | 5156 | static int |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5157 | drm_output_enable(struct weston_output *base) |
| 5158 | { |
| 5159 | struct drm_output *output = to_drm_output(base); |
| 5160 | struct drm_backend *b = to_drm_backend(base->compositor); |
Pekka Paalanen | 663d5e9 | 2017-09-08 13:32:40 +0300 | [diff] [blame] | 5161 | drmModeRes *resources; |
| 5162 | int ret; |
| 5163 | |
| 5164 | resources = drmModeGetResources(b->drm.fd); |
| 5165 | if (!resources) { |
| 5166 | weston_log("drmModeGetResources failed\n"); |
| 5167 | return -1; |
| 5168 | } |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 5169 | ret = drm_output_init_crtc(output, resources); |
Pekka Paalanen | 663d5e9 | 2017-09-08 13:32:40 +0300 | [diff] [blame] | 5170 | drmModeFreeResources(resources); |
| 5171 | if (ret < 0) |
| 5172 | return -1; |
| 5173 | |
| 5174 | if (drm_output_init_gamma_size(output) < 0) |
| 5175 | goto err; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5176 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 5177 | if (b->pageflip_timeout) |
| 5178 | drm_output_pageflip_timer_create(output); |
| 5179 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5180 | if (b->use_pixman) { |
| 5181 | if (drm_output_init_pixman(output, b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 5182 | weston_log("Failed to init output pixman state\n"); |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 5183 | goto err; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 5184 | } |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5185 | } else if (drm_output_init_egl(output, b) < 0) { |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 5186 | weston_log("Failed to init output gl state\n"); |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 5187 | goto err; |
Kristian Høgsberg | 1d1e0a5 | 2012-10-21 13:29:26 -0400 | [diff] [blame] | 5188 | } |
Kristian Høgsberg | 8e1f77f | 2012-05-03 11:39:35 -0400 | [diff] [blame] | 5189 | |
Pekka Paalanen | f8b850d | 2017-11-15 12:51:01 +0200 | [diff] [blame] | 5190 | drm_output_init_backlight(output); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 5191 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 5192 | output->base.start_repaint_loop = drm_output_start_repaint_loop; |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 5193 | output->base.repaint = drm_output_repaint; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 5194 | output->base.assign_planes = drm_assign_planes; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 5195 | output->base.set_dpms = drm_set_dpms; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 5196 | output->base.switch_mode = drm_output_switch_mode; |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 5197 | output->base.set_gamma = drm_output_set_gamma; |
| 5198 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 5199 | if (output->cursor_plane) |
| 5200 | weston_compositor_stack_plane(b->compositor, |
| 5201 | &output->cursor_plane->base, |
| 5202 | NULL); |
| 5203 | else |
| 5204 | b->cursors_are_broken = 1; |
| 5205 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 5206 | weston_compositor_stack_plane(b->compositor, |
| 5207 | &output->scanout_plane->base, |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5208 | &b->compositor->primary_plane); |
Ander Conselvan de Oliveira | 8ad1982 | 2013-03-05 17:30:27 +0200 | [diff] [blame] | 5209 | |
Pekka Paalanen | c0eb254 | 2017-11-15 13:37:18 +0200 | [diff] [blame] | 5210 | weston_log("Output %s (crtc %d) video modes:\n", |
| 5211 | output->base.name, output->crtc_id); |
| 5212 | drm_output_print_modes(output); |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 5213 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5214 | return 0; |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 5215 | |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 5216 | err: |
Pekka Paalanen | 663d5e9 | 2017-09-08 13:32:40 +0300 | [diff] [blame] | 5217 | drm_output_fini_crtc(output); |
| 5218 | |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 5219 | return -1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5220 | } |
| 5221 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 5222 | static void |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5223 | drm_output_deinit(struct weston_output *base) |
| 5224 | { |
| 5225 | struct drm_output *output = to_drm_output(base); |
| 5226 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 5227 | |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 5228 | if (b->use_pixman) |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5229 | drm_output_fini_pixman(output); |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 5230 | else |
| 5231 | drm_output_fini_egl(output); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5232 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 5233 | /* Since our planes are no longer in use anywhere, remove their base |
| 5234 | * weston_plane's link from the plane stacking list, unless we're |
| 5235 | * shutting down, in which case the plane has already been |
| 5236 | * destroyed. */ |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 5237 | if (!b->shutting_down) { |
| 5238 | wl_list_remove(&output->scanout_plane->base.link); |
| 5239 | wl_list_init(&output->scanout_plane->base.link); |
| 5240 | |
| 5241 | if (output->cursor_plane) { |
| 5242 | wl_list_remove(&output->cursor_plane->base.link); |
| 5243 | wl_list_init(&output->cursor_plane->base.link); |
| 5244 | /* Turn off hardware cursor */ |
| 5245 | drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0); |
| 5246 | } |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 5247 | } |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 5248 | |
Pekka Paalanen | 663d5e9 | 2017-09-08 13:32:40 +0300 | [diff] [blame] | 5249 | drm_output_fini_crtc(output); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5250 | } |
| 5251 | |
| 5252 | static void |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 5253 | drm_head_destroy(struct drm_head *head); |
| 5254 | |
| 5255 | static void |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5256 | drm_output_destroy(struct weston_output *base) |
| 5257 | { |
| 5258 | struct drm_output *output = to_drm_output(base); |
| 5259 | struct drm_backend *b = to_drm_backend(base->compositor); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5260 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 5261 | if (output->page_flip_pending || output->vblank_pending || |
| 5262 | output->atomic_complete_pending) { |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5263 | output->destroy_pending = 1; |
| 5264 | weston_log("destroy output while page flip pending\n"); |
| 5265 | return; |
| 5266 | } |
| 5267 | |
| 5268 | if (output->base.enabled) |
| 5269 | drm_output_deinit(&output->base); |
| 5270 | |
Pekka Paalanen | 383b3af | 2017-09-11 14:40:48 +0300 | [diff] [blame] | 5271 | drm_mode_list_destroy(b, &output->base.mode_list); |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 5272 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 5273 | if (output->pageflip_timer) |
| 5274 | wl_event_source_remove(output->pageflip_timer); |
| 5275 | |
Pekka Paalanen | ae6d35d | 2017-08-16 12:07:14 +0300 | [diff] [blame] | 5276 | weston_output_release(&output->base); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5277 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 5278 | assert(!output->state_last); |
| 5279 | drm_output_state_free(output->state_cur); |
| 5280 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5281 | free(output); |
| 5282 | } |
| 5283 | |
| 5284 | static int |
| 5285 | drm_output_disable(struct weston_output *base) |
| 5286 | { |
| 5287 | struct drm_output *output = to_drm_output(base); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5288 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 5289 | if (output->page_flip_pending || output->vblank_pending || |
| 5290 | output->atomic_complete_pending) { |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5291 | output->disable_pending = 1; |
| 5292 | return -1; |
| 5293 | } |
| 5294 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 5295 | weston_log("Disabling output %s\n", output->base.name); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 5296 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5297 | if (output->base.enabled) |
| 5298 | drm_output_deinit(&output->base); |
| 5299 | |
| 5300 | output->disable_pending = 0; |
| 5301 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5302 | return 0; |
| 5303 | } |
| 5304 | |
| 5305 | /** |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 5306 | * Update the list of unused connectors and CRTCs |
| 5307 | * |
Pekka Paalanen | eacec81 | 2017-09-12 13:43:51 +0300 | [diff] [blame] | 5308 | * This keeps the unused_crtc arrays up to date. |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 5309 | * |
| 5310 | * @param b Weston backend structure |
| 5311 | * @param resources DRM resources for this device |
| 5312 | */ |
| 5313 | static void |
| 5314 | drm_backend_update_unused_outputs(struct drm_backend *b, drmModeRes *resources) |
| 5315 | { |
| 5316 | int i; |
| 5317 | |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 5318 | wl_array_release(&b->unused_crtcs); |
| 5319 | wl_array_init(&b->unused_crtcs); |
| 5320 | |
| 5321 | for (i = 0; i < resources->count_crtcs; i++) { |
| 5322 | struct drm_output *output; |
| 5323 | uint32_t *crtc_id; |
| 5324 | |
| 5325 | output = drm_output_find_by_crtc(b, resources->crtcs[i]); |
| 5326 | if (output && output->base.enabled) |
| 5327 | continue; |
| 5328 | |
| 5329 | crtc_id = wl_array_add(&b->unused_crtcs, sizeof(*crtc_id)); |
| 5330 | *crtc_id = resources->crtcs[i]; |
| 5331 | } |
| 5332 | } |
| 5333 | |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 5334 | /** Replace connector data and monitor information |
| 5335 | * |
| 5336 | * @param head The head to update. |
| 5337 | * @param connector The connector data to be owned by the head, must match |
| 5338 | * the head's connector ID. |
| 5339 | * @return 0 on success, -1 on failure. |
| 5340 | * |
| 5341 | * Takes ownership of @c connector on success, not on failure. |
| 5342 | * |
| 5343 | * May schedule a heads changed call. |
| 5344 | */ |
| 5345 | static int |
| 5346 | drm_head_assign_connector_info(struct drm_head *head, |
| 5347 | drmModeConnector *connector) |
| 5348 | { |
| 5349 | drmModeObjectProperties *props; |
| 5350 | const char *make = "unknown"; |
| 5351 | const char *model = "unknown"; |
| 5352 | const char *serial_number = "unknown"; |
| 5353 | |
| 5354 | assert(connector); |
| 5355 | assert(head->connector_id == connector->connector_id); |
| 5356 | |
| 5357 | props = drmModeObjectGetProperties(head->backend->drm.fd, |
| 5358 | head->connector_id, |
| 5359 | DRM_MODE_OBJECT_CONNECTOR); |
| 5360 | if (!props) { |
| 5361 | weston_log("Error: failed to get connector '%s' properties\n", |
| 5362 | head->base.name); |
| 5363 | return -1; |
| 5364 | } |
| 5365 | |
| 5366 | if (head->connector) |
| 5367 | drmModeFreeConnector(head->connector); |
| 5368 | head->connector = connector; |
| 5369 | |
| 5370 | drm_property_info_populate(head->backend, connector_props, |
| 5371 | head->props_conn, |
| 5372 | WDRM_CONNECTOR__COUNT, props); |
| 5373 | find_and_parse_output_edid(head, props, &make, &model, &serial_number); |
| 5374 | weston_head_set_monitor_strings(&head->base, make, model, serial_number); |
| 5375 | weston_head_set_subpixel(&head->base, |
| 5376 | drm_subpixel_to_wayland(head->connector->subpixel)); |
| 5377 | |
| 5378 | weston_head_set_physical_size(&head->base, head->connector->mmWidth, |
| 5379 | head->connector->mmHeight); |
| 5380 | |
| 5381 | drmModeFreeObjectProperties(props); |
| 5382 | |
| 5383 | /* Unknown connection status is assumed disconnected. */ |
| 5384 | weston_head_set_connection_status(&head->base, |
| 5385 | head->connector->connection == DRM_MODE_CONNECTED); |
| 5386 | |
| 5387 | return 0; |
| 5388 | } |
| 5389 | |
Pekka Paalanen | 456dc73 | 2017-11-09 15:10:11 +0200 | [diff] [blame] | 5390 | static void |
| 5391 | drm_head_log_info(struct drm_head *head, const char *msg) |
| 5392 | { |
| 5393 | if (head->base.connected) { |
| 5394 | weston_log("DRM: head '%s' %s, connector %d is connected, " |
| 5395 | "EDID make '%s', model '%s', serial '%s'\n", |
| 5396 | head->base.name, msg, head->connector_id, |
| 5397 | head->base.make, head->base.model, |
| 5398 | head->base.serial_number ?: ""); |
| 5399 | } else { |
| 5400 | weston_log("DRM: head '%s' %s, connector %d is disconnected.\n", |
| 5401 | head->base.name, msg, head->connector_id); |
| 5402 | } |
| 5403 | } |
| 5404 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5405 | /** Update connector and monitor information |
| 5406 | * |
| 5407 | * @param head The head to update. |
| 5408 | * |
| 5409 | * Re-reads the DRM property lists for the connector and updates monitor |
| 5410 | * information and connection status. This may schedule a heads changed call |
| 5411 | * to the user. |
| 5412 | */ |
| 5413 | static void |
| 5414 | drm_head_update_info(struct drm_head *head) |
| 5415 | { |
| 5416 | drmModeConnector *connector; |
| 5417 | |
| 5418 | connector = drmModeGetConnector(head->backend->drm.fd, |
| 5419 | head->connector_id); |
| 5420 | if (!connector) { |
| 5421 | weston_log("DRM: getting connector info for '%s' failed.\n", |
| 5422 | head->base.name); |
| 5423 | return; |
| 5424 | } |
| 5425 | |
| 5426 | if (drm_head_assign_connector_info(head, connector) < 0) |
| 5427 | drmModeFreeConnector(connector); |
Pekka Paalanen | 456dc73 | 2017-11-09 15:10:11 +0200 | [diff] [blame] | 5428 | |
| 5429 | if (head->base.device_changed) |
| 5430 | drm_head_log_info(head, "updated"); |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5431 | } |
| 5432 | |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 5433 | /** |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 5434 | * Create a Weston head for a connector |
| 5435 | * |
| 5436 | * Given a DRM connector, create a matching drm_head structure and add it |
| 5437 | * to Weston's head list. |
| 5438 | * |
| 5439 | * @param b Weston backend structure |
| 5440 | * @param connector_id DRM connector ID for the head |
| 5441 | * @param drm_device udev device pointer |
| 5442 | * @returns The new head, or NULL on failure. |
| 5443 | */ |
| 5444 | static struct drm_head * |
| 5445 | drm_head_create(struct drm_backend *backend, uint32_t connector_id, |
| 5446 | struct udev_device *drm_device) |
| 5447 | { |
| 5448 | struct drm_head *head; |
| 5449 | drmModeConnector *connector; |
| 5450 | char *name; |
| 5451 | |
| 5452 | head = zalloc(sizeof *head); |
| 5453 | if (!head) |
| 5454 | return NULL; |
| 5455 | |
| 5456 | connector = drmModeGetConnector(backend->drm.fd, connector_id); |
| 5457 | if (!connector) |
| 5458 | goto err_alloc; |
| 5459 | |
| 5460 | name = make_connector_name(connector); |
| 5461 | if (!name) |
| 5462 | goto err_alloc; |
| 5463 | |
| 5464 | weston_head_init(&head->base, name); |
| 5465 | free(name); |
| 5466 | |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 5467 | head->connector_id = connector_id; |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 5468 | head->backend = backend; |
| 5469 | |
Pekka Paalanen | ce72424 | 2017-09-04 12:21:24 +0300 | [diff] [blame] | 5470 | head->backlight = backlight_init(drm_device, connector->connector_type); |
| 5471 | |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 5472 | if (drm_head_assign_connector_info(head, connector) < 0) |
| 5473 | goto err_init; |
| 5474 | |
| 5475 | if (head->connector->connector_type == DRM_MODE_CONNECTOR_LVDS || |
| 5476 | head->connector->connector_type == DRM_MODE_CONNECTOR_eDP) |
| 5477 | weston_head_set_internal(&head->base); |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 5478 | |
Pekka Paalanen | 9c03a7c | 2017-11-28 14:30:10 +0200 | [diff] [blame] | 5479 | if (drm_head_read_current_setup(head, backend) < 0) { |
Pekka Paalanen | 13d233e | 2017-09-11 14:06:11 +0300 | [diff] [blame] | 5480 | weston_log("Failed to retrieve current mode from connector %d.\n", |
| 5481 | head->connector_id); |
Pekka Paalanen | 6fae2be | 2017-11-28 14:33:52 +0200 | [diff] [blame] | 5482 | /* Not fatal. */ |
Pekka Paalanen | 13d233e | 2017-09-11 14:06:11 +0300 | [diff] [blame] | 5483 | } |
| 5484 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 5485 | weston_compositor_add_head(backend->compositor, &head->base); |
Pekka Paalanen | 456dc73 | 2017-11-09 15:10:11 +0200 | [diff] [blame] | 5486 | drm_head_log_info(head, "found"); |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 5487 | |
| 5488 | return head; |
| 5489 | |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 5490 | err_init: |
| 5491 | weston_head_release(&head->base); |
| 5492 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 5493 | err_alloc: |
| 5494 | if (connector) |
| 5495 | drmModeFreeConnector(connector); |
| 5496 | |
| 5497 | free(head); |
| 5498 | |
| 5499 | return NULL; |
| 5500 | } |
| 5501 | |
| 5502 | static void |
| 5503 | drm_head_destroy(struct drm_head *head) |
| 5504 | { |
| 5505 | weston_head_release(&head->base); |
Pekka Paalanen | ce72424 | 2017-09-04 12:21:24 +0300 | [diff] [blame] | 5506 | |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 5507 | drm_property_info_free(head->props_conn, WDRM_CONNECTOR__COUNT); |
| 5508 | drmModeFreeConnector(head->connector); |
| 5509 | |
Pekka Paalanen | ce72424 | 2017-09-04 12:21:24 +0300 | [diff] [blame] | 5510 | if (head->backlight) |
| 5511 | backlight_destroy(head->backlight); |
| 5512 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 5513 | free(head); |
| 5514 | } |
| 5515 | |
| 5516 | /** |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5517 | * Create a Weston output structure |
| 5518 | * |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5519 | * Create an "empty" drm_output. This is the implementation of |
| 5520 | * weston_backend::create_output. |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5521 | * |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5522 | * Creating an output is usually followed by drm_output_attach_head() |
| 5523 | * and drm_output_enable() to make use of it. |
| 5524 | * |
| 5525 | * @param compositor The compositor instance. |
| 5526 | * @param name Name for the new output. |
| 5527 | * @returns The output, or NULL on failure. |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5528 | */ |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5529 | static struct weston_output * |
| 5530 | drm_output_create(struct weston_compositor *compositor, const char *name) |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5531 | { |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5532 | struct drm_backend *b = to_drm_backend(compositor); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5533 | struct drm_output *output; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5534 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5535 | output = zalloc(sizeof *output); |
| 5536 | if (output == NULL) |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5537 | return NULL; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5538 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5539 | weston_output_init(&output->base, compositor, name); |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 5540 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5541 | output->base.enable = drm_output_enable; |
| 5542 | output->base.destroy = drm_output_destroy; |
| 5543 | output->base.disable = drm_output_disable; |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 5544 | output->base.attach_head = drm_output_attach_head; |
Pekka Paalanen | 7f85379 | 2017-11-29 14:33:33 +0200 | [diff] [blame] | 5545 | output->base.detach_head = drm_output_detach_head; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5546 | |
| 5547 | output->destroy_pending = 0; |
| 5548 | output->disable_pending = 0; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5549 | |
Pekka Paalanen | 01f6021 | 2017-03-24 15:39:24 +0200 | [diff] [blame] | 5550 | output->state_cur = drm_output_state_alloc(output, NULL); |
Pekka Paalanen | a0bfedc | 2017-04-03 14:42:51 +0300 | [diff] [blame] | 5551 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5552 | weston_compositor_add_pending_output(&output->base, b->compositor); |
| 5553 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5554 | return &output->base; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5555 | } |
| 5556 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5557 | static int |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5558 | drm_backend_create_heads(struct drm_backend *b, struct udev_device *drm_device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5559 | { |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5560 | struct drm_head *head; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5561 | drmModeRes *resources; |
| 5562 | int i; |
| 5563 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5564 | resources = drmModeGetResources(b->drm.fd); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5565 | if (!resources) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 5566 | weston_log("drmModeGetResources failed\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5567 | return -1; |
| 5568 | } |
| 5569 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5570 | b->min_width = resources->min_width; |
| 5571 | b->max_width = resources->max_width; |
| 5572 | b->min_height = resources->min_height; |
| 5573 | b->max_height = resources->max_height; |
Rob Clark | 4339add | 2012-08-09 14:18:28 -0500 | [diff] [blame] | 5574 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5575 | for (i = 0; i < resources->count_connectors; i++) { |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5576 | uint32_t connector_id = resources->connectors[i]; |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 5577 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5578 | head = drm_head_create(b, connector_id, drm_device); |
| 5579 | if (!head) { |
| 5580 | weston_log("DRM: failed to create head for connector %d.\n", |
| 5581 | connector_id); |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 5582 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5583 | } |
| 5584 | |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 5585 | drm_backend_update_unused_outputs(b, resources); |
| 5586 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5587 | drmModeFreeResources(resources); |
| 5588 | |
| 5589 | return 0; |
| 5590 | } |
| 5591 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5592 | static void |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5593 | drm_backend_update_heads(struct drm_backend *b, struct udev_device *drm_device) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5594 | { |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5595 | drmModeRes *resources; |
Pekka Paalanen | a0a3746 | 2017-08-31 15:41:57 +0300 | [diff] [blame] | 5596 | struct weston_head *base, *next; |
| 5597 | struct drm_head *head; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5598 | int i; |
| 5599 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5600 | resources = drmModeGetResources(b->drm.fd); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5601 | if (!resources) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 5602 | weston_log("drmModeGetResources failed\n"); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5603 | return; |
| 5604 | } |
| 5605 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5606 | /* collect new connectors that have appeared, e.g. MST */ |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5607 | for (i = 0; i < resources->count_connectors; i++) { |
Ucan, Emre (ADITG/SW1) | 21e4944 | 2017-02-02 14:06:55 +0000 | [diff] [blame] | 5608 | uint32_t connector_id = resources->connectors[i]; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 5609 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5610 | head = drm_head_find_by_connector(b, connector_id); |
| 5611 | if (head) { |
| 5612 | drm_head_update_info(head); |
| 5613 | } else { |
| 5614 | head = drm_head_create(b, connector_id, drm_device); |
| 5615 | if (!head) |
| 5616 | weston_log("DRM: failed to create head for hot-added connector %d.\n", |
| 5617 | connector_id); |
David Herrmann | 7551cff | 2011-12-08 17:05:43 +0100 | [diff] [blame] | 5618 | } |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5619 | } |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5620 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5621 | /* Remove connectors that have disappeared. */ |
Pekka Paalanen | a0a3746 | 2017-08-31 15:41:57 +0300 | [diff] [blame] | 5622 | wl_list_for_each_safe(base, next, |
| 5623 | &b->compositor->head_list, compositor_link) { |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5624 | bool removed = true; |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 5625 | |
Pekka Paalanen | a0a3746 | 2017-08-31 15:41:57 +0300 | [diff] [blame] | 5626 | head = to_drm_head(base); |
| 5627 | |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 5628 | for (i = 0; i < resources->count_connectors; i++) { |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5629 | if (resources->connectors[i] == head->connector_id) { |
| 5630 | removed = false; |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 5631 | break; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5632 | } |
| 5633 | } |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5634 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5635 | if (!removed) |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 5636 | continue; |
| 5637 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5638 | weston_log("DRM: head '%s' (connector %d) disappeared.\n", |
| 5639 | head->base.name, head->connector_id); |
| 5640 | drm_head_destroy(head); |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 5641 | } |
| 5642 | |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 5643 | drm_backend_update_unused_outputs(b, resources); |
| 5644 | |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 5645 | drmModeFreeResources(resources); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5646 | } |
| 5647 | |
| 5648 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5649 | udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5650 | { |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 5651 | const char *sysnum; |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 5652 | const char *val; |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 5653 | |
| 5654 | sysnum = udev_device_get_sysnum(device); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5655 | if (!sysnum || atoi(sysnum) != b->drm.id) |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 5656 | return 0; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 5657 | |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 5658 | val = udev_device_get_property_value(device, "HOTPLUG"); |
| 5659 | if (!val) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5660 | return 0; |
| 5661 | |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 5662 | return strcmp(val, "1") == 0; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5663 | } |
| 5664 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 5665 | static int |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5666 | udev_drm_event(int fd, uint32_t mask, void *data) |
| 5667 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5668 | struct drm_backend *b = data; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5669 | struct udev_device *event; |
| 5670 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5671 | event = udev_monitor_receive_device(b->udev_monitor); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 5672 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5673 | if (udev_event_is_hotplug(b, event)) |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5674 | drm_backend_update_heads(b, event); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5675 | |
| 5676 | udev_device_unref(event); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 5677 | |
| 5678 | return 1; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5679 | } |
| 5680 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 5681 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 5682 | drm_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 5683 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 5684 | struct drm_backend *b = to_drm_backend(ec); |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 5685 | struct weston_head *base, *next; |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 5686 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5687 | udev_input_destroy(&b->input); |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 5688 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5689 | wl_event_source_remove(b->udev_drm_source); |
| 5690 | wl_event_source_remove(b->drm_source); |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 5691 | |
Daniel Stone | b57c6a0 | 2017-10-05 16:27:21 +0100 | [diff] [blame] | 5692 | b->shutting_down = true; |
| 5693 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5694 | destroy_sprites(b); |
Kristian Høgsberg | 3d64a3e | 2013-05-10 12:36:04 -0400 | [diff] [blame] | 5695 | |
Ander Conselvan de Oliveira | 6b16214 | 2013-10-25 16:26:32 +0300 | [diff] [blame] | 5696 | weston_compositor_shutdown(ec); |
| 5697 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 5698 | wl_list_for_each_safe(base, next, &ec->head_list, compositor_link) |
| 5699 | drm_head_destroy(to_drm_head(base)); |
| 5700 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5701 | if (b->gbm) |
| 5702 | gbm_device_destroy(b->gbm); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 5703 | |
Pekka Paalanen | 5b0aa55 | 2017-12-07 16:06:05 +0200 | [diff] [blame] | 5704 | udev_monitor_unref(b->udev_monitor); |
Pekka Paalanen | 2a0c6c3 | 2017-09-13 16:48:01 +0300 | [diff] [blame] | 5705 | udev_unref(b->udev); |
| 5706 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5707 | weston_launcher_destroy(ec->launcher); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 5708 | |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 5709 | wl_array_release(&b->unused_crtcs); |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 5710 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5711 | close(b->drm.fd); |
Pekka Paalanen | 9bf4f37 | 2017-12-07 16:05:29 +0200 | [diff] [blame] | 5712 | free(b->drm.filename); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5713 | free(b); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 5714 | } |
| 5715 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 5716 | static void |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 5717 | session_notify(struct wl_listener *listener, void *data) |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 5718 | { |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 5719 | struct weston_compositor *compositor = data; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 5720 | struct drm_backend *b = to_drm_backend(compositor); |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 5721 | struct drm_plane *plane; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 5722 | struct drm_output *output; |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 5723 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5724 | if (compositor->session_active) { |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 5725 | weston_log("activating session\n"); |
Daniel Stone | f33e104 | 2016-11-05 08:10:13 +0000 | [diff] [blame] | 5726 | weston_compositor_wake(compositor); |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 5727 | weston_compositor_damage_all(compositor); |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 5728 | b->state_invalid = true; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5729 | udev_input_enable(&b->input); |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 5730 | } else { |
| 5731 | weston_log("deactivating session\n"); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5732 | udev_input_disable(&b->input); |
Kristian Høgsberg | 4014a6b | 2012-04-10 00:08:45 -0400 | [diff] [blame] | 5733 | |
Philipp Brüschweiler | 57edf7f | 2013-03-29 13:01:56 +0100 | [diff] [blame] | 5734 | weston_compositor_offscreen(compositor); |
Kristian Høgsberg | d8e181b | 2011-05-06 15:38:28 -0400 | [diff] [blame] | 5735 | |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 5736 | /* If we have a repaint scheduled (either from a |
| 5737 | * pending pageflip or the idle handler), make sure we |
| 5738 | * 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] | 5739 | * vt switched away. The OFFSCREEN state will prevent |
Abdur Rehman | 4dca0e1 | 2017-01-01 19:46:35 +0500 | [diff] [blame] | 5740 | * further attempts at repainting. When we switch |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 5741 | * back, we schedule a repaint, which will process |
| 5742 | * pending frame callbacks. */ |
| 5743 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5744 | wl_list_for_each(output, &compositor->output_list, base.link) { |
Daniel Stone | 09a97e2 | 2017-03-01 11:34:06 +0000 | [diff] [blame] | 5745 | output->base.repaint_needed = false; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 5746 | if (output->cursor_plane) |
| 5747 | drmModeSetCursor(b->drm.fd, output->crtc_id, |
| 5748 | 0, 0, 0); |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 5749 | } |
| 5750 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5751 | output = container_of(compositor->output_list.next, |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 5752 | struct drm_output, base.link); |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 5753 | |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 5754 | wl_list_for_each(plane, &b->plane_list, link) { |
| 5755 | if (plane->type != WDRM_PLANE_TYPE_OVERLAY) |
| 5756 | continue; |
| 5757 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5758 | drmModeSetPlane(b->drm.fd, |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 5759 | plane->plane_id, |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 5760 | output->crtc_id, 0, 0, |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 5761 | 0, 0, 0, 0, 0, 0, 0, 0); |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 5762 | } |
| 5763 | } |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 5764 | } |
| 5765 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 5766 | /** |
| 5767 | * Determines whether or not a device is capable of modesetting. If successful, |
| 5768 | * sets b->drm.fd and b->drm.filename to the opened device. |
| 5769 | */ |
| 5770 | static bool |
| 5771 | drm_device_is_kms(struct drm_backend *b, struct udev_device *device) |
| 5772 | { |
| 5773 | const char *filename = udev_device_get_devnode(device); |
| 5774 | const char *sysnum = udev_device_get_sysnum(device); |
| 5775 | drmModeRes *res; |
| 5776 | int id, fd; |
| 5777 | |
| 5778 | if (!filename) |
| 5779 | return false; |
| 5780 | |
| 5781 | fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR); |
| 5782 | if (fd < 0) |
| 5783 | return false; |
| 5784 | |
| 5785 | res = drmModeGetResources(fd); |
| 5786 | if (!res) |
| 5787 | goto out_fd; |
| 5788 | |
| 5789 | if (res->count_crtcs <= 0 || res->count_connectors <= 0 || |
| 5790 | res->count_encoders <= 0) |
| 5791 | goto out_res; |
| 5792 | |
| 5793 | if (sysnum) |
| 5794 | id = atoi(sysnum); |
| 5795 | if (!sysnum || id < 0) { |
| 5796 | weston_log("couldn't get sysnum for device %s\n", filename); |
| 5797 | goto out_res; |
| 5798 | } |
| 5799 | |
| 5800 | /* We can be called successfully on multiple devices; if we have, |
| 5801 | * clean up old entries. */ |
| 5802 | if (b->drm.fd >= 0) |
| 5803 | weston_launcher_close(b->compositor->launcher, b->drm.fd); |
| 5804 | free(b->drm.filename); |
| 5805 | |
| 5806 | b->drm.fd = fd; |
| 5807 | b->drm.id = id; |
| 5808 | b->drm.filename = strdup(filename); |
| 5809 | |
Sergi Granell | ceb5981 | 2017-03-28 12:44:04 +0200 | [diff] [blame] | 5810 | drmModeFreeResources(res); |
| 5811 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 5812 | return true; |
| 5813 | |
| 5814 | out_res: |
| 5815 | drmModeFreeResources(res); |
| 5816 | out_fd: |
| 5817 | weston_launcher_close(b->compositor->launcher, fd); |
| 5818 | return false; |
| 5819 | } |
| 5820 | |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5821 | /* |
| 5822 | * Find primary GPU |
| 5823 | * Some systems may have multiple DRM devices attached to a single seat. This |
| 5824 | * function loops over all devices and tries to find a PCI device with the |
| 5825 | * boot_vga sysfs attribute set to 1. |
| 5826 | * 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] | 5827 | * Devices are also vetted to make sure they are are capable of modesetting, |
| 5828 | * rather than pure render nodes (GPU with no display), or pure |
| 5829 | * memory-allocation devices (VGEM). |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5830 | */ |
| 5831 | static struct udev_device* |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5832 | find_primary_gpu(struct drm_backend *b, const char *seat) |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5833 | { |
| 5834 | struct udev_enumerate *e; |
| 5835 | struct udev_list_entry *entry; |
| 5836 | const char *path, *device_seat, *id; |
| 5837 | struct udev_device *device, *drm_device, *pci; |
| 5838 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5839 | e = udev_enumerate_new(b->udev); |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5840 | udev_enumerate_add_match_subsystem(e, "drm"); |
| 5841 | udev_enumerate_add_match_sysname(e, "card[0-9]*"); |
| 5842 | |
| 5843 | udev_enumerate_scan_devices(e); |
| 5844 | drm_device = NULL; |
| 5845 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 5846 | bool is_boot_vga = false; |
| 5847 | |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5848 | path = udev_list_entry_get_name(entry); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5849 | device = udev_device_new_from_syspath(b->udev, path); |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5850 | if (!device) |
| 5851 | continue; |
| 5852 | device_seat = udev_device_get_property_value(device, "ID_SEAT"); |
| 5853 | if (!device_seat) |
| 5854 | device_seat = default_seat; |
| 5855 | if (strcmp(device_seat, seat)) { |
| 5856 | udev_device_unref(device); |
| 5857 | continue; |
| 5858 | } |
| 5859 | |
| 5860 | pci = udev_device_get_parent_with_subsystem_devtype(device, |
| 5861 | "pci", NULL); |
| 5862 | if (pci) { |
| 5863 | id = udev_device_get_sysattr_value(pci, "boot_vga"); |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 5864 | if (id && !strcmp(id, "1")) |
| 5865 | is_boot_vga = true; |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5866 | } |
| 5867 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 5868 | /* If we already have a modesetting-capable device, and this |
| 5869 | * device isn't our boot-VGA device, we aren't going to use |
| 5870 | * it. */ |
| 5871 | if (!is_boot_vga && drm_device) { |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5872 | udev_device_unref(device); |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 5873 | continue; |
| 5874 | } |
| 5875 | |
| 5876 | /* Make sure this device is actually capable of modesetting; |
| 5877 | * if this call succeeds, b->drm.{fd,filename} will be set, |
| 5878 | * and any old values freed. */ |
| 5879 | if (!drm_device_is_kms(b, device)) { |
| 5880 | udev_device_unref(device); |
| 5881 | continue; |
| 5882 | } |
| 5883 | |
| 5884 | /* There can only be one boot_vga device, and we try to use it |
| 5885 | * at all costs. */ |
| 5886 | if (is_boot_vga) { |
| 5887 | if (drm_device) |
| 5888 | udev_device_unref(drm_device); |
| 5889 | drm_device = device; |
| 5890 | break; |
| 5891 | } |
| 5892 | |
| 5893 | /* Per the (!is_boot_vga && drm_device) test above, we only |
| 5894 | * trump existing saved devices with boot-VGA devices, so if |
| 5895 | * we end up here, this must be the first device we've seen. */ |
| 5896 | assert(!drm_device); |
| 5897 | drm_device = device; |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5898 | } |
| 5899 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 5900 | /* If we're returning a device to use, we must have an open FD for |
| 5901 | * it. */ |
| 5902 | assert(!!drm_device == (b->drm.fd >= 0)); |
| 5903 | |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5904 | udev_enumerate_unref(e); |
| 5905 | return drm_device; |
| 5906 | } |
| 5907 | |
Pekka Paalanen | b45ed8b | 2017-03-28 18:04:27 +0300 | [diff] [blame] | 5908 | static struct udev_device * |
| 5909 | open_specific_drm_device(struct drm_backend *b, const char *name) |
| 5910 | { |
| 5911 | struct udev_device *device; |
| 5912 | |
| 5913 | device = udev_device_new_from_subsystem_sysname(b->udev, "drm", name); |
| 5914 | if (!device) { |
| 5915 | weston_log("ERROR: could not open DRM device '%s'\n", name); |
| 5916 | return NULL; |
| 5917 | } |
| 5918 | |
| 5919 | if (!drm_device_is_kms(b, device)) { |
| 5920 | udev_device_unref(device); |
| 5921 | weston_log("ERROR: DRM device '%s' is not a KMS device.\n", name); |
| 5922 | return NULL; |
| 5923 | } |
| 5924 | |
| 5925 | /* If we're returning a device to use, we must have an open FD for |
| 5926 | * it. */ |
| 5927 | assert(b->drm.fd >= 0); |
| 5928 | |
| 5929 | return device; |
| 5930 | } |
| 5931 | |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 5932 | static void |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 5933 | planes_binding(struct weston_keyboard *keyboard, const struct timespec *time, |
| 5934 | uint32_t key, void *data) |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 5935 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5936 | struct drm_backend *b = data; |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 5937 | |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 5938 | switch (key) { |
| 5939 | case KEY_C: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5940 | b->cursors_are_broken ^= 1; |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 5941 | break; |
| 5942 | case KEY_V: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5943 | b->sprites_are_broken ^= 1; |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 5944 | break; |
| 5945 | case KEY_O: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5946 | b->sprites_hidden ^= 1; |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 5947 | break; |
| 5948 | default: |
| 5949 | break; |
| 5950 | } |
Ander Conselvan de Oliveira | 180f42a | 2012-11-21 15:11:37 +0200 | [diff] [blame] | 5951 | } |
| 5952 | |
Kristian Høgsberg | 0eac34a | 2013-08-30 14:28:22 -0700 | [diff] [blame] | 5953 | #ifdef BUILD_VAAPI_RECORDER |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5954 | static void |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 5955 | recorder_destroy(struct drm_output *output) |
| 5956 | { |
| 5957 | vaapi_recorder_destroy(output->recorder); |
| 5958 | output->recorder = NULL; |
| 5959 | |
| 5960 | output->base.disable_planes--; |
| 5961 | |
| 5962 | wl_list_remove(&output->recorder_frame_listener.link); |
| 5963 | weston_log("[libva recorder] done\n"); |
| 5964 | } |
| 5965 | |
| 5966 | static void |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5967 | recorder_frame_notify(struct wl_listener *listener, void *data) |
| 5968 | { |
| 5969 | struct drm_output *output; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5970 | struct drm_backend *b; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5971 | int fd, ret; |
| 5972 | |
| 5973 | output = container_of(listener, struct drm_output, |
| 5974 | recorder_frame_listener); |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 5975 | b = to_drm_backend(output->base.compositor); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5976 | |
| 5977 | if (!output->recorder) |
| 5978 | return; |
| 5979 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 5980 | ret = drmPrimeHandleToFD(b->drm.fd, |
| 5981 | output->scanout_plane->state_cur->fb->handle, |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5982 | DRM_CLOEXEC, &fd); |
| 5983 | if (ret) { |
| 5984 | weston_log("[libva recorder] " |
| 5985 | "failed to create prime fd for front buffer\n"); |
| 5986 | return; |
| 5987 | } |
| 5988 | |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 5989 | ret = vaapi_recorder_frame(output->recorder, fd, |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 5990 | output->scanout_plane->state_cur->fb->stride); |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 5991 | if (ret < 0) { |
| 5992 | weston_log("[libva recorder] aborted: %m\n"); |
| 5993 | recorder_destroy(output); |
| 5994 | } |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5995 | } |
| 5996 | |
| 5997 | static void * |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5998 | create_recorder(struct drm_backend *b, int width, int height, |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5999 | const char *filename) |
| 6000 | { |
| 6001 | int fd; |
| 6002 | drm_magic_t magic; |
| 6003 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6004 | fd = open(b->drm.filename, O_RDWR | O_CLOEXEC); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 6005 | if (fd < 0) |
| 6006 | return NULL; |
| 6007 | |
| 6008 | drmGetMagic(fd, &magic); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6009 | drmAuthMagic(b->drm.fd, magic); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 6010 | |
| 6011 | return vaapi_recorder_create(fd, width, height, filename); |
| 6012 | } |
| 6013 | |
| 6014 | static void |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 6015 | recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time, |
| 6016 | uint32_t key, void *data) |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 6017 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6018 | struct drm_backend *b = data; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 6019 | struct drm_output *output; |
| 6020 | int width, height; |
| 6021 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6022 | output = container_of(b->compositor->output_list.next, |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 6023 | struct drm_output, base.link); |
| 6024 | |
| 6025 | if (!output->recorder) { |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 6026 | if (output->gbm_format != GBM_FORMAT_XRGB8888) { |
Ander Conselvan de Oliveira | 2ef1cd1 | 2014-05-06 16:49:06 +0300 | [diff] [blame] | 6027 | weston_log("failed to start vaapi recorder: " |
| 6028 | "output format not supported\n"); |
| 6029 | return; |
| 6030 | } |
| 6031 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 6032 | width = output->base.current_mode->width; |
| 6033 | height = output->base.current_mode->height; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 6034 | |
| 6035 | output->recorder = |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6036 | create_recorder(b, width, height, "capture.h264"); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 6037 | if (!output->recorder) { |
| 6038 | weston_log("failed to create vaapi recorder\n"); |
| 6039 | return; |
| 6040 | } |
| 6041 | |
| 6042 | output->base.disable_planes++; |
| 6043 | |
| 6044 | output->recorder_frame_listener.notify = recorder_frame_notify; |
| 6045 | wl_signal_add(&output->base.frame_signal, |
| 6046 | &output->recorder_frame_listener); |
| 6047 | |
| 6048 | weston_output_schedule_repaint(&output->base); |
| 6049 | |
| 6050 | weston_log("[libva recorder] initialized\n"); |
| 6051 | } else { |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 6052 | recorder_destroy(output); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 6053 | } |
| 6054 | } |
| 6055 | #else |
| 6056 | static void |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 6057 | recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time, |
| 6058 | uint32_t key, void *data) |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 6059 | { |
| 6060 | weston_log("Compiled without libva support\n"); |
| 6061 | } |
| 6062 | #endif |
| 6063 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 6064 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6065 | switch_to_gl_renderer(struct drm_backend *b) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 6066 | { |
| 6067 | struct drm_output *output; |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 6068 | bool dmabuf_support_inited; |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 6069 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6070 | if (!b->use_pixman) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 6071 | return; |
| 6072 | |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 6073 | dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf; |
| 6074 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 6075 | weston_log("Switching to GL renderer\n"); |
| 6076 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6077 | b->gbm = create_gbm_device(b->drm.fd); |
| 6078 | if (!b->gbm) { |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 6079 | weston_log("Failed to create gbm device. " |
| 6080 | "Aborting renderer switch\n"); |
| 6081 | return; |
| 6082 | } |
| 6083 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6084 | 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] | 6085 | pixman_renderer_output_destroy(&output->base); |
| 6086 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6087 | b->compositor->renderer->destroy(b->compositor); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 6088 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6089 | if (drm_backend_create_gl_renderer(b) < 0) { |
| 6090 | gbm_device_destroy(b->gbm); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 6091 | weston_log("Failed to create GL renderer. Quitting.\n"); |
| 6092 | /* FIXME: we need a function to shutdown cleanly */ |
| 6093 | assert(0); |
| 6094 | } |
| 6095 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6096 | wl_list_for_each(output, &b->compositor->output_list, base.link) |
| 6097 | drm_output_init_egl(output, b); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 6098 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6099 | b->use_pixman = 0; |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 6100 | |
| 6101 | if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) { |
| 6102 | if (linux_dmabuf_setup(b->compositor) < 0) |
| 6103 | weston_log("Error: initializing dmabuf " |
| 6104 | "support failed.\n"); |
| 6105 | } |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 6106 | } |
| 6107 | |
| 6108 | static void |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 6109 | renderer_switch_binding(struct weston_keyboard *keyboard, |
| 6110 | const struct timespec *time, uint32_t key, void *data) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 6111 | { |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 6112 | struct drm_backend *b = |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 6113 | to_drm_backend(keyboard->seat->compositor); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 6114 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6115 | switch_to_gl_renderer(b); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 6116 | } |
| 6117 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 6118 | static const struct weston_drm_output_api api = { |
| 6119 | drm_output_set_mode, |
| 6120 | drm_output_set_gbm_format, |
| 6121 | drm_output_set_seat, |
| 6122 | }; |
| 6123 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6124 | static struct drm_backend * |
| 6125 | drm_backend_create(struct weston_compositor *compositor, |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6126 | struct weston_drm_backend_config *config) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 6127 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6128 | struct drm_backend *b; |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 6129 | struct udev_device *drm_device; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 6130 | struct wl_event_loop *loop; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6131 | const char *seat_id = default_seat; |
nerdopolis | b16c4ac | 2018-06-29 08:17:46 -0400 | [diff] [blame] | 6132 | const char *session_seat; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 6133 | int ret; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 6134 | |
nerdopolis | b16c4ac | 2018-06-29 08:17:46 -0400 | [diff] [blame] | 6135 | session_seat = getenv("XDG_SEAT"); |
| 6136 | if (session_seat) |
| 6137 | seat_id = session_seat; |
| 6138 | |
| 6139 | if (config->seat_id) |
| 6140 | seat_id = config->seat_id; |
| 6141 | |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 6142 | weston_log("initializing drm backend\n"); |
| 6143 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6144 | b = zalloc(sizeof *b); |
| 6145 | if (b == NULL) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 6146 | return NULL; |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 6147 | |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 6148 | b->state_invalid = true; |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 6149 | b->drm.fd = -1; |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 6150 | wl_array_init(&b->unused_crtcs); |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 6151 | |
Pekka Paalanen | 6858383 | 2015-05-19 09:53:16 +0300 | [diff] [blame] | 6152 | /* |
| 6153 | * KMS support for hardware planes cannot properly synchronize |
| 6154 | * without nuclear page flip. Without nuclear/atomic, hw plane |
| 6155 | * and cursor plane updates would either tear or cause extra |
| 6156 | * waits for vblanks which means dropping the compositor framerate |
Pekka Paalanen | 3f32a13 | 2015-09-07 15:38:43 +0300 | [diff] [blame] | 6157 | * to a fraction. For cursors, it's not so bad, so they are |
| 6158 | * enabled. |
Pekka Paalanen | 6858383 | 2015-05-19 09:53:16 +0300 | [diff] [blame] | 6159 | * |
| 6160 | * These can be enabled again when nuclear/atomic support lands. |
| 6161 | */ |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6162 | b->sprites_are_broken = 1; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6163 | b->compositor = compositor; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6164 | b->use_pixman = config->use_pixman; |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 6165 | b->pageflip_timeout = config->pageflip_timeout; |
Pekka Paalanen | dee412d | 2018-04-23 11:44:58 +0200 | [diff] [blame] | 6166 | b->use_pixman_shadow = config->use_pixman_shadow; |
Kristian Høgsberg | 8e6f376 | 2013-10-16 16:31:42 -0700 | [diff] [blame] | 6167 | |
Pekka Paalanen | 7da9a38 | 2017-08-30 11:29:49 +0300 | [diff] [blame] | 6168 | compositor->backend = &b->base; |
| 6169 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6170 | if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0) |
| 6171 | goto err_compositor; |
Kristian Høgsberg | 8e6f376 | 2013-10-16 16:31:42 -0700 | [diff] [blame] | 6172 | |
Rafal Mielniczuk | 32069c0 | 2013-03-27 18:39:28 +0100 | [diff] [blame] | 6173 | /* Check if we run drm-backend using weston-launch */ |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6174 | compositor->launcher = weston_launcher_connect(compositor, config->tty, |
| 6175 | seat_id, true); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6176 | if (compositor->launcher == NULL) { |
Pekka Paalanen | a453f4d | 2017-10-31 10:19:48 +0200 | [diff] [blame] | 6177 | weston_log("fatal: drm backend should be run using " |
| 6178 | "weston-launch binary, or your system should " |
| 6179 | "provide the logind D-Bus API.\n"); |
Rafal Mielniczuk | 32069c0 | 2013-03-27 18:39:28 +0100 | [diff] [blame] | 6180 | goto err_compositor; |
| 6181 | } |
| 6182 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6183 | b->udev = udev_new(); |
| 6184 | if (b->udev == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 6185 | weston_log("failed to initialize udev context\n"); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 6186 | goto err_launcher; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 6187 | } |
| 6188 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6189 | b->session_listener.notify = session_notify; |
| 6190 | wl_signal_add(&compositor->session_signal, &b->session_listener); |
Kristian Høgsberg | c5b9ddb | 2012-01-15 14:29:09 -0500 | [diff] [blame] | 6191 | |
Pekka Paalanen | b45ed8b | 2017-03-28 18:04:27 +0300 | [diff] [blame] | 6192 | if (config->specific_device) |
| 6193 | drm_device = open_specific_drm_device(b, config->specific_device); |
| 6194 | else |
| 6195 | drm_device = find_primary_gpu(b, seat_id); |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 6196 | if (drm_device == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 6197 | weston_log("no drm device found\n"); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 6198 | goto err_udev; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 6199 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 6200 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 6201 | if (init_kms_caps(b) < 0) { |
Ander Conselvan de Oliveira | 2292917 | 2013-01-25 15:13:02 +0200 | [diff] [blame] | 6202 | weston_log("failed to initialize kms\n"); |
| 6203 | goto err_udev_dev; |
| 6204 | } |
| 6205 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6206 | if (b->use_pixman) { |
| 6207 | if (init_pixman(b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 6208 | weston_log("failed to initialize pixman renderer\n"); |
| 6209 | goto err_udev_dev; |
| 6210 | } |
| 6211 | } else { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6212 | if (init_egl(b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 6213 | weston_log("failed to initialize egl\n"); |
| 6214 | goto err_udev_dev; |
| 6215 | } |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 6216 | } |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 6217 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6218 | b->base.destroy = drm_destroy; |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 6219 | b->base.repaint_begin = drm_repaint_begin; |
| 6220 | b->base.repaint_flush = drm_repaint_flush; |
| 6221 | b->base.repaint_cancel = drm_repaint_cancel; |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 6222 | b->base.create_output = drm_output_create; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 6223 | |
Bob Ham | 91880f1 | 2016-01-12 10:21:47 +0000 | [diff] [blame] | 6224 | weston_setup_vt_switch_bindings(compositor); |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 6225 | |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 6226 | wl_list_init(&b->plane_list); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6227 | create_sprites(b); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 6228 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6229 | if (udev_input_init(&b->input, |
Giulio Camuffo | 8aedf7b | 2016-06-02 21:48:12 +0300 | [diff] [blame] | 6230 | compositor, b->udev, seat_id, |
| 6231 | config->configure_device) < 0) { |
Ander Conselvan de Oliveira | 4ade0e4 | 2014-04-17 13:08:45 +0300 | [diff] [blame] | 6232 | weston_log("failed to create input devices\n"); |
| 6233 | goto err_sprite; |
| 6234 | } |
| 6235 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 6236 | if (drm_backend_create_heads(b, drm_device) < 0) { |
| 6237 | weston_log("Failed to create heads for %s\n", b->drm.filename); |
Ander Conselvan de Oliveira | 4ade0e4 | 2014-04-17 13:08:45 +0300 | [diff] [blame] | 6238 | goto err_udev_input; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 6239 | } |
| 6240 | |
Jason Ekstrand | 9fc7151 | 2014-04-02 19:53:46 -0500 | [diff] [blame] | 6241 | /* A this point we have some idea of whether or not we have a working |
| 6242 | * cursor plane. */ |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6243 | if (!b->cursors_are_broken) |
| 6244 | compositor->capabilities |= WESTON_CAP_CURSOR_PLANE; |
Jason Ekstrand | 9fc7151 | 2014-04-02 19:53:46 -0500 | [diff] [blame] | 6245 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6246 | loop = wl_display_get_event_loop(compositor->wl_display); |
| 6247 | b->drm_source = |
| 6248 | wl_event_loop_add_fd(loop, b->drm.fd, |
| 6249 | WL_EVENT_READABLE, on_drm_input, b); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 6250 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6251 | b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev"); |
| 6252 | if (b->udev_monitor == NULL) { |
Abdur Rehman | 4dca0e1 | 2017-01-01 19:46:35 +0500 | [diff] [blame] | 6253 | weston_log("failed to initialize udev monitor\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6254 | goto err_drm_source; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 6255 | } |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6256 | udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor, |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 6257 | "drm", NULL); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6258 | b->udev_drm_source = |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 6259 | wl_event_loop_add_fd(loop, |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6260 | udev_monitor_get_fd(b->udev_monitor), |
| 6261 | WL_EVENT_READABLE, udev_drm_event, b); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 6262 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6263 | if (udev_monitor_enable_receiving(b->udev_monitor) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 6264 | weston_log("failed to enable udev-monitor receiving\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6265 | goto err_udev_monitor; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 6266 | } |
| 6267 | |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6268 | udev_device_unref(drm_device); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6269 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6270 | weston_compositor_add_debug_binding(compositor, KEY_O, |
| 6271 | planes_binding, b); |
| 6272 | weston_compositor_add_debug_binding(compositor, KEY_C, |
| 6273 | planes_binding, b); |
| 6274 | weston_compositor_add_debug_binding(compositor, KEY_V, |
| 6275 | planes_binding, b); |
| 6276 | weston_compositor_add_debug_binding(compositor, KEY_Q, |
| 6277 | recorder_binding, b); |
| 6278 | weston_compositor_add_debug_binding(compositor, KEY_W, |
| 6279 | renderer_switch_binding, b); |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 6280 | |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 6281 | if (compositor->renderer->import_dmabuf) { |
| 6282 | if (linux_dmabuf_setup(compositor) < 0) |
| 6283 | weston_log("Error: initializing dmabuf " |
| 6284 | "support failed.\n"); |
| 6285 | } |
| 6286 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 6287 | ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME, |
| 6288 | &api, sizeof(api)); |
| 6289 | |
| 6290 | if (ret < 0) { |
| 6291 | weston_log("Failed to register output API.\n"); |
| 6292 | goto err_udev_monitor; |
| 6293 | } |
| 6294 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6295 | return b; |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6296 | |
| 6297 | err_udev_monitor: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6298 | wl_event_source_remove(b->udev_drm_source); |
| 6299 | udev_monitor_unref(b->udev_monitor); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6300 | err_drm_source: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6301 | wl_event_source_remove(b->drm_source); |
Ander Conselvan de Oliveira | 4ade0e4 | 2014-04-17 13:08:45 +0300 | [diff] [blame] | 6302 | err_udev_input: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6303 | udev_input_destroy(&b->input); |
Kristian Høgsberg | 2bc5e8e | 2012-09-06 20:51:00 -0400 | [diff] [blame] | 6304 | err_sprite: |
Emmanuel Gil Peyrot | b8347e3 | 2016-05-02 22:40:13 +0100 | [diff] [blame] | 6305 | if (b->gbm) |
| 6306 | gbm_device_destroy(b->gbm); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6307 | destroy_sprites(b); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6308 | err_udev_dev: |
| 6309 | udev_device_unref(drm_device); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 6310 | err_launcher: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6311 | weston_launcher_destroy(compositor->launcher); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6312 | err_udev: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6313 | udev_unref(b->udev); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6314 | err_compositor: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6315 | weston_compositor_shutdown(compositor); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6316 | free(b); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6317 | return NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 6318 | } |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 6319 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6320 | static void |
| 6321 | config_init_to_defaults(struct weston_drm_backend_config *config) |
| 6322 | { |
Pekka Paalanen | dee412d | 2018-04-23 11:44:58 +0200 | [diff] [blame] | 6323 | config->use_pixman_shadow = true; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6324 | } |
| 6325 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6326 | WL_EXPORT int |
Quentin Glidic | 23e1d6f | 2016-12-02 14:08:44 +0100 | [diff] [blame] | 6327 | weston_backend_init(struct weston_compositor *compositor, |
| 6328 | struct weston_backend_config *config_base) |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 6329 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6330 | struct drm_backend *b; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6331 | struct weston_drm_backend_config config = {{ 0, }}; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 6332 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6333 | if (config_base == NULL || |
| 6334 | config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION || |
| 6335 | config_base->struct_size > sizeof(struct weston_drm_backend_config)) { |
| 6336 | weston_log("drm backend config structure is invalid\n"); |
| 6337 | return -1; |
| 6338 | } |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 6339 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6340 | config_init_to_defaults(&config); |
| 6341 | memcpy(&config, config_base, config_base->struct_size); |
Kristian Høgsberg | d8e9833 | 2013-10-16 16:15:11 -0700 | [diff] [blame] | 6342 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6343 | b = drm_backend_create(compositor, &config); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6344 | if (b == NULL) |
| 6345 | return -1; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6346 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6347 | return 0; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 6348 | } |