Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1 | /* |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 2 | * Copyright © 2008-2011 Kristian Høgsberg |
| 3 | * Copyright © 2011 Intel Corporation |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 4 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 5 | * Permission is hereby granted, free of charge, to any person obtaining |
| 6 | * a copy of this software and associated documentation files (the |
| 7 | * "Software"), to deal in the Software without restriction, including |
| 8 | * without limitation the rights to use, copy, modify, merge, publish, |
| 9 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | * permit persons to whom the Software is furnished to do so, subject to |
| 11 | * the following conditions: |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 12 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 13 | * The above copyright notice and this permission notice (including the |
| 14 | * next paragraph) shall be included in all copies or substantial |
| 15 | * portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | * SOFTWARE. |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 25 | */ |
| 26 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 27 | #include "config.h" |
Kristian Høgsberg | 0b9334a | 2011-04-12 11:34:32 -0400 | [diff] [blame] | 28 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 29 | #include <errno.h> |
Jussi Kukkonen | 649bbce | 2016-07-19 14:16:27 +0300 | [diff] [blame] | 30 | #include <stdint.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 31 | #include <stdlib.h> |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 32 | #include <ctype.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 33 | #include <string.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <unistd.h> |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 36 | #include <linux/input.h> |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 37 | #include <linux/vt.h> |
Ander Conselvan de Oliveira | fd1f4c6 | 2012-06-26 17:09:14 +0300 | [diff] [blame] | 38 | #include <assert.h> |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 39 | #include <sys/mman.h> |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 40 | #include <dlfcn.h> |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 41 | #include <time.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 42 | |
Benjamin Franzke | c649a92 | 2011-03-02 11:56:04 +0100 | [diff] [blame] | 43 | #include <xf86drm.h> |
| 44 | #include <xf86drmMode.h> |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 45 | #include <drm_fourcc.h> |
Benjamin Franzke | c649a92 | 2011-03-02 11:56:04 +0100 | [diff] [blame] | 46 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 47 | #include <gbm.h> |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 48 | #include <libudev.h> |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 49 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 50 | #include "compositor.h" |
| 51 | #include "compositor-drm.h" |
Jon Cruz | 35b2eaa | 2015-06-15 15:37:08 -0700 | [diff] [blame] | 52 | #include "shared/helpers.h" |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 53 | #include "shared/timespec-util.h" |
John Kåre Alsaker | 30d2b1f | 2012-11-13 19:10:28 +0100 | [diff] [blame] | 54 | #include "gl-renderer.h" |
Vincent Abriou | c950667 | 2016-10-05 16:14:07 +0200 | [diff] [blame] | 55 | #include "weston-egl-ext.h" |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 56 | #include "pixman-renderer.h" |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 57 | #include "pixel-formats.h" |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 58 | #include "libbacklight.h" |
Peter Hutterer | 823ad33 | 2014-11-26 07:06:31 +1000 | [diff] [blame] | 59 | #include "libinput-seat.h" |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 60 | #include "launcher-util.h" |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 61 | #include "vaapi-recorder.h" |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 62 | #include "presentation-time-server-protocol.h" |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 63 | #include "linux-dmabuf.h" |
Micah Fedke | c889012 | 2017-02-01 15:28:23 -0500 | [diff] [blame] | 64 | #include "linux-dmabuf-unstable-v1-server-protocol.h" |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 65 | |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 66 | #ifndef DRM_CAP_TIMESTAMP_MONOTONIC |
| 67 | #define DRM_CAP_TIMESTAMP_MONOTONIC 0x6 |
| 68 | #endif |
| 69 | |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 70 | #ifndef DRM_CLIENT_CAP_UNIVERSAL_PLANES |
| 71 | #define DRM_CLIENT_CAP_UNIVERSAL_PLANES 2 |
| 72 | #endif |
| 73 | |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 74 | #ifndef DRM_CAP_CURSOR_WIDTH |
| 75 | #define DRM_CAP_CURSOR_WIDTH 0x8 |
| 76 | #endif |
| 77 | |
| 78 | #ifndef DRM_CAP_CURSOR_HEIGHT |
| 79 | #define DRM_CAP_CURSOR_HEIGHT 0x9 |
| 80 | #endif |
| 81 | |
| 82 | #ifndef GBM_BO_USE_CURSOR |
| 83 | #define GBM_BO_USE_CURSOR GBM_BO_USE_CURSOR_64X64 |
| 84 | #endif |
| 85 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 86 | #define MAX_CLONED_CONNECTORS 1 |
| 87 | |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 88 | /** |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 89 | * Represents the values of an enum-type KMS property |
| 90 | */ |
| 91 | struct drm_property_enum_info { |
| 92 | const char *name; /**< name as string (static, not freed) */ |
| 93 | bool valid; /**< true if value is supported; ignore if false */ |
| 94 | uint64_t value; /**< raw value */ |
| 95 | }; |
| 96 | |
| 97 | /** |
| 98 | * Holds information on a DRM property, including its ID and the enum |
| 99 | * values it holds. |
| 100 | * |
| 101 | * DRM properties are allocated dynamically, and maintained as DRM objects |
| 102 | * within the normal object ID space; they thus do not have a stable ID |
| 103 | * to refer to. This includes enum values, which must be referred to by |
| 104 | * integer values, but these are not stable. |
| 105 | * |
| 106 | * drm_property_info allows a cache to be maintained where Weston can use |
| 107 | * enum values internally to refer to properties, with the mapping to DRM |
| 108 | * ID values being maintained internally. |
| 109 | */ |
| 110 | struct drm_property_info { |
| 111 | const char *name; /**< name as string (static, not freed) */ |
| 112 | uint32_t prop_id; /**< KMS property object ID */ |
| 113 | unsigned int num_enum_values; /**< number of enum values */ |
| 114 | struct drm_property_enum_info *enum_values; /**< array of enum values */ |
| 115 | }; |
| 116 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 117 | /** |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 118 | * List of properties attached to DRM planes |
| 119 | */ |
| 120 | enum wdrm_plane_property { |
| 121 | WDRM_PLANE_TYPE = 0, |
| 122 | WDRM_PLANE_SRC_X, |
| 123 | WDRM_PLANE_SRC_Y, |
| 124 | WDRM_PLANE_SRC_W, |
| 125 | WDRM_PLANE_SRC_H, |
| 126 | WDRM_PLANE_CRTC_X, |
| 127 | WDRM_PLANE_CRTC_Y, |
| 128 | WDRM_PLANE_CRTC_W, |
| 129 | WDRM_PLANE_CRTC_H, |
| 130 | WDRM_PLANE_FB_ID, |
| 131 | WDRM_PLANE_CRTC_ID, |
| 132 | WDRM_PLANE__COUNT |
| 133 | }; |
| 134 | |
| 135 | /** |
| 136 | * Possible values for the WDRM_PLANE_TYPE property. |
| 137 | */ |
| 138 | enum wdrm_plane_type { |
| 139 | WDRM_PLANE_TYPE_PRIMARY = 0, |
| 140 | WDRM_PLANE_TYPE_CURSOR, |
| 141 | WDRM_PLANE_TYPE_OVERLAY, |
| 142 | WDRM_PLANE_TYPE__COUNT |
| 143 | }; |
| 144 | |
| 145 | static struct drm_property_enum_info plane_type_enums[] = { |
| 146 | [WDRM_PLANE_TYPE_PRIMARY] = { |
| 147 | .name = "Primary", |
| 148 | }, |
| 149 | [WDRM_PLANE_TYPE_OVERLAY] = { |
| 150 | .name = "Overlay", |
| 151 | }, |
| 152 | [WDRM_PLANE_TYPE_CURSOR] = { |
| 153 | .name = "Cursor", |
| 154 | }, |
| 155 | }; |
| 156 | |
| 157 | static const struct drm_property_info plane_props[] = { |
| 158 | [WDRM_PLANE_TYPE] = { |
| 159 | .name = "type", |
| 160 | .enum_values = plane_type_enums, |
| 161 | .num_enum_values = WDRM_PLANE_TYPE__COUNT, |
| 162 | }, |
| 163 | [WDRM_PLANE_SRC_X] = { .name = "SRC_X", }, |
| 164 | [WDRM_PLANE_SRC_Y] = { .name = "SRC_Y", }, |
| 165 | [WDRM_PLANE_SRC_W] = { .name = "SRC_W", }, |
| 166 | [WDRM_PLANE_SRC_H] = { .name = "SRC_H", }, |
| 167 | [WDRM_PLANE_CRTC_X] = { .name = "CRTC_X", }, |
| 168 | [WDRM_PLANE_CRTC_Y] = { .name = "CRTC_Y", }, |
| 169 | [WDRM_PLANE_CRTC_W] = { .name = "CRTC_W", }, |
| 170 | [WDRM_PLANE_CRTC_H] = { .name = "CRTC_H", }, |
| 171 | [WDRM_PLANE_FB_ID] = { .name = "FB_ID", }, |
| 172 | [WDRM_PLANE_CRTC_ID] = { .name = "CRTC_ID", }, |
| 173 | }; |
| 174 | |
| 175 | /** |
| 176 | * List of properties attached to a DRM connector |
| 177 | */ |
| 178 | enum wdrm_connector_property { |
| 179 | WDRM_CONNECTOR_EDID = 0, |
| 180 | WDRM_CONNECTOR_DPMS, |
| 181 | WDRM_CONNECTOR_CRTC_ID, |
| 182 | WDRM_CONNECTOR__COUNT |
| 183 | }; |
| 184 | |
| 185 | static const struct drm_property_info connector_props[] = { |
| 186 | [WDRM_CONNECTOR_EDID] = { .name = "EDID" }, |
| 187 | [WDRM_CONNECTOR_DPMS] = { .name = "DPMS" }, |
| 188 | [WDRM_CONNECTOR_CRTC_ID] = { .name = "CRTC_ID", }, |
| 189 | }; |
| 190 | |
| 191 | /** |
Pekka Paalanen | cd011a6 | 2016-11-15 22:07:49 +0000 | [diff] [blame] | 192 | * List of properties attached to DRM CRTCs |
| 193 | */ |
| 194 | enum wdrm_crtc_property { |
| 195 | WDRM_CRTC_MODE_ID = 0, |
| 196 | WDRM_CRTC_ACTIVE, |
| 197 | WDRM_CRTC__COUNT |
| 198 | }; |
| 199 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 200 | static const struct drm_property_info crtc_props[] = { |
| 201 | [WDRM_CRTC_MODE_ID] = { .name = "MODE_ID", }, |
| 202 | [WDRM_CRTC_ACTIVE] = { .name = "ACTIVE", }, |
| 203 | }; |
| 204 | |
Pekka Paalanen | cd011a6 | 2016-11-15 22:07:49 +0000 | [diff] [blame] | 205 | /** |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 206 | * Mode for drm_output_state_duplicate. |
| 207 | */ |
| 208 | enum drm_output_state_duplicate_mode { |
| 209 | DRM_OUTPUT_STATE_CLEAR_PLANES, /**< reset all planes to off */ |
| 210 | DRM_OUTPUT_STATE_PRESERVE_PLANES, /**< preserve plane state */ |
| 211 | }; |
| 212 | |
| 213 | /** |
| 214 | * Mode for drm_pending_state_apply and co. |
| 215 | */ |
| 216 | enum drm_state_apply_mode { |
| 217 | DRM_STATE_APPLY_SYNC, /**< state fully processed */ |
| 218 | DRM_STATE_APPLY_ASYNC, /**< state pending event delivery */ |
| 219 | }; |
| 220 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 221 | struct drm_backend { |
| 222 | struct weston_backend base; |
| 223 | struct weston_compositor *compositor; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 224 | |
| 225 | struct udev *udev; |
| 226 | struct wl_event_source *drm_source; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 227 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 228 | struct udev_monitor *udev_monitor; |
| 229 | struct wl_event_source *udev_drm_source; |
| 230 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 231 | struct { |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 232 | int id; |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 233 | int fd; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 234 | char *filename; |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 235 | } drm; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 236 | struct gbm_device *gbm; |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 237 | struct wl_listener session_listener; |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 238 | uint32_t gbm_format; |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 239 | |
Rob Clark | 4339add | 2012-08-09 14:18:28 -0500 | [diff] [blame] | 240 | /* we need these parameters in order to not fail drmModeAddFB2() |
| 241 | * due to out of bounds dimensions, and then mistakenly set |
| 242 | * sprites_are_broken: |
| 243 | */ |
Daniel Stone | f214fdc | 2016-11-14 17:43:57 +0000 | [diff] [blame] | 244 | int min_width, max_width; |
| 245 | int min_height, max_height; |
Rob Clark | 4339add | 2012-08-09 14:18:28 -0500 | [diff] [blame] | 246 | |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 247 | struct wl_list plane_list; |
Kristian Høgsberg | 65bec24 | 2012-03-05 19:57:35 -0500 | [diff] [blame] | 248 | int sprites_are_broken; |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 249 | int sprites_hidden; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 250 | |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 251 | void *repaint_data; |
| 252 | |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 253 | bool state_invalid; |
| 254 | |
Pekka Paalanen | eacec81 | 2017-09-12 13:43:51 +0300 | [diff] [blame] | 255 | /* CRTC IDs not used by any enabled output. */ |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 256 | struct wl_array unused_crtcs; |
| 257 | |
Rob Clark | ab5b1e3 | 2012-08-09 13:24:45 -0500 | [diff] [blame] | 258 | int cursors_are_broken; |
| 259 | |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 260 | bool universal_planes; |
Pekka Paalanen | cd011a6 | 2016-11-15 22:07:49 +0000 | [diff] [blame] | 261 | bool atomic_modeset; |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 262 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 263 | int use_pixman; |
| 264 | |
Rob Bradford | d355b80 | 2013-05-31 18:09:55 +0100 | [diff] [blame] | 265 | struct udev_input input; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 266 | |
Daniel Stone | 70d337d | 2015-06-16 18:42:23 +0100 | [diff] [blame] | 267 | int32_t cursor_width; |
| 268 | int32_t cursor_height; |
Ucan, Emre (ADITG/SW1) | 21e4944 | 2017-02-02 14:06:55 +0000 | [diff] [blame] | 269 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 270 | uint32_t pageflip_timeout; |
Daniel Stone | b57c6a0 | 2017-10-05 16:27:21 +0100 | [diff] [blame] | 271 | |
| 272 | bool shutting_down; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 273 | }; |
| 274 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 275 | struct drm_mode { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 276 | struct weston_mode base; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 277 | drmModeModeInfo mode_info; |
Daniel Stone | d5526cb | 2016-11-16 10:54:10 +0000 | [diff] [blame] | 278 | uint32_t blob_id; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 279 | }; |
| 280 | |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 281 | enum drm_fb_type { |
| 282 | BUFFER_INVALID = 0, /**< never used */ |
| 283 | BUFFER_CLIENT, /**< directly sourced from client */ |
| 284 | BUFFER_PIXMAN_DUMB, /**< internal Pixman rendering */ |
| 285 | BUFFER_GBM_SURFACE, /**< internal EGL rendering */ |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 286 | BUFFER_CURSOR, /**< internal cursor buffer */ |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 287 | }; |
| 288 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 289 | struct drm_fb { |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 290 | enum drm_fb_type type; |
| 291 | |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 292 | int refcnt; |
| 293 | |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 294 | uint32_t fb_id, stride, handle, size; |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 295 | const struct pixel_format_info *format; |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 296 | int width, height; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 297 | int fd; |
Pekka Paalanen | de685b8 | 2012-12-04 15:58:12 +0200 | [diff] [blame] | 298 | struct weston_buffer_reference buffer_ref; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 299 | |
| 300 | /* Used by gbm fbs */ |
| 301 | struct gbm_bo *bo; |
Daniel Stone | 05a5ac2 | 2017-04-04 17:54:25 +0100 | [diff] [blame] | 302 | struct gbm_surface *gbm_surface; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 303 | |
| 304 | /* Used by dumb fbs */ |
| 305 | void *map; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 306 | }; |
| 307 | |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 308 | struct drm_edid { |
| 309 | char eisa_id[13]; |
| 310 | char monitor_name[13]; |
| 311 | char pnp_id[5]; |
| 312 | char serial_number[13]; |
| 313 | }; |
| 314 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 315 | /** |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 316 | * Pending state holds one or more drm_output_state structures, collected from |
| 317 | * performing repaint. This pending state is transient, and only lives between |
| 318 | * beginning a repaint group and flushing the results: after flush, each |
| 319 | * output state will complete and be retired separately. |
| 320 | */ |
| 321 | struct drm_pending_state { |
| 322 | struct drm_backend *backend; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 323 | struct wl_list output_list; |
| 324 | }; |
| 325 | |
| 326 | /* |
| 327 | * Output state holds the dynamic state for one Weston output, i.e. a KMS CRTC, |
| 328 | * plus >= 1 each of encoder/connector/plane. Since everything but the planes |
| 329 | * is currently statically assigned per-output, we mainly use this to track |
| 330 | * plane state. |
| 331 | * |
| 332 | * pending_state is set when the output state is owned by a pending_state, |
| 333 | * i.e. when it is being constructed and has not yet been applied. When the |
| 334 | * output state has been applied, the owning pending_state is freed. |
| 335 | */ |
| 336 | struct drm_output_state { |
| 337 | struct drm_pending_state *pending_state; |
| 338 | struct drm_output *output; |
| 339 | struct wl_list link; |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 340 | enum dpms_enum dpms; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 341 | struct wl_list plane_list; |
| 342 | }; |
| 343 | |
| 344 | /** |
| 345 | * Plane state holds the dynamic state for a plane: where it is positioned, |
| 346 | * and which buffer it is currently displaying. |
| 347 | * |
| 348 | * The plane state is owned by an output state, except when setting an initial |
| 349 | * state. See drm_output_state for notes on state object lifetime. |
| 350 | */ |
| 351 | struct drm_plane_state { |
| 352 | struct drm_plane *plane; |
| 353 | struct drm_output *output; |
| 354 | struct drm_output_state *output_state; |
| 355 | |
| 356 | struct drm_fb *fb; |
| 357 | |
| 358 | int32_t src_x, src_y; |
| 359 | uint32_t src_w, src_h; |
| 360 | int32_t dest_x, dest_y; |
| 361 | uint32_t dest_w, dest_h; |
| 362 | |
| 363 | bool complete; |
| 364 | |
| 365 | struct wl_list link; /* drm_output_state::plane_list */ |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 366 | }; |
| 367 | |
| 368 | /** |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 369 | * A plane represents one buffer, positioned within a CRTC, and stacked |
| 370 | * relative to other planes on the same CRTC. |
| 371 | * |
| 372 | * Each CRTC has a 'primary plane', which use used to display the classic |
| 373 | * framebuffer contents, as accessed through the legacy drmModeSetCrtc |
| 374 | * call (which combines setting the CRTC's actual physical mode, and the |
| 375 | * properties of the primary plane). |
| 376 | * |
| 377 | * The cursor plane also has its own alternate legacy API. |
| 378 | * |
| 379 | * Other planes are used opportunistically to display content we do not |
| 380 | * wish to blit into the primary plane. These non-primary/cursor planes |
| 381 | * are referred to as 'sprites'. |
| 382 | */ |
| 383 | struct drm_plane { |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 384 | struct weston_plane base; |
| 385 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 386 | struct drm_backend *backend; |
| 387 | |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 388 | enum wdrm_plane_type type; |
| 389 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 390 | uint32_t possible_crtcs; |
| 391 | uint32_t plane_id; |
| 392 | uint32_t count_formats; |
| 393 | |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 394 | struct drm_property_info props[WDRM_PLANE__COUNT]; |
| 395 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 396 | /* The last state submitted to the kernel for this plane. */ |
| 397 | struct drm_plane_state *state_cur; |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 398 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 399 | struct wl_list link; |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 400 | |
| 401 | uint32_t formats[]; |
| 402 | }; |
| 403 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 404 | struct drm_head { |
| 405 | struct weston_head base; |
| 406 | struct drm_backend *backend; |
| 407 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 408 | drmModeConnector *connector; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 409 | uint32_t connector_id; |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 410 | struct drm_edid edid; |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 411 | |
| 412 | /* Holds the properties for the connector */ |
| 413 | struct drm_property_info props_conn[WDRM_CONNECTOR__COUNT]; |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 414 | |
| 415 | struct backlight *backlight; |
Pekka Paalanen | 13d233e | 2017-09-11 14:06:11 +0300 | [diff] [blame] | 416 | |
| 417 | drmModeModeInfo inherited_mode; /**< Original mode on the connector */ |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 418 | }; |
| 419 | |
| 420 | struct drm_output { |
| 421 | struct weston_output base; |
| 422 | |
| 423 | uint32_t crtc_id; /* object ID to pass to DRM functions */ |
| 424 | int pipe; /* index of CRTC in resource array / bitmasks */ |
| 425 | |
Pekka Paalanen | cd011a6 | 2016-11-15 22:07:49 +0000 | [diff] [blame] | 426 | /* Holds the properties for the CRTC */ |
| 427 | struct drm_property_info props_crtc[WDRM_CRTC__COUNT]; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 428 | |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 429 | int vblank_pending; |
| 430 | int page_flip_pending; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 431 | int atomic_complete_pending; |
Xiong Zhang | abd5d47 | 2013-10-11 14:43:07 +0800 | [diff] [blame] | 432 | int destroy_pending; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 433 | int disable_pending; |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 434 | int dpms_off_pending; |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 435 | |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 436 | struct drm_fb *gbm_cursor_fb[2]; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 437 | struct drm_plane *cursor_plane; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 438 | struct weston_view *cursor_view; |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 439 | int current_cursor; |
Daniel Stone | 5bb8f58 | 2017-04-04 17:54:28 +0100 | [diff] [blame] | 440 | |
| 441 | struct gbm_surface *gbm_surface; |
| 442 | uint32_t gbm_format; |
| 443 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 444 | /* Plane being displayed directly on the CRTC */ |
| 445 | struct drm_plane *scanout_plane; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 446 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 447 | /* The last state submitted to the kernel for this CRTC. */ |
| 448 | struct drm_output_state *state_cur; |
| 449 | /* The previously-submitted state, where the hardware has not |
| 450 | * yet acknowledged completion of state_cur. */ |
| 451 | struct drm_output_state *state_last; |
| 452 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 453 | struct drm_fb *dumb[2]; |
| 454 | pixman_image_t *image[2]; |
| 455 | int current_image; |
| 456 | pixman_region32_t previous_damage; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 457 | |
| 458 | struct vaapi_recorder *recorder; |
| 459 | struct wl_listener recorder_frame_listener; |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 460 | |
| 461 | struct wl_event_source *pageflip_timer; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 462 | }; |
| 463 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 464 | static struct gl_renderer_interface *gl_renderer; |
| 465 | |
Kristian Høgsberg | 98cfea6 | 2013-02-18 16:15:53 -0500 | [diff] [blame] | 466 | static const char default_seat[] = "seat0"; |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 467 | |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 468 | static void |
| 469 | wl_array_remove_uint32(struct wl_array *array, uint32_t elm) |
| 470 | { |
| 471 | uint32_t *pos, *end; |
| 472 | |
| 473 | end = (uint32_t *) ((char *) array->data + array->size); |
| 474 | |
| 475 | wl_array_for_each(pos, array) { |
| 476 | if (*pos != elm) |
| 477 | continue; |
| 478 | |
| 479 | array->size -= sizeof(*pos); |
| 480 | if (pos + 1 == end) |
| 481 | break; |
| 482 | |
| 483 | memmove(pos, pos + 1, (char *) end - (char *) (pos + 1)); |
| 484 | break; |
| 485 | } |
| 486 | } |
| 487 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 488 | static inline struct drm_head * |
| 489 | to_drm_head(struct weston_head *base) |
| 490 | { |
| 491 | return container_of(base, struct drm_head, base); |
| 492 | } |
| 493 | |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 494 | static inline struct drm_output * |
| 495 | to_drm_output(struct weston_output *base) |
| 496 | { |
| 497 | return container_of(base, struct drm_output, base); |
| 498 | } |
| 499 | |
| 500 | static inline struct drm_backend * |
| 501 | to_drm_backend(struct weston_compositor *base) |
| 502 | { |
| 503 | return container_of(base->backend, struct drm_backend, base); |
| 504 | } |
| 505 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 506 | static int |
| 507 | pageflip_timeout(void *data) { |
| 508 | /* |
| 509 | * Our timer just went off, that means we're not receiving drm |
| 510 | * page flip events anymore for that output. Let's gracefully exit |
| 511 | * weston with a return value so devs can debug what's going on. |
| 512 | */ |
| 513 | struct drm_output *output = data; |
| 514 | struct weston_compositor *compositor = output->base.compositor; |
| 515 | |
| 516 | weston_log("Pageflip timeout reached on output %s, your " |
| 517 | "driver is probably buggy! Exiting.\n", |
| 518 | output->base.name); |
| 519 | weston_compositor_exit_with_code(compositor, EXIT_FAILURE); |
| 520 | |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | /* Creates the pageflip timer. Note that it isn't armed by default */ |
| 525 | static int |
| 526 | drm_output_pageflip_timer_create(struct drm_output *output) |
| 527 | { |
| 528 | struct wl_event_loop *loop = NULL; |
| 529 | struct weston_compositor *ec = output->base.compositor; |
| 530 | |
| 531 | loop = wl_display_get_event_loop(ec->wl_display); |
| 532 | assert(loop); |
| 533 | output->pageflip_timer = wl_event_loop_add_timer(loop, |
| 534 | pageflip_timeout, |
| 535 | output); |
| 536 | |
| 537 | if (output->pageflip_timer == NULL) { |
| 538 | weston_log("creating drm pageflip timer failed: %m\n"); |
| 539 | return -1; |
| 540 | } |
| 541 | |
| 542 | return 0; |
| 543 | } |
| 544 | |
Daniel Stone | cb04cc4 | 2016-11-16 11:51:27 +0000 | [diff] [blame] | 545 | static inline struct drm_mode * |
| 546 | to_drm_mode(struct weston_mode *base) |
| 547 | { |
| 548 | return container_of(base, struct drm_mode, base); |
| 549 | } |
| 550 | |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 551 | /** |
| 552 | * Get the current value of a KMS property |
| 553 | * |
| 554 | * Given a drmModeObjectGetProperties return, as well as the drm_property_info |
| 555 | * for the target property, return the current value of that property, |
| 556 | * with an optional default. If the property is a KMS enum type, the return |
| 557 | * value will be translated into the appropriate internal enum. |
| 558 | * |
| 559 | * If the property is not present, the default value will be returned. |
| 560 | * |
| 561 | * @param info Internal structure for property to look up |
| 562 | * @param props Raw KMS properties for the target object |
| 563 | * @param def Value to return if property is not found |
| 564 | */ |
| 565 | static uint64_t |
| 566 | drm_property_get_value(struct drm_property_info *info, |
| 567 | drmModeObjectPropertiesPtr props, |
| 568 | uint64_t def) |
| 569 | { |
| 570 | unsigned int i; |
| 571 | |
| 572 | if (info->prop_id == 0) |
| 573 | return def; |
| 574 | |
| 575 | for (i = 0; i < props->count_props; i++) { |
| 576 | unsigned int j; |
| 577 | |
| 578 | if (props->props[i] != info->prop_id) |
| 579 | continue; |
| 580 | |
| 581 | /* Simple (non-enum) types can return the value directly */ |
| 582 | if (info->num_enum_values == 0) |
| 583 | return props->prop_values[i]; |
| 584 | |
| 585 | /* Map from raw value to enum value */ |
| 586 | for (j = 0; j < info->num_enum_values; j++) { |
| 587 | if (!info->enum_values[j].valid) |
| 588 | continue; |
| 589 | if (info->enum_values[j].value != props->prop_values[i]) |
| 590 | continue; |
| 591 | |
| 592 | return j; |
| 593 | } |
| 594 | |
| 595 | /* We don't have a mapping for this enum; return default. */ |
| 596 | break; |
| 597 | } |
| 598 | |
| 599 | return def; |
| 600 | } |
| 601 | |
| 602 | /** |
| 603 | * Cache DRM property values |
| 604 | * |
| 605 | * Update a per-object array of drm_property_info structures, given the |
| 606 | * DRM properties of the object. |
| 607 | * |
| 608 | * Call this every time an object newly appears (note that only connectors |
| 609 | * can be hotplugged), the first time it is seen, or when its status changes |
| 610 | * in a way which invalidates the potential property values (currently, the |
| 611 | * only case for this is connector hotplug). |
| 612 | * |
| 613 | * This updates the property IDs and enum values within the drm_property_info |
| 614 | * array. |
| 615 | * |
| 616 | * DRM property enum values are dynamic at runtime; the user must query the |
| 617 | * property to find out the desired runtime value for a requested string |
| 618 | * name. Using the 'type' field on planes as an example, there is no single |
| 619 | * hardcoded constant for primary plane types; instead, the property must be |
| 620 | * queried at runtime to find the value associated with the string "Primary". |
| 621 | * |
| 622 | * This helper queries and caches the enum values, to allow us to use a set |
| 623 | * of compile-time-constant enums portably across various implementations. |
| 624 | * The values given in enum_names are searched for, and stored in the |
| 625 | * same-indexed field of the map array. |
| 626 | * |
| 627 | * @param b DRM backend object |
| 628 | * @param src DRM property info array to source from |
| 629 | * @param info DRM property info array to copy into |
| 630 | * @param num_infos Number of entries in the source array |
| 631 | * @param props DRM object properties for the object |
| 632 | */ |
| 633 | static void |
| 634 | drm_property_info_populate(struct drm_backend *b, |
| 635 | const struct drm_property_info *src, |
| 636 | struct drm_property_info *info, |
| 637 | unsigned int num_infos, |
| 638 | drmModeObjectProperties *props) |
| 639 | { |
| 640 | drmModePropertyRes *prop; |
| 641 | unsigned i, j; |
| 642 | |
| 643 | for (i = 0; i < num_infos; i++) { |
| 644 | unsigned int j; |
| 645 | |
| 646 | info[i].name = src[i].name; |
| 647 | info[i].prop_id = 0; |
| 648 | info[i].num_enum_values = src[i].num_enum_values; |
| 649 | |
| 650 | if (src[i].num_enum_values == 0) |
| 651 | continue; |
| 652 | |
| 653 | info[i].enum_values = |
| 654 | malloc(src[i].num_enum_values * |
| 655 | sizeof(*info[i].enum_values)); |
| 656 | assert(info[i].enum_values); |
| 657 | for (j = 0; j < info[i].num_enum_values; j++) { |
| 658 | info[i].enum_values[j].name = src[i].enum_values[j].name; |
| 659 | info[i].enum_values[j].valid = false; |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | for (i = 0; i < props->count_props; i++) { |
| 664 | unsigned int k; |
| 665 | |
| 666 | prop = drmModeGetProperty(b->drm.fd, props->props[i]); |
| 667 | if (!prop) |
| 668 | continue; |
| 669 | |
| 670 | for (j = 0; j < num_infos; j++) { |
| 671 | if (!strcmp(prop->name, info[j].name)) |
| 672 | break; |
| 673 | } |
| 674 | |
| 675 | /* We don't know/care about this property. */ |
| 676 | if (j == num_infos) { |
| 677 | #ifdef DEBUG |
| 678 | weston_log("DRM debug: unrecognized property %u '%s'\n", |
| 679 | prop->prop_id, prop->name); |
| 680 | #endif |
| 681 | drmModeFreeProperty(prop); |
| 682 | continue; |
| 683 | } |
| 684 | |
| 685 | if (info[j].num_enum_values == 0 && |
| 686 | (prop->flags & DRM_MODE_PROP_ENUM)) { |
| 687 | weston_log("DRM: expected property %s to not be an" |
| 688 | " enum, but it is; ignoring\n", prop->name); |
| 689 | drmModeFreeProperty(prop); |
| 690 | continue; |
| 691 | } |
| 692 | |
| 693 | info[j].prop_id = props->props[i]; |
| 694 | |
| 695 | if (info[j].num_enum_values == 0) { |
| 696 | drmModeFreeProperty(prop); |
| 697 | continue; |
| 698 | } |
| 699 | |
| 700 | if (!(prop->flags & DRM_MODE_PROP_ENUM)) { |
| 701 | weston_log("DRM: expected property %s to be an enum," |
| 702 | " but it is not; ignoring\n", prop->name); |
| 703 | drmModeFreeProperty(prop); |
| 704 | info[j].prop_id = 0; |
| 705 | continue; |
| 706 | } |
| 707 | |
| 708 | for (k = 0; k < info[j].num_enum_values; k++) { |
| 709 | int l; |
| 710 | |
| 711 | for (l = 0; l < prop->count_enums; l++) { |
| 712 | if (!strcmp(prop->enums[l].name, |
| 713 | info[j].enum_values[k].name)) |
| 714 | break; |
| 715 | } |
| 716 | |
| 717 | if (l == prop->count_enums) |
| 718 | continue; |
| 719 | |
| 720 | info[j].enum_values[k].valid = true; |
| 721 | info[j].enum_values[k].value = prop->enums[l].value; |
| 722 | } |
| 723 | |
| 724 | drmModeFreeProperty(prop); |
| 725 | } |
| 726 | |
| 727 | #ifdef DEBUG |
| 728 | for (i = 0; i < num_infos; i++) { |
| 729 | if (info[i].prop_id == 0) |
| 730 | weston_log("DRM warning: property '%s' missing\n", |
| 731 | info[i].name); |
| 732 | } |
| 733 | #endif |
| 734 | } |
| 735 | |
| 736 | /** |
| 737 | * Free DRM property information |
| 738 | * |
Pekka Paalanen | 46e4f97 | 2017-09-07 15:32:01 +0300 | [diff] [blame] | 739 | * Frees all memory associated with a DRM property info array and zeroes |
| 740 | * it out, leaving it usable for a further drm_property_info_update() or |
| 741 | * drm_property_info_free(). |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 742 | * |
| 743 | * @param info DRM property info array |
| 744 | * @param num_props Number of entries in array to free |
| 745 | */ |
| 746 | static void |
| 747 | drm_property_info_free(struct drm_property_info *info, int num_props) |
| 748 | { |
| 749 | int i; |
| 750 | |
| 751 | for (i = 0; i < num_props; i++) |
| 752 | free(info[i].enum_values); |
Pekka Paalanen | 46e4f97 | 2017-09-07 15:32:01 +0300 | [diff] [blame] | 753 | |
| 754 | memset(info, 0, sizeof(*info) * num_props); |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 757 | static void |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 758 | drm_output_set_cursor(struct drm_output_state *output_state); |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 759 | |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 760 | static void |
| 761 | drm_output_update_msc(struct drm_output *output, unsigned int seq); |
| 762 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 763 | static void |
| 764 | drm_output_destroy(struct weston_output *output_base); |
| 765 | |
Daniel Stone | 5ff289a | 2017-10-07 12:59:02 +0100 | [diff] [blame] | 766 | /** |
| 767 | * Returns true if the plane can be used on the given output for its current |
| 768 | * repaint cycle. |
| 769 | */ |
| 770 | static bool |
| 771 | drm_plane_is_available(struct drm_plane *plane, struct drm_output *output) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 772 | { |
Daniel Stone | 5ff289a | 2017-10-07 12:59:02 +0100 | [diff] [blame] | 773 | assert(plane->state_cur); |
| 774 | |
| 775 | /* The plane still has a request not yet completed by the kernel. */ |
| 776 | if (!plane->state_cur->complete) |
| 777 | return false; |
| 778 | |
| 779 | /* The plane is still active on another output. */ |
| 780 | if (plane->state_cur->output && plane->state_cur->output != output) |
| 781 | return false; |
| 782 | |
| 783 | /* Check whether the plane can be used with this CRTC; possible_crtcs |
| 784 | * is a bitmask of CRTC indices (pipe), rather than CRTC object ID. */ |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 785 | return !!(plane->possible_crtcs & (1 << output->pipe)); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 786 | } |
| 787 | |
Daniel Stone | 72c0e1b | 2017-02-09 13:49:15 +0000 | [diff] [blame] | 788 | static struct drm_output * |
| 789 | drm_output_find_by_crtc(struct drm_backend *b, uint32_t crtc_id) |
| 790 | { |
| 791 | struct drm_output *output; |
| 792 | |
| 793 | wl_list_for_each(output, &b->compositor->output_list, base.link) { |
| 794 | if (output->crtc_id == crtc_id) |
| 795 | return output; |
| 796 | } |
| 797 | |
Daniel Stone | 72c0e1b | 2017-02-09 13:49:15 +0000 | [diff] [blame] | 798 | return NULL; |
| 799 | } |
| 800 | |
Pekka Paalanen | 54cc47c | 2017-08-31 11:58:41 +0300 | [diff] [blame] | 801 | static struct drm_head * |
| 802 | drm_head_find_by_connector(struct drm_backend *backend, uint32_t connector_id) |
| 803 | { |
| 804 | struct weston_head *base; |
| 805 | struct drm_head *head; |
| 806 | |
| 807 | wl_list_for_each(base, |
| 808 | &backend->compositor->head_list, compositor_link) { |
| 809 | head = to_drm_head(base); |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 810 | if (head->connector_id == connector_id) |
Pekka Paalanen | 54cc47c | 2017-08-31 11:58:41 +0300 | [diff] [blame] | 811 | return head; |
| 812 | } |
| 813 | |
| 814 | return NULL; |
| 815 | } |
| 816 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 817 | static void |
Tomohito Esaki | 576f42e | 2017-04-04 17:54:24 +0100 | [diff] [blame] | 818 | drm_fb_destroy(struct drm_fb *fb) |
| 819 | { |
| 820 | if (fb->fb_id != 0) |
| 821 | drmModeRmFB(fb->fd, fb->fb_id); |
| 822 | weston_buffer_reference(&fb->buffer_ref, NULL); |
| 823 | free(fb); |
| 824 | } |
| 825 | |
| 826 | static void |
| 827 | drm_fb_destroy_dumb(struct drm_fb *fb) |
| 828 | { |
| 829 | struct drm_mode_destroy_dumb destroy_arg; |
| 830 | |
| 831 | assert(fb->type == BUFFER_PIXMAN_DUMB); |
| 832 | |
| 833 | if (fb->map && fb->size > 0) |
| 834 | munmap(fb->map, fb->size); |
| 835 | |
| 836 | memset(&destroy_arg, 0, sizeof(destroy_arg)); |
| 837 | destroy_arg.handle = fb->handle; |
| 838 | drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg); |
| 839 | |
| 840 | drm_fb_destroy(fb); |
| 841 | } |
| 842 | |
| 843 | static void |
| 844 | drm_fb_destroy_gbm(struct gbm_bo *bo, void *data) |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 845 | { |
| 846 | struct drm_fb *fb = data; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 847 | |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 848 | assert(fb->type == BUFFER_GBM_SURFACE || fb->type == BUFFER_CLIENT || |
| 849 | fb->type == BUFFER_CURSOR); |
Tomohito Esaki | 576f42e | 2017-04-04 17:54:24 +0100 | [diff] [blame] | 850 | drm_fb_destroy(fb); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | static struct drm_fb * |
Daniel Stone | f214fdc | 2016-11-14 17:43:57 +0000 | [diff] [blame] | 854 | drm_fb_create_dumb(struct drm_backend *b, int width, int height, |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 855 | uint32_t format) |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 856 | { |
| 857 | struct drm_fb *fb; |
| 858 | int ret; |
| 859 | |
| 860 | struct drm_mode_create_dumb create_arg; |
| 861 | struct drm_mode_destroy_dumb destroy_arg; |
| 862 | struct drm_mode_map_dumb map_arg; |
Daniel Stone | 1de4252 | 2016-11-17 18:17:16 +0000 | [diff] [blame] | 863 | uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 }; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 864 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 865 | fb = zalloc(sizeof *fb); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 866 | if (!fb) |
| 867 | return NULL; |
| 868 | |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 869 | fb->refcnt = 1; |
| 870 | |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 871 | fb->format = pixel_format_get_info(format); |
| 872 | if (!fb->format) { |
| 873 | weston_log("failed to look up format 0x%lx\n", |
| 874 | (unsigned long) format); |
| 875 | goto err_fb; |
| 876 | } |
| 877 | |
| 878 | if (!fb->format->depth || !fb->format->bpp) { |
| 879 | weston_log("format 0x%lx is not compatible with dumb buffers\n", |
| 880 | (unsigned long) format); |
| 881 | goto err_fb; |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 882 | } |
| 883 | |
Kristian Høgsberg | ac6104e | 2013-08-21 22:14:14 -0700 | [diff] [blame] | 884 | memset(&create_arg, 0, sizeof create_arg); |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 885 | create_arg.bpp = fb->format->bpp; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 886 | create_arg.width = width; |
| 887 | create_arg.height = height; |
| 888 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 889 | 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] | 890 | if (ret) |
| 891 | goto err_fb; |
| 892 | |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 893 | fb->type = BUFFER_PIXMAN_DUMB; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 894 | fb->handle = create_arg.handle; |
| 895 | fb->stride = create_arg.pitch; |
| 896 | fb->size = create_arg.size; |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 897 | fb->width = width; |
| 898 | fb->height = height; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 899 | fb->fd = b->drm.fd; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 900 | |
Daniel Stone | 1de4252 | 2016-11-17 18:17:16 +0000 | [diff] [blame] | 901 | handles[0] = fb->handle; |
| 902 | pitches[0] = fb->stride; |
| 903 | offsets[0] = 0; |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 904 | |
Daniel Stone | 1de4252 | 2016-11-17 18:17:16 +0000 | [diff] [blame] | 905 | ret = drmModeAddFB2(b->drm.fd, width, height, fb->format->format, |
| 906 | handles, pitches, offsets, &fb->fb_id, 0); |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 907 | if (ret) { |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 908 | ret = drmModeAddFB(b->drm.fd, width, height, |
| 909 | fb->format->depth, fb->format->bpp, |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 910 | fb->stride, fb->handle, &fb->fb_id); |
| 911 | } |
| 912 | |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 913 | if (ret) |
| 914 | goto err_bo; |
| 915 | |
Kristian Høgsberg | ac6104e | 2013-08-21 22:14:14 -0700 | [diff] [blame] | 916 | memset(&map_arg, 0, sizeof map_arg); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 917 | map_arg.handle = fb->handle; |
Chris Michael | eb2074a | 2013-05-01 21:26:02 -0400 | [diff] [blame] | 918 | 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] | 919 | if (ret) |
| 920 | goto err_add_fb; |
| 921 | |
Chris Michael | 4a7ce1f | 2015-11-10 10:40:37 -0500 | [diff] [blame] | 922 | fb->map = mmap(NULL, fb->size, PROT_WRITE, |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 923 | MAP_SHARED, b->drm.fd, map_arg.offset); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 924 | if (fb->map == MAP_FAILED) |
| 925 | goto err_add_fb; |
| 926 | |
| 927 | return fb; |
| 928 | |
| 929 | err_add_fb: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 930 | drmModeRmFB(b->drm.fd, fb->fb_id); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 931 | err_bo: |
| 932 | memset(&destroy_arg, 0, sizeof(destroy_arg)); |
| 933 | destroy_arg.handle = create_arg.handle; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 934 | 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] | 935 | err_fb: |
| 936 | free(fb); |
| 937 | return NULL; |
| 938 | } |
| 939 | |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 940 | static struct drm_fb * |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 941 | drm_fb_ref(struct drm_fb *fb) |
| 942 | { |
| 943 | fb->refcnt++; |
| 944 | return fb; |
| 945 | } |
| 946 | |
| 947 | static struct drm_fb * |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 948 | drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend, |
| 949 | uint32_t format, enum drm_fb_type type) |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 950 | { |
| 951 | struct drm_fb *fb = gbm_bo_get_user_data(bo); |
Derek Foreman | 482ffdf | 2016-07-08 12:50:57 -0500 | [diff] [blame] | 952 | uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 }; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 953 | int ret; |
| 954 | |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 955 | if (fb) { |
| 956 | assert(fb->type == type); |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 957 | return drm_fb_ref(fb); |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 958 | } |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 959 | |
Daniel Stone | 1de4252 | 2016-11-17 18:17:16 +0000 | [diff] [blame] | 960 | assert(format != 0); |
| 961 | |
Bryce Harrington | de16d89 | 2014-11-20 22:21:57 -0800 | [diff] [blame] | 962 | fb = zalloc(sizeof *fb); |
| 963 | if (fb == NULL) |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 964 | return NULL; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 965 | |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 966 | fb->type = type; |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 967 | fb->refcnt = 1; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 968 | fb->bo = bo; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 969 | |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 970 | fb->width = gbm_bo_get_width(bo); |
| 971 | fb->height = gbm_bo_get_height(bo); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 972 | fb->stride = gbm_bo_get_stride(bo); |
| 973 | fb->handle = gbm_bo_get_handle(bo).u32; |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 974 | fb->format = pixel_format_get_info(format); |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 975 | fb->size = fb->stride * fb->height; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 976 | fb->fd = backend->drm.fd; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 977 | |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 978 | if (!fb->format) { |
| 979 | weston_log("couldn't look up format 0x%lx\n", |
| 980 | (unsigned long) format); |
| 981 | goto err_free; |
| 982 | } |
| 983 | |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 984 | if (backend->min_width > fb->width || |
| 985 | fb->width > backend->max_width || |
| 986 | backend->min_height > fb->height || |
| 987 | fb->height > backend->max_height) { |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 988 | weston_log("bo geometry out of bounds\n"); |
| 989 | goto err_free; |
| 990 | } |
| 991 | |
Daniel Stone | 1de4252 | 2016-11-17 18:17:16 +0000 | [diff] [blame] | 992 | handles[0] = fb->handle; |
| 993 | pitches[0] = fb->stride; |
| 994 | offsets[0] = 0; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 995 | |
Daniel Stone | 1de4252 | 2016-11-17 18:17:16 +0000 | [diff] [blame] | 996 | ret = drmModeAddFB2(backend->drm.fd, fb->width, fb->height, |
| 997 | fb->format->format, handles, pitches, offsets, |
| 998 | &fb->fb_id, 0); |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 999 | if (ret && fb->format->depth && fb->format->bpp) |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 1000 | ret = drmModeAddFB(backend->drm.fd, fb->width, fb->height, |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 1001 | fb->format->depth, fb->format->bpp, |
| 1002 | fb->stride, fb->handle, &fb->fb_id); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1003 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1004 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1005 | weston_log("failed to create kms fb: %m\n"); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1006 | goto err_free; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1007 | } |
| 1008 | |
Tomohito Esaki | 576f42e | 2017-04-04 17:54:24 +0100 | [diff] [blame] | 1009 | 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] | 1010 | |
| 1011 | return fb; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1012 | |
| 1013 | err_free: |
| 1014 | free(fb); |
| 1015 | return NULL; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | static void |
Jason Ekstrand | 6bd6294 | 2013-06-20 20:38:23 -0500 | [diff] [blame] | 1019 | 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] | 1020 | { |
Pekka Paalanen | de685b8 | 2012-12-04 15:58:12 +0200 | [diff] [blame] | 1021 | assert(fb->buffer_ref.buffer == NULL); |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 1022 | assert(fb->type == BUFFER_CLIENT); |
Pekka Paalanen | de685b8 | 2012-12-04 15:58:12 +0200 | [diff] [blame] | 1023 | weston_buffer_reference(&fb->buffer_ref, buffer); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1024 | } |
| 1025 | |
Ander Conselvan de Oliveira | 526d461 | 2013-01-25 15:13:03 +0200 | [diff] [blame] | 1026 | static void |
Daniel Stone | 05a5ac2 | 2017-04-04 17:54:25 +0100 | [diff] [blame] | 1027 | drm_fb_unref(struct drm_fb *fb) |
Ander Conselvan de Oliveira | 526d461 | 2013-01-25 15:13:03 +0200 | [diff] [blame] | 1028 | { |
| 1029 | if (!fb) |
| 1030 | return; |
| 1031 | |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 1032 | assert(fb->refcnt > 0); |
| 1033 | if (--fb->refcnt > 0) |
| 1034 | return; |
| 1035 | |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 1036 | switch (fb->type) { |
| 1037 | case BUFFER_PIXMAN_DUMB: |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 1038 | drm_fb_destroy_dumb(fb); |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 1039 | break; |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 1040 | case BUFFER_CURSOR: |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 1041 | case BUFFER_CLIENT: |
| 1042 | gbm_bo_destroy(fb->bo); |
| 1043 | break; |
| 1044 | case BUFFER_GBM_SURFACE: |
Daniel Stone | 05a5ac2 | 2017-04-04 17:54:25 +0100 | [diff] [blame] | 1045 | gbm_surface_release_buffer(fb->gbm_surface, fb->bo); |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 1046 | break; |
| 1047 | default: |
| 1048 | assert(NULL); |
| 1049 | break; |
Ander Conselvan de Oliveira | 526d461 | 2013-01-25 15:13:03 +0200 | [diff] [blame] | 1050 | } |
| 1051 | } |
| 1052 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1053 | /** |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1054 | * Allocate a new, empty, plane state. |
| 1055 | */ |
| 1056 | static struct drm_plane_state * |
| 1057 | drm_plane_state_alloc(struct drm_output_state *state_output, |
| 1058 | struct drm_plane *plane) |
| 1059 | { |
| 1060 | struct drm_plane_state *state = zalloc(sizeof(*state)); |
| 1061 | |
| 1062 | assert(state); |
| 1063 | state->output_state = state_output; |
| 1064 | state->plane = plane; |
| 1065 | |
| 1066 | /* Here we only add the plane state to the desired link, and not |
| 1067 | * set the member. Having an output pointer set means that the |
| 1068 | * plane will be displayed on the output; this won't be the case |
| 1069 | * when we go to disable a plane. In this case, it must be part of |
| 1070 | * the commit (and thus the output state), but the member must be |
| 1071 | * NULL, as it will not be on any output when the state takes |
| 1072 | * effect. |
| 1073 | */ |
| 1074 | if (state_output) |
| 1075 | wl_list_insert(&state_output->plane_list, &state->link); |
| 1076 | else |
| 1077 | wl_list_init(&state->link); |
| 1078 | |
| 1079 | return state; |
| 1080 | } |
| 1081 | |
| 1082 | /** |
| 1083 | * Free an existing plane state. As a special case, the state will not |
| 1084 | * normally be freed if it is the current state; see drm_plane_set_state. |
| 1085 | */ |
| 1086 | static void |
| 1087 | drm_plane_state_free(struct drm_plane_state *state, bool force) |
| 1088 | { |
| 1089 | if (!state) |
| 1090 | return; |
| 1091 | |
| 1092 | wl_list_remove(&state->link); |
| 1093 | wl_list_init(&state->link); |
| 1094 | state->output_state = NULL; |
| 1095 | |
| 1096 | if (force || state != state->plane->state_cur) { |
| 1097 | drm_fb_unref(state->fb); |
| 1098 | free(state); |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | /** |
| 1103 | * Duplicate an existing plane state into a new plane state, storing it within |
| 1104 | * the given output state. If the output state already contains a plane state |
| 1105 | * for the drm_plane referenced by 'src', that plane state is freed first. |
| 1106 | */ |
| 1107 | static struct drm_plane_state * |
| 1108 | drm_plane_state_duplicate(struct drm_output_state *state_output, |
| 1109 | struct drm_plane_state *src) |
| 1110 | { |
| 1111 | struct drm_plane_state *dst = malloc(sizeof(*dst)); |
| 1112 | struct drm_plane_state *old, *tmp; |
| 1113 | |
| 1114 | assert(src); |
| 1115 | assert(dst); |
| 1116 | *dst = *src; |
| 1117 | wl_list_init(&dst->link); |
| 1118 | |
| 1119 | wl_list_for_each_safe(old, tmp, &state_output->plane_list, link) { |
| 1120 | /* Duplicating a plane state into the same output state, so |
| 1121 | * it can replace itself with an identical copy of itself, |
| 1122 | * makes no sense. */ |
| 1123 | assert(old != src); |
| 1124 | if (old->plane == dst->plane) |
| 1125 | drm_plane_state_free(old, false); |
| 1126 | } |
| 1127 | |
| 1128 | wl_list_insert(&state_output->plane_list, &dst->link); |
| 1129 | if (src->fb) |
| 1130 | dst->fb = drm_fb_ref(src->fb); |
| 1131 | dst->output_state = state_output; |
| 1132 | dst->complete = false; |
| 1133 | |
| 1134 | return dst; |
| 1135 | } |
| 1136 | |
| 1137 | /** |
| 1138 | * Remove a plane state from an output state; if the plane was previously |
| 1139 | * enabled, then replace it with a disabling state. This ensures that the |
| 1140 | * output state was untouched from it was before the plane state was |
| 1141 | * modified by the caller of this function. |
| 1142 | * |
| 1143 | * This is required as drm_output_state_get_plane may either allocate a |
| 1144 | * new plane state, in which case this function will just perform a matching |
| 1145 | * drm_plane_state_free, or it may instead repurpose an existing disabling |
| 1146 | * state (if the plane was previously active), in which case this function |
| 1147 | * will reset it. |
| 1148 | */ |
| 1149 | static void |
| 1150 | drm_plane_state_put_back(struct drm_plane_state *state) |
| 1151 | { |
| 1152 | struct drm_output_state *state_output; |
| 1153 | struct drm_plane *plane; |
| 1154 | |
| 1155 | if (!state) |
| 1156 | return; |
| 1157 | |
| 1158 | state_output = state->output_state; |
| 1159 | plane = state->plane; |
| 1160 | drm_plane_state_free(state, false); |
| 1161 | |
| 1162 | /* Plane was previously disabled; no need to keep this temporary |
| 1163 | * state around. */ |
| 1164 | if (!plane->state_cur->fb) |
| 1165 | return; |
| 1166 | |
| 1167 | (void) drm_plane_state_alloc(state_output, plane); |
| 1168 | } |
| 1169 | |
| 1170 | /** |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 1171 | * Return a plane state from a drm_output_state. |
| 1172 | */ |
| 1173 | static struct drm_plane_state * |
| 1174 | drm_output_state_get_existing_plane(struct drm_output_state *state_output, |
| 1175 | struct drm_plane *plane) |
| 1176 | { |
| 1177 | struct drm_plane_state *ps; |
| 1178 | |
| 1179 | wl_list_for_each(ps, &state_output->plane_list, link) { |
| 1180 | if (ps->plane == plane) |
| 1181 | return ps; |
| 1182 | } |
| 1183 | |
| 1184 | return NULL; |
| 1185 | } |
| 1186 | |
| 1187 | /** |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1188 | * Return a plane state from a drm_output_state, either existing or |
| 1189 | * freshly allocated. |
| 1190 | */ |
| 1191 | static struct drm_plane_state * |
| 1192 | drm_output_state_get_plane(struct drm_output_state *state_output, |
| 1193 | struct drm_plane *plane) |
| 1194 | { |
| 1195 | struct drm_plane_state *ps; |
| 1196 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 1197 | ps = drm_output_state_get_existing_plane(state_output, plane); |
| 1198 | if (ps) |
| 1199 | return ps; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1200 | |
| 1201 | return drm_plane_state_alloc(state_output, plane); |
| 1202 | } |
| 1203 | |
| 1204 | /** |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1205 | * Allocate a new, empty drm_output_state. This should not generally be used |
| 1206 | * in the repaint cycle; see drm_output_state_duplicate. |
| 1207 | */ |
| 1208 | static struct drm_output_state * |
| 1209 | drm_output_state_alloc(struct drm_output *output, |
| 1210 | struct drm_pending_state *pending_state) |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 1211 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1212 | struct drm_output_state *state = zalloc(sizeof(*state)); |
| 1213 | |
| 1214 | assert(state); |
| 1215 | state->output = output; |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1216 | state->dpms = WESTON_DPMS_OFF; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1217 | state->pending_state = pending_state; |
| 1218 | if (pending_state) |
| 1219 | wl_list_insert(&pending_state->output_list, &state->link); |
| 1220 | else |
| 1221 | wl_list_init(&state->link); |
| 1222 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1223 | wl_list_init(&state->plane_list); |
| 1224 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1225 | return state; |
| 1226 | } |
| 1227 | |
| 1228 | /** |
| 1229 | * Duplicate an existing drm_output_state into a new one. This is generally |
| 1230 | * used during the repaint cycle, to capture the existing state of an output |
| 1231 | * and modify it to create a new state to be used. |
| 1232 | * |
| 1233 | * The mode determines whether the output will be reset to an a blank state, |
| 1234 | * or an exact mirror of the current state. |
| 1235 | */ |
| 1236 | static struct drm_output_state * |
| 1237 | drm_output_state_duplicate(struct drm_output_state *src, |
| 1238 | struct drm_pending_state *pending_state, |
| 1239 | enum drm_output_state_duplicate_mode plane_mode) |
| 1240 | { |
| 1241 | struct drm_output_state *dst = malloc(sizeof(*dst)); |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1242 | struct drm_plane_state *ps; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1243 | |
| 1244 | assert(dst); |
| 1245 | |
| 1246 | /* Copy the whole structure, then individually modify the |
| 1247 | * pending_state, as well as the list link into our pending |
| 1248 | * state. */ |
| 1249 | *dst = *src; |
| 1250 | |
| 1251 | dst->pending_state = pending_state; |
| 1252 | if (pending_state) |
| 1253 | wl_list_insert(&pending_state->output_list, &dst->link); |
| 1254 | else |
| 1255 | wl_list_init(&dst->link); |
| 1256 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1257 | wl_list_init(&dst->plane_list); |
| 1258 | |
| 1259 | wl_list_for_each(ps, &src->plane_list, link) { |
| 1260 | /* Don't carry planes which are now disabled; these should be |
| 1261 | * free for other outputs to reuse. */ |
| 1262 | if (!ps->output) |
| 1263 | continue; |
| 1264 | |
| 1265 | if (plane_mode == DRM_OUTPUT_STATE_CLEAR_PLANES) |
| 1266 | (void) drm_plane_state_alloc(dst, ps->plane); |
| 1267 | else |
| 1268 | (void) drm_plane_state_duplicate(dst, ps); |
| 1269 | } |
| 1270 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1271 | return dst; |
| 1272 | } |
| 1273 | |
| 1274 | /** |
| 1275 | * Free an unused drm_output_state. |
| 1276 | */ |
| 1277 | static void |
| 1278 | drm_output_state_free(struct drm_output_state *state) |
| 1279 | { |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1280 | struct drm_plane_state *ps, *next; |
| 1281 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1282 | if (!state) |
| 1283 | return; |
| 1284 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1285 | wl_list_for_each_safe(ps, next, &state->plane_list, link) |
| 1286 | drm_plane_state_free(ps, false); |
| 1287 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1288 | wl_list_remove(&state->link); |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1289 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1290 | free(state); |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 1291 | } |
| 1292 | |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 1293 | /** |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1294 | * Get output state to disable output |
| 1295 | * |
| 1296 | * Returns a pointer to an output_state object which can be used to disable |
| 1297 | * an output (e.g. DPMS off). |
| 1298 | * |
| 1299 | * @param pending_state The pending state object owning this update |
| 1300 | * @param output The output to disable |
| 1301 | * @returns A drm_output_state to disable the output |
| 1302 | */ |
| 1303 | static struct drm_output_state * |
| 1304 | drm_output_get_disable_state(struct drm_pending_state *pending_state, |
| 1305 | struct drm_output *output) |
| 1306 | { |
| 1307 | struct drm_output_state *output_state; |
| 1308 | |
| 1309 | output_state = drm_output_state_duplicate(output->state_cur, |
| 1310 | pending_state, |
| 1311 | DRM_OUTPUT_STATE_CLEAR_PLANES); |
| 1312 | output_state->dpms = WESTON_DPMS_OFF; |
| 1313 | |
| 1314 | return output_state; |
| 1315 | } |
| 1316 | |
| 1317 | /** |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 1318 | * Allocate a new drm_pending_state |
| 1319 | * |
| 1320 | * Allocate a new, empty, 'pending state' structure to be used across a |
| 1321 | * repaint cycle or similar. |
| 1322 | * |
| 1323 | * @param backend DRM backend |
| 1324 | * @returns Newly-allocated pending state structure |
| 1325 | */ |
| 1326 | static struct drm_pending_state * |
| 1327 | drm_pending_state_alloc(struct drm_backend *backend) |
| 1328 | { |
| 1329 | struct drm_pending_state *ret; |
| 1330 | |
| 1331 | ret = calloc(1, sizeof(*ret)); |
| 1332 | if (!ret) |
| 1333 | return NULL; |
| 1334 | |
| 1335 | ret->backend = backend; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1336 | wl_list_init(&ret->output_list); |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 1337 | |
| 1338 | return ret; |
| 1339 | } |
| 1340 | |
| 1341 | /** |
| 1342 | * Free a drm_pending_state structure |
| 1343 | * |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1344 | * Frees a pending_state structure, as well as any output_states connected |
| 1345 | * to this pending state. |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 1346 | * |
| 1347 | * @param pending_state Pending state structure to free |
| 1348 | */ |
| 1349 | static void |
| 1350 | drm_pending_state_free(struct drm_pending_state *pending_state) |
| 1351 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1352 | struct drm_output_state *output_state, *tmp; |
| 1353 | |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 1354 | if (!pending_state) |
| 1355 | return; |
| 1356 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1357 | wl_list_for_each_safe(output_state, tmp, &pending_state->output_list, |
| 1358 | link) { |
| 1359 | drm_output_state_free(output_state); |
| 1360 | } |
| 1361 | |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 1362 | free(pending_state); |
| 1363 | } |
| 1364 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1365 | /** |
| 1366 | * Find an output state in a pending state |
| 1367 | * |
| 1368 | * Given a pending_state structure, find the output_state for a particular |
| 1369 | * output. |
| 1370 | * |
| 1371 | * @param pending_state Pending state structure to search |
| 1372 | * @param output Output to find state for |
| 1373 | * @returns Output state if present, or NULL if not |
| 1374 | */ |
| 1375 | static struct drm_output_state * |
| 1376 | drm_pending_state_get_output(struct drm_pending_state *pending_state, |
| 1377 | struct drm_output *output) |
| 1378 | { |
| 1379 | struct drm_output_state *output_state; |
| 1380 | |
| 1381 | wl_list_for_each(output_state, &pending_state->output_list, link) { |
| 1382 | if (output_state->output == output) |
| 1383 | return output_state; |
| 1384 | } |
| 1385 | |
| 1386 | return NULL; |
| 1387 | } |
| 1388 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1389 | static int drm_pending_state_apply_sync(struct drm_pending_state *state); |
| 1390 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1391 | /** |
| 1392 | * Mark a drm_output_state (the output's last state) as complete. This handles |
| 1393 | * any post-completion actions such as updating the repaint timer, disabling the |
| 1394 | * output, and finally freeing the state. |
| 1395 | */ |
| 1396 | static void |
| 1397 | drm_output_update_complete(struct drm_output *output, uint32_t flags, |
| 1398 | unsigned int sec, unsigned int usec) |
| 1399 | { |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1400 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1401 | struct drm_plane_state *ps; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1402 | struct timespec ts; |
| 1403 | |
| 1404 | /* Stop the pageflip timer instead of rearming it here */ |
| 1405 | if (output->pageflip_timer) |
| 1406 | wl_event_source_timer_update(output->pageflip_timer, 0); |
| 1407 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1408 | wl_list_for_each(ps, &output->state_cur->plane_list, link) |
| 1409 | ps->complete = true; |
| 1410 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1411 | drm_output_state_free(output->state_last); |
| 1412 | output->state_last = NULL; |
| 1413 | |
| 1414 | if (output->destroy_pending) { |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1415 | output->destroy_pending = 0; |
| 1416 | output->disable_pending = 0; |
| 1417 | output->dpms_off_pending = 0; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1418 | drm_output_destroy(&output->base); |
| 1419 | return; |
| 1420 | } else if (output->disable_pending) { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1421 | output->disable_pending = 0; |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1422 | output->dpms_off_pending = 0; |
| 1423 | weston_output_disable(&output->base); |
| 1424 | return; |
| 1425 | } else if (output->dpms_off_pending) { |
| 1426 | struct drm_pending_state *pending = drm_pending_state_alloc(b); |
| 1427 | output->dpms_off_pending = 0; |
| 1428 | drm_output_get_disable_state(pending, output); |
| 1429 | drm_pending_state_apply_sync(pending); |
| 1430 | return; |
| 1431 | } else if (output->state_cur->dpms == WESTON_DPMS_OFF && |
| 1432 | output->base.repaint_status != REPAINT_AWAITING_COMPLETION) { |
| 1433 | /* DPMS can happen to us either in the middle of a repaint |
| 1434 | * cycle (when we have painted fresh content, only to throw it |
| 1435 | * away for DPMS off), or at any other random point. If the |
| 1436 | * latter is true, then we cannot go through finish_frame, |
| 1437 | * because the repaint machinery does not expect this. */ |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1438 | return; |
| 1439 | } |
| 1440 | |
| 1441 | ts.tv_sec = sec; |
| 1442 | ts.tv_nsec = usec * 1000; |
| 1443 | weston_output_finish_frame(&output->base, &ts, flags); |
| 1444 | |
| 1445 | /* We can't call this from frame_notify, because the output's |
| 1446 | * repaint needed flag is cleared just after that */ |
| 1447 | if (output->recorder) |
| 1448 | weston_output_schedule_repaint(&output->base); |
| 1449 | } |
| 1450 | |
| 1451 | /** |
| 1452 | * Mark an output state as current on the output, i.e. it has been |
| 1453 | * submitted to the kernel. The mode argument determines whether this |
| 1454 | * update will be applied synchronously (e.g. when calling drmModeSetCrtc), |
| 1455 | * or asynchronously (in which case we wait for events to complete). |
| 1456 | */ |
| 1457 | static void |
| 1458 | drm_output_assign_state(struct drm_output_state *state, |
| 1459 | enum drm_state_apply_mode mode) |
| 1460 | { |
| 1461 | struct drm_output *output = state->output; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 1462 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1463 | struct drm_plane_state *plane_state; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1464 | |
| 1465 | assert(!output->state_last); |
| 1466 | |
| 1467 | if (mode == DRM_STATE_APPLY_ASYNC) |
| 1468 | output->state_last = output->state_cur; |
| 1469 | else |
| 1470 | drm_output_state_free(output->state_cur); |
| 1471 | |
| 1472 | wl_list_remove(&state->link); |
| 1473 | wl_list_init(&state->link); |
| 1474 | state->pending_state = NULL; |
| 1475 | |
| 1476 | output->state_cur = state; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1477 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 1478 | if (b->atomic_modeset && mode == DRM_STATE_APPLY_ASYNC) |
| 1479 | output->atomic_complete_pending = 1; |
| 1480 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1481 | /* Replace state_cur on each affected plane with the new state, being |
| 1482 | * careful to dispose of orphaned (but only orphaned) previous state. |
| 1483 | * If the previous state is not orphaned (still has an output_state |
| 1484 | * attached), it will be disposed of by freeing the output_state. */ |
| 1485 | wl_list_for_each(plane_state, &state->plane_list, link) { |
| 1486 | struct drm_plane *plane = plane_state->plane; |
| 1487 | |
| 1488 | if (plane->state_cur && !plane->state_cur->output_state) |
| 1489 | drm_plane_state_free(plane->state_cur, true); |
| 1490 | plane->state_cur = plane_state; |
| 1491 | |
| 1492 | if (mode != DRM_STATE_APPLY_ASYNC) { |
| 1493 | plane_state->complete = true; |
| 1494 | continue; |
| 1495 | } |
| 1496 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 1497 | if (b->atomic_modeset) |
| 1498 | continue; |
| 1499 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1500 | if (plane->type == WDRM_PLANE_TYPE_OVERLAY) |
| 1501 | output->vblank_pending++; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1502 | else if (plane->type == WDRM_PLANE_TYPE_PRIMARY) |
| 1503 | output->page_flip_pending = 1; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1504 | } |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1507 | static int |
| 1508 | drm_view_transform_supported(struct weston_view *ev) |
| 1509 | { |
| 1510 | return !ev->transform.enabled || |
| 1511 | (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE); |
| 1512 | } |
| 1513 | |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 1514 | static uint32_t |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 1515 | drm_output_check_scanout_format(struct drm_output *output, |
| 1516 | struct weston_surface *es, struct gbm_bo *bo) |
| 1517 | { |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 1518 | uint32_t format; |
| 1519 | pixman_region32_t r; |
| 1520 | |
| 1521 | format = gbm_bo_get_format(bo); |
| 1522 | |
Kristian Høgsberg | 3f97b34 | 2013-10-16 16:08:57 -0700 | [diff] [blame] | 1523 | if (format == GBM_FORMAT_ARGB8888) { |
| 1524 | /* We can scanout an ARGB buffer if the surface's |
| 1525 | * opaque region covers the whole output, but we have |
| 1526 | * to use XRGB as the KMS format code. */ |
Kristian Høgsberg | 1be87e3 | 2014-01-17 14:22:41 -0800 | [diff] [blame] | 1527 | pixman_region32_init_rect(&r, 0, 0, |
| 1528 | output->base.width, |
| 1529 | output->base.height); |
| 1530 | pixman_region32_subtract(&r, &r, &es->opaque); |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 1531 | |
| 1532 | if (!pixman_region32_not_empty(&r)) |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 1533 | format = GBM_FORMAT_XRGB8888; |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 1534 | |
| 1535 | pixman_region32_fini(&r); |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 1536 | } |
Kristian Høgsberg | 3f97b34 | 2013-10-16 16:08:57 -0700 | [diff] [blame] | 1537 | |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 1538 | if (output->gbm_format == format) |
Kristian Høgsberg | 3f97b34 | 2013-10-16 16:08:57 -0700 | [diff] [blame] | 1539 | return format; |
| 1540 | |
| 1541 | return 0; |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 1542 | } |
| 1543 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1544 | static struct weston_plane * |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1545 | drm_output_prepare_scanout_view(struct drm_output_state *output_state, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1546 | struct weston_view *ev) |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 1547 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1548 | struct drm_output *output = output_state->output; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1549 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1550 | struct drm_plane *scanout_plane = output->scanout_plane; |
| 1551 | struct drm_plane_state *state; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1552 | struct weston_buffer *buffer = ev->surface->buffer_ref.buffer; |
Pekka Paalanen | 952b6c8 | 2014-03-14 14:38:15 +0200 | [diff] [blame] | 1553 | struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1554 | struct gbm_bo *bo; |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 1555 | uint32_t format; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 1556 | |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 1557 | /* Don't import buffers which span multiple outputs. */ |
| 1558 | if (ev->output_mask != (1u << output->base.id)) |
| 1559 | return NULL; |
| 1560 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1561 | /* We use GBM to import buffers. */ |
| 1562 | if (b->gbm == NULL) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1563 | return NULL; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 1564 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1565 | if (buffer == NULL) |
| 1566 | return NULL; |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 1567 | if (wl_shm_buffer_get(buffer->resource)) |
| 1568 | return NULL; |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1569 | |
| 1570 | /* Make sure our view is exactly compatible with the output. */ |
| 1571 | if (ev->geometry.x != output->base.x || |
| 1572 | ev->geometry.y != output->base.y) |
| 1573 | return NULL; |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 1574 | if (buffer->width != output->base.current_mode->width || |
| 1575 | buffer->height != output->base.current_mode->height) |
| 1576 | return NULL; |
| 1577 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1578 | if (ev->transform.enabled) |
| 1579 | return NULL; |
Pekka Paalanen | 5580f22 | 2015-02-17 16:33:18 +0200 | [diff] [blame] | 1580 | if (ev->geometry.scissor_enabled) |
| 1581 | return NULL; |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1582 | if (viewport->buffer.transform != output->base.transform) |
| 1583 | return NULL; |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 1584 | if (viewport->buffer.scale != output->base.current_scale) |
| 1585 | return NULL; |
| 1586 | if (!drm_view_transform_supported(ev)) |
| 1587 | return NULL; |
| 1588 | |
| 1589 | if (ev->alpha != 1.0f) |
| 1590 | return NULL; |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1591 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1592 | state = drm_output_state_get_plane(output_state, scanout_plane); |
| 1593 | if (state->fb) { |
| 1594 | /* If there is already a framebuffer on the scanout plane, |
| 1595 | * a client view has already been placed on the scanout |
| 1596 | * view. In that case, do not free or put back the state, |
| 1597 | * but just leave it in place and quietly exit. */ |
| 1598 | return NULL; |
| 1599 | } |
| 1600 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1601 | bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER, |
Kristian Høgsberg | 6399646 | 2013-09-03 22:27:08 -0700 | [diff] [blame] | 1602 | buffer->resource, GBM_BO_USE_SCANOUT); |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 1603 | |
Rob Bradford | 9b10187 | 2012-09-14 23:25:41 +0100 | [diff] [blame] | 1604 | /* Unable to use the buffer for scanout */ |
| 1605 | if (!bo) |
| 1606 | return NULL; |
| 1607 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1608 | format = drm_output_check_scanout_format(output, ev->surface, bo); |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 1609 | if (format == 0) { |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1610 | drm_plane_state_put_back(state); |
Ander Conselvan de Oliveira | a64b15d | 2012-05-02 16:42:22 +0300 | [diff] [blame] | 1611 | gbm_bo_destroy(bo); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1612 | return NULL; |
Ander Conselvan de Oliveira | a64b15d | 2012-05-02 16:42:22 +0300 | [diff] [blame] | 1613 | } |
| 1614 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1615 | state->fb = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT); |
| 1616 | if (!state->fb) { |
| 1617 | drm_plane_state_put_back(state); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1618 | gbm_bo_destroy(bo); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1619 | return NULL; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1620 | } |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 1621 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1622 | drm_fb_set_buffer(state->fb, buffer); |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 1623 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1624 | state->output = output; |
| 1625 | |
| 1626 | state->src_x = 0; |
| 1627 | state->src_y = 0; |
| 1628 | state->src_w = state->fb->width << 16; |
| 1629 | state->src_h = state->fb->height << 16; |
| 1630 | |
| 1631 | state->dest_x = 0; |
| 1632 | state->dest_y = 0; |
| 1633 | state->dest_w = output->base.current_mode->width; |
| 1634 | state->dest_h = output->base.current_mode->height; |
| 1635 | |
| 1636 | return &scanout_plane->base; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 1637 | } |
| 1638 | |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1639 | static struct drm_fb * |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1640 | 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] | 1641 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1642 | struct drm_output *output = state->output; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1643 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1644 | struct gbm_bo *bo; |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1645 | struct drm_fb *ret; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1646 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1647 | output->base.compositor->renderer->repaint_output(&output->base, |
| 1648 | damage); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1649 | |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 1650 | bo = gbm_surface_lock_front_buffer(output->gbm_surface); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1651 | if (!bo) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1652 | weston_log("failed to lock front buffer: %m\n"); |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1653 | return NULL; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1654 | } |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1655 | |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1656 | ret = drm_fb_get_from_bo(bo, b, output->gbm_format, BUFFER_GBM_SURFACE); |
| 1657 | if (!ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1658 | weston_log("failed to get drm_fb for bo\n"); |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 1659 | gbm_surface_release_buffer(output->gbm_surface, bo); |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1660 | return NULL; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1661 | } |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1662 | ret->gbm_surface = output->gbm_surface; |
| 1663 | |
| 1664 | return ret; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1665 | } |
| 1666 | |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1667 | static struct drm_fb * |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1668 | drm_output_render_pixman(struct drm_output_state *state, |
| 1669 | pixman_region32_t *damage) |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1670 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1671 | struct drm_output *output = state->output; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1672 | struct weston_compositor *ec = output->base.compositor; |
| 1673 | pixman_region32_t total_damage, previous_damage; |
| 1674 | |
| 1675 | pixman_region32_init(&total_damage); |
| 1676 | pixman_region32_init(&previous_damage); |
| 1677 | |
| 1678 | pixman_region32_copy(&previous_damage, damage); |
| 1679 | |
| 1680 | pixman_region32_union(&total_damage, damage, &output->previous_damage); |
| 1681 | pixman_region32_copy(&output->previous_damage, &previous_damage); |
| 1682 | |
| 1683 | output->current_image ^= 1; |
| 1684 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1685 | pixman_renderer_output_set_buffer(&output->base, |
| 1686 | output->image[output->current_image]); |
| 1687 | |
| 1688 | ec->renderer->repaint_output(&output->base, &total_damage); |
| 1689 | |
| 1690 | pixman_region32_fini(&total_damage); |
| 1691 | pixman_region32_fini(&previous_damage); |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1692 | |
| 1693 | return drm_fb_ref(output->dumb[output->current_image]); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1694 | } |
| 1695 | |
| 1696 | static void |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1697 | 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] | 1698 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1699 | struct drm_output *output = state->output; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1700 | struct weston_compositor *c = output->base.compositor; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1701 | struct drm_plane_state *scanout_state; |
Daniel Stone | e95169b | 2016-11-14 17:46:59 +0000 | [diff] [blame] | 1702 | struct drm_plane *scanout_plane = output->scanout_plane; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1703 | struct drm_backend *b = to_drm_backend(c); |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1704 | struct drm_fb *fb; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1705 | |
Daniel Stone | 4e84f7d | 2017-04-04 17:54:29 +0100 | [diff] [blame] | 1706 | /* If we already have a client buffer promoted to scanout, then we don't |
| 1707 | * want to render. */ |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1708 | scanout_state = drm_output_state_get_plane(state, |
| 1709 | output->scanout_plane); |
| 1710 | if (scanout_state->fb) |
Daniel Stone | 4e84f7d | 2017-04-04 17:54:29 +0100 | [diff] [blame] | 1711 | return; |
| 1712 | |
Daniel Stone | e95169b | 2016-11-14 17:46:59 +0000 | [diff] [blame] | 1713 | if (!pixman_region32_not_empty(damage) && |
| 1714 | scanout_plane->state_cur->fb && |
| 1715 | (scanout_plane->state_cur->fb->type == BUFFER_GBM_SURFACE || |
| 1716 | scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB) && |
| 1717 | scanout_plane->state_cur->fb->width == |
| 1718 | output->base.current_mode->width && |
| 1719 | scanout_plane->state_cur->fb->height == |
| 1720 | output->base.current_mode->height) { |
| 1721 | fb = drm_fb_ref(scanout_plane->state_cur->fb); |
| 1722 | } else if (b->use_pixman) { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1723 | fb = drm_output_render_pixman(state, damage); |
Daniel Stone | e95169b | 2016-11-14 17:46:59 +0000 | [diff] [blame] | 1724 | } else { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1725 | fb = drm_output_render_gl(state, damage); |
Daniel Stone | e95169b | 2016-11-14 17:46:59 +0000 | [diff] [blame] | 1726 | } |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1727 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1728 | if (!fb) { |
| 1729 | drm_plane_state_put_back(scanout_state); |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1730 | return; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
| 1733 | scanout_state->fb = fb; |
| 1734 | scanout_state->output = output; |
| 1735 | |
| 1736 | scanout_state->src_x = 0; |
| 1737 | scanout_state->src_y = 0; |
| 1738 | scanout_state->src_w = output->base.current_mode->width << 16; |
| 1739 | scanout_state->src_h = output->base.current_mode->height << 16; |
| 1740 | |
| 1741 | scanout_state->dest_x = 0; |
| 1742 | scanout_state->dest_y = 0; |
| 1743 | scanout_state->dest_w = scanout_state->src_w >> 16; |
| 1744 | scanout_state->dest_h = scanout_state->src_h >> 16; |
| 1745 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1746 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1747 | pixman_region32_subtract(&c->primary_plane.damage, |
| 1748 | &c->primary_plane.damage, damage); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1749 | } |
| 1750 | |
| 1751 | static void |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 1752 | drm_output_set_gamma(struct weston_output *output_base, |
| 1753 | uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b) |
| 1754 | { |
| 1755 | int rc; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1756 | struct drm_output *output = to_drm_output(output_base); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1757 | struct drm_backend *backend = |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1758 | to_drm_backend(output->base.compositor); |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 1759 | |
| 1760 | /* check */ |
| 1761 | if (output_base->gamma_size != size) |
| 1762 | return; |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 1763 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1764 | rc = drmModeCrtcSetGamma(backend->drm.fd, |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 1765 | output->crtc_id, |
| 1766 | size, r, g, b); |
| 1767 | if (rc) |
| 1768 | weston_log("set gamma failed: %m\n"); |
| 1769 | } |
| 1770 | |
Bryce Harrington | ada4f07 | 2015-06-30 13:25:46 -0700 | [diff] [blame] | 1771 | /* Determine the type of vblank synchronization to use for the output. |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 1772 | * |
Bryce Harrington | ada4f07 | 2015-06-30 13:25:46 -0700 | [diff] [blame] | 1773 | * The pipe parameter indicates which CRTC is in use. Knowing this, we |
| 1774 | * can determine which vblank sequence type to use for it. Traditional |
| 1775 | * cards had only two CRTCs, with CRTC 0 using no special flags, and |
| 1776 | * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe |
| 1777 | * parameter indicates this. |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 1778 | * |
Bryce Harrington | ada4f07 | 2015-06-30 13:25:46 -0700 | [diff] [blame] | 1779 | * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between |
| 1780 | * 0-31. If this is non-zero it indicates we're dealing with a |
| 1781 | * multi-gpu situation and we need to calculate the vblank sync |
| 1782 | * using DRM_BLANK_HIGH_CRTC_MASK. |
| 1783 | */ |
Pekka Paalanen | c8a1ff0 | 2015-07-02 15:06:08 +0300 | [diff] [blame] | 1784 | static unsigned int |
| 1785 | drm_waitvblank_pipe(struct drm_output *output) |
Mario Kleiner | 2ab4f4e | 2015-06-21 21:25:13 +0200 | [diff] [blame] | 1786 | { |
| 1787 | if (output->pipe > 1) |
| 1788 | return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) & |
| 1789 | DRM_VBLANK_HIGH_CRTC_MASK; |
| 1790 | else if (output->pipe > 0) |
| 1791 | return DRM_VBLANK_SECONDARY; |
| 1792 | else |
| 1793 | return 0; |
| 1794 | } |
| 1795 | |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 1796 | static int |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 1797 | drm_output_apply_state_legacy(struct drm_output_state *state) |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1798 | { |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1799 | struct drm_output *output = state->output; |
| 1800 | struct drm_backend *backend = to_drm_backend(output->base.compositor); |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1801 | struct drm_plane *scanout_plane = output->scanout_plane; |
Pekka Paalanen | 02aeb5c | 2017-09-12 16:02:01 +0300 | [diff] [blame] | 1802 | struct drm_property_info *dpms_prop; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1803 | struct drm_plane_state *scanout_state; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1804 | struct drm_plane_state *ps; |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 1805 | struct drm_plane *p; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1806 | struct drm_mode *mode; |
Pekka Paalanen | 02aeb5c | 2017-09-12 16:02:01 +0300 | [diff] [blame] | 1807 | struct drm_head *head; |
| 1808 | uint32_t connectors[MAX_CLONED_CONNECTORS]; |
| 1809 | int n_conn = 0; |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1810 | struct timespec now; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1811 | int ret = 0; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1812 | |
Pekka Paalanen | 02aeb5c | 2017-09-12 16:02:01 +0300 | [diff] [blame] | 1813 | wl_list_for_each(head, &output->base.head_list, base.output_link) { |
| 1814 | assert(n_conn < MAX_CLONED_CONNECTORS); |
| 1815 | connectors[n_conn++] = head->connector_id; |
| 1816 | } |
| 1817 | |
Derek Foreman | 2cd87fe | 2017-04-13 13:48:48 -0500 | [diff] [blame] | 1818 | /* If disable_planes is set then assign_planes() wasn't |
| 1819 | * called for this render, so we could still have a stale |
| 1820 | * cursor plane set up. |
| 1821 | */ |
| 1822 | if (output->base.disable_planes) { |
| 1823 | output->cursor_view = NULL; |
Greg V | 1f78176 | 2018-02-19 17:59:42 +0300 | [diff] [blame] | 1824 | if (output->cursor_plane) { |
| 1825 | output->cursor_plane->base.x = INT32_MIN; |
| 1826 | output->cursor_plane->base.y = INT32_MIN; |
| 1827 | } |
Derek Foreman | 2cd87fe | 2017-04-13 13:48:48 -0500 | [diff] [blame] | 1828 | } |
| 1829 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1830 | if (state->dpms != WESTON_DPMS_ON) { |
| 1831 | wl_list_for_each(ps, &state->plane_list, link) { |
| 1832 | p = ps->plane; |
| 1833 | assert(ps->fb == NULL); |
| 1834 | assert(ps->output == NULL); |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1835 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1836 | if (p->type != WDRM_PLANE_TYPE_OVERLAY) |
| 1837 | continue; |
| 1838 | |
| 1839 | ret = drmModeSetPlane(backend->drm.fd, p->plane_id, |
| 1840 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
| 1841 | if (ret) |
| 1842 | weston_log("drmModeSetPlane failed disable: %m\n"); |
| 1843 | } |
| 1844 | |
| 1845 | if (output->cursor_plane) { |
| 1846 | ret = drmModeSetCursor(backend->drm.fd, output->crtc_id, |
| 1847 | 0, 0, 0); |
| 1848 | if (ret) |
| 1849 | weston_log("drmModeSetCursor failed disable: %m\n"); |
| 1850 | } |
| 1851 | |
| 1852 | ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id, 0, 0, 0, |
Pekka Paalanen | 02aeb5c | 2017-09-12 16:02:01 +0300 | [diff] [blame] | 1853 | NULL, 0, NULL); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1854 | if (ret) |
| 1855 | weston_log("drmModeSetCrtc failed disabling: %m\n"); |
| 1856 | |
| 1857 | drm_output_assign_state(state, DRM_STATE_APPLY_SYNC); |
| 1858 | weston_compositor_read_presentation_clock(output->base.compositor, &now); |
| 1859 | drm_output_update_complete(output, |
| 1860 | WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION, |
| 1861 | now.tv_sec, now.tv_nsec / 1000); |
| 1862 | |
| 1863 | return 0; |
| 1864 | } |
| 1865 | |
| 1866 | scanout_state = |
| 1867 | drm_output_state_get_existing_plane(state, scanout_plane); |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 1868 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1869 | /* The legacy SetCrtc API doesn't allow us to do scaling, and the |
| 1870 | * legacy PageFlip API doesn't allow us to do clipping either. */ |
| 1871 | assert(scanout_state->src_x == 0); |
| 1872 | assert(scanout_state->src_y == 0); |
| 1873 | assert(scanout_state->src_w == |
| 1874 | (unsigned) (output->base.current_mode->width << 16)); |
| 1875 | assert(scanout_state->src_h == |
| 1876 | (unsigned) (output->base.current_mode->height << 16)); |
| 1877 | assert(scanout_state->dest_x == 0); |
| 1878 | assert(scanout_state->dest_y == 0); |
| 1879 | assert(scanout_state->dest_w == scanout_state->src_w >> 16); |
| 1880 | assert(scanout_state->dest_h == scanout_state->src_h >> 16); |
| 1881 | |
Daniel Stone | cb04cc4 | 2016-11-16 11:51:27 +0000 | [diff] [blame] | 1882 | mode = to_drm_mode(output->base.current_mode); |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 1883 | if (backend->state_invalid || !scanout_plane->state_cur->fb || |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1884 | scanout_plane->state_cur->fb->stride != scanout_state->fb->stride) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1885 | ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id, |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1886 | scanout_state->fb->fb_id, |
| 1887 | 0, 0, |
Pekka Paalanen | 02aeb5c | 2017-09-12 16:02:01 +0300 | [diff] [blame] | 1888 | connectors, n_conn, |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1889 | &mode->mode_info); |
| 1890 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1891 | weston_log("set mode failed: %m\n"); |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1892 | goto err; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1893 | } |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 1894 | } |
| 1895 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1896 | if (drmModePageFlip(backend->drm.fd, output->crtc_id, |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1897 | scanout_state->fb->fb_id, |
Kristian Høgsberg | 54f14c3 | 2012-01-18 11:47:41 -0500 | [diff] [blame] | 1898 | DRM_MODE_PAGE_FLIP_EVENT, output) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1899 | weston_log("queueing pageflip failed: %m\n"); |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1900 | goto err; |
Kristian Høgsberg | 54f14c3 | 2012-01-18 11:47:41 -0500 | [diff] [blame] | 1901 | } |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 1902 | |
Daniel Stone | 205c0a0 | 2017-04-04 17:54:33 +0100 | [diff] [blame] | 1903 | assert(!output->page_flip_pending); |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 1904 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 1905 | if (output->pageflip_timer) |
| 1906 | wl_event_source_timer_update(output->pageflip_timer, |
| 1907 | backend->pageflip_timeout); |
| 1908 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 1909 | drm_output_set_cursor(state); |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 1910 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1911 | /* |
| 1912 | * Now, update all the sprite surfaces |
| 1913 | */ |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1914 | wl_list_for_each(ps, &state->plane_list, link) { |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1915 | uint32_t flags = 0, fb_id = 0; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1916 | drmVBlank vbl = { |
| 1917 | .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT, |
| 1918 | .request.sequence = 1, |
| 1919 | }; |
| 1920 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1921 | p = ps->plane; |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 1922 | if (p->type != WDRM_PLANE_TYPE_OVERLAY) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1923 | continue; |
| 1924 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1925 | assert(p->state_cur->complete); |
| 1926 | assert(!!p->state_cur->output == !!p->state_cur->fb); |
| 1927 | assert(!p->state_cur->output || p->state_cur->output == output); |
| 1928 | assert(!ps->complete); |
| 1929 | assert(!ps->output || ps->output == output); |
| 1930 | assert(!!ps->output == !!ps->fb); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1931 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1932 | if (ps->fb && !backend->sprites_hidden) |
| 1933 | fb_id = ps->fb->fb_id; |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 1934 | |
| 1935 | ret = drmModeSetPlane(backend->drm.fd, p->plane_id, |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1936 | output->crtc_id, fb_id, flags, |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1937 | ps->dest_x, ps->dest_y, |
| 1938 | ps->dest_w, ps->dest_h, |
| 1939 | ps->src_x, ps->src_y, |
| 1940 | ps->src_w, ps->src_h); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1941 | if (ret) |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1942 | weston_log("setplane failed: %d: %s\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1943 | ret, strerror(errno)); |
| 1944 | |
Mario Kleiner | 2ab4f4e | 2015-06-21 21:25:13 +0200 | [diff] [blame] | 1945 | vbl.request.type |= drm_waitvblank_pipe(output); |
Rob Clark | 5ca1a47 | 2012-08-08 20:27:37 -0500 | [diff] [blame] | 1946 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1947 | /* |
| 1948 | * Queue a vblank signal so we know when the surface |
| 1949 | * becomes active on the display or has been replaced. |
| 1950 | */ |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1951 | vbl.request.signal = (unsigned long) ps; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1952 | ret = drmWaitVBlank(backend->drm.fd, &vbl); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1953 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1954 | weston_log("vblank event request failed: %d: %s\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1955 | ret, strerror(errno)); |
| 1956 | } |
| 1957 | } |
| 1958 | |
Pekka Paalanen | 02aeb5c | 2017-09-12 16:02:01 +0300 | [diff] [blame] | 1959 | if (state->dpms != output->state_cur->dpms) { |
| 1960 | wl_list_for_each(head, &output->base.head_list, base.output_link) { |
| 1961 | dpms_prop = &head->props_conn[WDRM_CONNECTOR_DPMS]; |
| 1962 | if (dpms_prop->prop_id == 0) |
| 1963 | continue; |
| 1964 | |
| 1965 | ret = drmModeConnectorSetProperty(backend->drm.fd, |
| 1966 | head->connector_id, |
| 1967 | dpms_prop->prop_id, |
| 1968 | state->dpms); |
| 1969 | if (ret) { |
| 1970 | weston_log("DRM: DPMS: failed property set for %s\n", |
| 1971 | head->base.name); |
| 1972 | } |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1973 | } |
| 1974 | } |
| 1975 | |
| 1976 | drm_output_assign_state(state, DRM_STATE_APPLY_ASYNC); |
| 1977 | |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 1978 | return 0; |
| 1979 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1980 | err: |
Kristian Høgsberg | b3955b0 | 2014-01-23 16:25:06 -0800 | [diff] [blame] | 1981 | output->cursor_view = NULL; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1982 | drm_output_state_free(state); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1983 | return -1; |
| 1984 | } |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 1985 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 1986 | #ifdef HAVE_DRM_ATOMIC |
| 1987 | static int |
| 1988 | crtc_add_prop(drmModeAtomicReq *req, struct drm_output *output, |
| 1989 | enum wdrm_crtc_property prop, uint64_t val) |
| 1990 | { |
| 1991 | struct drm_property_info *info = &output->props_crtc[prop]; |
| 1992 | int ret; |
| 1993 | |
| 1994 | if (info->prop_id == 0) |
| 1995 | return -1; |
| 1996 | |
| 1997 | ret = drmModeAtomicAddProperty(req, output->crtc_id, info->prop_id, |
| 1998 | val); |
| 1999 | return (ret <= 0) ? -1 : 0; |
| 2000 | } |
| 2001 | |
| 2002 | static int |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 2003 | connector_add_prop(drmModeAtomicReq *req, struct drm_head *head, |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2004 | enum wdrm_connector_property prop, uint64_t val) |
| 2005 | { |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 2006 | struct drm_property_info *info = &head->props_conn[prop]; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2007 | int ret; |
| 2008 | |
| 2009 | if (info->prop_id == 0) |
| 2010 | return -1; |
| 2011 | |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 2012 | ret = drmModeAtomicAddProperty(req, head->connector_id, |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2013 | info->prop_id, val); |
| 2014 | return (ret <= 0) ? -1 : 0; |
| 2015 | } |
| 2016 | |
| 2017 | static int |
| 2018 | plane_add_prop(drmModeAtomicReq *req, struct drm_plane *plane, |
| 2019 | enum wdrm_plane_property prop, uint64_t val) |
| 2020 | { |
| 2021 | struct drm_property_info *info = &plane->props[prop]; |
| 2022 | int ret; |
| 2023 | |
| 2024 | if (info->prop_id == 0) |
| 2025 | return -1; |
| 2026 | |
| 2027 | ret = drmModeAtomicAddProperty(req, plane->plane_id, info->prop_id, |
| 2028 | val); |
| 2029 | return (ret <= 0) ? -1 : 0; |
| 2030 | } |
| 2031 | |
| 2032 | static int |
| 2033 | drm_mode_ensure_blob(struct drm_backend *backend, struct drm_mode *mode) |
| 2034 | { |
| 2035 | int ret; |
| 2036 | |
| 2037 | if (mode->blob_id) |
| 2038 | return 0; |
| 2039 | |
| 2040 | ret = drmModeCreatePropertyBlob(backend->drm.fd, |
| 2041 | &mode->mode_info, |
| 2042 | sizeof(mode->mode_info), |
| 2043 | &mode->blob_id); |
| 2044 | if (ret != 0) |
| 2045 | weston_log("failed to create mode property blob: %m\n"); |
| 2046 | |
| 2047 | return ret; |
| 2048 | } |
| 2049 | |
| 2050 | static int |
| 2051 | drm_output_apply_state_atomic(struct drm_output_state *state, |
| 2052 | drmModeAtomicReq *req, |
| 2053 | uint32_t *flags) |
| 2054 | { |
| 2055 | struct drm_output *output = state->output; |
| 2056 | struct drm_backend *backend = to_drm_backend(output->base.compositor); |
| 2057 | struct drm_plane_state *plane_state; |
| 2058 | struct drm_mode *current_mode = to_drm_mode(output->base.current_mode); |
Pekka Paalanen | 2f66130 | 2017-09-12 16:07:32 +0300 | [diff] [blame] | 2059 | struct drm_head *head; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2060 | int ret = 0; |
| 2061 | |
| 2062 | if (state->dpms != output->state_cur->dpms) |
| 2063 | *flags |= DRM_MODE_ATOMIC_ALLOW_MODESET; |
| 2064 | |
| 2065 | if (state->dpms == WESTON_DPMS_ON) { |
| 2066 | ret = drm_mode_ensure_blob(backend, current_mode); |
| 2067 | if (ret != 0) |
| 2068 | return ret; |
| 2069 | |
| 2070 | ret |= crtc_add_prop(req, output, WDRM_CRTC_MODE_ID, |
| 2071 | current_mode->blob_id); |
| 2072 | ret |= crtc_add_prop(req, output, WDRM_CRTC_ACTIVE, 1); |
Pekka Paalanen | 2f66130 | 2017-09-12 16:07:32 +0300 | [diff] [blame] | 2073 | |
| 2074 | wl_list_for_each(head, &output->base.head_list, base.output_link) { |
| 2075 | ret |= connector_add_prop(req, head, WDRM_CONNECTOR_CRTC_ID, |
| 2076 | output->crtc_id); |
| 2077 | } |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2078 | } else { |
| 2079 | ret |= crtc_add_prop(req, output, WDRM_CRTC_MODE_ID, 0); |
| 2080 | ret |= crtc_add_prop(req, output, WDRM_CRTC_ACTIVE, 0); |
Pekka Paalanen | 2f66130 | 2017-09-12 16:07:32 +0300 | [diff] [blame] | 2081 | |
| 2082 | wl_list_for_each(head, &output->base.head_list, base.output_link) |
| 2083 | ret |= connector_add_prop(req, head, WDRM_CONNECTOR_CRTC_ID, 0); |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
| 2086 | if (ret != 0) { |
| 2087 | weston_log("couldn't set atomic CRTC/connector state\n"); |
| 2088 | return ret; |
| 2089 | } |
| 2090 | |
| 2091 | wl_list_for_each(plane_state, &state->plane_list, link) { |
| 2092 | struct drm_plane *plane = plane_state->plane; |
| 2093 | |
| 2094 | ret |= plane_add_prop(req, plane, WDRM_PLANE_FB_ID, |
| 2095 | plane_state->fb ? plane_state->fb->fb_id : 0); |
| 2096 | ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_ID, |
| 2097 | plane_state->fb ? output->crtc_id : 0); |
| 2098 | ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_X, |
| 2099 | plane_state->src_x); |
| 2100 | ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_Y, |
| 2101 | plane_state->src_y); |
| 2102 | ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_W, |
| 2103 | plane_state->src_w); |
| 2104 | ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_H, |
| 2105 | plane_state->src_h); |
| 2106 | ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_X, |
| 2107 | plane_state->dest_x); |
| 2108 | ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_Y, |
| 2109 | plane_state->dest_y); |
| 2110 | ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_W, |
| 2111 | plane_state->dest_w); |
| 2112 | ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_H, |
| 2113 | plane_state->dest_h); |
| 2114 | |
| 2115 | if (ret != 0) { |
| 2116 | weston_log("couldn't set plane state\n"); |
| 2117 | return ret; |
| 2118 | } |
| 2119 | } |
| 2120 | |
| 2121 | return 0; |
| 2122 | } |
| 2123 | |
| 2124 | /** |
| 2125 | * Helper function used only by drm_pending_state_apply, with the same |
| 2126 | * guarantees and constraints as that function. |
| 2127 | */ |
| 2128 | static int |
| 2129 | drm_pending_state_apply_atomic(struct drm_pending_state *pending_state, |
| 2130 | enum drm_state_apply_mode mode) |
| 2131 | { |
| 2132 | struct drm_backend *b = pending_state->backend; |
| 2133 | struct drm_output_state *output_state, *tmp; |
| 2134 | struct drm_plane *plane; |
| 2135 | drmModeAtomicReq *req = drmModeAtomicAlloc(); |
| 2136 | uint32_t flags = 0; |
| 2137 | int ret = 0; |
| 2138 | |
| 2139 | if (!req) |
| 2140 | return -1; |
| 2141 | |
| 2142 | if (b->state_invalid) { |
Pekka Paalanen | eacec81 | 2017-09-12 13:43:51 +0300 | [diff] [blame] | 2143 | struct weston_head *head_base; |
| 2144 | struct drm_head *head; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2145 | uint32_t *unused; |
| 2146 | int err; |
| 2147 | |
| 2148 | /* If we need to reset all our state (e.g. because we've |
| 2149 | * just started, or just been VT-switched in), explicitly |
| 2150 | * disable all the CRTCs and connectors we aren't using. */ |
Pekka Paalanen | eacec81 | 2017-09-12 13:43:51 +0300 | [diff] [blame] | 2151 | wl_list_for_each(head_base, |
| 2152 | &b->compositor->head_list, compositor_link) { |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2153 | struct drm_property_info *info; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2154 | |
Pekka Paalanen | eacec81 | 2017-09-12 13:43:51 +0300 | [diff] [blame] | 2155 | if (weston_head_is_enabled(head_base)) |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2156 | continue; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2157 | |
Pekka Paalanen | eacec81 | 2017-09-12 13:43:51 +0300 | [diff] [blame] | 2158 | head = to_drm_head(head_base); |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2159 | |
Pekka Paalanen | eacec81 | 2017-09-12 13:43:51 +0300 | [diff] [blame] | 2160 | info = &head->props_conn[WDRM_CONNECTOR_CRTC_ID]; |
| 2161 | err = drmModeAtomicAddProperty(req, head->connector_id, |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2162 | info->prop_id, 0); |
| 2163 | if (err <= 0) |
| 2164 | ret = -1; |
| 2165 | |
Pekka Paalanen | eacec81 | 2017-09-12 13:43:51 +0300 | [diff] [blame] | 2166 | info = &head->props_conn[WDRM_CONNECTOR_DPMS]; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2167 | if (info->prop_id > 0) |
Pekka Paalanen | eacec81 | 2017-09-12 13:43:51 +0300 | [diff] [blame] | 2168 | err = drmModeAtomicAddProperty(req, head->connector_id, |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2169 | info->prop_id, |
| 2170 | DRM_MODE_DPMS_OFF); |
| 2171 | if (err <= 0) |
| 2172 | ret = -1; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2173 | } |
| 2174 | |
| 2175 | wl_array_for_each(unused, &b->unused_crtcs) { |
| 2176 | struct drm_property_info infos[WDRM_CRTC__COUNT]; |
| 2177 | struct drm_property_info *info; |
| 2178 | drmModeObjectProperties *props; |
| 2179 | uint64_t active; |
| 2180 | |
| 2181 | memset(infos, 0, sizeof(infos)); |
| 2182 | |
| 2183 | /* We can't emit a disable on a CRTC that's already |
| 2184 | * off, as the kernel will refuse to generate an event |
| 2185 | * for an off->off state and fail the commit. |
| 2186 | */ |
| 2187 | props = drmModeObjectGetProperties(b->drm.fd, |
| 2188 | *unused, |
| 2189 | DRM_MODE_OBJECT_CRTC); |
| 2190 | if (!props) { |
| 2191 | ret = -1; |
| 2192 | continue; |
| 2193 | } |
| 2194 | |
| 2195 | drm_property_info_populate(b, crtc_props, infos, |
| 2196 | WDRM_CRTC__COUNT, |
| 2197 | props); |
| 2198 | |
| 2199 | info = &infos[WDRM_CRTC_ACTIVE]; |
| 2200 | active = drm_property_get_value(info, props, 0); |
| 2201 | drmModeFreeObjectProperties(props); |
| 2202 | if (active == 0) { |
| 2203 | drm_property_info_free(infos, WDRM_CRTC__COUNT); |
| 2204 | continue; |
| 2205 | } |
| 2206 | |
| 2207 | err = drmModeAtomicAddProperty(req, *unused, |
| 2208 | info->prop_id, 0); |
| 2209 | if (err <= 0) |
| 2210 | ret = -1; |
| 2211 | |
| 2212 | info = &infos[WDRM_CRTC_MODE_ID]; |
| 2213 | err = drmModeAtomicAddProperty(req, *unused, |
| 2214 | info->prop_id, 0); |
| 2215 | if (err <= 0) |
| 2216 | ret = -1; |
| 2217 | |
| 2218 | drm_property_info_free(infos, WDRM_CRTC__COUNT); |
| 2219 | } |
| 2220 | |
| 2221 | /* Disable all the planes; planes which are being used will |
| 2222 | * override this state in the output-state application. */ |
| 2223 | wl_list_for_each(plane, &b->plane_list, link) { |
| 2224 | plane_add_prop(req, plane, WDRM_PLANE_CRTC_ID, 0); |
| 2225 | plane_add_prop(req, plane, WDRM_PLANE_FB_ID, 0); |
| 2226 | } |
| 2227 | |
| 2228 | flags |= DRM_MODE_ATOMIC_ALLOW_MODESET; |
| 2229 | } |
| 2230 | |
| 2231 | wl_list_for_each(output_state, &pending_state->output_list, link) { |
| 2232 | if (mode == DRM_STATE_APPLY_SYNC) |
| 2233 | assert(output_state->dpms == WESTON_DPMS_OFF); |
| 2234 | ret |= drm_output_apply_state_atomic(output_state, req, &flags); |
| 2235 | } |
| 2236 | |
| 2237 | if (ret != 0) { |
| 2238 | weston_log("atomic: couldn't compile atomic state\n"); |
| 2239 | goto out; |
| 2240 | } |
| 2241 | |
| 2242 | switch (mode) { |
| 2243 | case DRM_STATE_APPLY_SYNC: |
| 2244 | break; |
| 2245 | case DRM_STATE_APPLY_ASYNC: |
| 2246 | flags |= DRM_MODE_PAGE_FLIP_EVENT | DRM_MODE_ATOMIC_NONBLOCK; |
| 2247 | break; |
| 2248 | } |
| 2249 | |
| 2250 | ret = drmModeAtomicCommit(b->drm.fd, req, flags, b); |
| 2251 | if (ret != 0) { |
| 2252 | weston_log("atomic: couldn't commit new state: %m\n"); |
| 2253 | goto out; |
| 2254 | } |
| 2255 | |
| 2256 | wl_list_for_each_safe(output_state, tmp, &pending_state->output_list, |
| 2257 | link) |
| 2258 | drm_output_assign_state(output_state, mode); |
| 2259 | |
| 2260 | b->state_invalid = false; |
| 2261 | |
| 2262 | assert(wl_list_empty(&pending_state->output_list)); |
| 2263 | |
| 2264 | out: |
| 2265 | drmModeAtomicFree(req); |
| 2266 | drm_pending_state_free(pending_state); |
| 2267 | return ret; |
| 2268 | } |
| 2269 | #endif |
| 2270 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2271 | /** |
| 2272 | * Applies all of a pending_state asynchronously: the primary entry point for |
| 2273 | * applying KMS state to a device. Updates the state for all outputs in the |
| 2274 | * pending_state, as well as disabling any unclaimed outputs. |
| 2275 | * |
| 2276 | * Unconditionally takes ownership of pending_state, and clears state_invalid. |
| 2277 | */ |
| 2278 | static int |
| 2279 | drm_pending_state_apply(struct drm_pending_state *pending_state) |
| 2280 | { |
| 2281 | struct drm_backend *b = pending_state->backend; |
| 2282 | struct drm_output_state *output_state, *tmp; |
| 2283 | uint32_t *unused; |
| 2284 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2285 | #ifdef HAVE_DRM_ATOMIC |
| 2286 | if (b->atomic_modeset) |
| 2287 | return drm_pending_state_apply_atomic(pending_state, |
| 2288 | DRM_STATE_APPLY_ASYNC); |
| 2289 | #endif |
| 2290 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2291 | if (b->state_invalid) { |
| 2292 | /* If we need to reset all our state (e.g. because we've |
| 2293 | * just started, or just been VT-switched in), explicitly |
| 2294 | * disable all the CRTCs we aren't using. This also disables |
| 2295 | * all connectors on these CRTCs, so we don't need to do that |
| 2296 | * separately with the pre-atomic API. */ |
| 2297 | wl_array_for_each(unused, &b->unused_crtcs) |
| 2298 | drmModeSetCrtc(b->drm.fd, *unused, 0, 0, 0, NULL, 0, |
| 2299 | NULL); |
| 2300 | } |
| 2301 | |
| 2302 | wl_list_for_each_safe(output_state, tmp, &pending_state->output_list, |
| 2303 | link) { |
| 2304 | struct drm_output *output = output_state->output; |
| 2305 | int ret; |
| 2306 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2307 | ret = drm_output_apply_state_legacy(output_state); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2308 | if (ret != 0) { |
| 2309 | weston_log("Couldn't apply state for output %s\n", |
| 2310 | output->base.name); |
| 2311 | } |
| 2312 | } |
| 2313 | |
| 2314 | b->state_invalid = false; |
| 2315 | |
| 2316 | assert(wl_list_empty(&pending_state->output_list)); |
| 2317 | |
| 2318 | drm_pending_state_free(pending_state); |
| 2319 | |
| 2320 | return 0; |
| 2321 | } |
| 2322 | |
| 2323 | /** |
| 2324 | * The synchronous version of drm_pending_state_apply. May only be used to |
| 2325 | * disable outputs. Does so synchronously: the request is guaranteed to have |
| 2326 | * completed on return, and the output will not be touched afterwards. |
| 2327 | * |
| 2328 | * Unconditionally takes ownership of pending_state, and clears state_invalid. |
| 2329 | */ |
| 2330 | static int |
| 2331 | drm_pending_state_apply_sync(struct drm_pending_state *pending_state) |
| 2332 | { |
| 2333 | struct drm_backend *b = pending_state->backend; |
| 2334 | struct drm_output_state *output_state, *tmp; |
| 2335 | uint32_t *unused; |
| 2336 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2337 | #ifdef HAVE_DRM_ATOMIC |
| 2338 | if (b->atomic_modeset) |
| 2339 | return drm_pending_state_apply_atomic(pending_state, |
| 2340 | DRM_STATE_APPLY_SYNC); |
| 2341 | #endif |
| 2342 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2343 | if (b->state_invalid) { |
| 2344 | /* If we need to reset all our state (e.g. because we've |
| 2345 | * just started, or just been VT-switched in), explicitly |
| 2346 | * disable all the CRTCs we aren't using. This also disables |
| 2347 | * all connectors on these CRTCs, so we don't need to do that |
| 2348 | * separately with the pre-atomic API. */ |
| 2349 | wl_array_for_each(unused, &b->unused_crtcs) |
| 2350 | drmModeSetCrtc(b->drm.fd, *unused, 0, 0, 0, NULL, 0, |
| 2351 | NULL); |
| 2352 | } |
| 2353 | |
| 2354 | wl_list_for_each_safe(output_state, tmp, &pending_state->output_list, |
| 2355 | link) { |
| 2356 | int ret; |
| 2357 | |
| 2358 | assert(output_state->dpms == WESTON_DPMS_OFF); |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2359 | ret = drm_output_apply_state_legacy(output_state); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2360 | if (ret != 0) { |
| 2361 | weston_log("Couldn't apply state for output %s\n", |
| 2362 | output_state->output->base.name); |
| 2363 | } |
| 2364 | } |
| 2365 | |
| 2366 | b->state_invalid = false; |
| 2367 | |
| 2368 | assert(wl_list_empty(&pending_state->output_list)); |
| 2369 | |
| 2370 | drm_pending_state_free(pending_state); |
| 2371 | |
| 2372 | return 0; |
| 2373 | } |
| 2374 | |
| 2375 | static int |
| 2376 | drm_output_repaint(struct weston_output *output_base, |
| 2377 | pixman_region32_t *damage, |
| 2378 | void *repaint_data) |
| 2379 | { |
| 2380 | struct drm_pending_state *pending_state = repaint_data; |
| 2381 | struct drm_output *output = to_drm_output(output_base); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2382 | struct drm_output_state *state = NULL; |
| 2383 | struct drm_plane_state *scanout_state; |
| 2384 | |
| 2385 | if (output->disable_pending || output->destroy_pending) |
| 2386 | goto err; |
| 2387 | |
| 2388 | assert(!output->state_last); |
| 2389 | |
| 2390 | /* If planes have been disabled in the core, we might not have |
| 2391 | * hit assign_planes at all, so might not have valid output state |
| 2392 | * here. */ |
| 2393 | state = drm_pending_state_get_output(pending_state, output); |
| 2394 | if (!state) |
| 2395 | state = drm_output_state_duplicate(output->state_cur, |
| 2396 | pending_state, |
| 2397 | DRM_OUTPUT_STATE_CLEAR_PLANES); |
| 2398 | state->dpms = WESTON_DPMS_ON; |
| 2399 | |
| 2400 | drm_output_render(state, damage); |
| 2401 | scanout_state = drm_output_state_get_plane(state, |
| 2402 | output->scanout_plane); |
| 2403 | if (!scanout_state || !scanout_state->fb) |
| 2404 | goto err; |
| 2405 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2406 | return 0; |
| 2407 | |
| 2408 | err: |
| 2409 | drm_output_state_free(state); |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 2410 | return -1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2411 | } |
| 2412 | |
| 2413 | static void |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2414 | drm_output_start_repaint_loop(struct weston_output *output_base) |
| 2415 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2416 | struct drm_output *output = to_drm_output(output_base); |
Daniel Stone | 8747f95 | 2016-11-29 20:17:32 +0000 | [diff] [blame] | 2417 | struct drm_pending_state *pending_state; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 2418 | struct drm_plane *scanout_plane = output->scanout_plane; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2419 | struct drm_backend *backend = |
| 2420 | to_drm_backend(output_base->compositor); |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 2421 | struct timespec ts, tnow; |
| 2422 | struct timespec vbl2now; |
| 2423 | int64_t refresh_nsec; |
| 2424 | int ret; |
| 2425 | drmVBlank vbl = { |
| 2426 | .request.type = DRM_VBLANK_RELATIVE, |
| 2427 | .request.sequence = 0, |
| 2428 | .request.signal = 0, |
| 2429 | }; |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 2430 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2431 | if (output->disable_pending || output->destroy_pending) |
Xiong Zhang | abd5d47 | 2013-10-11 14:43:07 +0800 | [diff] [blame] | 2432 | return; |
| 2433 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 2434 | if (!output->scanout_plane->state_cur->fb) { |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 2435 | /* We can't page flip if there's no mode set */ |
David Herrmann | 3c688c5 | 2013-10-22 17:11:25 +0200 | [diff] [blame] | 2436 | goto finish_frame; |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 2437 | } |
| 2438 | |
Pekka Paalanen | 6b65d8f | 2017-07-27 13:44:32 +0300 | [diff] [blame] | 2439 | /* Need to smash all state in from scratch; current timings might not |
| 2440 | * be what we want, page flip might not work, etc. |
| 2441 | */ |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 2442 | if (backend->state_invalid) |
Pekka Paalanen | 6b65d8f | 2017-07-27 13:44:32 +0300 | [diff] [blame] | 2443 | goto finish_frame; |
| 2444 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 2445 | assert(scanout_plane->state_cur->output == output); |
| 2446 | |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 2447 | /* Try to get current msc and timestamp via instant query */ |
| 2448 | vbl.request.type |= drm_waitvblank_pipe(output); |
| 2449 | ret = drmWaitVBlank(backend->drm.fd, &vbl); |
| 2450 | |
| 2451 | /* Error ret or zero timestamp means failure to get valid timestamp */ |
| 2452 | if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) { |
| 2453 | ts.tv_sec = vbl.reply.tval_sec; |
| 2454 | ts.tv_nsec = vbl.reply.tval_usec * 1000; |
| 2455 | |
| 2456 | /* Valid timestamp for most recent vblank - not stale? |
| 2457 | * Stale ts could happen on Linux 3.17+, so make sure it |
| 2458 | * is not older than 1 refresh duration since now. |
| 2459 | */ |
| 2460 | weston_compositor_read_presentation_clock(backend->compositor, |
| 2461 | &tnow); |
| 2462 | timespec_sub(&vbl2now, &tnow, &ts); |
| 2463 | refresh_nsec = |
| 2464 | millihz_to_nsec(output->base.current_mode->refresh); |
| 2465 | if (timespec_to_nsec(&vbl2now) < refresh_nsec) { |
| 2466 | drm_output_update_msc(output, vbl.reply.sequence); |
| 2467 | weston_output_finish_frame(output_base, &ts, |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 2468 | WP_PRESENTATION_FEEDBACK_INVALID); |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 2469 | return; |
| 2470 | } |
| 2471 | } |
| 2472 | |
| 2473 | /* Immediate query didn't provide valid timestamp. |
| 2474 | * Use pageflip fallback. |
| 2475 | */ |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2476 | |
Daniel Stone | 205c0a0 | 2017-04-04 17:54:33 +0100 | [diff] [blame] | 2477 | assert(!output->page_flip_pending); |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2478 | assert(!output->state_last); |
| 2479 | |
| 2480 | pending_state = drm_pending_state_alloc(backend); |
Daniel Stone | 8747f95 | 2016-11-29 20:17:32 +0000 | [diff] [blame] | 2481 | drm_output_state_duplicate(output->state_cur, pending_state, |
| 2482 | DRM_OUTPUT_STATE_PRESERVE_PLANES); |
Daniel Stone | 205c0a0 | 2017-04-04 17:54:33 +0100 | [diff] [blame] | 2483 | |
Daniel Stone | 8747f95 | 2016-11-29 20:17:32 +0000 | [diff] [blame] | 2484 | ret = drm_pending_state_apply(pending_state); |
| 2485 | if (ret != 0) { |
| 2486 | weston_log("applying repaint-start state failed: %m\n"); |
David Herrmann | 3c688c5 | 2013-10-22 17:11:25 +0200 | [diff] [blame] | 2487 | goto finish_frame; |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2488 | } |
David Herrmann | 3c688c5 | 2013-10-22 17:11:25 +0200 | [diff] [blame] | 2489 | |
| 2490 | return; |
| 2491 | |
| 2492 | finish_frame: |
| 2493 | /* if we cannot page-flip, immediately finish frame */ |
Daniel Stone | 3615ce1 | 2017-03-01 11:34:05 +0000 | [diff] [blame] | 2494 | weston_output_finish_frame(output_base, NULL, |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 2495 | WP_PRESENTATION_FEEDBACK_INVALID); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2496 | } |
| 2497 | |
| 2498 | static void |
Pekka Paalanen | 641307c | 2014-09-23 22:08:47 -0400 | [diff] [blame] | 2499 | drm_output_update_msc(struct drm_output *output, unsigned int seq) |
| 2500 | { |
| 2501 | uint64_t msc_hi = output->base.msc >> 32; |
| 2502 | |
| 2503 | if (seq < (output->base.msc & 0xffffffff)) |
| 2504 | msc_hi++; |
| 2505 | |
| 2506 | output->base.msc = (msc_hi << 32) + seq; |
| 2507 | } |
| 2508 | |
| 2509 | static void |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2510 | vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec, |
| 2511 | void *data) |
| 2512 | { |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2513 | struct drm_plane_state *ps = (struct drm_plane_state *) data; |
| 2514 | struct drm_output_state *os = ps->output_state; |
| 2515 | struct drm_output *output = os->output; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2516 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 2517 | uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION | |
| 2518 | WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK; |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 2519 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2520 | assert(!b->atomic_modeset); |
| 2521 | |
Pekka Paalanen | 641307c | 2014-09-23 22:08:47 -0400 | [diff] [blame] | 2522 | drm_output_update_msc(output, frame); |
Daniel Stone | 65d87d0 | 2017-04-04 17:54:32 +0100 | [diff] [blame] | 2523 | output->vblank_pending--; |
| 2524 | assert(output->vblank_pending >= 0); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2525 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2526 | assert(ps->fb); |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 2527 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2528 | if (output->page_flip_pending || output->vblank_pending) |
| 2529 | return; |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 2530 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2531 | drm_output_update_complete(output, flags, sec, usec); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2532 | } |
| 2533 | |
| 2534 | static void |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2535 | page_flip_handler(int fd, unsigned int frame, |
| 2536 | unsigned int sec, unsigned int usec, void *data) |
| 2537 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2538 | struct drm_output *output = data; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2539 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 2540 | uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC | |
| 2541 | WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION | |
| 2542 | WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2543 | |
Pekka Paalanen | 641307c | 2014-09-23 22:08:47 -0400 | [diff] [blame] | 2544 | drm_output_update_msc(output, frame); |
| 2545 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2546 | assert(!b->atomic_modeset); |
Daniel Stone | 205c0a0 | 2017-04-04 17:54:33 +0100 | [diff] [blame] | 2547 | assert(output->page_flip_pending); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2548 | output->page_flip_pending = 0; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 2549 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2550 | if (output->vblank_pending) |
| 2551 | return; |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 2552 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2553 | drm_output_update_complete(output, flags, sec, usec); |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 2554 | } |
| 2555 | |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 2556 | /** |
| 2557 | * Begin a new repaint cycle |
| 2558 | * |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2559 | * Called by the core compositor at the beginning of a repaint cycle. Creates |
| 2560 | * a new pending_state structure to own any output state created by individual |
| 2561 | * output repaint functions until the repaint is flushed or cancelled. |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 2562 | */ |
| 2563 | static void * |
| 2564 | drm_repaint_begin(struct weston_compositor *compositor) |
| 2565 | { |
| 2566 | struct drm_backend *b = to_drm_backend(compositor); |
| 2567 | struct drm_pending_state *ret; |
| 2568 | |
| 2569 | ret = drm_pending_state_alloc(b); |
| 2570 | b->repaint_data = ret; |
| 2571 | |
| 2572 | return ret; |
| 2573 | } |
| 2574 | |
| 2575 | /** |
| 2576 | * Flush a repaint set |
| 2577 | * |
| 2578 | * Called by the core compositor when a repaint cycle has been completed |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2579 | * and should be flushed. Frees the pending state, transitioning ownership |
| 2580 | * of the output state from the pending state, to the update itself. When |
| 2581 | * the update completes (see drm_output_update_complete), the output |
| 2582 | * state will be freed. |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 2583 | */ |
| 2584 | static void |
| 2585 | drm_repaint_flush(struct weston_compositor *compositor, void *repaint_data) |
| 2586 | { |
| 2587 | struct drm_backend *b = to_drm_backend(compositor); |
| 2588 | struct drm_pending_state *pending_state = repaint_data; |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 2589 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2590 | drm_pending_state_apply(pending_state); |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 2591 | b->repaint_data = NULL; |
| 2592 | } |
| 2593 | |
| 2594 | /** |
| 2595 | * Cancel a repaint set |
| 2596 | * |
| 2597 | * Called by the core compositor when a repaint has finished, so the data |
| 2598 | * held across the repaint cycle should be discarded. |
| 2599 | */ |
| 2600 | static void |
| 2601 | drm_repaint_cancel(struct weston_compositor *compositor, void *repaint_data) |
| 2602 | { |
| 2603 | struct drm_backend *b = to_drm_backend(compositor); |
| 2604 | struct drm_pending_state *pending_state = repaint_data; |
| 2605 | |
| 2606 | drm_pending_state_free(pending_state); |
| 2607 | b->repaint_data = NULL; |
| 2608 | } |
| 2609 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2610 | #ifdef HAVE_DRM_ATOMIC |
| 2611 | static void |
| 2612 | atomic_flip_handler(int fd, unsigned int frame, unsigned int sec, |
| 2613 | unsigned int usec, unsigned int crtc_id, void *data) |
| 2614 | { |
| 2615 | struct drm_backend *b = data; |
| 2616 | struct drm_output *output = drm_output_find_by_crtc(b, crtc_id); |
| 2617 | uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC | |
| 2618 | WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION | |
| 2619 | WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK; |
| 2620 | |
| 2621 | /* During the initial modeset, we can disable CRTCs which we don't |
| 2622 | * actually handle during normal operation; this will give us events |
| 2623 | * for unknown outputs. Ignore them. */ |
| 2624 | if (!output || !output->base.enabled) |
| 2625 | return; |
| 2626 | |
| 2627 | drm_output_update_msc(output, frame); |
| 2628 | |
| 2629 | assert(b->atomic_modeset); |
| 2630 | assert(output->atomic_complete_pending); |
| 2631 | output->atomic_complete_pending = 0; |
| 2632 | |
| 2633 | drm_output_update_complete(output, flags, sec, usec); |
| 2634 | } |
| 2635 | #endif |
| 2636 | |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 2637 | static uint32_t |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2638 | drm_output_check_plane_format(struct drm_plane *p, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2639 | struct weston_view *ev, struct gbm_bo *bo) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2640 | { |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 2641 | uint32_t i, format; |
| 2642 | |
| 2643 | format = gbm_bo_get_format(bo); |
| 2644 | |
| 2645 | if (format == GBM_FORMAT_ARGB8888) { |
| 2646 | pixman_region32_t r; |
| 2647 | |
Kristian Høgsberg | 63093a3 | 2013-03-01 14:29:16 -0500 | [diff] [blame] | 2648 | pixman_region32_init_rect(&r, 0, 0, |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 2649 | ev->surface->width, |
| 2650 | ev->surface->height); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2651 | pixman_region32_subtract(&r, &r, &ev->surface->opaque); |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 2652 | |
| 2653 | if (!pixman_region32_not_empty(&r)) |
| 2654 | format = GBM_FORMAT_XRGB8888; |
| 2655 | |
| 2656 | pixman_region32_fini(&r); |
| 2657 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2658 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2659 | for (i = 0; i < p->count_formats; i++) |
| 2660 | if (p->formats[i] == format) |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 2661 | return format; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2662 | |
| 2663 | return 0; |
| 2664 | } |
| 2665 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 2666 | static struct weston_plane * |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2667 | drm_output_prepare_overlay_view(struct drm_output_state *output_state, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2668 | struct weston_view *ev) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2669 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2670 | struct drm_output *output = output_state->output; |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 2671 | struct weston_compositor *ec = output->base.compositor; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2672 | struct drm_backend *b = to_drm_backend(ec); |
Pekka Paalanen | 952b6c8 | 2014-03-14 14:38:15 +0200 | [diff] [blame] | 2673 | struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport; |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 2674 | struct wl_resource *buffer_resource; |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2675 | struct drm_plane *p; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2676 | struct drm_plane_state *state = NULL; |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 2677 | struct linux_dmabuf_buffer *dmabuf; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2678 | struct gbm_bo *bo; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2679 | pixman_region32_t dest_rect, src_rect; |
Ander Conselvan de Oliveira | 409eebf | 2012-12-05 15:14:04 +0200 | [diff] [blame] | 2680 | pixman_box32_t *box, tbox; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2681 | uint32_t format; |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 2682 | wl_fixed_t sx1, sy1, sx2, sy2; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2683 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2684 | if (b->sprites_are_broken) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 2685 | return NULL; |
Kristian Høgsberg | 65bec24 | 2012-03-05 19:57:35 -0500 | [diff] [blame] | 2686 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 2687 | /* Don't import buffers which span multiple outputs. */ |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 2688 | if (ev->output_mask != (1u << output->base.id)) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 2689 | return NULL; |
Ander Conselvan de Oliveira | d450b19 | 2012-06-26 17:09:12 +0300 | [diff] [blame] | 2690 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 2691 | /* We can only import GBM buffers. */ |
| 2692 | if (b->gbm == NULL) |
| 2693 | return NULL; |
| 2694 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2695 | if (ev->surface->buffer_ref.buffer == NULL) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 2696 | return NULL; |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 2697 | buffer_resource = ev->surface->buffer_ref.buffer->resource; |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 2698 | if (wl_shm_buffer_get(buffer_resource)) |
Rob Clark | 702ffae | 2012-08-09 14:18:27 -0500 | [diff] [blame] | 2699 | return NULL; |
| 2700 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 2701 | if (viewport->buffer.transform != output->base.transform) |
| 2702 | return NULL; |
| 2703 | if (viewport->buffer.scale != output->base.current_scale) |
| 2704 | return NULL; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2705 | if (!drm_view_transform_supported(ev)) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 2706 | return NULL; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2707 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 2708 | if (ev->alpha != 1.0f) |
| 2709 | return NULL; |
| 2710 | |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 2711 | wl_list_for_each(p, &b->plane_list, link) { |
| 2712 | if (p->type != WDRM_PLANE_TYPE_OVERLAY) |
| 2713 | continue; |
| 2714 | |
Daniel Stone | 5ff289a | 2017-10-07 12:59:02 +0100 | [diff] [blame] | 2715 | if (!drm_plane_is_available(p, output)) |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2716 | continue; |
| 2717 | |
| 2718 | state = drm_output_state_get_plane(output_state, p); |
| 2719 | if (state->fb) { |
| 2720 | state = NULL; |
| 2721 | continue; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2722 | } |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2723 | |
| 2724 | break; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2725 | } |
| 2726 | |
| 2727 | /* No sprites available */ |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2728 | if (!state) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 2729 | return NULL; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2730 | |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 2731 | if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) { |
Bryce Harrington | a358207 | 2015-08-14 12:23:13 -0700 | [diff] [blame] | 2732 | #ifdef HAVE_GBM_FD_IMPORT |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 2733 | /* XXX: TODO: |
| 2734 | * |
| 2735 | * Use AddFB2 directly, do not go via GBM. |
| 2736 | * Add support for multiplanar formats. |
| 2737 | * Both require refactoring in the DRM-backend to |
| 2738 | * support a mix of gbm_bos and drmfbs. |
| 2739 | */ |
| 2740 | struct gbm_import_fd_data gbm_dmabuf = { |
Emmanuel Gil Peyrot | c399692 | 2015-11-24 19:28:24 +0000 | [diff] [blame] | 2741 | .fd = dmabuf->attributes.fd[0], |
| 2742 | .width = dmabuf->attributes.width, |
| 2743 | .height = dmabuf->attributes.height, |
| 2744 | .stride = dmabuf->attributes.stride[0], |
| 2745 | .format = dmabuf->attributes.format |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 2746 | }; |
| 2747 | |
Micah Fedke | c889012 | 2017-02-01 15:28:23 -0500 | [diff] [blame] | 2748 | /* XXX: TODO: |
| 2749 | * |
| 2750 | * Currently the buffer is rejected if any dmabuf attribute |
| 2751 | * flag is set. This keeps us from passing an inverted / |
| 2752 | * interlaced / bottom-first buffer (or any other type that may |
| 2753 | * be added in the future) through to an overlay. Ultimately, |
| 2754 | * these types of buffers should be handled through buffer |
| 2755 | * transforms and not as spot-checks requiring specific |
| 2756 | * knowledge. */ |
| 2757 | if (dmabuf->attributes.n_planes != 1 || |
| 2758 | dmabuf->attributes.offset[0] != 0 || |
| 2759 | dmabuf->attributes.flags) |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 2760 | return NULL; |
| 2761 | |
| 2762 | bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf, |
| 2763 | GBM_BO_USE_SCANOUT); |
Bryce Harrington | a358207 | 2015-08-14 12:23:13 -0700 | [diff] [blame] | 2764 | #else |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2765 | goto err; |
Bryce Harrington | a358207 | 2015-08-14 12:23:13 -0700 | [diff] [blame] | 2766 | #endif |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 2767 | } else { |
| 2768 | bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER, |
| 2769 | buffer_resource, GBM_BO_USE_SCANOUT); |
| 2770 | } |
Kristian Høgsberg | 2763a2e | 2012-07-13 22:54:43 -0400 | [diff] [blame] | 2771 | if (!bo) |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2772 | goto err; |
Kristian Høgsberg | 2763a2e | 2012-07-13 22:54:43 -0400 | [diff] [blame] | 2773 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2774 | format = drm_output_check_plane_format(p, ev, bo); |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2775 | if (format == 0) |
| 2776 | goto err; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2777 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2778 | state->fb = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT); |
| 2779 | if (!state->fb) |
| 2780 | goto err; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 2781 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2782 | drm_fb_set_buffer(state->fb, ev->surface->buffer_ref.buffer); |
| 2783 | |
| 2784 | state->output = output; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2785 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2786 | box = pixman_region32_extents(&ev->transform.boundingbox); |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2787 | p->base.x = box->x1; |
| 2788 | p->base.y = box->y1; |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 2789 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2790 | /* |
| 2791 | * Calculate the source & dest rects properly based on actual |
Derek Foreman | 4b1a0a1 | 2014-09-10 15:37:33 -0500 | [diff] [blame] | 2792 | * position (note the caller has called weston_view_update_transform() |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2793 | * for us already). |
| 2794 | */ |
| 2795 | pixman_region32_init(&dest_rect); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2796 | pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox, |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 2797 | &output->base.region); |
| 2798 | pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2799 | box = pixman_region32_extents(&dest_rect); |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 2800 | tbox = weston_transformed_rect(output->base.width, |
| 2801 | output->base.height, |
| 2802 | output->base.transform, |
| 2803 | output->base.current_scale, |
Alexander Larsson | d9a7bb7 | 2013-05-22 14:41:39 +0200 | [diff] [blame] | 2804 | *box); |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2805 | state->dest_x = tbox.x1; |
| 2806 | state->dest_y = tbox.y1; |
| 2807 | state->dest_w = tbox.x2 - tbox.x1; |
| 2808 | state->dest_h = tbox.y2 - tbox.y1; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2809 | pixman_region32_fini(&dest_rect); |
| 2810 | |
| 2811 | pixman_region32_init(&src_rect); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2812 | pixman_region32_intersect(&src_rect, &ev->transform.boundingbox, |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 2813 | &output->base.region); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2814 | box = pixman_region32_extents(&src_rect); |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 2815 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2816 | weston_view_from_global_fixed(ev, |
| 2817 | wl_fixed_from_int(box->x1), |
| 2818 | wl_fixed_from_int(box->y1), |
| 2819 | &sx1, &sy1); |
| 2820 | weston_view_from_global_fixed(ev, |
| 2821 | wl_fixed_from_int(box->x2), |
| 2822 | wl_fixed_from_int(box->y2), |
| 2823 | &sx2, &sy2); |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 2824 | |
| 2825 | if (sx1 < 0) |
| 2826 | sx1 = 0; |
| 2827 | if (sy1 < 0) |
| 2828 | sy1 = 0; |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 2829 | if (sx2 > wl_fixed_from_int(ev->surface->width)) |
| 2830 | sx2 = wl_fixed_from_int(ev->surface->width); |
| 2831 | if (sy2 > wl_fixed_from_int(ev->surface->height)) |
| 2832 | sy2 = wl_fixed_from_int(ev->surface->height); |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 2833 | |
Ander Conselvan de Oliveira | 409eebf | 2012-12-05 15:14:04 +0200 | [diff] [blame] | 2834 | tbox.x1 = sx1; |
| 2835 | tbox.y1 = sy1; |
| 2836 | tbox.x2 = sx2; |
| 2837 | tbox.y2 = sy2; |
| 2838 | |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 2839 | tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width), |
| 2840 | wl_fixed_from_int(ev->surface->height), |
Pekka Paalanen | 952b6c8 | 2014-03-14 14:38:15 +0200 | [diff] [blame] | 2841 | viewport->buffer.transform, |
| 2842 | viewport->buffer.scale, |
Pekka Paalanen | 1fd9c0f | 2013-11-26 18:19:41 +0100 | [diff] [blame] | 2843 | tbox); |
Ander Conselvan de Oliveira | 409eebf | 2012-12-05 15:14:04 +0200 | [diff] [blame] | 2844 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2845 | state->src_x = tbox.x1 << 8; |
| 2846 | state->src_y = tbox.y1 << 8; |
| 2847 | state->src_w = (tbox.x2 - tbox.x1) << 8; |
| 2848 | state->src_h = (tbox.y2 - tbox.y1) << 8; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2849 | pixman_region32_fini(&src_rect); |
| 2850 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2851 | return &p->base; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2852 | |
| 2853 | err: |
| 2854 | drm_plane_state_put_back(state); |
| 2855 | if (bo) |
| 2856 | gbm_bo_destroy(bo); |
| 2857 | return NULL; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2858 | } |
| 2859 | |
Pekka Paalanen | d0ead48 | 2014-06-16 12:05:40 +0300 | [diff] [blame] | 2860 | /** |
| 2861 | * Update the image for the current cursor surface |
| 2862 | * |
| 2863 | * @param b DRM backend structure |
| 2864 | * @param bo GBM buffer object to write into |
| 2865 | * @param ev View to use for cursor image |
| 2866 | */ |
| 2867 | static void |
| 2868 | cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo, |
| 2869 | struct weston_view *ev) |
| 2870 | { |
| 2871 | struct weston_buffer *buffer = ev->surface->buffer_ref.buffer; |
| 2872 | uint32_t buf[b->cursor_width * b->cursor_height]; |
| 2873 | int32_t stride; |
| 2874 | uint8_t *s; |
| 2875 | int i; |
| 2876 | |
| 2877 | assert(buffer && buffer->shm_buffer); |
| 2878 | assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource)); |
| 2879 | assert(ev->surface->width <= b->cursor_width); |
| 2880 | assert(ev->surface->height <= b->cursor_height); |
| 2881 | |
| 2882 | memset(buf, 0, sizeof buf); |
| 2883 | stride = wl_shm_buffer_get_stride(buffer->shm_buffer); |
| 2884 | s = wl_shm_buffer_get_data(buffer->shm_buffer); |
| 2885 | |
| 2886 | wl_shm_buffer_begin_access(buffer->shm_buffer); |
| 2887 | for (i = 0; i < ev->surface->height; i++) |
| 2888 | memcpy(buf + i * b->cursor_width, |
| 2889 | s + i * stride, |
| 2890 | ev->surface->width * 4); |
| 2891 | wl_shm_buffer_end_access(buffer->shm_buffer); |
| 2892 | |
| 2893 | if (gbm_bo_write(bo, buf, sizeof buf) < 0) |
| 2894 | weston_log("failed update cursor: %m\n"); |
| 2895 | } |
| 2896 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 2897 | static struct weston_plane * |
| 2898 | drm_output_prepare_cursor_view(struct drm_output_state *output_state, |
| 2899 | struct weston_view *ev) |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 2900 | { |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 2901 | struct drm_output *output = output_state->output; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2902 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 2903 | struct drm_plane *plane = output->cursor_plane; |
| 2904 | struct drm_plane_state *plane_state; |
| 2905 | struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport; |
| 2906 | struct wl_shm_buffer *shmbuf; |
| 2907 | bool needs_update = false; |
Derek Foreman | be428b3 | 2015-11-24 11:39:38 -0600 | [diff] [blame] | 2908 | float x, y; |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 2909 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 2910 | if (!plane) |
| 2911 | return NULL; |
| 2912 | |
| 2913 | if (b->cursors_are_broken) |
| 2914 | return NULL; |
| 2915 | |
| 2916 | if (!plane->state_cur->complete) |
| 2917 | return NULL; |
| 2918 | |
| 2919 | if (plane->state_cur->output && plane->state_cur->output != output) |
| 2920 | return NULL; |
| 2921 | |
| 2922 | /* Don't import buffers which span multiple outputs. */ |
| 2923 | if (ev->output_mask != (1u << output->base.id)) |
| 2924 | return NULL; |
| 2925 | |
| 2926 | /* We use GBM to import SHM buffers. */ |
| 2927 | if (b->gbm == NULL) |
| 2928 | return NULL; |
| 2929 | |
| 2930 | if (ev->surface->buffer_ref.buffer == NULL) |
| 2931 | return NULL; |
| 2932 | shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource); |
| 2933 | if (!shmbuf) |
| 2934 | return NULL; |
| 2935 | if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888) |
| 2936 | return NULL; |
| 2937 | |
| 2938 | if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL) |
| 2939 | return NULL; |
| 2940 | if (ev->transform.enabled && |
| 2941 | (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE)) |
| 2942 | return NULL; |
| 2943 | if (viewport->buffer.scale != output->base.current_scale) |
| 2944 | return NULL; |
| 2945 | if (ev->geometry.scissor_enabled) |
| 2946 | return NULL; |
| 2947 | |
| 2948 | if (ev->surface->width > b->cursor_width || |
| 2949 | ev->surface->height > b->cursor_height) |
| 2950 | return NULL; |
| 2951 | |
| 2952 | plane_state = |
| 2953 | drm_output_state_get_plane(output_state, output->cursor_plane); |
| 2954 | |
| 2955 | if (plane_state && plane_state->fb) |
| 2956 | return NULL; |
| 2957 | |
| 2958 | /* Since we're setting plane state up front, we need to work out |
| 2959 | * whether or not we need to upload a new cursor. We can't use the |
| 2960 | * plane damage, since the planes haven't actually been calculated |
| 2961 | * yet: instead try to figure it out directly. KMS cursor planes are |
| 2962 | * pretty unique here, in that they lie partway between a Weston plane |
| 2963 | * (direct scanout) and a renderer. */ |
| 2964 | if (ev != output->cursor_view || |
| 2965 | pixman_region32_not_empty(&ev->surface->damage)) { |
| 2966 | output->current_cursor++; |
| 2967 | output->current_cursor = |
| 2968 | output->current_cursor % |
| 2969 | ARRAY_LENGTH(output->gbm_cursor_fb); |
| 2970 | needs_update = true; |
| 2971 | } |
| 2972 | |
| 2973 | output->cursor_view = ev; |
| 2974 | weston_view_to_global_float(ev, 0, 0, &x, &y); |
| 2975 | plane->base.x = x; |
| 2976 | plane->base.y = y; |
| 2977 | |
| 2978 | plane_state->fb = |
| 2979 | drm_fb_ref(output->gbm_cursor_fb[output->current_cursor]); |
| 2980 | plane_state->output = output; |
| 2981 | plane_state->src_x = 0; |
| 2982 | plane_state->src_y = 0; |
| 2983 | plane_state->src_w = b->cursor_width << 16; |
| 2984 | plane_state->src_h = b->cursor_height << 16; |
| 2985 | plane_state->dest_x = (x - output->base.x) * output->base.current_scale; |
| 2986 | plane_state->dest_y = (y - output->base.y) * output->base.current_scale; |
| 2987 | plane_state->dest_w = b->cursor_width; |
| 2988 | plane_state->dest_h = b->cursor_height; |
| 2989 | |
| 2990 | if (needs_update) |
| 2991 | cursor_bo_update(b, plane_state->fb->bo, ev); |
| 2992 | |
| 2993 | return &plane->base; |
| 2994 | } |
| 2995 | |
| 2996 | static void |
| 2997 | drm_output_set_cursor(struct drm_output_state *output_state) |
| 2998 | { |
| 2999 | struct drm_output *output = output_state->output; |
| 3000 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
| 3001 | struct drm_plane *plane = output->cursor_plane; |
| 3002 | struct drm_plane_state *state; |
| 3003 | EGLint handle; |
| 3004 | struct gbm_bo *bo; |
| 3005 | |
| 3006 | if (!plane) |
| 3007 | return; |
| 3008 | |
| 3009 | state = drm_output_state_get_existing_plane(output_state, plane); |
| 3010 | if (!state) |
| 3011 | return; |
| 3012 | |
| 3013 | if (!state->fb) { |
| 3014 | pixman_region32_fini(&plane->base.damage); |
| 3015 | pixman_region32_init(&plane->base.damage); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3016 | drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0); |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 3017 | return; |
| 3018 | } |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 3019 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3020 | assert(state->fb == output->gbm_cursor_fb[output->current_cursor]); |
| 3021 | assert(!plane->state_cur->output || plane->state_cur->output == output); |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 3022 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3023 | if (plane->state_cur->fb != state->fb) { |
| 3024 | bo = state->fb->bo; |
Kristian Høgsberg | 1f5de35 | 2012-07-18 12:09:58 -0400 | [diff] [blame] | 3025 | handle = gbm_bo_get_handle(bo).s32; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3026 | if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle, |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3027 | b->cursor_width, b->cursor_height)) { |
Pekka Paalanen | ae29da2 | 2012-08-06 14:57:05 +0300 | [diff] [blame] | 3028 | weston_log("failed to set cursor: %m\n"); |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3029 | goto err; |
Rob Clark | ab5b1e3 | 2012-08-09 13:24:45 -0500 | [diff] [blame] | 3030 | } |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 3031 | } |
| 3032 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3033 | pixman_region32_fini(&plane->base.damage); |
| 3034 | pixman_region32_init(&plane->base.damage); |
Pekka Paalanen | 7eaed40 | 2015-11-27 14:20:58 +0200 | [diff] [blame] | 3035 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3036 | if (drmModeMoveCursor(b->drm.fd, output->crtc_id, |
| 3037 | state->dest_x, state->dest_y)) { |
Daniel Stone | a7cba1d | 2017-04-04 17:54:21 +0100 | [diff] [blame] | 3038 | weston_log("failed to move cursor: %m\n"); |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3039 | goto err; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 3040 | } |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3041 | |
| 3042 | return; |
| 3043 | |
| 3044 | err: |
| 3045 | b->cursors_are_broken = 1; |
| 3046 | drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0); |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 3047 | } |
| 3048 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3049 | static void |
Daniel Stone | b1f166d | 2017-03-01 11:34:10 +0000 | [diff] [blame] | 3050 | drm_assign_planes(struct weston_output *output_base, void *repaint_data) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3051 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 3052 | struct drm_backend *b = to_drm_backend(output_base->compositor); |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 3053 | struct drm_pending_state *pending_state = repaint_data; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 3054 | struct drm_output *output = to_drm_output(output_base); |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 3055 | struct drm_output_state *state; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3056 | struct drm_plane_state *plane_state; |
Daniel Stone | c3fcb5b | 2016-12-01 12:33:54 +0000 | [diff] [blame] | 3057 | struct weston_view *ev; |
Daniel Stone | 115ed2c | 2016-11-08 21:19:22 +0000 | [diff] [blame] | 3058 | pixman_region32_t surface_overlap, renderer_region; |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 3059 | struct weston_plane *primary, *next_plane; |
Matt Hoosier | df57303 | 2017-08-24 09:24:20 -0500 | [diff] [blame] | 3060 | bool picked_scanout = false; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3061 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 3062 | assert(!output->state_last); |
| 3063 | state = drm_output_state_duplicate(output->state_cur, |
| 3064 | pending_state, |
| 3065 | DRM_OUTPUT_STATE_CLEAR_PLANES); |
| 3066 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3067 | /* |
| 3068 | * Find a surface for each sprite in the output using some heuristics: |
| 3069 | * 1) size |
| 3070 | * 2) frequency of update |
| 3071 | * 3) opacity (though some hw might support alpha blending) |
| 3072 | * 4) clipping (this can be fixed with color keys) |
| 3073 | * |
| 3074 | * The idea is to save on blitting since this should save power. |
| 3075 | * If we can get a large video surface on the sprite for example, |
| 3076 | * the main display surface may not need to update at all, and |
| 3077 | * the client buffer can be used directly for the sprite surface |
| 3078 | * as we do for flipping full screen surfaces. |
| 3079 | */ |
Daniel Stone | 115ed2c | 2016-11-08 21:19:22 +0000 | [diff] [blame] | 3080 | pixman_region32_init(&renderer_region); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3081 | primary = &output_base->compositor->primary_plane; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 3082 | |
Daniel Stone | c3fcb5b | 2016-12-01 12:33:54 +0000 | [diff] [blame] | 3083 | wl_list_for_each(ev, &output_base->compositor->view_list, link) { |
Ander Conselvan de Oliveira | 895b1fd | 2013-11-19 15:22:05 +0200 | [diff] [blame] | 3084 | struct weston_surface *es = ev->surface; |
| 3085 | |
| 3086 | /* Test whether this buffer can ever go into a plane: |
| 3087 | * non-shm, or small enough to be a cursor. |
| 3088 | * |
| 3089 | * Also, keep a reference when using the pixman renderer. |
| 3090 | * That makes it possible to do a seamless switch to the GL |
| 3091 | * renderer and since the pixman renderer keeps a reference |
| 3092 | * to the buffer anyway, there is no side effects. |
Pekka Paalanen | ccfeae2 | 2012-12-04 15:58:14 +0200 | [diff] [blame] | 3093 | */ |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3094 | if (b->use_pixman || |
Ander Conselvan de Oliveira | 895b1fd | 2013-11-19 15:22:05 +0200 | [diff] [blame] | 3095 | (es->buffer_ref.buffer && |
| 3096 | (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) || |
Derek Foreman | 8743047 | 2015-10-15 10:24:48 -0500 | [diff] [blame] | 3097 | (ev->surface->width <= b->cursor_width && |
| 3098 | ev->surface->height <= b->cursor_height)))) |
Derek Foreman | 0fd6d4e | 2014-10-10 09:36:45 -0500 | [diff] [blame] | 3099 | es->keep_buffer = true; |
Ander Conselvan de Oliveira | 895b1fd | 2013-11-19 15:22:05 +0200 | [diff] [blame] | 3100 | else |
Derek Foreman | 0fd6d4e | 2014-10-10 09:36:45 -0500 | [diff] [blame] | 3101 | es->keep_buffer = false; |
Pekka Paalanen | ccfeae2 | 2012-12-04 15:58:14 +0200 | [diff] [blame] | 3102 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3103 | pixman_region32_init(&surface_overlap); |
Daniel Stone | 115ed2c | 2016-11-08 21:19:22 +0000 | [diff] [blame] | 3104 | pixman_region32_intersect(&surface_overlap, &renderer_region, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 3105 | &ev->transform.boundingbox); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3106 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 3107 | next_plane = NULL; |
Matt Hoosier | df57303 | 2017-08-24 09:24:20 -0500 | [diff] [blame] | 3108 | if (pixman_region32_not_empty(&surface_overlap) || picked_scanout) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 3109 | next_plane = primary; |
| 3110 | if (next_plane == NULL) |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 3111 | next_plane = drm_output_prepare_cursor_view(state, ev); |
Matt Hoosier | df57303 | 2017-08-24 09:24:20 -0500 | [diff] [blame] | 3112 | |
| 3113 | /* If a higher-stacked view already got assigned to scanout, it's incorrect to |
| 3114 | * assign a subsequent (lower-stacked) view to scanout. |
| 3115 | */ |
| 3116 | if (next_plane == NULL) { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 3117 | next_plane = drm_output_prepare_scanout_view(state, ev); |
Matt Hoosier | df57303 | 2017-08-24 09:24:20 -0500 | [diff] [blame] | 3118 | if (next_plane) |
| 3119 | picked_scanout = true; |
| 3120 | } |
| 3121 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 3122 | if (next_plane == NULL) |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 3123 | next_plane = drm_output_prepare_overlay_view(state, ev); |
Matt Hoosier | df57303 | 2017-08-24 09:24:20 -0500 | [diff] [blame] | 3124 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 3125 | if (next_plane == NULL) |
| 3126 | next_plane = primary; |
Pekka Paalanen | bf0e031 | 2014-12-17 16:20:41 +0200 | [diff] [blame] | 3127 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 3128 | weston_view_move_to_plane(ev, next_plane); |
Pekka Paalanen | bf0e031 | 2014-12-17 16:20:41 +0200 | [diff] [blame] | 3129 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 3130 | if (next_plane == primary) |
Daniel Stone | 115ed2c | 2016-11-08 21:19:22 +0000 | [diff] [blame] | 3131 | pixman_region32_union(&renderer_region, |
| 3132 | &renderer_region, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 3133 | &ev->transform.boundingbox); |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 3134 | |
Pekka Paalanen | bf0e031 | 2014-12-17 16:20:41 +0200 | [diff] [blame] | 3135 | if (next_plane == primary || |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3136 | (output->cursor_plane && |
| 3137 | next_plane == &output->cursor_plane->base)) { |
Pekka Paalanen | bf0e031 | 2014-12-17 16:20:41 +0200 | [diff] [blame] | 3138 | /* cursor plane involves a copy */ |
| 3139 | ev->psf_flags = 0; |
| 3140 | } else { |
| 3141 | /* All other planes are a direct scanout of a |
| 3142 | * single client buffer. |
| 3143 | */ |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 3144 | ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY; |
Pekka Paalanen | bf0e031 | 2014-12-17 16:20:41 +0200 | [diff] [blame] | 3145 | } |
| 3146 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3147 | pixman_region32_fini(&surface_overlap); |
| 3148 | } |
Daniel Stone | 115ed2c | 2016-11-08 21:19:22 +0000 | [diff] [blame] | 3149 | pixman_region32_fini(&renderer_region); |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3150 | |
| 3151 | /* We rely on output->cursor_view being both an accurate reflection of |
| 3152 | * the cursor plane's state, but also being maintained across repaints |
| 3153 | * to avoid unnecessary damage uploads, per the comment in |
| 3154 | * drm_output_prepare_cursor_view. In the event that we go from having |
| 3155 | * a cursor view to not having a cursor view, we need to clear it. */ |
| 3156 | if (output->cursor_view) { |
| 3157 | plane_state = |
| 3158 | drm_output_state_get_existing_plane(state, |
| 3159 | output->cursor_plane); |
| 3160 | if (!plane_state || !plane_state->fb) |
| 3161 | output->cursor_view = NULL; |
| 3162 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3163 | } |
| 3164 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 3165 | /** |
| 3166 | * Find the closest-matching mode for a given target |
| 3167 | * |
| 3168 | * Given a target mode, find the most suitable mode amongst the output's |
| 3169 | * current mode list to use, preferring the current mode if possible, to |
| 3170 | * avoid an expensive mode switch. |
| 3171 | * |
| 3172 | * @param output DRM output |
| 3173 | * @param target_mode Mode to attempt to match |
| 3174 | * @returns Pointer to a mode from the output's mode list |
| 3175 | */ |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3176 | static struct drm_mode * |
| 3177 | choose_mode (struct drm_output *output, struct weston_mode *target_mode) |
| 3178 | { |
| 3179 | struct drm_mode *tmp_mode = NULL, *mode; |
| 3180 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 3181 | if (output->base.current_mode->width == target_mode->width && |
| 3182 | output->base.current_mode->height == target_mode->height && |
| 3183 | (output->base.current_mode->refresh == target_mode->refresh || |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3184 | target_mode->refresh == 0)) |
Daniel Stone | cb04cc4 | 2016-11-16 11:51:27 +0000 | [diff] [blame] | 3185 | return to_drm_mode(output->base.current_mode); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3186 | |
| 3187 | wl_list_for_each(mode, &output->base.mode_list, base.link) { |
| 3188 | if (mode->mode_info.hdisplay == target_mode->width && |
| 3189 | mode->mode_info.vdisplay == target_mode->height) { |
Mario Kleiner | 872797c | 2015-06-21 21:25:09 +0200 | [diff] [blame] | 3190 | if (mode->base.refresh == target_mode->refresh || |
| 3191 | target_mode->refresh == 0) { |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3192 | return mode; |
Daniel Stone | f556ebe | 2015-05-21 08:28:58 +0100 | [diff] [blame] | 3193 | } else if (!tmp_mode) |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3194 | tmp_mode = mode; |
| 3195 | } |
| 3196 | } |
| 3197 | |
| 3198 | return tmp_mode; |
| 3199 | } |
| 3200 | |
| 3201 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3202 | drm_output_init_egl(struct drm_output *output, struct drm_backend *b); |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 3203 | static void |
| 3204 | drm_output_fini_egl(struct drm_output *output); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3205 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3206 | drm_output_init_pixman(struct drm_output *output, struct drm_backend *b); |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 3207 | static void |
| 3208 | drm_output_fini_pixman(struct drm_output *output); |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 3209 | |
| 3210 | static int |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3211 | drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode) |
| 3212 | { |
Daniel Stone | 02d487a | 2017-10-07 14:01:45 +0100 | [diff] [blame] | 3213 | struct drm_output *output = to_drm_output(output_base); |
| 3214 | struct drm_backend *b = to_drm_backend(output_base->compositor); |
| 3215 | struct drm_mode *drm_mode = choose_mode(output, mode); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3216 | |
| 3217 | if (!drm_mode) { |
Daniel Stone | 02d487a | 2017-10-07 14:01:45 +0100 | [diff] [blame] | 3218 | weston_log("%s: invalid resolution %dx%d\n", |
| 3219 | output_base->name, mode->width, mode->height); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3220 | return -1; |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 3221 | } |
| 3222 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 3223 | if (&drm_mode->base == output->base.current_mode) |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3224 | return 0; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3225 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 3226 | output->base.current_mode->flags = 0; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3227 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 3228 | output->base.current_mode = &drm_mode->base; |
| 3229 | output->base.current_mode->flags = |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3230 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 3231 | |
Daniel Stone | f30a18c | 2017-04-04 17:54:31 +0100 | [diff] [blame] | 3232 | /* XXX: This drops our current buffer too early, before we've started |
| 3233 | * displaying it. Ideally this should be much more atomic and |
| 3234 | * integrated with a full repaint cycle, rather than doing a |
| 3235 | * sledgehammer modeswitch first, and only later showing new |
| 3236 | * content. |
| 3237 | */ |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 3238 | b->state_invalid = true; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3239 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3240 | if (b->use_pixman) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3241 | drm_output_fini_pixman(output); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3242 | if (drm_output_init_pixman(output, b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3243 | weston_log("failed to init output pixman state with " |
| 3244 | "new mode\n"); |
| 3245 | return -1; |
| 3246 | } |
| 3247 | } else { |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 3248 | drm_output_fini_egl(output); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3249 | if (drm_output_init_egl(output, b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3250 | weston_log("failed to init output egl state with " |
| 3251 | "new mode"); |
| 3252 | return -1; |
| 3253 | } |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 3254 | } |
| 3255 | |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3256 | return 0; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3257 | } |
| 3258 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 3259 | static int |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3260 | on_drm_input(int fd, uint32_t mask, void *data) |
| 3261 | { |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 3262 | #ifdef HAVE_DRM_ATOMIC |
| 3263 | struct drm_backend *b = data; |
| 3264 | #endif |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3265 | drmEventContext evctx; |
| 3266 | |
| 3267 | memset(&evctx, 0, sizeof evctx); |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 3268 | #ifndef HAVE_DRM_ATOMIC |
Emil Velikov | 863e66b | 2017-04-04 18:07:34 +0100 | [diff] [blame] | 3269 | evctx.version = 2; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 3270 | #else |
| 3271 | evctx.version = 3; |
| 3272 | if (b->atomic_modeset) |
| 3273 | evctx.page_flip_handler2 = atomic_flip_handler; |
| 3274 | else |
| 3275 | #endif |
| 3276 | evctx.page_flip_handler = page_flip_handler; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3277 | evctx.vblank_handler = vblank_handler; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3278 | drmHandleEvent(fd, &evctx); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 3279 | |
| 3280 | return 1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3281 | } |
| 3282 | |
| 3283 | static int |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3284 | init_kms_caps(struct drm_backend *b) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3285 | { |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 3286 | uint64_t cap; |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3287 | int ret; |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 3288 | clockid_t clk_id; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 3289 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3290 | weston_log("using %s\n", b->drm.filename); |
Kristian Høgsberg | b71302e | 2012-05-10 12:28:35 -0400 | [diff] [blame] | 3291 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3292 | ret = drmGetCap(b->drm.fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap); |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 3293 | if (ret == 0 && cap == 1) |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 3294 | clk_id = CLOCK_MONOTONIC; |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 3295 | else |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 3296 | clk_id = CLOCK_REALTIME; |
| 3297 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3298 | if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) { |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 3299 | weston_log("Error: failed to set presentation clock %d.\n", |
| 3300 | clk_id); |
| 3301 | return -1; |
| 3302 | } |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 3303 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3304 | ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_WIDTH, &cap); |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 3305 | if (ret == 0) |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3306 | b->cursor_width = cap; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 3307 | else |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3308 | b->cursor_width = 64; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 3309 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3310 | ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_HEIGHT, &cap); |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 3311 | if (ret == 0) |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3312 | b->cursor_height = cap; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 3313 | else |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3314 | b->cursor_height = 64; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 3315 | |
Daniel Stone | be1090b | 2017-09-06 17:29:57 +0100 | [diff] [blame] | 3316 | if (!getenv("WESTON_DISABLE_UNIVERSAL_PLANES")) { |
| 3317 | ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1); |
| 3318 | b->universal_planes = (ret == 0); |
| 3319 | } |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 3320 | weston_log("DRM: %s universal planes\n", |
| 3321 | b->universal_planes ? "supports" : "does not support"); |
| 3322 | |
Pekka Paalanen | cd011a6 | 2016-11-15 22:07:49 +0000 | [diff] [blame] | 3323 | #ifdef HAVE_DRM_ATOMIC |
| 3324 | if (b->universal_planes && !getenv("WESTON_DISABLE_ATOMIC")) { |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 3325 | ret = drmGetCap(b->drm.fd, DRM_CAP_CRTC_IN_VBLANK_EVENT, &cap); |
| 3326 | if (ret != 0) |
| 3327 | cap = 0; |
Pekka Paalanen | cd011a6 | 2016-11-15 22:07:49 +0000 | [diff] [blame] | 3328 | ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_ATOMIC, 1); |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 3329 | b->atomic_modeset = ((ret == 0) && (cap == 1)); |
Pekka Paalanen | cd011a6 | 2016-11-15 22:07:49 +0000 | [diff] [blame] | 3330 | } |
| 3331 | #endif |
| 3332 | weston_log("DRM: %s atomic modesetting\n", |
| 3333 | b->atomic_modeset ? "supports" : "does not support"); |
| 3334 | |
Ander Conselvan de Oliveira | 2292917 | 2013-01-25 15:13:02 +0200 | [diff] [blame] | 3335 | return 0; |
| 3336 | } |
| 3337 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3338 | static struct gbm_device * |
| 3339 | create_gbm_device(int fd) |
Ander Conselvan de Oliveira | 2292917 | 2013-01-25 15:13:02 +0200 | [diff] [blame] | 3340 | { |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3341 | struct gbm_device *gbm; |
Alexandru DAMIAN | be0ac5b | 2013-10-02 17:51:05 +0100 | [diff] [blame] | 3342 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 3343 | gl_renderer = weston_load_module("gl-renderer.so", |
| 3344 | "gl_renderer_interface"); |
| 3345 | if (!gl_renderer) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3346 | return NULL; |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 3347 | |
| 3348 | /* GBM will load a dri driver, but even though they need symbols from |
| 3349 | * libglapi, in some version of Mesa they are not linked to it. Since |
| 3350 | * only the gl-renderer module links to it, the call above won't make |
| 3351 | * these symbols globally available, and loading the DRI driver fails. |
| 3352 | * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */ |
| 3353 | dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL); |
| 3354 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3355 | gbm = gbm_create_device(fd); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3356 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3357 | return gbm; |
| 3358 | } |
| 3359 | |
Bryce Harrington | c056a98 | 2015-05-19 15:25:18 -0700 | [diff] [blame] | 3360 | /* When initializing EGL, if the preferred buffer format isn't available |
Bryce Harrington | b993998 | 2016-04-15 20:28:26 -0700 | [diff] [blame] | 3361 | * 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] | 3362 | * |
| 3363 | * This returns 0 if substitution isn't possible, but 0 might be a |
| 3364 | * legitimate format for other EGL platforms, so the caller is |
| 3365 | * responsible for checking for 0 before calling gl_renderer->create(). |
| 3366 | * |
| 3367 | * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689 |
| 3368 | * but it's entirely possible we'll see this again on other implementations. |
| 3369 | */ |
| 3370 | static int |
| 3371 | fallback_format_for(uint32_t format) |
| 3372 | { |
| 3373 | switch (format) { |
| 3374 | case GBM_FORMAT_XRGB8888: |
| 3375 | return GBM_FORMAT_ARGB8888; |
| 3376 | case GBM_FORMAT_XRGB2101010: |
| 3377 | return GBM_FORMAT_ARGB2101010; |
| 3378 | default: |
| 3379 | return 0; |
| 3380 | } |
| 3381 | } |
| 3382 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3383 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3384 | drm_backend_create_gl_renderer(struct drm_backend *b) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3385 | { |
Derek Foreman | 6d55637 | 2015-11-04 14:47:33 -0600 | [diff] [blame] | 3386 | EGLint format[3] = { |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 3387 | b->gbm_format, |
| 3388 | fallback_format_for(b->gbm_format), |
Derek Foreman | 6d55637 | 2015-11-04 14:47:33 -0600 | [diff] [blame] | 3389 | 0, |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 3390 | }; |
Derek Foreman | 6d55637 | 2015-11-04 14:47:33 -0600 | [diff] [blame] | 3391 | int n_formats = 2; |
John Kåre Alsaker | ef591aa | 2013-03-02 12:27:39 +0100 | [diff] [blame] | 3392 | |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 3393 | if (format[1]) |
Derek Foreman | 6d55637 | 2015-11-04 14:47:33 -0600 | [diff] [blame] | 3394 | n_formats = 3; |
Miguel A. Vico | dddc670 | 2016-05-18 17:41:07 +0200 | [diff] [blame] | 3395 | if (gl_renderer->display_create(b->compositor, |
| 3396 | EGL_PLATFORM_GBM_KHR, |
| 3397 | (void *)b->gbm, |
Miguel A. Vico | 41700e3 | 2016-05-18 17:47:59 +0200 | [diff] [blame] | 3398 | NULL, |
Miguel A. Vico | dddc670 | 2016-05-18 17:41:07 +0200 | [diff] [blame] | 3399 | gl_renderer->opaque_attribs, |
| 3400 | format, |
| 3401 | n_formats) < 0) { |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3402 | return -1; |
| 3403 | } |
| 3404 | |
| 3405 | return 0; |
| 3406 | } |
| 3407 | |
| 3408 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3409 | init_egl(struct drm_backend *b) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3410 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3411 | b->gbm = create_gbm_device(b->drm.fd); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3412 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3413 | if (!b->gbm) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3414 | return -1; |
| 3415 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3416 | if (drm_backend_create_gl_renderer(b) < 0) { |
| 3417 | gbm_device_destroy(b->gbm); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 3418 | return -1; |
| 3419 | } |
| 3420 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3421 | return 0; |
| 3422 | } |
| 3423 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3424 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3425 | init_pixman(struct drm_backend *b) |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3426 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3427 | return pixman_renderer_init(b->compositor); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3428 | } |
| 3429 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 3430 | /** |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3431 | * Create a drm_plane for a hardware plane |
| 3432 | * |
| 3433 | * Creates one drm_plane structure for a hardware plane, and initialises its |
| 3434 | * properties and formats. |
| 3435 | * |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3436 | * In the absence of universal plane support, where KMS does not explicitly |
| 3437 | * expose the primary and cursor planes to userspace, this may also create |
| 3438 | * an 'internal' plane for internal management. |
| 3439 | * |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3440 | * This function does not add the plane to the list of usable planes in Weston |
| 3441 | * itself; the caller is responsible for this. |
| 3442 | * |
| 3443 | * Call drm_plane_destroy to clean up the plane. |
| 3444 | * |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3445 | * @sa drm_output_find_special_plane |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3446 | * @param b DRM compositor backend |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3447 | * @param kplane DRM plane to create, or NULL if creating internal plane |
| 3448 | * @param output Output to create internal plane for, or NULL |
| 3449 | * @param type Type to use when creating internal plane, or invalid |
| 3450 | * @param format Format to use for internal planes, or 0 |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3451 | */ |
| 3452 | static struct drm_plane * |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3453 | drm_plane_create(struct drm_backend *b, const drmModePlane *kplane, |
| 3454 | struct drm_output *output, enum wdrm_plane_type type, |
| 3455 | uint32_t format) |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3456 | { |
| 3457 | struct drm_plane *plane; |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 3458 | drmModeObjectProperties *props; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3459 | int num_formats = (kplane) ? kplane->count_formats : 1; |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 3460 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3461 | plane = zalloc(sizeof(*plane) + |
| 3462 | (sizeof(uint32_t) * num_formats)); |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3463 | if (!plane) { |
| 3464 | weston_log("%s: out of memory\n", __func__); |
| 3465 | return NULL; |
| 3466 | } |
| 3467 | |
| 3468 | plane->backend = b; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 3469 | plane->state_cur = drm_plane_state_alloc(NULL, plane); |
| 3470 | plane->state_cur->complete = true; |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3471 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3472 | if (kplane) { |
| 3473 | plane->possible_crtcs = kplane->possible_crtcs; |
| 3474 | plane->plane_id = kplane->plane_id; |
| 3475 | plane->count_formats = kplane->count_formats; |
| 3476 | memcpy(plane->formats, kplane->formats, |
| 3477 | kplane->count_formats * sizeof(kplane->formats[0])); |
| 3478 | |
| 3479 | props = drmModeObjectGetProperties(b->drm.fd, kplane->plane_id, |
| 3480 | DRM_MODE_OBJECT_PLANE); |
| 3481 | if (!props) { |
| 3482 | weston_log("couldn't get plane properties\n"); |
| 3483 | goto err; |
| 3484 | } |
| 3485 | drm_property_info_populate(b, plane_props, plane->props, |
| 3486 | WDRM_PLANE__COUNT, props); |
| 3487 | plane->type = |
| 3488 | drm_property_get_value(&plane->props[WDRM_PLANE_TYPE], |
| 3489 | props, |
| 3490 | WDRM_PLANE_TYPE__COUNT); |
| 3491 | drmModeFreeObjectProperties(props); |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 3492 | } |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3493 | else { |
| 3494 | plane->possible_crtcs = (1 << output->pipe); |
| 3495 | plane->plane_id = 0; |
| 3496 | plane->count_formats = 1; |
| 3497 | plane->formats[0] = format; |
| 3498 | plane->type = type; |
| 3499 | } |
| 3500 | |
| 3501 | if (plane->type == WDRM_PLANE_TYPE__COUNT) |
| 3502 | goto err_props; |
| 3503 | |
| 3504 | /* With universal planes, everything is a DRM plane; without |
| 3505 | * universal planes, the only DRM planes are overlay planes. |
| 3506 | * Everything else is a fake plane. */ |
| 3507 | if (b->universal_planes) { |
| 3508 | assert(kplane); |
| 3509 | } else { |
| 3510 | if (kplane) |
| 3511 | assert(plane->type == WDRM_PLANE_TYPE_OVERLAY); |
| 3512 | else |
| 3513 | assert(plane->type != WDRM_PLANE_TYPE_OVERLAY && |
| 3514 | output); |
| 3515 | } |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 3516 | |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3517 | weston_plane_init(&plane->base, b->compositor, 0, 0); |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 3518 | wl_list_insert(&b->plane_list, &plane->link); |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3519 | |
| 3520 | return plane; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3521 | |
| 3522 | err_props: |
| 3523 | drm_property_info_free(plane->props, WDRM_PLANE__COUNT); |
| 3524 | err: |
| 3525 | drm_plane_state_free(plane->state_cur, true); |
| 3526 | free(plane); |
| 3527 | return NULL; |
| 3528 | } |
| 3529 | |
| 3530 | /** |
| 3531 | * Find, or create, a special-purpose plane |
| 3532 | * |
| 3533 | * Primary and cursor planes are a special case, in that before universal |
| 3534 | * planes, they are driven by non-plane API calls. Without universal plane |
| 3535 | * support, the only way to configure a primary plane is via drmModeSetCrtc, |
| 3536 | * and the only way to configure a cursor plane is drmModeSetCursor2. |
| 3537 | * |
| 3538 | * Although they may actually be regular planes in the hardware, without |
| 3539 | * universal plane support, these planes are not actually exposed to |
| 3540 | * userspace in the regular plane list. |
| 3541 | * |
| 3542 | * However, for ease of internal tracking, we want to manage all planes |
| 3543 | * through the same drm_plane structures. Therefore, when we are running |
| 3544 | * without universal plane support, we create fake drm_plane structures |
| 3545 | * to track these planes. |
| 3546 | * |
| 3547 | * @param b DRM backend |
| 3548 | * @param output Output to use for plane |
| 3549 | * @param type Type of plane |
| 3550 | */ |
| 3551 | static struct drm_plane * |
| 3552 | drm_output_find_special_plane(struct drm_backend *b, struct drm_output *output, |
| 3553 | enum wdrm_plane_type type) |
| 3554 | { |
| 3555 | struct drm_plane *plane; |
| 3556 | |
| 3557 | if (!b->universal_planes) { |
| 3558 | uint32_t format; |
| 3559 | |
| 3560 | switch (type) { |
| 3561 | case WDRM_PLANE_TYPE_CURSOR: |
| 3562 | format = GBM_FORMAT_ARGB8888; |
| 3563 | break; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 3564 | case WDRM_PLANE_TYPE_PRIMARY: |
| 3565 | /* We don't know what formats the primary plane supports |
| 3566 | * before universal planes, so we just assume that the |
| 3567 | * GBM format works; however, this isn't set until after |
| 3568 | * the output is created. */ |
| 3569 | format = 0; |
| 3570 | break; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3571 | default: |
| 3572 | assert(!"invalid type in drm_output_find_special_plane"); |
| 3573 | break; |
| 3574 | } |
| 3575 | |
| 3576 | return drm_plane_create(b, NULL, output, type, format); |
| 3577 | } |
| 3578 | |
| 3579 | wl_list_for_each(plane, &b->plane_list, link) { |
| 3580 | struct drm_output *tmp; |
| 3581 | bool found_elsewhere = false; |
| 3582 | |
| 3583 | if (plane->type != type) |
| 3584 | continue; |
| 3585 | if (!drm_plane_is_available(plane, output)) |
| 3586 | continue; |
| 3587 | |
| 3588 | /* On some platforms, primary/cursor planes can roam |
| 3589 | * between different CRTCs, so make sure we don't claim the |
| 3590 | * same plane for two outputs. */ |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3591 | wl_list_for_each(tmp, &b->compositor->output_list, |
| 3592 | base.link) { |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 3593 | if (tmp->cursor_plane == plane || |
| 3594 | tmp->scanout_plane == plane) { |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3595 | found_elsewhere = true; |
| 3596 | break; |
| 3597 | } |
| 3598 | } |
| 3599 | |
| 3600 | if (found_elsewhere) |
| 3601 | continue; |
| 3602 | |
| 3603 | plane->possible_crtcs = (1 << output->pipe); |
| 3604 | return plane; |
| 3605 | } |
| 3606 | |
| 3607 | return NULL; |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3608 | } |
| 3609 | |
| 3610 | /** |
| 3611 | * Destroy one DRM plane |
| 3612 | * |
| 3613 | * Destroy a DRM plane, removing it from screen and releasing its retained |
| 3614 | * buffers in the process. The counterpart to drm_plane_create. |
| 3615 | * |
| 3616 | * @param plane Plane to deallocate (will be freed) |
| 3617 | */ |
| 3618 | static void |
| 3619 | drm_plane_destroy(struct drm_plane *plane) |
| 3620 | { |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3621 | if (plane->type == WDRM_PLANE_TYPE_OVERLAY) |
| 3622 | drmModeSetPlane(plane->backend->drm.fd, plane->plane_id, |
| 3623 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 3624 | drm_plane_state_free(plane->state_cur, true); |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 3625 | drm_property_info_free(plane->props, WDRM_PLANE__COUNT); |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3626 | weston_plane_release(&plane->base); |
| 3627 | wl_list_remove(&plane->link); |
| 3628 | free(plane); |
| 3629 | } |
| 3630 | |
| 3631 | /** |
| 3632 | * Initialise sprites (overlay planes) |
| 3633 | * |
| 3634 | * Walk the list of provided DRM planes, and add overlay planes. |
| 3635 | * |
| 3636 | * Call destroy_sprites to free these planes. |
| 3637 | * |
| 3638 | * @param b DRM compositor backend |
| 3639 | */ |
| 3640 | static void |
| 3641 | create_sprites(struct drm_backend *b) |
| 3642 | { |
| 3643 | drmModePlaneRes *kplane_res; |
| 3644 | drmModePlane *kplane; |
| 3645 | struct drm_plane *drm_plane; |
| 3646 | uint32_t i; |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3647 | kplane_res = drmModeGetPlaneResources(b->drm.fd); |
| 3648 | if (!kplane_res) { |
| 3649 | weston_log("failed to get plane resources: %s\n", |
| 3650 | strerror(errno)); |
| 3651 | return; |
| 3652 | } |
| 3653 | |
| 3654 | for (i = 0; i < kplane_res->count_planes; i++) { |
| 3655 | kplane = drmModeGetPlane(b->drm.fd, kplane_res->planes[i]); |
| 3656 | if (!kplane) |
| 3657 | continue; |
| 3658 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3659 | drm_plane = drm_plane_create(b, kplane, NULL, |
| 3660 | WDRM_PLANE_TYPE__COUNT, 0); |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3661 | drmModeFreePlane(kplane); |
| 3662 | if (!drm_plane) |
| 3663 | continue; |
| 3664 | |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 3665 | if (drm_plane->type == WDRM_PLANE_TYPE_OVERLAY) |
| 3666 | weston_compositor_stack_plane(b->compositor, |
| 3667 | &drm_plane->base, |
| 3668 | &b->compositor->primary_plane); |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3669 | } |
| 3670 | |
| 3671 | drmModeFreePlaneResources(kplane_res); |
| 3672 | } |
| 3673 | |
| 3674 | /** |
| 3675 | * Clean up sprites (overlay planes) |
| 3676 | * |
| 3677 | * The counterpart to create_sprites. |
| 3678 | * |
| 3679 | * @param b DRM compositor backend |
| 3680 | */ |
| 3681 | static void |
| 3682 | destroy_sprites(struct drm_backend *b) |
| 3683 | { |
| 3684 | struct drm_plane *plane, *next; |
| 3685 | |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 3686 | wl_list_for_each_safe(plane, next, &b->plane_list, link) |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3687 | drm_plane_destroy(plane); |
| 3688 | } |
| 3689 | |
Pekka Paalanen | dc14fd4 | 2017-11-10 15:31:39 +0200 | [diff] [blame] | 3690 | static uint32_t |
| 3691 | drm_refresh_rate_mHz(const drmModeModeInfo *info) |
| 3692 | { |
| 3693 | uint64_t refresh; |
| 3694 | |
| 3695 | /* Calculate higher precision (mHz) refresh rate */ |
| 3696 | refresh = (info->clock * 1000000LL / info->htotal + |
| 3697 | info->vtotal / 2) / info->vtotal; |
| 3698 | |
| 3699 | if (info->flags & DRM_MODE_FLAG_INTERLACE) |
| 3700 | refresh *= 2; |
| 3701 | if (info->flags & DRM_MODE_FLAG_DBLSCAN) |
| 3702 | refresh /= 2; |
| 3703 | if (info->vscan > 1) |
| 3704 | refresh /= info->vscan; |
| 3705 | |
| 3706 | return refresh; |
| 3707 | } |
| 3708 | |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3709 | /** |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 3710 | * Add a mode to output's mode list |
| 3711 | * |
| 3712 | * Copy the supplied DRM mode into a Weston mode structure, and add it to the |
| 3713 | * output's mode list. |
| 3714 | * |
| 3715 | * @param output DRM output to add mode to |
| 3716 | * @param info DRM mode structure to add |
| 3717 | * @returns Newly-allocated Weston/DRM mode structure |
| 3718 | */ |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 3719 | static struct drm_mode * |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 3720 | drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info) |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 3721 | { |
| 3722 | struct drm_mode *mode; |
| 3723 | |
| 3724 | mode = malloc(sizeof *mode); |
| 3725 | if (mode == NULL) |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 3726 | return NULL; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 3727 | |
| 3728 | mode->base.flags = 0; |
Alexander Larsson | 0b13506 | 2013-05-28 16:23:36 +0200 | [diff] [blame] | 3729 | mode->base.width = info->hdisplay; |
| 3730 | mode->base.height = info->vdisplay; |
Kristian Høgsberg | c4621b0 | 2012-05-10 12:23:53 -0400 | [diff] [blame] | 3731 | |
Pekka Paalanen | dc14fd4 | 2017-11-10 15:31:39 +0200 | [diff] [blame] | 3732 | mode->base.refresh = drm_refresh_rate_mHz(info); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 3733 | mode->mode_info = *info; |
Daniel Stone | d5526cb | 2016-11-16 10:54:10 +0000 | [diff] [blame] | 3734 | mode->blob_id = 0; |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 3735 | |
| 3736 | if (info->type & DRM_MODE_TYPE_PREFERRED) |
| 3737 | mode->base.flags |= WL_OUTPUT_MODE_PREFERRED; |
| 3738 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 3739 | wl_list_insert(output->base.mode_list.prev, &mode->base.link); |
| 3740 | |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 3741 | return mode; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 3742 | } |
| 3743 | |
Daniel Stone | d5526cb | 2016-11-16 10:54:10 +0000 | [diff] [blame] | 3744 | /** |
| 3745 | * Destroys a mode, and removes it from the list. |
| 3746 | */ |
| 3747 | static void |
| 3748 | drm_output_destroy_mode(struct drm_backend *backend, struct drm_mode *mode) |
| 3749 | { |
| 3750 | if (mode->blob_id) |
| 3751 | drmModeDestroyPropertyBlob(backend->drm.fd, mode->blob_id); |
| 3752 | wl_list_remove(&mode->base.link); |
| 3753 | free(mode); |
| 3754 | } |
| 3755 | |
Pekka Paalanen | 383b3af | 2017-09-11 14:40:48 +0300 | [diff] [blame] | 3756 | /** Destroy a list of drm_modes |
| 3757 | * |
| 3758 | * @param backend The backend for releasing mode property blobs. |
| 3759 | * @param mode_list The list linked by drm_mode::base.link. |
| 3760 | */ |
| 3761 | static void |
| 3762 | drm_mode_list_destroy(struct drm_backend *backend, struct wl_list *mode_list) |
| 3763 | { |
| 3764 | struct drm_mode *mode, *next; |
| 3765 | |
| 3766 | wl_list_for_each_safe(mode, next, mode_list, base.link) |
| 3767 | drm_output_destroy_mode(backend, mode); |
| 3768 | } |
| 3769 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 3770 | static int |
| 3771 | drm_subpixel_to_wayland(int drm_value) |
| 3772 | { |
| 3773 | switch (drm_value) { |
| 3774 | default: |
| 3775 | case DRM_MODE_SUBPIXEL_UNKNOWN: |
| 3776 | return WL_OUTPUT_SUBPIXEL_UNKNOWN; |
| 3777 | case DRM_MODE_SUBPIXEL_NONE: |
| 3778 | return WL_OUTPUT_SUBPIXEL_NONE; |
| 3779 | case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB: |
| 3780 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB; |
| 3781 | case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR: |
| 3782 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR; |
| 3783 | case DRM_MODE_SUBPIXEL_VERTICAL_RGB: |
| 3784 | return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB; |
| 3785 | case DRM_MODE_SUBPIXEL_VERTICAL_BGR: |
| 3786 | return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR; |
| 3787 | } |
| 3788 | } |
| 3789 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 3790 | /* returns a value between 0-255 range, where higher is brighter */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3791 | static uint32_t |
Pekka Paalanen | ce72424 | 2017-09-04 12:21:24 +0300 | [diff] [blame] | 3792 | drm_get_backlight(struct drm_head *head) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3793 | { |
| 3794 | long brightness, max_brightness, norm; |
| 3795 | |
Pekka Paalanen | ce72424 | 2017-09-04 12:21:24 +0300 | [diff] [blame] | 3796 | brightness = backlight_get_brightness(head->backlight); |
| 3797 | max_brightness = backlight_get_max_brightness(head->backlight); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3798 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 3799 | /* convert it on a scale of 0 to 255 */ |
| 3800 | norm = (brightness * 255)/(max_brightness); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3801 | |
| 3802 | return (uint32_t) norm; |
| 3803 | } |
| 3804 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 3805 | /* values accepted are between 0-255 range */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3806 | static void |
| 3807 | drm_set_backlight(struct weston_output *output_base, uint32_t value) |
| 3808 | { |
Pekka Paalanen | ecc8cce | 2017-09-12 16:14:31 +0300 | [diff] [blame] | 3809 | struct drm_output *output = to_drm_output(output_base); |
| 3810 | struct drm_head *head; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3811 | long max_brightness, new_brightness; |
| 3812 | |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 3813 | if (value > 255) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3814 | return; |
| 3815 | |
Pekka Paalanen | ecc8cce | 2017-09-12 16:14:31 +0300 | [diff] [blame] | 3816 | wl_list_for_each(head, &output->base.head_list, base.output_link) { |
| 3817 | if (!head->backlight) |
| 3818 | return; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3819 | |
Pekka Paalanen | ecc8cce | 2017-09-12 16:14:31 +0300 | [diff] [blame] | 3820 | max_brightness = backlight_get_max_brightness(head->backlight); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3821 | |
Pekka Paalanen | ecc8cce | 2017-09-12 16:14:31 +0300 | [diff] [blame] | 3822 | /* get denormalized value */ |
| 3823 | new_brightness = (value * max_brightness) / 255; |
| 3824 | |
| 3825 | backlight_set_brightness(head->backlight, new_brightness); |
| 3826 | } |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3827 | } |
| 3828 | |
Pekka Paalanen | f8b850d | 2017-11-15 12:51:01 +0200 | [diff] [blame] | 3829 | static void |
| 3830 | drm_output_init_backlight(struct drm_output *output) |
| 3831 | { |
| 3832 | struct weston_head *base; |
| 3833 | struct drm_head *head; |
| 3834 | |
| 3835 | output->base.set_backlight = NULL; |
| 3836 | |
| 3837 | wl_list_for_each(base, &output->base.head_list, output_link) { |
| 3838 | head = to_drm_head(base); |
| 3839 | |
| 3840 | if (head->backlight) { |
| 3841 | weston_log("Initialized backlight for head '%s', device %s\n", |
| 3842 | head->base.name, head->backlight->path); |
| 3843 | |
| 3844 | if (!output->base.set_backlight) { |
| 3845 | output->base.set_backlight = drm_set_backlight; |
| 3846 | output->base.backlight_current = |
| 3847 | drm_get_backlight(head); |
| 3848 | } |
| 3849 | } |
| 3850 | } |
| 3851 | |
| 3852 | if (!output->base.set_backlight) { |
| 3853 | weston_log("No backlight control for output '%s'\n", |
| 3854 | output->base.name); |
| 3855 | } |
| 3856 | } |
| 3857 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 3858 | /** |
| 3859 | * Power output on or off |
| 3860 | * |
| 3861 | * The DPMS/power level of an output is used to switch it on or off. This |
| 3862 | * is DRM's hook for doing so, which can called either as part of repaint, |
| 3863 | * or independently of the repaint loop. |
| 3864 | * |
| 3865 | * If we are called as part of repaint, we simply set the relevant bit in |
| 3866 | * state and return. |
| 3867 | */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3868 | static void |
| 3869 | drm_set_dpms(struct weston_output *output_base, enum dpms_enum level) |
| 3870 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 3871 | struct drm_output *output = to_drm_output(output_base); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 3872 | struct drm_backend *b = to_drm_backend(output_base->compositor); |
| 3873 | struct drm_pending_state *pending_state = b->repaint_data; |
| 3874 | struct drm_output_state *state; |
Daniel Stone | 36609c7 | 2015-06-18 07:49:02 +0100 | [diff] [blame] | 3875 | int ret; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3876 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 3877 | if (output->state_cur->dpms == level) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3878 | return; |
| 3879 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 3880 | /* If we're being called during the repaint loop, then this is |
| 3881 | * simple: discard any previously-generated state, and create a new |
| 3882 | * state where we disable everything. When we come to flush, this |
| 3883 | * will be applied. |
| 3884 | * |
| 3885 | * However, we need to be careful: we can be called whilst another |
| 3886 | * output is in its repaint cycle (pending_state exists), but our |
| 3887 | * output still has an incomplete state application outstanding. |
| 3888 | * In that case, we need to wait until that completes. */ |
| 3889 | if (pending_state && !output->state_last) { |
| 3890 | /* The repaint loop already sets DPMS on; we don't need to |
| 3891 | * explicitly set it on here, as it will already happen |
| 3892 | * whilst applying the repaint state. */ |
| 3893 | if (level == WESTON_DPMS_ON) |
| 3894 | return; |
| 3895 | |
| 3896 | state = drm_pending_state_get_output(pending_state, output); |
| 3897 | if (state) |
| 3898 | drm_output_state_free(state); |
| 3899 | state = drm_output_get_disable_state(pending_state, output); |
Daniel Stone | 36609c7 | 2015-06-18 07:49:02 +0100 | [diff] [blame] | 3900 | return; |
| 3901 | } |
| 3902 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 3903 | /* As we throw everything away when disabling, just send us back through |
| 3904 | * a repaint cycle. */ |
| 3905 | if (level == WESTON_DPMS_ON) { |
| 3906 | if (output->dpms_off_pending) |
| 3907 | output->dpms_off_pending = 0; |
| 3908 | weston_output_schedule_repaint(output_base); |
| 3909 | return; |
| 3910 | } |
| 3911 | |
| 3912 | /* If we've already got a request in the pipeline, then we need to |
| 3913 | * park our DPMS request until that request has quiesced. */ |
| 3914 | if (output->state_last) { |
| 3915 | output->dpms_off_pending = 1; |
| 3916 | return; |
| 3917 | } |
| 3918 | |
| 3919 | pending_state = drm_pending_state_alloc(b); |
| 3920 | drm_output_get_disable_state(pending_state, output); |
| 3921 | ret = drm_pending_state_apply_sync(pending_state); |
| 3922 | if (ret != 0) |
| 3923 | weston_log("drm_set_dpms: couldn't disable output?\n"); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3924 | } |
| 3925 | |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 3926 | static const char * const connector_type_names[] = { |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 3927 | [DRM_MODE_CONNECTOR_Unknown] = "Unknown", |
| 3928 | [DRM_MODE_CONNECTOR_VGA] = "VGA", |
| 3929 | [DRM_MODE_CONNECTOR_DVII] = "DVI-I", |
| 3930 | [DRM_MODE_CONNECTOR_DVID] = "DVI-D", |
| 3931 | [DRM_MODE_CONNECTOR_DVIA] = "DVI-A", |
| 3932 | [DRM_MODE_CONNECTOR_Composite] = "Composite", |
| 3933 | [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO", |
| 3934 | [DRM_MODE_CONNECTOR_LVDS] = "LVDS", |
| 3935 | [DRM_MODE_CONNECTOR_Component] = "Component", |
| 3936 | [DRM_MODE_CONNECTOR_9PinDIN] = "DIN", |
| 3937 | [DRM_MODE_CONNECTOR_DisplayPort] = "DP", |
| 3938 | [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A", |
| 3939 | [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B", |
| 3940 | [DRM_MODE_CONNECTOR_TV] = "TV", |
| 3941 | [DRM_MODE_CONNECTOR_eDP] = "eDP", |
Pekka Paalanen | ab81f15 | 2015-08-24 14:27:07 +0300 | [diff] [blame] | 3942 | #ifdef DRM_MODE_CONNECTOR_DSI |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 3943 | [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual", |
| 3944 | [DRM_MODE_CONNECTOR_DSI] = "DSI", |
Pekka Paalanen | ab81f15 | 2015-08-24 14:27:07 +0300 | [diff] [blame] | 3945 | #endif |
Kristian Høgsberg | 2f9ed71 | 2012-07-26 17:57:15 -0400 | [diff] [blame] | 3946 | }; |
| 3947 | |
Pekka Paalanen | 1f21ef1 | 2017-04-03 13:33:26 +0300 | [diff] [blame] | 3948 | /** Create a name given a DRM connector |
| 3949 | * |
| 3950 | * \param con The DRM connector whose type and id form the name. |
| 3951 | * \return A newly allocate string, or NULL on error. Must be free()'d |
| 3952 | * after use. |
| 3953 | * |
| 3954 | * The name does not identify the DRM display device. |
| 3955 | */ |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 3956 | static char * |
| 3957 | make_connector_name(const drmModeConnector *con) |
| 3958 | { |
Pekka Paalanen | 1f21ef1 | 2017-04-03 13:33:26 +0300 | [diff] [blame] | 3959 | char *name; |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 3960 | const char *type_name = NULL; |
Pekka Paalanen | 1f21ef1 | 2017-04-03 13:33:26 +0300 | [diff] [blame] | 3961 | int ret; |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 3962 | |
| 3963 | if (con->connector_type < ARRAY_LENGTH(connector_type_names)) |
| 3964 | type_name = connector_type_names[con->connector_type]; |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 3965 | |
| 3966 | if (!type_name) |
| 3967 | type_name = "UNNAMED"; |
| 3968 | |
Pekka Paalanen | 1f21ef1 | 2017-04-03 13:33:26 +0300 | [diff] [blame] | 3969 | ret = asprintf(&name, "%s-%d", type_name, con->connector_type_id); |
| 3970 | if (ret < 0) |
| 3971 | return NULL; |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 3972 | |
Pekka Paalanen | 1f21ef1 | 2017-04-03 13:33:26 +0300 | [diff] [blame] | 3973 | return name; |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 3974 | } |
| 3975 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3976 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3977 | find_crtc_for_connector(struct drm_backend *b, |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 3978 | drmModeRes *resources, drmModeConnector *connector) |
| 3979 | { |
| 3980 | drmModeEncoder *encoder; |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 3981 | int i, j; |
Daniel Stone | 75487c2 | 2017-01-16 14:33:38 +0000 | [diff] [blame] | 3982 | int ret = -1; |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 3983 | |
| 3984 | for (j = 0; j < connector->count_encoders; j++) { |
Daniel Stone | 75487c2 | 2017-01-16 14:33:38 +0000 | [diff] [blame] | 3985 | uint32_t possible_crtcs, encoder_id, crtc_id; |
| 3986 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3987 | encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]); |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 3988 | if (encoder == NULL) { |
| 3989 | weston_log("Failed to get encoder.\n"); |
Daniel Stone | 75487c2 | 2017-01-16 14:33:38 +0000 | [diff] [blame] | 3990 | continue; |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 3991 | } |
Daniel Stone | 75487c2 | 2017-01-16 14:33:38 +0000 | [diff] [blame] | 3992 | encoder_id = encoder->encoder_id; |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 3993 | possible_crtcs = encoder->possible_crtcs; |
Daniel Stone | 75487c2 | 2017-01-16 14:33:38 +0000 | [diff] [blame] | 3994 | crtc_id = encoder->crtc_id; |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 3995 | drmModeFreeEncoder(encoder); |
| 3996 | |
| 3997 | for (i = 0; i < resources->count_crtcs; i++) { |
Daniel Stone | 72c0e1b | 2017-02-09 13:49:15 +0000 | [diff] [blame] | 3998 | if (!(possible_crtcs & (1 << i))) |
| 3999 | continue; |
| 4000 | |
| 4001 | if (drm_output_find_by_crtc(b, resources->crtcs[i])) |
| 4002 | continue; |
| 4003 | |
Daniel Stone | 75487c2 | 2017-01-16 14:33:38 +0000 | [diff] [blame] | 4004 | /* Try to preserve the existing |
| 4005 | * CRTC -> encoder -> connector routing; it makes |
| 4006 | * initialisation faster, and also since we have a |
| 4007 | * very dumb picking algorithm, may preserve a better |
| 4008 | * choice. */ |
| 4009 | if (!connector->encoder_id || |
| 4010 | (encoder_id == connector->encoder_id && |
| 4011 | crtc_id == resources->crtcs[i])) |
| 4012 | return i; |
| 4013 | |
| 4014 | ret = i; |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 4015 | } |
| 4016 | } |
| 4017 | |
Daniel Stone | 75487c2 | 2017-01-16 14:33:38 +0000 | [diff] [blame] | 4018 | return ret; |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 4019 | } |
| 4020 | |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 4021 | static void drm_output_fini_cursor_egl(struct drm_output *output) |
| 4022 | { |
| 4023 | unsigned int i; |
| 4024 | |
| 4025 | for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) { |
| 4026 | drm_fb_unref(output->gbm_cursor_fb[i]); |
| 4027 | output->gbm_cursor_fb[i] = NULL; |
| 4028 | } |
| 4029 | } |
| 4030 | |
| 4031 | static int |
| 4032 | drm_output_init_cursor_egl(struct drm_output *output, struct drm_backend *b) |
| 4033 | { |
| 4034 | unsigned int i; |
| 4035 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 4036 | /* No point creating cursors if we don't have a plane for them. */ |
| 4037 | if (!output->cursor_plane) |
| 4038 | return 0; |
| 4039 | |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 4040 | for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) { |
| 4041 | struct gbm_bo *bo; |
| 4042 | |
| 4043 | bo = gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height, |
| 4044 | GBM_FORMAT_ARGB8888, |
| 4045 | GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE); |
| 4046 | if (!bo) |
| 4047 | goto err; |
| 4048 | |
| 4049 | output->gbm_cursor_fb[i] = |
| 4050 | drm_fb_get_from_bo(bo, b, GBM_FORMAT_ARGB8888, |
| 4051 | BUFFER_CURSOR); |
| 4052 | if (!output->gbm_cursor_fb[i]) { |
| 4053 | gbm_bo_destroy(bo); |
| 4054 | goto err; |
| 4055 | } |
| 4056 | } |
| 4057 | |
| 4058 | return 0; |
| 4059 | |
| 4060 | err: |
| 4061 | weston_log("cursor buffers unavailable, using gl cursors\n"); |
| 4062 | b->cursors_are_broken = 1; |
| 4063 | drm_output_fini_cursor_egl(output); |
| 4064 | return -1; |
| 4065 | } |
| 4066 | |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 4067 | /* Init output state that depends on gl or gbm */ |
| 4068 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 4069 | 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] | 4070 | { |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 4071 | EGLint format[2] = { |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 4072 | output->gbm_format, |
| 4073 | fallback_format_for(output->gbm_format), |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 4074 | }; |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 4075 | int n_formats = 1; |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 4076 | |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 4077 | output->gbm_surface = gbm_surface_create(b->gbm, |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 4078 | output->base.current_mode->width, |
| 4079 | output->base.current_mode->height, |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 4080 | format[0], |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 4081 | GBM_BO_USE_SCANOUT | |
| 4082 | GBM_BO_USE_RENDERING); |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 4083 | if (!output->gbm_surface) { |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 4084 | weston_log("failed to create gbm surface\n"); |
| 4085 | return -1; |
| 4086 | } |
| 4087 | |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 4088 | if (format[1]) |
| 4089 | n_formats = 2; |
Miguel A. Vico | c095cde | 2016-05-18 17:43:00 +0200 | [diff] [blame] | 4090 | if (gl_renderer->output_window_create(&output->base, |
| 4091 | (EGLNativeWindowType)output->gbm_surface, |
| 4092 | output->gbm_surface, |
| 4093 | gl_renderer->opaque_attribs, |
| 4094 | format, |
| 4095 | n_formats) < 0) { |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 4096 | weston_log("failed to create gl renderer output state\n"); |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 4097 | gbm_surface_destroy(output->gbm_surface); |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 4098 | return -1; |
| 4099 | } |
| 4100 | |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 4101 | drm_output_init_cursor_egl(output, b); |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 4102 | |
| 4103 | return 0; |
| 4104 | } |
| 4105 | |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 4106 | static void |
| 4107 | drm_output_fini_egl(struct drm_output *output) |
| 4108 | { |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 4109 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
| 4110 | |
| 4111 | /* Destroying the GBM surface will destroy all our GBM buffers, |
| 4112 | * regardless of refcount. Ensure we destroy them here. */ |
| 4113 | if (!b->shutting_down && |
| 4114 | output->scanout_plane->state_cur->fb && |
| 4115 | output->scanout_plane->state_cur->fb->type == BUFFER_GBM_SURFACE) { |
| 4116 | drm_plane_state_free(output->scanout_plane->state_cur, true); |
| 4117 | output->scanout_plane->state_cur = |
| 4118 | drm_plane_state_alloc(NULL, output->scanout_plane); |
| 4119 | output->scanout_plane->state_cur->complete = true; |
| 4120 | } |
| 4121 | |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 4122 | gl_renderer->output_destroy(&output->base); |
| 4123 | gbm_surface_destroy(output->gbm_surface); |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 4124 | drm_output_fini_cursor_egl(output); |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 4125 | } |
| 4126 | |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 4127 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 4128 | 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] | 4129 | { |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 4130 | int w = output->base.current_mode->width; |
| 4131 | int h = output->base.current_mode->height; |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 4132 | uint32_t format = output->gbm_format; |
| 4133 | uint32_t pixman_format; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4134 | unsigned int i; |
| 4135 | |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 4136 | switch (format) { |
| 4137 | case GBM_FORMAT_XRGB8888: |
| 4138 | pixman_format = PIXMAN_x8r8g8b8; |
| 4139 | break; |
| 4140 | case GBM_FORMAT_RGB565: |
| 4141 | pixman_format = PIXMAN_r5g6b5; |
| 4142 | break; |
| 4143 | default: |
| 4144 | weston_log("Unsupported pixman format 0x%x\n", format); |
| 4145 | return -1; |
| 4146 | } |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4147 | |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 4148 | /* FIXME error checking */ |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4149 | for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) { |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 4150 | 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] | 4151 | if (!output->dumb[i]) |
| 4152 | goto err; |
| 4153 | |
| 4154 | output->image[i] = |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 4155 | pixman_image_create_bits(pixman_format, w, h, |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4156 | output->dumb[i]->map, |
| 4157 | output->dumb[i]->stride); |
| 4158 | if (!output->image[i]) |
| 4159 | goto err; |
| 4160 | } |
| 4161 | |
| 4162 | if (pixman_renderer_output_create(&output->base) < 0) |
| 4163 | goto err; |
| 4164 | |
| 4165 | pixman_region32_init_rect(&output->previous_damage, |
Alexander Larsson | 0b13506 | 2013-05-28 16:23:36 +0200 | [diff] [blame] | 4166 | 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] | 4167 | |
| 4168 | return 0; |
| 4169 | |
| 4170 | err: |
| 4171 | for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) { |
| 4172 | if (output->dumb[i]) |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 4173 | drm_fb_unref(output->dumb[i]); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4174 | if (output->image[i]) |
| 4175 | pixman_image_unref(output->image[i]); |
| 4176 | |
| 4177 | output->dumb[i] = NULL; |
| 4178 | output->image[i] = NULL; |
| 4179 | } |
| 4180 | |
| 4181 | return -1; |
| 4182 | } |
| 4183 | |
| 4184 | static void |
| 4185 | drm_output_fini_pixman(struct drm_output *output) |
| 4186 | { |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 4187 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4188 | unsigned int i; |
| 4189 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 4190 | /* Destroying the Pixman surface will destroy all our buffers, |
| 4191 | * regardless of refcount. Ensure we destroy them here. */ |
| 4192 | if (!b->shutting_down && |
| 4193 | output->scanout_plane->state_cur->fb && |
| 4194 | output->scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB) { |
| 4195 | drm_plane_state_free(output->scanout_plane->state_cur, true); |
| 4196 | output->scanout_plane->state_cur = |
| 4197 | drm_plane_state_alloc(NULL, output->scanout_plane); |
| 4198 | output->scanout_plane->state_cur->complete = true; |
| 4199 | } |
| 4200 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4201 | pixman_renderer_output_destroy(&output->base); |
| 4202 | pixman_region32_fini(&output->previous_damage); |
| 4203 | |
| 4204 | for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4205 | pixman_image_unref(output->image[i]); |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 4206 | drm_fb_unref(output->dumb[i]); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4207 | output->dumb[i] = NULL; |
| 4208 | output->image[i] = NULL; |
| 4209 | } |
| 4210 | } |
| 4211 | |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4212 | static void |
| 4213 | edid_parse_string(const uint8_t *data, char text[]) |
| 4214 | { |
| 4215 | int i; |
| 4216 | int replaced = 0; |
| 4217 | |
| 4218 | /* this is always 12 bytes, but we can't guarantee it's null |
| 4219 | * terminated or not junk. */ |
| 4220 | strncpy(text, (const char *) data, 12); |
| 4221 | |
Bryce Harrington | 9c7de16 | 2015-08-28 13:04:26 -0700 | [diff] [blame] | 4222 | /* guarantee our new string is null-terminated */ |
| 4223 | text[12] = '\0'; |
| 4224 | |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4225 | /* remove insane chars */ |
| 4226 | for (i = 0; text[i] != '\0'; i++) { |
| 4227 | if (text[i] == '\n' || |
| 4228 | text[i] == '\r') { |
| 4229 | text[i] = '\0'; |
| 4230 | break; |
| 4231 | } |
| 4232 | } |
| 4233 | |
| 4234 | /* ensure string is printable */ |
| 4235 | for (i = 0; text[i] != '\0'; i++) { |
| 4236 | if (!isprint(text[i])) { |
| 4237 | text[i] = '-'; |
| 4238 | replaced++; |
| 4239 | } |
| 4240 | } |
| 4241 | |
| 4242 | /* if the string is random junk, ignore the string */ |
| 4243 | if (replaced > 4) |
| 4244 | text[0] = '\0'; |
| 4245 | } |
| 4246 | |
| 4247 | #define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe |
| 4248 | #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc |
| 4249 | #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff |
| 4250 | #define EDID_OFFSET_DATA_BLOCKS 0x36 |
| 4251 | #define EDID_OFFSET_LAST_BLOCK 0x6c |
| 4252 | #define EDID_OFFSET_PNPID 0x08 |
| 4253 | #define EDID_OFFSET_SERIAL 0x0c |
| 4254 | |
| 4255 | static int |
| 4256 | edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length) |
| 4257 | { |
| 4258 | int i; |
| 4259 | uint32_t serial_number; |
| 4260 | |
| 4261 | /* check header */ |
| 4262 | if (length < 128) |
| 4263 | return -1; |
| 4264 | if (data[0] != 0x00 || data[1] != 0xff) |
| 4265 | return -1; |
| 4266 | |
| 4267 | /* decode the PNP ID from three 5 bit words packed into 2 bytes |
| 4268 | * /--08--\/--09--\ |
| 4269 | * 7654321076543210 |
| 4270 | * |\---/\---/\---/ |
| 4271 | * R C1 C2 C3 */ |
| 4272 | edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1; |
| 4273 | edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1; |
| 4274 | edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1; |
| 4275 | edid->pnp_id[3] = '\0'; |
| 4276 | |
| 4277 | /* maybe there isn't a ASCII serial number descriptor, so use this instead */ |
| 4278 | serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0]; |
| 4279 | serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100; |
| 4280 | serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000; |
| 4281 | serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000; |
| 4282 | if (serial_number > 0) |
| 4283 | sprintf(edid->serial_number, "%lu", (unsigned long) serial_number); |
| 4284 | |
| 4285 | /* parse EDID data */ |
| 4286 | for (i = EDID_OFFSET_DATA_BLOCKS; |
| 4287 | i <= EDID_OFFSET_LAST_BLOCK; |
| 4288 | i += 18) { |
| 4289 | /* ignore pixel clock data */ |
| 4290 | if (data[i] != 0) |
| 4291 | continue; |
| 4292 | if (data[i+2] != 0) |
| 4293 | continue; |
| 4294 | |
| 4295 | /* any useful blocks? */ |
| 4296 | if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) { |
| 4297 | edid_parse_string(&data[i+5], |
| 4298 | edid->monitor_name); |
| 4299 | } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) { |
| 4300 | edid_parse_string(&data[i+5], |
| 4301 | edid->serial_number); |
| 4302 | } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) { |
| 4303 | edid_parse_string(&data[i+5], |
| 4304 | edid->eisa_id); |
| 4305 | } |
| 4306 | } |
| 4307 | return 0; |
| 4308 | } |
| 4309 | |
Pekka Paalanen | 6f1866b | 2017-04-03 14:22:51 +0300 | [diff] [blame] | 4310 | /** Parse monitor make, model and serial from EDID |
| 4311 | * |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 4312 | * \param head The head whose \c drm_edid to fill in. |
Pekka Paalanen | 6f1866b | 2017-04-03 14:22:51 +0300 | [diff] [blame] | 4313 | * \param props The DRM connector properties to get the EDID from. |
| 4314 | * \param make[out] The monitor make (PNP ID). |
| 4315 | * \param model[out] The monitor model (name). |
| 4316 | * \param serial_number[out] The monitor serial number. |
| 4317 | * |
| 4318 | * Each of \c *make, \c *model and \c *serial_number are set only if the |
| 4319 | * information is found in the EDID. The pointers they are set to must not |
| 4320 | * be free()'d explicitly, instead they get implicitly freed when the |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 4321 | * \c drm_head is destroyed. |
Pekka Paalanen | 6f1866b | 2017-04-03 14:22:51 +0300 | [diff] [blame] | 4322 | */ |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4323 | static void |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 4324 | find_and_parse_output_edid(struct drm_head *head, |
Pekka Paalanen | 6f1866b | 2017-04-03 14:22:51 +0300 | [diff] [blame] | 4325 | drmModeObjectPropertiesPtr props, |
| 4326 | const char **make, |
| 4327 | const char **model, |
| 4328 | const char **serial_number) |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4329 | { |
| 4330 | drmModePropertyBlobPtr edid_blob = NULL; |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 4331 | uint32_t blob_id; |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4332 | int rc; |
| 4333 | |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 4334 | blob_id = |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 4335 | drm_property_get_value(&head->props_conn[WDRM_CONNECTOR_EDID], |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 4336 | props, 0); |
| 4337 | if (!blob_id) |
| 4338 | return; |
| 4339 | |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 4340 | edid_blob = drmModeGetPropertyBlob(head->backend->drm.fd, blob_id); |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4341 | if (!edid_blob) |
| 4342 | return; |
| 4343 | |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 4344 | rc = edid_parse(&head->edid, |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4345 | edid_blob->data, |
| 4346 | edid_blob->length); |
| 4347 | if (!rc) { |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 4348 | if (head->edid.pnp_id[0] != '\0') |
| 4349 | *make = head->edid.pnp_id; |
| 4350 | if (head->edid.monitor_name[0] != '\0') |
| 4351 | *model = head->edid.monitor_name; |
| 4352 | if (head->edid.serial_number[0] != '\0') |
| 4353 | *serial_number = head->edid.serial_number; |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4354 | } |
| 4355 | drmModeFreePropertyBlob(edid_blob); |
| 4356 | } |
| 4357 | |
Kristian Høgsberg | a30989a | 2013-05-23 17:23:15 -0400 | [diff] [blame] | 4358 | static int |
| 4359 | parse_modeline(const char *s, drmModeModeInfo *mode) |
| 4360 | { |
| 4361 | char hsync[16]; |
| 4362 | char vsync[16]; |
| 4363 | float fclock; |
| 4364 | |
Pekka Paalanen | dc4e3c6 | 2017-12-05 15:37:41 +0200 | [diff] [blame] | 4365 | memset(mode, 0, sizeof *mode); |
| 4366 | |
Kristian Høgsberg | a30989a | 2013-05-23 17:23:15 -0400 | [diff] [blame] | 4367 | mode->type = DRM_MODE_TYPE_USERDEF; |
| 4368 | mode->hskew = 0; |
| 4369 | mode->vscan = 0; |
| 4370 | mode->vrefresh = 0; |
| 4371 | mode->flags = 0; |
| 4372 | |
Rob Bradford | 307e09e | 2013-07-26 16:29:40 +0100 | [diff] [blame] | 4373 | 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] | 4374 | &fclock, |
| 4375 | &mode->hdisplay, |
| 4376 | &mode->hsync_start, |
| 4377 | &mode->hsync_end, |
| 4378 | &mode->htotal, |
| 4379 | &mode->vdisplay, |
| 4380 | &mode->vsync_start, |
| 4381 | &mode->vsync_end, |
| 4382 | &mode->vtotal, hsync, vsync) != 11) |
| 4383 | return -1; |
| 4384 | |
| 4385 | mode->clock = fclock * 1000; |
| 4386 | if (strcmp(hsync, "+hsync") == 0) |
| 4387 | mode->flags |= DRM_MODE_FLAG_PHSYNC; |
| 4388 | else if (strcmp(hsync, "-hsync") == 0) |
| 4389 | mode->flags |= DRM_MODE_FLAG_NHSYNC; |
| 4390 | else |
| 4391 | return -1; |
| 4392 | |
| 4393 | if (strcmp(vsync, "+vsync") == 0) |
| 4394 | mode->flags |= DRM_MODE_FLAG_PVSYNC; |
| 4395 | else if (strcmp(vsync, "-vsync") == 0) |
| 4396 | mode->flags |= DRM_MODE_FLAG_NVSYNC; |
| 4397 | else |
| 4398 | return -1; |
| 4399 | |
Emmanuel Gil Peyrot | a62138b | 2016-05-02 22:40:11 +0100 | [diff] [blame] | 4400 | snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f", |
| 4401 | mode->hdisplay, mode->vdisplay, fclock); |
| 4402 | |
Kristian Høgsberg | a30989a | 2013-05-23 17:23:15 -0400 | [diff] [blame] | 4403 | return 0; |
| 4404 | } |
| 4405 | |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 4406 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 4407 | setup_output_seat_constraint(struct drm_backend *b, |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 4408 | struct weston_output *output, |
| 4409 | const char *s) |
| 4410 | { |
| 4411 | if (strcmp(s, "") != 0) { |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 4412 | struct weston_pointer *pointer; |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 4413 | struct udev_seat *seat; |
| 4414 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 4415 | seat = udev_seat_get_named(&b->input, s); |
Derek Foreman | 0720ea3 | 2015-07-15 13:00:35 -0500 | [diff] [blame] | 4416 | if (!seat) |
| 4417 | return; |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 4418 | |
Derek Foreman | 0720ea3 | 2015-07-15 13:00:35 -0500 | [diff] [blame] | 4419 | seat->base.output = output; |
| 4420 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 4421 | pointer = weston_seat_get_pointer(&seat->base); |
| 4422 | if (pointer) |
| 4423 | weston_pointer_clamp(pointer, |
| 4424 | &pointer->x, |
| 4425 | &pointer->y); |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 4426 | } |
| 4427 | } |
| 4428 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4429 | static int |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 4430 | drm_output_attach_head(struct weston_output *output_base, |
| 4431 | struct weston_head *head_base) |
| 4432 | { |
| 4433 | if (wl_list_length(&output_base->head_list) >= MAX_CLONED_CONNECTORS) |
| 4434 | return -1; |
| 4435 | |
| 4436 | return 0; |
| 4437 | } |
| 4438 | |
| 4439 | static int |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4440 | 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] | 4441 | { |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 4442 | int ret = 0; |
| 4443 | |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 4444 | if (s == NULL) |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4445 | *gbm_format = default_value; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 4446 | else if (strcmp(s, "xrgb8888") == 0) |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4447 | *gbm_format = GBM_FORMAT_XRGB8888; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 4448 | else if (strcmp(s, "rgb565") == 0) |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4449 | *gbm_format = GBM_FORMAT_RGB565; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 4450 | else if (strcmp(s, "xrgb2101010") == 0) |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4451 | *gbm_format = GBM_FORMAT_XRGB2101010; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 4452 | else { |
| 4453 | weston_log("fatal: unrecognized pixel format: %s\n", s); |
| 4454 | ret = -1; |
| 4455 | } |
| 4456 | |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 4457 | return ret; |
| 4458 | } |
| 4459 | |
Pekka Paalanen | f005f25 | 2017-11-10 16:34:39 +0200 | [diff] [blame] | 4460 | static uint32_t |
| 4461 | u32distance(uint32_t a, uint32_t b) |
| 4462 | { |
| 4463 | if (a < b) |
| 4464 | return b - a; |
| 4465 | else |
| 4466 | return a - b; |
| 4467 | } |
| 4468 | |
| 4469 | /** Choose equivalent mode |
| 4470 | * |
| 4471 | * If the two modes are not equivalent, return NULL. |
| 4472 | * Otherwise return the mode that is more likely to work in place of both. |
| 4473 | * |
| 4474 | * None of the fuzzy matching criteria in this function have any justification. |
| 4475 | * |
| 4476 | * typedef struct _drmModeModeInfo { |
| 4477 | * uint32_t clock; |
| 4478 | * uint16_t hdisplay, hsync_start, hsync_end, htotal, hskew; |
| 4479 | * uint16_t vdisplay, vsync_start, vsync_end, vtotal, vscan; |
| 4480 | * |
| 4481 | * uint32_t vrefresh; |
| 4482 | * |
| 4483 | * uint32_t flags; |
| 4484 | * uint32_t type; |
| 4485 | * char name[DRM_DISPLAY_MODE_LEN]; |
| 4486 | * } drmModeModeInfo, *drmModeModeInfoPtr; |
| 4487 | */ |
| 4488 | static const drmModeModeInfo * |
| 4489 | drm_mode_pick_equivalent(const drmModeModeInfo *a, const drmModeModeInfo *b) |
| 4490 | { |
| 4491 | uint32_t refresh_a, refresh_b; |
| 4492 | |
| 4493 | if (a->hdisplay != b->hdisplay || a->vdisplay != b->vdisplay) |
| 4494 | return NULL; |
| 4495 | |
| 4496 | if (a->flags != b->flags) |
| 4497 | return NULL; |
| 4498 | |
| 4499 | /* kHz */ |
| 4500 | if (u32distance(a->clock, b->clock) > 500) |
| 4501 | return NULL; |
| 4502 | |
| 4503 | refresh_a = drm_refresh_rate_mHz(a); |
| 4504 | refresh_b = drm_refresh_rate_mHz(b); |
| 4505 | if (u32distance(refresh_a, refresh_b) > 50) |
| 4506 | return NULL; |
| 4507 | |
| 4508 | if ((a->type ^ b->type) & DRM_MODE_TYPE_PREFERRED) { |
| 4509 | if (a->type & DRM_MODE_TYPE_PREFERRED) |
| 4510 | return a; |
| 4511 | else |
| 4512 | return b; |
| 4513 | } |
| 4514 | |
| 4515 | return a; |
| 4516 | } |
| 4517 | |
| 4518 | /* If the given mode info is not already in the list, add it. |
| 4519 | * If it is in the list, either keep the existing or replace it, |
| 4520 | * depending on which one is "better". |
| 4521 | */ |
| 4522 | static int |
| 4523 | drm_output_try_add_mode(struct drm_output *output, const drmModeModeInfo *info) |
| 4524 | { |
| 4525 | struct weston_mode *base; |
| 4526 | struct drm_mode *mode; |
| 4527 | struct drm_backend *backend; |
| 4528 | const drmModeModeInfo *chosen = NULL; |
| 4529 | |
| 4530 | assert(info); |
| 4531 | |
| 4532 | wl_list_for_each(base, &output->base.mode_list, link) { |
| 4533 | mode = to_drm_mode(base); |
| 4534 | chosen = drm_mode_pick_equivalent(&mode->mode_info, info); |
| 4535 | if (chosen) |
| 4536 | break; |
| 4537 | } |
| 4538 | |
| 4539 | if (chosen == info) { |
| 4540 | backend = to_drm_backend(output->base.compositor); |
| 4541 | drm_output_destroy_mode(backend, mode); |
| 4542 | chosen = NULL; |
| 4543 | } |
| 4544 | |
| 4545 | if (!chosen) { |
| 4546 | mode = drm_output_add_mode(output, info); |
| 4547 | if (!mode) |
| 4548 | return -1; |
| 4549 | } |
| 4550 | /* else { the equivalent mode is already in the list } */ |
| 4551 | |
| 4552 | return 0; |
| 4553 | } |
| 4554 | |
Pekka Paalanen | 4be2485 | 2017-09-11 15:01:12 +0300 | [diff] [blame] | 4555 | /** Rewrite the output's mode list |
| 4556 | * |
| 4557 | * @param output The output. |
| 4558 | * @return 0 on success, -1 on failure. |
| 4559 | * |
| 4560 | * Destroy all existing modes in the list, and reconstruct a new list from |
| 4561 | * scratch, based on the currently attached heads. |
| 4562 | * |
| 4563 | * On failure the output's mode list may contain some modes. |
| 4564 | */ |
| 4565 | static int |
| 4566 | drm_output_update_modelist_from_heads(struct drm_output *output) |
| 4567 | { |
| 4568 | struct drm_backend *backend = to_drm_backend(output->base.compositor); |
| 4569 | struct weston_head *head_base; |
| 4570 | struct drm_head *head; |
Pekka Paalanen | 4be2485 | 2017-09-11 15:01:12 +0300 | [diff] [blame] | 4571 | int i; |
Pekka Paalanen | f005f25 | 2017-11-10 16:34:39 +0200 | [diff] [blame] | 4572 | int ret; |
Pekka Paalanen | 4be2485 | 2017-09-11 15:01:12 +0300 | [diff] [blame] | 4573 | |
| 4574 | assert(!output->base.enabled); |
| 4575 | |
| 4576 | drm_mode_list_destroy(backend, &output->base.mode_list); |
| 4577 | |
Pekka Paalanen | f005f25 | 2017-11-10 16:34:39 +0200 | [diff] [blame] | 4578 | wl_list_for_each(head_base, &output->base.head_list, output_link) { |
| 4579 | head = to_drm_head(head_base); |
| 4580 | for (i = 0; i < head->connector->count_modes; i++) { |
| 4581 | ret = drm_output_try_add_mode(output, |
| 4582 | &head->connector->modes[i]); |
| 4583 | if (ret < 0) |
| 4584 | return -1; |
| 4585 | } |
Pekka Paalanen | 4be2485 | 2017-09-11 15:01:12 +0300 | [diff] [blame] | 4586 | } |
| 4587 | |
| 4588 | return 0; |
| 4589 | } |
| 4590 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4591 | /** |
| 4592 | * Choose suitable mode for an output |
| 4593 | * |
| 4594 | * Find the most suitable mode to use for initial setup (or reconfiguration on |
| 4595 | * hotplug etc) for a DRM output. |
| 4596 | * |
| 4597 | * @param output DRM output to choose mode for |
| 4598 | * @param kind Strategy and preference to use when choosing mode |
| 4599 | * @param width Desired width for this output |
| 4600 | * @param height Desired height for this output |
| 4601 | * @param current_mode Mode currently being displayed on this output |
| 4602 | * @param modeline Manually-entered mode (may be NULL) |
| 4603 | * @returns A mode from the output's mode list, or NULL if none available |
| 4604 | */ |
| 4605 | static struct drm_mode * |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4606 | drm_output_choose_initial_mode(struct drm_backend *backend, |
| 4607 | struct drm_output *output, |
| 4608 | enum weston_drm_backend_output_mode mode, |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4609 | const char *modeline, |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4610 | const drmModeModeInfo *current_mode) |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4611 | { |
| 4612 | struct drm_mode *preferred = NULL; |
| 4613 | struct drm_mode *current = NULL; |
| 4614 | struct drm_mode *configured = NULL; |
| 4615 | struct drm_mode *best = NULL; |
| 4616 | struct drm_mode *drm_mode; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4617 | drmModeModeInfo drm_modeline; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4618 | int32_t width = 0; |
| 4619 | int32_t height = 0; |
Fabien Dessenne | 2d66a7d | 2017-01-17 17:17:21 +0100 | [diff] [blame] | 4620 | uint32_t refresh = 0; |
| 4621 | int n; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4622 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4623 | if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) { |
Fabien Dessenne | 2d66a7d | 2017-01-17 17:17:21 +0100 | [diff] [blame] | 4624 | n = sscanf(modeline, "%dx%d@%d", &width, &height, &refresh); |
| 4625 | if (n != 2 && n != 3) { |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4626 | width = -1; |
| 4627 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4628 | if (parse_modeline(modeline, &drm_modeline) == 0) { |
| 4629 | configured = drm_output_add_mode(output, &drm_modeline); |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4630 | if (!configured) |
| 4631 | return NULL; |
| 4632 | } else { |
| 4633 | weston_log("Invalid modeline \"%s\" for output %s\n", |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4634 | modeline, output->base.name); |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4635 | } |
| 4636 | } |
| 4637 | } |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4638 | |
| 4639 | 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] | 4640 | if (width == drm_mode->base.width && |
Fabien Dessenne | 2d66a7d | 2017-01-17 17:17:21 +0100 | [diff] [blame] | 4641 | height == drm_mode->base.height && |
| 4642 | (refresh == 0 || refresh == drm_mode->mode_info.vrefresh)) |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4643 | configured = drm_mode; |
| 4644 | |
comic fans | 7a5c562 | 2016-03-17 14:29:27 +0200 | [diff] [blame] | 4645 | if (memcmp(current_mode, &drm_mode->mode_info, |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4646 | sizeof *current_mode) == 0) |
| 4647 | current = drm_mode; |
| 4648 | |
| 4649 | if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED) |
| 4650 | preferred = drm_mode; |
| 4651 | |
| 4652 | best = drm_mode; |
| 4653 | } |
| 4654 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4655 | if (current == NULL && current_mode->clock != 0) { |
| 4656 | current = drm_output_add_mode(output, current_mode); |
| 4657 | if (!current) |
| 4658 | return NULL; |
| 4659 | } |
| 4660 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4661 | if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT) |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4662 | configured = current; |
| 4663 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4664 | if (configured) |
| 4665 | return configured; |
| 4666 | |
| 4667 | if (preferred) |
| 4668 | return preferred; |
| 4669 | |
| 4670 | if (current) |
| 4671 | return current; |
| 4672 | |
| 4673 | if (best) |
| 4674 | return best; |
| 4675 | |
| 4676 | weston_log("no available modes for %s\n", output->base.name); |
| 4677 | return NULL; |
| 4678 | } |
| 4679 | |
Pekka Paalanen | eee580b | 2014-06-04 16:43:06 +0300 | [diff] [blame] | 4680 | static int |
| 4681 | connector_get_current_mode(drmModeConnector *connector, int drm_fd, |
| 4682 | drmModeModeInfo *mode) |
| 4683 | { |
| 4684 | drmModeEncoder *encoder; |
| 4685 | drmModeCrtc *crtc; |
| 4686 | |
| 4687 | /* Get the current mode on the crtc that's currently driving |
| 4688 | * this connector. */ |
| 4689 | encoder = drmModeGetEncoder(drm_fd, connector->encoder_id); |
| 4690 | memset(mode, 0, sizeof *mode); |
| 4691 | if (encoder != NULL) { |
| 4692 | crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id); |
| 4693 | drmModeFreeEncoder(encoder); |
| 4694 | if (crtc == NULL) |
| 4695 | return -1; |
| 4696 | if (crtc->mode_valid) |
| 4697 | *mode = crtc->mode; |
| 4698 | drmModeFreeCrtc(crtc); |
| 4699 | } |
| 4700 | |
| 4701 | return 0; |
| 4702 | } |
| 4703 | |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 4704 | static int |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4705 | drm_output_set_mode(struct weston_output *base, |
| 4706 | enum weston_drm_backend_output_mode mode, |
| 4707 | const char *modeline) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 4708 | { |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4709 | struct drm_output *output = to_drm_output(base); |
| 4710 | struct drm_backend *b = to_drm_backend(base->compositor); |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 4711 | 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] | 4712 | |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 4713 | struct drm_mode *current; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 4714 | |
Pekka Paalanen | 4be2485 | 2017-09-11 15:01:12 +0300 | [diff] [blame] | 4715 | if (drm_output_update_modelist_from_heads(output) < 0) |
| 4716 | return -1; |
| 4717 | |
Pekka Paalanen | 13d233e | 2017-09-11 14:06:11 +0300 | [diff] [blame] | 4718 | current = drm_output_choose_initial_mode(b, output, mode, modeline, |
| 4719 | &head->inherited_mode); |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4720 | if (!current) |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 4721 | return -1; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4722 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4723 | output->base.current_mode = ¤t->base; |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 4724 | output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT; |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame] | 4725 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4726 | /* Set native_ fields, so weston_output_mode_switch_to_native() works */ |
| 4727 | output->base.native_mode = output->base.current_mode; |
| 4728 | output->base.native_scale = output->base.current_scale; |
| 4729 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4730 | return 0; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4731 | } |
| 4732 | |
| 4733 | static void |
| 4734 | drm_output_set_gbm_format(struct weston_output *base, |
| 4735 | const char *gbm_format) |
| 4736 | { |
| 4737 | struct drm_output *output = to_drm_output(base); |
| 4738 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 4739 | |
| 4740 | if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1) |
| 4741 | output->gbm_format = b->gbm_format; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 4742 | |
| 4743 | /* Without universal planes, we can't discover which formats are |
| 4744 | * supported by the primary plane; we just hope that the GBM format |
| 4745 | * works. */ |
| 4746 | if (!b->universal_planes) |
| 4747 | output->scanout_plane->formats[0] = output->gbm_format; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4748 | } |
| 4749 | |
| 4750 | static void |
| 4751 | drm_output_set_seat(struct weston_output *base, |
| 4752 | const char *seat) |
| 4753 | { |
| 4754 | struct drm_output *output = to_drm_output(base); |
| 4755 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 4756 | |
| 4757 | setup_output_seat_constraint(b, &output->base, |
| 4758 | seat ? seat : ""); |
| 4759 | } |
| 4760 | |
| 4761 | static int |
Pekka Paalanen | c4db6f7 | 2017-09-05 16:37:03 +0300 | [diff] [blame] | 4762 | drm_output_init_gamma_size(struct drm_output *output) |
| 4763 | { |
| 4764 | struct drm_backend *backend = to_drm_backend(output->base.compositor); |
| 4765 | drmModeCrtc *crtc; |
| 4766 | |
| 4767 | assert(output->base.compositor); |
| 4768 | assert(output->crtc_id != 0); |
| 4769 | crtc = drmModeGetCrtc(backend->drm.fd, output->crtc_id); |
| 4770 | if (!crtc) |
| 4771 | return -1; |
| 4772 | |
| 4773 | output->base.gamma_size = crtc->gamma_size; |
| 4774 | |
| 4775 | drmModeFreeCrtc(crtc); |
| 4776 | |
| 4777 | return 0; |
| 4778 | } |
| 4779 | |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 4780 | /** Allocate a CRTC for the output |
| 4781 | * |
| 4782 | * @param output The output with no allocated CRTC. |
| 4783 | * @param resources DRM KMS resources. |
| 4784 | * @param connector The DRM KMS connector data. |
| 4785 | * @return 0 on success, -1 on failure. |
| 4786 | * |
| 4787 | * Finds a free CRTC that can drive the given connector, reserves the CRTC |
| 4788 | * for the output, and loads the CRTC properties. |
| 4789 | * |
| 4790 | * Populates the cursor and scanout planes. |
| 4791 | * |
| 4792 | * On failure, the output remains without a CRTC. |
| 4793 | */ |
| 4794 | static int |
| 4795 | drm_output_init_crtc(struct drm_output *output, |
| 4796 | drmModeRes *resources, drmModeConnector *connector) |
| 4797 | { |
| 4798 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
| 4799 | drmModeObjectPropertiesPtr props; |
| 4800 | int i; |
| 4801 | |
| 4802 | assert(output->crtc_id == 0); |
| 4803 | |
| 4804 | i = find_crtc_for_connector(b, resources, connector); |
| 4805 | if (i < 0) { |
| 4806 | weston_log("No usable crtc/encoder pair for connector.\n"); |
| 4807 | return -1; |
| 4808 | } |
| 4809 | |
| 4810 | output->crtc_id = resources->crtcs[i]; |
| 4811 | output->pipe = i; |
| 4812 | |
| 4813 | props = drmModeObjectGetProperties(b->drm.fd, output->crtc_id, |
| 4814 | DRM_MODE_OBJECT_CRTC); |
| 4815 | if (!props) { |
| 4816 | weston_log("failed to get CRTC properties\n"); |
| 4817 | goto err_crtc; |
| 4818 | } |
| 4819 | drm_property_info_populate(b, crtc_props, output->props_crtc, |
| 4820 | WDRM_CRTC__COUNT, props); |
| 4821 | drmModeFreeObjectProperties(props); |
| 4822 | |
| 4823 | output->scanout_plane = |
| 4824 | drm_output_find_special_plane(b, output, |
| 4825 | WDRM_PLANE_TYPE_PRIMARY); |
| 4826 | if (!output->scanout_plane) { |
| 4827 | weston_log("Failed to find primary plane for output %s\n", |
| 4828 | output->base.name); |
| 4829 | goto err_crtc; |
| 4830 | } |
| 4831 | |
| 4832 | /* Failing to find a cursor plane is not fatal, as we'll fall back |
| 4833 | * to software cursor. */ |
| 4834 | output->cursor_plane = |
| 4835 | drm_output_find_special_plane(b, output, |
| 4836 | WDRM_PLANE_TYPE_CURSOR); |
| 4837 | |
Pekka Paalanen | 663d5e9 | 2017-09-08 13:32:40 +0300 | [diff] [blame] | 4838 | wl_array_remove_uint32(&b->unused_crtcs, output->crtc_id); |
| 4839 | |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 4840 | return 0; |
| 4841 | |
| 4842 | err_crtc: |
| 4843 | output->crtc_id = 0; |
| 4844 | output->pipe = 0; |
| 4845 | |
| 4846 | return -1; |
| 4847 | } |
| 4848 | |
| 4849 | /** Free the CRTC from the output |
| 4850 | * |
| 4851 | * @param output The output whose CRTC to deallocate. |
| 4852 | * |
| 4853 | * The CRTC reserved for the given output becomes free to use again. |
| 4854 | */ |
| 4855 | static void |
| 4856 | drm_output_fini_crtc(struct drm_output *output) |
| 4857 | { |
| 4858 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Pekka Paalanen | 663d5e9 | 2017-09-08 13:32:40 +0300 | [diff] [blame] | 4859 | uint32_t *unused; |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 4860 | |
| 4861 | if (!b->universal_planes && !b->shutting_down) { |
| 4862 | /* With universal planes, the 'special' planes are allocated at |
| 4863 | * startup, freed at shutdown, and live on the plane list in |
| 4864 | * between. We want the planes to continue to exist and be freed |
| 4865 | * up for other outputs. |
| 4866 | * |
| 4867 | * Without universal planes, our special planes are |
| 4868 | * pseudo-planes allocated at output creation, freed at output |
| 4869 | * destruction, and not usable by other outputs. |
| 4870 | * |
| 4871 | * On the other hand, if the compositor is already shutting down, |
| 4872 | * the plane has already been destroyed. |
| 4873 | */ |
| 4874 | if (output->cursor_plane) |
| 4875 | drm_plane_destroy(output->cursor_plane); |
| 4876 | if (output->scanout_plane) |
| 4877 | drm_plane_destroy(output->scanout_plane); |
| 4878 | } |
| 4879 | |
| 4880 | drm_property_info_free(output->props_crtc, WDRM_CRTC__COUNT); |
Pekka Paalanen | 663d5e9 | 2017-09-08 13:32:40 +0300 | [diff] [blame] | 4881 | |
| 4882 | assert(output->crtc_id != 0); |
| 4883 | |
| 4884 | unused = wl_array_add(&b->unused_crtcs, sizeof(*unused)); |
| 4885 | *unused = output->crtc_id; |
| 4886 | |
| 4887 | /* Force resetting unused CRTCs */ |
| 4888 | b->state_invalid = true; |
| 4889 | |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 4890 | output->crtc_id = 0; |
| 4891 | output->cursor_plane = NULL; |
| 4892 | output->scanout_plane = NULL; |
| 4893 | } |
| 4894 | |
Pekka Paalanen | c0eb254 | 2017-11-15 13:37:18 +0200 | [diff] [blame^] | 4895 | static void |
| 4896 | drm_output_print_modes(struct drm_output *output) |
| 4897 | { |
| 4898 | struct weston_mode *m; |
| 4899 | struct drm_mode *dm; |
| 4900 | |
| 4901 | wl_list_for_each(m, &output->base.mode_list, link) { |
| 4902 | dm = to_drm_mode(m); |
| 4903 | |
| 4904 | weston_log_continue(STAMP_SPACE "%dx%d@%.1f%s%s, %.1f MHz\n", |
| 4905 | m->width, m->height, m->refresh / 1000.0, |
| 4906 | m->flags & WL_OUTPUT_MODE_PREFERRED ? |
| 4907 | ", preferred" : "", |
| 4908 | m->flags & WL_OUTPUT_MODE_CURRENT ? |
| 4909 | ", current" : "", |
| 4910 | dm->mode_info.clock / 1000.0); |
| 4911 | } |
| 4912 | } |
| 4913 | |
Pekka Paalanen | c4db6f7 | 2017-09-05 16:37:03 +0300 | [diff] [blame] | 4914 | static int |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4915 | drm_output_enable(struct weston_output *base) |
| 4916 | { |
| 4917 | struct drm_output *output = to_drm_output(base); |
| 4918 | struct drm_backend *b = to_drm_backend(base->compositor); |
Pekka Paalanen | ce72424 | 2017-09-04 12:21:24 +0300 | [diff] [blame] | 4919 | struct drm_head *head = to_drm_head(weston_output_get_first_head(base)); |
Pekka Paalanen | 663d5e9 | 2017-09-08 13:32:40 +0300 | [diff] [blame] | 4920 | drmModeRes *resources; |
| 4921 | int ret; |
| 4922 | |
| 4923 | resources = drmModeGetResources(b->drm.fd); |
| 4924 | if (!resources) { |
| 4925 | weston_log("drmModeGetResources failed\n"); |
| 4926 | return -1; |
| 4927 | } |
| 4928 | ret = drm_output_init_crtc(output, resources, head->connector); |
| 4929 | drmModeFreeResources(resources); |
| 4930 | if (ret < 0) |
| 4931 | return -1; |
| 4932 | |
| 4933 | if (drm_output_init_gamma_size(output) < 0) |
| 4934 | goto err; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4935 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 4936 | if (b->pageflip_timeout) |
| 4937 | drm_output_pageflip_timer_create(output); |
| 4938 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 4939 | if (b->use_pixman) { |
| 4940 | if (drm_output_init_pixman(output, b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4941 | weston_log("Failed to init output pixman state\n"); |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 4942 | goto err; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4943 | } |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 4944 | } else if (drm_output_init_egl(output, b) < 0) { |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 4945 | weston_log("Failed to init output gl state\n"); |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 4946 | goto err; |
Kristian Høgsberg | 1d1e0a5 | 2012-10-21 13:29:26 -0400 | [diff] [blame] | 4947 | } |
Kristian Høgsberg | 8e1f77f | 2012-05-03 11:39:35 -0400 | [diff] [blame] | 4948 | |
Pekka Paalanen | f8b850d | 2017-11-15 12:51:01 +0200 | [diff] [blame] | 4949 | drm_output_init_backlight(output); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 4950 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 4951 | output->base.start_repaint_loop = drm_output_start_repaint_loop; |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 4952 | output->base.repaint = drm_output_repaint; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 4953 | output->base.assign_planes = drm_assign_planes; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 4954 | output->base.set_dpms = drm_set_dpms; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 4955 | output->base.switch_mode = drm_output_switch_mode; |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 4956 | output->base.set_gamma = drm_output_set_gamma; |
| 4957 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 4958 | if (output->cursor_plane) |
| 4959 | weston_compositor_stack_plane(b->compositor, |
| 4960 | &output->cursor_plane->base, |
| 4961 | NULL); |
| 4962 | else |
| 4963 | b->cursors_are_broken = 1; |
| 4964 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 4965 | weston_compositor_stack_plane(b->compositor, |
| 4966 | &output->scanout_plane->base, |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 4967 | &b->compositor->primary_plane); |
Ander Conselvan de Oliveira | 8ad1982 | 2013-03-05 17:30:27 +0200 | [diff] [blame] | 4968 | |
Pekka Paalanen | c0eb254 | 2017-11-15 13:37:18 +0200 | [diff] [blame^] | 4969 | weston_log("Output %s (crtc %d) video modes:\n", |
| 4970 | output->base.name, output->crtc_id); |
| 4971 | drm_output_print_modes(output); |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 4972 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 4973 | return 0; |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 4974 | |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 4975 | err: |
Pekka Paalanen | 663d5e9 | 2017-09-08 13:32:40 +0300 | [diff] [blame] | 4976 | drm_output_fini_crtc(output); |
| 4977 | |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 4978 | return -1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 4979 | } |
| 4980 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 4981 | static void |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4982 | drm_output_deinit(struct weston_output *base) |
| 4983 | { |
| 4984 | struct drm_output *output = to_drm_output(base); |
| 4985 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 4986 | |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 4987 | if (b->use_pixman) |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4988 | drm_output_fini_pixman(output); |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 4989 | else |
| 4990 | drm_output_fini_egl(output); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4991 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 4992 | /* Since our planes are no longer in use anywhere, remove their base |
| 4993 | * weston_plane's link from the plane stacking list, unless we're |
| 4994 | * shutting down, in which case the plane has already been |
| 4995 | * destroyed. */ |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 4996 | if (!b->shutting_down) { |
| 4997 | wl_list_remove(&output->scanout_plane->base.link); |
| 4998 | wl_list_init(&output->scanout_plane->base.link); |
| 4999 | |
| 5000 | if (output->cursor_plane) { |
| 5001 | wl_list_remove(&output->cursor_plane->base.link); |
| 5002 | wl_list_init(&output->cursor_plane->base.link); |
| 5003 | /* Turn off hardware cursor */ |
| 5004 | drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0); |
| 5005 | } |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 5006 | } |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 5007 | |
Pekka Paalanen | 663d5e9 | 2017-09-08 13:32:40 +0300 | [diff] [blame] | 5008 | drm_output_fini_crtc(output); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5009 | } |
| 5010 | |
| 5011 | static void |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 5012 | drm_head_destroy(struct drm_head *head); |
| 5013 | |
| 5014 | static void |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5015 | drm_output_destroy(struct weston_output *base) |
| 5016 | { |
| 5017 | struct drm_output *output = to_drm_output(base); |
| 5018 | struct drm_backend *b = to_drm_backend(base->compositor); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5019 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 5020 | if (output->page_flip_pending || output->vblank_pending || |
| 5021 | output->atomic_complete_pending) { |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5022 | output->destroy_pending = 1; |
| 5023 | weston_log("destroy output while page flip pending\n"); |
| 5024 | return; |
| 5025 | } |
| 5026 | |
| 5027 | if (output->base.enabled) |
| 5028 | drm_output_deinit(&output->base); |
| 5029 | |
Pekka Paalanen | 383b3af | 2017-09-11 14:40:48 +0300 | [diff] [blame] | 5030 | drm_mode_list_destroy(b, &output->base.mode_list); |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 5031 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 5032 | if (output->pageflip_timer) |
| 5033 | wl_event_source_remove(output->pageflip_timer); |
| 5034 | |
Pekka Paalanen | ae6d35d | 2017-08-16 12:07:14 +0300 | [diff] [blame] | 5035 | weston_output_release(&output->base); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5036 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 5037 | assert(!output->state_last); |
| 5038 | drm_output_state_free(output->state_cur); |
| 5039 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5040 | free(output); |
| 5041 | } |
| 5042 | |
| 5043 | static int |
| 5044 | drm_output_disable(struct weston_output *base) |
| 5045 | { |
| 5046 | struct drm_output *output = to_drm_output(base); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5047 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 5048 | if (output->page_flip_pending || output->vblank_pending || |
| 5049 | output->atomic_complete_pending) { |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5050 | output->disable_pending = 1; |
| 5051 | return -1; |
| 5052 | } |
| 5053 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 5054 | weston_log("Disabling output %s\n", output->base.name); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 5055 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5056 | if (output->base.enabled) |
| 5057 | drm_output_deinit(&output->base); |
| 5058 | |
| 5059 | output->disable_pending = 0; |
| 5060 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5061 | return 0; |
| 5062 | } |
| 5063 | |
| 5064 | /** |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 5065 | * Update the list of unused connectors and CRTCs |
| 5066 | * |
Pekka Paalanen | eacec81 | 2017-09-12 13:43:51 +0300 | [diff] [blame] | 5067 | * This keeps the unused_crtc arrays up to date. |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 5068 | * |
| 5069 | * @param b Weston backend structure |
| 5070 | * @param resources DRM resources for this device |
| 5071 | */ |
| 5072 | static void |
| 5073 | drm_backend_update_unused_outputs(struct drm_backend *b, drmModeRes *resources) |
| 5074 | { |
| 5075 | int i; |
| 5076 | |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 5077 | wl_array_release(&b->unused_crtcs); |
| 5078 | wl_array_init(&b->unused_crtcs); |
| 5079 | |
| 5080 | for (i = 0; i < resources->count_crtcs; i++) { |
| 5081 | struct drm_output *output; |
| 5082 | uint32_t *crtc_id; |
| 5083 | |
| 5084 | output = drm_output_find_by_crtc(b, resources->crtcs[i]); |
| 5085 | if (output && output->base.enabled) |
| 5086 | continue; |
| 5087 | |
| 5088 | crtc_id = wl_array_add(&b->unused_crtcs, sizeof(*crtc_id)); |
| 5089 | *crtc_id = resources->crtcs[i]; |
| 5090 | } |
| 5091 | } |
| 5092 | |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 5093 | /** Replace connector data and monitor information |
| 5094 | * |
| 5095 | * @param head The head to update. |
| 5096 | * @param connector The connector data to be owned by the head, must match |
| 5097 | * the head's connector ID. |
| 5098 | * @return 0 on success, -1 on failure. |
| 5099 | * |
| 5100 | * Takes ownership of @c connector on success, not on failure. |
| 5101 | * |
| 5102 | * May schedule a heads changed call. |
| 5103 | */ |
| 5104 | static int |
| 5105 | drm_head_assign_connector_info(struct drm_head *head, |
| 5106 | drmModeConnector *connector) |
| 5107 | { |
| 5108 | drmModeObjectProperties *props; |
| 5109 | const char *make = "unknown"; |
| 5110 | const char *model = "unknown"; |
| 5111 | const char *serial_number = "unknown"; |
| 5112 | |
| 5113 | assert(connector); |
| 5114 | assert(head->connector_id == connector->connector_id); |
| 5115 | |
| 5116 | props = drmModeObjectGetProperties(head->backend->drm.fd, |
| 5117 | head->connector_id, |
| 5118 | DRM_MODE_OBJECT_CONNECTOR); |
| 5119 | if (!props) { |
| 5120 | weston_log("Error: failed to get connector '%s' properties\n", |
| 5121 | head->base.name); |
| 5122 | return -1; |
| 5123 | } |
| 5124 | |
| 5125 | if (head->connector) |
| 5126 | drmModeFreeConnector(head->connector); |
| 5127 | head->connector = connector; |
| 5128 | |
| 5129 | drm_property_info_populate(head->backend, connector_props, |
| 5130 | head->props_conn, |
| 5131 | WDRM_CONNECTOR__COUNT, props); |
| 5132 | find_and_parse_output_edid(head, props, &make, &model, &serial_number); |
| 5133 | weston_head_set_monitor_strings(&head->base, make, model, serial_number); |
| 5134 | weston_head_set_subpixel(&head->base, |
| 5135 | drm_subpixel_to_wayland(head->connector->subpixel)); |
| 5136 | |
| 5137 | weston_head_set_physical_size(&head->base, head->connector->mmWidth, |
| 5138 | head->connector->mmHeight); |
| 5139 | |
| 5140 | drmModeFreeObjectProperties(props); |
| 5141 | |
| 5142 | /* Unknown connection status is assumed disconnected. */ |
| 5143 | weston_head_set_connection_status(&head->base, |
| 5144 | head->connector->connection == DRM_MODE_CONNECTED); |
| 5145 | |
| 5146 | return 0; |
| 5147 | } |
| 5148 | |
Pekka Paalanen | 456dc73 | 2017-11-09 15:10:11 +0200 | [diff] [blame] | 5149 | static void |
| 5150 | drm_head_log_info(struct drm_head *head, const char *msg) |
| 5151 | { |
| 5152 | if (head->base.connected) { |
| 5153 | weston_log("DRM: head '%s' %s, connector %d is connected, " |
| 5154 | "EDID make '%s', model '%s', serial '%s'\n", |
| 5155 | head->base.name, msg, head->connector_id, |
| 5156 | head->base.make, head->base.model, |
| 5157 | head->base.serial_number ?: ""); |
| 5158 | } else { |
| 5159 | weston_log("DRM: head '%s' %s, connector %d is disconnected.\n", |
| 5160 | head->base.name, msg, head->connector_id); |
| 5161 | } |
| 5162 | } |
| 5163 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5164 | /** Update connector and monitor information |
| 5165 | * |
| 5166 | * @param head The head to update. |
| 5167 | * |
| 5168 | * Re-reads the DRM property lists for the connector and updates monitor |
| 5169 | * information and connection status. This may schedule a heads changed call |
| 5170 | * to the user. |
| 5171 | */ |
| 5172 | static void |
| 5173 | drm_head_update_info(struct drm_head *head) |
| 5174 | { |
| 5175 | drmModeConnector *connector; |
| 5176 | |
| 5177 | connector = drmModeGetConnector(head->backend->drm.fd, |
| 5178 | head->connector_id); |
| 5179 | if (!connector) { |
| 5180 | weston_log("DRM: getting connector info for '%s' failed.\n", |
| 5181 | head->base.name); |
| 5182 | return; |
| 5183 | } |
| 5184 | |
| 5185 | if (drm_head_assign_connector_info(head, connector) < 0) |
| 5186 | drmModeFreeConnector(connector); |
Pekka Paalanen | 456dc73 | 2017-11-09 15:10:11 +0200 | [diff] [blame] | 5187 | |
| 5188 | if (head->base.device_changed) |
| 5189 | drm_head_log_info(head, "updated"); |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5190 | } |
| 5191 | |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 5192 | /** |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 5193 | * Create a Weston head for a connector |
| 5194 | * |
| 5195 | * Given a DRM connector, create a matching drm_head structure and add it |
| 5196 | * to Weston's head list. |
| 5197 | * |
| 5198 | * @param b Weston backend structure |
| 5199 | * @param connector_id DRM connector ID for the head |
| 5200 | * @param drm_device udev device pointer |
| 5201 | * @returns The new head, or NULL on failure. |
| 5202 | */ |
| 5203 | static struct drm_head * |
| 5204 | drm_head_create(struct drm_backend *backend, uint32_t connector_id, |
| 5205 | struct udev_device *drm_device) |
| 5206 | { |
| 5207 | struct drm_head *head; |
| 5208 | drmModeConnector *connector; |
| 5209 | char *name; |
| 5210 | |
| 5211 | head = zalloc(sizeof *head); |
| 5212 | if (!head) |
| 5213 | return NULL; |
| 5214 | |
| 5215 | connector = drmModeGetConnector(backend->drm.fd, connector_id); |
| 5216 | if (!connector) |
| 5217 | goto err_alloc; |
| 5218 | |
| 5219 | name = make_connector_name(connector); |
| 5220 | if (!name) |
| 5221 | goto err_alloc; |
| 5222 | |
| 5223 | weston_head_init(&head->base, name); |
| 5224 | free(name); |
| 5225 | |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 5226 | head->connector_id = connector_id; |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 5227 | head->backend = backend; |
| 5228 | |
Pekka Paalanen | ce72424 | 2017-09-04 12:21:24 +0300 | [diff] [blame] | 5229 | head->backlight = backlight_init(drm_device, connector->connector_type); |
| 5230 | |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 5231 | if (drm_head_assign_connector_info(head, connector) < 0) |
| 5232 | goto err_init; |
| 5233 | |
| 5234 | if (head->connector->connector_type == DRM_MODE_CONNECTOR_LVDS || |
| 5235 | head->connector->connector_type == DRM_MODE_CONNECTOR_eDP) |
| 5236 | weston_head_set_internal(&head->base); |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 5237 | |
Pekka Paalanen | 13d233e | 2017-09-11 14:06:11 +0300 | [diff] [blame] | 5238 | if (connector_get_current_mode(head->connector, backend->drm.fd, |
| 5239 | &head->inherited_mode) < 0) { |
| 5240 | weston_log("Failed to retrieve current mode from connector %d.\n", |
| 5241 | head->connector_id); |
| 5242 | /* Continue, inherited_mode was memset to zero. */ |
| 5243 | } |
| 5244 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 5245 | weston_compositor_add_head(backend->compositor, &head->base); |
Pekka Paalanen | 456dc73 | 2017-11-09 15:10:11 +0200 | [diff] [blame] | 5246 | drm_head_log_info(head, "found"); |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 5247 | |
| 5248 | return head; |
| 5249 | |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 5250 | err_init: |
| 5251 | weston_head_release(&head->base); |
| 5252 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 5253 | err_alloc: |
| 5254 | if (connector) |
| 5255 | drmModeFreeConnector(connector); |
| 5256 | |
| 5257 | free(head); |
| 5258 | |
| 5259 | return NULL; |
| 5260 | } |
| 5261 | |
| 5262 | static void |
| 5263 | drm_head_destroy(struct drm_head *head) |
| 5264 | { |
| 5265 | weston_head_release(&head->base); |
Pekka Paalanen | ce72424 | 2017-09-04 12:21:24 +0300 | [diff] [blame] | 5266 | |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 5267 | drm_property_info_free(head->props_conn, WDRM_CONNECTOR__COUNT); |
| 5268 | drmModeFreeConnector(head->connector); |
| 5269 | |
Pekka Paalanen | ce72424 | 2017-09-04 12:21:24 +0300 | [diff] [blame] | 5270 | if (head->backlight) |
| 5271 | backlight_destroy(head->backlight); |
| 5272 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 5273 | free(head); |
| 5274 | } |
| 5275 | |
| 5276 | /** |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5277 | * Create a Weston output structure |
| 5278 | * |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5279 | * Create an "empty" drm_output. This is the implementation of |
| 5280 | * weston_backend::create_output. |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5281 | * |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5282 | * Creating an output is usually followed by drm_output_attach_head() |
| 5283 | * and drm_output_enable() to make use of it. |
| 5284 | * |
| 5285 | * @param compositor The compositor instance. |
| 5286 | * @param name Name for the new output. |
| 5287 | * @returns The output, or NULL on failure. |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5288 | */ |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5289 | static struct weston_output * |
| 5290 | drm_output_create(struct weston_compositor *compositor, const char *name) |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5291 | { |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5292 | struct drm_backend *b = to_drm_backend(compositor); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5293 | struct drm_output *output; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5294 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5295 | output = zalloc(sizeof *output); |
| 5296 | if (output == NULL) |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5297 | return NULL; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5298 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5299 | weston_output_init(&output->base, compositor, name); |
Pekka Paalanen | c1e89ba | 2017-08-31 16:18:48 +0300 | [diff] [blame] | 5300 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5301 | output->base.enable = drm_output_enable; |
| 5302 | output->base.destroy = drm_output_destroy; |
| 5303 | output->base.disable = drm_output_disable; |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 5304 | output->base.attach_head = drm_output_attach_head; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5305 | |
| 5306 | output->destroy_pending = 0; |
| 5307 | output->disable_pending = 0; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5308 | |
Pekka Paalanen | 01f6021 | 2017-03-24 15:39:24 +0200 | [diff] [blame] | 5309 | output->state_cur = drm_output_state_alloc(output, NULL); |
Pekka Paalanen | a0bfedc | 2017-04-03 14:42:51 +0300 | [diff] [blame] | 5310 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5311 | weston_compositor_add_pending_output(&output->base, b->compositor); |
| 5312 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5313 | return &output->base; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5314 | } |
| 5315 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5316 | static int |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5317 | 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] | 5318 | { |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5319 | struct drm_head *head; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5320 | drmModeRes *resources; |
| 5321 | int i; |
| 5322 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5323 | resources = drmModeGetResources(b->drm.fd); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5324 | if (!resources) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 5325 | weston_log("drmModeGetResources failed\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5326 | return -1; |
| 5327 | } |
| 5328 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5329 | b->min_width = resources->min_width; |
| 5330 | b->max_width = resources->max_width; |
| 5331 | b->min_height = resources->min_height; |
| 5332 | b->max_height = resources->max_height; |
Rob Clark | 4339add | 2012-08-09 14:18:28 -0500 | [diff] [blame] | 5333 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5334 | for (i = 0; i < resources->count_connectors; i++) { |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5335 | uint32_t connector_id = resources->connectors[i]; |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 5336 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5337 | head = drm_head_create(b, connector_id, drm_device); |
| 5338 | if (!head) { |
| 5339 | weston_log("DRM: failed to create head for connector %d.\n", |
| 5340 | connector_id); |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 5341 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5342 | } |
| 5343 | |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 5344 | drm_backend_update_unused_outputs(b, resources); |
| 5345 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5346 | drmModeFreeResources(resources); |
| 5347 | |
| 5348 | return 0; |
| 5349 | } |
| 5350 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5351 | static void |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5352 | 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] | 5353 | { |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5354 | drmModeRes *resources; |
Pekka Paalanen | a0a3746 | 2017-08-31 15:41:57 +0300 | [diff] [blame] | 5355 | struct weston_head *base, *next; |
| 5356 | struct drm_head *head; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5357 | int i; |
| 5358 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5359 | resources = drmModeGetResources(b->drm.fd); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5360 | if (!resources) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 5361 | weston_log("drmModeGetResources failed\n"); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5362 | return; |
| 5363 | } |
| 5364 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5365 | /* collect new connectors that have appeared, e.g. MST */ |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5366 | for (i = 0; i < resources->count_connectors; i++) { |
Ucan, Emre (ADITG/SW1) | 21e4944 | 2017-02-02 14:06:55 +0000 | [diff] [blame] | 5367 | uint32_t connector_id = resources->connectors[i]; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 5368 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5369 | head = drm_head_find_by_connector(b, connector_id); |
| 5370 | if (head) { |
| 5371 | drm_head_update_info(head); |
| 5372 | } else { |
| 5373 | head = drm_head_create(b, connector_id, drm_device); |
| 5374 | if (!head) |
| 5375 | weston_log("DRM: failed to create head for hot-added connector %d.\n", |
| 5376 | connector_id); |
David Herrmann | 7551cff | 2011-12-08 17:05:43 +0100 | [diff] [blame] | 5377 | } |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5378 | } |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5379 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5380 | /* Remove connectors that have disappeared. */ |
Pekka Paalanen | a0a3746 | 2017-08-31 15:41:57 +0300 | [diff] [blame] | 5381 | wl_list_for_each_safe(base, next, |
| 5382 | &b->compositor->head_list, compositor_link) { |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5383 | bool removed = true; |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 5384 | |
Pekka Paalanen | a0a3746 | 2017-08-31 15:41:57 +0300 | [diff] [blame] | 5385 | head = to_drm_head(base); |
| 5386 | |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 5387 | for (i = 0; i < resources->count_connectors; i++) { |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5388 | if (resources->connectors[i] == head->connector_id) { |
| 5389 | removed = false; |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 5390 | break; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5391 | } |
| 5392 | } |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5393 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5394 | if (!removed) |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 5395 | continue; |
| 5396 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5397 | weston_log("DRM: head '%s' (connector %d) disappeared.\n", |
| 5398 | head->base.name, head->connector_id); |
| 5399 | drm_head_destroy(head); |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 5400 | } |
| 5401 | |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 5402 | drm_backend_update_unused_outputs(b, resources); |
| 5403 | |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 5404 | drmModeFreeResources(resources); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5405 | } |
| 5406 | |
| 5407 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5408 | udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5409 | { |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 5410 | const char *sysnum; |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 5411 | const char *val; |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 5412 | |
| 5413 | sysnum = udev_device_get_sysnum(device); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5414 | if (!sysnum || atoi(sysnum) != b->drm.id) |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 5415 | return 0; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 5416 | |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 5417 | val = udev_device_get_property_value(device, "HOTPLUG"); |
| 5418 | if (!val) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5419 | return 0; |
| 5420 | |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 5421 | return strcmp(val, "1") == 0; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5422 | } |
| 5423 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 5424 | static int |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5425 | udev_drm_event(int fd, uint32_t mask, void *data) |
| 5426 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5427 | struct drm_backend *b = data; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5428 | struct udev_device *event; |
| 5429 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5430 | event = udev_monitor_receive_device(b->udev_monitor); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 5431 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5432 | if (udev_event_is_hotplug(b, event)) |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5433 | drm_backend_update_heads(b, event); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5434 | |
| 5435 | udev_device_unref(event); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 5436 | |
| 5437 | return 1; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5438 | } |
| 5439 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 5440 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 5441 | drm_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 5442 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 5443 | struct drm_backend *b = to_drm_backend(ec); |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 5444 | struct weston_head *base, *next; |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 5445 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5446 | udev_input_destroy(&b->input); |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 5447 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5448 | wl_event_source_remove(b->udev_drm_source); |
| 5449 | wl_event_source_remove(b->drm_source); |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 5450 | |
Daniel Stone | b57c6a0 | 2017-10-05 16:27:21 +0100 | [diff] [blame] | 5451 | b->shutting_down = true; |
| 5452 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5453 | destroy_sprites(b); |
Kristian Høgsberg | 3d64a3e | 2013-05-10 12:36:04 -0400 | [diff] [blame] | 5454 | |
Ander Conselvan de Oliveira | 6b16214 | 2013-10-25 16:26:32 +0300 | [diff] [blame] | 5455 | weston_compositor_shutdown(ec); |
| 5456 | |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 5457 | wl_list_for_each_safe(base, next, &ec->head_list, compositor_link) |
| 5458 | drm_head_destroy(to_drm_head(base)); |
| 5459 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5460 | if (b->gbm) |
| 5461 | gbm_device_destroy(b->gbm); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 5462 | |
Pekka Paalanen | 5b0aa55 | 2017-12-07 16:06:05 +0200 | [diff] [blame] | 5463 | udev_monitor_unref(b->udev_monitor); |
Pekka Paalanen | 2a0c6c3 | 2017-09-13 16:48:01 +0300 | [diff] [blame] | 5464 | udev_unref(b->udev); |
| 5465 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5466 | weston_launcher_destroy(ec->launcher); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 5467 | |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 5468 | wl_array_release(&b->unused_crtcs); |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 5469 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5470 | close(b->drm.fd); |
Pekka Paalanen | 9bf4f37 | 2017-12-07 16:05:29 +0200 | [diff] [blame] | 5471 | free(b->drm.filename); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5472 | free(b); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 5473 | } |
| 5474 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 5475 | static void |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 5476 | session_notify(struct wl_listener *listener, void *data) |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 5477 | { |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 5478 | struct weston_compositor *compositor = data; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 5479 | struct drm_backend *b = to_drm_backend(compositor); |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 5480 | struct drm_plane *plane; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 5481 | struct drm_output *output; |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 5482 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5483 | if (compositor->session_active) { |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 5484 | weston_log("activating session\n"); |
Daniel Stone | f33e104 | 2016-11-05 08:10:13 +0000 | [diff] [blame] | 5485 | weston_compositor_wake(compositor); |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 5486 | weston_compositor_damage_all(compositor); |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 5487 | b->state_invalid = true; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5488 | udev_input_enable(&b->input); |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 5489 | } else { |
| 5490 | weston_log("deactivating session\n"); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5491 | udev_input_disable(&b->input); |
Kristian Høgsberg | 4014a6b | 2012-04-10 00:08:45 -0400 | [diff] [blame] | 5492 | |
Philipp Brüschweiler | 57edf7f | 2013-03-29 13:01:56 +0100 | [diff] [blame] | 5493 | weston_compositor_offscreen(compositor); |
Kristian Høgsberg | d8e181b | 2011-05-06 15:38:28 -0400 | [diff] [blame] | 5494 | |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 5495 | /* If we have a repaint scheduled (either from a |
| 5496 | * pending pageflip or the idle handler), make sure we |
| 5497 | * 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] | 5498 | * vt switched away. The OFFSCREEN state will prevent |
Abdur Rehman | 4dca0e1 | 2017-01-01 19:46:35 +0500 | [diff] [blame] | 5499 | * further attempts at repainting. When we switch |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 5500 | * back, we schedule a repaint, which will process |
| 5501 | * pending frame callbacks. */ |
| 5502 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5503 | wl_list_for_each(output, &compositor->output_list, base.link) { |
Daniel Stone | 09a97e2 | 2017-03-01 11:34:06 +0000 | [diff] [blame] | 5504 | output->base.repaint_needed = false; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 5505 | if (output->cursor_plane) |
| 5506 | drmModeSetCursor(b->drm.fd, output->crtc_id, |
| 5507 | 0, 0, 0); |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 5508 | } |
| 5509 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5510 | output = container_of(compositor->output_list.next, |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 5511 | struct drm_output, base.link); |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 5512 | |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 5513 | wl_list_for_each(plane, &b->plane_list, link) { |
| 5514 | if (plane->type != WDRM_PLANE_TYPE_OVERLAY) |
| 5515 | continue; |
| 5516 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5517 | drmModeSetPlane(b->drm.fd, |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 5518 | plane->plane_id, |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 5519 | output->crtc_id, 0, 0, |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 5520 | 0, 0, 0, 0, 0, 0, 0, 0); |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 5521 | } |
| 5522 | } |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 5523 | } |
| 5524 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 5525 | /** |
| 5526 | * Determines whether or not a device is capable of modesetting. If successful, |
| 5527 | * sets b->drm.fd and b->drm.filename to the opened device. |
| 5528 | */ |
| 5529 | static bool |
| 5530 | drm_device_is_kms(struct drm_backend *b, struct udev_device *device) |
| 5531 | { |
| 5532 | const char *filename = udev_device_get_devnode(device); |
| 5533 | const char *sysnum = udev_device_get_sysnum(device); |
| 5534 | drmModeRes *res; |
| 5535 | int id, fd; |
| 5536 | |
| 5537 | if (!filename) |
| 5538 | return false; |
| 5539 | |
| 5540 | fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR); |
| 5541 | if (fd < 0) |
| 5542 | return false; |
| 5543 | |
| 5544 | res = drmModeGetResources(fd); |
| 5545 | if (!res) |
| 5546 | goto out_fd; |
| 5547 | |
| 5548 | if (res->count_crtcs <= 0 || res->count_connectors <= 0 || |
| 5549 | res->count_encoders <= 0) |
| 5550 | goto out_res; |
| 5551 | |
| 5552 | if (sysnum) |
| 5553 | id = atoi(sysnum); |
| 5554 | if (!sysnum || id < 0) { |
| 5555 | weston_log("couldn't get sysnum for device %s\n", filename); |
| 5556 | goto out_res; |
| 5557 | } |
| 5558 | |
| 5559 | /* We can be called successfully on multiple devices; if we have, |
| 5560 | * clean up old entries. */ |
| 5561 | if (b->drm.fd >= 0) |
| 5562 | weston_launcher_close(b->compositor->launcher, b->drm.fd); |
| 5563 | free(b->drm.filename); |
| 5564 | |
| 5565 | b->drm.fd = fd; |
| 5566 | b->drm.id = id; |
| 5567 | b->drm.filename = strdup(filename); |
| 5568 | |
Sergi Granell | ceb5981 | 2017-03-28 12:44:04 +0200 | [diff] [blame] | 5569 | drmModeFreeResources(res); |
| 5570 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 5571 | return true; |
| 5572 | |
| 5573 | out_res: |
| 5574 | drmModeFreeResources(res); |
| 5575 | out_fd: |
| 5576 | weston_launcher_close(b->compositor->launcher, fd); |
| 5577 | return false; |
| 5578 | } |
| 5579 | |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5580 | /* |
| 5581 | * Find primary GPU |
| 5582 | * Some systems may have multiple DRM devices attached to a single seat. This |
| 5583 | * function loops over all devices and tries to find a PCI device with the |
| 5584 | * boot_vga sysfs attribute set to 1. |
| 5585 | * 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] | 5586 | * Devices are also vetted to make sure they are are capable of modesetting, |
| 5587 | * rather than pure render nodes (GPU with no display), or pure |
| 5588 | * memory-allocation devices (VGEM). |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5589 | */ |
| 5590 | static struct udev_device* |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5591 | find_primary_gpu(struct drm_backend *b, const char *seat) |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5592 | { |
| 5593 | struct udev_enumerate *e; |
| 5594 | struct udev_list_entry *entry; |
| 5595 | const char *path, *device_seat, *id; |
| 5596 | struct udev_device *device, *drm_device, *pci; |
| 5597 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5598 | e = udev_enumerate_new(b->udev); |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5599 | udev_enumerate_add_match_subsystem(e, "drm"); |
| 5600 | udev_enumerate_add_match_sysname(e, "card[0-9]*"); |
| 5601 | |
| 5602 | udev_enumerate_scan_devices(e); |
| 5603 | drm_device = NULL; |
| 5604 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 5605 | bool is_boot_vga = false; |
| 5606 | |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5607 | path = udev_list_entry_get_name(entry); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5608 | device = udev_device_new_from_syspath(b->udev, path); |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5609 | if (!device) |
| 5610 | continue; |
| 5611 | device_seat = udev_device_get_property_value(device, "ID_SEAT"); |
| 5612 | if (!device_seat) |
| 5613 | device_seat = default_seat; |
| 5614 | if (strcmp(device_seat, seat)) { |
| 5615 | udev_device_unref(device); |
| 5616 | continue; |
| 5617 | } |
| 5618 | |
| 5619 | pci = udev_device_get_parent_with_subsystem_devtype(device, |
| 5620 | "pci", NULL); |
| 5621 | if (pci) { |
| 5622 | id = udev_device_get_sysattr_value(pci, "boot_vga"); |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 5623 | if (id && !strcmp(id, "1")) |
| 5624 | is_boot_vga = true; |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5625 | } |
| 5626 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 5627 | /* If we already have a modesetting-capable device, and this |
| 5628 | * device isn't our boot-VGA device, we aren't going to use |
| 5629 | * it. */ |
| 5630 | if (!is_boot_vga && drm_device) { |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5631 | udev_device_unref(device); |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 5632 | continue; |
| 5633 | } |
| 5634 | |
| 5635 | /* Make sure this device is actually capable of modesetting; |
| 5636 | * if this call succeeds, b->drm.{fd,filename} will be set, |
| 5637 | * and any old values freed. */ |
| 5638 | if (!drm_device_is_kms(b, device)) { |
| 5639 | udev_device_unref(device); |
| 5640 | continue; |
| 5641 | } |
| 5642 | |
| 5643 | /* There can only be one boot_vga device, and we try to use it |
| 5644 | * at all costs. */ |
| 5645 | if (is_boot_vga) { |
| 5646 | if (drm_device) |
| 5647 | udev_device_unref(drm_device); |
| 5648 | drm_device = device; |
| 5649 | break; |
| 5650 | } |
| 5651 | |
| 5652 | /* Per the (!is_boot_vga && drm_device) test above, we only |
| 5653 | * trump existing saved devices with boot-VGA devices, so if |
| 5654 | * we end up here, this must be the first device we've seen. */ |
| 5655 | assert(!drm_device); |
| 5656 | drm_device = device; |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5657 | } |
| 5658 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 5659 | /* If we're returning a device to use, we must have an open FD for |
| 5660 | * it. */ |
| 5661 | assert(!!drm_device == (b->drm.fd >= 0)); |
| 5662 | |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5663 | udev_enumerate_unref(e); |
| 5664 | return drm_device; |
| 5665 | } |
| 5666 | |
Pekka Paalanen | b45ed8b | 2017-03-28 18:04:27 +0300 | [diff] [blame] | 5667 | static struct udev_device * |
| 5668 | open_specific_drm_device(struct drm_backend *b, const char *name) |
| 5669 | { |
| 5670 | struct udev_device *device; |
| 5671 | |
| 5672 | device = udev_device_new_from_subsystem_sysname(b->udev, "drm", name); |
| 5673 | if (!device) { |
| 5674 | weston_log("ERROR: could not open DRM device '%s'\n", name); |
| 5675 | return NULL; |
| 5676 | } |
| 5677 | |
| 5678 | if (!drm_device_is_kms(b, device)) { |
| 5679 | udev_device_unref(device); |
| 5680 | weston_log("ERROR: DRM device '%s' is not a KMS device.\n", name); |
| 5681 | return NULL; |
| 5682 | } |
| 5683 | |
| 5684 | /* If we're returning a device to use, we must have an open FD for |
| 5685 | * it. */ |
| 5686 | assert(b->drm.fd >= 0); |
| 5687 | |
| 5688 | return device; |
| 5689 | } |
| 5690 | |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 5691 | static void |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 5692 | planes_binding(struct weston_keyboard *keyboard, const struct timespec *time, |
| 5693 | uint32_t key, void *data) |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 5694 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5695 | struct drm_backend *b = data; |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 5696 | |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 5697 | switch (key) { |
| 5698 | case KEY_C: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5699 | b->cursors_are_broken ^= 1; |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 5700 | break; |
| 5701 | case KEY_V: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5702 | b->sprites_are_broken ^= 1; |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 5703 | break; |
| 5704 | case KEY_O: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5705 | b->sprites_hidden ^= 1; |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 5706 | break; |
| 5707 | default: |
| 5708 | break; |
| 5709 | } |
Ander Conselvan de Oliveira | 180f42a | 2012-11-21 15:11:37 +0200 | [diff] [blame] | 5710 | } |
| 5711 | |
Kristian Høgsberg | 0eac34a | 2013-08-30 14:28:22 -0700 | [diff] [blame] | 5712 | #ifdef BUILD_VAAPI_RECORDER |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5713 | static void |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 5714 | recorder_destroy(struct drm_output *output) |
| 5715 | { |
| 5716 | vaapi_recorder_destroy(output->recorder); |
| 5717 | output->recorder = NULL; |
| 5718 | |
| 5719 | output->base.disable_planes--; |
| 5720 | |
| 5721 | wl_list_remove(&output->recorder_frame_listener.link); |
| 5722 | weston_log("[libva recorder] done\n"); |
| 5723 | } |
| 5724 | |
| 5725 | static void |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5726 | recorder_frame_notify(struct wl_listener *listener, void *data) |
| 5727 | { |
| 5728 | struct drm_output *output; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5729 | struct drm_backend *b; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5730 | int fd, ret; |
| 5731 | |
| 5732 | output = container_of(listener, struct drm_output, |
| 5733 | recorder_frame_listener); |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 5734 | b = to_drm_backend(output->base.compositor); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5735 | |
| 5736 | if (!output->recorder) |
| 5737 | return; |
| 5738 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 5739 | ret = drmPrimeHandleToFD(b->drm.fd, |
| 5740 | output->scanout_plane->state_cur->fb->handle, |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5741 | DRM_CLOEXEC, &fd); |
| 5742 | if (ret) { |
| 5743 | weston_log("[libva recorder] " |
| 5744 | "failed to create prime fd for front buffer\n"); |
| 5745 | return; |
| 5746 | } |
| 5747 | |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 5748 | ret = vaapi_recorder_frame(output->recorder, fd, |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 5749 | output->scanout_plane->state_cur->fb->stride); |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 5750 | if (ret < 0) { |
| 5751 | weston_log("[libva recorder] aborted: %m\n"); |
| 5752 | recorder_destroy(output); |
| 5753 | } |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5754 | } |
| 5755 | |
| 5756 | static void * |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5757 | create_recorder(struct drm_backend *b, int width, int height, |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5758 | const char *filename) |
| 5759 | { |
| 5760 | int fd; |
| 5761 | drm_magic_t magic; |
| 5762 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5763 | fd = open(b->drm.filename, O_RDWR | O_CLOEXEC); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5764 | if (fd < 0) |
| 5765 | return NULL; |
| 5766 | |
| 5767 | drmGetMagic(fd, &magic); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5768 | drmAuthMagic(b->drm.fd, magic); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5769 | |
| 5770 | return vaapi_recorder_create(fd, width, height, filename); |
| 5771 | } |
| 5772 | |
| 5773 | static void |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 5774 | recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time, |
| 5775 | uint32_t key, void *data) |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5776 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5777 | struct drm_backend *b = data; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5778 | struct drm_output *output; |
| 5779 | int width, height; |
| 5780 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5781 | output = container_of(b->compositor->output_list.next, |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5782 | struct drm_output, base.link); |
| 5783 | |
| 5784 | if (!output->recorder) { |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 5785 | if (output->gbm_format != GBM_FORMAT_XRGB8888) { |
Ander Conselvan de Oliveira | 2ef1cd1 | 2014-05-06 16:49:06 +0300 | [diff] [blame] | 5786 | weston_log("failed to start vaapi recorder: " |
| 5787 | "output format not supported\n"); |
| 5788 | return; |
| 5789 | } |
| 5790 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 5791 | width = output->base.current_mode->width; |
| 5792 | height = output->base.current_mode->height; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5793 | |
| 5794 | output->recorder = |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5795 | create_recorder(b, width, height, "capture.h264"); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5796 | if (!output->recorder) { |
| 5797 | weston_log("failed to create vaapi recorder\n"); |
| 5798 | return; |
| 5799 | } |
| 5800 | |
| 5801 | output->base.disable_planes++; |
| 5802 | |
| 5803 | output->recorder_frame_listener.notify = recorder_frame_notify; |
| 5804 | wl_signal_add(&output->base.frame_signal, |
| 5805 | &output->recorder_frame_listener); |
| 5806 | |
| 5807 | weston_output_schedule_repaint(&output->base); |
| 5808 | |
| 5809 | weston_log("[libva recorder] initialized\n"); |
| 5810 | } else { |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 5811 | recorder_destroy(output); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5812 | } |
| 5813 | } |
| 5814 | #else |
| 5815 | static void |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 5816 | recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time, |
| 5817 | uint32_t key, void *data) |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5818 | { |
| 5819 | weston_log("Compiled without libva support\n"); |
| 5820 | } |
| 5821 | #endif |
| 5822 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 5823 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5824 | switch_to_gl_renderer(struct drm_backend *b) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 5825 | { |
| 5826 | struct drm_output *output; |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 5827 | bool dmabuf_support_inited; |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 5828 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5829 | if (!b->use_pixman) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 5830 | return; |
| 5831 | |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 5832 | dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf; |
| 5833 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 5834 | weston_log("Switching to GL renderer\n"); |
| 5835 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5836 | b->gbm = create_gbm_device(b->drm.fd); |
| 5837 | if (!b->gbm) { |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 5838 | weston_log("Failed to create gbm device. " |
| 5839 | "Aborting renderer switch\n"); |
| 5840 | return; |
| 5841 | } |
| 5842 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5843 | 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] | 5844 | pixman_renderer_output_destroy(&output->base); |
| 5845 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5846 | b->compositor->renderer->destroy(b->compositor); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 5847 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5848 | if (drm_backend_create_gl_renderer(b) < 0) { |
| 5849 | gbm_device_destroy(b->gbm); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 5850 | weston_log("Failed to create GL renderer. Quitting.\n"); |
| 5851 | /* FIXME: we need a function to shutdown cleanly */ |
| 5852 | assert(0); |
| 5853 | } |
| 5854 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5855 | wl_list_for_each(output, &b->compositor->output_list, base.link) |
| 5856 | drm_output_init_egl(output, b); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 5857 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5858 | b->use_pixman = 0; |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 5859 | |
| 5860 | if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) { |
| 5861 | if (linux_dmabuf_setup(b->compositor) < 0) |
| 5862 | weston_log("Error: initializing dmabuf " |
| 5863 | "support failed.\n"); |
| 5864 | } |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 5865 | } |
| 5866 | |
| 5867 | static void |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 5868 | renderer_switch_binding(struct weston_keyboard *keyboard, |
| 5869 | const struct timespec *time, uint32_t key, void *data) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 5870 | { |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 5871 | struct drm_backend *b = |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 5872 | to_drm_backend(keyboard->seat->compositor); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 5873 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5874 | switch_to_gl_renderer(b); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 5875 | } |
| 5876 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5877 | static const struct weston_drm_output_api api = { |
| 5878 | drm_output_set_mode, |
| 5879 | drm_output_set_gbm_format, |
| 5880 | drm_output_set_seat, |
| 5881 | }; |
| 5882 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5883 | static struct drm_backend * |
| 5884 | drm_backend_create(struct weston_compositor *compositor, |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5885 | struct weston_drm_backend_config *config) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5886 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5887 | struct drm_backend *b; |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5888 | struct udev_device *drm_device; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5889 | struct wl_event_loop *loop; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5890 | const char *seat_id = default_seat; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5891 | int ret; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5892 | |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 5893 | weston_log("initializing drm backend\n"); |
| 5894 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5895 | b = zalloc(sizeof *b); |
| 5896 | if (b == NULL) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 5897 | return NULL; |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 5898 | |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 5899 | b->state_invalid = true; |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 5900 | b->drm.fd = -1; |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 5901 | wl_array_init(&b->unused_crtcs); |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 5902 | |
Pekka Paalanen | 6858383 | 2015-05-19 09:53:16 +0300 | [diff] [blame] | 5903 | /* |
| 5904 | * KMS support for hardware planes cannot properly synchronize |
| 5905 | * without nuclear page flip. Without nuclear/atomic, hw plane |
| 5906 | * and cursor plane updates would either tear or cause extra |
| 5907 | * waits for vblanks which means dropping the compositor framerate |
Pekka Paalanen | 3f32a13 | 2015-09-07 15:38:43 +0300 | [diff] [blame] | 5908 | * to a fraction. For cursors, it's not so bad, so they are |
| 5909 | * enabled. |
Pekka Paalanen | 6858383 | 2015-05-19 09:53:16 +0300 | [diff] [blame] | 5910 | * |
| 5911 | * These can be enabled again when nuclear/atomic support lands. |
| 5912 | */ |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5913 | b->sprites_are_broken = 1; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5914 | b->compositor = compositor; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5915 | b->use_pixman = config->use_pixman; |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 5916 | b->pageflip_timeout = config->pageflip_timeout; |
Kristian Høgsberg | 8e6f376 | 2013-10-16 16:31:42 -0700 | [diff] [blame] | 5917 | |
Pekka Paalanen | 7da9a38 | 2017-08-30 11:29:49 +0300 | [diff] [blame] | 5918 | compositor->backend = &b->base; |
| 5919 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5920 | if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0) |
| 5921 | goto err_compositor; |
Kristian Høgsberg | 8e6f376 | 2013-10-16 16:31:42 -0700 | [diff] [blame] | 5922 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5923 | if (config->seat_id) |
| 5924 | seat_id = config->seat_id; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 5925 | |
Rafal Mielniczuk | 32069c0 | 2013-03-27 18:39:28 +0100 | [diff] [blame] | 5926 | /* Check if we run drm-backend using weston-launch */ |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5927 | compositor->launcher = weston_launcher_connect(compositor, config->tty, |
| 5928 | seat_id, true); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5929 | if (compositor->launcher == NULL) { |
Pekka Paalanen | a453f4d | 2017-10-31 10:19:48 +0200 | [diff] [blame] | 5930 | weston_log("fatal: drm backend should be run using " |
| 5931 | "weston-launch binary, or your system should " |
| 5932 | "provide the logind D-Bus API.\n"); |
Rafal Mielniczuk | 32069c0 | 2013-03-27 18:39:28 +0100 | [diff] [blame] | 5933 | goto err_compositor; |
| 5934 | } |
| 5935 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5936 | b->udev = udev_new(); |
| 5937 | if (b->udev == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 5938 | weston_log("failed to initialize udev context\n"); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 5939 | goto err_launcher; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5940 | } |
| 5941 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5942 | b->session_listener.notify = session_notify; |
| 5943 | wl_signal_add(&compositor->session_signal, &b->session_listener); |
Kristian Høgsberg | c5b9ddb | 2012-01-15 14:29:09 -0500 | [diff] [blame] | 5944 | |
Pekka Paalanen | b45ed8b | 2017-03-28 18:04:27 +0300 | [diff] [blame] | 5945 | if (config->specific_device) |
| 5946 | drm_device = open_specific_drm_device(b, config->specific_device); |
| 5947 | else |
| 5948 | drm_device = find_primary_gpu(b, seat_id); |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 5949 | if (drm_device == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 5950 | weston_log("no drm device found\n"); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 5951 | goto err_udev; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 5952 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5953 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 5954 | if (init_kms_caps(b) < 0) { |
Ander Conselvan de Oliveira | 2292917 | 2013-01-25 15:13:02 +0200 | [diff] [blame] | 5955 | weston_log("failed to initialize kms\n"); |
| 5956 | goto err_udev_dev; |
| 5957 | } |
| 5958 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5959 | if (b->use_pixman) { |
| 5960 | if (init_pixman(b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 5961 | weston_log("failed to initialize pixman renderer\n"); |
| 5962 | goto err_udev_dev; |
| 5963 | } |
| 5964 | } else { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5965 | if (init_egl(b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 5966 | weston_log("failed to initialize egl\n"); |
| 5967 | goto err_udev_dev; |
| 5968 | } |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 5969 | } |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 5970 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5971 | b->base.destroy = drm_destroy; |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 5972 | b->base.repaint_begin = drm_repaint_begin; |
| 5973 | b->base.repaint_flush = drm_repaint_flush; |
| 5974 | b->base.repaint_cancel = drm_repaint_cancel; |
Pekka Paalanen | c112f00 | 2017-08-28 16:27:20 +0300 | [diff] [blame] | 5975 | b->base.create_output = drm_output_create; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 5976 | |
Bob Ham | 91880f1 | 2016-01-12 10:21:47 +0000 | [diff] [blame] | 5977 | weston_setup_vt_switch_bindings(compositor); |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 5978 | |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 5979 | wl_list_init(&b->plane_list); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5980 | create_sprites(b); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 5981 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5982 | if (udev_input_init(&b->input, |
Giulio Camuffo | 8aedf7b | 2016-06-02 21:48:12 +0300 | [diff] [blame] | 5983 | compositor, b->udev, seat_id, |
| 5984 | config->configure_device) < 0) { |
Ander Conselvan de Oliveira | 4ade0e4 | 2014-04-17 13:08:45 +0300 | [diff] [blame] | 5985 | weston_log("failed to create input devices\n"); |
| 5986 | goto err_sprite; |
| 5987 | } |
| 5988 | |
Pekka Paalanen | d2e6242 | 2017-09-08 15:48:07 +0300 | [diff] [blame] | 5989 | if (drm_backend_create_heads(b, drm_device) < 0) { |
| 5990 | 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] | 5991 | goto err_udev_input; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 5992 | } |
| 5993 | |
Jason Ekstrand | 9fc7151 | 2014-04-02 19:53:46 -0500 | [diff] [blame] | 5994 | /* A this point we have some idea of whether or not we have a working |
| 5995 | * cursor plane. */ |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5996 | if (!b->cursors_are_broken) |
| 5997 | compositor->capabilities |= WESTON_CAP_CURSOR_PLANE; |
Jason Ekstrand | 9fc7151 | 2014-04-02 19:53:46 -0500 | [diff] [blame] | 5998 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5999 | loop = wl_display_get_event_loop(compositor->wl_display); |
| 6000 | b->drm_source = |
| 6001 | wl_event_loop_add_fd(loop, b->drm.fd, |
| 6002 | WL_EVENT_READABLE, on_drm_input, b); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 6003 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6004 | b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev"); |
| 6005 | if (b->udev_monitor == NULL) { |
Abdur Rehman | 4dca0e1 | 2017-01-01 19:46:35 +0500 | [diff] [blame] | 6006 | weston_log("failed to initialize udev monitor\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6007 | goto err_drm_source; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 6008 | } |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6009 | udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor, |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 6010 | "drm", NULL); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6011 | b->udev_drm_source = |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 6012 | wl_event_loop_add_fd(loop, |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6013 | udev_monitor_get_fd(b->udev_monitor), |
| 6014 | WL_EVENT_READABLE, udev_drm_event, b); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 6015 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6016 | if (udev_monitor_enable_receiving(b->udev_monitor) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 6017 | weston_log("failed to enable udev-monitor receiving\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6018 | goto err_udev_monitor; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 6019 | } |
| 6020 | |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6021 | udev_device_unref(drm_device); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6022 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6023 | weston_compositor_add_debug_binding(compositor, KEY_O, |
| 6024 | planes_binding, b); |
| 6025 | weston_compositor_add_debug_binding(compositor, KEY_C, |
| 6026 | planes_binding, b); |
| 6027 | weston_compositor_add_debug_binding(compositor, KEY_V, |
| 6028 | planes_binding, b); |
| 6029 | weston_compositor_add_debug_binding(compositor, KEY_Q, |
| 6030 | recorder_binding, b); |
| 6031 | weston_compositor_add_debug_binding(compositor, KEY_W, |
| 6032 | renderer_switch_binding, b); |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 6033 | |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 6034 | if (compositor->renderer->import_dmabuf) { |
| 6035 | if (linux_dmabuf_setup(compositor) < 0) |
| 6036 | weston_log("Error: initializing dmabuf " |
| 6037 | "support failed.\n"); |
| 6038 | } |
| 6039 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 6040 | ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME, |
| 6041 | &api, sizeof(api)); |
| 6042 | |
| 6043 | if (ret < 0) { |
| 6044 | weston_log("Failed to register output API.\n"); |
| 6045 | goto err_udev_monitor; |
| 6046 | } |
| 6047 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6048 | return b; |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6049 | |
| 6050 | err_udev_monitor: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6051 | wl_event_source_remove(b->udev_drm_source); |
| 6052 | udev_monitor_unref(b->udev_monitor); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6053 | err_drm_source: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6054 | wl_event_source_remove(b->drm_source); |
Ander Conselvan de Oliveira | 4ade0e4 | 2014-04-17 13:08:45 +0300 | [diff] [blame] | 6055 | err_udev_input: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6056 | udev_input_destroy(&b->input); |
Kristian Høgsberg | 2bc5e8e | 2012-09-06 20:51:00 -0400 | [diff] [blame] | 6057 | err_sprite: |
Emmanuel Gil Peyrot | b8347e3 | 2016-05-02 22:40:13 +0100 | [diff] [blame] | 6058 | if (b->gbm) |
| 6059 | gbm_device_destroy(b->gbm); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6060 | destroy_sprites(b); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6061 | err_udev_dev: |
| 6062 | udev_device_unref(drm_device); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 6063 | err_launcher: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6064 | weston_launcher_destroy(compositor->launcher); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6065 | err_udev: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6066 | udev_unref(b->udev); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6067 | err_compositor: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6068 | weston_compositor_shutdown(compositor); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6069 | free(b); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 6070 | return NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 6071 | } |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 6072 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6073 | static void |
| 6074 | config_init_to_defaults(struct weston_drm_backend_config *config) |
| 6075 | { |
| 6076 | } |
| 6077 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6078 | WL_EXPORT int |
Quentin Glidic | 23e1d6f | 2016-12-02 14:08:44 +0100 | [diff] [blame] | 6079 | weston_backend_init(struct weston_compositor *compositor, |
| 6080 | struct weston_backend_config *config_base) |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 6081 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6082 | struct drm_backend *b; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6083 | struct weston_drm_backend_config config = {{ 0, }}; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 6084 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6085 | if (config_base == NULL || |
| 6086 | config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION || |
| 6087 | config_base->struct_size > sizeof(struct weston_drm_backend_config)) { |
| 6088 | weston_log("drm backend config structure is invalid\n"); |
| 6089 | return -1; |
| 6090 | } |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 6091 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6092 | config_init_to_defaults(&config); |
| 6093 | memcpy(&config, config_base, config_base->struct_size); |
Kristian Høgsberg | d8e9833 | 2013-10-16 16:15:11 -0700 | [diff] [blame] | 6094 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6095 | b = drm_backend_create(compositor, &config); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6096 | if (b == NULL) |
| 6097 | return -1; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 6098 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 6099 | return 0; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 6100 | } |