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, |
Sergi Granell | f445622 | 2017-01-12 17:17:32 +0000 | [diff] [blame] | 147 | WDRM_PLANE_IN_FORMATS, |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 148 | WDRM_PLANE__COUNT |
| 149 | }; |
| 150 | |
| 151 | /** |
| 152 | * Possible values for the WDRM_PLANE_TYPE property. |
| 153 | */ |
| 154 | enum wdrm_plane_type { |
| 155 | WDRM_PLANE_TYPE_PRIMARY = 0, |
| 156 | WDRM_PLANE_TYPE_CURSOR, |
| 157 | WDRM_PLANE_TYPE_OVERLAY, |
| 158 | WDRM_PLANE_TYPE__COUNT |
| 159 | }; |
| 160 | |
| 161 | static struct drm_property_enum_info plane_type_enums[] = { |
| 162 | [WDRM_PLANE_TYPE_PRIMARY] = { |
| 163 | .name = "Primary", |
| 164 | }, |
| 165 | [WDRM_PLANE_TYPE_OVERLAY] = { |
| 166 | .name = "Overlay", |
| 167 | }, |
| 168 | [WDRM_PLANE_TYPE_CURSOR] = { |
| 169 | .name = "Cursor", |
| 170 | }, |
| 171 | }; |
| 172 | |
| 173 | static const struct drm_property_info plane_props[] = { |
| 174 | [WDRM_PLANE_TYPE] = { |
| 175 | .name = "type", |
| 176 | .enum_values = plane_type_enums, |
| 177 | .num_enum_values = WDRM_PLANE_TYPE__COUNT, |
| 178 | }, |
| 179 | [WDRM_PLANE_SRC_X] = { .name = "SRC_X", }, |
| 180 | [WDRM_PLANE_SRC_Y] = { .name = "SRC_Y", }, |
| 181 | [WDRM_PLANE_SRC_W] = { .name = "SRC_W", }, |
| 182 | [WDRM_PLANE_SRC_H] = { .name = "SRC_H", }, |
| 183 | [WDRM_PLANE_CRTC_X] = { .name = "CRTC_X", }, |
| 184 | [WDRM_PLANE_CRTC_Y] = { .name = "CRTC_Y", }, |
| 185 | [WDRM_PLANE_CRTC_W] = { .name = "CRTC_W", }, |
| 186 | [WDRM_PLANE_CRTC_H] = { .name = "CRTC_H", }, |
| 187 | [WDRM_PLANE_FB_ID] = { .name = "FB_ID", }, |
| 188 | [WDRM_PLANE_CRTC_ID] = { .name = "CRTC_ID", }, |
Sergi Granell | f445622 | 2017-01-12 17:17:32 +0000 | [diff] [blame] | 189 | [WDRM_PLANE_IN_FORMATS] = { .name = "IN_FORMATS" }, |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | /** |
| 193 | * List of properties attached to a DRM connector |
| 194 | */ |
| 195 | enum wdrm_connector_property { |
| 196 | WDRM_CONNECTOR_EDID = 0, |
| 197 | WDRM_CONNECTOR_DPMS, |
| 198 | WDRM_CONNECTOR_CRTC_ID, |
| 199 | WDRM_CONNECTOR__COUNT |
| 200 | }; |
| 201 | |
Daniel Stone | 7625577 | 2018-07-06 11:36:49 +0100 | [diff] [blame] | 202 | enum wdrm_dpms_state { |
| 203 | WDRM_DPMS_STATE_OFF = 0, |
| 204 | WDRM_DPMS_STATE_ON, |
| 205 | WDRM_DPMS_STATE_STANDBY, /* unused */ |
| 206 | WDRM_DPMS_STATE_SUSPEND, /* unused */ |
| 207 | WDRM_DPMS_STATE__COUNT |
| 208 | }; |
| 209 | |
| 210 | static struct drm_property_enum_info dpms_state_enums[] = { |
| 211 | [WDRM_DPMS_STATE_OFF] = { |
| 212 | .name = "Off", |
| 213 | }, |
| 214 | [WDRM_DPMS_STATE_ON] = { |
| 215 | .name = "On", |
| 216 | }, |
| 217 | [WDRM_DPMS_STATE_STANDBY] = { |
| 218 | .name = "Standby", |
| 219 | }, |
| 220 | [WDRM_DPMS_STATE_SUSPEND] = { |
| 221 | .name = "Suspend", |
| 222 | }, |
| 223 | }; |
| 224 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 225 | static const struct drm_property_info connector_props[] = { |
| 226 | [WDRM_CONNECTOR_EDID] = { .name = "EDID" }, |
Daniel Stone | 7625577 | 2018-07-06 11:36:49 +0100 | [diff] [blame] | 227 | [WDRM_CONNECTOR_DPMS] = { |
| 228 | .name = "DPMS", |
| 229 | .enum_values = dpms_state_enums, |
| 230 | .num_enum_values = WDRM_DPMS_STATE__COUNT, |
| 231 | }, |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 232 | [WDRM_CONNECTOR_CRTC_ID] = { .name = "CRTC_ID", }, |
| 233 | }; |
| 234 | |
| 235 | /** |
Pekka Paalanen | cd011a6 | 2016-11-15 22:07:49 +0000 | [diff] [blame] | 236 | * List of properties attached to DRM CRTCs |
| 237 | */ |
| 238 | enum wdrm_crtc_property { |
| 239 | WDRM_CRTC_MODE_ID = 0, |
| 240 | WDRM_CRTC_ACTIVE, |
| 241 | WDRM_CRTC__COUNT |
| 242 | }; |
| 243 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 244 | static const struct drm_property_info crtc_props[] = { |
| 245 | [WDRM_CRTC_MODE_ID] = { .name = "MODE_ID", }, |
| 246 | [WDRM_CRTC_ACTIVE] = { .name = "ACTIVE", }, |
| 247 | }; |
| 248 | |
Pekka Paalanen | cd011a6 | 2016-11-15 22:07:49 +0000 | [diff] [blame] | 249 | /** |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 250 | * Mode for drm_output_state_duplicate. |
| 251 | */ |
| 252 | enum drm_output_state_duplicate_mode { |
| 253 | DRM_OUTPUT_STATE_CLEAR_PLANES, /**< reset all planes to off */ |
| 254 | DRM_OUTPUT_STATE_PRESERVE_PLANES, /**< preserve plane state */ |
| 255 | }; |
| 256 | |
| 257 | /** |
| 258 | * Mode for drm_pending_state_apply and co. |
| 259 | */ |
| 260 | enum drm_state_apply_mode { |
| 261 | DRM_STATE_APPLY_SYNC, /**< state fully processed */ |
| 262 | DRM_STATE_APPLY_ASYNC, /**< state pending event delivery */ |
Daniel Stone | bb6c19f | 2016-12-08 17:27:17 +0000 | [diff] [blame] | 263 | DRM_STATE_TEST_ONLY, /**< test if the state can be applied */ |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 264 | }; |
| 265 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 266 | struct drm_backend { |
| 267 | struct weston_backend base; |
| 268 | struct weston_compositor *compositor; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 269 | |
| 270 | struct udev *udev; |
| 271 | struct wl_event_source *drm_source; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 272 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 273 | struct udev_monitor *udev_monitor; |
| 274 | struct wl_event_source *udev_drm_source; |
| 275 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 276 | struct { |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 277 | int id; |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 278 | int fd; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 279 | char *filename; |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 280 | } drm; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 281 | struct gbm_device *gbm; |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 282 | struct wl_listener session_listener; |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 283 | uint32_t gbm_format; |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 284 | |
Rob Clark | 4339add | 2012-08-09 14:18:28 -0500 | [diff] [blame] | 285 | /* we need these parameters in order to not fail drmModeAddFB2() |
| 286 | * due to out of bounds dimensions, and then mistakenly set |
| 287 | * sprites_are_broken: |
| 288 | */ |
Daniel Stone | f214fdc | 2016-11-14 17:43:57 +0000 | [diff] [blame] | 289 | int min_width, max_width; |
| 290 | int min_height, max_height; |
Rob Clark | 4339add | 2012-08-09 14:18:28 -0500 | [diff] [blame] | 291 | |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 292 | struct wl_list plane_list; |
Kristian Høgsberg | 65bec24 | 2012-03-05 19:57:35 -0500 | [diff] [blame] | 293 | int sprites_are_broken; |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 294 | int sprites_hidden; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 295 | |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 296 | void *repaint_data; |
| 297 | |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 298 | bool state_invalid; |
| 299 | |
Pekka Paalanen | eacec81 | 2017-09-12 13:43:51 +0300 | [diff] [blame] | 300 | /* CRTC IDs not used by any enabled output. */ |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 301 | struct wl_array unused_crtcs; |
| 302 | |
Rob Clark | ab5b1e3 | 2012-08-09 13:24:45 -0500 | [diff] [blame] | 303 | int cursors_are_broken; |
| 304 | |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 305 | bool universal_planes; |
Pekka Paalanen | cd011a6 | 2016-11-15 22:07:49 +0000 | [diff] [blame] | 306 | bool atomic_modeset; |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 307 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 308 | int use_pixman; |
Pekka Paalanen | dee412d | 2018-04-23 11:44:58 +0200 | [diff] [blame] | 309 | bool use_pixman_shadow; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 310 | |
Rob Bradford | d355b80 | 2013-05-31 18:09:55 +0100 | [diff] [blame] | 311 | struct udev_input input; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 312 | |
Daniel Stone | 70d337d | 2015-06-16 18:42:23 +0100 | [diff] [blame] | 313 | int32_t cursor_width; |
| 314 | int32_t cursor_height; |
Ucan, Emre (ADITG/SW1) | 21e4944 | 2017-02-02 14:06:55 +0000 | [diff] [blame] | 315 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 316 | uint32_t pageflip_timeout; |
Daniel Stone | b57c6a0 | 2017-10-05 16:27:21 +0100 | [diff] [blame] | 317 | |
| 318 | bool shutting_down; |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 319 | |
| 320 | bool aspect_ratio_supported; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 321 | }; |
| 322 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 323 | struct drm_mode { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 324 | struct weston_mode base; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 325 | drmModeModeInfo mode_info; |
Daniel Stone | d5526cb | 2016-11-16 10:54:10 +0000 | [diff] [blame] | 326 | uint32_t blob_id; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 327 | }; |
| 328 | |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 329 | enum drm_fb_type { |
| 330 | BUFFER_INVALID = 0, /**< never used */ |
| 331 | BUFFER_CLIENT, /**< directly sourced from client */ |
Daniel Stone | f522e22 | 2016-11-18 12:31:26 +0000 | [diff] [blame] | 332 | BUFFER_DMABUF, /**< imported from linux_dmabuf client */ |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 333 | BUFFER_PIXMAN_DUMB, /**< internal Pixman rendering */ |
| 334 | BUFFER_GBM_SURFACE, /**< internal EGL rendering */ |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 335 | BUFFER_CURSOR, /**< internal cursor buffer */ |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 336 | }; |
| 337 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 338 | struct drm_fb { |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 339 | enum drm_fb_type type; |
| 340 | |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 341 | int refcnt; |
| 342 | |
Daniel Stone | 8eece0c | 2016-11-17 17:54:00 +0000 | [diff] [blame] | 343 | uint32_t fb_id, size; |
| 344 | uint32_t handles[4]; |
| 345 | uint32_t strides[4]; |
| 346 | uint32_t offsets[4]; |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 347 | const struct pixel_format_info *format; |
Daniel Stone | 65a4dbc | 2016-12-08 16:36:18 +0000 | [diff] [blame] | 348 | uint64_t modifier; |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 349 | int width, height; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 350 | int fd; |
Pekka Paalanen | de685b8 | 2012-12-04 15:58:12 +0200 | [diff] [blame] | 351 | struct weston_buffer_reference buffer_ref; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 352 | |
| 353 | /* Used by gbm fbs */ |
| 354 | struct gbm_bo *bo; |
Daniel Stone | 05a5ac2 | 2017-04-04 17:54:25 +0100 | [diff] [blame] | 355 | struct gbm_surface *gbm_surface; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 356 | |
| 357 | /* Used by dumb fbs */ |
| 358 | void *map; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 359 | }; |
| 360 | |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 361 | struct drm_edid { |
| 362 | char eisa_id[13]; |
| 363 | char monitor_name[13]; |
| 364 | char pnp_id[5]; |
| 365 | char serial_number[13]; |
| 366 | }; |
| 367 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 368 | /** |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 369 | * Pending state holds one or more drm_output_state structures, collected from |
| 370 | * performing repaint. This pending state is transient, and only lives between |
| 371 | * beginning a repaint group and flushing the results: after flush, each |
| 372 | * output state will complete and be retired separately. |
| 373 | */ |
| 374 | struct drm_pending_state { |
| 375 | struct drm_backend *backend; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 376 | struct wl_list output_list; |
| 377 | }; |
| 378 | |
| 379 | /* |
| 380 | * Output state holds the dynamic state for one Weston output, i.e. a KMS CRTC, |
| 381 | * plus >= 1 each of encoder/connector/plane. Since everything but the planes |
| 382 | * is currently statically assigned per-output, we mainly use this to track |
| 383 | * plane state. |
| 384 | * |
| 385 | * pending_state is set when the output state is owned by a pending_state, |
| 386 | * i.e. when it is being constructed and has not yet been applied. When the |
| 387 | * output state has been applied, the owning pending_state is freed. |
| 388 | */ |
| 389 | struct drm_output_state { |
| 390 | struct drm_pending_state *pending_state; |
| 391 | struct drm_output *output; |
| 392 | struct wl_list link; |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 393 | enum dpms_enum dpms; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 394 | struct wl_list plane_list; |
| 395 | }; |
| 396 | |
| 397 | /** |
| 398 | * Plane state holds the dynamic state for a plane: where it is positioned, |
| 399 | * and which buffer it is currently displaying. |
| 400 | * |
| 401 | * The plane state is owned by an output state, except when setting an initial |
| 402 | * state. See drm_output_state for notes on state object lifetime. |
| 403 | */ |
| 404 | struct drm_plane_state { |
| 405 | struct drm_plane *plane; |
| 406 | struct drm_output *output; |
| 407 | struct drm_output_state *output_state; |
| 408 | |
| 409 | struct drm_fb *fb; |
| 410 | |
Daniel Stone | ee1aea7 | 2017-12-18 13:41:09 +0000 | [diff] [blame] | 411 | struct weston_view *ev; /**< maintained for drm_assign_planes only */ |
| 412 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 413 | int32_t src_x, src_y; |
| 414 | uint32_t src_w, src_h; |
| 415 | int32_t dest_x, dest_y; |
| 416 | uint32_t dest_w, dest_h; |
| 417 | |
| 418 | bool complete; |
| 419 | |
| 420 | struct wl_list link; /* drm_output_state::plane_list */ |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 421 | }; |
| 422 | |
| 423 | /** |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 424 | * A plane represents one buffer, positioned within a CRTC, and stacked |
| 425 | * relative to other planes on the same CRTC. |
| 426 | * |
| 427 | * Each CRTC has a 'primary plane', which use used to display the classic |
| 428 | * framebuffer contents, as accessed through the legacy drmModeSetCrtc |
| 429 | * call (which combines setting the CRTC's actual physical mode, and the |
| 430 | * properties of the primary plane). |
| 431 | * |
| 432 | * The cursor plane also has its own alternate legacy API. |
| 433 | * |
| 434 | * Other planes are used opportunistically to display content we do not |
| 435 | * wish to blit into the primary plane. These non-primary/cursor planes |
| 436 | * are referred to as 'sprites'. |
| 437 | */ |
| 438 | struct drm_plane { |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 439 | struct weston_plane base; |
| 440 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 441 | struct drm_backend *backend; |
| 442 | |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 443 | enum wdrm_plane_type type; |
| 444 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 445 | uint32_t possible_crtcs; |
| 446 | uint32_t plane_id; |
| 447 | uint32_t count_formats; |
| 448 | |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 449 | struct drm_property_info props[WDRM_PLANE__COUNT]; |
| 450 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 451 | /* The last state submitted to the kernel for this plane. */ |
| 452 | struct drm_plane_state *state_cur; |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 453 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 454 | struct wl_list link; |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 455 | |
Sergi Granell | f445622 | 2017-01-12 17:17:32 +0000 | [diff] [blame] | 456 | struct { |
| 457 | uint32_t format; |
| 458 | uint32_t count_modifiers; |
| 459 | uint64_t *modifiers; |
| 460 | } formats[]; |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 461 | }; |
| 462 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 463 | struct drm_head { |
| 464 | struct weston_head base; |
| 465 | struct drm_backend *backend; |
| 466 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 467 | drmModeConnector *connector; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 468 | uint32_t connector_id; |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 469 | struct drm_edid edid; |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 470 | |
| 471 | /* Holds the properties for the connector */ |
| 472 | struct drm_property_info props_conn[WDRM_CONNECTOR__COUNT]; |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 473 | |
| 474 | struct backlight *backlight; |
Pekka Paalanen | 13d233e | 2017-09-11 14:06:11 +0300 | [diff] [blame] | 475 | |
| 476 | drmModeModeInfo inherited_mode; /**< Original mode on the connector */ |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 477 | uint32_t inherited_crtc_id; /**< Original CRTC assignment */ |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 478 | }; |
| 479 | |
| 480 | struct drm_output { |
| 481 | struct weston_output base; |
| 482 | |
| 483 | uint32_t crtc_id; /* object ID to pass to DRM functions */ |
| 484 | int pipe; /* index of CRTC in resource array / bitmasks */ |
| 485 | |
Pekka Paalanen | cd011a6 | 2016-11-15 22:07:49 +0000 | [diff] [blame] | 486 | /* Holds the properties for the CRTC */ |
| 487 | struct drm_property_info props_crtc[WDRM_CRTC__COUNT]; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 488 | |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 489 | int vblank_pending; |
| 490 | int page_flip_pending; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 491 | int atomic_complete_pending; |
Xiong Zhang | abd5d47 | 2013-10-11 14:43:07 +0800 | [diff] [blame] | 492 | int destroy_pending; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 493 | int disable_pending; |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 494 | int dpms_off_pending; |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 495 | |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 496 | struct drm_fb *gbm_cursor_fb[2]; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 497 | struct drm_plane *cursor_plane; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 498 | struct weston_view *cursor_view; |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 499 | int current_cursor; |
Daniel Stone | 5bb8f58 | 2017-04-04 17:54:28 +0100 | [diff] [blame] | 500 | |
| 501 | struct gbm_surface *gbm_surface; |
| 502 | uint32_t gbm_format; |
| 503 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 504 | /* Plane being displayed directly on the CRTC */ |
| 505 | struct drm_plane *scanout_plane; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 506 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 507 | /* The last state submitted to the kernel for this CRTC. */ |
| 508 | struct drm_output_state *state_cur; |
| 509 | /* The previously-submitted state, where the hardware has not |
| 510 | * yet acknowledged completion of state_cur. */ |
| 511 | struct drm_output_state *state_last; |
| 512 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 513 | struct drm_fb *dumb[2]; |
| 514 | pixman_image_t *image[2]; |
| 515 | int current_image; |
| 516 | pixman_region32_t previous_damage; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 517 | |
| 518 | struct vaapi_recorder *recorder; |
| 519 | struct wl_listener recorder_frame_listener; |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 520 | |
| 521 | struct wl_event_source *pageflip_timer; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 522 | }; |
| 523 | |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 524 | static const char *const aspect_ratio_as_string[] = { |
| 525 | [WESTON_MODE_PIC_AR_NONE] = "", |
| 526 | [WESTON_MODE_PIC_AR_4_3] = " 4:3", |
| 527 | [WESTON_MODE_PIC_AR_16_9] = " 16:9", |
| 528 | [WESTON_MODE_PIC_AR_64_27] = " 64:27", |
| 529 | [WESTON_MODE_PIC_AR_256_135] = " 256:135", |
| 530 | }; |
| 531 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 532 | static struct gl_renderer_interface *gl_renderer; |
| 533 | |
Kristian Høgsberg | 98cfea6 | 2013-02-18 16:15:53 -0500 | [diff] [blame] | 534 | static const char default_seat[] = "seat0"; |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 535 | |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 536 | static void |
| 537 | wl_array_remove_uint32(struct wl_array *array, uint32_t elm) |
| 538 | { |
| 539 | uint32_t *pos, *end; |
| 540 | |
| 541 | end = (uint32_t *) ((char *) array->data + array->size); |
| 542 | |
| 543 | wl_array_for_each(pos, array) { |
| 544 | if (*pos != elm) |
| 545 | continue; |
| 546 | |
| 547 | array->size -= sizeof(*pos); |
| 548 | if (pos + 1 == end) |
| 549 | break; |
| 550 | |
| 551 | memmove(pos, pos + 1, (char *) end - (char *) (pos + 1)); |
| 552 | break; |
| 553 | } |
| 554 | } |
| 555 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 556 | static inline struct drm_head * |
| 557 | to_drm_head(struct weston_head *base) |
| 558 | { |
| 559 | return container_of(base, struct drm_head, base); |
| 560 | } |
| 561 | |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 562 | static inline struct drm_output * |
| 563 | to_drm_output(struct weston_output *base) |
| 564 | { |
| 565 | return container_of(base, struct drm_output, base); |
| 566 | } |
| 567 | |
| 568 | static inline struct drm_backend * |
| 569 | to_drm_backend(struct weston_compositor *base) |
| 570 | { |
| 571 | return container_of(base->backend, struct drm_backend, base); |
| 572 | } |
| 573 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 574 | static int |
| 575 | pageflip_timeout(void *data) { |
| 576 | /* |
| 577 | * Our timer just went off, that means we're not receiving drm |
| 578 | * page flip events anymore for that output. Let's gracefully exit |
| 579 | * weston with a return value so devs can debug what's going on. |
| 580 | */ |
| 581 | struct drm_output *output = data; |
| 582 | struct weston_compositor *compositor = output->base.compositor; |
| 583 | |
| 584 | weston_log("Pageflip timeout reached on output %s, your " |
| 585 | "driver is probably buggy! Exiting.\n", |
| 586 | output->base.name); |
| 587 | weston_compositor_exit_with_code(compositor, EXIT_FAILURE); |
| 588 | |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | /* Creates the pageflip timer. Note that it isn't armed by default */ |
| 593 | static int |
| 594 | drm_output_pageflip_timer_create(struct drm_output *output) |
| 595 | { |
| 596 | struct wl_event_loop *loop = NULL; |
| 597 | struct weston_compositor *ec = output->base.compositor; |
| 598 | |
| 599 | loop = wl_display_get_event_loop(ec->wl_display); |
| 600 | assert(loop); |
| 601 | output->pageflip_timer = wl_event_loop_add_timer(loop, |
| 602 | pageflip_timeout, |
| 603 | output); |
| 604 | |
| 605 | if (output->pageflip_timer == NULL) { |
| 606 | weston_log("creating drm pageflip timer failed: %m\n"); |
| 607 | return -1; |
| 608 | } |
| 609 | |
| 610 | return 0; |
| 611 | } |
| 612 | |
Daniel Stone | cb04cc4 | 2016-11-16 11:51:27 +0000 | [diff] [blame] | 613 | static inline struct drm_mode * |
| 614 | to_drm_mode(struct weston_mode *base) |
| 615 | { |
| 616 | return container_of(base, struct drm_mode, base); |
| 617 | } |
| 618 | |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 619 | /** |
| 620 | * Get the current value of a KMS property |
| 621 | * |
| 622 | * Given a drmModeObjectGetProperties return, as well as the drm_property_info |
| 623 | * for the target property, return the current value of that property, |
| 624 | * with an optional default. If the property is a KMS enum type, the return |
| 625 | * value will be translated into the appropriate internal enum. |
| 626 | * |
| 627 | * If the property is not present, the default value will be returned. |
| 628 | * |
| 629 | * @param info Internal structure for property to look up |
| 630 | * @param props Raw KMS properties for the target object |
| 631 | * @param def Value to return if property is not found |
| 632 | */ |
| 633 | static uint64_t |
| 634 | drm_property_get_value(struct drm_property_info *info, |
Daniel Stone | 85eebdf | 2018-07-05 17:55:43 +0100 | [diff] [blame] | 635 | const drmModeObjectProperties *props, |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 636 | uint64_t def) |
| 637 | { |
| 638 | unsigned int i; |
| 639 | |
| 640 | if (info->prop_id == 0) |
| 641 | return def; |
| 642 | |
| 643 | for (i = 0; i < props->count_props; i++) { |
| 644 | unsigned int j; |
| 645 | |
| 646 | if (props->props[i] != info->prop_id) |
| 647 | continue; |
| 648 | |
| 649 | /* Simple (non-enum) types can return the value directly */ |
| 650 | if (info->num_enum_values == 0) |
| 651 | return props->prop_values[i]; |
| 652 | |
| 653 | /* Map from raw value to enum value */ |
| 654 | for (j = 0; j < info->num_enum_values; j++) { |
| 655 | if (!info->enum_values[j].valid) |
| 656 | continue; |
| 657 | if (info->enum_values[j].value != props->prop_values[i]) |
| 658 | continue; |
| 659 | |
| 660 | return j; |
| 661 | } |
| 662 | |
| 663 | /* We don't have a mapping for this enum; return default. */ |
| 664 | break; |
| 665 | } |
| 666 | |
| 667 | return def; |
| 668 | } |
| 669 | |
| 670 | /** |
| 671 | * Cache DRM property values |
| 672 | * |
| 673 | * Update a per-object array of drm_property_info structures, given the |
| 674 | * DRM properties of the object. |
| 675 | * |
| 676 | * Call this every time an object newly appears (note that only connectors |
| 677 | * can be hotplugged), the first time it is seen, or when its status changes |
| 678 | * in a way which invalidates the potential property values (currently, the |
| 679 | * only case for this is connector hotplug). |
| 680 | * |
| 681 | * This updates the property IDs and enum values within the drm_property_info |
| 682 | * array. |
| 683 | * |
| 684 | * DRM property enum values are dynamic at runtime; the user must query the |
| 685 | * property to find out the desired runtime value for a requested string |
| 686 | * name. Using the 'type' field on planes as an example, there is no single |
| 687 | * hardcoded constant for primary plane types; instead, the property must be |
| 688 | * queried at runtime to find the value associated with the string "Primary". |
| 689 | * |
| 690 | * This helper queries and caches the enum values, to allow us to use a set |
| 691 | * of compile-time-constant enums portably across various implementations. |
| 692 | * The values given in enum_names are searched for, and stored in the |
| 693 | * same-indexed field of the map array. |
| 694 | * |
| 695 | * @param b DRM backend object |
| 696 | * @param src DRM property info array to source from |
| 697 | * @param info DRM property info array to copy into |
| 698 | * @param num_infos Number of entries in the source array |
| 699 | * @param props DRM object properties for the object |
| 700 | */ |
| 701 | static void |
| 702 | drm_property_info_populate(struct drm_backend *b, |
| 703 | const struct drm_property_info *src, |
| 704 | struct drm_property_info *info, |
| 705 | unsigned int num_infos, |
| 706 | drmModeObjectProperties *props) |
| 707 | { |
| 708 | drmModePropertyRes *prop; |
| 709 | unsigned i, j; |
| 710 | |
| 711 | for (i = 0; i < num_infos; i++) { |
| 712 | unsigned int j; |
| 713 | |
| 714 | info[i].name = src[i].name; |
| 715 | info[i].prop_id = 0; |
| 716 | info[i].num_enum_values = src[i].num_enum_values; |
| 717 | |
| 718 | if (src[i].num_enum_values == 0) |
| 719 | continue; |
| 720 | |
| 721 | info[i].enum_values = |
| 722 | malloc(src[i].num_enum_values * |
| 723 | sizeof(*info[i].enum_values)); |
| 724 | assert(info[i].enum_values); |
| 725 | for (j = 0; j < info[i].num_enum_values; j++) { |
| 726 | info[i].enum_values[j].name = src[i].enum_values[j].name; |
| 727 | info[i].enum_values[j].valid = false; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | for (i = 0; i < props->count_props; i++) { |
| 732 | unsigned int k; |
| 733 | |
| 734 | prop = drmModeGetProperty(b->drm.fd, props->props[i]); |
| 735 | if (!prop) |
| 736 | continue; |
| 737 | |
| 738 | for (j = 0; j < num_infos; j++) { |
| 739 | if (!strcmp(prop->name, info[j].name)) |
| 740 | break; |
| 741 | } |
| 742 | |
| 743 | /* We don't know/care about this property. */ |
| 744 | if (j == num_infos) { |
| 745 | #ifdef DEBUG |
| 746 | weston_log("DRM debug: unrecognized property %u '%s'\n", |
| 747 | prop->prop_id, prop->name); |
| 748 | #endif |
| 749 | drmModeFreeProperty(prop); |
| 750 | continue; |
| 751 | } |
| 752 | |
| 753 | if (info[j].num_enum_values == 0 && |
| 754 | (prop->flags & DRM_MODE_PROP_ENUM)) { |
| 755 | weston_log("DRM: expected property %s to not be an" |
| 756 | " enum, but it is; ignoring\n", prop->name); |
| 757 | drmModeFreeProperty(prop); |
| 758 | continue; |
| 759 | } |
| 760 | |
| 761 | info[j].prop_id = props->props[i]; |
| 762 | |
| 763 | if (info[j].num_enum_values == 0) { |
| 764 | drmModeFreeProperty(prop); |
| 765 | continue; |
| 766 | } |
| 767 | |
| 768 | if (!(prop->flags & DRM_MODE_PROP_ENUM)) { |
| 769 | weston_log("DRM: expected property %s to be an enum," |
| 770 | " but it is not; ignoring\n", prop->name); |
| 771 | drmModeFreeProperty(prop); |
| 772 | info[j].prop_id = 0; |
| 773 | continue; |
| 774 | } |
| 775 | |
| 776 | for (k = 0; k < info[j].num_enum_values; k++) { |
| 777 | int l; |
| 778 | |
| 779 | for (l = 0; l < prop->count_enums; l++) { |
| 780 | if (!strcmp(prop->enums[l].name, |
| 781 | info[j].enum_values[k].name)) |
| 782 | break; |
| 783 | } |
| 784 | |
| 785 | if (l == prop->count_enums) |
| 786 | continue; |
| 787 | |
| 788 | info[j].enum_values[k].valid = true; |
| 789 | info[j].enum_values[k].value = prop->enums[l].value; |
| 790 | } |
| 791 | |
| 792 | drmModeFreeProperty(prop); |
| 793 | } |
| 794 | |
| 795 | #ifdef DEBUG |
| 796 | for (i = 0; i < num_infos; i++) { |
| 797 | if (info[i].prop_id == 0) |
| 798 | weston_log("DRM warning: property '%s' missing\n", |
| 799 | info[i].name); |
| 800 | } |
| 801 | #endif |
| 802 | } |
| 803 | |
| 804 | /** |
| 805 | * Free DRM property information |
| 806 | * |
Pekka Paalanen | 46e4f97 | 2017-09-07 15:32:01 +0300 | [diff] [blame] | 807 | * Frees all memory associated with a DRM property info array and zeroes |
| 808 | * it out, leaving it usable for a further drm_property_info_update() or |
| 809 | * drm_property_info_free(). |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 810 | * |
| 811 | * @param info DRM property info array |
| 812 | * @param num_props Number of entries in array to free |
| 813 | */ |
| 814 | static void |
| 815 | drm_property_info_free(struct drm_property_info *info, int num_props) |
| 816 | { |
| 817 | int i; |
| 818 | |
| 819 | for (i = 0; i < num_props; i++) |
| 820 | free(info[i].enum_values); |
Pekka Paalanen | 46e4f97 | 2017-09-07 15:32:01 +0300 | [diff] [blame] | 821 | |
| 822 | memset(info, 0, sizeof(*info) * num_props); |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 823 | } |
| 824 | |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 825 | static void |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 826 | drm_output_set_cursor(struct drm_output_state *output_state); |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 827 | |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 828 | static void |
| 829 | drm_output_update_msc(struct drm_output *output, unsigned int seq); |
| 830 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 831 | static void |
| 832 | drm_output_destroy(struct weston_output *output_base); |
| 833 | |
Daniel Stone | 5ff289a | 2017-10-07 12:59:02 +0100 | [diff] [blame] | 834 | /** |
| 835 | * Returns true if the plane can be used on the given output for its current |
| 836 | * repaint cycle. |
| 837 | */ |
| 838 | static bool |
| 839 | drm_plane_is_available(struct drm_plane *plane, struct drm_output *output) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 840 | { |
Daniel Stone | 5ff289a | 2017-10-07 12:59:02 +0100 | [diff] [blame] | 841 | assert(plane->state_cur); |
| 842 | |
| 843 | /* The plane still has a request not yet completed by the kernel. */ |
| 844 | if (!plane->state_cur->complete) |
| 845 | return false; |
| 846 | |
| 847 | /* The plane is still active on another output. */ |
| 848 | if (plane->state_cur->output && plane->state_cur->output != output) |
| 849 | return false; |
| 850 | |
| 851 | /* Check whether the plane can be used with this CRTC; possible_crtcs |
| 852 | * is a bitmask of CRTC indices (pipe), rather than CRTC object ID. */ |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 853 | return !!(plane->possible_crtcs & (1 << output->pipe)); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 854 | } |
| 855 | |
Daniel Stone | 72c0e1b | 2017-02-09 13:49:15 +0000 | [diff] [blame] | 856 | static struct drm_output * |
| 857 | drm_output_find_by_crtc(struct drm_backend *b, uint32_t crtc_id) |
| 858 | { |
| 859 | struct drm_output *output; |
| 860 | |
| 861 | wl_list_for_each(output, &b->compositor->output_list, base.link) { |
| 862 | if (output->crtc_id == crtc_id) |
| 863 | return output; |
| 864 | } |
| 865 | |
Daniel Stone | 72c0e1b | 2017-02-09 13:49:15 +0000 | [diff] [blame] | 866 | return NULL; |
| 867 | } |
| 868 | |
Pekka Paalanen | 54cc47c | 2017-08-31 11:58:41 +0300 | [diff] [blame] | 869 | static struct drm_head * |
| 870 | drm_head_find_by_connector(struct drm_backend *backend, uint32_t connector_id) |
| 871 | { |
| 872 | struct weston_head *base; |
| 873 | struct drm_head *head; |
| 874 | |
| 875 | wl_list_for_each(base, |
| 876 | &backend->compositor->head_list, compositor_link) { |
| 877 | head = to_drm_head(base); |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 878 | if (head->connector_id == connector_id) |
Pekka Paalanen | 54cc47c | 2017-08-31 11:58:41 +0300 | [diff] [blame] | 879 | return head; |
| 880 | } |
| 881 | |
| 882 | return NULL; |
| 883 | } |
| 884 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 885 | static void |
Tomohito Esaki | 576f42e | 2017-04-04 17:54:24 +0100 | [diff] [blame] | 886 | drm_fb_destroy(struct drm_fb *fb) |
| 887 | { |
| 888 | if (fb->fb_id != 0) |
| 889 | drmModeRmFB(fb->fd, fb->fb_id); |
| 890 | weston_buffer_reference(&fb->buffer_ref, NULL); |
| 891 | free(fb); |
| 892 | } |
| 893 | |
| 894 | static void |
| 895 | drm_fb_destroy_dumb(struct drm_fb *fb) |
| 896 | { |
| 897 | struct drm_mode_destroy_dumb destroy_arg; |
| 898 | |
| 899 | assert(fb->type == BUFFER_PIXMAN_DUMB); |
| 900 | |
| 901 | if (fb->map && fb->size > 0) |
| 902 | munmap(fb->map, fb->size); |
| 903 | |
| 904 | memset(&destroy_arg, 0, sizeof(destroy_arg)); |
Daniel Stone | 8eece0c | 2016-11-17 17:54:00 +0000 | [diff] [blame] | 905 | destroy_arg.handle = fb->handles[0]; |
Tomohito Esaki | 576f42e | 2017-04-04 17:54:24 +0100 | [diff] [blame] | 906 | drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg); |
| 907 | |
| 908 | drm_fb_destroy(fb); |
| 909 | } |
| 910 | |
| 911 | static void |
| 912 | drm_fb_destroy_gbm(struct gbm_bo *bo, void *data) |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 913 | { |
| 914 | struct drm_fb *fb = data; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 915 | |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 916 | assert(fb->type == BUFFER_GBM_SURFACE || fb->type == BUFFER_CLIENT || |
| 917 | fb->type == BUFFER_CURSOR); |
Tomohito Esaki | 576f42e | 2017-04-04 17:54:24 +0100 | [diff] [blame] | 918 | drm_fb_destroy(fb); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 919 | } |
| 920 | |
Daniel Stone | 8eece0c | 2016-11-17 17:54:00 +0000 | [diff] [blame] | 921 | static int |
| 922 | drm_fb_addfb(struct drm_fb *fb) |
| 923 | { |
Daniel Stone | 65a4dbc | 2016-12-08 16:36:18 +0000 | [diff] [blame] | 924 | int ret = -EINVAL; |
| 925 | #ifdef HAVE_DRM_ADDFB2_MODIFIERS |
| 926 | uint64_t mods[4] = { }; |
Daniel Stone | dc082cb | 2018-07-09 13:49:04 +0100 | [diff] [blame] | 927 | size_t i; |
Daniel Stone | 65a4dbc | 2016-12-08 16:36:18 +0000 | [diff] [blame] | 928 | #endif |
| 929 | |
| 930 | /* If we have a modifier set, we must only use the WithModifiers |
| 931 | * entrypoint; we cannot import it through legacy ioctls. */ |
| 932 | if (fb->modifier != DRM_FORMAT_MOD_INVALID) { |
| 933 | /* KMS demands that if a modifier is set, it must be the same |
| 934 | * for all planes. */ |
| 935 | #ifdef HAVE_DRM_ADDFB2_MODIFIERS |
Daniel Stone | dc082cb | 2018-07-09 13:49:04 +0100 | [diff] [blame] | 936 | for (i = 0; i < ARRAY_LENGTH(mods) && fb->handles[i]; i++) |
Daniel Stone | 65a4dbc | 2016-12-08 16:36:18 +0000 | [diff] [blame] | 937 | mods[i] = fb->modifier; |
| 938 | ret = drmModeAddFB2WithModifiers(fb->fd, fb->width, fb->height, |
| 939 | fb->format->format, |
| 940 | fb->handles, fb->strides, |
| 941 | fb->offsets, mods, &fb->fb_id, |
| 942 | DRM_MODE_FB_MODIFIERS); |
| 943 | #endif |
| 944 | return ret; |
| 945 | } |
Daniel Stone | 8eece0c | 2016-11-17 17:54:00 +0000 | [diff] [blame] | 946 | |
| 947 | ret = drmModeAddFB2(fb->fd, fb->width, fb->height, fb->format->format, |
| 948 | fb->handles, fb->strides, fb->offsets, &fb->fb_id, |
| 949 | 0); |
| 950 | if (ret == 0) |
| 951 | return 0; |
| 952 | |
| 953 | /* Legacy AddFB can't always infer the format from depth/bpp alone, so |
| 954 | * check if our format is one of the lucky ones. */ |
| 955 | if (!fb->format->depth || !fb->format->bpp) |
| 956 | return ret; |
| 957 | |
| 958 | /* Cannot fall back to AddFB for multi-planar formats either. */ |
| 959 | if (fb->handles[1] || fb->handles[2] || fb->handles[3]) |
| 960 | return ret; |
| 961 | |
| 962 | ret = drmModeAddFB(fb->fd, fb->width, fb->height, |
| 963 | fb->format->depth, fb->format->bpp, |
| 964 | fb->strides[0], fb->handles[0], &fb->fb_id); |
| 965 | return ret; |
| 966 | } |
| 967 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 968 | static struct drm_fb * |
Daniel Stone | f214fdc | 2016-11-14 17:43:57 +0000 | [diff] [blame] | 969 | drm_fb_create_dumb(struct drm_backend *b, int width, int height, |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 970 | uint32_t format) |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 971 | { |
| 972 | struct drm_fb *fb; |
| 973 | int ret; |
| 974 | |
| 975 | struct drm_mode_create_dumb create_arg; |
| 976 | struct drm_mode_destroy_dumb destroy_arg; |
| 977 | struct drm_mode_map_dumb map_arg; |
| 978 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 979 | fb = zalloc(sizeof *fb); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 980 | if (!fb) |
| 981 | return NULL; |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 982 | fb->refcnt = 1; |
| 983 | |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 984 | fb->format = pixel_format_get_info(format); |
| 985 | if (!fb->format) { |
| 986 | weston_log("failed to look up format 0x%lx\n", |
| 987 | (unsigned long) format); |
| 988 | goto err_fb; |
| 989 | } |
| 990 | |
| 991 | if (!fb->format->depth || !fb->format->bpp) { |
| 992 | weston_log("format 0x%lx is not compatible with dumb buffers\n", |
| 993 | (unsigned long) format); |
| 994 | goto err_fb; |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 995 | } |
| 996 | |
Kristian Høgsberg | ac6104e | 2013-08-21 22:14:14 -0700 | [diff] [blame] | 997 | memset(&create_arg, 0, sizeof create_arg); |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 998 | create_arg.bpp = fb->format->bpp; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 999 | create_arg.width = width; |
| 1000 | create_arg.height = height; |
| 1001 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1002 | 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] | 1003 | if (ret) |
| 1004 | goto err_fb; |
| 1005 | |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 1006 | fb->type = BUFFER_PIXMAN_DUMB; |
Daniel Stone | 65a4dbc | 2016-12-08 16:36:18 +0000 | [diff] [blame] | 1007 | fb->modifier = DRM_FORMAT_MOD_INVALID; |
Daniel Stone | 8eece0c | 2016-11-17 17:54:00 +0000 | [diff] [blame] | 1008 | fb->handles[0] = create_arg.handle; |
| 1009 | fb->strides[0] = create_arg.pitch; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 1010 | fb->size = create_arg.size; |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 1011 | fb->width = width; |
| 1012 | fb->height = height; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1013 | fb->fd = b->drm.fd; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 1014 | |
Daniel Stone | 8eece0c | 2016-11-17 17:54:00 +0000 | [diff] [blame] | 1015 | if (drm_fb_addfb(fb) != 0) { |
| 1016 | weston_log("failed to create kms fb: %m\n"); |
| 1017 | goto err_bo; |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 1018 | } |
| 1019 | |
Kristian Høgsberg | ac6104e | 2013-08-21 22:14:14 -0700 | [diff] [blame] | 1020 | memset(&map_arg, 0, sizeof map_arg); |
Daniel Stone | 8eece0c | 2016-11-17 17:54:00 +0000 | [diff] [blame] | 1021 | map_arg.handle = fb->handles[0]; |
Chris Michael | eb2074a | 2013-05-01 21:26:02 -0400 | [diff] [blame] | 1022 | 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] | 1023 | if (ret) |
| 1024 | goto err_add_fb; |
| 1025 | |
Chris Michael | 4a7ce1f | 2015-11-10 10:40:37 -0500 | [diff] [blame] | 1026 | fb->map = mmap(NULL, fb->size, PROT_WRITE, |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1027 | MAP_SHARED, b->drm.fd, map_arg.offset); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 1028 | if (fb->map == MAP_FAILED) |
| 1029 | goto err_add_fb; |
| 1030 | |
| 1031 | return fb; |
| 1032 | |
| 1033 | err_add_fb: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1034 | drmModeRmFB(b->drm.fd, fb->fb_id); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 1035 | err_bo: |
| 1036 | memset(&destroy_arg, 0, sizeof(destroy_arg)); |
| 1037 | destroy_arg.handle = create_arg.handle; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1038 | 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] | 1039 | err_fb: |
| 1040 | free(fb); |
| 1041 | return NULL; |
| 1042 | } |
| 1043 | |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 1044 | static struct drm_fb * |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 1045 | drm_fb_ref(struct drm_fb *fb) |
| 1046 | { |
| 1047 | fb->refcnt++; |
| 1048 | return fb; |
| 1049 | } |
| 1050 | |
Daniel Stone | f522e22 | 2016-11-18 12:31:26 +0000 | [diff] [blame] | 1051 | static void |
| 1052 | drm_fb_destroy_dmabuf(struct drm_fb *fb) |
| 1053 | { |
| 1054 | /* We deliberately do not close the GEM handles here; GBM manages |
| 1055 | * their lifetime through the BO. */ |
| 1056 | if (fb->bo) |
| 1057 | gbm_bo_destroy(fb->bo); |
| 1058 | drm_fb_destroy(fb); |
| 1059 | } |
| 1060 | |
| 1061 | static struct drm_fb * |
| 1062 | drm_fb_get_from_dmabuf(struct linux_dmabuf_buffer *dmabuf, |
| 1063 | struct drm_backend *backend, bool is_opaque) |
| 1064 | { |
| 1065 | #ifdef HAVE_GBM_FD_IMPORT |
| 1066 | struct drm_fb *fb; |
| 1067 | struct gbm_import_fd_data import_legacy = { |
| 1068 | .width = dmabuf->attributes.width, |
| 1069 | .height = dmabuf->attributes.height, |
| 1070 | .format = dmabuf->attributes.format, |
| 1071 | .stride = dmabuf->attributes.stride[0], |
| 1072 | .fd = dmabuf->attributes.fd[0], |
| 1073 | }; |
| 1074 | struct gbm_import_fd_modifier_data import_mod = { |
| 1075 | .width = dmabuf->attributes.width, |
| 1076 | .height = dmabuf->attributes.height, |
| 1077 | .format = dmabuf->attributes.format, |
| 1078 | .num_fds = dmabuf->attributes.n_planes, |
| 1079 | .modifier = dmabuf->attributes.modifier[0], |
| 1080 | }; |
| 1081 | int i; |
| 1082 | |
| 1083 | /* XXX: TODO: |
| 1084 | * |
| 1085 | * Currently the buffer is rejected if any dmabuf attribute |
| 1086 | * flag is set. This keeps us from passing an inverted / |
| 1087 | * interlaced / bottom-first buffer (or any other type that may |
| 1088 | * be added in the future) through to an overlay. Ultimately, |
| 1089 | * these types of buffers should be handled through buffer |
| 1090 | * transforms and not as spot-checks requiring specific |
| 1091 | * knowledge. */ |
| 1092 | if (dmabuf->attributes.flags) |
| 1093 | return NULL; |
| 1094 | |
| 1095 | fb = zalloc(sizeof *fb); |
| 1096 | if (fb == NULL) |
| 1097 | return NULL; |
| 1098 | |
| 1099 | fb->refcnt = 1; |
| 1100 | fb->type = BUFFER_DMABUF; |
| 1101 | |
| 1102 | static_assert(ARRAY_LENGTH(import_mod.fds) == |
| 1103 | ARRAY_LENGTH(dmabuf->attributes.fd), |
| 1104 | "GBM and linux_dmabuf FD size must match"); |
| 1105 | static_assert(sizeof(import_mod.fds) == sizeof(dmabuf->attributes.fd), |
| 1106 | "GBM and linux_dmabuf FD size must match"); |
| 1107 | memcpy(import_mod.fds, dmabuf->attributes.fd, sizeof(import_mod.fds)); |
| 1108 | |
| 1109 | static_assert(ARRAY_LENGTH(import_mod.strides) == |
| 1110 | ARRAY_LENGTH(dmabuf->attributes.stride), |
| 1111 | "GBM and linux_dmabuf stride size must match"); |
| 1112 | static_assert(sizeof(import_mod.strides) == |
| 1113 | sizeof(dmabuf->attributes.stride), |
| 1114 | "GBM and linux_dmabuf stride size must match"); |
| 1115 | memcpy(import_mod.strides, dmabuf->attributes.stride, |
| 1116 | sizeof(import_mod.strides)); |
| 1117 | |
| 1118 | static_assert(ARRAY_LENGTH(import_mod.offsets) == |
| 1119 | ARRAY_LENGTH(dmabuf->attributes.offset), |
| 1120 | "GBM and linux_dmabuf offset size must match"); |
| 1121 | static_assert(sizeof(import_mod.offsets) == |
| 1122 | sizeof(dmabuf->attributes.offset), |
| 1123 | "GBM and linux_dmabuf offset size must match"); |
| 1124 | memcpy(import_mod.offsets, dmabuf->attributes.offset, |
| 1125 | sizeof(import_mod.offsets)); |
| 1126 | |
| 1127 | /* The legacy FD-import path does not allow us to supply modifiers, |
| 1128 | * multiple planes, or buffer offsets. */ |
| 1129 | if (dmabuf->attributes.modifier[0] != DRM_FORMAT_MOD_INVALID || |
| 1130 | import_mod.num_fds > 1 || |
| 1131 | import_mod.offsets[0] > 0) { |
| 1132 | fb->bo = gbm_bo_import(backend->gbm, GBM_BO_IMPORT_FD_MODIFIER, |
| 1133 | &import_mod, |
| 1134 | GBM_BO_USE_SCANOUT); |
| 1135 | } else { |
| 1136 | fb->bo = gbm_bo_import(backend->gbm, GBM_BO_IMPORT_FD, |
| 1137 | &import_legacy, |
| 1138 | GBM_BO_USE_SCANOUT); |
| 1139 | } |
| 1140 | |
| 1141 | if (!fb->bo) |
| 1142 | goto err_free; |
| 1143 | |
| 1144 | fb->width = dmabuf->attributes.width; |
| 1145 | fb->height = dmabuf->attributes.height; |
| 1146 | fb->modifier = dmabuf->attributes.modifier[0]; |
| 1147 | fb->size = 0; |
| 1148 | fb->fd = backend->drm.fd; |
| 1149 | |
| 1150 | static_assert(ARRAY_LENGTH(fb->strides) == |
| 1151 | ARRAY_LENGTH(dmabuf->attributes.stride), |
| 1152 | "drm_fb and dmabuf stride size must match"); |
| 1153 | static_assert(sizeof(fb->strides) == sizeof(dmabuf->attributes.stride), |
| 1154 | "drm_fb and dmabuf stride size must match"); |
| 1155 | memcpy(fb->strides, dmabuf->attributes.stride, sizeof(fb->strides)); |
| 1156 | static_assert(ARRAY_LENGTH(fb->offsets) == |
| 1157 | ARRAY_LENGTH(dmabuf->attributes.offset), |
| 1158 | "drm_fb and dmabuf offset size must match"); |
| 1159 | static_assert(sizeof(fb->offsets) == sizeof(dmabuf->attributes.offset), |
| 1160 | "drm_fb and dmabuf offset size must match"); |
| 1161 | memcpy(fb->offsets, dmabuf->attributes.offset, sizeof(fb->offsets)); |
| 1162 | |
| 1163 | fb->format = pixel_format_get_info(dmabuf->attributes.format); |
| 1164 | if (!fb->format) { |
| 1165 | weston_log("couldn't look up format info for 0x%lx\n", |
| 1166 | (unsigned long) dmabuf->attributes.format); |
| 1167 | goto err_free; |
| 1168 | } |
| 1169 | |
| 1170 | if (is_opaque) |
| 1171 | fb->format = pixel_format_get_opaque_substitute(fb->format); |
| 1172 | |
| 1173 | if (backend->min_width > fb->width || |
| 1174 | fb->width > backend->max_width || |
| 1175 | backend->min_height > fb->height || |
| 1176 | fb->height > backend->max_height) { |
| 1177 | weston_log("bo geometry out of bounds\n"); |
| 1178 | goto err_free; |
| 1179 | } |
| 1180 | |
| 1181 | for (i = 0; i < dmabuf->attributes.n_planes; i++) { |
| 1182 | fb->handles[i] = gbm_bo_get_handle_for_plane(fb->bo, i).u32; |
| 1183 | if (!fb->handles[i]) |
| 1184 | goto err_free; |
| 1185 | } |
| 1186 | |
| 1187 | if (drm_fb_addfb(fb) != 0) { |
| 1188 | weston_log("failed to create kms fb: %m\n"); |
| 1189 | goto err_free; |
| 1190 | } |
| 1191 | |
| 1192 | return fb; |
| 1193 | |
| 1194 | err_free: |
| 1195 | drm_fb_destroy_dmabuf(fb); |
| 1196 | #endif |
| 1197 | return NULL; |
| 1198 | } |
| 1199 | |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 1200 | static struct drm_fb * |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 1201 | drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend, |
Daniel Stone | db10df1 | 2016-12-08 13:15:58 +0000 | [diff] [blame] | 1202 | bool is_opaque, enum drm_fb_type type) |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1203 | { |
| 1204 | struct drm_fb *fb = gbm_bo_get_user_data(bo); |
Daniel Stone | 244244d | 2016-11-18 18:02:08 +0000 | [diff] [blame] | 1205 | #ifdef HAVE_GBM_MODIFIERS |
| 1206 | int i; |
| 1207 | #endif |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1208 | |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 1209 | if (fb) { |
| 1210 | assert(fb->type == type); |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 1211 | return drm_fb_ref(fb); |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 1212 | } |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1213 | |
Bryce Harrington | de16d89 | 2014-11-20 22:21:57 -0800 | [diff] [blame] | 1214 | fb = zalloc(sizeof *fb); |
| 1215 | if (fb == NULL) |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 1216 | return NULL; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1217 | |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 1218 | fb->type = type; |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 1219 | fb->refcnt = 1; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1220 | fb->bo = bo; |
Daniel Stone | 244244d | 2016-11-18 18:02:08 +0000 | [diff] [blame] | 1221 | fb->fd = backend->drm.fd; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1222 | |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 1223 | fb->width = gbm_bo_get_width(bo); |
| 1224 | fb->height = gbm_bo_get_height(bo); |
Daniel Stone | 244244d | 2016-11-18 18:02:08 +0000 | [diff] [blame] | 1225 | fb->format = pixel_format_get_info(gbm_bo_get_format(bo)); |
| 1226 | fb->size = 0; |
| 1227 | |
| 1228 | #ifdef HAVE_GBM_MODIFIERS |
| 1229 | fb->modifier = gbm_bo_get_modifier(bo); |
| 1230 | for (i = 0; i < gbm_bo_get_plane_count(bo); i++) { |
| 1231 | fb->strides[i] = gbm_bo_get_stride_for_plane(bo, i); |
| 1232 | fb->handles[i] = gbm_bo_get_handle_for_plane(bo, i).u32; |
| 1233 | fb->offsets[i] = gbm_bo_get_offset(bo, i); |
| 1234 | } |
| 1235 | #else |
Daniel Stone | 8eece0c | 2016-11-17 17:54:00 +0000 | [diff] [blame] | 1236 | fb->strides[0] = gbm_bo_get_stride(bo); |
| 1237 | fb->handles[0] = gbm_bo_get_handle(bo).u32; |
Daniel Stone | 65a4dbc | 2016-12-08 16:36:18 +0000 | [diff] [blame] | 1238 | fb->modifier = DRM_FORMAT_MOD_INVALID; |
Daniel Stone | 244244d | 2016-11-18 18:02:08 +0000 | [diff] [blame] | 1239 | #endif |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1240 | |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 1241 | if (!fb->format) { |
| 1242 | weston_log("couldn't look up format 0x%lx\n", |
Daniel Stone | db10df1 | 2016-12-08 13:15:58 +0000 | [diff] [blame] | 1243 | (unsigned long) gbm_bo_get_format(bo)); |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 1244 | goto err_free; |
| 1245 | } |
| 1246 | |
Daniel Stone | db10df1 | 2016-12-08 13:15:58 +0000 | [diff] [blame] | 1247 | /* We can scanout an ARGB buffer if the surface's opaque region covers |
| 1248 | * the whole output, but we have to use XRGB as the KMS format code. */ |
| 1249 | if (is_opaque) |
| 1250 | fb->format = pixel_format_get_opaque_substitute(fb->format); |
| 1251 | |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 1252 | if (backend->min_width > fb->width || |
| 1253 | fb->width > backend->max_width || |
| 1254 | backend->min_height > fb->height || |
| 1255 | fb->height > backend->max_height) { |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1256 | weston_log("bo geometry out of bounds\n"); |
| 1257 | goto err_free; |
| 1258 | } |
| 1259 | |
Daniel Stone | 8eece0c | 2016-11-17 17:54:00 +0000 | [diff] [blame] | 1260 | if (drm_fb_addfb(fb) != 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1261 | weston_log("failed to create kms fb: %m\n"); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1262 | goto err_free; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1263 | } |
| 1264 | |
Tomohito Esaki | 576f42e | 2017-04-04 17:54:24 +0100 | [diff] [blame] | 1265 | 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] | 1266 | |
| 1267 | return fb; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1268 | |
| 1269 | err_free: |
| 1270 | free(fb); |
| 1271 | return NULL; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | static void |
Jason Ekstrand | 6bd6294 | 2013-06-20 20:38:23 -0500 | [diff] [blame] | 1275 | 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] | 1276 | { |
Pekka Paalanen | de685b8 | 2012-12-04 15:58:12 +0200 | [diff] [blame] | 1277 | assert(fb->buffer_ref.buffer == NULL); |
Daniel Stone | f522e22 | 2016-11-18 12:31:26 +0000 | [diff] [blame] | 1278 | assert(fb->type == BUFFER_CLIENT || fb->type == BUFFER_DMABUF); |
Pekka Paalanen | de685b8 | 2012-12-04 15:58:12 +0200 | [diff] [blame] | 1279 | weston_buffer_reference(&fb->buffer_ref, buffer); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1280 | } |
| 1281 | |
Ander Conselvan de Oliveira | 526d461 | 2013-01-25 15:13:03 +0200 | [diff] [blame] | 1282 | static void |
Daniel Stone | 05a5ac2 | 2017-04-04 17:54:25 +0100 | [diff] [blame] | 1283 | drm_fb_unref(struct drm_fb *fb) |
Ander Conselvan de Oliveira | 526d461 | 2013-01-25 15:13:03 +0200 | [diff] [blame] | 1284 | { |
| 1285 | if (!fb) |
| 1286 | return; |
| 1287 | |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 1288 | assert(fb->refcnt > 0); |
| 1289 | if (--fb->refcnt > 0) |
| 1290 | return; |
| 1291 | |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 1292 | switch (fb->type) { |
| 1293 | case BUFFER_PIXMAN_DUMB: |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 1294 | drm_fb_destroy_dumb(fb); |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 1295 | break; |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 1296 | case BUFFER_CURSOR: |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 1297 | case BUFFER_CLIENT: |
| 1298 | gbm_bo_destroy(fb->bo); |
| 1299 | break; |
| 1300 | case BUFFER_GBM_SURFACE: |
Daniel Stone | 05a5ac2 | 2017-04-04 17:54:25 +0100 | [diff] [blame] | 1301 | gbm_surface_release_buffer(fb->gbm_surface, fb->bo); |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 1302 | break; |
Daniel Stone | f522e22 | 2016-11-18 12:31:26 +0000 | [diff] [blame] | 1303 | case BUFFER_DMABUF: |
| 1304 | drm_fb_destroy_dmabuf(fb); |
| 1305 | break; |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 1306 | default: |
| 1307 | assert(NULL); |
| 1308 | break; |
Ander Conselvan de Oliveira | 526d461 | 2013-01-25 15:13:03 +0200 | [diff] [blame] | 1309 | } |
| 1310 | } |
| 1311 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1312 | /** |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1313 | * Allocate a new, empty, plane state. |
| 1314 | */ |
| 1315 | static struct drm_plane_state * |
| 1316 | drm_plane_state_alloc(struct drm_output_state *state_output, |
| 1317 | struct drm_plane *plane) |
| 1318 | { |
| 1319 | struct drm_plane_state *state = zalloc(sizeof(*state)); |
| 1320 | |
| 1321 | assert(state); |
| 1322 | state->output_state = state_output; |
| 1323 | state->plane = plane; |
| 1324 | |
| 1325 | /* Here we only add the plane state to the desired link, and not |
| 1326 | * set the member. Having an output pointer set means that the |
| 1327 | * plane will be displayed on the output; this won't be the case |
| 1328 | * when we go to disable a plane. In this case, it must be part of |
| 1329 | * the commit (and thus the output state), but the member must be |
| 1330 | * NULL, as it will not be on any output when the state takes |
| 1331 | * effect. |
| 1332 | */ |
| 1333 | if (state_output) |
| 1334 | wl_list_insert(&state_output->plane_list, &state->link); |
| 1335 | else |
| 1336 | wl_list_init(&state->link); |
| 1337 | |
| 1338 | return state; |
| 1339 | } |
| 1340 | |
| 1341 | /** |
| 1342 | * Free an existing plane state. As a special case, the state will not |
| 1343 | * normally be freed if it is the current state; see drm_plane_set_state. |
| 1344 | */ |
| 1345 | static void |
| 1346 | drm_plane_state_free(struct drm_plane_state *state, bool force) |
| 1347 | { |
| 1348 | if (!state) |
| 1349 | return; |
| 1350 | |
| 1351 | wl_list_remove(&state->link); |
| 1352 | wl_list_init(&state->link); |
| 1353 | state->output_state = NULL; |
| 1354 | |
| 1355 | if (force || state != state->plane->state_cur) { |
| 1356 | drm_fb_unref(state->fb); |
| 1357 | free(state); |
| 1358 | } |
| 1359 | } |
| 1360 | |
| 1361 | /** |
| 1362 | * Duplicate an existing plane state into a new plane state, storing it within |
| 1363 | * the given output state. If the output state already contains a plane state |
| 1364 | * for the drm_plane referenced by 'src', that plane state is freed first. |
| 1365 | */ |
| 1366 | static struct drm_plane_state * |
| 1367 | drm_plane_state_duplicate(struct drm_output_state *state_output, |
| 1368 | struct drm_plane_state *src) |
| 1369 | { |
| 1370 | struct drm_plane_state *dst = malloc(sizeof(*dst)); |
| 1371 | struct drm_plane_state *old, *tmp; |
| 1372 | |
| 1373 | assert(src); |
| 1374 | assert(dst); |
| 1375 | *dst = *src; |
| 1376 | wl_list_init(&dst->link); |
| 1377 | |
| 1378 | wl_list_for_each_safe(old, tmp, &state_output->plane_list, link) { |
| 1379 | /* Duplicating a plane state into the same output state, so |
| 1380 | * it can replace itself with an identical copy of itself, |
| 1381 | * makes no sense. */ |
| 1382 | assert(old != src); |
| 1383 | if (old->plane == dst->plane) |
| 1384 | drm_plane_state_free(old, false); |
| 1385 | } |
| 1386 | |
| 1387 | wl_list_insert(&state_output->plane_list, &dst->link); |
| 1388 | if (src->fb) |
| 1389 | dst->fb = drm_fb_ref(src->fb); |
| 1390 | dst->output_state = state_output; |
| 1391 | dst->complete = false; |
| 1392 | |
| 1393 | return dst; |
| 1394 | } |
| 1395 | |
| 1396 | /** |
| 1397 | * Remove a plane state from an output state; if the plane was previously |
| 1398 | * enabled, then replace it with a disabling state. This ensures that the |
| 1399 | * output state was untouched from it was before the plane state was |
| 1400 | * modified by the caller of this function. |
| 1401 | * |
| 1402 | * This is required as drm_output_state_get_plane may either allocate a |
| 1403 | * new plane state, in which case this function will just perform a matching |
| 1404 | * drm_plane_state_free, or it may instead repurpose an existing disabling |
| 1405 | * state (if the plane was previously active), in which case this function |
| 1406 | * will reset it. |
| 1407 | */ |
| 1408 | static void |
| 1409 | drm_plane_state_put_back(struct drm_plane_state *state) |
| 1410 | { |
| 1411 | struct drm_output_state *state_output; |
| 1412 | struct drm_plane *plane; |
| 1413 | |
| 1414 | if (!state) |
| 1415 | return; |
| 1416 | |
| 1417 | state_output = state->output_state; |
| 1418 | plane = state->plane; |
| 1419 | drm_plane_state_free(state, false); |
| 1420 | |
| 1421 | /* Plane was previously disabled; no need to keep this temporary |
| 1422 | * state around. */ |
| 1423 | if (!plane->state_cur->fb) |
| 1424 | return; |
| 1425 | |
| 1426 | (void) drm_plane_state_alloc(state_output, plane); |
| 1427 | } |
| 1428 | |
Daniel Stone | ce13747 | 2016-11-16 19:35:03 +0000 | [diff] [blame] | 1429 | static bool |
| 1430 | drm_view_transform_supported(struct weston_view *ev, struct weston_output *output) |
| 1431 | { |
| 1432 | struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport; |
| 1433 | |
| 1434 | /* This will incorrectly disallow cases where the combination of |
| 1435 | * buffer and view transformations match the output transform. |
| 1436 | * Fixing this requires a full analysis of the transformation |
| 1437 | * chain. */ |
| 1438 | if (ev->transform.enabled && |
| 1439 | ev->transform.matrix.type >= WESTON_MATRIX_TRANSFORM_ROTATE) |
| 1440 | return false; |
| 1441 | |
| 1442 | if (viewport->buffer.transform != output->transform) |
| 1443 | return false; |
| 1444 | |
| 1445 | return true; |
| 1446 | } |
| 1447 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1448 | /** |
Daniel Stone | d6e2a76 | 2016-11-16 19:33:20 +0000 | [diff] [blame] | 1449 | * Given a weston_view, fill the drm_plane_state's co-ordinates to display on |
| 1450 | * a given plane. |
| 1451 | */ |
Daniel Stone | ce13747 | 2016-11-16 19:35:03 +0000 | [diff] [blame] | 1452 | static bool |
Daniel Stone | d6e2a76 | 2016-11-16 19:33:20 +0000 | [diff] [blame] | 1453 | drm_plane_state_coords_for_view(struct drm_plane_state *state, |
| 1454 | struct weston_view *ev) |
| 1455 | { |
| 1456 | struct drm_output *output = state->output; |
Daniel Stone | df2726a | 2017-02-07 18:48:19 +0000 | [diff] [blame] | 1457 | struct weston_buffer *buffer = ev->surface->buffer_ref.buffer; |
Daniel Stone | d6e2a76 | 2016-11-16 19:33:20 +0000 | [diff] [blame] | 1458 | pixman_region32_t dest_rect, src_rect; |
| 1459 | pixman_box32_t *box, tbox; |
Daniel Stone | df2726a | 2017-02-07 18:48:19 +0000 | [diff] [blame] | 1460 | float sxf1, syf1, sxf2, syf2; |
Daniel Stone | d6e2a76 | 2016-11-16 19:33:20 +0000 | [diff] [blame] | 1461 | |
Daniel Stone | ce13747 | 2016-11-16 19:35:03 +0000 | [diff] [blame] | 1462 | if (!drm_view_transform_supported(ev, &output->base)) |
| 1463 | return false; |
| 1464 | |
Daniel Stone | d6e2a76 | 2016-11-16 19:33:20 +0000 | [diff] [blame] | 1465 | /* Update the base weston_plane co-ordinates. */ |
| 1466 | box = pixman_region32_extents(&ev->transform.boundingbox); |
| 1467 | state->plane->base.x = box->x1; |
| 1468 | state->plane->base.y = box->y1; |
| 1469 | |
| 1470 | /* First calculate the destination co-ordinates by taking the |
| 1471 | * area of the view which is visible on this output, performing any |
| 1472 | * transforms to account for output rotation and scale as necessary. */ |
| 1473 | pixman_region32_init(&dest_rect); |
| 1474 | pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox, |
| 1475 | &output->base.region); |
| 1476 | pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y); |
| 1477 | box = pixman_region32_extents(&dest_rect); |
| 1478 | tbox = weston_transformed_rect(output->base.width, |
| 1479 | output->base.height, |
| 1480 | output->base.transform, |
| 1481 | output->base.current_scale, |
| 1482 | *box); |
| 1483 | state->dest_x = tbox.x1; |
| 1484 | state->dest_y = tbox.y1; |
| 1485 | state->dest_w = tbox.x2 - tbox.x1; |
| 1486 | state->dest_h = tbox.y2 - tbox.y1; |
| 1487 | pixman_region32_fini(&dest_rect); |
| 1488 | |
| 1489 | /* Now calculate the source rectangle, by finding the extents of the |
| 1490 | * view, and working backwards to source co-ordinates. */ |
| 1491 | pixman_region32_init(&src_rect); |
| 1492 | pixman_region32_intersect(&src_rect, &ev->transform.boundingbox, |
| 1493 | &output->base.region); |
| 1494 | box = pixman_region32_extents(&src_rect); |
Daniel Stone | df2726a | 2017-02-07 18:48:19 +0000 | [diff] [blame] | 1495 | weston_view_from_global_float(ev, box->x1, box->y1, &sxf1, &syf1); |
| 1496 | weston_surface_to_buffer_float(ev->surface, sxf1, syf1, &sxf1, &syf1); |
| 1497 | weston_view_from_global_float(ev, box->x2, box->y2, &sxf2, &syf2); |
| 1498 | weston_surface_to_buffer_float(ev->surface, sxf2, syf2, &sxf2, &syf2); |
| 1499 | pixman_region32_fini(&src_rect); |
Daniel Stone | d6e2a76 | 2016-11-16 19:33:20 +0000 | [diff] [blame] | 1500 | |
Daniel Stone | df2726a | 2017-02-07 18:48:19 +0000 | [diff] [blame] | 1501 | /* Buffer transforms may mean that x2 is to the left of x1, and/or that |
| 1502 | * y2 is above y1. */ |
| 1503 | if (sxf2 < sxf1) { |
| 1504 | double tmp = sxf1; |
| 1505 | sxf1 = sxf2; |
| 1506 | sxf2 = tmp; |
| 1507 | } |
| 1508 | if (syf2 < syf1) { |
| 1509 | double tmp = syf1; |
| 1510 | syf1 = syf2; |
| 1511 | syf2 = tmp; |
| 1512 | } |
| 1513 | |
| 1514 | /* Shift from S23.8 wl_fixed to U16.16 KMS fixed-point encoding. */ |
| 1515 | state->src_x = wl_fixed_from_double(sxf1) << 8; |
| 1516 | state->src_y = wl_fixed_from_double(syf1) << 8; |
| 1517 | state->src_w = wl_fixed_from_double(sxf2 - sxf1) << 8; |
| 1518 | state->src_h = wl_fixed_from_double(syf2 - syf1) << 8; |
Daniel Stone | d6e2a76 | 2016-11-16 19:33:20 +0000 | [diff] [blame] | 1519 | |
| 1520 | /* Clamp our source co-ordinates to surface bounds; it's possible |
| 1521 | * for intermediate translations to give us slightly incorrect |
| 1522 | * co-ordinates if we have, for example, multiple zooming |
| 1523 | * transformations. View bounding boxes are also explicitly rounded |
| 1524 | * greedily. */ |
Daniel Stone | df2726a | 2017-02-07 18:48:19 +0000 | [diff] [blame] | 1525 | if (state->src_x < 0) |
| 1526 | state->src_x = 0; |
| 1527 | if (state->src_y < 0) |
| 1528 | state->src_y = 0; |
| 1529 | if (state->src_w > (uint32_t) ((buffer->width << 16) - state->src_x)) |
| 1530 | state->src_w = (buffer->width << 16) - state->src_x; |
| 1531 | if (state->src_h > (uint32_t) ((buffer->height << 16) - state->src_y)) |
| 1532 | state->src_h = (buffer->height << 16) - state->src_y; |
Daniel Stone | ce13747 | 2016-11-16 19:35:03 +0000 | [diff] [blame] | 1533 | |
| 1534 | return true; |
Daniel Stone | d6e2a76 | 2016-11-16 19:33:20 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
Daniel Stone | f11ec02 | 2016-11-17 17:32:42 +0000 | [diff] [blame] | 1537 | static bool |
| 1538 | drm_view_is_opaque(struct weston_view *ev) |
| 1539 | { |
| 1540 | pixman_region32_t r; |
| 1541 | bool ret = false; |
| 1542 | |
| 1543 | pixman_region32_init_rect(&r, 0, 0, |
| 1544 | ev->surface->width, |
| 1545 | ev->surface->height); |
| 1546 | pixman_region32_subtract(&r, &r, &ev->surface->opaque); |
| 1547 | |
| 1548 | if (!pixman_region32_not_empty(&r)) |
| 1549 | ret = true; |
| 1550 | |
| 1551 | pixman_region32_fini(&r); |
| 1552 | |
| 1553 | return ret; |
| 1554 | } |
| 1555 | |
| 1556 | static struct drm_fb * |
| 1557 | drm_fb_get_from_view(struct drm_output_state *state, struct weston_view *ev) |
| 1558 | { |
| 1559 | struct drm_output *output = state->output; |
| 1560 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
| 1561 | struct weston_buffer *buffer = ev->surface->buffer_ref.buffer; |
Daniel Stone | f522e22 | 2016-11-18 12:31:26 +0000 | [diff] [blame] | 1562 | bool is_opaque = drm_view_is_opaque(ev); |
Daniel Stone | f11ec02 | 2016-11-17 17:32:42 +0000 | [diff] [blame] | 1563 | struct linux_dmabuf_buffer *dmabuf; |
| 1564 | struct drm_fb *fb; |
Daniel Stone | f11ec02 | 2016-11-17 17:32:42 +0000 | [diff] [blame] | 1565 | |
Daniel Stone | f11ec02 | 2016-11-17 17:32:42 +0000 | [diff] [blame] | 1566 | if (ev->alpha != 1.0f) |
| 1567 | return NULL; |
| 1568 | |
| 1569 | if (!drm_view_transform_supported(ev, &output->base)) |
| 1570 | return NULL; |
| 1571 | |
| 1572 | if (!buffer) |
| 1573 | return NULL; |
| 1574 | |
| 1575 | if (wl_shm_buffer_get(buffer->resource)) |
| 1576 | return NULL; |
| 1577 | |
Daniel Stone | f522e22 | 2016-11-18 12:31:26 +0000 | [diff] [blame] | 1578 | /* GBM is used for dmabuf import as well as from client wl_buffer. */ |
Daniel Stone | f11ec02 | 2016-11-17 17:32:42 +0000 | [diff] [blame] | 1579 | if (!b->gbm) |
| 1580 | return NULL; |
| 1581 | |
| 1582 | dmabuf = linux_dmabuf_buffer_get(buffer->resource); |
| 1583 | if (dmabuf) { |
Daniel Stone | f522e22 | 2016-11-18 12:31:26 +0000 | [diff] [blame] | 1584 | fb = drm_fb_get_from_dmabuf(dmabuf, b, is_opaque); |
| 1585 | if (!fb) |
Daniel Stone | f11ec02 | 2016-11-17 17:32:42 +0000 | [diff] [blame] | 1586 | return NULL; |
Daniel Stone | f11ec02 | 2016-11-17 17:32:42 +0000 | [diff] [blame] | 1587 | } else { |
Daniel Stone | f522e22 | 2016-11-18 12:31:26 +0000 | [diff] [blame] | 1588 | struct gbm_bo *bo; |
| 1589 | |
Daniel Stone | f11ec02 | 2016-11-17 17:32:42 +0000 | [diff] [blame] | 1590 | bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER, |
| 1591 | buffer->resource, GBM_BO_USE_SCANOUT); |
Daniel Stone | f522e22 | 2016-11-18 12:31:26 +0000 | [diff] [blame] | 1592 | if (!bo) |
| 1593 | return NULL; |
Daniel Stone | f11ec02 | 2016-11-17 17:32:42 +0000 | [diff] [blame] | 1594 | |
Daniel Stone | f522e22 | 2016-11-18 12:31:26 +0000 | [diff] [blame] | 1595 | fb = drm_fb_get_from_bo(bo, b, is_opaque, BUFFER_CLIENT); |
| 1596 | if (!fb) { |
| 1597 | gbm_bo_destroy(bo); |
| 1598 | return NULL; |
| 1599 | } |
Daniel Stone | f11ec02 | 2016-11-17 17:32:42 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
| 1602 | drm_fb_set_buffer(fb, buffer); |
| 1603 | return fb; |
| 1604 | } |
| 1605 | |
Daniel Stone | d6e2a76 | 2016-11-16 19:33:20 +0000 | [diff] [blame] | 1606 | /** |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 1607 | * Return a plane state from a drm_output_state. |
| 1608 | */ |
| 1609 | static struct drm_plane_state * |
| 1610 | drm_output_state_get_existing_plane(struct drm_output_state *state_output, |
| 1611 | struct drm_plane *plane) |
| 1612 | { |
| 1613 | struct drm_plane_state *ps; |
| 1614 | |
| 1615 | wl_list_for_each(ps, &state_output->plane_list, link) { |
| 1616 | if (ps->plane == plane) |
| 1617 | return ps; |
| 1618 | } |
| 1619 | |
| 1620 | return NULL; |
| 1621 | } |
| 1622 | |
| 1623 | /** |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1624 | * Return a plane state from a drm_output_state, either existing or |
| 1625 | * freshly allocated. |
| 1626 | */ |
| 1627 | static struct drm_plane_state * |
| 1628 | drm_output_state_get_plane(struct drm_output_state *state_output, |
| 1629 | struct drm_plane *plane) |
| 1630 | { |
| 1631 | struct drm_plane_state *ps; |
| 1632 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 1633 | ps = drm_output_state_get_existing_plane(state_output, plane); |
| 1634 | if (ps) |
| 1635 | return ps; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1636 | |
| 1637 | return drm_plane_state_alloc(state_output, plane); |
| 1638 | } |
| 1639 | |
| 1640 | /** |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1641 | * Allocate a new, empty drm_output_state. This should not generally be used |
| 1642 | * in the repaint cycle; see drm_output_state_duplicate. |
| 1643 | */ |
| 1644 | static struct drm_output_state * |
| 1645 | drm_output_state_alloc(struct drm_output *output, |
| 1646 | struct drm_pending_state *pending_state) |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 1647 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1648 | struct drm_output_state *state = zalloc(sizeof(*state)); |
| 1649 | |
| 1650 | assert(state); |
| 1651 | state->output = output; |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1652 | state->dpms = WESTON_DPMS_OFF; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1653 | state->pending_state = pending_state; |
| 1654 | if (pending_state) |
| 1655 | wl_list_insert(&pending_state->output_list, &state->link); |
| 1656 | else |
| 1657 | wl_list_init(&state->link); |
| 1658 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1659 | wl_list_init(&state->plane_list); |
| 1660 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1661 | return state; |
| 1662 | } |
| 1663 | |
| 1664 | /** |
| 1665 | * Duplicate an existing drm_output_state into a new one. This is generally |
| 1666 | * used during the repaint cycle, to capture the existing state of an output |
| 1667 | * and modify it to create a new state to be used. |
| 1668 | * |
| 1669 | * The mode determines whether the output will be reset to an a blank state, |
| 1670 | * or an exact mirror of the current state. |
| 1671 | */ |
| 1672 | static struct drm_output_state * |
| 1673 | drm_output_state_duplicate(struct drm_output_state *src, |
| 1674 | struct drm_pending_state *pending_state, |
| 1675 | enum drm_output_state_duplicate_mode plane_mode) |
| 1676 | { |
| 1677 | struct drm_output_state *dst = malloc(sizeof(*dst)); |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1678 | struct drm_plane_state *ps; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1679 | |
| 1680 | assert(dst); |
| 1681 | |
| 1682 | /* Copy the whole structure, then individually modify the |
| 1683 | * pending_state, as well as the list link into our pending |
| 1684 | * state. */ |
| 1685 | *dst = *src; |
| 1686 | |
| 1687 | dst->pending_state = pending_state; |
| 1688 | if (pending_state) |
| 1689 | wl_list_insert(&pending_state->output_list, &dst->link); |
| 1690 | else |
| 1691 | wl_list_init(&dst->link); |
| 1692 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1693 | wl_list_init(&dst->plane_list); |
| 1694 | |
| 1695 | wl_list_for_each(ps, &src->plane_list, link) { |
| 1696 | /* Don't carry planes which are now disabled; these should be |
| 1697 | * free for other outputs to reuse. */ |
| 1698 | if (!ps->output) |
| 1699 | continue; |
| 1700 | |
| 1701 | if (plane_mode == DRM_OUTPUT_STATE_CLEAR_PLANES) |
| 1702 | (void) drm_plane_state_alloc(dst, ps->plane); |
| 1703 | else |
| 1704 | (void) drm_plane_state_duplicate(dst, ps); |
| 1705 | } |
| 1706 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1707 | return dst; |
| 1708 | } |
| 1709 | |
| 1710 | /** |
| 1711 | * Free an unused drm_output_state. |
| 1712 | */ |
| 1713 | static void |
| 1714 | drm_output_state_free(struct drm_output_state *state) |
| 1715 | { |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1716 | struct drm_plane_state *ps, *next; |
| 1717 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1718 | if (!state) |
| 1719 | return; |
| 1720 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1721 | wl_list_for_each_safe(ps, next, &state->plane_list, link) |
| 1722 | drm_plane_state_free(ps, false); |
| 1723 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1724 | wl_list_remove(&state->link); |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1725 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1726 | free(state); |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 1727 | } |
| 1728 | |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 1729 | /** |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1730 | * Get output state to disable output |
| 1731 | * |
| 1732 | * Returns a pointer to an output_state object which can be used to disable |
| 1733 | * an output (e.g. DPMS off). |
| 1734 | * |
| 1735 | * @param pending_state The pending state object owning this update |
| 1736 | * @param output The output to disable |
| 1737 | * @returns A drm_output_state to disable the output |
| 1738 | */ |
| 1739 | static struct drm_output_state * |
| 1740 | drm_output_get_disable_state(struct drm_pending_state *pending_state, |
| 1741 | struct drm_output *output) |
| 1742 | { |
| 1743 | struct drm_output_state *output_state; |
| 1744 | |
| 1745 | output_state = drm_output_state_duplicate(output->state_cur, |
| 1746 | pending_state, |
| 1747 | DRM_OUTPUT_STATE_CLEAR_PLANES); |
| 1748 | output_state->dpms = WESTON_DPMS_OFF; |
| 1749 | |
| 1750 | return output_state; |
| 1751 | } |
| 1752 | |
| 1753 | /** |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 1754 | * Allocate a new drm_pending_state |
| 1755 | * |
| 1756 | * Allocate a new, empty, 'pending state' structure to be used across a |
| 1757 | * repaint cycle or similar. |
| 1758 | * |
| 1759 | * @param backend DRM backend |
| 1760 | * @returns Newly-allocated pending state structure |
| 1761 | */ |
| 1762 | static struct drm_pending_state * |
| 1763 | drm_pending_state_alloc(struct drm_backend *backend) |
| 1764 | { |
| 1765 | struct drm_pending_state *ret; |
| 1766 | |
| 1767 | ret = calloc(1, sizeof(*ret)); |
| 1768 | if (!ret) |
| 1769 | return NULL; |
| 1770 | |
| 1771 | ret->backend = backend; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1772 | wl_list_init(&ret->output_list); |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 1773 | |
| 1774 | return ret; |
| 1775 | } |
| 1776 | |
| 1777 | /** |
| 1778 | * Free a drm_pending_state structure |
| 1779 | * |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1780 | * Frees a pending_state structure, as well as any output_states connected |
| 1781 | * to this pending state. |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 1782 | * |
| 1783 | * @param pending_state Pending state structure to free |
| 1784 | */ |
| 1785 | static void |
| 1786 | drm_pending_state_free(struct drm_pending_state *pending_state) |
| 1787 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1788 | struct drm_output_state *output_state, *tmp; |
| 1789 | |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 1790 | if (!pending_state) |
| 1791 | return; |
| 1792 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1793 | wl_list_for_each_safe(output_state, tmp, &pending_state->output_list, |
| 1794 | link) { |
| 1795 | drm_output_state_free(output_state); |
| 1796 | } |
| 1797 | |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 1798 | free(pending_state); |
| 1799 | } |
| 1800 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1801 | /** |
| 1802 | * Find an output state in a pending state |
| 1803 | * |
| 1804 | * Given a pending_state structure, find the output_state for a particular |
| 1805 | * output. |
| 1806 | * |
| 1807 | * @param pending_state Pending state structure to search |
| 1808 | * @param output Output to find state for |
| 1809 | * @returns Output state if present, or NULL if not |
| 1810 | */ |
| 1811 | static struct drm_output_state * |
| 1812 | drm_pending_state_get_output(struct drm_pending_state *pending_state, |
| 1813 | struct drm_output *output) |
| 1814 | { |
| 1815 | struct drm_output_state *output_state; |
| 1816 | |
| 1817 | wl_list_for_each(output_state, &pending_state->output_list, link) { |
| 1818 | if (output_state->output == output) |
| 1819 | return output_state; |
| 1820 | } |
| 1821 | |
| 1822 | return NULL; |
| 1823 | } |
| 1824 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1825 | static int drm_pending_state_apply_sync(struct drm_pending_state *state); |
Daniel Stone | bb6c19f | 2016-12-08 17:27:17 +0000 | [diff] [blame] | 1826 | static int drm_pending_state_test(struct drm_pending_state *state); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1827 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1828 | /** |
| 1829 | * Mark a drm_output_state (the output's last state) as complete. This handles |
| 1830 | * any post-completion actions such as updating the repaint timer, disabling the |
| 1831 | * output, and finally freeing the state. |
| 1832 | */ |
| 1833 | static void |
| 1834 | drm_output_update_complete(struct drm_output *output, uint32_t flags, |
| 1835 | unsigned int sec, unsigned int usec) |
| 1836 | { |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1837 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1838 | struct drm_plane_state *ps; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1839 | struct timespec ts; |
| 1840 | |
| 1841 | /* Stop the pageflip timer instead of rearming it here */ |
| 1842 | if (output->pageflip_timer) |
| 1843 | wl_event_source_timer_update(output->pageflip_timer, 0); |
| 1844 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1845 | wl_list_for_each(ps, &output->state_cur->plane_list, link) |
| 1846 | ps->complete = true; |
| 1847 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1848 | drm_output_state_free(output->state_last); |
| 1849 | output->state_last = NULL; |
| 1850 | |
| 1851 | if (output->destroy_pending) { |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1852 | output->destroy_pending = 0; |
| 1853 | output->disable_pending = 0; |
| 1854 | output->dpms_off_pending = 0; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1855 | drm_output_destroy(&output->base); |
| 1856 | return; |
| 1857 | } else if (output->disable_pending) { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1858 | output->disable_pending = 0; |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1859 | output->dpms_off_pending = 0; |
| 1860 | weston_output_disable(&output->base); |
| 1861 | return; |
| 1862 | } else if (output->dpms_off_pending) { |
| 1863 | struct drm_pending_state *pending = drm_pending_state_alloc(b); |
| 1864 | output->dpms_off_pending = 0; |
| 1865 | drm_output_get_disable_state(pending, output); |
| 1866 | drm_pending_state_apply_sync(pending); |
| 1867 | return; |
| 1868 | } else if (output->state_cur->dpms == WESTON_DPMS_OFF && |
| 1869 | output->base.repaint_status != REPAINT_AWAITING_COMPLETION) { |
| 1870 | /* DPMS can happen to us either in the middle of a repaint |
| 1871 | * cycle (when we have painted fresh content, only to throw it |
| 1872 | * away for DPMS off), or at any other random point. If the |
| 1873 | * latter is true, then we cannot go through finish_frame, |
| 1874 | * because the repaint machinery does not expect this. */ |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1875 | return; |
| 1876 | } |
| 1877 | |
| 1878 | ts.tv_sec = sec; |
| 1879 | ts.tv_nsec = usec * 1000; |
| 1880 | weston_output_finish_frame(&output->base, &ts, flags); |
| 1881 | |
| 1882 | /* We can't call this from frame_notify, because the output's |
| 1883 | * repaint needed flag is cleared just after that */ |
| 1884 | if (output->recorder) |
| 1885 | weston_output_schedule_repaint(&output->base); |
| 1886 | } |
| 1887 | |
| 1888 | /** |
| 1889 | * Mark an output state as current on the output, i.e. it has been |
| 1890 | * submitted to the kernel. The mode argument determines whether this |
| 1891 | * update will be applied synchronously (e.g. when calling drmModeSetCrtc), |
| 1892 | * or asynchronously (in which case we wait for events to complete). |
| 1893 | */ |
| 1894 | static void |
| 1895 | drm_output_assign_state(struct drm_output_state *state, |
| 1896 | enum drm_state_apply_mode mode) |
| 1897 | { |
| 1898 | struct drm_output *output = state->output; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 1899 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1900 | struct drm_plane_state *plane_state; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1901 | |
| 1902 | assert(!output->state_last); |
| 1903 | |
| 1904 | if (mode == DRM_STATE_APPLY_ASYNC) |
| 1905 | output->state_last = output->state_cur; |
| 1906 | else |
| 1907 | drm_output_state_free(output->state_cur); |
| 1908 | |
| 1909 | wl_list_remove(&state->link); |
| 1910 | wl_list_init(&state->link); |
| 1911 | state->pending_state = NULL; |
| 1912 | |
| 1913 | output->state_cur = state; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1914 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 1915 | if (b->atomic_modeset && mode == DRM_STATE_APPLY_ASYNC) |
| 1916 | output->atomic_complete_pending = 1; |
| 1917 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1918 | /* Replace state_cur on each affected plane with the new state, being |
| 1919 | * careful to dispose of orphaned (but only orphaned) previous state. |
| 1920 | * If the previous state is not orphaned (still has an output_state |
| 1921 | * attached), it will be disposed of by freeing the output_state. */ |
| 1922 | wl_list_for_each(plane_state, &state->plane_list, link) { |
| 1923 | struct drm_plane *plane = plane_state->plane; |
| 1924 | |
| 1925 | if (plane->state_cur && !plane->state_cur->output_state) |
| 1926 | drm_plane_state_free(plane->state_cur, true); |
| 1927 | plane->state_cur = plane_state; |
| 1928 | |
| 1929 | if (mode != DRM_STATE_APPLY_ASYNC) { |
| 1930 | plane_state->complete = true; |
| 1931 | continue; |
| 1932 | } |
| 1933 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 1934 | if (b->atomic_modeset) |
| 1935 | continue; |
| 1936 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1937 | if (plane->type == WDRM_PLANE_TYPE_OVERLAY) |
| 1938 | output->vblank_pending++; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1939 | else if (plane->type == WDRM_PLANE_TYPE_PRIMARY) |
| 1940 | output->page_flip_pending = 1; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1941 | } |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1942 | } |
| 1943 | |
Daniel Stone | f7a2f83 | 2016-12-08 17:19:09 +0000 | [diff] [blame] | 1944 | enum drm_output_propose_state_mode { |
| 1945 | DRM_OUTPUT_PROPOSE_STATE_MIXED, /**< mix renderer & planes */ |
| 1946 | DRM_OUTPUT_PROPOSE_STATE_RENDERER_ONLY, /**< only assign to renderer & cursor */ |
Daniel Stone | d12e516 | 2018-07-10 18:19:37 +0100 | [diff] [blame] | 1947 | DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY, /**< no renderer use, only planes */ |
Daniel Stone | f7a2f83 | 2016-12-08 17:19:09 +0000 | [diff] [blame] | 1948 | }; |
| 1949 | |
Daniel Stone | f829062 | 2016-12-09 17:32:10 +0000 | [diff] [blame] | 1950 | static struct drm_plane_state * |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1951 | drm_output_prepare_scanout_view(struct drm_output_state *output_state, |
Daniel Stone | a284d27 | 2018-07-10 18:40:12 +0100 | [diff] [blame^] | 1952 | struct weston_view *ev, |
| 1953 | enum drm_output_propose_state_mode mode) |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 1954 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1955 | struct drm_output *output = output_state->output; |
Daniel Stone | 44abfaa | 2018-07-10 14:31:06 +0100 | [diff] [blame] | 1956 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1957 | struct drm_plane *scanout_plane = output->scanout_plane; |
| 1958 | struct drm_plane_state *state; |
Daniel Stone | bdf3e7e | 2016-11-17 17:33:08 +0000 | [diff] [blame] | 1959 | struct drm_fb *fb; |
Daniel Stone | 7cdf231 | 2016-11-16 19:40:29 +0000 | [diff] [blame] | 1960 | pixman_box32_t *extents; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 1961 | |
Daniel Stone | 44abfaa | 2018-07-10 14:31:06 +0100 | [diff] [blame] | 1962 | assert(!b->sprites_are_broken); |
Daniel Stone | a284d27 | 2018-07-10 18:40:12 +0100 | [diff] [blame^] | 1963 | assert(mode == DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY); |
Daniel Stone | 44abfaa | 2018-07-10 14:31:06 +0100 | [diff] [blame] | 1964 | |
Daniel Stone | 7cdf231 | 2016-11-16 19:40:29 +0000 | [diff] [blame] | 1965 | /* Check the view spans exactly the output size, calculated in the |
| 1966 | * logical co-ordinate space. */ |
| 1967 | extents = pixman_region32_extents(&ev->transform.boundingbox); |
| 1968 | if (extents->x1 != output->base.x || |
| 1969 | extents->y1 != output->base.y || |
| 1970 | extents->x2 != output->base.x + output->base.width || |
| 1971 | extents->y2 != output->base.y + output->base.height) |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 1972 | return NULL; |
| 1973 | |
| 1974 | if (ev->alpha != 1.0f) |
| 1975 | return NULL; |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1976 | |
Daniel Stone | bdf3e7e | 2016-11-17 17:33:08 +0000 | [diff] [blame] | 1977 | fb = drm_fb_get_from_view(output_state, ev); |
| 1978 | if (!fb) |
| 1979 | return NULL; |
| 1980 | |
| 1981 | /* Can't change formats with just a pageflip */ |
| 1982 | if (fb->format->format != output->gbm_format) { |
| 1983 | drm_fb_unref(fb); |
| 1984 | return NULL; |
| 1985 | } |
| 1986 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1987 | state = drm_output_state_get_plane(output_state, scanout_plane); |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1988 | |
Daniel Stone | a284d27 | 2018-07-10 18:40:12 +0100 | [diff] [blame^] | 1989 | /* The only way we can already have a buffer in the scanout plane is |
| 1990 | * if we are in mixed mode, or if a client buffer has already been |
| 1991 | * placed into scanout. The former case will never call into here, |
| 1992 | * and in the latter case, the view must have been marked as occluded, |
| 1993 | * meaning we should never have ended up here. */ |
| 1994 | assert(!state->fb); |
Daniel Stone | bdf3e7e | 2016-11-17 17:33:08 +0000 | [diff] [blame] | 1995 | state->fb = fb; |
Daniel Stone | ee1aea7 | 2017-12-18 13:41:09 +0000 | [diff] [blame] | 1996 | state->ev = ev; |
Daniel Stone | 7cdf231 | 2016-11-16 19:40:29 +0000 | [diff] [blame] | 1997 | state->output = output; |
| 1998 | if (!drm_plane_state_coords_for_view(state, ev)) |
| 1999 | goto err; |
| 2000 | |
| 2001 | /* The legacy API does not let us perform cropping or scaling. */ |
| 2002 | if (state->src_x != 0 || state->src_y != 0 || |
| 2003 | state->src_w != state->dest_w << 16 || |
| 2004 | state->src_h != state->dest_h << 16 || |
| 2005 | state->dest_x != 0 || state->dest_y != 0 || |
| 2006 | state->dest_w != (unsigned) output->base.current_mode->width || |
| 2007 | state->dest_h != (unsigned) output->base.current_mode->height) |
| 2008 | goto err; |
| 2009 | |
Daniel Stone | a284d27 | 2018-07-10 18:40:12 +0100 | [diff] [blame^] | 2010 | /* In plane-only mode, we don't need to test the state now, as we |
| 2011 | * will only test it once at the end. */ |
Daniel Stone | f829062 | 2016-12-09 17:32:10 +0000 | [diff] [blame] | 2012 | return state; |
Daniel Stone | 7cdf231 | 2016-11-16 19:40:29 +0000 | [diff] [blame] | 2013 | |
| 2014 | err: |
| 2015 | drm_plane_state_put_back(state); |
| 2016 | return NULL; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 2017 | } |
| 2018 | |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 2019 | static struct drm_fb * |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2020 | 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] | 2021 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2022 | struct drm_output *output = state->output; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2023 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 2024 | struct gbm_bo *bo; |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 2025 | struct drm_fb *ret; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 2026 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2027 | output->base.compositor->renderer->repaint_output(&output->base, |
| 2028 | damage); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 2029 | |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 2030 | bo = gbm_surface_lock_front_buffer(output->gbm_surface); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 2031 | if (!bo) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2032 | weston_log("failed to lock front buffer: %m\n"); |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 2033 | return NULL; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 2034 | } |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 2035 | |
Daniel Stone | db10df1 | 2016-12-08 13:15:58 +0000 | [diff] [blame] | 2036 | /* The renderer always produces an opaque image. */ |
| 2037 | ret = drm_fb_get_from_bo(bo, b, true, BUFFER_GBM_SURFACE); |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 2038 | if (!ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2039 | weston_log("failed to get drm_fb for bo\n"); |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 2040 | gbm_surface_release_buffer(output->gbm_surface, bo); |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 2041 | return NULL; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 2042 | } |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 2043 | ret->gbm_surface = output->gbm_surface; |
| 2044 | |
| 2045 | return ret; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 2046 | } |
| 2047 | |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 2048 | static struct drm_fb * |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2049 | drm_output_render_pixman(struct drm_output_state *state, |
| 2050 | pixman_region32_t *damage) |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2051 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2052 | struct drm_output *output = state->output; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2053 | struct weston_compositor *ec = output->base.compositor; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2054 | |
| 2055 | output->current_image ^= 1; |
| 2056 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2057 | pixman_renderer_output_set_buffer(&output->base, |
| 2058 | output->image[output->current_image]); |
Pekka Paalanen | acf50c3 | 2018-04-23 11:44:56 +0200 | [diff] [blame] | 2059 | pixman_renderer_output_set_hw_extra_damage(&output->base, |
| 2060 | &output->previous_damage); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2061 | |
Pekka Paalanen | acf50c3 | 2018-04-23 11:44:56 +0200 | [diff] [blame] | 2062 | ec->renderer->repaint_output(&output->base, damage); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2063 | |
Pekka Paalanen | acf50c3 | 2018-04-23 11:44:56 +0200 | [diff] [blame] | 2064 | pixman_region32_copy(&output->previous_damage, damage); |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 2065 | |
| 2066 | return drm_fb_ref(output->dumb[output->current_image]); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2067 | } |
| 2068 | |
| 2069 | static void |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2070 | 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] | 2071 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2072 | struct drm_output *output = state->output; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2073 | struct weston_compositor *c = output->base.compositor; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 2074 | struct drm_plane_state *scanout_state; |
Daniel Stone | e95169b | 2016-11-14 17:46:59 +0000 | [diff] [blame] | 2075 | struct drm_plane *scanout_plane = output->scanout_plane; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2076 | struct drm_backend *b = to_drm_backend(c); |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 2077 | struct drm_fb *fb; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2078 | |
Daniel Stone | 4e84f7d | 2017-04-04 17:54:29 +0100 | [diff] [blame] | 2079 | /* If we already have a client buffer promoted to scanout, then we don't |
| 2080 | * want to render. */ |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 2081 | scanout_state = drm_output_state_get_plane(state, |
| 2082 | output->scanout_plane); |
| 2083 | if (scanout_state->fb) |
Daniel Stone | 4e84f7d | 2017-04-04 17:54:29 +0100 | [diff] [blame] | 2084 | return; |
| 2085 | |
Daniel Stone | e95169b | 2016-11-14 17:46:59 +0000 | [diff] [blame] | 2086 | if (!pixman_region32_not_empty(damage) && |
| 2087 | scanout_plane->state_cur->fb && |
| 2088 | (scanout_plane->state_cur->fb->type == BUFFER_GBM_SURFACE || |
| 2089 | scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB) && |
| 2090 | scanout_plane->state_cur->fb->width == |
| 2091 | output->base.current_mode->width && |
| 2092 | scanout_plane->state_cur->fb->height == |
| 2093 | output->base.current_mode->height) { |
| 2094 | fb = drm_fb_ref(scanout_plane->state_cur->fb); |
| 2095 | } else if (b->use_pixman) { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2096 | fb = drm_output_render_pixman(state, damage); |
Daniel Stone | e95169b | 2016-11-14 17:46:59 +0000 | [diff] [blame] | 2097 | } else { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2098 | fb = drm_output_render_gl(state, damage); |
Daniel Stone | e95169b | 2016-11-14 17:46:59 +0000 | [diff] [blame] | 2099 | } |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 2100 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 2101 | if (!fb) { |
| 2102 | drm_plane_state_put_back(scanout_state); |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 2103 | return; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 2104 | } |
| 2105 | |
| 2106 | scanout_state->fb = fb; |
| 2107 | scanout_state->output = output; |
| 2108 | |
| 2109 | scanout_state->src_x = 0; |
| 2110 | scanout_state->src_y = 0; |
| 2111 | scanout_state->src_w = output->base.current_mode->width << 16; |
| 2112 | scanout_state->src_h = output->base.current_mode->height << 16; |
| 2113 | |
| 2114 | scanout_state->dest_x = 0; |
| 2115 | scanout_state->dest_y = 0; |
| 2116 | scanout_state->dest_w = scanout_state->src_w >> 16; |
| 2117 | scanout_state->dest_h = scanout_state->src_h >> 16; |
| 2118 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2119 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2120 | pixman_region32_subtract(&c->primary_plane.damage, |
| 2121 | &c->primary_plane.damage, damage); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2122 | } |
| 2123 | |
| 2124 | static void |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 2125 | drm_output_set_gamma(struct weston_output *output_base, |
| 2126 | uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b) |
| 2127 | { |
| 2128 | int rc; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2129 | struct drm_output *output = to_drm_output(output_base); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2130 | struct drm_backend *backend = |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2131 | to_drm_backend(output->base.compositor); |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 2132 | |
| 2133 | /* check */ |
| 2134 | if (output_base->gamma_size != size) |
| 2135 | return; |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 2136 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2137 | rc = drmModeCrtcSetGamma(backend->drm.fd, |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 2138 | output->crtc_id, |
| 2139 | size, r, g, b); |
| 2140 | if (rc) |
| 2141 | weston_log("set gamma failed: %m\n"); |
| 2142 | } |
| 2143 | |
Bryce Harrington | ada4f07 | 2015-06-30 13:25:46 -0700 | [diff] [blame] | 2144 | /* Determine the type of vblank synchronization to use for the output. |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 2145 | * |
Bryce Harrington | ada4f07 | 2015-06-30 13:25:46 -0700 | [diff] [blame] | 2146 | * The pipe parameter indicates which CRTC is in use. Knowing this, we |
| 2147 | * can determine which vblank sequence type to use for it. Traditional |
| 2148 | * cards had only two CRTCs, with CRTC 0 using no special flags, and |
| 2149 | * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe |
| 2150 | * parameter indicates this. |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 2151 | * |
Bryce Harrington | ada4f07 | 2015-06-30 13:25:46 -0700 | [diff] [blame] | 2152 | * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between |
| 2153 | * 0-31. If this is non-zero it indicates we're dealing with a |
| 2154 | * multi-gpu situation and we need to calculate the vblank sync |
| 2155 | * using DRM_BLANK_HIGH_CRTC_MASK. |
| 2156 | */ |
Pekka Paalanen | c8a1ff0 | 2015-07-02 15:06:08 +0300 | [diff] [blame] | 2157 | static unsigned int |
| 2158 | drm_waitvblank_pipe(struct drm_output *output) |
Mario Kleiner | 2ab4f4e | 2015-06-21 21:25:13 +0200 | [diff] [blame] | 2159 | { |
| 2160 | if (output->pipe > 1) |
| 2161 | return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) & |
| 2162 | DRM_VBLANK_HIGH_CRTC_MASK; |
| 2163 | else if (output->pipe > 0) |
| 2164 | return DRM_VBLANK_SECONDARY; |
| 2165 | else |
| 2166 | return 0; |
| 2167 | } |
| 2168 | |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 2169 | static int |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2170 | drm_output_apply_state_legacy(struct drm_output_state *state) |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 2171 | { |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2172 | struct drm_output *output = state->output; |
| 2173 | struct drm_backend *backend = to_drm_backend(output->base.compositor); |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 2174 | struct drm_plane *scanout_plane = output->scanout_plane; |
Pekka Paalanen | 02aeb5c | 2017-09-12 16:02:01 +0300 | [diff] [blame] | 2175 | struct drm_property_info *dpms_prop; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 2176 | struct drm_plane_state *scanout_state; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2177 | struct drm_plane_state *ps; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 2178 | struct drm_mode *mode; |
Pekka Paalanen | 02aeb5c | 2017-09-12 16:02:01 +0300 | [diff] [blame] | 2179 | struct drm_head *head; |
| 2180 | uint32_t connectors[MAX_CLONED_CONNECTORS]; |
| 2181 | int n_conn = 0; |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2182 | struct timespec now; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2183 | int ret = 0; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 2184 | |
Pekka Paalanen | 02aeb5c | 2017-09-12 16:02:01 +0300 | [diff] [blame] | 2185 | wl_list_for_each(head, &output->base.head_list, base.output_link) { |
| 2186 | assert(n_conn < MAX_CLONED_CONNECTORS); |
| 2187 | connectors[n_conn++] = head->connector_id; |
| 2188 | } |
| 2189 | |
Derek Foreman | 2cd87fe | 2017-04-13 13:48:48 -0500 | [diff] [blame] | 2190 | /* If disable_planes is set then assign_planes() wasn't |
| 2191 | * called for this render, so we could still have a stale |
| 2192 | * cursor plane set up. |
| 2193 | */ |
| 2194 | if (output->base.disable_planes) { |
| 2195 | output->cursor_view = NULL; |
Greg V | 1f78176 | 2018-02-19 17:59:42 +0300 | [diff] [blame] | 2196 | if (output->cursor_plane) { |
| 2197 | output->cursor_plane->base.x = INT32_MIN; |
| 2198 | output->cursor_plane->base.y = INT32_MIN; |
| 2199 | } |
Derek Foreman | 2cd87fe | 2017-04-13 13:48:48 -0500 | [diff] [blame] | 2200 | } |
| 2201 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2202 | if (state->dpms != WESTON_DPMS_ON) { |
| 2203 | wl_list_for_each(ps, &state->plane_list, link) { |
Daniel Stone | f829062 | 2016-12-09 17:32:10 +0000 | [diff] [blame] | 2204 | struct drm_plane *p = ps->plane; |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2205 | assert(ps->fb == NULL); |
| 2206 | assert(ps->output == NULL); |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 2207 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2208 | if (p->type != WDRM_PLANE_TYPE_OVERLAY) |
| 2209 | continue; |
| 2210 | |
| 2211 | ret = drmModeSetPlane(backend->drm.fd, p->plane_id, |
| 2212 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
| 2213 | if (ret) |
| 2214 | weston_log("drmModeSetPlane failed disable: %m\n"); |
| 2215 | } |
| 2216 | |
| 2217 | if (output->cursor_plane) { |
| 2218 | ret = drmModeSetCursor(backend->drm.fd, output->crtc_id, |
| 2219 | 0, 0, 0); |
| 2220 | if (ret) |
| 2221 | weston_log("drmModeSetCursor failed disable: %m\n"); |
| 2222 | } |
| 2223 | |
| 2224 | ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id, 0, 0, 0, |
Pekka Paalanen | 02aeb5c | 2017-09-12 16:02:01 +0300 | [diff] [blame] | 2225 | NULL, 0, NULL); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2226 | if (ret) |
| 2227 | weston_log("drmModeSetCrtc failed disabling: %m\n"); |
| 2228 | |
| 2229 | drm_output_assign_state(state, DRM_STATE_APPLY_SYNC); |
| 2230 | weston_compositor_read_presentation_clock(output->base.compositor, &now); |
| 2231 | drm_output_update_complete(output, |
| 2232 | WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION, |
| 2233 | now.tv_sec, now.tv_nsec / 1000); |
| 2234 | |
| 2235 | return 0; |
| 2236 | } |
| 2237 | |
| 2238 | scanout_state = |
| 2239 | drm_output_state_get_existing_plane(state, scanout_plane); |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 2240 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 2241 | /* The legacy SetCrtc API doesn't allow us to do scaling, and the |
| 2242 | * legacy PageFlip API doesn't allow us to do clipping either. */ |
| 2243 | assert(scanout_state->src_x == 0); |
| 2244 | assert(scanout_state->src_y == 0); |
| 2245 | assert(scanout_state->src_w == |
| 2246 | (unsigned) (output->base.current_mode->width << 16)); |
| 2247 | assert(scanout_state->src_h == |
| 2248 | (unsigned) (output->base.current_mode->height << 16)); |
| 2249 | assert(scanout_state->dest_x == 0); |
| 2250 | assert(scanout_state->dest_y == 0); |
| 2251 | assert(scanout_state->dest_w == scanout_state->src_w >> 16); |
| 2252 | assert(scanout_state->dest_h == scanout_state->src_h >> 16); |
| 2253 | |
Daniel Stone | cb04cc4 | 2016-11-16 11:51:27 +0000 | [diff] [blame] | 2254 | mode = to_drm_mode(output->base.current_mode); |
Daniel Stone | 8eece0c | 2016-11-17 17:54:00 +0000 | [diff] [blame] | 2255 | if (backend->state_invalid || |
| 2256 | !scanout_plane->state_cur->fb || |
| 2257 | scanout_plane->state_cur->fb->strides[0] != |
| 2258 | scanout_state->fb->strides[0]) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2259 | ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id, |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 2260 | scanout_state->fb->fb_id, |
| 2261 | 0, 0, |
Pekka Paalanen | 02aeb5c | 2017-09-12 16:02:01 +0300 | [diff] [blame] | 2262 | connectors, n_conn, |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 2263 | &mode->mode_info); |
| 2264 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2265 | weston_log("set mode failed: %m\n"); |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2266 | goto err; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 2267 | } |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 2268 | } |
| 2269 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2270 | if (drmModePageFlip(backend->drm.fd, output->crtc_id, |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 2271 | scanout_state->fb->fb_id, |
Kristian Høgsberg | 54f14c3 | 2012-01-18 11:47:41 -0500 | [diff] [blame] | 2272 | DRM_MODE_PAGE_FLIP_EVENT, output) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2273 | weston_log("queueing pageflip failed: %m\n"); |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2274 | goto err; |
Kristian Høgsberg | 54f14c3 | 2012-01-18 11:47:41 -0500 | [diff] [blame] | 2275 | } |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 2276 | |
Daniel Stone | 205c0a0 | 2017-04-04 17:54:33 +0100 | [diff] [blame] | 2277 | assert(!output->page_flip_pending); |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 2278 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 2279 | if (output->pageflip_timer) |
| 2280 | wl_event_source_timer_update(output->pageflip_timer, |
| 2281 | backend->pageflip_timeout); |
| 2282 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 2283 | drm_output_set_cursor(state); |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 2284 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2285 | /* |
| 2286 | * Now, update all the sprite surfaces |
| 2287 | */ |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2288 | wl_list_for_each(ps, &state->plane_list, link) { |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 2289 | uint32_t flags = 0, fb_id = 0; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2290 | drmVBlank vbl = { |
| 2291 | .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT, |
| 2292 | .request.sequence = 1, |
| 2293 | }; |
Daniel Stone | f829062 | 2016-12-09 17:32:10 +0000 | [diff] [blame] | 2294 | struct drm_plane *p = ps->plane; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2295 | |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 2296 | if (p->type != WDRM_PLANE_TYPE_OVERLAY) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2297 | continue; |
| 2298 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2299 | assert(p->state_cur->complete); |
| 2300 | assert(!!p->state_cur->output == !!p->state_cur->fb); |
| 2301 | assert(!p->state_cur->output || p->state_cur->output == output); |
| 2302 | assert(!ps->complete); |
| 2303 | assert(!ps->output || ps->output == output); |
| 2304 | assert(!!ps->output == !!ps->fb); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 2305 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2306 | if (ps->fb && !backend->sprites_hidden) |
| 2307 | fb_id = ps->fb->fb_id; |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 2308 | |
| 2309 | ret = drmModeSetPlane(backend->drm.fd, p->plane_id, |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 2310 | output->crtc_id, fb_id, flags, |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2311 | ps->dest_x, ps->dest_y, |
| 2312 | ps->dest_w, ps->dest_h, |
| 2313 | ps->src_x, ps->src_y, |
| 2314 | ps->src_w, ps->src_h); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2315 | if (ret) |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2316 | weston_log("setplane failed: %d: %s\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2317 | ret, strerror(errno)); |
| 2318 | |
Mario Kleiner | 2ab4f4e | 2015-06-21 21:25:13 +0200 | [diff] [blame] | 2319 | vbl.request.type |= drm_waitvblank_pipe(output); |
Rob Clark | 5ca1a47 | 2012-08-08 20:27:37 -0500 | [diff] [blame] | 2320 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2321 | /* |
| 2322 | * Queue a vblank signal so we know when the surface |
| 2323 | * becomes active on the display or has been replaced. |
| 2324 | */ |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2325 | vbl.request.signal = (unsigned long) ps; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2326 | ret = drmWaitVBlank(backend->drm.fd, &vbl); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2327 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2328 | weston_log("vblank event request failed: %d: %s\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2329 | ret, strerror(errno)); |
| 2330 | } |
| 2331 | } |
| 2332 | |
Pekka Paalanen | 02aeb5c | 2017-09-12 16:02:01 +0300 | [diff] [blame] | 2333 | if (state->dpms != output->state_cur->dpms) { |
| 2334 | wl_list_for_each(head, &output->base.head_list, base.output_link) { |
| 2335 | dpms_prop = &head->props_conn[WDRM_CONNECTOR_DPMS]; |
| 2336 | if (dpms_prop->prop_id == 0) |
| 2337 | continue; |
| 2338 | |
| 2339 | ret = drmModeConnectorSetProperty(backend->drm.fd, |
| 2340 | head->connector_id, |
| 2341 | dpms_prop->prop_id, |
| 2342 | state->dpms); |
| 2343 | if (ret) { |
| 2344 | weston_log("DRM: DPMS: failed property set for %s\n", |
| 2345 | head->base.name); |
| 2346 | } |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2347 | } |
| 2348 | } |
| 2349 | |
| 2350 | drm_output_assign_state(state, DRM_STATE_APPLY_ASYNC); |
| 2351 | |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 2352 | return 0; |
| 2353 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2354 | err: |
Kristian Høgsberg | b3955b0 | 2014-01-23 16:25:06 -0800 | [diff] [blame] | 2355 | output->cursor_view = NULL; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2356 | drm_output_state_free(state); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2357 | return -1; |
| 2358 | } |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 2359 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2360 | #ifdef HAVE_DRM_ATOMIC |
| 2361 | static int |
| 2362 | crtc_add_prop(drmModeAtomicReq *req, struct drm_output *output, |
| 2363 | enum wdrm_crtc_property prop, uint64_t val) |
| 2364 | { |
| 2365 | struct drm_property_info *info = &output->props_crtc[prop]; |
| 2366 | int ret; |
| 2367 | |
| 2368 | if (info->prop_id == 0) |
| 2369 | return -1; |
| 2370 | |
| 2371 | ret = drmModeAtomicAddProperty(req, output->crtc_id, info->prop_id, |
| 2372 | val); |
| 2373 | return (ret <= 0) ? -1 : 0; |
| 2374 | } |
| 2375 | |
| 2376 | static int |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 2377 | connector_add_prop(drmModeAtomicReq *req, struct drm_head *head, |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2378 | enum wdrm_connector_property prop, uint64_t val) |
| 2379 | { |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 2380 | struct drm_property_info *info = &head->props_conn[prop]; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2381 | int ret; |
| 2382 | |
| 2383 | if (info->prop_id == 0) |
| 2384 | return -1; |
| 2385 | |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 2386 | ret = drmModeAtomicAddProperty(req, head->connector_id, |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2387 | info->prop_id, val); |
| 2388 | return (ret <= 0) ? -1 : 0; |
| 2389 | } |
| 2390 | |
| 2391 | static int |
| 2392 | plane_add_prop(drmModeAtomicReq *req, struct drm_plane *plane, |
| 2393 | enum wdrm_plane_property prop, uint64_t val) |
| 2394 | { |
| 2395 | struct drm_property_info *info = &plane->props[prop]; |
| 2396 | int ret; |
| 2397 | |
| 2398 | if (info->prop_id == 0) |
| 2399 | return -1; |
| 2400 | |
| 2401 | ret = drmModeAtomicAddProperty(req, plane->plane_id, info->prop_id, |
| 2402 | val); |
| 2403 | return (ret <= 0) ? -1 : 0; |
| 2404 | } |
| 2405 | |
| 2406 | static int |
| 2407 | drm_mode_ensure_blob(struct drm_backend *backend, struct drm_mode *mode) |
| 2408 | { |
| 2409 | int ret; |
| 2410 | |
| 2411 | if (mode->blob_id) |
| 2412 | return 0; |
| 2413 | |
| 2414 | ret = drmModeCreatePropertyBlob(backend->drm.fd, |
| 2415 | &mode->mode_info, |
| 2416 | sizeof(mode->mode_info), |
| 2417 | &mode->blob_id); |
| 2418 | if (ret != 0) |
| 2419 | weston_log("failed to create mode property blob: %m\n"); |
| 2420 | |
| 2421 | return ret; |
| 2422 | } |
| 2423 | |
| 2424 | static int |
| 2425 | drm_output_apply_state_atomic(struct drm_output_state *state, |
| 2426 | drmModeAtomicReq *req, |
| 2427 | uint32_t *flags) |
| 2428 | { |
| 2429 | struct drm_output *output = state->output; |
| 2430 | struct drm_backend *backend = to_drm_backend(output->base.compositor); |
| 2431 | struct drm_plane_state *plane_state; |
| 2432 | struct drm_mode *current_mode = to_drm_mode(output->base.current_mode); |
Pekka Paalanen | 2f66130 | 2017-09-12 16:07:32 +0300 | [diff] [blame] | 2433 | struct drm_head *head; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2434 | int ret = 0; |
| 2435 | |
| 2436 | if (state->dpms != output->state_cur->dpms) |
| 2437 | *flags |= DRM_MODE_ATOMIC_ALLOW_MODESET; |
| 2438 | |
| 2439 | if (state->dpms == WESTON_DPMS_ON) { |
| 2440 | ret = drm_mode_ensure_blob(backend, current_mode); |
| 2441 | if (ret != 0) |
| 2442 | return ret; |
| 2443 | |
| 2444 | ret |= crtc_add_prop(req, output, WDRM_CRTC_MODE_ID, |
| 2445 | current_mode->blob_id); |
| 2446 | ret |= crtc_add_prop(req, output, WDRM_CRTC_ACTIVE, 1); |
Pekka Paalanen | 2f66130 | 2017-09-12 16:07:32 +0300 | [diff] [blame] | 2447 | |
Daniel Stone | 7625577 | 2018-07-06 11:36:49 +0100 | [diff] [blame] | 2448 | /* No need for the DPMS property, since it is implicit in |
| 2449 | * routing and CRTC activity. */ |
Pekka Paalanen | 2f66130 | 2017-09-12 16:07:32 +0300 | [diff] [blame] | 2450 | wl_list_for_each(head, &output->base.head_list, base.output_link) { |
| 2451 | ret |= connector_add_prop(req, head, WDRM_CONNECTOR_CRTC_ID, |
| 2452 | output->crtc_id); |
| 2453 | } |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2454 | } else { |
| 2455 | ret |= crtc_add_prop(req, output, WDRM_CRTC_MODE_ID, 0); |
| 2456 | ret |= crtc_add_prop(req, output, WDRM_CRTC_ACTIVE, 0); |
Pekka Paalanen | 2f66130 | 2017-09-12 16:07:32 +0300 | [diff] [blame] | 2457 | |
Daniel Stone | 7625577 | 2018-07-06 11:36:49 +0100 | [diff] [blame] | 2458 | /* No need for the DPMS property, since it is implicit in |
| 2459 | * routing and CRTC activity. */ |
Pekka Paalanen | 2f66130 | 2017-09-12 16:07:32 +0300 | [diff] [blame] | 2460 | wl_list_for_each(head, &output->base.head_list, base.output_link) |
| 2461 | ret |= connector_add_prop(req, head, WDRM_CONNECTOR_CRTC_ID, 0); |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
| 2464 | if (ret != 0) { |
| 2465 | weston_log("couldn't set atomic CRTC/connector state\n"); |
| 2466 | return ret; |
| 2467 | } |
| 2468 | |
| 2469 | wl_list_for_each(plane_state, &state->plane_list, link) { |
| 2470 | struct drm_plane *plane = plane_state->plane; |
| 2471 | |
| 2472 | ret |= plane_add_prop(req, plane, WDRM_PLANE_FB_ID, |
| 2473 | plane_state->fb ? plane_state->fb->fb_id : 0); |
| 2474 | ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_ID, |
| 2475 | plane_state->fb ? output->crtc_id : 0); |
| 2476 | ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_X, |
| 2477 | plane_state->src_x); |
| 2478 | ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_Y, |
| 2479 | plane_state->src_y); |
| 2480 | ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_W, |
| 2481 | plane_state->src_w); |
| 2482 | ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_H, |
| 2483 | plane_state->src_h); |
| 2484 | ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_X, |
| 2485 | plane_state->dest_x); |
| 2486 | ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_Y, |
| 2487 | plane_state->dest_y); |
| 2488 | ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_W, |
| 2489 | plane_state->dest_w); |
| 2490 | ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_H, |
| 2491 | plane_state->dest_h); |
| 2492 | |
| 2493 | if (ret != 0) { |
| 2494 | weston_log("couldn't set plane state\n"); |
| 2495 | return ret; |
| 2496 | } |
| 2497 | } |
| 2498 | |
| 2499 | return 0; |
| 2500 | } |
| 2501 | |
| 2502 | /** |
| 2503 | * Helper function used only by drm_pending_state_apply, with the same |
| 2504 | * guarantees and constraints as that function. |
| 2505 | */ |
| 2506 | static int |
| 2507 | drm_pending_state_apply_atomic(struct drm_pending_state *pending_state, |
| 2508 | enum drm_state_apply_mode mode) |
| 2509 | { |
| 2510 | struct drm_backend *b = pending_state->backend; |
| 2511 | struct drm_output_state *output_state, *tmp; |
| 2512 | struct drm_plane *plane; |
| 2513 | drmModeAtomicReq *req = drmModeAtomicAlloc(); |
| 2514 | uint32_t flags = 0; |
| 2515 | int ret = 0; |
| 2516 | |
| 2517 | if (!req) |
| 2518 | return -1; |
| 2519 | |
| 2520 | if (b->state_invalid) { |
Pekka Paalanen | eacec81 | 2017-09-12 13:43:51 +0300 | [diff] [blame] | 2521 | struct weston_head *head_base; |
| 2522 | struct drm_head *head; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2523 | uint32_t *unused; |
| 2524 | int err; |
| 2525 | |
| 2526 | /* If we need to reset all our state (e.g. because we've |
| 2527 | * just started, or just been VT-switched in), explicitly |
| 2528 | * disable all the CRTCs and connectors we aren't using. */ |
Pekka Paalanen | eacec81 | 2017-09-12 13:43:51 +0300 | [diff] [blame] | 2529 | wl_list_for_each(head_base, |
| 2530 | &b->compositor->head_list, compositor_link) { |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2531 | struct drm_property_info *info; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2532 | |
Pekka Paalanen | eacec81 | 2017-09-12 13:43:51 +0300 | [diff] [blame] | 2533 | if (weston_head_is_enabled(head_base)) |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2534 | continue; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2535 | |
Pekka Paalanen | eacec81 | 2017-09-12 13:43:51 +0300 | [diff] [blame] | 2536 | head = to_drm_head(head_base); |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2537 | |
Pekka Paalanen | eacec81 | 2017-09-12 13:43:51 +0300 | [diff] [blame] | 2538 | info = &head->props_conn[WDRM_CONNECTOR_CRTC_ID]; |
| 2539 | err = drmModeAtomicAddProperty(req, head->connector_id, |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2540 | info->prop_id, 0); |
| 2541 | if (err <= 0) |
| 2542 | ret = -1; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2543 | } |
| 2544 | |
| 2545 | wl_array_for_each(unused, &b->unused_crtcs) { |
| 2546 | struct drm_property_info infos[WDRM_CRTC__COUNT]; |
| 2547 | struct drm_property_info *info; |
| 2548 | drmModeObjectProperties *props; |
| 2549 | uint64_t active; |
| 2550 | |
| 2551 | memset(infos, 0, sizeof(infos)); |
| 2552 | |
| 2553 | /* We can't emit a disable on a CRTC that's already |
| 2554 | * off, as the kernel will refuse to generate an event |
| 2555 | * for an off->off state and fail the commit. |
| 2556 | */ |
| 2557 | props = drmModeObjectGetProperties(b->drm.fd, |
| 2558 | *unused, |
| 2559 | DRM_MODE_OBJECT_CRTC); |
| 2560 | if (!props) { |
| 2561 | ret = -1; |
| 2562 | continue; |
| 2563 | } |
| 2564 | |
| 2565 | drm_property_info_populate(b, crtc_props, infos, |
| 2566 | WDRM_CRTC__COUNT, |
| 2567 | props); |
| 2568 | |
| 2569 | info = &infos[WDRM_CRTC_ACTIVE]; |
| 2570 | active = drm_property_get_value(info, props, 0); |
| 2571 | drmModeFreeObjectProperties(props); |
| 2572 | if (active == 0) { |
| 2573 | drm_property_info_free(infos, WDRM_CRTC__COUNT); |
| 2574 | continue; |
| 2575 | } |
| 2576 | |
| 2577 | err = drmModeAtomicAddProperty(req, *unused, |
| 2578 | info->prop_id, 0); |
| 2579 | if (err <= 0) |
| 2580 | ret = -1; |
| 2581 | |
| 2582 | info = &infos[WDRM_CRTC_MODE_ID]; |
| 2583 | err = drmModeAtomicAddProperty(req, *unused, |
| 2584 | info->prop_id, 0); |
| 2585 | if (err <= 0) |
| 2586 | ret = -1; |
| 2587 | |
| 2588 | drm_property_info_free(infos, WDRM_CRTC__COUNT); |
| 2589 | } |
| 2590 | |
| 2591 | /* Disable all the planes; planes which are being used will |
| 2592 | * override this state in the output-state application. */ |
| 2593 | wl_list_for_each(plane, &b->plane_list, link) { |
| 2594 | plane_add_prop(req, plane, WDRM_PLANE_CRTC_ID, 0); |
| 2595 | plane_add_prop(req, plane, WDRM_PLANE_FB_ID, 0); |
| 2596 | } |
| 2597 | |
| 2598 | flags |= DRM_MODE_ATOMIC_ALLOW_MODESET; |
| 2599 | } |
| 2600 | |
| 2601 | wl_list_for_each(output_state, &pending_state->output_list, link) { |
| 2602 | if (mode == DRM_STATE_APPLY_SYNC) |
| 2603 | assert(output_state->dpms == WESTON_DPMS_OFF); |
| 2604 | ret |= drm_output_apply_state_atomic(output_state, req, &flags); |
| 2605 | } |
| 2606 | |
| 2607 | if (ret != 0) { |
| 2608 | weston_log("atomic: couldn't compile atomic state\n"); |
| 2609 | goto out; |
| 2610 | } |
| 2611 | |
| 2612 | switch (mode) { |
| 2613 | case DRM_STATE_APPLY_SYNC: |
| 2614 | break; |
| 2615 | case DRM_STATE_APPLY_ASYNC: |
| 2616 | flags |= DRM_MODE_PAGE_FLIP_EVENT | DRM_MODE_ATOMIC_NONBLOCK; |
| 2617 | break; |
Daniel Stone | bb6c19f | 2016-12-08 17:27:17 +0000 | [diff] [blame] | 2618 | case DRM_STATE_TEST_ONLY: |
| 2619 | flags |= DRM_MODE_ATOMIC_TEST_ONLY; |
| 2620 | break; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2621 | } |
| 2622 | |
| 2623 | ret = drmModeAtomicCommit(b->drm.fd, req, flags, b); |
Daniel Stone | bb6c19f | 2016-12-08 17:27:17 +0000 | [diff] [blame] | 2624 | |
| 2625 | /* Test commits do not take ownership of the state; return |
| 2626 | * without freeing here. */ |
| 2627 | if (mode == DRM_STATE_TEST_ONLY) { |
| 2628 | drmModeAtomicFree(req); |
| 2629 | return ret; |
| 2630 | } |
| 2631 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2632 | if (ret != 0) { |
| 2633 | weston_log("atomic: couldn't commit new state: %m\n"); |
| 2634 | goto out; |
| 2635 | } |
| 2636 | |
| 2637 | wl_list_for_each_safe(output_state, tmp, &pending_state->output_list, |
| 2638 | link) |
| 2639 | drm_output_assign_state(output_state, mode); |
| 2640 | |
| 2641 | b->state_invalid = false; |
| 2642 | |
| 2643 | assert(wl_list_empty(&pending_state->output_list)); |
| 2644 | |
| 2645 | out: |
| 2646 | drmModeAtomicFree(req); |
| 2647 | drm_pending_state_free(pending_state); |
| 2648 | return ret; |
| 2649 | } |
| 2650 | #endif |
| 2651 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2652 | /** |
Daniel Stone | bb6c19f | 2016-12-08 17:27:17 +0000 | [diff] [blame] | 2653 | * Tests a pending state, to see if the kernel will accept the update as |
| 2654 | * constructed. |
| 2655 | * |
| 2656 | * Using atomic modesetting, the kernel performs the same checks as it would |
| 2657 | * on a real commit, returning success or failure without actually modifying |
| 2658 | * the running state. It does not return -EBUSY if there are pending updates |
| 2659 | * in flight, so states may be tested at any point, however this means a |
| 2660 | * state which passed testing may fail on a real commit if the timing is not |
| 2661 | * respected (e.g. committing before the previous commit has completed). |
| 2662 | * |
| 2663 | * Without atomic modesetting, we have no way to check, so we optimistically |
| 2664 | * claim it will work. |
| 2665 | * |
| 2666 | * Unlike drm_pending_state_apply() and drm_pending_state_apply_sync(), this |
| 2667 | * function does _not_ take ownership of pending_state, nor does it clear |
| 2668 | * state_invalid. |
| 2669 | */ |
| 2670 | static int |
| 2671 | drm_pending_state_test(struct drm_pending_state *pending_state) |
| 2672 | { |
| 2673 | #ifdef HAVE_DRM_ATOMIC |
| 2674 | struct drm_backend *b = pending_state->backend; |
| 2675 | |
| 2676 | if (b->atomic_modeset) |
| 2677 | return drm_pending_state_apply_atomic(pending_state, |
| 2678 | DRM_STATE_TEST_ONLY); |
| 2679 | #endif |
| 2680 | |
| 2681 | /* We have no way to test state before application on the legacy |
| 2682 | * modesetting API, so just claim it succeeded. */ |
| 2683 | return 0; |
| 2684 | } |
| 2685 | |
| 2686 | /** |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2687 | * Applies all of a pending_state asynchronously: the primary entry point for |
| 2688 | * applying KMS state to a device. Updates the state for all outputs in the |
| 2689 | * pending_state, as well as disabling any unclaimed outputs. |
| 2690 | * |
| 2691 | * Unconditionally takes ownership of pending_state, and clears state_invalid. |
| 2692 | */ |
| 2693 | static int |
| 2694 | drm_pending_state_apply(struct drm_pending_state *pending_state) |
| 2695 | { |
| 2696 | struct drm_backend *b = pending_state->backend; |
| 2697 | struct drm_output_state *output_state, *tmp; |
| 2698 | uint32_t *unused; |
| 2699 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2700 | #ifdef HAVE_DRM_ATOMIC |
| 2701 | if (b->atomic_modeset) |
| 2702 | return drm_pending_state_apply_atomic(pending_state, |
| 2703 | DRM_STATE_APPLY_ASYNC); |
| 2704 | #endif |
| 2705 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2706 | if (b->state_invalid) { |
| 2707 | /* If we need to reset all our state (e.g. because we've |
| 2708 | * just started, or just been VT-switched in), explicitly |
| 2709 | * disable all the CRTCs we aren't using. This also disables |
| 2710 | * all connectors on these CRTCs, so we don't need to do that |
| 2711 | * separately with the pre-atomic API. */ |
| 2712 | wl_array_for_each(unused, &b->unused_crtcs) |
| 2713 | drmModeSetCrtc(b->drm.fd, *unused, 0, 0, 0, NULL, 0, |
| 2714 | NULL); |
| 2715 | } |
| 2716 | |
| 2717 | wl_list_for_each_safe(output_state, tmp, &pending_state->output_list, |
| 2718 | link) { |
| 2719 | struct drm_output *output = output_state->output; |
| 2720 | int ret; |
| 2721 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2722 | ret = drm_output_apply_state_legacy(output_state); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2723 | if (ret != 0) { |
| 2724 | weston_log("Couldn't apply state for output %s\n", |
| 2725 | output->base.name); |
| 2726 | } |
| 2727 | } |
| 2728 | |
| 2729 | b->state_invalid = false; |
| 2730 | |
| 2731 | assert(wl_list_empty(&pending_state->output_list)); |
| 2732 | |
| 2733 | drm_pending_state_free(pending_state); |
| 2734 | |
| 2735 | return 0; |
| 2736 | } |
| 2737 | |
| 2738 | /** |
| 2739 | * The synchronous version of drm_pending_state_apply. May only be used to |
| 2740 | * disable outputs. Does so synchronously: the request is guaranteed to have |
| 2741 | * completed on return, and the output will not be touched afterwards. |
| 2742 | * |
| 2743 | * Unconditionally takes ownership of pending_state, and clears state_invalid. |
| 2744 | */ |
| 2745 | static int |
| 2746 | drm_pending_state_apply_sync(struct drm_pending_state *pending_state) |
| 2747 | { |
| 2748 | struct drm_backend *b = pending_state->backend; |
| 2749 | struct drm_output_state *output_state, *tmp; |
| 2750 | uint32_t *unused; |
| 2751 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2752 | #ifdef HAVE_DRM_ATOMIC |
| 2753 | if (b->atomic_modeset) |
| 2754 | return drm_pending_state_apply_atomic(pending_state, |
| 2755 | DRM_STATE_APPLY_SYNC); |
| 2756 | #endif |
| 2757 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2758 | if (b->state_invalid) { |
| 2759 | /* If we need to reset all our state (e.g. because we've |
| 2760 | * just started, or just been VT-switched in), explicitly |
| 2761 | * disable all the CRTCs we aren't using. This also disables |
| 2762 | * all connectors on these CRTCs, so we don't need to do that |
| 2763 | * separately with the pre-atomic API. */ |
| 2764 | wl_array_for_each(unused, &b->unused_crtcs) |
| 2765 | drmModeSetCrtc(b->drm.fd, *unused, 0, 0, 0, NULL, 0, |
| 2766 | NULL); |
| 2767 | } |
| 2768 | |
| 2769 | wl_list_for_each_safe(output_state, tmp, &pending_state->output_list, |
| 2770 | link) { |
| 2771 | int ret; |
| 2772 | |
| 2773 | assert(output_state->dpms == WESTON_DPMS_OFF); |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2774 | ret = drm_output_apply_state_legacy(output_state); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2775 | if (ret != 0) { |
| 2776 | weston_log("Couldn't apply state for output %s\n", |
| 2777 | output_state->output->base.name); |
| 2778 | } |
| 2779 | } |
| 2780 | |
| 2781 | b->state_invalid = false; |
| 2782 | |
| 2783 | assert(wl_list_empty(&pending_state->output_list)); |
| 2784 | |
| 2785 | drm_pending_state_free(pending_state); |
| 2786 | |
| 2787 | return 0; |
| 2788 | } |
| 2789 | |
| 2790 | static int |
| 2791 | drm_output_repaint(struct weston_output *output_base, |
| 2792 | pixman_region32_t *damage, |
| 2793 | void *repaint_data) |
| 2794 | { |
| 2795 | struct drm_pending_state *pending_state = repaint_data; |
| 2796 | struct drm_output *output = to_drm_output(output_base); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2797 | struct drm_output_state *state = NULL; |
| 2798 | struct drm_plane_state *scanout_state; |
| 2799 | |
| 2800 | if (output->disable_pending || output->destroy_pending) |
| 2801 | goto err; |
| 2802 | |
| 2803 | assert(!output->state_last); |
| 2804 | |
| 2805 | /* If planes have been disabled in the core, we might not have |
| 2806 | * hit assign_planes at all, so might not have valid output state |
| 2807 | * here. */ |
| 2808 | state = drm_pending_state_get_output(pending_state, output); |
| 2809 | if (!state) |
| 2810 | state = drm_output_state_duplicate(output->state_cur, |
| 2811 | pending_state, |
| 2812 | DRM_OUTPUT_STATE_CLEAR_PLANES); |
| 2813 | state->dpms = WESTON_DPMS_ON; |
| 2814 | |
| 2815 | drm_output_render(state, damage); |
| 2816 | scanout_state = drm_output_state_get_plane(state, |
| 2817 | output->scanout_plane); |
| 2818 | if (!scanout_state || !scanout_state->fb) |
| 2819 | goto err; |
| 2820 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2821 | return 0; |
| 2822 | |
| 2823 | err: |
| 2824 | drm_output_state_free(state); |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 2825 | return -1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2826 | } |
| 2827 | |
| 2828 | static void |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2829 | drm_output_start_repaint_loop(struct weston_output *output_base) |
| 2830 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2831 | struct drm_output *output = to_drm_output(output_base); |
Daniel Stone | 8747f95 | 2016-11-29 20:17:32 +0000 | [diff] [blame] | 2832 | struct drm_pending_state *pending_state; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 2833 | struct drm_plane *scanout_plane = output->scanout_plane; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2834 | struct drm_backend *backend = |
| 2835 | to_drm_backend(output_base->compositor); |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 2836 | struct timespec ts, tnow; |
| 2837 | struct timespec vbl2now; |
| 2838 | int64_t refresh_nsec; |
| 2839 | int ret; |
| 2840 | drmVBlank vbl = { |
| 2841 | .request.type = DRM_VBLANK_RELATIVE, |
| 2842 | .request.sequence = 0, |
| 2843 | .request.signal = 0, |
| 2844 | }; |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 2845 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2846 | if (output->disable_pending || output->destroy_pending) |
Xiong Zhang | abd5d47 | 2013-10-11 14:43:07 +0800 | [diff] [blame] | 2847 | return; |
| 2848 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 2849 | if (!output->scanout_plane->state_cur->fb) { |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 2850 | /* We can't page flip if there's no mode set */ |
David Herrmann | 3c688c5 | 2013-10-22 17:11:25 +0200 | [diff] [blame] | 2851 | goto finish_frame; |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 2852 | } |
| 2853 | |
Pekka Paalanen | 6b65d8f | 2017-07-27 13:44:32 +0300 | [diff] [blame] | 2854 | /* Need to smash all state in from scratch; current timings might not |
| 2855 | * be what we want, page flip might not work, etc. |
| 2856 | */ |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 2857 | if (backend->state_invalid) |
Pekka Paalanen | 6b65d8f | 2017-07-27 13:44:32 +0300 | [diff] [blame] | 2858 | goto finish_frame; |
| 2859 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 2860 | assert(scanout_plane->state_cur->output == output); |
| 2861 | |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 2862 | /* Try to get current msc and timestamp via instant query */ |
| 2863 | vbl.request.type |= drm_waitvblank_pipe(output); |
| 2864 | ret = drmWaitVBlank(backend->drm.fd, &vbl); |
| 2865 | |
| 2866 | /* Error ret or zero timestamp means failure to get valid timestamp */ |
| 2867 | if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) { |
| 2868 | ts.tv_sec = vbl.reply.tval_sec; |
| 2869 | ts.tv_nsec = vbl.reply.tval_usec * 1000; |
| 2870 | |
| 2871 | /* Valid timestamp for most recent vblank - not stale? |
| 2872 | * Stale ts could happen on Linux 3.17+, so make sure it |
| 2873 | * is not older than 1 refresh duration since now. |
| 2874 | */ |
| 2875 | weston_compositor_read_presentation_clock(backend->compositor, |
| 2876 | &tnow); |
| 2877 | timespec_sub(&vbl2now, &tnow, &ts); |
| 2878 | refresh_nsec = |
| 2879 | millihz_to_nsec(output->base.current_mode->refresh); |
| 2880 | if (timespec_to_nsec(&vbl2now) < refresh_nsec) { |
| 2881 | drm_output_update_msc(output, vbl.reply.sequence); |
| 2882 | weston_output_finish_frame(output_base, &ts, |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 2883 | WP_PRESENTATION_FEEDBACK_INVALID); |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 2884 | return; |
| 2885 | } |
| 2886 | } |
| 2887 | |
| 2888 | /* Immediate query didn't provide valid timestamp. |
| 2889 | * Use pageflip fallback. |
| 2890 | */ |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2891 | |
Daniel Stone | 205c0a0 | 2017-04-04 17:54:33 +0100 | [diff] [blame] | 2892 | assert(!output->page_flip_pending); |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2893 | assert(!output->state_last); |
| 2894 | |
| 2895 | pending_state = drm_pending_state_alloc(backend); |
Daniel Stone | 8747f95 | 2016-11-29 20:17:32 +0000 | [diff] [blame] | 2896 | drm_output_state_duplicate(output->state_cur, pending_state, |
| 2897 | DRM_OUTPUT_STATE_PRESERVE_PLANES); |
Daniel Stone | 205c0a0 | 2017-04-04 17:54:33 +0100 | [diff] [blame] | 2898 | |
Daniel Stone | 8747f95 | 2016-11-29 20:17:32 +0000 | [diff] [blame] | 2899 | ret = drm_pending_state_apply(pending_state); |
| 2900 | if (ret != 0) { |
| 2901 | weston_log("applying repaint-start state failed: %m\n"); |
David Herrmann | 3c688c5 | 2013-10-22 17:11:25 +0200 | [diff] [blame] | 2902 | goto finish_frame; |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2903 | } |
David Herrmann | 3c688c5 | 2013-10-22 17:11:25 +0200 | [diff] [blame] | 2904 | |
| 2905 | return; |
| 2906 | |
| 2907 | finish_frame: |
| 2908 | /* if we cannot page-flip, immediately finish frame */ |
Daniel Stone | 3615ce1 | 2017-03-01 11:34:05 +0000 | [diff] [blame] | 2909 | weston_output_finish_frame(output_base, NULL, |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 2910 | WP_PRESENTATION_FEEDBACK_INVALID); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2911 | } |
| 2912 | |
| 2913 | static void |
Pekka Paalanen | 641307c | 2014-09-23 22:08:47 -0400 | [diff] [blame] | 2914 | drm_output_update_msc(struct drm_output *output, unsigned int seq) |
| 2915 | { |
| 2916 | uint64_t msc_hi = output->base.msc >> 32; |
| 2917 | |
| 2918 | if (seq < (output->base.msc & 0xffffffff)) |
| 2919 | msc_hi++; |
| 2920 | |
| 2921 | output->base.msc = (msc_hi << 32) + seq; |
| 2922 | } |
| 2923 | |
| 2924 | static void |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2925 | vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec, |
| 2926 | void *data) |
| 2927 | { |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2928 | struct drm_plane_state *ps = (struct drm_plane_state *) data; |
| 2929 | struct drm_output_state *os = ps->output_state; |
| 2930 | struct drm_output *output = os->output; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2931 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 2932 | uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION | |
| 2933 | WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK; |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 2934 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2935 | assert(!b->atomic_modeset); |
| 2936 | |
Pekka Paalanen | 641307c | 2014-09-23 22:08:47 -0400 | [diff] [blame] | 2937 | drm_output_update_msc(output, frame); |
Daniel Stone | 65d87d0 | 2017-04-04 17:54:32 +0100 | [diff] [blame] | 2938 | output->vblank_pending--; |
| 2939 | assert(output->vblank_pending >= 0); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2940 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2941 | assert(ps->fb); |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 2942 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2943 | if (output->page_flip_pending || output->vblank_pending) |
| 2944 | return; |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 2945 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2946 | drm_output_update_complete(output, flags, sec, usec); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2947 | } |
| 2948 | |
| 2949 | static void |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2950 | page_flip_handler(int fd, unsigned int frame, |
| 2951 | unsigned int sec, unsigned int usec, void *data) |
| 2952 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2953 | struct drm_output *output = data; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2954 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 2955 | uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC | |
| 2956 | WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION | |
| 2957 | WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2958 | |
Pekka Paalanen | 641307c | 2014-09-23 22:08:47 -0400 | [diff] [blame] | 2959 | drm_output_update_msc(output, frame); |
| 2960 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2961 | assert(!b->atomic_modeset); |
Daniel Stone | 205c0a0 | 2017-04-04 17:54:33 +0100 | [diff] [blame] | 2962 | assert(output->page_flip_pending); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2963 | output->page_flip_pending = 0; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 2964 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2965 | if (output->vblank_pending) |
| 2966 | return; |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 2967 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2968 | drm_output_update_complete(output, flags, sec, usec); |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 2969 | } |
| 2970 | |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 2971 | /** |
| 2972 | * Begin a new repaint cycle |
| 2973 | * |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2974 | * Called by the core compositor at the beginning of a repaint cycle. Creates |
| 2975 | * a new pending_state structure to own any output state created by individual |
| 2976 | * output repaint functions until the repaint is flushed or cancelled. |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 2977 | */ |
| 2978 | static void * |
| 2979 | drm_repaint_begin(struct weston_compositor *compositor) |
| 2980 | { |
| 2981 | struct drm_backend *b = to_drm_backend(compositor); |
| 2982 | struct drm_pending_state *ret; |
| 2983 | |
| 2984 | ret = drm_pending_state_alloc(b); |
| 2985 | b->repaint_data = ret; |
| 2986 | |
| 2987 | return ret; |
| 2988 | } |
| 2989 | |
| 2990 | /** |
| 2991 | * Flush a repaint set |
| 2992 | * |
| 2993 | * Called by the core compositor when a repaint cycle has been completed |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2994 | * and should be flushed. Frees the pending state, transitioning ownership |
| 2995 | * of the output state from the pending state, to the update itself. When |
| 2996 | * the update completes (see drm_output_update_complete), the output |
| 2997 | * state will be freed. |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 2998 | */ |
| 2999 | static void |
| 3000 | drm_repaint_flush(struct weston_compositor *compositor, void *repaint_data) |
| 3001 | { |
| 3002 | struct drm_backend *b = to_drm_backend(compositor); |
| 3003 | struct drm_pending_state *pending_state = repaint_data; |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 3004 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 3005 | drm_pending_state_apply(pending_state); |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 3006 | b->repaint_data = NULL; |
| 3007 | } |
| 3008 | |
| 3009 | /** |
| 3010 | * Cancel a repaint set |
| 3011 | * |
| 3012 | * Called by the core compositor when a repaint has finished, so the data |
| 3013 | * held across the repaint cycle should be discarded. |
| 3014 | */ |
| 3015 | static void |
| 3016 | drm_repaint_cancel(struct weston_compositor *compositor, void *repaint_data) |
| 3017 | { |
| 3018 | struct drm_backend *b = to_drm_backend(compositor); |
| 3019 | struct drm_pending_state *pending_state = repaint_data; |
| 3020 | |
| 3021 | drm_pending_state_free(pending_state); |
| 3022 | b->repaint_data = NULL; |
| 3023 | } |
| 3024 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 3025 | #ifdef HAVE_DRM_ATOMIC |
| 3026 | static void |
| 3027 | atomic_flip_handler(int fd, unsigned int frame, unsigned int sec, |
| 3028 | unsigned int usec, unsigned int crtc_id, void *data) |
| 3029 | { |
| 3030 | struct drm_backend *b = data; |
| 3031 | struct drm_output *output = drm_output_find_by_crtc(b, crtc_id); |
| 3032 | uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC | |
| 3033 | WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION | |
| 3034 | WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK; |
| 3035 | |
| 3036 | /* During the initial modeset, we can disable CRTCs which we don't |
| 3037 | * actually handle during normal operation; this will give us events |
| 3038 | * for unknown outputs. Ignore them. */ |
| 3039 | if (!output || !output->base.enabled) |
| 3040 | return; |
| 3041 | |
| 3042 | drm_output_update_msc(output, frame); |
| 3043 | |
| 3044 | assert(b->atomic_modeset); |
| 3045 | assert(output->atomic_complete_pending); |
| 3046 | output->atomic_complete_pending = 0; |
| 3047 | |
| 3048 | drm_output_update_complete(output, flags, sec, usec); |
| 3049 | } |
| 3050 | #endif |
| 3051 | |
Daniel Stone | f829062 | 2016-12-09 17:32:10 +0000 | [diff] [blame] | 3052 | static struct drm_plane_state * |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 3053 | drm_output_prepare_overlay_view(struct drm_output_state *output_state, |
Daniel Stone | a284d27 | 2018-07-10 18:40:12 +0100 | [diff] [blame^] | 3054 | struct weston_view *ev, |
| 3055 | enum drm_output_propose_state_mode mode) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3056 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 3057 | struct drm_output *output = output_state->output; |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 3058 | struct weston_compositor *ec = output->base.compositor; |
Daniel Stone | d6e2a76 | 2016-11-16 19:33:20 +0000 | [diff] [blame] | 3059 | struct drm_backend *b = to_drm_backend(ec); |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 3060 | struct drm_plane *p; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 3061 | struct drm_plane_state *state = NULL; |
Daniel Stone | f11ec02 | 2016-11-17 17:32:42 +0000 | [diff] [blame] | 3062 | struct drm_fb *fb; |
Daniel Stone | db10df1 | 2016-12-08 13:15:58 +0000 | [diff] [blame] | 3063 | unsigned int i; |
Daniel Stone | a284d27 | 2018-07-10 18:40:12 +0100 | [diff] [blame^] | 3064 | int ret; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3065 | |
Daniel Stone | 44abfaa | 2018-07-10 14:31:06 +0100 | [diff] [blame] | 3066 | assert(!b->sprites_are_broken); |
Kristian Høgsberg | 65bec24 | 2012-03-05 19:57:35 -0500 | [diff] [blame] | 3067 | |
Daniel Stone | f11ec02 | 2016-11-17 17:32:42 +0000 | [diff] [blame] | 3068 | fb = drm_fb_get_from_view(output_state, ev); |
| 3069 | if (!fb) |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 3070 | return NULL; |
| 3071 | |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 3072 | wl_list_for_each(p, &b->plane_list, link) { |
| 3073 | if (p->type != WDRM_PLANE_TYPE_OVERLAY) |
| 3074 | continue; |
| 3075 | |
Daniel Stone | 5ff289a | 2017-10-07 12:59:02 +0100 | [diff] [blame] | 3076 | if (!drm_plane_is_available(p, output)) |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 3077 | continue; |
| 3078 | |
Daniel Stone | f11ec02 | 2016-11-17 17:32:42 +0000 | [diff] [blame] | 3079 | /* Check whether the format is supported */ |
| 3080 | for (i = 0; i < p->count_formats; i++) { |
Sergi Granell | f445622 | 2017-01-12 17:17:32 +0000 | [diff] [blame] | 3081 | unsigned int j; |
| 3082 | |
| 3083 | if (p->formats[i].format != fb->format->format) |
| 3084 | continue; |
| 3085 | |
| 3086 | if (fb->modifier == DRM_FORMAT_MOD_INVALID) |
| 3087 | break; |
| 3088 | |
| 3089 | for (j = 0; j < p->formats[i].count_modifiers; j++) { |
| 3090 | if (p->formats[i].modifiers[j] == fb->modifier) |
| 3091 | break; |
| 3092 | } |
| 3093 | if (j != p->formats[i].count_modifiers) |
Daniel Stone | f11ec02 | 2016-11-17 17:32:42 +0000 | [diff] [blame] | 3094 | break; |
| 3095 | } |
| 3096 | if (i == p->count_formats) |
| 3097 | continue; |
| 3098 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 3099 | state = drm_output_state_get_plane(output_state, p); |
| 3100 | if (state->fb) { |
| 3101 | state = NULL; |
| 3102 | continue; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3103 | } |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 3104 | |
Daniel Stone | a284d27 | 2018-07-10 18:40:12 +0100 | [diff] [blame^] | 3105 | state->ev = ev; |
| 3106 | state->output = output; |
| 3107 | if (!drm_plane_state_coords_for_view(state, ev)) { |
| 3108 | drm_plane_state_put_back(state); |
| 3109 | state = NULL; |
| 3110 | continue; |
| 3111 | } |
| 3112 | if (state->src_w != state->dest_w << 16 || |
| 3113 | state->src_h != state->dest_h << 16) { |
| 3114 | drm_plane_state_put_back(state); |
| 3115 | state = NULL; |
| 3116 | continue; |
| 3117 | } |
| 3118 | |
| 3119 | /* We hold one reference for the lifetime of this function; |
| 3120 | * from calling drm_fb_get_from_view, to the out label where |
| 3121 | * we unconditionally drop the reference. So, we take another |
| 3122 | * reference here to live within the state. */ |
| 3123 | state->fb = drm_fb_ref(fb); |
| 3124 | |
| 3125 | /* In planes-only mode, we don't have an incremental state to |
| 3126 | * test against, so we just hope it'll work. */ |
| 3127 | if (mode == DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY) |
| 3128 | goto out; |
| 3129 | |
| 3130 | ret = drm_pending_state_test(output_state->pending_state); |
| 3131 | if (ret == 0) |
| 3132 | goto out; |
| 3133 | |
| 3134 | drm_plane_state_put_back(state); |
| 3135 | state = NULL; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3136 | } |
| 3137 | |
Daniel Stone | a284d27 | 2018-07-10 18:40:12 +0100 | [diff] [blame^] | 3138 | out: |
| 3139 | drm_fb_unref(fb); |
Daniel Stone | f829062 | 2016-12-09 17:32:10 +0000 | [diff] [blame] | 3140 | return state; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3141 | } |
| 3142 | |
Pekka Paalanen | d0ead48 | 2014-06-16 12:05:40 +0300 | [diff] [blame] | 3143 | /** |
| 3144 | * Update the image for the current cursor surface |
| 3145 | * |
Daniel Stone | 9b56038 | 2016-11-16 19:46:35 +0000 | [diff] [blame] | 3146 | * @param plane_state DRM cursor plane state |
| 3147 | * @param ev Source view for cursor |
Pekka Paalanen | d0ead48 | 2014-06-16 12:05:40 +0300 | [diff] [blame] | 3148 | */ |
| 3149 | static void |
Daniel Stone | 9b56038 | 2016-11-16 19:46:35 +0000 | [diff] [blame] | 3150 | cursor_bo_update(struct drm_plane_state *plane_state, struct weston_view *ev) |
Pekka Paalanen | d0ead48 | 2014-06-16 12:05:40 +0300 | [diff] [blame] | 3151 | { |
Daniel Stone | 9b56038 | 2016-11-16 19:46:35 +0000 | [diff] [blame] | 3152 | struct drm_backend *b = plane_state->plane->backend; |
| 3153 | struct gbm_bo *bo = plane_state->fb->bo; |
Pekka Paalanen | d0ead48 | 2014-06-16 12:05:40 +0300 | [diff] [blame] | 3154 | struct weston_buffer *buffer = ev->surface->buffer_ref.buffer; |
| 3155 | uint32_t buf[b->cursor_width * b->cursor_height]; |
| 3156 | int32_t stride; |
| 3157 | uint8_t *s; |
| 3158 | int i; |
| 3159 | |
| 3160 | assert(buffer && buffer->shm_buffer); |
| 3161 | assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource)); |
Daniel Stone | 9b56038 | 2016-11-16 19:46:35 +0000 | [diff] [blame] | 3162 | assert(buffer->width <= b->cursor_width); |
| 3163 | assert(buffer->height <= b->cursor_height); |
Pekka Paalanen | d0ead48 | 2014-06-16 12:05:40 +0300 | [diff] [blame] | 3164 | |
| 3165 | memset(buf, 0, sizeof buf); |
| 3166 | stride = wl_shm_buffer_get_stride(buffer->shm_buffer); |
| 3167 | s = wl_shm_buffer_get_data(buffer->shm_buffer); |
| 3168 | |
| 3169 | wl_shm_buffer_begin_access(buffer->shm_buffer); |
Daniel Stone | 9b56038 | 2016-11-16 19:46:35 +0000 | [diff] [blame] | 3170 | for (i = 0; i < buffer->height; i++) |
Pekka Paalanen | d0ead48 | 2014-06-16 12:05:40 +0300 | [diff] [blame] | 3171 | memcpy(buf + i * b->cursor_width, |
| 3172 | s + i * stride, |
Daniel Stone | 9b56038 | 2016-11-16 19:46:35 +0000 | [diff] [blame] | 3173 | buffer->width * 4); |
Pekka Paalanen | d0ead48 | 2014-06-16 12:05:40 +0300 | [diff] [blame] | 3174 | wl_shm_buffer_end_access(buffer->shm_buffer); |
| 3175 | |
| 3176 | if (gbm_bo_write(bo, buf, sizeof buf) < 0) |
| 3177 | weston_log("failed update cursor: %m\n"); |
| 3178 | } |
| 3179 | |
Daniel Stone | f829062 | 2016-12-09 17:32:10 +0000 | [diff] [blame] | 3180 | static struct drm_plane_state * |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3181 | drm_output_prepare_cursor_view(struct drm_output_state *output_state, |
| 3182 | struct weston_view *ev) |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 3183 | { |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3184 | struct drm_output *output = output_state->output; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 3185 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3186 | struct drm_plane *plane = output->cursor_plane; |
| 3187 | struct drm_plane_state *plane_state; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3188 | struct wl_shm_buffer *shmbuf; |
| 3189 | bool needs_update = false; |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 3190 | |
Daniel Stone | f7a2f83 | 2016-12-08 17:19:09 +0000 | [diff] [blame] | 3191 | assert(!b->cursors_are_broken); |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3192 | |
Daniel Stone | f7a2f83 | 2016-12-08 17:19:09 +0000 | [diff] [blame] | 3193 | if (!plane) |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3194 | return NULL; |
| 3195 | |
| 3196 | if (!plane->state_cur->complete) |
| 3197 | return NULL; |
| 3198 | |
| 3199 | if (plane->state_cur->output && plane->state_cur->output != output) |
| 3200 | return NULL; |
| 3201 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3202 | /* We use GBM to import SHM buffers. */ |
| 3203 | if (b->gbm == NULL) |
| 3204 | return NULL; |
| 3205 | |
| 3206 | if (ev->surface->buffer_ref.buffer == NULL) |
| 3207 | return NULL; |
| 3208 | shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource); |
| 3209 | if (!shmbuf) |
| 3210 | return NULL; |
| 3211 | if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888) |
| 3212 | return NULL; |
| 3213 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3214 | plane_state = |
| 3215 | drm_output_state_get_plane(output_state, output->cursor_plane); |
| 3216 | |
| 3217 | if (plane_state && plane_state->fb) |
| 3218 | return NULL; |
| 3219 | |
Daniel Stone | 9b56038 | 2016-11-16 19:46:35 +0000 | [diff] [blame] | 3220 | /* We can't scale with the legacy API, and we don't try to account for |
| 3221 | * simple cropping/translation in cursor_bo_update. */ |
| 3222 | plane_state->output = output; |
| 3223 | if (!drm_plane_state_coords_for_view(plane_state, ev)) |
| 3224 | goto err; |
| 3225 | |
| 3226 | if (plane_state->src_x != 0 || plane_state->src_y != 0 || |
| 3227 | plane_state->src_w > (unsigned) b->cursor_width << 16 || |
| 3228 | plane_state->src_h > (unsigned) b->cursor_height << 16 || |
| 3229 | plane_state->src_w != plane_state->dest_w << 16 || |
| 3230 | plane_state->src_h != plane_state->dest_h << 16) |
| 3231 | goto err; |
| 3232 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3233 | /* Since we're setting plane state up front, we need to work out |
| 3234 | * whether or not we need to upload a new cursor. We can't use the |
| 3235 | * plane damage, since the planes haven't actually been calculated |
| 3236 | * yet: instead try to figure it out directly. KMS cursor planes are |
| 3237 | * pretty unique here, in that they lie partway between a Weston plane |
| 3238 | * (direct scanout) and a renderer. */ |
| 3239 | if (ev != output->cursor_view || |
| 3240 | pixman_region32_not_empty(&ev->surface->damage)) { |
| 3241 | output->current_cursor++; |
| 3242 | output->current_cursor = |
| 3243 | output->current_cursor % |
| 3244 | ARRAY_LENGTH(output->gbm_cursor_fb); |
| 3245 | needs_update = true; |
| 3246 | } |
| 3247 | |
| 3248 | output->cursor_view = ev; |
Daniel Stone | ee1aea7 | 2017-12-18 13:41:09 +0000 | [diff] [blame] | 3249 | plane_state->ev = ev; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3250 | |
| 3251 | plane_state->fb = |
| 3252 | drm_fb_ref(output->gbm_cursor_fb[output->current_cursor]); |
Daniel Stone | 9b56038 | 2016-11-16 19:46:35 +0000 | [diff] [blame] | 3253 | |
| 3254 | if (needs_update) |
| 3255 | cursor_bo_update(plane_state, ev); |
| 3256 | |
| 3257 | /* The cursor API is somewhat special: in cursor_bo_update(), we upload |
| 3258 | * a buffer which is always cursor_width x cursor_height, even if the |
| 3259 | * surface we want to promote is actually smaller than this. Manually |
| 3260 | * mangle the plane state to deal with this. */ |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3261 | plane_state->src_w = b->cursor_width << 16; |
| 3262 | plane_state->src_h = b->cursor_height << 16; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3263 | plane_state->dest_w = b->cursor_width; |
| 3264 | plane_state->dest_h = b->cursor_height; |
| 3265 | |
Daniel Stone | f829062 | 2016-12-09 17:32:10 +0000 | [diff] [blame] | 3266 | return plane_state; |
Daniel Stone | 9b56038 | 2016-11-16 19:46:35 +0000 | [diff] [blame] | 3267 | |
| 3268 | err: |
| 3269 | drm_plane_state_put_back(plane_state); |
| 3270 | return NULL; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3271 | } |
| 3272 | |
| 3273 | static void |
| 3274 | drm_output_set_cursor(struct drm_output_state *output_state) |
| 3275 | { |
| 3276 | struct drm_output *output = output_state->output; |
| 3277 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
| 3278 | struct drm_plane *plane = output->cursor_plane; |
| 3279 | struct drm_plane_state *state; |
| 3280 | EGLint handle; |
| 3281 | struct gbm_bo *bo; |
| 3282 | |
| 3283 | if (!plane) |
| 3284 | return; |
| 3285 | |
| 3286 | state = drm_output_state_get_existing_plane(output_state, plane); |
| 3287 | if (!state) |
| 3288 | return; |
| 3289 | |
| 3290 | if (!state->fb) { |
| 3291 | pixman_region32_fini(&plane->base.damage); |
| 3292 | pixman_region32_init(&plane->base.damage); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3293 | drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0); |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 3294 | return; |
| 3295 | } |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 3296 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3297 | assert(state->fb == output->gbm_cursor_fb[output->current_cursor]); |
| 3298 | assert(!plane->state_cur->output || plane->state_cur->output == output); |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 3299 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3300 | if (plane->state_cur->fb != state->fb) { |
| 3301 | bo = state->fb->bo; |
Kristian Høgsberg | 1f5de35 | 2012-07-18 12:09:58 -0400 | [diff] [blame] | 3302 | handle = gbm_bo_get_handle(bo).s32; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3303 | if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle, |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3304 | b->cursor_width, b->cursor_height)) { |
Pekka Paalanen | ae29da2 | 2012-08-06 14:57:05 +0300 | [diff] [blame] | 3305 | weston_log("failed to set cursor: %m\n"); |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3306 | goto err; |
Rob Clark | ab5b1e3 | 2012-08-09 13:24:45 -0500 | [diff] [blame] | 3307 | } |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 3308 | } |
| 3309 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3310 | pixman_region32_fini(&plane->base.damage); |
| 3311 | pixman_region32_init(&plane->base.damage); |
Pekka Paalanen | 7eaed40 | 2015-11-27 14:20:58 +0200 | [diff] [blame] | 3312 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3313 | if (drmModeMoveCursor(b->drm.fd, output->crtc_id, |
| 3314 | state->dest_x, state->dest_y)) { |
Daniel Stone | a7cba1d | 2017-04-04 17:54:21 +0100 | [diff] [blame] | 3315 | weston_log("failed to move cursor: %m\n"); |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3316 | goto err; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 3317 | } |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3318 | |
| 3319 | return; |
| 3320 | |
| 3321 | err: |
| 3322 | b->cursors_are_broken = 1; |
| 3323 | drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0); |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 3324 | } |
| 3325 | |
Daniel Stone | ee1aea7 | 2017-12-18 13:41:09 +0000 | [diff] [blame] | 3326 | static struct drm_output_state * |
| 3327 | drm_output_propose_state(struct weston_output *output_base, |
Daniel Stone | f7a2f83 | 2016-12-08 17:19:09 +0000 | [diff] [blame] | 3328 | struct drm_pending_state *pending_state, |
| 3329 | enum drm_output_propose_state_mode mode) |
Daniel Stone | ee1aea7 | 2017-12-18 13:41:09 +0000 | [diff] [blame] | 3330 | { |
| 3331 | struct drm_output *output = to_drm_output(output_base); |
Daniel Stone | 44abfaa | 2018-07-10 14:31:06 +0100 | [diff] [blame] | 3332 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Daniel Stone | ee1aea7 | 2017-12-18 13:41:09 +0000 | [diff] [blame] | 3333 | struct drm_output_state *state; |
Daniel Stone | a284d27 | 2018-07-10 18:40:12 +0100 | [diff] [blame^] | 3334 | struct drm_plane_state *scanout_state = NULL; |
Daniel Stone | ee1aea7 | 2017-12-18 13:41:09 +0000 | [diff] [blame] | 3335 | struct weston_view *ev; |
Daniel Stone | 8108239 | 2016-12-09 18:03:31 +0000 | [diff] [blame] | 3336 | pixman_region32_t surface_overlap, renderer_region, occluded_region; |
Daniel Stone | f7a2f83 | 2016-12-08 17:19:09 +0000 | [diff] [blame] | 3337 | bool planes_ok = (mode != DRM_OUTPUT_PROPOSE_STATE_RENDERER_ONLY); |
Daniel Stone | d12e516 | 2018-07-10 18:19:37 +0100 | [diff] [blame] | 3338 | bool renderer_ok = (mode != DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY); |
Daniel Stone | bb6c19f | 2016-12-08 17:27:17 +0000 | [diff] [blame] | 3339 | int ret; |
Daniel Stone | ee1aea7 | 2017-12-18 13:41:09 +0000 | [diff] [blame] | 3340 | |
| 3341 | assert(!output->state_last); |
| 3342 | state = drm_output_state_duplicate(output->state_cur, |
| 3343 | pending_state, |
| 3344 | DRM_OUTPUT_STATE_CLEAR_PLANES); |
| 3345 | |
Daniel Stone | a284d27 | 2018-07-10 18:40:12 +0100 | [diff] [blame^] | 3346 | /* We implement mixed mode by progressively creating and testing |
| 3347 | * incremental states, of scanout + overlay + cursor. Since we |
| 3348 | * walk our views top to bottom, the scanout plane is last, however |
| 3349 | * we always need it in our scene for the test modeset to be |
| 3350 | * meaningful. To do this, we steal a reference to the last |
| 3351 | * renderer framebuffer we have, if we think it's basically |
| 3352 | * compatible. If we don't have that, then we conservatively fall |
| 3353 | * back to only using the renderer for this repaint. */ |
| 3354 | if (mode == DRM_OUTPUT_PROPOSE_STATE_MIXED) { |
| 3355 | struct drm_plane *plane = output->scanout_plane; |
| 3356 | struct drm_fb *scanout_fb = plane->state_cur->fb; |
| 3357 | |
| 3358 | if (!scanout_fb || |
| 3359 | (scanout_fb->type != BUFFER_GBM_SURFACE && |
| 3360 | scanout_fb->type != BUFFER_PIXMAN_DUMB)) { |
| 3361 | drm_output_state_free(state); |
| 3362 | return NULL; |
| 3363 | } |
| 3364 | |
| 3365 | if (scanout_fb->width != output_base->current_mode->width || |
| 3366 | scanout_fb->height != output_base->current_mode->height) { |
| 3367 | drm_output_state_free(state); |
| 3368 | return NULL; |
| 3369 | } |
| 3370 | |
| 3371 | scanout_state = drm_plane_state_duplicate(state, |
| 3372 | plane->state_cur); |
| 3373 | } |
| 3374 | |
Daniel Stone | ee1aea7 | 2017-12-18 13:41:09 +0000 | [diff] [blame] | 3375 | /* |
| 3376 | * Find a surface for each sprite in the output using some heuristics: |
| 3377 | * 1) size |
| 3378 | * 2) frequency of update |
| 3379 | * 3) opacity (though some hw might support alpha blending) |
| 3380 | * 4) clipping (this can be fixed with color keys) |
| 3381 | * |
| 3382 | * The idea is to save on blitting since this should save power. |
| 3383 | * If we can get a large video surface on the sprite for example, |
| 3384 | * the main display surface may not need to update at all, and |
| 3385 | * the client buffer can be used directly for the sprite surface |
| 3386 | * as we do for flipping full screen surfaces. |
| 3387 | */ |
| 3388 | pixman_region32_init(&renderer_region); |
Daniel Stone | 8108239 | 2016-12-09 18:03:31 +0000 | [diff] [blame] | 3389 | pixman_region32_init(&occluded_region); |
Daniel Stone | ee1aea7 | 2017-12-18 13:41:09 +0000 | [diff] [blame] | 3390 | |
| 3391 | wl_list_for_each(ev, &output_base->compositor->view_list, link) { |
Daniel Stone | f829062 | 2016-12-09 17:32:10 +0000 | [diff] [blame] | 3392 | struct drm_plane_state *ps = NULL; |
| 3393 | bool force_renderer = false; |
Daniel Stone | 8108239 | 2016-12-09 18:03:31 +0000 | [diff] [blame] | 3394 | pixman_region32_t clipped_view; |
| 3395 | bool occluded = false; |
Daniel Stone | ee1aea7 | 2017-12-18 13:41:09 +0000 | [diff] [blame] | 3396 | |
Daniel Stone | 231ae2f | 2016-11-29 21:03:44 +0000 | [diff] [blame] | 3397 | /* If this view doesn't touch our output at all, there's no |
| 3398 | * reason to do anything with it. */ |
| 3399 | if (!(ev->output_mask & (1u << output->base.id))) |
| 3400 | continue; |
| 3401 | |
| 3402 | /* We only assign planes to views which are exclusively present |
| 3403 | * on our output. */ |
| 3404 | if (ev->output_mask != (1u << output->base.id)) |
Daniel Stone | f829062 | 2016-12-09 17:32:10 +0000 | [diff] [blame] | 3405 | force_renderer = true; |
Daniel Stone | 231ae2f | 2016-11-29 21:03:44 +0000 | [diff] [blame] | 3406 | |
Daniel Stone | ca6fbe3 | 2018-07-10 18:08:12 +0100 | [diff] [blame] | 3407 | if (!ev->surface->buffer_ref.buffer) |
| 3408 | force_renderer = true; |
| 3409 | |
Daniel Stone | 8108239 | 2016-12-09 18:03:31 +0000 | [diff] [blame] | 3410 | /* Ignore views we know to be totally occluded. */ |
| 3411 | pixman_region32_init(&clipped_view); |
| 3412 | pixman_region32_intersect(&clipped_view, |
| 3413 | &ev->transform.boundingbox, |
| 3414 | &output->base.region); |
| 3415 | |
| 3416 | pixman_region32_init(&surface_overlap); |
| 3417 | pixman_region32_subtract(&surface_overlap, &clipped_view, |
| 3418 | &occluded_region); |
| 3419 | occluded = !pixman_region32_not_empty(&surface_overlap); |
| 3420 | if (occluded) { |
| 3421 | pixman_region32_fini(&surface_overlap); |
| 3422 | pixman_region32_fini(&clipped_view); |
| 3423 | continue; |
| 3424 | } |
| 3425 | |
Daniel Stone | ee1aea7 | 2017-12-18 13:41:09 +0000 | [diff] [blame] | 3426 | /* Since we process views from top to bottom, we know that if |
| 3427 | * the view intersects the calculated renderer region, it must |
| 3428 | * be part of, or occluded by, it, and cannot go on a plane. */ |
Daniel Stone | ee1aea7 | 2017-12-18 13:41:09 +0000 | [diff] [blame] | 3429 | pixman_region32_intersect(&surface_overlap, &renderer_region, |
Daniel Stone | 8108239 | 2016-12-09 18:03:31 +0000 | [diff] [blame] | 3430 | &clipped_view); |
Daniel Stone | ee1aea7 | 2017-12-18 13:41:09 +0000 | [diff] [blame] | 3431 | if (pixman_region32_not_empty(&surface_overlap)) |
Daniel Stone | f829062 | 2016-12-09 17:32:10 +0000 | [diff] [blame] | 3432 | force_renderer = true; |
Daniel Stone | a0f8276 | 2018-07-10 11:44:25 +0100 | [diff] [blame] | 3433 | |
| 3434 | /* We do not control the stacking order of overlay planes; |
| 3435 | * the scanout plane is strictly stacked bottom and the cursor |
| 3436 | * plane top, but the ordering of overlay planes with respect |
| 3437 | * to each other is undefined. Make sure we do not have two |
| 3438 | * planes overlapping each other. */ |
| 3439 | pixman_region32_intersect(&surface_overlap, &occluded_region, |
| 3440 | &clipped_view); |
| 3441 | if (pixman_region32_not_empty(&surface_overlap)) |
Daniel Stone | f829062 | 2016-12-09 17:32:10 +0000 | [diff] [blame] | 3442 | force_renderer = true; |
Daniel Stone | ee1aea7 | 2017-12-18 13:41:09 +0000 | [diff] [blame] | 3443 | pixman_region32_fini(&surface_overlap); |
| 3444 | |
Daniel Stone | f7a2f83 | 2016-12-08 17:19:09 +0000 | [diff] [blame] | 3445 | /* The cursor plane is 'special' in the sense that we can still |
| 3446 | * place it in the legacy API, and we gate that with a separate |
| 3447 | * cursors_are_broken flag. */ |
Daniel Stone | f829062 | 2016-12-09 17:32:10 +0000 | [diff] [blame] | 3448 | if (!force_renderer && !b->cursors_are_broken) |
| 3449 | ps = drm_output_prepare_cursor_view(state, ev); |
Daniel Stone | ee1aea7 | 2017-12-18 13:41:09 +0000 | [diff] [blame] | 3450 | |
Daniel Stone | f829062 | 2016-12-09 17:32:10 +0000 | [diff] [blame] | 3451 | /* If sprites are disabled or the view is not fully opaque, we |
| 3452 | * must put the view into the renderer - unless it has already |
| 3453 | * been placed in the cursor plane, which can handle alpha. */ |
| 3454 | if (!ps && !planes_ok) |
| 3455 | force_renderer = true; |
| 3456 | if (!ps && !drm_view_is_opaque(ev)) |
| 3457 | force_renderer = true; |
Daniel Stone | 8108239 | 2016-12-09 18:03:31 +0000 | [diff] [blame] | 3458 | |
Daniel Stone | a284d27 | 2018-07-10 18:40:12 +0100 | [diff] [blame^] | 3459 | /* Only try to place scanout surfaces in planes-only mode; in |
| 3460 | * mixed mode, we have already failed to place a view on the |
| 3461 | * scanout surface, forcing usage of the renderer on the |
| 3462 | * scanout plane. */ |
| 3463 | if (!ps && !force_renderer && !renderer_ok) |
| 3464 | ps = drm_output_prepare_scanout_view(state, ev, mode); |
Daniel Stone | f829062 | 2016-12-09 17:32:10 +0000 | [diff] [blame] | 3465 | if (!ps && !force_renderer) |
Daniel Stone | a284d27 | 2018-07-10 18:40:12 +0100 | [diff] [blame^] | 3466 | ps = drm_output_prepare_overlay_view(state, ev, mode); |
Daniel Stone | 44abfaa | 2018-07-10 14:31:06 +0100 | [diff] [blame] | 3467 | |
Daniel Stone | f829062 | 2016-12-09 17:32:10 +0000 | [diff] [blame] | 3468 | if (ps) { |
Daniel Stone | f7a2f83 | 2016-12-08 17:19:09 +0000 | [diff] [blame] | 3469 | /* If we have been assigned to an overlay or scanout |
| 3470 | * plane, add this area to the occluded region, so |
| 3471 | * other views are known to be behind it. The cursor |
| 3472 | * plane, however, is special, in that it blends with |
| 3473 | * the content underneath it: the area should neither |
| 3474 | * be added to the renderer region nor the occluded |
| 3475 | * region. */ |
Daniel Stone | f829062 | 2016-12-09 17:32:10 +0000 | [diff] [blame] | 3476 | if (ps->plane->type != WDRM_PLANE_TYPE_CURSOR) { |
Daniel Stone | f7a2f83 | 2016-12-08 17:19:09 +0000 | [diff] [blame] | 3477 | pixman_region32_union(&occluded_region, |
| 3478 | &occluded_region, |
| 3479 | &clipped_view); |
| 3480 | pixman_region32_fini(&clipped_view); |
| 3481 | } |
| 3482 | continue; |
| 3483 | } |
Daniel Stone | ee1aea7 | 2017-12-18 13:41:09 +0000 | [diff] [blame] | 3484 | |
Daniel Stone | d12e516 | 2018-07-10 18:19:37 +0100 | [diff] [blame] | 3485 | /* We have been assigned to the primary (renderer) plane: |
| 3486 | * check if this is OK, and add ourselves to the renderer |
| 3487 | * region if so. */ |
| 3488 | if (!renderer_ok) { |
| 3489 | pixman_region32_fini(&clipped_view); |
| 3490 | goto err_region; |
| 3491 | } |
| 3492 | |
Daniel Stone | f7a2f83 | 2016-12-08 17:19:09 +0000 | [diff] [blame] | 3493 | pixman_region32_union(&renderer_region, |
| 3494 | &renderer_region, |
| 3495 | &clipped_view); |
Daniel Stone | 8108239 | 2016-12-09 18:03:31 +0000 | [diff] [blame] | 3496 | pixman_region32_fini(&clipped_view); |
Daniel Stone | ee1aea7 | 2017-12-18 13:41:09 +0000 | [diff] [blame] | 3497 | } |
| 3498 | pixman_region32_fini(&renderer_region); |
Daniel Stone | 8108239 | 2016-12-09 18:03:31 +0000 | [diff] [blame] | 3499 | pixman_region32_fini(&occluded_region); |
Daniel Stone | ee1aea7 | 2017-12-18 13:41:09 +0000 | [diff] [blame] | 3500 | |
Daniel Stone | bb6c19f | 2016-12-08 17:27:17 +0000 | [diff] [blame] | 3501 | /* Check to see if this state will actually work. */ |
| 3502 | ret = drm_pending_state_test(state->pending_state); |
| 3503 | if (ret != 0) |
| 3504 | goto err; |
| 3505 | |
Daniel Stone | a284d27 | 2018-07-10 18:40:12 +0100 | [diff] [blame^] | 3506 | /* Counterpart to duplicating scanout state at the top of this |
| 3507 | * function: if we have taken a renderer framebuffer and placed it in |
| 3508 | * the pending state in order to incrementally test overlay planes, |
| 3509 | * remove it now. */ |
| 3510 | if (mode == DRM_OUTPUT_PROPOSE_STATE_MIXED) { |
| 3511 | assert(scanout_state->fb->type == BUFFER_GBM_SURFACE || |
| 3512 | scanout_state->fb->type == BUFFER_PIXMAN_DUMB); |
| 3513 | drm_plane_state_put_back(scanout_state); |
| 3514 | } |
| 3515 | |
Daniel Stone | ee1aea7 | 2017-12-18 13:41:09 +0000 | [diff] [blame] | 3516 | return state; |
Daniel Stone | bb6c19f | 2016-12-08 17:27:17 +0000 | [diff] [blame] | 3517 | |
Daniel Stone | d12e516 | 2018-07-10 18:19:37 +0100 | [diff] [blame] | 3518 | err_region: |
| 3519 | pixman_region32_fini(&renderer_region); |
| 3520 | pixman_region32_fini(&occluded_region); |
Daniel Stone | bb6c19f | 2016-12-08 17:27:17 +0000 | [diff] [blame] | 3521 | err: |
| 3522 | drm_output_state_free(state); |
| 3523 | return NULL; |
Daniel Stone | ee1aea7 | 2017-12-18 13:41:09 +0000 | [diff] [blame] | 3524 | } |
| 3525 | |
| 3526 | static void |
| 3527 | drm_assign_planes(struct weston_output *output_base, void *repaint_data) |
| 3528 | { |
| 3529 | struct drm_backend *b = to_drm_backend(output_base->compositor); |
| 3530 | struct drm_pending_state *pending_state = repaint_data; |
| 3531 | struct drm_output *output = to_drm_output(output_base); |
Daniel Stone | f7a2f83 | 2016-12-08 17:19:09 +0000 | [diff] [blame] | 3532 | struct drm_output_state *state = NULL; |
Daniel Stone | ee1aea7 | 2017-12-18 13:41:09 +0000 | [diff] [blame] | 3533 | struct drm_plane_state *plane_state; |
| 3534 | struct weston_view *ev; |
| 3535 | struct weston_plane *primary = &output_base->compositor->primary_plane; |
| 3536 | |
Daniel Stone | d12e516 | 2018-07-10 18:19:37 +0100 | [diff] [blame] | 3537 | if (!b->sprites_are_broken) { |
Daniel Stone | f7a2f83 | 2016-12-08 17:19:09 +0000 | [diff] [blame] | 3538 | state = drm_output_propose_state(output_base, pending_state, |
Daniel Stone | d12e516 | 2018-07-10 18:19:37 +0100 | [diff] [blame] | 3539 | DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY); |
| 3540 | if (!state) |
| 3541 | state = drm_output_propose_state(output_base, pending_state, |
| 3542 | DRM_OUTPUT_PROPOSE_STATE_MIXED); |
| 3543 | } |
Daniel Stone | f7a2f83 | 2016-12-08 17:19:09 +0000 | [diff] [blame] | 3544 | |
| 3545 | if (!state) |
| 3546 | state = drm_output_propose_state(output_base, pending_state, |
| 3547 | DRM_OUTPUT_PROPOSE_STATE_RENDERER_ONLY); |
| 3548 | |
| 3549 | assert(state); |
Daniel Stone | ee1aea7 | 2017-12-18 13:41:09 +0000 | [diff] [blame] | 3550 | |
| 3551 | wl_list_for_each(ev, &output_base->compositor->view_list, link) { |
| 3552 | struct drm_plane *target_plane = NULL; |
| 3553 | |
Daniel Stone | 231ae2f | 2016-11-29 21:03:44 +0000 | [diff] [blame] | 3554 | /* If this view doesn't touch our output at all, there's no |
| 3555 | * reason to do anything with it. */ |
| 3556 | if (!(ev->output_mask & (1u << output->base.id))) |
| 3557 | continue; |
| 3558 | |
Daniel Stone | ee1aea7 | 2017-12-18 13:41:09 +0000 | [diff] [blame] | 3559 | /* Test whether this buffer can ever go into a plane: |
| 3560 | * non-shm, or small enough to be a cursor. |
| 3561 | * |
| 3562 | * Also, keep a reference when using the pixman renderer. |
| 3563 | * That makes it possible to do a seamless switch to the GL |
| 3564 | * renderer and since the pixman renderer keeps a reference |
| 3565 | * to the buffer anyway, there is no side effects. |
| 3566 | */ |
| 3567 | if (b->use_pixman || |
| 3568 | (ev->surface->buffer_ref.buffer && |
| 3569 | (!wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource) || |
| 3570 | (ev->surface->width <= b->cursor_width && |
| 3571 | ev->surface->height <= b->cursor_height)))) |
| 3572 | ev->surface->keep_buffer = true; |
| 3573 | else |
| 3574 | ev->surface->keep_buffer = false; |
| 3575 | |
| 3576 | /* This is a bit unpleasant, but lacking a temporary place to |
| 3577 | * hang a plane off the view, we have to do a nested walk. |
| 3578 | * Our first-order iteration has to be planes rather than |
| 3579 | * views, because otherwise we won't reset views which were |
| 3580 | * previously on planes to being on the primary plane. */ |
| 3581 | wl_list_for_each(plane_state, &state->plane_list, link) { |
| 3582 | if (plane_state->ev == ev) { |
| 3583 | plane_state->ev = NULL; |
| 3584 | target_plane = plane_state->plane; |
| 3585 | break; |
| 3586 | } |
| 3587 | } |
| 3588 | |
| 3589 | if (target_plane) |
| 3590 | weston_view_move_to_plane(ev, &target_plane->base); |
| 3591 | else |
| 3592 | weston_view_move_to_plane(ev, primary); |
| 3593 | |
| 3594 | if (!target_plane || |
| 3595 | target_plane->type == WDRM_PLANE_TYPE_CURSOR) { |
| 3596 | /* cursor plane & renderer involve a copy */ |
| 3597 | ev->psf_flags = 0; |
| 3598 | } else { |
| 3599 | /* All other planes are a direct scanout of a |
| 3600 | * single client buffer. |
| 3601 | */ |
| 3602 | ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY; |
| 3603 | } |
| 3604 | } |
| 3605 | |
| 3606 | /* We rely on output->cursor_view being both an accurate reflection of |
| 3607 | * the cursor plane's state, but also being maintained across repaints |
| 3608 | * to avoid unnecessary damage uploads, per the comment in |
| 3609 | * drm_output_prepare_cursor_view. In the event that we go from having |
| 3610 | * a cursor view to not having a cursor view, we need to clear it. */ |
| 3611 | if (output->cursor_view) { |
| 3612 | plane_state = |
| 3613 | drm_output_state_get_existing_plane(state, |
| 3614 | output->cursor_plane); |
| 3615 | if (!plane_state || !plane_state->fb) |
| 3616 | output->cursor_view = NULL; |
| 3617 | } |
| 3618 | } |
| 3619 | |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 3620 | /* |
| 3621 | * Get the aspect-ratio from drmModeModeInfo mode flags. |
| 3622 | * |
| 3623 | * @param drm_mode_flags- flags from drmModeModeInfo structure. |
| 3624 | * @returns aspect-ratio as encoded in enum 'weston_mode_aspect_ratio'. |
| 3625 | */ |
| 3626 | static enum weston_mode_aspect_ratio |
| 3627 | drm_to_weston_mode_aspect_ratio(uint32_t drm_mode_flags) |
| 3628 | { |
| 3629 | return (drm_mode_flags & DRM_MODE_FLAG_PIC_AR_MASK) >> |
| 3630 | DRM_MODE_FLAG_PIC_AR_BITS_POS; |
| 3631 | } |
| 3632 | |
| 3633 | static const char * |
| 3634 | aspect_ratio_to_string(enum weston_mode_aspect_ratio ratio) |
| 3635 | { |
| 3636 | if (ratio < 0 || ratio >= ARRAY_LENGTH(aspect_ratio_as_string) || |
| 3637 | !aspect_ratio_as_string[ratio]) |
| 3638 | return " (unknown aspect ratio)"; |
| 3639 | |
| 3640 | return aspect_ratio_as_string[ratio]; |
| 3641 | } |
| 3642 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 3643 | /** |
| 3644 | * Find the closest-matching mode for a given target |
| 3645 | * |
| 3646 | * Given a target mode, find the most suitable mode amongst the output's |
| 3647 | * current mode list to use, preferring the current mode if possible, to |
| 3648 | * avoid an expensive mode switch. |
| 3649 | * |
| 3650 | * @param output DRM output |
| 3651 | * @param target_mode Mode to attempt to match |
| 3652 | * @returns Pointer to a mode from the output's mode list |
| 3653 | */ |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3654 | static struct drm_mode * |
| 3655 | choose_mode (struct drm_output *output, struct weston_mode *target_mode) |
| 3656 | { |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 3657 | struct drm_mode *tmp_mode = NULL, *mode_fall_back = NULL, *mode; |
| 3658 | enum weston_mode_aspect_ratio src_aspect = WESTON_MODE_PIC_AR_NONE; |
| 3659 | enum weston_mode_aspect_ratio target_aspect = WESTON_MODE_PIC_AR_NONE; |
| 3660 | struct drm_backend *b; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3661 | |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 3662 | b = to_drm_backend(output->base.compositor); |
| 3663 | target_aspect = target_mode->aspect_ratio; |
| 3664 | src_aspect = output->base.current_mode->aspect_ratio; |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 3665 | if (output->base.current_mode->width == target_mode->width && |
| 3666 | output->base.current_mode->height == target_mode->height && |
| 3667 | (output->base.current_mode->refresh == target_mode->refresh || |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 3668 | target_mode->refresh == 0)) { |
| 3669 | if (!b->aspect_ratio_supported || src_aspect == target_aspect) |
| 3670 | return to_drm_mode(output->base.current_mode); |
| 3671 | } |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3672 | |
| 3673 | wl_list_for_each(mode, &output->base.mode_list, base.link) { |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 3674 | |
| 3675 | src_aspect = mode->base.aspect_ratio; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3676 | if (mode->mode_info.hdisplay == target_mode->width && |
| 3677 | mode->mode_info.vdisplay == target_mode->height) { |
Mario Kleiner | 872797c | 2015-06-21 21:25:09 +0200 | [diff] [blame] | 3678 | if (mode->base.refresh == target_mode->refresh || |
| 3679 | target_mode->refresh == 0) { |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 3680 | if (!b->aspect_ratio_supported || |
| 3681 | src_aspect == target_aspect) |
| 3682 | return mode; |
| 3683 | else if (!mode_fall_back) |
| 3684 | mode_fall_back = mode; |
| 3685 | } else if (!tmp_mode) { |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3686 | tmp_mode = mode; |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 3687 | } |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3688 | } |
| 3689 | } |
| 3690 | |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 3691 | if (mode_fall_back) |
| 3692 | return mode_fall_back; |
| 3693 | |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3694 | return tmp_mode; |
| 3695 | } |
| 3696 | |
| 3697 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3698 | drm_output_init_egl(struct drm_output *output, struct drm_backend *b); |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 3699 | static void |
| 3700 | drm_output_fini_egl(struct drm_output *output); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3701 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3702 | drm_output_init_pixman(struct drm_output *output, struct drm_backend *b); |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 3703 | static void |
| 3704 | drm_output_fini_pixman(struct drm_output *output); |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 3705 | |
| 3706 | static int |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3707 | drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode) |
| 3708 | { |
Daniel Stone | 02d487a | 2017-10-07 14:01:45 +0100 | [diff] [blame] | 3709 | struct drm_output *output = to_drm_output(output_base); |
| 3710 | struct drm_backend *b = to_drm_backend(output_base->compositor); |
| 3711 | struct drm_mode *drm_mode = choose_mode(output, mode); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3712 | |
| 3713 | if (!drm_mode) { |
Daniel Stone | 02d487a | 2017-10-07 14:01:45 +0100 | [diff] [blame] | 3714 | weston_log("%s: invalid resolution %dx%d\n", |
| 3715 | output_base->name, mode->width, mode->height); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3716 | return -1; |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 3717 | } |
| 3718 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 3719 | if (&drm_mode->base == output->base.current_mode) |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3720 | return 0; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3721 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 3722 | output->base.current_mode->flags = 0; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3723 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 3724 | output->base.current_mode = &drm_mode->base; |
| 3725 | output->base.current_mode->flags = |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3726 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 3727 | |
Daniel Stone | f30a18c | 2017-04-04 17:54:31 +0100 | [diff] [blame] | 3728 | /* XXX: This drops our current buffer too early, before we've started |
| 3729 | * displaying it. Ideally this should be much more atomic and |
| 3730 | * integrated with a full repaint cycle, rather than doing a |
| 3731 | * sledgehammer modeswitch first, and only later showing new |
| 3732 | * content. |
| 3733 | */ |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 3734 | b->state_invalid = true; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3735 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3736 | if (b->use_pixman) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3737 | drm_output_fini_pixman(output); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3738 | if (drm_output_init_pixman(output, b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3739 | weston_log("failed to init output pixman state with " |
| 3740 | "new mode\n"); |
| 3741 | return -1; |
| 3742 | } |
| 3743 | } else { |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 3744 | drm_output_fini_egl(output); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3745 | if (drm_output_init_egl(output, b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3746 | weston_log("failed to init output egl state with " |
| 3747 | "new mode"); |
| 3748 | return -1; |
| 3749 | } |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 3750 | } |
| 3751 | |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3752 | return 0; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3753 | } |
| 3754 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 3755 | static int |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3756 | on_drm_input(int fd, uint32_t mask, void *data) |
| 3757 | { |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 3758 | #ifdef HAVE_DRM_ATOMIC |
| 3759 | struct drm_backend *b = data; |
| 3760 | #endif |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3761 | drmEventContext evctx; |
| 3762 | |
| 3763 | memset(&evctx, 0, sizeof evctx); |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 3764 | #ifndef HAVE_DRM_ATOMIC |
Emil Velikov | 863e66b | 2017-04-04 18:07:34 +0100 | [diff] [blame] | 3765 | evctx.version = 2; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 3766 | #else |
| 3767 | evctx.version = 3; |
| 3768 | if (b->atomic_modeset) |
| 3769 | evctx.page_flip_handler2 = atomic_flip_handler; |
| 3770 | else |
| 3771 | #endif |
| 3772 | evctx.page_flip_handler = page_flip_handler; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3773 | evctx.vblank_handler = vblank_handler; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3774 | drmHandleEvent(fd, &evctx); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 3775 | |
| 3776 | return 1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3777 | } |
| 3778 | |
| 3779 | static int |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3780 | init_kms_caps(struct drm_backend *b) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3781 | { |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 3782 | uint64_t cap; |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3783 | int ret; |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 3784 | clockid_t clk_id; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 3785 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3786 | weston_log("using %s\n", b->drm.filename); |
Kristian Høgsberg | b71302e | 2012-05-10 12:28:35 -0400 | [diff] [blame] | 3787 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3788 | ret = drmGetCap(b->drm.fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap); |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 3789 | if (ret == 0 && cap == 1) |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 3790 | clk_id = CLOCK_MONOTONIC; |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 3791 | else |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 3792 | clk_id = CLOCK_REALTIME; |
| 3793 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3794 | if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) { |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 3795 | weston_log("Error: failed to set presentation clock %d.\n", |
| 3796 | clk_id); |
| 3797 | return -1; |
| 3798 | } |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 3799 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3800 | ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_WIDTH, &cap); |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 3801 | if (ret == 0) |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3802 | b->cursor_width = cap; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 3803 | else |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3804 | b->cursor_width = 64; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 3805 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3806 | ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_HEIGHT, &cap); |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 3807 | if (ret == 0) |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3808 | b->cursor_height = cap; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 3809 | else |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3810 | b->cursor_height = 64; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 3811 | |
Daniel Stone | be1090b | 2017-09-06 17:29:57 +0100 | [diff] [blame] | 3812 | if (!getenv("WESTON_DISABLE_UNIVERSAL_PLANES")) { |
| 3813 | ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1); |
| 3814 | b->universal_planes = (ret == 0); |
| 3815 | } |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 3816 | weston_log("DRM: %s universal planes\n", |
| 3817 | b->universal_planes ? "supports" : "does not support"); |
| 3818 | |
Pekka Paalanen | cd011a6 | 2016-11-15 22:07:49 +0000 | [diff] [blame] | 3819 | #ifdef HAVE_DRM_ATOMIC |
| 3820 | if (b->universal_planes && !getenv("WESTON_DISABLE_ATOMIC")) { |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 3821 | ret = drmGetCap(b->drm.fd, DRM_CAP_CRTC_IN_VBLANK_EVENT, &cap); |
| 3822 | if (ret != 0) |
| 3823 | cap = 0; |
Pekka Paalanen | cd011a6 | 2016-11-15 22:07:49 +0000 | [diff] [blame] | 3824 | ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_ATOMIC, 1); |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 3825 | b->atomic_modeset = ((ret == 0) && (cap == 1)); |
Pekka Paalanen | cd011a6 | 2016-11-15 22:07:49 +0000 | [diff] [blame] | 3826 | } |
| 3827 | #endif |
| 3828 | weston_log("DRM: %s atomic modesetting\n", |
| 3829 | b->atomic_modeset ? "supports" : "does not support"); |
| 3830 | |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 3831 | ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_ASPECT_RATIO, 1); |
| 3832 | b->aspect_ratio_supported = (ret == 0); |
| 3833 | weston_log("DRM: %s picture aspect ratio\n", |
| 3834 | b->aspect_ratio_supported ? "supports" : "does not support"); |
| 3835 | |
Ander Conselvan de Oliveira | 2292917 | 2013-01-25 15:13:02 +0200 | [diff] [blame] | 3836 | return 0; |
| 3837 | } |
| 3838 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3839 | static struct gbm_device * |
| 3840 | create_gbm_device(int fd) |
Ander Conselvan de Oliveira | 2292917 | 2013-01-25 15:13:02 +0200 | [diff] [blame] | 3841 | { |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3842 | struct gbm_device *gbm; |
Alexandru DAMIAN | be0ac5b | 2013-10-02 17:51:05 +0100 | [diff] [blame] | 3843 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 3844 | gl_renderer = weston_load_module("gl-renderer.so", |
| 3845 | "gl_renderer_interface"); |
| 3846 | if (!gl_renderer) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3847 | return NULL; |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 3848 | |
| 3849 | /* GBM will load a dri driver, but even though they need symbols from |
| 3850 | * libglapi, in some version of Mesa they are not linked to it. Since |
| 3851 | * only the gl-renderer module links to it, the call above won't make |
| 3852 | * these symbols globally available, and loading the DRI driver fails. |
| 3853 | * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */ |
| 3854 | dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL); |
| 3855 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3856 | gbm = gbm_create_device(fd); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3857 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3858 | return gbm; |
| 3859 | } |
| 3860 | |
Bryce Harrington | c056a98 | 2015-05-19 15:25:18 -0700 | [diff] [blame] | 3861 | /* When initializing EGL, if the preferred buffer format isn't available |
Bryce Harrington | b993998 | 2016-04-15 20:28:26 -0700 | [diff] [blame] | 3862 | * 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] | 3863 | * |
| 3864 | * This returns 0 if substitution isn't possible, but 0 might be a |
| 3865 | * legitimate format for other EGL platforms, so the caller is |
| 3866 | * responsible for checking for 0 before calling gl_renderer->create(). |
| 3867 | * |
| 3868 | * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689 |
| 3869 | * but it's entirely possible we'll see this again on other implementations. |
| 3870 | */ |
| 3871 | static int |
| 3872 | fallback_format_for(uint32_t format) |
| 3873 | { |
| 3874 | switch (format) { |
| 3875 | case GBM_FORMAT_XRGB8888: |
| 3876 | return GBM_FORMAT_ARGB8888; |
| 3877 | case GBM_FORMAT_XRGB2101010: |
| 3878 | return GBM_FORMAT_ARGB2101010; |
| 3879 | default: |
| 3880 | return 0; |
| 3881 | } |
| 3882 | } |
| 3883 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3884 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3885 | drm_backend_create_gl_renderer(struct drm_backend *b) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3886 | { |
Derek Foreman | 6d55637 | 2015-11-04 14:47:33 -0600 | [diff] [blame] | 3887 | EGLint format[3] = { |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 3888 | b->gbm_format, |
| 3889 | fallback_format_for(b->gbm_format), |
Derek Foreman | 6d55637 | 2015-11-04 14:47:33 -0600 | [diff] [blame] | 3890 | 0, |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 3891 | }; |
Derek Foreman | 6d55637 | 2015-11-04 14:47:33 -0600 | [diff] [blame] | 3892 | int n_formats = 2; |
John Kåre Alsaker | ef591aa | 2013-03-02 12:27:39 +0100 | [diff] [blame] | 3893 | |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 3894 | if (format[1]) |
Derek Foreman | 6d55637 | 2015-11-04 14:47:33 -0600 | [diff] [blame] | 3895 | n_formats = 3; |
Miguel A. Vico | dddc670 | 2016-05-18 17:41:07 +0200 | [diff] [blame] | 3896 | if (gl_renderer->display_create(b->compositor, |
| 3897 | EGL_PLATFORM_GBM_KHR, |
| 3898 | (void *)b->gbm, |
Miguel A. Vico | 41700e3 | 2016-05-18 17:47:59 +0200 | [diff] [blame] | 3899 | NULL, |
Miguel A. Vico | dddc670 | 2016-05-18 17:41:07 +0200 | [diff] [blame] | 3900 | gl_renderer->opaque_attribs, |
| 3901 | format, |
| 3902 | n_formats) < 0) { |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3903 | return -1; |
| 3904 | } |
| 3905 | |
| 3906 | return 0; |
| 3907 | } |
| 3908 | |
| 3909 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3910 | init_egl(struct drm_backend *b) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3911 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3912 | b->gbm = create_gbm_device(b->drm.fd); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3913 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3914 | if (!b->gbm) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3915 | return -1; |
| 3916 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3917 | if (drm_backend_create_gl_renderer(b) < 0) { |
| 3918 | gbm_device_destroy(b->gbm); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 3919 | return -1; |
| 3920 | } |
| 3921 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3922 | return 0; |
| 3923 | } |
| 3924 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3925 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3926 | init_pixman(struct drm_backend *b) |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3927 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3928 | return pixman_renderer_init(b->compositor); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3929 | } |
| 3930 | |
Sergi Granell | f445622 | 2017-01-12 17:17:32 +0000 | [diff] [blame] | 3931 | #ifdef HAVE_DRM_FORMATS_BLOB |
| 3932 | static inline uint32_t * |
| 3933 | formats_ptr(struct drm_format_modifier_blob *blob) |
| 3934 | { |
| 3935 | return (uint32_t *)(((char *)blob) + blob->formats_offset); |
| 3936 | } |
| 3937 | |
| 3938 | static inline struct drm_format_modifier * |
| 3939 | modifiers_ptr(struct drm_format_modifier_blob *blob) |
| 3940 | { |
| 3941 | return (struct drm_format_modifier *) |
| 3942 | (((char *)blob) + blob->modifiers_offset); |
| 3943 | } |
| 3944 | #endif |
| 3945 | |
| 3946 | /** |
| 3947 | * Populates the plane's formats array, using either the IN_FORMATS blob |
| 3948 | * property (if available), or the plane's format list if not. |
| 3949 | */ |
| 3950 | static int |
| 3951 | drm_plane_populate_formats(struct drm_plane *plane, const drmModePlane *kplane, |
| 3952 | const drmModeObjectProperties *props) |
| 3953 | { |
| 3954 | unsigned i; |
| 3955 | #ifdef HAVE_DRM_FORMATS_BLOB |
| 3956 | drmModePropertyBlobRes *blob; |
| 3957 | struct drm_format_modifier_blob *fmt_mod_blob; |
| 3958 | struct drm_format_modifier *blob_modifiers; |
| 3959 | uint32_t *blob_formats; |
| 3960 | uint32_t blob_id; |
| 3961 | |
| 3962 | blob_id = drm_property_get_value(&plane->props[WDRM_PLANE_IN_FORMATS], |
| 3963 | props, |
| 3964 | 0); |
| 3965 | if (blob_id == 0) |
| 3966 | goto fallback; |
| 3967 | |
| 3968 | blob = drmModeGetPropertyBlob(plane->backend->drm.fd, blob_id); |
| 3969 | if (!blob) |
| 3970 | goto fallback; |
| 3971 | |
| 3972 | fmt_mod_blob = blob->data; |
| 3973 | blob_formats = formats_ptr(fmt_mod_blob); |
| 3974 | blob_modifiers = modifiers_ptr(fmt_mod_blob); |
| 3975 | |
| 3976 | if (plane->count_formats != fmt_mod_blob->count_formats) { |
| 3977 | weston_log("DRM backend: format count differs between " |
| 3978 | "plane (%d) and IN_FORMATS (%d)\n", |
| 3979 | plane->count_formats, |
| 3980 | fmt_mod_blob->count_formats); |
| 3981 | weston_log("This represents a kernel bug; Weston is " |
| 3982 | "unable to continue.\n"); |
| 3983 | abort(); |
| 3984 | } |
| 3985 | |
| 3986 | for (i = 0; i < fmt_mod_blob->count_formats; i++) { |
| 3987 | uint32_t count_modifiers = 0; |
| 3988 | uint64_t *modifiers = NULL; |
| 3989 | unsigned j; |
| 3990 | |
| 3991 | for (j = 0; j < fmt_mod_blob->count_modifiers; j++) { |
| 3992 | struct drm_format_modifier *mod = &blob_modifiers[j]; |
| 3993 | |
| 3994 | if ((i < mod->offset) || (i > mod->offset + 63)) |
| 3995 | continue; |
| 3996 | if (!(mod->formats & (1 << (i - mod->offset)))) |
| 3997 | continue; |
| 3998 | |
| 3999 | modifiers = realloc(modifiers, |
| 4000 | (count_modifiers + 1) * |
| 4001 | sizeof(modifiers[0])); |
| 4002 | assert(modifiers); |
| 4003 | modifiers[count_modifiers++] = mod->modifier; |
| 4004 | } |
| 4005 | |
| 4006 | plane->formats[i].format = blob_formats[i]; |
| 4007 | plane->formats[i].modifiers = modifiers; |
| 4008 | plane->formats[i].count_modifiers = count_modifiers; |
| 4009 | } |
| 4010 | |
| 4011 | drmModeFreePropertyBlob(blob); |
| 4012 | |
| 4013 | return 0; |
| 4014 | |
| 4015 | fallback: |
| 4016 | #endif |
| 4017 | /* No IN_FORMATS blob available, so just use the old. */ |
| 4018 | assert(plane->count_formats == kplane->count_formats); |
| 4019 | for (i = 0; i < kplane->count_formats; i++) |
| 4020 | plane->formats[i].format = kplane->formats[i]; |
| 4021 | |
| 4022 | return 0; |
| 4023 | } |
| 4024 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4025 | /** |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 4026 | * Create a drm_plane for a hardware plane |
| 4027 | * |
| 4028 | * Creates one drm_plane structure for a hardware plane, and initialises its |
| 4029 | * properties and formats. |
| 4030 | * |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 4031 | * In the absence of universal plane support, where KMS does not explicitly |
| 4032 | * expose the primary and cursor planes to userspace, this may also create |
| 4033 | * an 'internal' plane for internal management. |
| 4034 | * |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 4035 | * This function does not add the plane to the list of usable planes in Weston |
| 4036 | * itself; the caller is responsible for this. |
| 4037 | * |
| 4038 | * Call drm_plane_destroy to clean up the plane. |
| 4039 | * |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 4040 | * @sa drm_output_find_special_plane |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 4041 | * @param b DRM compositor backend |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 4042 | * @param kplane DRM plane to create, or NULL if creating internal plane |
| 4043 | * @param output Output to create internal plane for, or NULL |
| 4044 | * @param type Type to use when creating internal plane, or invalid |
| 4045 | * @param format Format to use for internal planes, or 0 |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 4046 | */ |
| 4047 | static struct drm_plane * |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 4048 | drm_plane_create(struct drm_backend *b, const drmModePlane *kplane, |
| 4049 | struct drm_output *output, enum wdrm_plane_type type, |
| 4050 | uint32_t format) |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 4051 | { |
| 4052 | struct drm_plane *plane; |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 4053 | drmModeObjectProperties *props; |
Sergi Granell | f445622 | 2017-01-12 17:17:32 +0000 | [diff] [blame] | 4054 | uint32_t num_formats = (kplane) ? kplane->count_formats : 1; |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 4055 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 4056 | plane = zalloc(sizeof(*plane) + |
Sergi Granell | f445622 | 2017-01-12 17:17:32 +0000 | [diff] [blame] | 4057 | (sizeof(plane->formats[0]) * num_formats)); |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 4058 | if (!plane) { |
| 4059 | weston_log("%s: out of memory\n", __func__); |
| 4060 | return NULL; |
| 4061 | } |
| 4062 | |
| 4063 | plane->backend = b; |
Sergi Granell | f445622 | 2017-01-12 17:17:32 +0000 | [diff] [blame] | 4064 | plane->count_formats = num_formats; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 4065 | plane->state_cur = drm_plane_state_alloc(NULL, plane); |
| 4066 | plane->state_cur->complete = true; |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 4067 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 4068 | if (kplane) { |
| 4069 | plane->possible_crtcs = kplane->possible_crtcs; |
| 4070 | plane->plane_id = kplane->plane_id; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 4071 | |
| 4072 | props = drmModeObjectGetProperties(b->drm.fd, kplane->plane_id, |
| 4073 | DRM_MODE_OBJECT_PLANE); |
| 4074 | if (!props) { |
| 4075 | weston_log("couldn't get plane properties\n"); |
| 4076 | goto err; |
| 4077 | } |
| 4078 | drm_property_info_populate(b, plane_props, plane->props, |
| 4079 | WDRM_PLANE__COUNT, props); |
| 4080 | plane->type = |
| 4081 | drm_property_get_value(&plane->props[WDRM_PLANE_TYPE], |
| 4082 | props, |
| 4083 | WDRM_PLANE_TYPE__COUNT); |
Sergi Granell | f445622 | 2017-01-12 17:17:32 +0000 | [diff] [blame] | 4084 | |
| 4085 | if (drm_plane_populate_formats(plane, kplane, props) < 0) { |
| 4086 | drmModeFreeObjectProperties(props); |
| 4087 | goto err; |
| 4088 | } |
| 4089 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 4090 | drmModeFreeObjectProperties(props); |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 4091 | } |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 4092 | else { |
| 4093 | plane->possible_crtcs = (1 << output->pipe); |
| 4094 | plane->plane_id = 0; |
| 4095 | plane->count_formats = 1; |
Sergi Granell | f445622 | 2017-01-12 17:17:32 +0000 | [diff] [blame] | 4096 | plane->formats[0].format = format; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 4097 | plane->type = type; |
| 4098 | } |
| 4099 | |
| 4100 | if (plane->type == WDRM_PLANE_TYPE__COUNT) |
| 4101 | goto err_props; |
| 4102 | |
| 4103 | /* With universal planes, everything is a DRM plane; without |
| 4104 | * universal planes, the only DRM planes are overlay planes. |
| 4105 | * Everything else is a fake plane. */ |
| 4106 | if (b->universal_planes) { |
| 4107 | assert(kplane); |
| 4108 | } else { |
| 4109 | if (kplane) |
| 4110 | assert(plane->type == WDRM_PLANE_TYPE_OVERLAY); |
| 4111 | else |
| 4112 | assert(plane->type != WDRM_PLANE_TYPE_OVERLAY && |
| 4113 | output); |
| 4114 | } |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 4115 | |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 4116 | weston_plane_init(&plane->base, b->compositor, 0, 0); |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 4117 | wl_list_insert(&b->plane_list, &plane->link); |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 4118 | |
| 4119 | return plane; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 4120 | |
| 4121 | err_props: |
| 4122 | drm_property_info_free(plane->props, WDRM_PLANE__COUNT); |
| 4123 | err: |
| 4124 | drm_plane_state_free(plane->state_cur, true); |
| 4125 | free(plane); |
| 4126 | return NULL; |
| 4127 | } |
| 4128 | |
| 4129 | /** |
| 4130 | * Find, or create, a special-purpose plane |
| 4131 | * |
| 4132 | * Primary and cursor planes are a special case, in that before universal |
| 4133 | * planes, they are driven by non-plane API calls. Without universal plane |
| 4134 | * support, the only way to configure a primary plane is via drmModeSetCrtc, |
| 4135 | * and the only way to configure a cursor plane is drmModeSetCursor2. |
| 4136 | * |
| 4137 | * Although they may actually be regular planes in the hardware, without |
| 4138 | * universal plane support, these planes are not actually exposed to |
| 4139 | * userspace in the regular plane list. |
| 4140 | * |
| 4141 | * However, for ease of internal tracking, we want to manage all planes |
| 4142 | * through the same drm_plane structures. Therefore, when we are running |
| 4143 | * without universal plane support, we create fake drm_plane structures |
| 4144 | * to track these planes. |
| 4145 | * |
| 4146 | * @param b DRM backend |
| 4147 | * @param output Output to use for plane |
| 4148 | * @param type Type of plane |
| 4149 | */ |
| 4150 | static struct drm_plane * |
| 4151 | drm_output_find_special_plane(struct drm_backend *b, struct drm_output *output, |
| 4152 | enum wdrm_plane_type type) |
| 4153 | { |
| 4154 | struct drm_plane *plane; |
| 4155 | |
| 4156 | if (!b->universal_planes) { |
| 4157 | uint32_t format; |
| 4158 | |
| 4159 | switch (type) { |
| 4160 | case WDRM_PLANE_TYPE_CURSOR: |
| 4161 | format = GBM_FORMAT_ARGB8888; |
| 4162 | break; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 4163 | case WDRM_PLANE_TYPE_PRIMARY: |
| 4164 | /* We don't know what formats the primary plane supports |
| 4165 | * before universal planes, so we just assume that the |
| 4166 | * GBM format works; however, this isn't set until after |
| 4167 | * the output is created. */ |
| 4168 | format = 0; |
| 4169 | break; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 4170 | default: |
| 4171 | assert(!"invalid type in drm_output_find_special_plane"); |
| 4172 | break; |
| 4173 | } |
| 4174 | |
| 4175 | return drm_plane_create(b, NULL, output, type, format); |
| 4176 | } |
| 4177 | |
| 4178 | wl_list_for_each(plane, &b->plane_list, link) { |
| 4179 | struct drm_output *tmp; |
| 4180 | bool found_elsewhere = false; |
| 4181 | |
| 4182 | if (plane->type != type) |
| 4183 | continue; |
| 4184 | if (!drm_plane_is_available(plane, output)) |
| 4185 | continue; |
| 4186 | |
| 4187 | /* On some platforms, primary/cursor planes can roam |
| 4188 | * between different CRTCs, so make sure we don't claim the |
| 4189 | * same plane for two outputs. */ |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 4190 | wl_list_for_each(tmp, &b->compositor->output_list, |
| 4191 | base.link) { |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 4192 | if (tmp->cursor_plane == plane || |
| 4193 | tmp->scanout_plane == plane) { |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 4194 | found_elsewhere = true; |
| 4195 | break; |
| 4196 | } |
| 4197 | } |
| 4198 | |
| 4199 | if (found_elsewhere) |
| 4200 | continue; |
| 4201 | |
| 4202 | plane->possible_crtcs = (1 << output->pipe); |
| 4203 | return plane; |
| 4204 | } |
| 4205 | |
| 4206 | return NULL; |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 4207 | } |
| 4208 | |
| 4209 | /** |
| 4210 | * Destroy one DRM plane |
| 4211 | * |
| 4212 | * Destroy a DRM plane, removing it from screen and releasing its retained |
| 4213 | * buffers in the process. The counterpart to drm_plane_create. |
| 4214 | * |
| 4215 | * @param plane Plane to deallocate (will be freed) |
| 4216 | */ |
| 4217 | static void |
| 4218 | drm_plane_destroy(struct drm_plane *plane) |
| 4219 | { |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 4220 | if (plane->type == WDRM_PLANE_TYPE_OVERLAY) |
| 4221 | drmModeSetPlane(plane->backend->drm.fd, plane->plane_id, |
| 4222 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 4223 | drm_plane_state_free(plane->state_cur, true); |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 4224 | drm_property_info_free(plane->props, WDRM_PLANE__COUNT); |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 4225 | weston_plane_release(&plane->base); |
| 4226 | wl_list_remove(&plane->link); |
| 4227 | free(plane); |
| 4228 | } |
| 4229 | |
| 4230 | /** |
| 4231 | * Initialise sprites (overlay planes) |
| 4232 | * |
| 4233 | * Walk the list of provided DRM planes, and add overlay planes. |
| 4234 | * |
| 4235 | * Call destroy_sprites to free these planes. |
| 4236 | * |
| 4237 | * @param b DRM compositor backend |
| 4238 | */ |
| 4239 | static void |
| 4240 | create_sprites(struct drm_backend *b) |
| 4241 | { |
| 4242 | drmModePlaneRes *kplane_res; |
| 4243 | drmModePlane *kplane; |
| 4244 | struct drm_plane *drm_plane; |
| 4245 | uint32_t i; |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 4246 | kplane_res = drmModeGetPlaneResources(b->drm.fd); |
| 4247 | if (!kplane_res) { |
| 4248 | weston_log("failed to get plane resources: %s\n", |
| 4249 | strerror(errno)); |
| 4250 | return; |
| 4251 | } |
| 4252 | |
| 4253 | for (i = 0; i < kplane_res->count_planes; i++) { |
| 4254 | kplane = drmModeGetPlane(b->drm.fd, kplane_res->planes[i]); |
| 4255 | if (!kplane) |
| 4256 | continue; |
| 4257 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 4258 | drm_plane = drm_plane_create(b, kplane, NULL, |
| 4259 | WDRM_PLANE_TYPE__COUNT, 0); |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 4260 | drmModeFreePlane(kplane); |
| 4261 | if (!drm_plane) |
| 4262 | continue; |
| 4263 | |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 4264 | if (drm_plane->type == WDRM_PLANE_TYPE_OVERLAY) |
| 4265 | weston_compositor_stack_plane(b->compositor, |
| 4266 | &drm_plane->base, |
| 4267 | &b->compositor->primary_plane); |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 4268 | } |
| 4269 | |
| 4270 | drmModeFreePlaneResources(kplane_res); |
| 4271 | } |
| 4272 | |
| 4273 | /** |
| 4274 | * Clean up sprites (overlay planes) |
| 4275 | * |
| 4276 | * The counterpart to create_sprites. |
| 4277 | * |
| 4278 | * @param b DRM compositor backend |
| 4279 | */ |
| 4280 | static void |
| 4281 | destroy_sprites(struct drm_backend *b) |
| 4282 | { |
| 4283 | struct drm_plane *plane, *next; |
| 4284 | |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 4285 | wl_list_for_each_safe(plane, next, &b->plane_list, link) |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 4286 | drm_plane_destroy(plane); |
| 4287 | } |
| 4288 | |
Pekka Paalanen | dc14fd4 | 2017-11-10 15:31:39 +0200 | [diff] [blame] | 4289 | static uint32_t |
| 4290 | drm_refresh_rate_mHz(const drmModeModeInfo *info) |
| 4291 | { |
| 4292 | uint64_t refresh; |
| 4293 | |
| 4294 | /* Calculate higher precision (mHz) refresh rate */ |
| 4295 | refresh = (info->clock * 1000000LL / info->htotal + |
| 4296 | info->vtotal / 2) / info->vtotal; |
| 4297 | |
| 4298 | if (info->flags & DRM_MODE_FLAG_INTERLACE) |
| 4299 | refresh *= 2; |
| 4300 | if (info->flags & DRM_MODE_FLAG_DBLSCAN) |
| 4301 | refresh /= 2; |
| 4302 | if (info->vscan > 1) |
| 4303 | refresh /= info->vscan; |
| 4304 | |
| 4305 | return refresh; |
| 4306 | } |
| 4307 | |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 4308 | /** |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4309 | * Add a mode to output's mode list |
| 4310 | * |
| 4311 | * Copy the supplied DRM mode into a Weston mode structure, and add it to the |
| 4312 | * output's mode list. |
| 4313 | * |
| 4314 | * @param output DRM output to add mode to |
| 4315 | * @param info DRM mode structure to add |
| 4316 | * @returns Newly-allocated Weston/DRM mode structure |
| 4317 | */ |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 4318 | static struct drm_mode * |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4319 | drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info) |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 4320 | { |
| 4321 | struct drm_mode *mode; |
| 4322 | |
| 4323 | mode = malloc(sizeof *mode); |
| 4324 | if (mode == NULL) |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 4325 | return NULL; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 4326 | |
| 4327 | mode->base.flags = 0; |
Alexander Larsson | 0b13506 | 2013-05-28 16:23:36 +0200 | [diff] [blame] | 4328 | mode->base.width = info->hdisplay; |
| 4329 | mode->base.height = info->vdisplay; |
Kristian Høgsberg | c4621b0 | 2012-05-10 12:23:53 -0400 | [diff] [blame] | 4330 | |
Pekka Paalanen | dc14fd4 | 2017-11-10 15:31:39 +0200 | [diff] [blame] | 4331 | mode->base.refresh = drm_refresh_rate_mHz(info); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 4332 | mode->mode_info = *info; |
Daniel Stone | d5526cb | 2016-11-16 10:54:10 +0000 | [diff] [blame] | 4333 | mode->blob_id = 0; |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 4334 | |
| 4335 | if (info->type & DRM_MODE_TYPE_PREFERRED) |
| 4336 | mode->base.flags |= WL_OUTPUT_MODE_PREFERRED; |
| 4337 | |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 4338 | mode->base.aspect_ratio = drm_to_weston_mode_aspect_ratio(info->flags); |
| 4339 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 4340 | wl_list_insert(output->base.mode_list.prev, &mode->base.link); |
| 4341 | |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 4342 | return mode; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 4343 | } |
| 4344 | |
Daniel Stone | d5526cb | 2016-11-16 10:54:10 +0000 | [diff] [blame] | 4345 | /** |
| 4346 | * Destroys a mode, and removes it from the list. |
| 4347 | */ |
| 4348 | static void |
| 4349 | drm_output_destroy_mode(struct drm_backend *backend, struct drm_mode *mode) |
| 4350 | { |
| 4351 | if (mode->blob_id) |
| 4352 | drmModeDestroyPropertyBlob(backend->drm.fd, mode->blob_id); |
| 4353 | wl_list_remove(&mode->base.link); |
| 4354 | free(mode); |
| 4355 | } |
| 4356 | |
Pekka Paalanen | 383b3af | 2017-09-11 14:40:48 +0300 | [diff] [blame] | 4357 | /** Destroy a list of drm_modes |
| 4358 | * |
| 4359 | * @param backend The backend for releasing mode property blobs. |
| 4360 | * @param mode_list The list linked by drm_mode::base.link. |
| 4361 | */ |
| 4362 | static void |
| 4363 | drm_mode_list_destroy(struct drm_backend *backend, struct wl_list *mode_list) |
| 4364 | { |
| 4365 | struct drm_mode *mode, *next; |
| 4366 | |
| 4367 | wl_list_for_each_safe(mode, next, mode_list, base.link) |
| 4368 | drm_output_destroy_mode(backend, mode); |
| 4369 | } |
| 4370 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 4371 | static int |
| 4372 | drm_subpixel_to_wayland(int drm_value) |
| 4373 | { |
| 4374 | switch (drm_value) { |
| 4375 | default: |
| 4376 | case DRM_MODE_SUBPIXEL_UNKNOWN: |
| 4377 | return WL_OUTPUT_SUBPIXEL_UNKNOWN; |
| 4378 | case DRM_MODE_SUBPIXEL_NONE: |
| 4379 | return WL_OUTPUT_SUBPIXEL_NONE; |
| 4380 | case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB: |
| 4381 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB; |
| 4382 | case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR: |
| 4383 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR; |
| 4384 | case DRM_MODE_SUBPIXEL_VERTICAL_RGB: |
| 4385 | return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB; |
| 4386 | case DRM_MODE_SUBPIXEL_VERTICAL_BGR: |
| 4387 | return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR; |
| 4388 | } |
| 4389 | } |
| 4390 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 4391 | /* returns a value between 0-255 range, where higher is brighter */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 4392 | static uint32_t |
Pekka Paalanen | ce72424 | 2017-09-04 12:21:24 +0300 | [diff] [blame] | 4393 | drm_get_backlight(struct drm_head *head) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 4394 | { |
| 4395 | long brightness, max_brightness, norm; |
| 4396 | |
Pekka Paalanen | ce72424 | 2017-09-04 12:21:24 +0300 | [diff] [blame] | 4397 | brightness = backlight_get_brightness(head->backlight); |
| 4398 | max_brightness = backlight_get_max_brightness(head->backlight); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 4399 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 4400 | /* convert it on a scale of 0 to 255 */ |
| 4401 | norm = (brightness * 255)/(max_brightness); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 4402 | |
| 4403 | return (uint32_t) norm; |
| 4404 | } |
| 4405 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 4406 | /* values accepted are between 0-255 range */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 4407 | static void |
| 4408 | drm_set_backlight(struct weston_output *output_base, uint32_t value) |
| 4409 | { |
Pekka Paalanen | ecc8cce | 2017-09-12 16:14:31 +0300 | [diff] [blame] | 4410 | struct drm_output *output = to_drm_output(output_base); |
| 4411 | struct drm_head *head; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 4412 | long max_brightness, new_brightness; |
| 4413 | |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 4414 | if (value > 255) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 4415 | return; |
| 4416 | |
Pekka Paalanen | ecc8cce | 2017-09-12 16:14:31 +0300 | [diff] [blame] | 4417 | wl_list_for_each(head, &output->base.head_list, base.output_link) { |
| 4418 | if (!head->backlight) |
| 4419 | return; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 4420 | |
Pekka Paalanen | ecc8cce | 2017-09-12 16:14:31 +0300 | [diff] [blame] | 4421 | max_brightness = backlight_get_max_brightness(head->backlight); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 4422 | |
Pekka Paalanen | ecc8cce | 2017-09-12 16:14:31 +0300 | [diff] [blame] | 4423 | /* get denormalized value */ |
| 4424 | new_brightness = (value * max_brightness) / 255; |
| 4425 | |
| 4426 | backlight_set_brightness(head->backlight, new_brightness); |
| 4427 | } |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 4428 | } |
| 4429 | |
Pekka Paalanen | f8b850d | 2017-11-15 12:51:01 +0200 | [diff] [blame] | 4430 | static void |
| 4431 | drm_output_init_backlight(struct drm_output *output) |
| 4432 | { |
| 4433 | struct weston_head *base; |
| 4434 | struct drm_head *head; |
| 4435 | |
| 4436 | output->base.set_backlight = NULL; |
| 4437 | |
| 4438 | wl_list_for_each(base, &output->base.head_list, output_link) { |
| 4439 | head = to_drm_head(base); |
| 4440 | |
| 4441 | if (head->backlight) { |
| 4442 | weston_log("Initialized backlight for head '%s', device %s\n", |
| 4443 | head->base.name, head->backlight->path); |
| 4444 | |
| 4445 | if (!output->base.set_backlight) { |
| 4446 | output->base.set_backlight = drm_set_backlight; |
| 4447 | output->base.backlight_current = |
| 4448 | drm_get_backlight(head); |
| 4449 | } |
| 4450 | } |
| 4451 | } |
| 4452 | |
| 4453 | if (!output->base.set_backlight) { |
| 4454 | weston_log("No backlight control for output '%s'\n", |
| 4455 | output->base.name); |
| 4456 | } |
| 4457 | } |
| 4458 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 4459 | /** |
| 4460 | * Power output on or off |
| 4461 | * |
| 4462 | * The DPMS/power level of an output is used to switch it on or off. This |
| 4463 | * is DRM's hook for doing so, which can called either as part of repaint, |
| 4464 | * or independently of the repaint loop. |
| 4465 | * |
| 4466 | * If we are called as part of repaint, we simply set the relevant bit in |
| 4467 | * state and return. |
| 4468 | */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 4469 | static void |
| 4470 | drm_set_dpms(struct weston_output *output_base, enum dpms_enum level) |
| 4471 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 4472 | struct drm_output *output = to_drm_output(output_base); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 4473 | struct drm_backend *b = to_drm_backend(output_base->compositor); |
| 4474 | struct drm_pending_state *pending_state = b->repaint_data; |
| 4475 | struct drm_output_state *state; |
Daniel Stone | 36609c7 | 2015-06-18 07:49:02 +0100 | [diff] [blame] | 4476 | int ret; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 4477 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 4478 | if (output->state_cur->dpms == level) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 4479 | return; |
| 4480 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 4481 | /* If we're being called during the repaint loop, then this is |
| 4482 | * simple: discard any previously-generated state, and create a new |
| 4483 | * state where we disable everything. When we come to flush, this |
| 4484 | * will be applied. |
| 4485 | * |
| 4486 | * However, we need to be careful: we can be called whilst another |
| 4487 | * output is in its repaint cycle (pending_state exists), but our |
| 4488 | * output still has an incomplete state application outstanding. |
| 4489 | * In that case, we need to wait until that completes. */ |
| 4490 | if (pending_state && !output->state_last) { |
| 4491 | /* The repaint loop already sets DPMS on; we don't need to |
| 4492 | * explicitly set it on here, as it will already happen |
| 4493 | * whilst applying the repaint state. */ |
| 4494 | if (level == WESTON_DPMS_ON) |
| 4495 | return; |
| 4496 | |
| 4497 | state = drm_pending_state_get_output(pending_state, output); |
| 4498 | if (state) |
| 4499 | drm_output_state_free(state); |
| 4500 | state = drm_output_get_disable_state(pending_state, output); |
Daniel Stone | 36609c7 | 2015-06-18 07:49:02 +0100 | [diff] [blame] | 4501 | return; |
| 4502 | } |
| 4503 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 4504 | /* As we throw everything away when disabling, just send us back through |
| 4505 | * a repaint cycle. */ |
| 4506 | if (level == WESTON_DPMS_ON) { |
| 4507 | if (output->dpms_off_pending) |
| 4508 | output->dpms_off_pending = 0; |
| 4509 | weston_output_schedule_repaint(output_base); |
| 4510 | return; |
| 4511 | } |
| 4512 | |
| 4513 | /* If we've already got a request in the pipeline, then we need to |
| 4514 | * park our DPMS request until that request has quiesced. */ |
| 4515 | if (output->state_last) { |
| 4516 | output->dpms_off_pending = 1; |
| 4517 | return; |
| 4518 | } |
| 4519 | |
| 4520 | pending_state = drm_pending_state_alloc(b); |
| 4521 | drm_output_get_disable_state(pending_state, output); |
| 4522 | ret = drm_pending_state_apply_sync(pending_state); |
| 4523 | if (ret != 0) |
| 4524 | weston_log("drm_set_dpms: couldn't disable output?\n"); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 4525 | } |
| 4526 | |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 4527 | static const char * const connector_type_names[] = { |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 4528 | [DRM_MODE_CONNECTOR_Unknown] = "Unknown", |
| 4529 | [DRM_MODE_CONNECTOR_VGA] = "VGA", |
| 4530 | [DRM_MODE_CONNECTOR_DVII] = "DVI-I", |
| 4531 | [DRM_MODE_CONNECTOR_DVID] = "DVI-D", |
| 4532 | [DRM_MODE_CONNECTOR_DVIA] = "DVI-A", |
| 4533 | [DRM_MODE_CONNECTOR_Composite] = "Composite", |
| 4534 | [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO", |
| 4535 | [DRM_MODE_CONNECTOR_LVDS] = "LVDS", |
| 4536 | [DRM_MODE_CONNECTOR_Component] = "Component", |
| 4537 | [DRM_MODE_CONNECTOR_9PinDIN] = "DIN", |
| 4538 | [DRM_MODE_CONNECTOR_DisplayPort] = "DP", |
| 4539 | [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A", |
| 4540 | [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B", |
| 4541 | [DRM_MODE_CONNECTOR_TV] = "TV", |
| 4542 | [DRM_MODE_CONNECTOR_eDP] = "eDP", |
Pekka Paalanen | ab81f15 | 2015-08-24 14:27:07 +0300 | [diff] [blame] | 4543 | #ifdef DRM_MODE_CONNECTOR_DSI |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 4544 | [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual", |
| 4545 | [DRM_MODE_CONNECTOR_DSI] = "DSI", |
Pekka Paalanen | ab81f15 | 2015-08-24 14:27:07 +0300 | [diff] [blame] | 4546 | #endif |
Kristian Høgsberg | 2f9ed71 | 2012-07-26 17:57:15 -0400 | [diff] [blame] | 4547 | }; |
| 4548 | |
Pekka Paalanen | 1f21ef1 | 2017-04-03 13:33:26 +0300 | [diff] [blame] | 4549 | /** Create a name given a DRM connector |
| 4550 | * |
| 4551 | * \param con The DRM connector whose type and id form the name. |
| 4552 | * \return A newly allocate string, or NULL on error. Must be free()'d |
| 4553 | * after use. |
| 4554 | * |
| 4555 | * The name does not identify the DRM display device. |
| 4556 | */ |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 4557 | static char * |
| 4558 | make_connector_name(const drmModeConnector *con) |
| 4559 | { |
Pekka Paalanen | 1f21ef1 | 2017-04-03 13:33:26 +0300 | [diff] [blame] | 4560 | char *name; |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 4561 | const char *type_name = NULL; |
Pekka Paalanen | 1f21ef1 | 2017-04-03 13:33:26 +0300 | [diff] [blame] | 4562 | int ret; |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 4563 | |
| 4564 | if (con->connector_type < ARRAY_LENGTH(connector_type_names)) |
| 4565 | type_name = connector_type_names[con->connector_type]; |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 4566 | |
| 4567 | if (!type_name) |
| 4568 | type_name = "UNNAMED"; |
| 4569 | |
Pekka Paalanen | 1f21ef1 | 2017-04-03 13:33:26 +0300 | [diff] [blame] | 4570 | ret = asprintf(&name, "%s-%d", type_name, con->connector_type_id); |
| 4571 | if (ret < 0) |
| 4572 | return NULL; |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 4573 | |
Pekka Paalanen | 1f21ef1 | 2017-04-03 13:33:26 +0300 | [diff] [blame] | 4574 | return name; |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 4575 | } |
| 4576 | |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 4577 | static void drm_output_fini_cursor_egl(struct drm_output *output) |
| 4578 | { |
| 4579 | unsigned int i; |
| 4580 | |
| 4581 | for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) { |
| 4582 | drm_fb_unref(output->gbm_cursor_fb[i]); |
| 4583 | output->gbm_cursor_fb[i] = NULL; |
| 4584 | } |
| 4585 | } |
| 4586 | |
| 4587 | static int |
| 4588 | drm_output_init_cursor_egl(struct drm_output *output, struct drm_backend *b) |
| 4589 | { |
| 4590 | unsigned int i; |
| 4591 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 4592 | /* No point creating cursors if we don't have a plane for them. */ |
| 4593 | if (!output->cursor_plane) |
| 4594 | return 0; |
| 4595 | |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 4596 | for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) { |
| 4597 | struct gbm_bo *bo; |
| 4598 | |
| 4599 | bo = gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height, |
| 4600 | GBM_FORMAT_ARGB8888, |
| 4601 | GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE); |
| 4602 | if (!bo) |
| 4603 | goto err; |
| 4604 | |
| 4605 | output->gbm_cursor_fb[i] = |
Daniel Stone | db10df1 | 2016-12-08 13:15:58 +0000 | [diff] [blame] | 4606 | drm_fb_get_from_bo(bo, b, false, BUFFER_CURSOR); |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 4607 | if (!output->gbm_cursor_fb[i]) { |
| 4608 | gbm_bo_destroy(bo); |
| 4609 | goto err; |
| 4610 | } |
| 4611 | } |
| 4612 | |
| 4613 | return 0; |
| 4614 | |
| 4615 | err: |
| 4616 | weston_log("cursor buffers unavailable, using gl cursors\n"); |
| 4617 | b->cursors_are_broken = 1; |
| 4618 | drm_output_fini_cursor_egl(output); |
| 4619 | return -1; |
| 4620 | } |
| 4621 | |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 4622 | /* Init output state that depends on gl or gbm */ |
| 4623 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 4624 | 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] | 4625 | { |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 4626 | EGLint format[2] = { |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 4627 | output->gbm_format, |
| 4628 | fallback_format_for(output->gbm_format), |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 4629 | }; |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 4630 | int n_formats = 1; |
Daniel Stone | 244244d | 2016-11-18 18:02:08 +0000 | [diff] [blame] | 4631 | struct weston_mode *mode = output->base.current_mode; |
| 4632 | struct drm_plane *plane = output->scanout_plane; |
| 4633 | unsigned int i; |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 4634 | |
Daniel Stone | 244244d | 2016-11-18 18:02:08 +0000 | [diff] [blame] | 4635 | for (i = 0; i < plane->count_formats; i++) { |
| 4636 | if (plane->formats[i].format == output->gbm_format) |
| 4637 | break; |
| 4638 | } |
| 4639 | |
| 4640 | if (i == plane->count_formats) { |
| 4641 | weston_log("format 0x%x not supported by output %s\n", |
| 4642 | output->gbm_format, output->base.name); |
| 4643 | return -1; |
| 4644 | } |
| 4645 | |
| 4646 | #ifdef HAVE_GBM_MODIFIERS |
| 4647 | if (plane->formats[i].count_modifiers > 0) { |
| 4648 | output->gbm_surface = |
| 4649 | gbm_surface_create_with_modifiers(b->gbm, |
| 4650 | mode->width, |
| 4651 | mode->height, |
| 4652 | output->gbm_format, |
| 4653 | plane->formats[i].modifiers, |
| 4654 | plane->formats[i].count_modifiers); |
| 4655 | } else |
| 4656 | #endif |
| 4657 | { |
| 4658 | output->gbm_surface = |
| 4659 | gbm_surface_create(b->gbm, mode->width, mode->height, |
| 4660 | output->gbm_format, |
| 4661 | GBM_BO_USE_RENDERING | GBM_BO_USE_SCANOUT); |
| 4662 | } |
| 4663 | |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 4664 | if (!output->gbm_surface) { |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 4665 | weston_log("failed to create gbm surface\n"); |
| 4666 | return -1; |
| 4667 | } |
| 4668 | |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 4669 | if (format[1]) |
| 4670 | n_formats = 2; |
Miguel A. Vico | c095cde | 2016-05-18 17:43:00 +0200 | [diff] [blame] | 4671 | if (gl_renderer->output_window_create(&output->base, |
| 4672 | (EGLNativeWindowType)output->gbm_surface, |
| 4673 | output->gbm_surface, |
| 4674 | gl_renderer->opaque_attribs, |
| 4675 | format, |
| 4676 | n_formats) < 0) { |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 4677 | weston_log("failed to create gl renderer output state\n"); |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 4678 | gbm_surface_destroy(output->gbm_surface); |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 4679 | return -1; |
| 4680 | } |
| 4681 | |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 4682 | drm_output_init_cursor_egl(output, b); |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 4683 | |
| 4684 | return 0; |
| 4685 | } |
| 4686 | |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 4687 | static void |
| 4688 | drm_output_fini_egl(struct drm_output *output) |
| 4689 | { |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 4690 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
| 4691 | |
| 4692 | /* Destroying the GBM surface will destroy all our GBM buffers, |
| 4693 | * regardless of refcount. Ensure we destroy them here. */ |
| 4694 | if (!b->shutting_down && |
| 4695 | output->scanout_plane->state_cur->fb && |
| 4696 | output->scanout_plane->state_cur->fb->type == BUFFER_GBM_SURFACE) { |
| 4697 | drm_plane_state_free(output->scanout_plane->state_cur, true); |
| 4698 | output->scanout_plane->state_cur = |
| 4699 | drm_plane_state_alloc(NULL, output->scanout_plane); |
| 4700 | output->scanout_plane->state_cur->complete = true; |
| 4701 | } |
| 4702 | |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 4703 | gl_renderer->output_destroy(&output->base); |
| 4704 | gbm_surface_destroy(output->gbm_surface); |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 4705 | drm_output_fini_cursor_egl(output); |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 4706 | } |
| 4707 | |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 4708 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 4709 | 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] | 4710 | { |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 4711 | int w = output->base.current_mode->width; |
| 4712 | int h = output->base.current_mode->height; |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 4713 | uint32_t format = output->gbm_format; |
| 4714 | uint32_t pixman_format; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4715 | unsigned int i; |
Pekka Paalanen | dee412d | 2018-04-23 11:44:58 +0200 | [diff] [blame] | 4716 | uint32_t flags = 0; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4717 | |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 4718 | switch (format) { |
| 4719 | case GBM_FORMAT_XRGB8888: |
| 4720 | pixman_format = PIXMAN_x8r8g8b8; |
| 4721 | break; |
| 4722 | case GBM_FORMAT_RGB565: |
| 4723 | pixman_format = PIXMAN_r5g6b5; |
| 4724 | break; |
| 4725 | default: |
| 4726 | weston_log("Unsupported pixman format 0x%x\n", format); |
| 4727 | return -1; |
| 4728 | } |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4729 | |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 4730 | /* FIXME error checking */ |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4731 | for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) { |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 4732 | 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] | 4733 | if (!output->dumb[i]) |
| 4734 | goto err; |
| 4735 | |
| 4736 | output->image[i] = |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 4737 | pixman_image_create_bits(pixman_format, w, h, |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4738 | output->dumb[i]->map, |
Daniel Stone | 8eece0c | 2016-11-17 17:54:00 +0000 | [diff] [blame] | 4739 | output->dumb[i]->strides[0]); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4740 | if (!output->image[i]) |
| 4741 | goto err; |
| 4742 | } |
| 4743 | |
Pekka Paalanen | dee412d | 2018-04-23 11:44:58 +0200 | [diff] [blame] | 4744 | if (b->use_pixman_shadow) |
| 4745 | flags |= PIXMAN_RENDERER_OUTPUT_USE_SHADOW; |
| 4746 | |
| 4747 | if (pixman_renderer_output_create(&output->base, flags) < 0) |
| 4748 | goto err; |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 4749 | |
Pekka Paalanen | dee412d | 2018-04-23 11:44:58 +0200 | [diff] [blame] | 4750 | weston_log("DRM: output %s %s shadow framebuffer.\n", output->base.name, |
| 4751 | b->use_pixman_shadow ? "uses" : "does not use"); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4752 | |
| 4753 | pixman_region32_init_rect(&output->previous_damage, |
Alexander Larsson | 0b13506 | 2013-05-28 16:23:36 +0200 | [diff] [blame] | 4754 | 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] | 4755 | |
| 4756 | return 0; |
| 4757 | |
| 4758 | err: |
| 4759 | for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) { |
| 4760 | if (output->dumb[i]) |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 4761 | drm_fb_unref(output->dumb[i]); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4762 | if (output->image[i]) |
| 4763 | pixman_image_unref(output->image[i]); |
| 4764 | |
| 4765 | output->dumb[i] = NULL; |
| 4766 | output->image[i] = NULL; |
| 4767 | } |
| 4768 | |
| 4769 | return -1; |
| 4770 | } |
| 4771 | |
| 4772 | static void |
| 4773 | drm_output_fini_pixman(struct drm_output *output) |
| 4774 | { |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 4775 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4776 | unsigned int i; |
| 4777 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 4778 | /* Destroying the Pixman surface will destroy all our buffers, |
| 4779 | * regardless of refcount. Ensure we destroy them here. */ |
| 4780 | if (!b->shutting_down && |
| 4781 | output->scanout_plane->state_cur->fb && |
| 4782 | output->scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB) { |
| 4783 | drm_plane_state_free(output->scanout_plane->state_cur, true); |
| 4784 | output->scanout_plane->state_cur = |
| 4785 | drm_plane_state_alloc(NULL, output->scanout_plane); |
| 4786 | output->scanout_plane->state_cur->complete = true; |
| 4787 | } |
| 4788 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4789 | pixman_renderer_output_destroy(&output->base); |
| 4790 | pixman_region32_fini(&output->previous_damage); |
| 4791 | |
| 4792 | for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4793 | pixman_image_unref(output->image[i]); |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 4794 | drm_fb_unref(output->dumb[i]); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4795 | output->dumb[i] = NULL; |
| 4796 | output->image[i] = NULL; |
| 4797 | } |
| 4798 | } |
| 4799 | |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4800 | static void |
| 4801 | edid_parse_string(const uint8_t *data, char text[]) |
| 4802 | { |
| 4803 | int i; |
| 4804 | int replaced = 0; |
| 4805 | |
| 4806 | /* this is always 12 bytes, but we can't guarantee it's null |
| 4807 | * terminated or not junk. */ |
| 4808 | strncpy(text, (const char *) data, 12); |
| 4809 | |
Bryce Harrington | 9c7de16 | 2015-08-28 13:04:26 -0700 | [diff] [blame] | 4810 | /* guarantee our new string is null-terminated */ |
| 4811 | text[12] = '\0'; |
| 4812 | |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4813 | /* remove insane chars */ |
| 4814 | for (i = 0; text[i] != '\0'; i++) { |
| 4815 | if (text[i] == '\n' || |
| 4816 | text[i] == '\r') { |
| 4817 | text[i] = '\0'; |
| 4818 | break; |
| 4819 | } |
| 4820 | } |
| 4821 | |
| 4822 | /* ensure string is printable */ |
| 4823 | for (i = 0; text[i] != '\0'; i++) { |
| 4824 | if (!isprint(text[i])) { |
| 4825 | text[i] = '-'; |
| 4826 | replaced++; |
| 4827 | } |
| 4828 | } |
| 4829 | |
| 4830 | /* if the string is random junk, ignore the string */ |
| 4831 | if (replaced > 4) |
| 4832 | text[0] = '\0'; |
| 4833 | } |
| 4834 | |
| 4835 | #define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe |
| 4836 | #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc |
| 4837 | #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff |
| 4838 | #define EDID_OFFSET_DATA_BLOCKS 0x36 |
| 4839 | #define EDID_OFFSET_LAST_BLOCK 0x6c |
| 4840 | #define EDID_OFFSET_PNPID 0x08 |
| 4841 | #define EDID_OFFSET_SERIAL 0x0c |
| 4842 | |
| 4843 | static int |
| 4844 | edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length) |
| 4845 | { |
| 4846 | int i; |
| 4847 | uint32_t serial_number; |
| 4848 | |
| 4849 | /* check header */ |
| 4850 | if (length < 128) |
| 4851 | return -1; |
| 4852 | if (data[0] != 0x00 || data[1] != 0xff) |
| 4853 | return -1; |
| 4854 | |
| 4855 | /* decode the PNP ID from three 5 bit words packed into 2 bytes |
| 4856 | * /--08--\/--09--\ |
| 4857 | * 7654321076543210 |
| 4858 | * |\---/\---/\---/ |
| 4859 | * R C1 C2 C3 */ |
| 4860 | edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1; |
| 4861 | edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1; |
| 4862 | edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1; |
| 4863 | edid->pnp_id[3] = '\0'; |
| 4864 | |
| 4865 | /* maybe there isn't a ASCII serial number descriptor, so use this instead */ |
| 4866 | serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0]; |
| 4867 | serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100; |
| 4868 | serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000; |
| 4869 | serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000; |
| 4870 | if (serial_number > 0) |
| 4871 | sprintf(edid->serial_number, "%lu", (unsigned long) serial_number); |
| 4872 | |
| 4873 | /* parse EDID data */ |
| 4874 | for (i = EDID_OFFSET_DATA_BLOCKS; |
| 4875 | i <= EDID_OFFSET_LAST_BLOCK; |
| 4876 | i += 18) { |
| 4877 | /* ignore pixel clock data */ |
| 4878 | if (data[i] != 0) |
| 4879 | continue; |
| 4880 | if (data[i+2] != 0) |
| 4881 | continue; |
| 4882 | |
| 4883 | /* any useful blocks? */ |
| 4884 | if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) { |
| 4885 | edid_parse_string(&data[i+5], |
| 4886 | edid->monitor_name); |
| 4887 | } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) { |
| 4888 | edid_parse_string(&data[i+5], |
| 4889 | edid->serial_number); |
| 4890 | } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) { |
| 4891 | edid_parse_string(&data[i+5], |
| 4892 | edid->eisa_id); |
| 4893 | } |
| 4894 | } |
| 4895 | return 0; |
| 4896 | } |
| 4897 | |
Pekka Paalanen | 6f1866b | 2017-04-03 14:22:51 +0300 | [diff] [blame] | 4898 | /** Parse monitor make, model and serial from EDID |
| 4899 | * |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 4900 | * \param head The head whose \c drm_edid to fill in. |
Pekka Paalanen | 6f1866b | 2017-04-03 14:22:51 +0300 | [diff] [blame] | 4901 | * \param props The DRM connector properties to get the EDID from. |
| 4902 | * \param make[out] The monitor make (PNP ID). |
| 4903 | * \param model[out] The monitor model (name). |
| 4904 | * \param serial_number[out] The monitor serial number. |
| 4905 | * |
| 4906 | * Each of \c *make, \c *model and \c *serial_number are set only if the |
| 4907 | * information is found in the EDID. The pointers they are set to must not |
| 4908 | * be free()'d explicitly, instead they get implicitly freed when the |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 4909 | * \c drm_head is destroyed. |
Pekka Paalanen | 6f1866b | 2017-04-03 14:22:51 +0300 | [diff] [blame] | 4910 | */ |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4911 | static void |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 4912 | find_and_parse_output_edid(struct drm_head *head, |
Pekka Paalanen | 6f1866b | 2017-04-03 14:22:51 +0300 | [diff] [blame] | 4913 | drmModeObjectPropertiesPtr props, |
| 4914 | const char **make, |
| 4915 | const char **model, |
| 4916 | const char **serial_number) |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4917 | { |
| 4918 | drmModePropertyBlobPtr edid_blob = NULL; |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 4919 | uint32_t blob_id; |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4920 | int rc; |
| 4921 | |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 4922 | blob_id = |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 4923 | drm_property_get_value(&head->props_conn[WDRM_CONNECTOR_EDID], |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 4924 | props, 0); |
| 4925 | if (!blob_id) |
| 4926 | return; |
| 4927 | |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 4928 | edid_blob = drmModeGetPropertyBlob(head->backend->drm.fd, blob_id); |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4929 | if (!edid_blob) |
| 4930 | return; |
| 4931 | |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 4932 | rc = edid_parse(&head->edid, |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4933 | edid_blob->data, |
| 4934 | edid_blob->length); |
| 4935 | if (!rc) { |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 4936 | if (head->edid.pnp_id[0] != '\0') |
| 4937 | *make = head->edid.pnp_id; |
| 4938 | if (head->edid.monitor_name[0] != '\0') |
| 4939 | *model = head->edid.monitor_name; |
| 4940 | if (head->edid.serial_number[0] != '\0') |
| 4941 | *serial_number = head->edid.serial_number; |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4942 | } |
| 4943 | drmModeFreePropertyBlob(edid_blob); |
| 4944 | } |
| 4945 | |
Kristian Høgsberg | a30989a | 2013-05-23 17:23:15 -0400 | [diff] [blame] | 4946 | static int |
| 4947 | parse_modeline(const char *s, drmModeModeInfo *mode) |
| 4948 | { |
| 4949 | char hsync[16]; |
| 4950 | char vsync[16]; |
| 4951 | float fclock; |
| 4952 | |
Pekka Paalanen | dc4e3c6 | 2017-12-05 15:37:41 +0200 | [diff] [blame] | 4953 | memset(mode, 0, sizeof *mode); |
| 4954 | |
Kristian Høgsberg | a30989a | 2013-05-23 17:23:15 -0400 | [diff] [blame] | 4955 | mode->type = DRM_MODE_TYPE_USERDEF; |
| 4956 | mode->hskew = 0; |
| 4957 | mode->vscan = 0; |
| 4958 | mode->vrefresh = 0; |
| 4959 | mode->flags = 0; |
| 4960 | |
Rob Bradford | 307e09e | 2013-07-26 16:29:40 +0100 | [diff] [blame] | 4961 | 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] | 4962 | &fclock, |
| 4963 | &mode->hdisplay, |
| 4964 | &mode->hsync_start, |
| 4965 | &mode->hsync_end, |
| 4966 | &mode->htotal, |
| 4967 | &mode->vdisplay, |
| 4968 | &mode->vsync_start, |
| 4969 | &mode->vsync_end, |
| 4970 | &mode->vtotal, hsync, vsync) != 11) |
| 4971 | return -1; |
| 4972 | |
| 4973 | mode->clock = fclock * 1000; |
Guido Günther | 92278e0 | 2018-06-26 20:40:08 +0200 | [diff] [blame] | 4974 | if (strcasecmp(hsync, "+hsync") == 0) |
Kristian Høgsberg | a30989a | 2013-05-23 17:23:15 -0400 | [diff] [blame] | 4975 | mode->flags |= DRM_MODE_FLAG_PHSYNC; |
Guido Günther | 92278e0 | 2018-06-26 20:40:08 +0200 | [diff] [blame] | 4976 | else if (strcasecmp(hsync, "-hsync") == 0) |
Kristian Høgsberg | a30989a | 2013-05-23 17:23:15 -0400 | [diff] [blame] | 4977 | mode->flags |= DRM_MODE_FLAG_NHSYNC; |
| 4978 | else |
| 4979 | return -1; |
| 4980 | |
Guido Günther | 92278e0 | 2018-06-26 20:40:08 +0200 | [diff] [blame] | 4981 | if (strcasecmp(vsync, "+vsync") == 0) |
Kristian Høgsberg | a30989a | 2013-05-23 17:23:15 -0400 | [diff] [blame] | 4982 | mode->flags |= DRM_MODE_FLAG_PVSYNC; |
Guido Günther | 92278e0 | 2018-06-26 20:40:08 +0200 | [diff] [blame] | 4983 | else if (strcasecmp(vsync, "-vsync") == 0) |
Kristian Høgsberg | a30989a | 2013-05-23 17:23:15 -0400 | [diff] [blame] | 4984 | mode->flags |= DRM_MODE_FLAG_NVSYNC; |
| 4985 | else |
| 4986 | return -1; |
| 4987 | |
Emmanuel Gil Peyrot | a62138b | 2016-05-02 22:40:11 +0100 | [diff] [blame] | 4988 | snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f", |
| 4989 | mode->hdisplay, mode->vdisplay, fclock); |
| 4990 | |
Kristian Høgsberg | a30989a | 2013-05-23 17:23:15 -0400 | [diff] [blame] | 4991 | return 0; |
| 4992 | } |
| 4993 | |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 4994 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 4995 | setup_output_seat_constraint(struct drm_backend *b, |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 4996 | struct weston_output *output, |
| 4997 | const char *s) |
| 4998 | { |
| 4999 | if (strcmp(s, "") != 0) { |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 5000 | struct weston_pointer *pointer; |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 5001 | struct udev_seat *seat; |
| 5002 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5003 | seat = udev_seat_get_named(&b->input, s); |
Derek Foreman | 0720ea3 | 2015-07-15 13:00:35 -0500 | [diff] [blame] | 5004 | if (!seat) |
| 5005 | return; |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 5006 | |
Derek Foreman | 0720ea3 | 2015-07-15 13:00:35 -0500 | [diff] [blame] | 5007 | seat->base.output = output; |
| 5008 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 5009 | pointer = weston_seat_get_pointer(&seat->base); |
| 5010 | if (pointer) |
| 5011 | weston_pointer_clamp(pointer, |
| 5012 | &pointer->x, |
| 5013 | &pointer->y); |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 5014 | } |
| 5015 | } |
| 5016 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 5017 | static int |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 5018 | drm_output_attach_head(struct weston_output *output_base, |
| 5019 | struct weston_head *head_base) |
| 5020 | { |
Pekka Paalanen | d5f98d8 | 2017-12-08 14:45:00 +0200 | [diff] [blame] | 5021 | struct drm_backend *b = to_drm_backend(output_base->compositor); |
| 5022 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 5023 | if (wl_list_length(&output_base->head_list) >= MAX_CLONED_CONNECTORS) |
| 5024 | return -1; |
| 5025 | |
Pekka Paalanen | d5f98d8 | 2017-12-08 14:45:00 +0200 | [diff] [blame] | 5026 | if (!output_base->enabled) |
| 5027 | return 0; |
| 5028 | |
| 5029 | /* XXX: ensure the configuration will work. |
| 5030 | * This is actually impossible without major infrastructure |
| 5031 | * work. */ |
| 5032 | |
| 5033 | /* Need to go through modeset to add connectors. */ |
| 5034 | /* XXX: Ideally we'd do this per-output, not globally. */ |
| 5035 | /* XXX: Doing it globally, what guarantees another output's update |
| 5036 | * will not clear the flag before this output is updated? |
| 5037 | */ |
| 5038 | b->state_invalid = true; |
| 5039 | |
| 5040 | weston_output_schedule_repaint(output_base); |
| 5041 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 5042 | return 0; |
| 5043 | } |
| 5044 | |
Pekka Paalanen | 7f85379 | 2017-11-29 14:33:33 +0200 | [diff] [blame] | 5045 | static void |
| 5046 | drm_output_detach_head(struct weston_output *output_base, |
| 5047 | struct weston_head *head_base) |
| 5048 | { |
| 5049 | struct drm_backend *b = to_drm_backend(output_base->compositor); |
| 5050 | |
| 5051 | if (!output_base->enabled) |
| 5052 | return; |
| 5053 | |
| 5054 | /* Need to go through modeset to drop connectors that should no longer |
| 5055 | * be driven. */ |
| 5056 | /* XXX: Ideally we'd do this per-output, not globally. */ |
| 5057 | b->state_invalid = true; |
| 5058 | |
| 5059 | weston_output_schedule_repaint(output_base); |
| 5060 | } |
| 5061 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 5062 | static int |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5063 | 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] | 5064 | { |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 5065 | int ret = 0; |
| 5066 | |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 5067 | if (s == NULL) |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5068 | *gbm_format = default_value; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 5069 | else if (strcmp(s, "xrgb8888") == 0) |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5070 | *gbm_format = GBM_FORMAT_XRGB8888; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 5071 | else if (strcmp(s, "rgb565") == 0) |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5072 | *gbm_format = GBM_FORMAT_RGB565; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 5073 | else if (strcmp(s, "xrgb2101010") == 0) |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5074 | *gbm_format = GBM_FORMAT_XRGB2101010; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 5075 | else { |
| 5076 | weston_log("fatal: unrecognized pixel format: %s\n", s); |
| 5077 | ret = -1; |
| 5078 | } |
| 5079 | |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 5080 | return ret; |
| 5081 | } |
| 5082 | |
Pekka Paalanen | f005f25 | 2017-11-10 16:34:39 +0200 | [diff] [blame] | 5083 | static uint32_t |
| 5084 | u32distance(uint32_t a, uint32_t b) |
| 5085 | { |
| 5086 | if (a < b) |
| 5087 | return b - a; |
| 5088 | else |
| 5089 | return a - b; |
| 5090 | } |
| 5091 | |
| 5092 | /** Choose equivalent mode |
| 5093 | * |
| 5094 | * If the two modes are not equivalent, return NULL. |
| 5095 | * Otherwise return the mode that is more likely to work in place of both. |
| 5096 | * |
| 5097 | * None of the fuzzy matching criteria in this function have any justification. |
| 5098 | * |
| 5099 | * typedef struct _drmModeModeInfo { |
| 5100 | * uint32_t clock; |
| 5101 | * uint16_t hdisplay, hsync_start, hsync_end, htotal, hskew; |
| 5102 | * uint16_t vdisplay, vsync_start, vsync_end, vtotal, vscan; |
| 5103 | * |
| 5104 | * uint32_t vrefresh; |
| 5105 | * |
| 5106 | * uint32_t flags; |
| 5107 | * uint32_t type; |
| 5108 | * char name[DRM_DISPLAY_MODE_LEN]; |
| 5109 | * } drmModeModeInfo, *drmModeModeInfoPtr; |
| 5110 | */ |
| 5111 | static const drmModeModeInfo * |
| 5112 | drm_mode_pick_equivalent(const drmModeModeInfo *a, const drmModeModeInfo *b) |
| 5113 | { |
| 5114 | uint32_t refresh_a, refresh_b; |
| 5115 | |
| 5116 | if (a->hdisplay != b->hdisplay || a->vdisplay != b->vdisplay) |
| 5117 | return NULL; |
| 5118 | |
| 5119 | if (a->flags != b->flags) |
| 5120 | return NULL; |
| 5121 | |
| 5122 | /* kHz */ |
| 5123 | if (u32distance(a->clock, b->clock) > 500) |
| 5124 | return NULL; |
| 5125 | |
| 5126 | refresh_a = drm_refresh_rate_mHz(a); |
| 5127 | refresh_b = drm_refresh_rate_mHz(b); |
| 5128 | if (u32distance(refresh_a, refresh_b) > 50) |
| 5129 | return NULL; |
| 5130 | |
| 5131 | if ((a->type ^ b->type) & DRM_MODE_TYPE_PREFERRED) { |
| 5132 | if (a->type & DRM_MODE_TYPE_PREFERRED) |
| 5133 | return a; |
| 5134 | else |
| 5135 | return b; |
| 5136 | } |
| 5137 | |
| 5138 | return a; |
| 5139 | } |
| 5140 | |
| 5141 | /* If the given mode info is not already in the list, add it. |
| 5142 | * If it is in the list, either keep the existing or replace it, |
| 5143 | * depending on which one is "better". |
| 5144 | */ |
| 5145 | static int |
| 5146 | drm_output_try_add_mode(struct drm_output *output, const drmModeModeInfo *info) |
| 5147 | { |
| 5148 | struct weston_mode *base; |
| 5149 | struct drm_mode *mode; |
| 5150 | struct drm_backend *backend; |
| 5151 | const drmModeModeInfo *chosen = NULL; |
| 5152 | |
| 5153 | assert(info); |
| 5154 | |
| 5155 | wl_list_for_each(base, &output->base.mode_list, link) { |
| 5156 | mode = to_drm_mode(base); |
| 5157 | chosen = drm_mode_pick_equivalent(&mode->mode_info, info); |
| 5158 | if (chosen) |
| 5159 | break; |
| 5160 | } |
| 5161 | |
| 5162 | if (chosen == info) { |
| 5163 | backend = to_drm_backend(output->base.compositor); |
| 5164 | drm_output_destroy_mode(backend, mode); |
| 5165 | chosen = NULL; |
| 5166 | } |
| 5167 | |
| 5168 | if (!chosen) { |
| 5169 | mode = drm_output_add_mode(output, info); |
| 5170 | if (!mode) |
| 5171 | return -1; |
| 5172 | } |
| 5173 | /* else { the equivalent mode is already in the list } */ |
| 5174 | |
| 5175 | return 0; |
| 5176 | } |
| 5177 | |
Pekka Paalanen | 4be2485 | 2017-09-11 15:01:12 +0300 | [diff] [blame] | 5178 | /** Rewrite the output's mode list |
| 5179 | * |
| 5180 | * @param output The output. |
| 5181 | * @return 0 on success, -1 on failure. |
| 5182 | * |
| 5183 | * Destroy all existing modes in the list, and reconstruct a new list from |
| 5184 | * scratch, based on the currently attached heads. |
| 5185 | * |
| 5186 | * On failure the output's mode list may contain some modes. |
| 5187 | */ |
| 5188 | static int |
| 5189 | drm_output_update_modelist_from_heads(struct drm_output *output) |
| 5190 | { |
| 5191 | struct drm_backend *backend = to_drm_backend(output->base.compositor); |
| 5192 | struct weston_head *head_base; |
| 5193 | struct drm_head *head; |
Pekka Paalanen | 4be2485 | 2017-09-11 15:01:12 +0300 | [diff] [blame] | 5194 | int i; |
Pekka Paalanen | f005f25 | 2017-11-10 16:34:39 +0200 | [diff] [blame] | 5195 | int ret; |
Pekka Paalanen | 4be2485 | 2017-09-11 15:01:12 +0300 | [diff] [blame] | 5196 | |
| 5197 | assert(!output->base.enabled); |
| 5198 | |
| 5199 | drm_mode_list_destroy(backend, &output->base.mode_list); |
| 5200 | |
Pekka Paalanen | f005f25 | 2017-11-10 16:34:39 +0200 | [diff] [blame] | 5201 | wl_list_for_each(head_base, &output->base.head_list, output_link) { |
| 5202 | head = to_drm_head(head_base); |
| 5203 | for (i = 0; i < head->connector->count_modes; i++) { |
| 5204 | ret = drm_output_try_add_mode(output, |
| 5205 | &head->connector->modes[i]); |
| 5206 | if (ret < 0) |
| 5207 | return -1; |
| 5208 | } |
Pekka Paalanen | 4be2485 | 2017-09-11 15:01:12 +0300 | [diff] [blame] | 5209 | } |
| 5210 | |
| 5211 | return 0; |
| 5212 | } |
| 5213 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 5214 | /** |
| 5215 | * Choose suitable mode for an output |
| 5216 | * |
| 5217 | * Find the most suitable mode to use for initial setup (or reconfiguration on |
| 5218 | * hotplug etc) for a DRM output. |
| 5219 | * |
| 5220 | * @param output DRM output to choose mode for |
| 5221 | * @param kind Strategy and preference to use when choosing mode |
| 5222 | * @param width Desired width for this output |
| 5223 | * @param height Desired height for this output |
| 5224 | * @param current_mode Mode currently being displayed on this output |
| 5225 | * @param modeline Manually-entered mode (may be NULL) |
| 5226 | * @returns A mode from the output's mode list, or NULL if none available |
| 5227 | */ |
| 5228 | static struct drm_mode * |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5229 | drm_output_choose_initial_mode(struct drm_backend *backend, |
| 5230 | struct drm_output *output, |
| 5231 | enum weston_drm_backend_output_mode mode, |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5232 | const char *modeline, |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5233 | const drmModeModeInfo *current_mode) |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 5234 | { |
| 5235 | struct drm_mode *preferred = NULL; |
| 5236 | struct drm_mode *current = NULL; |
| 5237 | struct drm_mode *configured = NULL; |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 5238 | struct drm_mode *config_fall_back = NULL; |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 5239 | struct drm_mode *best = NULL; |
| 5240 | struct drm_mode *drm_mode; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5241 | drmModeModeInfo drm_modeline; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5242 | int32_t width = 0; |
| 5243 | int32_t height = 0; |
Fabien Dessenne | 2d66a7d | 2017-01-17 17:17:21 +0100 | [diff] [blame] | 5244 | uint32_t refresh = 0; |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 5245 | uint32_t aspect_width = 0; |
| 5246 | uint32_t aspect_height = 0; |
| 5247 | enum weston_mode_aspect_ratio aspect_ratio = WESTON_MODE_PIC_AR_NONE; |
Fabien Dessenne | 2d66a7d | 2017-01-17 17:17:21 +0100 | [diff] [blame] | 5248 | int n; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5249 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5250 | if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) { |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 5251 | n = sscanf(modeline, "%dx%d@%d %u:%u", &width, &height, |
| 5252 | &refresh, &aspect_width, &aspect_height); |
| 5253 | if (backend->aspect_ratio_supported && n == 5) { |
| 5254 | if (aspect_width == 4 && aspect_height == 3) |
| 5255 | aspect_ratio = WESTON_MODE_PIC_AR_4_3; |
| 5256 | else if (aspect_width == 16 && aspect_height == 9) |
| 5257 | aspect_ratio = WESTON_MODE_PIC_AR_16_9; |
| 5258 | else if (aspect_width == 64 && aspect_height == 27) |
| 5259 | aspect_ratio = WESTON_MODE_PIC_AR_64_27; |
| 5260 | else if (aspect_width == 256 && aspect_height == 135) |
| 5261 | aspect_ratio = WESTON_MODE_PIC_AR_256_135; |
| 5262 | else |
| 5263 | weston_log("Invalid modeline \"%s\" for output %s\n", |
| 5264 | modeline, output->base.name); |
| 5265 | } |
| 5266 | if (n != 2 && n != 3 && n != 5) { |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5267 | width = -1; |
| 5268 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5269 | if (parse_modeline(modeline, &drm_modeline) == 0) { |
| 5270 | configured = drm_output_add_mode(output, &drm_modeline); |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5271 | if (!configured) |
| 5272 | return NULL; |
| 5273 | } else { |
| 5274 | weston_log("Invalid modeline \"%s\" for output %s\n", |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5275 | modeline, output->base.name); |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5276 | } |
| 5277 | } |
| 5278 | } |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 5279 | |
| 5280 | 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] | 5281 | if (width == drm_mode->base.width && |
Fabien Dessenne | 2d66a7d | 2017-01-17 17:17:21 +0100 | [diff] [blame] | 5282 | height == drm_mode->base.height && |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 5283 | (refresh == 0 || refresh == drm_mode->mode_info.vrefresh)) { |
| 5284 | if (!backend->aspect_ratio_supported || |
| 5285 | aspect_ratio == drm_mode->base.aspect_ratio) |
| 5286 | configured = drm_mode; |
| 5287 | else |
| 5288 | config_fall_back = drm_mode; |
| 5289 | } |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 5290 | |
comic fans | 7a5c562 | 2016-03-17 14:29:27 +0200 | [diff] [blame] | 5291 | if (memcmp(current_mode, &drm_mode->mode_info, |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 5292 | sizeof *current_mode) == 0) |
| 5293 | current = drm_mode; |
| 5294 | |
| 5295 | if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED) |
| 5296 | preferred = drm_mode; |
| 5297 | |
| 5298 | best = drm_mode; |
| 5299 | } |
| 5300 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 5301 | if (current == NULL && current_mode->clock != 0) { |
| 5302 | current = drm_output_add_mode(output, current_mode); |
| 5303 | if (!current) |
| 5304 | return NULL; |
| 5305 | } |
| 5306 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5307 | if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT) |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 5308 | configured = current; |
| 5309 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 5310 | if (configured) |
| 5311 | return configured; |
| 5312 | |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 5313 | if (config_fall_back) |
| 5314 | return config_fall_back; |
| 5315 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 5316 | if (preferred) |
| 5317 | return preferred; |
| 5318 | |
| 5319 | if (current) |
| 5320 | return current; |
| 5321 | |
| 5322 | if (best) |
| 5323 | return best; |
| 5324 | |
| 5325 | weston_log("no available modes for %s\n", output->base.name); |
| 5326 | return NULL; |
| 5327 | } |
| 5328 | |
Pekka Paalanen | eee580b | 2014-06-04 16:43:06 +0300 | [diff] [blame] | 5329 | static int |
Pekka Paalanen | 9c03a7c | 2017-11-28 14:30:10 +0200 | [diff] [blame] | 5330 | 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] | 5331 | { |
Pekka Paalanen | 9c03a7c | 2017-11-28 14:30:10 +0200 | [diff] [blame] | 5332 | int drm_fd = backend->drm.fd; |
Pekka Paalanen | eee580b | 2014-06-04 16:43:06 +0300 | [diff] [blame] | 5333 | drmModeEncoder *encoder; |
| 5334 | drmModeCrtc *crtc; |
| 5335 | |
| 5336 | /* Get the current mode on the crtc that's currently driving |
| 5337 | * this connector. */ |
Pekka Paalanen | 9c03a7c | 2017-11-28 14:30:10 +0200 | [diff] [blame] | 5338 | encoder = drmModeGetEncoder(drm_fd, head->connector->encoder_id); |
Pekka Paalanen | eee580b | 2014-06-04 16:43:06 +0300 | [diff] [blame] | 5339 | if (encoder != NULL) { |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 5340 | head->inherited_crtc_id = encoder->crtc_id; |
| 5341 | |
Pekka Paalanen | eee580b | 2014-06-04 16:43:06 +0300 | [diff] [blame] | 5342 | crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id); |
| 5343 | drmModeFreeEncoder(encoder); |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 5344 | |
Pekka Paalanen | eee580b | 2014-06-04 16:43:06 +0300 | [diff] [blame] | 5345 | if (crtc == NULL) |
| 5346 | return -1; |
| 5347 | if (crtc->mode_valid) |
Pekka Paalanen | 6fae2be | 2017-11-28 14:33:52 +0200 | [diff] [blame] | 5348 | head->inherited_mode = crtc->mode; |
Pekka Paalanen | eee580b | 2014-06-04 16:43:06 +0300 | [diff] [blame] | 5349 | drmModeFreeCrtc(crtc); |
| 5350 | } |
| 5351 | |
| 5352 | return 0; |
| 5353 | } |
| 5354 | |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 5355 | static int |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5356 | drm_output_set_mode(struct weston_output *base, |
| 5357 | enum weston_drm_backend_output_mode mode, |
| 5358 | const char *modeline) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5359 | { |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5360 | struct drm_output *output = to_drm_output(base); |
| 5361 | struct drm_backend *b = to_drm_backend(base->compositor); |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 5362 | 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] | 5363 | |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 5364 | struct drm_mode *current; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5365 | |
Pekka Paalanen | 4be2485 | 2017-09-11 15:01:12 +0300 | [diff] [blame] | 5366 | if (drm_output_update_modelist_from_heads(output) < 0) |
| 5367 | return -1; |
| 5368 | |
Pekka Paalanen | 13d233e | 2017-09-11 14:06:11 +0300 | [diff] [blame] | 5369 | current = drm_output_choose_initial_mode(b, output, mode, modeline, |
| 5370 | &head->inherited_mode); |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 5371 | if (!current) |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 5372 | return -1; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5373 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 5374 | output->base.current_mode = ¤t->base; |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 5375 | output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT; |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame] | 5376 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5377 | /* Set native_ fields, so weston_output_mode_switch_to_native() works */ |
| 5378 | output->base.native_mode = output->base.current_mode; |
| 5379 | output->base.native_scale = output->base.current_scale; |
| 5380 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5381 | return 0; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5382 | } |
| 5383 | |
| 5384 | static void |
| 5385 | drm_output_set_gbm_format(struct weston_output *base, |
| 5386 | const char *gbm_format) |
| 5387 | { |
| 5388 | struct drm_output *output = to_drm_output(base); |
| 5389 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 5390 | |
| 5391 | if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1) |
| 5392 | output->gbm_format = b->gbm_format; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 5393 | |
| 5394 | /* Without universal planes, we can't discover which formats are |
| 5395 | * supported by the primary plane; we just hope that the GBM format |
| 5396 | * works. */ |
| 5397 | if (!b->universal_planes) |
Sergi Granell | f445622 | 2017-01-12 17:17:32 +0000 | [diff] [blame] | 5398 | output->scanout_plane->formats[0].format = output->gbm_format; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5399 | } |
| 5400 | |
| 5401 | static void |
| 5402 | drm_output_set_seat(struct weston_output *base, |
| 5403 | const char *seat) |
| 5404 | { |
| 5405 | struct drm_output *output = to_drm_output(base); |
| 5406 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 5407 | |
| 5408 | setup_output_seat_constraint(b, &output->base, |
| 5409 | seat ? seat : ""); |
| 5410 | } |
| 5411 | |
| 5412 | static int |
Pekka Paalanen | c4db6f7 | 2017-09-05 16:37:03 +0300 | [diff] [blame] | 5413 | drm_output_init_gamma_size(struct drm_output *output) |
| 5414 | { |
| 5415 | struct drm_backend *backend = to_drm_backend(output->base.compositor); |
| 5416 | drmModeCrtc *crtc; |
| 5417 | |
| 5418 | assert(output->base.compositor); |
| 5419 | assert(output->crtc_id != 0); |
| 5420 | crtc = drmModeGetCrtc(backend->drm.fd, output->crtc_id); |
| 5421 | if (!crtc) |
| 5422 | return -1; |
| 5423 | |
| 5424 | output->base.gamma_size = crtc->gamma_size; |
| 5425 | |
| 5426 | drmModeFreeCrtc(crtc); |
| 5427 | |
| 5428 | return 0; |
| 5429 | } |
| 5430 | |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 5431 | static uint32_t |
| 5432 | drm_head_get_possible_crtcs_mask(struct drm_head *head) |
| 5433 | { |
| 5434 | uint32_t possible_crtcs = 0; |
| 5435 | drmModeEncoder *encoder; |
| 5436 | int i; |
| 5437 | |
| 5438 | for (i = 0; i < head->connector->count_encoders; i++) { |
| 5439 | encoder = drmModeGetEncoder(head->backend->drm.fd, |
| 5440 | head->connector->encoders[i]); |
| 5441 | if (!encoder) |
| 5442 | continue; |
| 5443 | |
| 5444 | possible_crtcs |= encoder->possible_crtcs; |
| 5445 | drmModeFreeEncoder(encoder); |
| 5446 | } |
| 5447 | |
| 5448 | return possible_crtcs; |
| 5449 | } |
| 5450 | |
| 5451 | static int |
| 5452 | drm_crtc_get_index(drmModeRes *resources, uint32_t crtc_id) |
| 5453 | { |
| 5454 | int i; |
| 5455 | |
| 5456 | for (i = 0; i < resources->count_crtcs; i++) { |
| 5457 | if (resources->crtcs[i] == crtc_id) |
| 5458 | return i; |
| 5459 | } |
| 5460 | |
| 5461 | assert(0 && "unknown crtc id"); |
| 5462 | return -1; |
| 5463 | } |
| 5464 | |
| 5465 | /** Pick a CRTC that might be able to drive all attached connectors |
| 5466 | * |
| 5467 | * @param output The output whose attached heads to include. |
| 5468 | * @param resources The DRM KMS resources. |
| 5469 | * @return CRTC index, or -1 on failure or not found. |
| 5470 | */ |
| 5471 | static int |
| 5472 | drm_output_pick_crtc(struct drm_output *output, drmModeRes *resources) |
| 5473 | { |
| 5474 | struct drm_backend *backend; |
| 5475 | struct weston_head *base; |
| 5476 | struct drm_head *head; |
| 5477 | uint32_t possible_crtcs = 0xffffffff; |
| 5478 | int existing_crtc[32]; |
| 5479 | unsigned j, n = 0; |
| 5480 | uint32_t crtc_id; |
| 5481 | int best_crtc_index = -1; |
Pekka Paalanen | db4c7d7 | 2017-11-28 16:11:00 +0200 | [diff] [blame] | 5482 | int fallback_crtc_index = -1; |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 5483 | int i; |
Pekka Paalanen | db4c7d7 | 2017-11-28 16:11:00 +0200 | [diff] [blame] | 5484 | bool match; |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 5485 | |
| 5486 | backend = to_drm_backend(output->base.compositor); |
| 5487 | |
| 5488 | /* This algorithm ignores drmModeEncoder::possible_clones restriction, |
| 5489 | * because it is more often set wrong than not in the kernel. */ |
| 5490 | |
| 5491 | /* Accumulate a mask of possible crtcs and find existing routings. */ |
| 5492 | wl_list_for_each(base, &output->base.head_list, output_link) { |
| 5493 | head = to_drm_head(base); |
| 5494 | |
| 5495 | possible_crtcs &= drm_head_get_possible_crtcs_mask(head); |
| 5496 | |
| 5497 | crtc_id = head->inherited_crtc_id; |
| 5498 | if (crtc_id > 0 && n < ARRAY_LENGTH(existing_crtc)) |
| 5499 | existing_crtc[n++] = drm_crtc_get_index(resources, |
| 5500 | crtc_id); |
| 5501 | } |
| 5502 | |
| 5503 | /* Find a crtc that could drive each connector individually at least, |
| 5504 | * and prefer existing routings. */ |
| 5505 | for (i = 0; i < resources->count_crtcs; i++) { |
| 5506 | crtc_id = resources->crtcs[i]; |
| 5507 | |
| 5508 | /* Could the crtc not drive each connector? */ |
| 5509 | if (!(possible_crtcs & (1 << i))) |
| 5510 | continue; |
| 5511 | |
| 5512 | /* Is the crtc already in use? */ |
| 5513 | if (drm_output_find_by_crtc(backend, crtc_id)) |
| 5514 | continue; |
| 5515 | |
| 5516 | /* Try to preserve the existing CRTC -> connector routing; |
| 5517 | * it makes initialisation faster, and also since we have a |
| 5518 | * very dumb picking algorithm, may preserve a better |
| 5519 | * choice. */ |
| 5520 | for (j = 0; j < n; j++) { |
| 5521 | if (existing_crtc[j] == i) |
| 5522 | return i; |
| 5523 | } |
| 5524 | |
Pekka Paalanen | db4c7d7 | 2017-11-28 16:11:00 +0200 | [diff] [blame] | 5525 | /* Check if any other head had existing routing to this CRTC. |
| 5526 | * If they did, this is not the best CRTC as it might be needed |
| 5527 | * for another output we haven't enabled yet. */ |
| 5528 | match = false; |
| 5529 | wl_list_for_each(base, &backend->compositor->head_list, |
| 5530 | compositor_link) { |
| 5531 | head = to_drm_head(base); |
| 5532 | |
| 5533 | if (head->base.output == &output->base) |
| 5534 | continue; |
| 5535 | |
| 5536 | if (weston_head_is_enabled(&head->base)) |
| 5537 | continue; |
| 5538 | |
| 5539 | if (head->inherited_crtc_id == crtc_id) { |
| 5540 | match = true; |
| 5541 | break; |
| 5542 | } |
| 5543 | } |
| 5544 | if (!match) |
| 5545 | best_crtc_index = i; |
| 5546 | |
| 5547 | fallback_crtc_index = i; |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 5548 | } |
| 5549 | |
| 5550 | if (best_crtc_index != -1) |
| 5551 | return best_crtc_index; |
| 5552 | |
Pekka Paalanen | db4c7d7 | 2017-11-28 16:11:00 +0200 | [diff] [blame] | 5553 | if (fallback_crtc_index != -1) |
| 5554 | return fallback_crtc_index; |
| 5555 | |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 5556 | /* Likely possible_crtcs was empty due to asking for clones, |
| 5557 | * but since the DRM documentation says the kernel lies, let's |
| 5558 | * pick one crtc anyway. Trial and error is the only way to |
| 5559 | * be sure if something doesn't work. */ |
| 5560 | |
| 5561 | /* First pick any existing assignment. */ |
| 5562 | for (j = 0; j < n; j++) { |
| 5563 | crtc_id = resources->crtcs[existing_crtc[j]]; |
| 5564 | if (!drm_output_find_by_crtc(backend, crtc_id)) |
| 5565 | return existing_crtc[j]; |
| 5566 | } |
| 5567 | |
| 5568 | /* Otherwise pick any available crtc. */ |
| 5569 | for (i = 0; i < resources->count_crtcs; i++) { |
| 5570 | crtc_id = resources->crtcs[i]; |
| 5571 | |
| 5572 | if (!drm_output_find_by_crtc(backend, crtc_id)) |
| 5573 | return i; |
| 5574 | } |
| 5575 | |
| 5576 | return -1; |
| 5577 | } |
| 5578 | |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 5579 | /** Allocate a CRTC for the output |
| 5580 | * |
| 5581 | * @param output The output with no allocated CRTC. |
| 5582 | * @param resources DRM KMS resources. |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 5583 | * @return 0 on success, -1 on failure. |
| 5584 | * |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 5585 | * 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] | 5586 | * for the output, and loads the CRTC properties. |
| 5587 | * |
| 5588 | * Populates the cursor and scanout planes. |
| 5589 | * |
| 5590 | * On failure, the output remains without a CRTC. |
| 5591 | */ |
| 5592 | static int |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 5593 | drm_output_init_crtc(struct drm_output *output, drmModeRes *resources) |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 5594 | { |
| 5595 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
| 5596 | drmModeObjectPropertiesPtr props; |
| 5597 | int i; |
| 5598 | |
| 5599 | assert(output->crtc_id == 0); |
| 5600 | |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 5601 | i = drm_output_pick_crtc(output, resources); |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 5602 | if (i < 0) { |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 5603 | weston_log("Output '%s': No available CRTCs.\n", |
| 5604 | output->base.name); |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 5605 | return -1; |
| 5606 | } |
| 5607 | |
| 5608 | output->crtc_id = resources->crtcs[i]; |
| 5609 | output->pipe = i; |
| 5610 | |
| 5611 | props = drmModeObjectGetProperties(b->drm.fd, output->crtc_id, |
| 5612 | DRM_MODE_OBJECT_CRTC); |
| 5613 | if (!props) { |
| 5614 | weston_log("failed to get CRTC properties\n"); |
| 5615 | goto err_crtc; |
| 5616 | } |
| 5617 | drm_property_info_populate(b, crtc_props, output->props_crtc, |
| 5618 | WDRM_CRTC__COUNT, props); |
| 5619 | drmModeFreeObjectProperties(props); |
| 5620 | |
| 5621 | output->scanout_plane = |
| 5622 | drm_output_find_special_plane(b, output, |
| 5623 | WDRM_PLANE_TYPE_PRIMARY); |
| 5624 | if (!output->scanout_plane) { |
| 5625 | weston_log("Failed to find primary plane for output %s\n", |
| 5626 | output->base.name); |
| 5627 | goto err_crtc; |
| 5628 | } |
| 5629 | |
| 5630 | /* Failing to find a cursor plane is not fatal, as we'll fall back |
| 5631 | * to software cursor. */ |
| 5632 | output->cursor_plane = |
| 5633 | drm_output_find_special_plane(b, output, |
| 5634 | WDRM_PLANE_TYPE_CURSOR); |
| 5635 | |
Pekka Paalanen | 663d5e9 | 2017-09-08 13:32:40 +0300 | [diff] [blame] | 5636 | wl_array_remove_uint32(&b->unused_crtcs, output->crtc_id); |
| 5637 | |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 5638 | return 0; |
| 5639 | |
| 5640 | err_crtc: |
| 5641 | output->crtc_id = 0; |
| 5642 | output->pipe = 0; |
| 5643 | |
| 5644 | return -1; |
| 5645 | } |
| 5646 | |
| 5647 | /** Free the CRTC from the output |
| 5648 | * |
| 5649 | * @param output The output whose CRTC to deallocate. |
| 5650 | * |
| 5651 | * The CRTC reserved for the given output becomes free to use again. |
| 5652 | */ |
| 5653 | static void |
| 5654 | drm_output_fini_crtc(struct drm_output *output) |
| 5655 | { |
| 5656 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Pekka Paalanen | 663d5e9 | 2017-09-08 13:32:40 +0300 | [diff] [blame] | 5657 | uint32_t *unused; |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 5658 | |
| 5659 | if (!b->universal_planes && !b->shutting_down) { |
| 5660 | /* With universal planes, the 'special' planes are allocated at |
| 5661 | * startup, freed at shutdown, and live on the plane list in |
| 5662 | * between. We want the planes to continue to exist and be freed |
| 5663 | * up for other outputs. |
| 5664 | * |
| 5665 | * Without universal planes, our special planes are |
| 5666 | * pseudo-planes allocated at output creation, freed at output |
| 5667 | * destruction, and not usable by other outputs. |
| 5668 | * |
| 5669 | * On the other hand, if the compositor is already shutting down, |
| 5670 | * the plane has already been destroyed. |
| 5671 | */ |
| 5672 | if (output->cursor_plane) |
| 5673 | drm_plane_destroy(output->cursor_plane); |
| 5674 | if (output->scanout_plane) |
| 5675 | drm_plane_destroy(output->scanout_plane); |
| 5676 | } |
| 5677 | |
| 5678 | drm_property_info_free(output->props_crtc, WDRM_CRTC__COUNT); |
Pekka Paalanen | 663d5e9 | 2017-09-08 13:32:40 +0300 | [diff] [blame] | 5679 | |
| 5680 | assert(output->crtc_id != 0); |
| 5681 | |
| 5682 | unused = wl_array_add(&b->unused_crtcs, sizeof(*unused)); |
| 5683 | *unused = output->crtc_id; |
| 5684 | |
| 5685 | /* Force resetting unused CRTCs */ |
| 5686 | b->state_invalid = true; |
| 5687 | |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 5688 | output->crtc_id = 0; |
| 5689 | output->cursor_plane = NULL; |
| 5690 | output->scanout_plane = NULL; |
| 5691 | } |
| 5692 | |
Pekka Paalanen | c0eb254 | 2017-11-15 13:37:18 +0200 | [diff] [blame] | 5693 | static void |
| 5694 | drm_output_print_modes(struct drm_output *output) |
| 5695 | { |
| 5696 | struct weston_mode *m; |
| 5697 | struct drm_mode *dm; |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 5698 | const char *aspect_ratio; |
Pekka Paalanen | c0eb254 | 2017-11-15 13:37:18 +0200 | [diff] [blame] | 5699 | |
| 5700 | wl_list_for_each(m, &output->base.mode_list, link) { |
| 5701 | dm = to_drm_mode(m); |
| 5702 | |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 5703 | aspect_ratio = aspect_ratio_to_string(m->aspect_ratio); |
| 5704 | 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] | 5705 | m->width, m->height, m->refresh / 1000.0, |
Ankit Nautiyal | a21c393 | 2097-03-19 00:24:57 +0530 | [diff] [blame] | 5706 | aspect_ratio, |
Pekka Paalanen | c0eb254 | 2017-11-15 13:37:18 +0200 | [diff] [blame] | 5707 | m->flags & WL_OUTPUT_MODE_PREFERRED ? |
| 5708 | ", preferred" : "", |
| 5709 | m->flags & WL_OUTPUT_MODE_CURRENT ? |
| 5710 | ", current" : "", |
| 5711 | dm->mode_info.clock / 1000.0); |
| 5712 | } |
| 5713 | } |
| 5714 | |
Pekka Paalanen | c4db6f7 | 2017-09-05 16:37:03 +0300 | [diff] [blame] | 5715 | static int |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5716 | drm_output_enable(struct weston_output *base) |
| 5717 | { |
| 5718 | struct drm_output *output = to_drm_output(base); |
| 5719 | struct drm_backend *b = to_drm_backend(base->compositor); |
Pekka Paalanen | 663d5e9 | 2017-09-08 13:32:40 +0300 | [diff] [blame] | 5720 | drmModeRes *resources; |
| 5721 | int ret; |
| 5722 | |
| 5723 | resources = drmModeGetResources(b->drm.fd); |
| 5724 | if (!resources) { |
| 5725 | weston_log("drmModeGetResources failed\n"); |
| 5726 | return -1; |
| 5727 | } |
Pekka Paalanen | 27cc481 | 2017-11-20 13:31:06 +0200 | [diff] [blame] | 5728 | ret = drm_output_init_crtc(output, resources); |
Pekka Paalanen | 663d5e9 | 2017-09-08 13:32:40 +0300 | [diff] [blame] | 5729 | drmModeFreeResources(resources); |
| 5730 | if (ret < 0) |
| 5731 | return -1; |
| 5732 | |
| 5733 | if (drm_output_init_gamma_size(output) < 0) |
| 5734 | goto err; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5735 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 5736 | if (b->pageflip_timeout) |
| 5737 | drm_output_pageflip_timer_create(output); |
| 5738 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5739 | if (b->use_pixman) { |
| 5740 | if (drm_output_init_pixman(output, b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 5741 | weston_log("Failed to init output pixman state\n"); |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 5742 | goto err; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 5743 | } |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5744 | } else if (drm_output_init_egl(output, b) < 0) { |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 5745 | weston_log("Failed to init output gl state\n"); |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 5746 | goto err; |
Kristian Høgsberg | 1d1e0a5 | 2012-10-21 13:29:26 -0400 | [diff] [blame] | 5747 | } |
Kristian Høgsberg | 8e1f77f | 2012-05-03 11:39:35 -0400 | [diff] [blame] | 5748 | |
Pekka Paalanen | f8b850d | 2017-11-15 12:51:01 +0200 | [diff] [blame] | 5749 | drm_output_init_backlight(output); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 5750 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 5751 | output->base.start_repaint_loop = drm_output_start_repaint_loop; |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 5752 | output->base.repaint = drm_output_repaint; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 5753 | output->base.assign_planes = drm_assign_planes; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 5754 | output->base.set_dpms = drm_set_dpms; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 5755 | output->base.switch_mode = drm_output_switch_mode; |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 5756 | output->base.set_gamma = drm_output_set_gamma; |
| 5757 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 5758 | if (output->cursor_plane) |
| 5759 | weston_compositor_stack_plane(b->compositor, |
| 5760 | &output->cursor_plane->base, |
| 5761 | NULL); |
| 5762 | else |
| 5763 | b->cursors_are_broken = 1; |
| 5764 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 5765 | weston_compositor_stack_plane(b->compositor, |
| 5766 | &output->scanout_plane->base, |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5767 | &b->compositor->primary_plane); |
Ander Conselvan de Oliveira | 8ad1982 | 2013-03-05 17:30:27 +0200 | [diff] [blame] | 5768 | |
Pekka Paalanen | c0eb254 | 2017-11-15 13:37:18 +0200 | [diff] [blame] | 5769 | weston_log("Output %s (crtc %d) video modes:\n", |
| 5770 | output->base.name, output->crtc_id); |
| 5771 | drm_output_print_modes(output); |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 5772 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5773 | return 0; |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 5774 | |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 5775 | err: |
Pekka Paalanen | 663d5e9 | 2017-09-08 13:32:40 +0300 | [diff] [blame] | 5776 | drm_output_fini_crtc(output); |
| 5777 | |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 5778 | return -1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5779 | } |
| 5780 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 5781 | static void |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5782 | drm_output_deinit(struct weston_output *base) |
| 5783 | { |
| 5784 | struct drm_output *output = to_drm_output(base); |
| 5785 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 5786 | |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 5787 | if (b->use_pixman) |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5788 | drm_output_fini_pixman(output); |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 5789 | else |
| 5790 | drm_output_fini_egl(output); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5791 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 5792 | /* Since our planes are no longer in use anywhere, remove their base |
| 5793 | * weston_plane's link from the plane stacking list, unless we're |
| 5794 | * shutting down, in which case the plane has already been |
| 5795 | * destroyed. */ |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 5796 | if (!b->shutting_down) { |
| 5797 | wl_list_remove(&output->scanout_plane->base.link); |
| 5798 | wl_list_init(&output->scanout_plane->base.link); |
| 5799 | |
| 5800 | if (output->cursor_plane) { |
| 5801 | wl_list_remove(&output->cursor_plane->base.link); |
| 5802 | wl_list_init(&output->cursor_plane->base.link); |
| 5803 | /* Turn off hardware cursor */ |
| 5804 | drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0); |
| 5805 | } |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 5806 | } |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 5807 | |
Pekka Paalanen | 663d5e9 | 2017-09-08 13:32:40 +0300 | [diff] [blame] | 5808 | drm_output_fini_crtc(output); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5809 | } |
| 5810 | |
| 5811 | static void |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 5812 | drm_head_destroy(struct drm_head *head); |
| 5813 | |
| 5814 | static void |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5815 | drm_output_destroy(struct weston_output *base) |
| 5816 | { |
| 5817 | struct drm_output *output = to_drm_output(base); |
| 5818 | struct drm_backend *b = to_drm_backend(base->compositor); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5819 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 5820 | if (output->page_flip_pending || output->vblank_pending || |
| 5821 | output->atomic_complete_pending) { |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5822 | output->destroy_pending = 1; |
| 5823 | weston_log("destroy output while page flip pending\n"); |
| 5824 | return; |
| 5825 | } |
| 5826 | |
| 5827 | if (output->base.enabled) |
| 5828 | drm_output_deinit(&output->base); |
| 5829 | |
Pekka Paalanen | 383b3af | 2017-09-11 14:40:48 +0300 | [diff] [blame] | 5830 | drm_mode_list_destroy(b, &output->base.mode_list); |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 5831 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 5832 | if (output->pageflip_timer) |
| 5833 | wl_event_source_remove(output->pageflip_timer); |
| 5834 | |
Pekka Paalanen | ae6d35d | 2017-08-16 12:07:14 +0300 | [diff] [blame] | 5835 | weston_output_release(&output->base); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5836 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 5837 | assert(!output->state_last); |
| 5838 | drm_output_state_free(output->state_cur); |
| 5839 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5840 | free(output); |
| 5841 | } |
| 5842 | |
| 5843 | static int |
| 5844 | drm_output_disable(struct weston_output *base) |
| 5845 | { |
| 5846 | struct drm_output *output = to_drm_output(base); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5847 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 5848 | if (output->page_flip_pending || output->vblank_pending || |
| 5849 | output->atomic_complete_pending) { |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5850 | output->disable_pending = 1; |
| 5851 | return -1; |
| 5852 | } |
| 5853 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 5854 | weston_log("Disabling output %s\n", output->base.name); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 5855 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5856 | if (output->base.enabled) |
| 5857 | drm_output_deinit(&output->base); |
| 5858 | |
| 5859 | output->disable_pending = 0; |
| 5860 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5861 | return 0; |
| 5862 | } |
| 5863 | |
| 5864 | /** |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 5865 | * Update the list of unused connectors and CRTCs |
| 5866 | * |
Pekka Paalanen | eacec81 | 2017-09-12 13:43:51 +0300 | [diff] [blame] | 5867 | * This keeps the unused_crtc arrays up to date. |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 5868 | * |
| 5869 | * @param b Weston backend structure |
| 5870 | * @param resources DRM resources for this device |
| 5871 | */ |
| 5872 | static void |
| 5873 | drm_backend_update_unused_outputs(struct drm_backend *b, drmModeRes *resources) |
| 5874 | { |
| 5875 | int i; |
| 5876 | |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 5877 | wl_array_release(&b->unused_crtcs); |
| 5878 | wl_array_init(&b->unused_crtcs); |
| 5879 | |
| 5880 | for (i = 0; i < resources->count_crtcs; i++) { |
| 5881 | struct drm_output *output; |
| 5882 | uint32_t *crtc_id; |
| 5883 | |
| 5884 | output = drm_output_find_by_crtc(b, resources->crtcs[i]); |
| 5885 | if (output && output->base.enabled) |
| 5886 | continue; |
| 5887 | |
| 5888 | crtc_id = wl_array_add(&b->unused_crtcs, sizeof(*crtc_id)); |
| 5889 | *crtc_id = resources->crtcs[i]; |
| 5890 | } |
| 5891 | } |
| 5892 | |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 5893 | /** Replace connector data and monitor information |
| 5894 | * |
| 5895 | * @param head The head to update. |
| 5896 | * @param connector The connector data to be owned by the head, must match |
| 5897 | * the head's connector ID. |
| 5898 | * @return 0 on success, -1 on failure. |
| 5899 | * |
| 5900 | * Takes ownership of @c connector on success, not on failure. |
| 5901 | * |
| 5902 | * May schedule a heads changed call. |
| 5903 | */ |
| 5904 | static int |
| 5905 | drm_head_assign_connector_info(struct drm_head *head, |
| 5906 | drmModeConnector *connector) |
| 5907 | { |
| 5908 | drmModeObjectProperties *props; |
| 5909 | const char *make = "unknown"; |
| 5910 | const char *model = "unknown"; |
| 5911 | const char *serial_number = "unknown"; |
| 5912 | |
| 5913 | assert(connector); |
| 5914 | assert(head->connector_id == connector->connector_id); |
| 5915 | |
| 5916 | props = drmModeObjectGetProperties(head->backend->drm.fd, |
| 5917 | head->connector_id, |
| 5918 | DRM_MODE_OBJECT_CONNECTOR); |
| 5919 | if (!props) { |
| 5920 | weston_log("Error: failed to get connector '%s' properties\n", |
| 5921 | head->base.name); |
| 5922 | return -1; |
| 5923 | } |
| 5924 | |
| 5925 | if (head->connector) |
| 5926 | drmModeFreeConnector(head->connector); |
| 5927 | head->connector = connector; |
| 5928 | |
| 5929 | drm_property_info_populate(head->backend, connector_props, |
| 5930 | head->props_conn, |
| 5931 | WDRM_CONNECTOR__COUNT, props); |
| 5932 | find_and_parse_output_edid(head, props, &make, &model, &serial_number); |
| 5933 | weston_head_set_monitor_strings(&head->base, make, model, serial_number); |
| 5934 | weston_head_set_subpixel(&head->base, |
| 5935 | drm_subpixel_to_wayland(head->connector->subpixel)); |
| 5936 | |
| 5937 | weston_head_set_physical_size(&head->base, head->connector->mmWidth, |
| 5938 | head->connector->mmHeight); |
| 5939 | |
| 5940 | drmModeFreeObjectProperties(props); |
| 5941 | |
| 5942 | /* Unknown connection status is assumed disconnected. */ |
| 5943 | weston_head_set_connection_status(&head->base, |
| 5944 | head->connector->connection == DRM_MODE_CONNECTED); |
| 5945 | |
| 5946 | return 0; |
| 5947 | } |
| 5948 | |
Pekka Paalanen | 456dc73 | 2017-11-09 15:10:11 +0200 | [diff] [blame] | 5949 | static void |
| 5950 | drm_head_log_info(struct drm_head *head, const char *msg) |
| 5951 | { |
| 5952 | if (head->base.connected) { |
| 5953 | weston_log("DRM: head '%s' %s, connector %d is connected, " |
| 5954 | "EDID make '%s', model '%s', serial '%s'\n", |
| 5955 | head->base.name, msg, head->connector_id, |
| 5956 | head->base.make, head->base.model, |
| 5957 | head->base.serial_number ?: ""); |
| 5958 | } else { |
| 5959 | weston_log("DRM: head '%s' %s, connector %d is disconnected.\n", |
| 5960 | head->base.name, msg, head->connector_id); |
| 5961 | } |
| 5962 | } |
| 5963 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5964 | /** Update connector and monitor information |
| 5965 | * |
| 5966 | * @param head The head to update. |
| 5967 | * |
| 5968 | * Re-reads the DRM property lists for the connector and updates monitor |
| 5969 | * information and connection status. This may schedule a heads changed call |
| 5970 | * to the user. |
| 5971 | */ |
| 5972 | static void |
| 5973 | drm_head_update_info(struct drm_head *head) |
| 5974 | { |
| 5975 | drmModeConnector *connector; |
| 5976 | |
| 5977 | connector = drmModeGetConnector(head->backend->drm.fd, |
| 5978 | head->connector_id); |
| 5979 | if (!connector) { |
| 5980 | weston_log("DRM: getting connector info for '%s' failed.\n", |
| 5981 | head->base.name); |
| 5982 | return; |
| 5983 | } |
| 5984 | |
| 5985 | if (drm_head_assign_connector_info(head, connector) < 0) |
| 5986 | drmModeFreeConnector(connector); |
Pekka Paalanen | 456dc73 | 2017-11-09 15:10:11 +0200 | [diff] [blame] | 5987 | |
| 5988 | if (head->base.device_changed) |
| 5989 | drm_head_log_info(head, "updated"); |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5990 | } |
| 5991 | |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 5992 | /** |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 5993 | * Create a Weston head for a connector |
| 5994 | * |
| 5995 | * Given a DRM connector, create a matching drm_head structure and add it |
| 5996 | * to Weston's head list. |
| 5997 | * |
| 5998 | * @param b Weston backend structure |
| 5999 | * @param connector_id DRM connector ID for the head |
| 6000 | * @param drm_device udev device pointer |
| 6001 | * @returns The new head, or NULL on failure. |
| 6002 | */ |
| 6003 | static struct drm_head * |
| 6004 | drm_head_create(struct drm_backend *backend, uint32_t connector_id, |
| 6005 | struct udev_device *drm_device) |
| 6006 | { |
| 6007 | struct drm_head *head; |
| 6008 | drmModeConnector *connector; |
| 6009 | char *name; |
| 6010 | |
| 6011 | head = zalloc(sizeof *head); |
| 6012 | if (!head) |
| 6013 | return NULL; |
| 6014 | |
| 6015 | connector = drmModeGetConnector(backend->drm.fd, connector_id); |
| 6016 | if (!connector) |
| 6017 | goto err_alloc; |
| 6018 | |
| 6019 | name = make_connector_name(connector); |
| 6020 | if (!name) |
| 6021 | goto err_alloc; |
| 6022 | |
| 6023 | weston_head_init(&head->base, name); |
| 6024 | free(name); |
| 6025 | |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 6026 | head->connector_id = connector_id; |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 6027 | head->backend = backend; |
| 6028 | |
Pekka Paalanen | ce72424 | 2017-09-04 12:21:24 +0300 | [diff] [blame] | 6029 | head->backlight = backlight_init(drm_device, connector->connector_type); |
| 6030 | |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 6031 | if (drm_head_assign_connector_info(head, connector) < 0) |
| 6032 | goto err_init; |
| 6033 | |
| 6034 | if (head->connector->connector_type == DRM_MODE_CONNECTOR_LVDS || |
| 6035 | head->connector->connector_type == DRM_MODE_CONNECTOR_eDP) |
| 6036 | weston_head_set_internal(&head->base); |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 6037 | |
Pekka Paalanen | 9c03a7c | 2017-11-28 14:30:10 +0200 | [diff] [blame] | 6038 | if (drm_head_read_current_setup(head, backend) < 0) { |
Pekka Paalanen | 13d233e | 2017-09-11 14:06:11 +0300 | [diff] [blame] | 6039 | weston_log("Failed to retrieve current mode from connector %d.\n", |
| 6040 | head->connector_id); |
Pekka Paalanen | 6fae2be | 2017-11-28 14:33:52 +0200 | [diff] [blame] | 6041 | /* Not fatal. */ |
Pekka Paalanen | 13d233e | 2017-09-11 14:06:11 +0300 | [diff] [blame] | 6042 | } |
| 6043 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 6044 | weston_compositor_add_head(backend->compositor, &head->base); |
Pekka Paalanen | 456dc73 | 2017-11-09 15:10:11 +0200 | [diff] [blame] | 6045 | drm_head_log_info(head, "found"); |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 6046 | |
| 6047 | return head; |
| 6048 | |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 6049 | err_init: |
| 6050 | weston_head_release(&head->base); |
| 6051 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 6052 | err_alloc: |
| 6053 | if (connector) |
| 6054 | drmModeFreeConnector(connector); |
| 6055 | |
| 6056 | free(head); |
| 6057 | |
| 6058 | return NULL; |
| 6059 | } |
| 6060 | |
| 6061 | static void |
| 6062 | drm_head_destroy(struct drm_head *head) |
| 6063 | { |
| 6064 | weston_head_release(&head->base); |
Pekka Paalanen | ce72424 | 2017-09-04 12:21:24 +0300 | [diff] [blame] | 6065 | |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 6066 | drm_property_info_free(head->props_conn, WDRM_CONNECTOR__COUNT); |
| 6067 | drmModeFreeConnector(head->connector); |
| 6068 | |
Pekka Paalanen | ce72424 | 2017-09-04 12:21:24 +0300 | [diff] [blame] | 6069 | if (head->backlight) |
| 6070 | backlight_destroy(head->backlight); |
| 6071 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 6072 | free(head); |
| 6073 | } |
| 6074 | |
| 6075 | /** |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 6076 | * Create a Weston output structure |
| 6077 | * |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 6078 | * Create an "empty" drm_output. This is the implementation of |
| 6079 | * weston_backend::create_output. |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 6080 | * |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 6081 | * Creating an output is usually followed by drm_output_attach_head() |
| 6082 | * and drm_output_enable() to make use of it. |
| 6083 | * |
| 6084 | * @param compositor The compositor instance. |
| 6085 | * @param name Name for the new output. |
| 6086 | * @returns The output, or NULL on failure. |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 6087 | */ |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 6088 | static struct weston_output * |
| 6089 | drm_output_create(struct weston_compositor *compositor, const char *name) |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 6090 | { |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 6091 | struct drm_backend *b = to_drm_backend(compositor); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 6092 | struct drm_output *output; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 6093 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 6094 | output = zalloc(sizeof *output); |
| 6095 | if (output == NULL) |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 6096 | return NULL; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 6097 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 6098 | weston_output_init(&output->base, compositor, name); |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 6099 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 6100 | output->base.enable = drm_output_enable; |
| 6101 | output->base.destroy = drm_output_destroy; |
| 6102 | output->base.disable = drm_output_disable; |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 6103 | output->base.attach_head = drm_output_attach_head; |
Pekka Paalanen | 7f85379 | 2017-11-29 14:33:33 +0200 | [diff] [blame] | 6104 | output->base.detach_head = drm_output_detach_head; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 6105 | |
| 6106 | output->destroy_pending = 0; |
| 6107 | output->disable_pending = 0; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 6108 | |
Pekka Paalanen | 01f6021 | 2017-03-24 15:39:24 +0200 | [diff] [blame] | 6109 | output->state_cur = drm_output_state_alloc(output, NULL); |
Pekka Paalanen | a0bfedc | 2017-04-03 14:42:51 +0300 | [diff] [blame] | 6110 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 6111 | weston_compositor_add_pending_output(&output->base, b->compositor); |
| 6112 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 6113 | return &output->base; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 6114 | } |
| 6115 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 6116 | static int |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 6117 | 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] | 6118 | { |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 6119 | struct drm_head *head; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 6120 | drmModeRes *resources; |
| 6121 | int i; |
| 6122 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6123 | resources = drmModeGetResources(b->drm.fd); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 6124 | if (!resources) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 6125 | weston_log("drmModeGetResources failed\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 6126 | return -1; |
| 6127 | } |
| 6128 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6129 | b->min_width = resources->min_width; |
| 6130 | b->max_width = resources->max_width; |
| 6131 | b->min_height = resources->min_height; |
| 6132 | b->max_height = resources->max_height; |
Rob Clark | 4339add | 2012-08-09 14:18:28 -0500 | [diff] [blame] | 6133 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 6134 | for (i = 0; i < resources->count_connectors; i++) { |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 6135 | uint32_t connector_id = resources->connectors[i]; |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 6136 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 6137 | head = drm_head_create(b, connector_id, drm_device); |
| 6138 | if (!head) { |
| 6139 | weston_log("DRM: failed to create head for connector %d.\n", |
| 6140 | connector_id); |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 6141 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 6142 | } |
| 6143 | |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 6144 | drm_backend_update_unused_outputs(b, resources); |
| 6145 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 6146 | drmModeFreeResources(resources); |
| 6147 | |
| 6148 | return 0; |
| 6149 | } |
| 6150 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 6151 | static void |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 6152 | 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] | 6153 | { |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 6154 | drmModeRes *resources; |
Pekka Paalanen | a0a3746 | 2017-08-31 15:41:57 +0300 | [diff] [blame] | 6155 | struct weston_head *base, *next; |
| 6156 | struct drm_head *head; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 6157 | int i; |
| 6158 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6159 | resources = drmModeGetResources(b->drm.fd); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 6160 | if (!resources) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 6161 | weston_log("drmModeGetResources failed\n"); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 6162 | return; |
| 6163 | } |
| 6164 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 6165 | /* collect new connectors that have appeared, e.g. MST */ |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 6166 | for (i = 0; i < resources->count_connectors; i++) { |
Ucan, Emre (ADITG/SW1) | 21e4944 | 2017-02-02 14:06:55 +0000 | [diff] [blame] | 6167 | uint32_t connector_id = resources->connectors[i]; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 6168 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 6169 | head = drm_head_find_by_connector(b, connector_id); |
| 6170 | if (head) { |
| 6171 | drm_head_update_info(head); |
| 6172 | } else { |
| 6173 | head = drm_head_create(b, connector_id, drm_device); |
| 6174 | if (!head) |
| 6175 | weston_log("DRM: failed to create head for hot-added connector %d.\n", |
| 6176 | connector_id); |
David Herrmann | 7551cff | 2011-12-08 17:05:43 +0100 | [diff] [blame] | 6177 | } |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 6178 | } |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 6179 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 6180 | /* Remove connectors that have disappeared. */ |
Pekka Paalanen | a0a3746 | 2017-08-31 15:41:57 +0300 | [diff] [blame] | 6181 | wl_list_for_each_safe(base, next, |
| 6182 | &b->compositor->head_list, compositor_link) { |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 6183 | bool removed = true; |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 6184 | |
Pekka Paalanen | a0a3746 | 2017-08-31 15:41:57 +0300 | [diff] [blame] | 6185 | head = to_drm_head(base); |
| 6186 | |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 6187 | for (i = 0; i < resources->count_connectors; i++) { |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 6188 | if (resources->connectors[i] == head->connector_id) { |
| 6189 | removed = false; |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 6190 | break; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 6191 | } |
| 6192 | } |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 6193 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 6194 | if (!removed) |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 6195 | continue; |
| 6196 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 6197 | weston_log("DRM: head '%s' (connector %d) disappeared.\n", |
| 6198 | head->base.name, head->connector_id); |
| 6199 | drm_head_destroy(head); |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 6200 | } |
| 6201 | |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 6202 | drm_backend_update_unused_outputs(b, resources); |
| 6203 | |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 6204 | drmModeFreeResources(resources); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 6205 | } |
| 6206 | |
| 6207 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6208 | udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 6209 | { |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 6210 | const char *sysnum; |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 6211 | const char *val; |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 6212 | |
| 6213 | sysnum = udev_device_get_sysnum(device); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6214 | if (!sysnum || atoi(sysnum) != b->drm.id) |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 6215 | return 0; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 6216 | |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 6217 | val = udev_device_get_property_value(device, "HOTPLUG"); |
| 6218 | if (!val) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 6219 | return 0; |
| 6220 | |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 6221 | return strcmp(val, "1") == 0; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 6222 | } |
| 6223 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 6224 | static int |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 6225 | udev_drm_event(int fd, uint32_t mask, void *data) |
| 6226 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6227 | struct drm_backend *b = data; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 6228 | struct udev_device *event; |
| 6229 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6230 | event = udev_monitor_receive_device(b->udev_monitor); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 6231 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6232 | if (udev_event_is_hotplug(b, event)) |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 6233 | drm_backend_update_heads(b, event); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 6234 | |
| 6235 | udev_device_unref(event); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 6236 | |
| 6237 | return 1; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 6238 | } |
| 6239 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 6240 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 6241 | drm_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 6242 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 6243 | struct drm_backend *b = to_drm_backend(ec); |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 6244 | struct weston_head *base, *next; |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 6245 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6246 | udev_input_destroy(&b->input); |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 6247 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6248 | wl_event_source_remove(b->udev_drm_source); |
| 6249 | wl_event_source_remove(b->drm_source); |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 6250 | |
Daniel Stone | b57c6a0 | 2017-10-05 16:27:21 +0100 | [diff] [blame] | 6251 | b->shutting_down = true; |
| 6252 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6253 | destroy_sprites(b); |
Kristian Høgsberg | 3d64a3e | 2013-05-10 12:36:04 -0400 | [diff] [blame] | 6254 | |
Ander Conselvan de Oliveira | 6b16214 | 2013-10-25 16:26:32 +0300 | [diff] [blame] | 6255 | weston_compositor_shutdown(ec); |
| 6256 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 6257 | wl_list_for_each_safe(base, next, &ec->head_list, compositor_link) |
| 6258 | drm_head_destroy(to_drm_head(base)); |
| 6259 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6260 | if (b->gbm) |
| 6261 | gbm_device_destroy(b->gbm); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 6262 | |
Pekka Paalanen | 5b0aa55 | 2017-12-07 16:06:05 +0200 | [diff] [blame] | 6263 | udev_monitor_unref(b->udev_monitor); |
Pekka Paalanen | 2a0c6c3 | 2017-09-13 16:48:01 +0300 | [diff] [blame] | 6264 | udev_unref(b->udev); |
| 6265 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6266 | weston_launcher_destroy(ec->launcher); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 6267 | |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 6268 | wl_array_release(&b->unused_crtcs); |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 6269 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6270 | close(b->drm.fd); |
Pekka Paalanen | 9bf4f37 | 2017-12-07 16:05:29 +0200 | [diff] [blame] | 6271 | free(b->drm.filename); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6272 | free(b); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 6273 | } |
| 6274 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 6275 | static void |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 6276 | session_notify(struct wl_listener *listener, void *data) |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 6277 | { |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 6278 | struct weston_compositor *compositor = data; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 6279 | struct drm_backend *b = to_drm_backend(compositor); |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 6280 | struct drm_plane *plane; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 6281 | struct drm_output *output; |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 6282 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6283 | if (compositor->session_active) { |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 6284 | weston_log("activating session\n"); |
Daniel Stone | f33e104 | 2016-11-05 08:10:13 +0000 | [diff] [blame] | 6285 | weston_compositor_wake(compositor); |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 6286 | weston_compositor_damage_all(compositor); |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 6287 | b->state_invalid = true; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6288 | udev_input_enable(&b->input); |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 6289 | } else { |
| 6290 | weston_log("deactivating session\n"); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6291 | udev_input_disable(&b->input); |
Kristian Høgsberg | 4014a6b | 2012-04-10 00:08:45 -0400 | [diff] [blame] | 6292 | |
Philipp Brüschweiler | 57edf7f | 2013-03-29 13:01:56 +0100 | [diff] [blame] | 6293 | weston_compositor_offscreen(compositor); |
Kristian Høgsberg | d8e181b | 2011-05-06 15:38:28 -0400 | [diff] [blame] | 6294 | |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 6295 | /* If we have a repaint scheduled (either from a |
| 6296 | * pending pageflip or the idle handler), make sure we |
| 6297 | * 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] | 6298 | * vt switched away. The OFFSCREEN state will prevent |
Abdur Rehman | 4dca0e1 | 2017-01-01 19:46:35 +0500 | [diff] [blame] | 6299 | * further attempts at repainting. When we switch |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 6300 | * back, we schedule a repaint, which will process |
| 6301 | * pending frame callbacks. */ |
| 6302 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6303 | wl_list_for_each(output, &compositor->output_list, base.link) { |
Daniel Stone | 09a97e2 | 2017-03-01 11:34:06 +0000 | [diff] [blame] | 6304 | output->base.repaint_needed = false; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 6305 | if (output->cursor_plane) |
| 6306 | drmModeSetCursor(b->drm.fd, output->crtc_id, |
| 6307 | 0, 0, 0); |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 6308 | } |
| 6309 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6310 | output = container_of(compositor->output_list.next, |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 6311 | struct drm_output, base.link); |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 6312 | |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 6313 | wl_list_for_each(plane, &b->plane_list, link) { |
| 6314 | if (plane->type != WDRM_PLANE_TYPE_OVERLAY) |
| 6315 | continue; |
| 6316 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6317 | drmModeSetPlane(b->drm.fd, |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 6318 | plane->plane_id, |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 6319 | output->crtc_id, 0, 0, |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 6320 | 0, 0, 0, 0, 0, 0, 0, 0); |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 6321 | } |
| 6322 | } |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 6323 | } |
| 6324 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 6325 | /** |
| 6326 | * Determines whether or not a device is capable of modesetting. If successful, |
| 6327 | * sets b->drm.fd and b->drm.filename to the opened device. |
| 6328 | */ |
| 6329 | static bool |
| 6330 | drm_device_is_kms(struct drm_backend *b, struct udev_device *device) |
| 6331 | { |
| 6332 | const char *filename = udev_device_get_devnode(device); |
| 6333 | const char *sysnum = udev_device_get_sysnum(device); |
| 6334 | drmModeRes *res; |
| 6335 | int id, fd; |
| 6336 | |
| 6337 | if (!filename) |
| 6338 | return false; |
| 6339 | |
| 6340 | fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR); |
| 6341 | if (fd < 0) |
| 6342 | return false; |
| 6343 | |
| 6344 | res = drmModeGetResources(fd); |
| 6345 | if (!res) |
| 6346 | goto out_fd; |
| 6347 | |
| 6348 | if (res->count_crtcs <= 0 || res->count_connectors <= 0 || |
| 6349 | res->count_encoders <= 0) |
| 6350 | goto out_res; |
| 6351 | |
| 6352 | if (sysnum) |
| 6353 | id = atoi(sysnum); |
| 6354 | if (!sysnum || id < 0) { |
| 6355 | weston_log("couldn't get sysnum for device %s\n", filename); |
| 6356 | goto out_res; |
| 6357 | } |
| 6358 | |
| 6359 | /* We can be called successfully on multiple devices; if we have, |
| 6360 | * clean up old entries. */ |
| 6361 | if (b->drm.fd >= 0) |
| 6362 | weston_launcher_close(b->compositor->launcher, b->drm.fd); |
| 6363 | free(b->drm.filename); |
| 6364 | |
| 6365 | b->drm.fd = fd; |
| 6366 | b->drm.id = id; |
| 6367 | b->drm.filename = strdup(filename); |
| 6368 | |
Sergi Granell | ceb5981 | 2017-03-28 12:44:04 +0200 | [diff] [blame] | 6369 | drmModeFreeResources(res); |
| 6370 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 6371 | return true; |
| 6372 | |
| 6373 | out_res: |
| 6374 | drmModeFreeResources(res); |
| 6375 | out_fd: |
| 6376 | weston_launcher_close(b->compositor->launcher, fd); |
| 6377 | return false; |
| 6378 | } |
| 6379 | |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 6380 | /* |
| 6381 | * Find primary GPU |
| 6382 | * Some systems may have multiple DRM devices attached to a single seat. This |
| 6383 | * function loops over all devices and tries to find a PCI device with the |
| 6384 | * boot_vga sysfs attribute set to 1. |
| 6385 | * 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] | 6386 | * Devices are also vetted to make sure they are are capable of modesetting, |
| 6387 | * rather than pure render nodes (GPU with no display), or pure |
| 6388 | * memory-allocation devices (VGEM). |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 6389 | */ |
| 6390 | static struct udev_device* |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6391 | find_primary_gpu(struct drm_backend *b, const char *seat) |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 6392 | { |
| 6393 | struct udev_enumerate *e; |
| 6394 | struct udev_list_entry *entry; |
| 6395 | const char *path, *device_seat, *id; |
| 6396 | struct udev_device *device, *drm_device, *pci; |
| 6397 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6398 | e = udev_enumerate_new(b->udev); |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 6399 | udev_enumerate_add_match_subsystem(e, "drm"); |
| 6400 | udev_enumerate_add_match_sysname(e, "card[0-9]*"); |
| 6401 | |
| 6402 | udev_enumerate_scan_devices(e); |
| 6403 | drm_device = NULL; |
| 6404 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 6405 | bool is_boot_vga = false; |
| 6406 | |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 6407 | path = udev_list_entry_get_name(entry); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6408 | device = udev_device_new_from_syspath(b->udev, path); |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 6409 | if (!device) |
| 6410 | continue; |
| 6411 | device_seat = udev_device_get_property_value(device, "ID_SEAT"); |
| 6412 | if (!device_seat) |
| 6413 | device_seat = default_seat; |
| 6414 | if (strcmp(device_seat, seat)) { |
| 6415 | udev_device_unref(device); |
| 6416 | continue; |
| 6417 | } |
| 6418 | |
| 6419 | pci = udev_device_get_parent_with_subsystem_devtype(device, |
| 6420 | "pci", NULL); |
| 6421 | if (pci) { |
| 6422 | id = udev_device_get_sysattr_value(pci, "boot_vga"); |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 6423 | if (id && !strcmp(id, "1")) |
| 6424 | is_boot_vga = true; |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 6425 | } |
| 6426 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 6427 | /* If we already have a modesetting-capable device, and this |
| 6428 | * device isn't our boot-VGA device, we aren't going to use |
| 6429 | * it. */ |
| 6430 | if (!is_boot_vga && drm_device) { |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 6431 | udev_device_unref(device); |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 6432 | continue; |
| 6433 | } |
| 6434 | |
| 6435 | /* Make sure this device is actually capable of modesetting; |
| 6436 | * if this call succeeds, b->drm.{fd,filename} will be set, |
| 6437 | * and any old values freed. */ |
| 6438 | if (!drm_device_is_kms(b, device)) { |
| 6439 | udev_device_unref(device); |
| 6440 | continue; |
| 6441 | } |
| 6442 | |
| 6443 | /* There can only be one boot_vga device, and we try to use it |
| 6444 | * at all costs. */ |
| 6445 | if (is_boot_vga) { |
| 6446 | if (drm_device) |
| 6447 | udev_device_unref(drm_device); |
| 6448 | drm_device = device; |
| 6449 | break; |
| 6450 | } |
| 6451 | |
| 6452 | /* Per the (!is_boot_vga && drm_device) test above, we only |
| 6453 | * trump existing saved devices with boot-VGA devices, so if |
| 6454 | * we end up here, this must be the first device we've seen. */ |
| 6455 | assert(!drm_device); |
| 6456 | drm_device = device; |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 6457 | } |
| 6458 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 6459 | /* If we're returning a device to use, we must have an open FD for |
| 6460 | * it. */ |
| 6461 | assert(!!drm_device == (b->drm.fd >= 0)); |
| 6462 | |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 6463 | udev_enumerate_unref(e); |
| 6464 | return drm_device; |
| 6465 | } |
| 6466 | |
Pekka Paalanen | b45ed8b | 2017-03-28 18:04:27 +0300 | [diff] [blame] | 6467 | static struct udev_device * |
| 6468 | open_specific_drm_device(struct drm_backend *b, const char *name) |
| 6469 | { |
| 6470 | struct udev_device *device; |
| 6471 | |
| 6472 | device = udev_device_new_from_subsystem_sysname(b->udev, "drm", name); |
| 6473 | if (!device) { |
| 6474 | weston_log("ERROR: could not open DRM device '%s'\n", name); |
| 6475 | return NULL; |
| 6476 | } |
| 6477 | |
| 6478 | if (!drm_device_is_kms(b, device)) { |
| 6479 | udev_device_unref(device); |
| 6480 | weston_log("ERROR: DRM device '%s' is not a KMS device.\n", name); |
| 6481 | return NULL; |
| 6482 | } |
| 6483 | |
| 6484 | /* If we're returning a device to use, we must have an open FD for |
| 6485 | * it. */ |
| 6486 | assert(b->drm.fd >= 0); |
| 6487 | |
| 6488 | return device; |
| 6489 | } |
| 6490 | |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 6491 | static void |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 6492 | planes_binding(struct weston_keyboard *keyboard, const struct timespec *time, |
| 6493 | uint32_t key, void *data) |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 6494 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6495 | struct drm_backend *b = data; |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 6496 | |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 6497 | switch (key) { |
| 6498 | case KEY_C: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6499 | b->cursors_are_broken ^= 1; |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 6500 | break; |
| 6501 | case KEY_V: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6502 | b->sprites_are_broken ^= 1; |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 6503 | break; |
| 6504 | case KEY_O: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6505 | b->sprites_hidden ^= 1; |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 6506 | break; |
| 6507 | default: |
| 6508 | break; |
| 6509 | } |
Ander Conselvan de Oliveira | 180f42a | 2012-11-21 15:11:37 +0200 | [diff] [blame] | 6510 | } |
| 6511 | |
Kristian Høgsberg | 0eac34a | 2013-08-30 14:28:22 -0700 | [diff] [blame] | 6512 | #ifdef BUILD_VAAPI_RECORDER |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 6513 | static void |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 6514 | recorder_destroy(struct drm_output *output) |
| 6515 | { |
| 6516 | vaapi_recorder_destroy(output->recorder); |
| 6517 | output->recorder = NULL; |
| 6518 | |
| 6519 | output->base.disable_planes--; |
| 6520 | |
| 6521 | wl_list_remove(&output->recorder_frame_listener.link); |
| 6522 | weston_log("[libva recorder] done\n"); |
| 6523 | } |
| 6524 | |
| 6525 | static void |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 6526 | recorder_frame_notify(struct wl_listener *listener, void *data) |
| 6527 | { |
| 6528 | struct drm_output *output; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6529 | struct drm_backend *b; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 6530 | int fd, ret; |
| 6531 | |
| 6532 | output = container_of(listener, struct drm_output, |
| 6533 | recorder_frame_listener); |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 6534 | b = to_drm_backend(output->base.compositor); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 6535 | |
| 6536 | if (!output->recorder) |
| 6537 | return; |
| 6538 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 6539 | ret = drmPrimeHandleToFD(b->drm.fd, |
Daniel Stone | 8eece0c | 2016-11-17 17:54:00 +0000 | [diff] [blame] | 6540 | output->scanout_plane->state_cur->fb->handles[0], |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 6541 | DRM_CLOEXEC, &fd); |
| 6542 | if (ret) { |
| 6543 | weston_log("[libva recorder] " |
| 6544 | "failed to create prime fd for front buffer\n"); |
| 6545 | return; |
| 6546 | } |
| 6547 | |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 6548 | ret = vaapi_recorder_frame(output->recorder, fd, |
Daniel Stone | 8eece0c | 2016-11-17 17:54:00 +0000 | [diff] [blame] | 6549 | output->scanout_plane->state_cur->fb->strides[0]); |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 6550 | if (ret < 0) { |
| 6551 | weston_log("[libva recorder] aborted: %m\n"); |
| 6552 | recorder_destroy(output); |
| 6553 | } |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 6554 | } |
| 6555 | |
| 6556 | static void * |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6557 | create_recorder(struct drm_backend *b, int width, int height, |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 6558 | const char *filename) |
| 6559 | { |
| 6560 | int fd; |
| 6561 | drm_magic_t magic; |
| 6562 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6563 | fd = open(b->drm.filename, O_RDWR | O_CLOEXEC); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 6564 | if (fd < 0) |
| 6565 | return NULL; |
| 6566 | |
| 6567 | drmGetMagic(fd, &magic); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6568 | drmAuthMagic(b->drm.fd, magic); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 6569 | |
| 6570 | return vaapi_recorder_create(fd, width, height, filename); |
| 6571 | } |
| 6572 | |
| 6573 | static void |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 6574 | recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time, |
| 6575 | uint32_t key, void *data) |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 6576 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6577 | struct drm_backend *b = data; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 6578 | struct drm_output *output; |
| 6579 | int width, height; |
| 6580 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6581 | output = container_of(b->compositor->output_list.next, |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 6582 | struct drm_output, base.link); |
| 6583 | |
| 6584 | if (!output->recorder) { |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 6585 | if (output->gbm_format != GBM_FORMAT_XRGB8888) { |
Ander Conselvan de Oliveira | 2ef1cd1 | 2014-05-06 16:49:06 +0300 | [diff] [blame] | 6586 | weston_log("failed to start vaapi recorder: " |
| 6587 | "output format not supported\n"); |
| 6588 | return; |
| 6589 | } |
| 6590 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 6591 | width = output->base.current_mode->width; |
| 6592 | height = output->base.current_mode->height; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 6593 | |
| 6594 | output->recorder = |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6595 | create_recorder(b, width, height, "capture.h264"); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 6596 | if (!output->recorder) { |
| 6597 | weston_log("failed to create vaapi recorder\n"); |
| 6598 | return; |
| 6599 | } |
| 6600 | |
| 6601 | output->base.disable_planes++; |
| 6602 | |
| 6603 | output->recorder_frame_listener.notify = recorder_frame_notify; |
| 6604 | wl_signal_add(&output->base.frame_signal, |
| 6605 | &output->recorder_frame_listener); |
| 6606 | |
| 6607 | weston_output_schedule_repaint(&output->base); |
| 6608 | |
| 6609 | weston_log("[libva recorder] initialized\n"); |
| 6610 | } else { |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 6611 | recorder_destroy(output); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 6612 | } |
| 6613 | } |
| 6614 | #else |
| 6615 | static void |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 6616 | recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time, |
| 6617 | uint32_t key, void *data) |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 6618 | { |
| 6619 | weston_log("Compiled without libva support\n"); |
| 6620 | } |
| 6621 | #endif |
| 6622 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 6623 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6624 | switch_to_gl_renderer(struct drm_backend *b) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 6625 | { |
| 6626 | struct drm_output *output; |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 6627 | bool dmabuf_support_inited; |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 6628 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6629 | if (!b->use_pixman) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 6630 | return; |
| 6631 | |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 6632 | dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf; |
| 6633 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 6634 | weston_log("Switching to GL renderer\n"); |
| 6635 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6636 | b->gbm = create_gbm_device(b->drm.fd); |
| 6637 | if (!b->gbm) { |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 6638 | weston_log("Failed to create gbm device. " |
| 6639 | "Aborting renderer switch\n"); |
| 6640 | return; |
| 6641 | } |
| 6642 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6643 | 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] | 6644 | pixman_renderer_output_destroy(&output->base); |
| 6645 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6646 | b->compositor->renderer->destroy(b->compositor); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 6647 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6648 | if (drm_backend_create_gl_renderer(b) < 0) { |
| 6649 | gbm_device_destroy(b->gbm); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 6650 | weston_log("Failed to create GL renderer. Quitting.\n"); |
| 6651 | /* FIXME: we need a function to shutdown cleanly */ |
| 6652 | assert(0); |
| 6653 | } |
| 6654 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6655 | wl_list_for_each(output, &b->compositor->output_list, base.link) |
| 6656 | drm_output_init_egl(output, b); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 6657 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6658 | b->use_pixman = 0; |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 6659 | |
| 6660 | if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) { |
| 6661 | if (linux_dmabuf_setup(b->compositor) < 0) |
| 6662 | weston_log("Error: initializing dmabuf " |
| 6663 | "support failed.\n"); |
| 6664 | } |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 6665 | } |
| 6666 | |
| 6667 | static void |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 6668 | renderer_switch_binding(struct weston_keyboard *keyboard, |
| 6669 | const struct timespec *time, uint32_t key, void *data) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 6670 | { |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 6671 | struct drm_backend *b = |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 6672 | to_drm_backend(keyboard->seat->compositor); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 6673 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6674 | switch_to_gl_renderer(b); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 6675 | } |
| 6676 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 6677 | static const struct weston_drm_output_api api = { |
| 6678 | drm_output_set_mode, |
| 6679 | drm_output_set_gbm_format, |
| 6680 | drm_output_set_seat, |
| 6681 | }; |
| 6682 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6683 | static struct drm_backend * |
| 6684 | drm_backend_create(struct weston_compositor *compositor, |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6685 | struct weston_drm_backend_config *config) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 6686 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6687 | struct drm_backend *b; |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 6688 | struct udev_device *drm_device; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 6689 | struct wl_event_loop *loop; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6690 | const char *seat_id = default_seat; |
nerdopolis | b16c4ac | 2018-06-29 08:17:46 -0400 | [diff] [blame] | 6691 | const char *session_seat; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 6692 | int ret; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 6693 | |
nerdopolis | b16c4ac | 2018-06-29 08:17:46 -0400 | [diff] [blame] | 6694 | session_seat = getenv("XDG_SEAT"); |
| 6695 | if (session_seat) |
| 6696 | seat_id = session_seat; |
| 6697 | |
| 6698 | if (config->seat_id) |
| 6699 | seat_id = config->seat_id; |
| 6700 | |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 6701 | weston_log("initializing drm backend\n"); |
| 6702 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6703 | b = zalloc(sizeof *b); |
| 6704 | if (b == NULL) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 6705 | return NULL; |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 6706 | |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 6707 | b->state_invalid = true; |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 6708 | b->drm.fd = -1; |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 6709 | wl_array_init(&b->unused_crtcs); |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 6710 | |
Pekka Paalanen | 6858383 | 2015-05-19 09:53:16 +0300 | [diff] [blame] | 6711 | /* |
| 6712 | * KMS support for hardware planes cannot properly synchronize |
| 6713 | * without nuclear page flip. Without nuclear/atomic, hw plane |
| 6714 | * and cursor plane updates would either tear or cause extra |
| 6715 | * waits for vblanks which means dropping the compositor framerate |
Pekka Paalanen | 3f32a13 | 2015-09-07 15:38:43 +0300 | [diff] [blame] | 6716 | * to a fraction. For cursors, it's not so bad, so they are |
| 6717 | * enabled. |
Pekka Paalanen | 6858383 | 2015-05-19 09:53:16 +0300 | [diff] [blame] | 6718 | * |
| 6719 | * These can be enabled again when nuclear/atomic support lands. |
| 6720 | */ |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6721 | b->sprites_are_broken = 1; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6722 | b->compositor = compositor; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6723 | b->use_pixman = config->use_pixman; |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 6724 | b->pageflip_timeout = config->pageflip_timeout; |
Pekka Paalanen | dee412d | 2018-04-23 11:44:58 +0200 | [diff] [blame] | 6725 | b->use_pixman_shadow = config->use_pixman_shadow; |
Kristian Høgsberg | 8e6f376 | 2013-10-16 16:31:42 -0700 | [diff] [blame] | 6726 | |
Pekka Paalanen | 7da9a38 | 2017-08-30 11:29:49 +0300 | [diff] [blame] | 6727 | compositor->backend = &b->base; |
| 6728 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6729 | if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0) |
| 6730 | goto err_compositor; |
Kristian Høgsberg | 8e6f376 | 2013-10-16 16:31:42 -0700 | [diff] [blame] | 6731 | |
Rafal Mielniczuk | 32069c0 | 2013-03-27 18:39:28 +0100 | [diff] [blame] | 6732 | /* Check if we run drm-backend using weston-launch */ |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6733 | compositor->launcher = weston_launcher_connect(compositor, config->tty, |
| 6734 | seat_id, true); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6735 | if (compositor->launcher == NULL) { |
Pekka Paalanen | a453f4d | 2017-10-31 10:19:48 +0200 | [diff] [blame] | 6736 | weston_log("fatal: drm backend should be run using " |
| 6737 | "weston-launch binary, or your system should " |
| 6738 | "provide the logind D-Bus API.\n"); |
Rafal Mielniczuk | 32069c0 | 2013-03-27 18:39:28 +0100 | [diff] [blame] | 6739 | goto err_compositor; |
| 6740 | } |
| 6741 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6742 | b->udev = udev_new(); |
| 6743 | if (b->udev == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 6744 | weston_log("failed to initialize udev context\n"); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 6745 | goto err_launcher; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 6746 | } |
| 6747 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6748 | b->session_listener.notify = session_notify; |
| 6749 | wl_signal_add(&compositor->session_signal, &b->session_listener); |
Kristian Høgsberg | c5b9ddb | 2012-01-15 14:29:09 -0500 | [diff] [blame] | 6750 | |
Pekka Paalanen | b45ed8b | 2017-03-28 18:04:27 +0300 | [diff] [blame] | 6751 | if (config->specific_device) |
| 6752 | drm_device = open_specific_drm_device(b, config->specific_device); |
| 6753 | else |
| 6754 | drm_device = find_primary_gpu(b, seat_id); |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 6755 | if (drm_device == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 6756 | weston_log("no drm device found\n"); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 6757 | goto err_udev; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 6758 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 6759 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 6760 | if (init_kms_caps(b) < 0) { |
Ander Conselvan de Oliveira | 2292917 | 2013-01-25 15:13:02 +0200 | [diff] [blame] | 6761 | weston_log("failed to initialize kms\n"); |
| 6762 | goto err_udev_dev; |
| 6763 | } |
| 6764 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6765 | if (b->use_pixman) { |
| 6766 | if (init_pixman(b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 6767 | weston_log("failed to initialize pixman renderer\n"); |
| 6768 | goto err_udev_dev; |
| 6769 | } |
| 6770 | } else { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6771 | if (init_egl(b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 6772 | weston_log("failed to initialize egl\n"); |
| 6773 | goto err_udev_dev; |
| 6774 | } |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 6775 | } |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 6776 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6777 | b->base.destroy = drm_destroy; |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 6778 | b->base.repaint_begin = drm_repaint_begin; |
| 6779 | b->base.repaint_flush = drm_repaint_flush; |
| 6780 | b->base.repaint_cancel = drm_repaint_cancel; |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 6781 | b->base.create_output = drm_output_create; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 6782 | |
Bob Ham | 91880f1 | 2016-01-12 10:21:47 +0000 | [diff] [blame] | 6783 | weston_setup_vt_switch_bindings(compositor); |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 6784 | |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 6785 | wl_list_init(&b->plane_list); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6786 | create_sprites(b); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 6787 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6788 | if (udev_input_init(&b->input, |
Giulio Camuffo | 8aedf7b | 2016-06-02 21:48:12 +0300 | [diff] [blame] | 6789 | compositor, b->udev, seat_id, |
| 6790 | config->configure_device) < 0) { |
Ander Conselvan de Oliveira | 4ade0e4 | 2014-04-17 13:08:45 +0300 | [diff] [blame] | 6791 | weston_log("failed to create input devices\n"); |
| 6792 | goto err_sprite; |
| 6793 | } |
| 6794 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 6795 | if (drm_backend_create_heads(b, drm_device) < 0) { |
| 6796 | 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] | 6797 | goto err_udev_input; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 6798 | } |
| 6799 | |
Jason Ekstrand | 9fc7151 | 2014-04-02 19:53:46 -0500 | [diff] [blame] | 6800 | /* A this point we have some idea of whether or not we have a working |
| 6801 | * cursor plane. */ |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6802 | if (!b->cursors_are_broken) |
| 6803 | compositor->capabilities |= WESTON_CAP_CURSOR_PLANE; |
Jason Ekstrand | 9fc7151 | 2014-04-02 19:53:46 -0500 | [diff] [blame] | 6804 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6805 | loop = wl_display_get_event_loop(compositor->wl_display); |
| 6806 | b->drm_source = |
| 6807 | wl_event_loop_add_fd(loop, b->drm.fd, |
| 6808 | WL_EVENT_READABLE, on_drm_input, b); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 6809 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6810 | b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev"); |
| 6811 | if (b->udev_monitor == NULL) { |
Abdur Rehman | 4dca0e1 | 2017-01-01 19:46:35 +0500 | [diff] [blame] | 6812 | weston_log("failed to initialize udev monitor\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6813 | goto err_drm_source; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 6814 | } |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6815 | udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor, |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 6816 | "drm", NULL); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6817 | b->udev_drm_source = |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 6818 | wl_event_loop_add_fd(loop, |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6819 | udev_monitor_get_fd(b->udev_monitor), |
| 6820 | WL_EVENT_READABLE, udev_drm_event, b); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 6821 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6822 | if (udev_monitor_enable_receiving(b->udev_monitor) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 6823 | weston_log("failed to enable udev-monitor receiving\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6824 | goto err_udev_monitor; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 6825 | } |
| 6826 | |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6827 | udev_device_unref(drm_device); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6828 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6829 | weston_compositor_add_debug_binding(compositor, KEY_O, |
| 6830 | planes_binding, b); |
| 6831 | weston_compositor_add_debug_binding(compositor, KEY_C, |
| 6832 | planes_binding, b); |
| 6833 | weston_compositor_add_debug_binding(compositor, KEY_V, |
| 6834 | planes_binding, b); |
| 6835 | weston_compositor_add_debug_binding(compositor, KEY_Q, |
| 6836 | recorder_binding, b); |
| 6837 | weston_compositor_add_debug_binding(compositor, KEY_W, |
| 6838 | renderer_switch_binding, b); |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 6839 | |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 6840 | if (compositor->renderer->import_dmabuf) { |
| 6841 | if (linux_dmabuf_setup(compositor) < 0) |
| 6842 | weston_log("Error: initializing dmabuf " |
| 6843 | "support failed.\n"); |
| 6844 | } |
| 6845 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 6846 | ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME, |
| 6847 | &api, sizeof(api)); |
| 6848 | |
| 6849 | if (ret < 0) { |
| 6850 | weston_log("Failed to register output API.\n"); |
| 6851 | goto err_udev_monitor; |
| 6852 | } |
| 6853 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6854 | return b; |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6855 | |
| 6856 | err_udev_monitor: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6857 | wl_event_source_remove(b->udev_drm_source); |
| 6858 | udev_monitor_unref(b->udev_monitor); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6859 | err_drm_source: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6860 | wl_event_source_remove(b->drm_source); |
Ander Conselvan de Oliveira | 4ade0e4 | 2014-04-17 13:08:45 +0300 | [diff] [blame] | 6861 | err_udev_input: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6862 | udev_input_destroy(&b->input); |
Kristian Høgsberg | 2bc5e8e | 2012-09-06 20:51:00 -0400 | [diff] [blame] | 6863 | err_sprite: |
Emmanuel Gil Peyrot | b8347e3 | 2016-05-02 22:40:13 +0100 | [diff] [blame] | 6864 | if (b->gbm) |
| 6865 | gbm_device_destroy(b->gbm); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6866 | destroy_sprites(b); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6867 | err_udev_dev: |
| 6868 | udev_device_unref(drm_device); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 6869 | err_launcher: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6870 | weston_launcher_destroy(compositor->launcher); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6871 | err_udev: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6872 | udev_unref(b->udev); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6873 | err_compositor: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6874 | weston_compositor_shutdown(compositor); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6875 | free(b); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6876 | return NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 6877 | } |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 6878 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6879 | static void |
| 6880 | config_init_to_defaults(struct weston_drm_backend_config *config) |
| 6881 | { |
Pekka Paalanen | dee412d | 2018-04-23 11:44:58 +0200 | [diff] [blame] | 6882 | config->use_pixman_shadow = true; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6883 | } |
| 6884 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6885 | WL_EXPORT int |
Quentin Glidic | 23e1d6f | 2016-12-02 14:08:44 +0100 | [diff] [blame] | 6886 | weston_backend_init(struct weston_compositor *compositor, |
| 6887 | struct weston_backend_config *config_base) |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 6888 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6889 | struct drm_backend *b; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6890 | struct weston_drm_backend_config config = {{ 0, }}; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 6891 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6892 | if (config_base == NULL || |
| 6893 | config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION || |
| 6894 | config_base->struct_size > sizeof(struct weston_drm_backend_config)) { |
| 6895 | weston_log("drm backend config structure is invalid\n"); |
| 6896 | return -1; |
| 6897 | } |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 6898 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6899 | config_init_to_defaults(&config); |
| 6900 | memcpy(&config, config_base, config_base->struct_size); |
Kristian Høgsberg | d8e9833 | 2013-10-16 16:15:11 -0700 | [diff] [blame] | 6901 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6902 | b = drm_backend_create(compositor, &config); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6903 | if (b == NULL) |
| 6904 | return -1; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6905 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6906 | return 0; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 6907 | } |