Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1 | /* |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 2 | * Copyright © 2008-2011 Kristian Høgsberg |
| 3 | * Copyright © 2011 Intel Corporation |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 4 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 5 | * Permission is hereby granted, free of charge, to any person obtaining |
| 6 | * a copy of this software and associated documentation files (the |
| 7 | * "Software"), to deal in the Software without restriction, including |
| 8 | * without limitation the rights to use, copy, modify, merge, publish, |
| 9 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | * permit persons to whom the Software is furnished to do so, subject to |
| 11 | * the following conditions: |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 12 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 13 | * The above copyright notice and this permission notice (including the |
| 14 | * next paragraph) shall be included in all copies or substantial |
| 15 | * portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | * SOFTWARE. |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 25 | */ |
| 26 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 27 | #include "config.h" |
Kristian Høgsberg | 0b9334a | 2011-04-12 11:34:32 -0400 | [diff] [blame] | 28 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 29 | #include <errno.h> |
Jussi Kukkonen | 649bbce | 2016-07-19 14:16:27 +0300 | [diff] [blame] | 30 | #include <stdint.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 31 | #include <stdlib.h> |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 32 | #include <ctype.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 33 | #include <string.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <unistd.h> |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 36 | #include <linux/input.h> |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 37 | #include <linux/vt.h> |
Ander Conselvan de Oliveira | fd1f4c6 | 2012-06-26 17:09:14 +0300 | [diff] [blame] | 38 | #include <assert.h> |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 39 | #include <sys/mman.h> |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 40 | #include <dlfcn.h> |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 41 | #include <time.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 42 | |
Benjamin Franzke | c649a92 | 2011-03-02 11:56:04 +0100 | [diff] [blame] | 43 | #include <xf86drm.h> |
| 44 | #include <xf86drmMode.h> |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 45 | #include <drm_fourcc.h> |
Benjamin Franzke | c649a92 | 2011-03-02 11:56:04 +0100 | [diff] [blame] | 46 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 47 | #include <gbm.h> |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 48 | #include <libudev.h> |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 49 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 50 | #include "compositor.h" |
| 51 | #include "compositor-drm.h" |
Jon Cruz | 35b2eaa | 2015-06-15 15:37:08 -0700 | [diff] [blame] | 52 | #include "shared/helpers.h" |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 53 | #include "shared/timespec-util.h" |
John Kåre Alsaker | 30d2b1f | 2012-11-13 19:10:28 +0100 | [diff] [blame] | 54 | #include "gl-renderer.h" |
Vincent Abriou | c950667 | 2016-10-05 16:14:07 +0200 | [diff] [blame] | 55 | #include "weston-egl-ext.h" |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 56 | #include "pixman-renderer.h" |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 57 | #include "pixel-formats.h" |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 58 | #include "libbacklight.h" |
Peter Hutterer | 823ad33 | 2014-11-26 07:06:31 +1000 | [diff] [blame] | 59 | #include "libinput-seat.h" |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 60 | #include "launcher-util.h" |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 61 | #include "vaapi-recorder.h" |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 62 | #include "presentation-time-server-protocol.h" |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 63 | #include "linux-dmabuf.h" |
Micah Fedke | c889012 | 2017-02-01 15:28:23 -0500 | [diff] [blame] | 64 | #include "linux-dmabuf-unstable-v1-server-protocol.h" |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 65 | |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 66 | #ifndef DRM_CAP_TIMESTAMP_MONOTONIC |
| 67 | #define DRM_CAP_TIMESTAMP_MONOTONIC 0x6 |
| 68 | #endif |
| 69 | |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 70 | #ifndef DRM_CLIENT_CAP_UNIVERSAL_PLANES |
| 71 | #define DRM_CLIENT_CAP_UNIVERSAL_PLANES 2 |
| 72 | #endif |
| 73 | |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 74 | #ifndef DRM_CAP_CURSOR_WIDTH |
| 75 | #define DRM_CAP_CURSOR_WIDTH 0x8 |
| 76 | #endif |
| 77 | |
| 78 | #ifndef DRM_CAP_CURSOR_HEIGHT |
| 79 | #define DRM_CAP_CURSOR_HEIGHT 0x9 |
| 80 | #endif |
| 81 | |
| 82 | #ifndef GBM_BO_USE_CURSOR |
| 83 | #define GBM_BO_USE_CURSOR GBM_BO_USE_CURSOR_64X64 |
| 84 | #endif |
| 85 | |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 86 | /** |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 87 | * Represents the values of an enum-type KMS property |
| 88 | */ |
| 89 | struct drm_property_enum_info { |
| 90 | const char *name; /**< name as string (static, not freed) */ |
| 91 | bool valid; /**< true if value is supported; ignore if false */ |
| 92 | uint64_t value; /**< raw value */ |
| 93 | }; |
| 94 | |
| 95 | /** |
| 96 | * Holds information on a DRM property, including its ID and the enum |
| 97 | * values it holds. |
| 98 | * |
| 99 | * DRM properties are allocated dynamically, and maintained as DRM objects |
| 100 | * within the normal object ID space; they thus do not have a stable ID |
| 101 | * to refer to. This includes enum values, which must be referred to by |
| 102 | * integer values, but these are not stable. |
| 103 | * |
| 104 | * drm_property_info allows a cache to be maintained where Weston can use |
| 105 | * enum values internally to refer to properties, with the mapping to DRM |
| 106 | * ID values being maintained internally. |
| 107 | */ |
| 108 | struct drm_property_info { |
| 109 | const char *name; /**< name as string (static, not freed) */ |
| 110 | uint32_t prop_id; /**< KMS property object ID */ |
| 111 | unsigned int num_enum_values; /**< number of enum values */ |
| 112 | struct drm_property_enum_info *enum_values; /**< array of enum values */ |
| 113 | }; |
| 114 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 115 | /** |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 116 | * List of properties attached to DRM planes |
| 117 | */ |
| 118 | enum wdrm_plane_property { |
| 119 | WDRM_PLANE_TYPE = 0, |
| 120 | WDRM_PLANE_SRC_X, |
| 121 | WDRM_PLANE_SRC_Y, |
| 122 | WDRM_PLANE_SRC_W, |
| 123 | WDRM_PLANE_SRC_H, |
| 124 | WDRM_PLANE_CRTC_X, |
| 125 | WDRM_PLANE_CRTC_Y, |
| 126 | WDRM_PLANE_CRTC_W, |
| 127 | WDRM_PLANE_CRTC_H, |
| 128 | WDRM_PLANE_FB_ID, |
| 129 | WDRM_PLANE_CRTC_ID, |
| 130 | WDRM_PLANE__COUNT |
| 131 | }; |
| 132 | |
| 133 | /** |
| 134 | * Possible values for the WDRM_PLANE_TYPE property. |
| 135 | */ |
| 136 | enum wdrm_plane_type { |
| 137 | WDRM_PLANE_TYPE_PRIMARY = 0, |
| 138 | WDRM_PLANE_TYPE_CURSOR, |
| 139 | WDRM_PLANE_TYPE_OVERLAY, |
| 140 | WDRM_PLANE_TYPE__COUNT |
| 141 | }; |
| 142 | |
| 143 | static struct drm_property_enum_info plane_type_enums[] = { |
| 144 | [WDRM_PLANE_TYPE_PRIMARY] = { |
| 145 | .name = "Primary", |
| 146 | }, |
| 147 | [WDRM_PLANE_TYPE_OVERLAY] = { |
| 148 | .name = "Overlay", |
| 149 | }, |
| 150 | [WDRM_PLANE_TYPE_CURSOR] = { |
| 151 | .name = "Cursor", |
| 152 | }, |
| 153 | }; |
| 154 | |
| 155 | static const struct drm_property_info plane_props[] = { |
| 156 | [WDRM_PLANE_TYPE] = { |
| 157 | .name = "type", |
| 158 | .enum_values = plane_type_enums, |
| 159 | .num_enum_values = WDRM_PLANE_TYPE__COUNT, |
| 160 | }, |
| 161 | [WDRM_PLANE_SRC_X] = { .name = "SRC_X", }, |
| 162 | [WDRM_PLANE_SRC_Y] = { .name = "SRC_Y", }, |
| 163 | [WDRM_PLANE_SRC_W] = { .name = "SRC_W", }, |
| 164 | [WDRM_PLANE_SRC_H] = { .name = "SRC_H", }, |
| 165 | [WDRM_PLANE_CRTC_X] = { .name = "CRTC_X", }, |
| 166 | [WDRM_PLANE_CRTC_Y] = { .name = "CRTC_Y", }, |
| 167 | [WDRM_PLANE_CRTC_W] = { .name = "CRTC_W", }, |
| 168 | [WDRM_PLANE_CRTC_H] = { .name = "CRTC_H", }, |
| 169 | [WDRM_PLANE_FB_ID] = { .name = "FB_ID", }, |
| 170 | [WDRM_PLANE_CRTC_ID] = { .name = "CRTC_ID", }, |
| 171 | }; |
| 172 | |
| 173 | /** |
| 174 | * List of properties attached to a DRM connector |
| 175 | */ |
| 176 | enum wdrm_connector_property { |
| 177 | WDRM_CONNECTOR_EDID = 0, |
| 178 | WDRM_CONNECTOR_DPMS, |
| 179 | WDRM_CONNECTOR_CRTC_ID, |
| 180 | WDRM_CONNECTOR__COUNT |
| 181 | }; |
| 182 | |
| 183 | static const struct drm_property_info connector_props[] = { |
| 184 | [WDRM_CONNECTOR_EDID] = { .name = "EDID" }, |
| 185 | [WDRM_CONNECTOR_DPMS] = { .name = "DPMS" }, |
| 186 | [WDRM_CONNECTOR_CRTC_ID] = { .name = "CRTC_ID", }, |
| 187 | }; |
| 188 | |
| 189 | /** |
Pekka Paalanen | cd011a6 | 2016-11-15 22:07:49 +0000 | [diff] [blame] | 190 | * List of properties attached to DRM CRTCs |
| 191 | */ |
| 192 | enum wdrm_crtc_property { |
| 193 | WDRM_CRTC_MODE_ID = 0, |
| 194 | WDRM_CRTC_ACTIVE, |
| 195 | WDRM_CRTC__COUNT |
| 196 | }; |
| 197 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 198 | static const struct drm_property_info crtc_props[] = { |
| 199 | [WDRM_CRTC_MODE_ID] = { .name = "MODE_ID", }, |
| 200 | [WDRM_CRTC_ACTIVE] = { .name = "ACTIVE", }, |
| 201 | }; |
| 202 | |
Pekka Paalanen | cd011a6 | 2016-11-15 22:07:49 +0000 | [diff] [blame] | 203 | /** |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 204 | * Mode for drm_output_state_duplicate. |
| 205 | */ |
| 206 | enum drm_output_state_duplicate_mode { |
| 207 | DRM_OUTPUT_STATE_CLEAR_PLANES, /**< reset all planes to off */ |
| 208 | DRM_OUTPUT_STATE_PRESERVE_PLANES, /**< preserve plane state */ |
| 209 | }; |
| 210 | |
| 211 | /** |
| 212 | * Mode for drm_pending_state_apply and co. |
| 213 | */ |
| 214 | enum drm_state_apply_mode { |
| 215 | DRM_STATE_APPLY_SYNC, /**< state fully processed */ |
| 216 | DRM_STATE_APPLY_ASYNC, /**< state pending event delivery */ |
| 217 | }; |
| 218 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 219 | struct drm_backend { |
| 220 | struct weston_backend base; |
| 221 | struct weston_compositor *compositor; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 222 | |
| 223 | struct udev *udev; |
| 224 | struct wl_event_source *drm_source; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 225 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 226 | struct udev_monitor *udev_monitor; |
| 227 | struct wl_event_source *udev_drm_source; |
| 228 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 229 | struct { |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 230 | int id; |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 231 | int fd; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 232 | char *filename; |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 233 | } drm; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 234 | struct gbm_device *gbm; |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 235 | struct wl_listener session_listener; |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 236 | uint32_t gbm_format; |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 237 | |
Rob Clark | 4339add | 2012-08-09 14:18:28 -0500 | [diff] [blame] | 238 | /* we need these parameters in order to not fail drmModeAddFB2() |
| 239 | * due to out of bounds dimensions, and then mistakenly set |
| 240 | * sprites_are_broken: |
| 241 | */ |
Daniel Stone | f214fdc | 2016-11-14 17:43:57 +0000 | [diff] [blame] | 242 | int min_width, max_width; |
| 243 | int min_height, max_height; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 244 | int no_addfb2; |
Rob Clark | 4339add | 2012-08-09 14:18:28 -0500 | [diff] [blame] | 245 | |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 246 | struct wl_list plane_list; |
Kristian Høgsberg | 65bec24 | 2012-03-05 19:57:35 -0500 | [diff] [blame] | 247 | int sprites_are_broken; |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 248 | int sprites_hidden; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 249 | |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 250 | void *repaint_data; |
| 251 | |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 252 | bool state_invalid; |
| 253 | |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 254 | /* Connector and CRTC IDs not used by any enabled output. */ |
| 255 | struct wl_array unused_connectors; |
| 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 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 404 | struct drm_output { |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 405 | struct weston_output base; |
| 406 | drmModeConnector *connector; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 407 | |
Daniel Stone | 1733923 | 2015-11-28 12:09:47 +0000 | [diff] [blame] | 408 | uint32_t crtc_id; /* object ID to pass to DRM functions */ |
| 409 | int pipe; /* index of CRTC in resource array / bitmasks */ |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 410 | uint32_t connector_id; |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 411 | struct drm_edid edid; |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 412 | |
| 413 | /* Holds the properties for the connector */ |
| 414 | struct drm_property_info props_conn[WDRM_CONNECTOR__COUNT]; |
Pekka Paalanen | cd011a6 | 2016-11-15 22:07:49 +0000 | [diff] [blame] | 415 | /* Holds the properties for the CRTC */ |
| 416 | struct drm_property_info props_crtc[WDRM_CRTC__COUNT]; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 417 | |
Daniel Stone | 5bb8f58 | 2017-04-04 17:54:28 +0100 | [diff] [blame] | 418 | struct backlight *backlight; |
Daniel Stone | 36609c7 | 2015-06-18 07:49:02 +0100 | [diff] [blame] | 419 | |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 420 | int vblank_pending; |
| 421 | int page_flip_pending; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 422 | int atomic_complete_pending; |
Xiong Zhang | abd5d47 | 2013-10-11 14:43:07 +0800 | [diff] [blame] | 423 | int destroy_pending; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 424 | int disable_pending; |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 425 | int dpms_off_pending; |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 426 | |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 427 | struct drm_fb *gbm_cursor_fb[2]; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 428 | struct drm_plane *cursor_plane; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 429 | struct weston_view *cursor_view; |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 430 | int current_cursor; |
Daniel Stone | 5bb8f58 | 2017-04-04 17:54:28 +0100 | [diff] [blame] | 431 | |
| 432 | struct gbm_surface *gbm_surface; |
| 433 | uint32_t gbm_format; |
| 434 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 435 | /* Plane being displayed directly on the CRTC */ |
| 436 | struct drm_plane *scanout_plane; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 437 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 438 | /* The last state submitted to the kernel for this CRTC. */ |
| 439 | struct drm_output_state *state_cur; |
| 440 | /* The previously-submitted state, where the hardware has not |
| 441 | * yet acknowledged completion of state_cur. */ |
| 442 | struct drm_output_state *state_last; |
| 443 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 444 | struct drm_fb *dumb[2]; |
| 445 | pixman_image_t *image[2]; |
| 446 | int current_image; |
| 447 | pixman_region32_t previous_damage; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 448 | |
| 449 | struct vaapi_recorder *recorder; |
| 450 | struct wl_listener recorder_frame_listener; |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 451 | |
| 452 | struct wl_event_source *pageflip_timer; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 453 | }; |
| 454 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 455 | static struct gl_renderer_interface *gl_renderer; |
| 456 | |
Kristian Høgsberg | 98cfea6 | 2013-02-18 16:15:53 -0500 | [diff] [blame] | 457 | static const char default_seat[] = "seat0"; |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 458 | |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 459 | static void |
| 460 | wl_array_remove_uint32(struct wl_array *array, uint32_t elm) |
| 461 | { |
| 462 | uint32_t *pos, *end; |
| 463 | |
| 464 | end = (uint32_t *) ((char *) array->data + array->size); |
| 465 | |
| 466 | wl_array_for_each(pos, array) { |
| 467 | if (*pos != elm) |
| 468 | continue; |
| 469 | |
| 470 | array->size -= sizeof(*pos); |
| 471 | if (pos + 1 == end) |
| 472 | break; |
| 473 | |
| 474 | memmove(pos, pos + 1, (char *) end - (char *) (pos + 1)); |
| 475 | break; |
| 476 | } |
| 477 | } |
| 478 | |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 479 | static inline struct drm_output * |
| 480 | to_drm_output(struct weston_output *base) |
| 481 | { |
| 482 | return container_of(base, struct drm_output, base); |
| 483 | } |
| 484 | |
| 485 | static inline struct drm_backend * |
| 486 | to_drm_backend(struct weston_compositor *base) |
| 487 | { |
| 488 | return container_of(base->backend, struct drm_backend, base); |
| 489 | } |
| 490 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 491 | static int |
| 492 | pageflip_timeout(void *data) { |
| 493 | /* |
| 494 | * Our timer just went off, that means we're not receiving drm |
| 495 | * page flip events anymore for that output. Let's gracefully exit |
| 496 | * weston with a return value so devs can debug what's going on. |
| 497 | */ |
| 498 | struct drm_output *output = data; |
| 499 | struct weston_compositor *compositor = output->base.compositor; |
| 500 | |
| 501 | weston_log("Pageflip timeout reached on output %s, your " |
| 502 | "driver is probably buggy! Exiting.\n", |
| 503 | output->base.name); |
| 504 | weston_compositor_exit_with_code(compositor, EXIT_FAILURE); |
| 505 | |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | /* Creates the pageflip timer. Note that it isn't armed by default */ |
| 510 | static int |
| 511 | drm_output_pageflip_timer_create(struct drm_output *output) |
| 512 | { |
| 513 | struct wl_event_loop *loop = NULL; |
| 514 | struct weston_compositor *ec = output->base.compositor; |
| 515 | |
| 516 | loop = wl_display_get_event_loop(ec->wl_display); |
| 517 | assert(loop); |
| 518 | output->pageflip_timer = wl_event_loop_add_timer(loop, |
| 519 | pageflip_timeout, |
| 520 | output); |
| 521 | |
| 522 | if (output->pageflip_timer == NULL) { |
| 523 | weston_log("creating drm pageflip timer failed: %m\n"); |
| 524 | return -1; |
| 525 | } |
| 526 | |
| 527 | return 0; |
| 528 | } |
| 529 | |
Daniel Stone | cb04cc4 | 2016-11-16 11:51:27 +0000 | [diff] [blame] | 530 | static inline struct drm_mode * |
| 531 | to_drm_mode(struct weston_mode *base) |
| 532 | { |
| 533 | return container_of(base, struct drm_mode, base); |
| 534 | } |
| 535 | |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 536 | /** |
| 537 | * Get the current value of a KMS property |
| 538 | * |
| 539 | * Given a drmModeObjectGetProperties return, as well as the drm_property_info |
| 540 | * for the target property, return the current value of that property, |
| 541 | * with an optional default. If the property is a KMS enum type, the return |
| 542 | * value will be translated into the appropriate internal enum. |
| 543 | * |
| 544 | * If the property is not present, the default value will be returned. |
| 545 | * |
| 546 | * @param info Internal structure for property to look up |
| 547 | * @param props Raw KMS properties for the target object |
| 548 | * @param def Value to return if property is not found |
| 549 | */ |
| 550 | static uint64_t |
| 551 | drm_property_get_value(struct drm_property_info *info, |
| 552 | drmModeObjectPropertiesPtr props, |
| 553 | uint64_t def) |
| 554 | { |
| 555 | unsigned int i; |
| 556 | |
| 557 | if (info->prop_id == 0) |
| 558 | return def; |
| 559 | |
| 560 | for (i = 0; i < props->count_props; i++) { |
| 561 | unsigned int j; |
| 562 | |
| 563 | if (props->props[i] != info->prop_id) |
| 564 | continue; |
| 565 | |
| 566 | /* Simple (non-enum) types can return the value directly */ |
| 567 | if (info->num_enum_values == 0) |
| 568 | return props->prop_values[i]; |
| 569 | |
| 570 | /* Map from raw value to enum value */ |
| 571 | for (j = 0; j < info->num_enum_values; j++) { |
| 572 | if (!info->enum_values[j].valid) |
| 573 | continue; |
| 574 | if (info->enum_values[j].value != props->prop_values[i]) |
| 575 | continue; |
| 576 | |
| 577 | return j; |
| 578 | } |
| 579 | |
| 580 | /* We don't have a mapping for this enum; return default. */ |
| 581 | break; |
| 582 | } |
| 583 | |
| 584 | return def; |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * Cache DRM property values |
| 589 | * |
| 590 | * Update a per-object array of drm_property_info structures, given the |
| 591 | * DRM properties of the object. |
| 592 | * |
| 593 | * Call this every time an object newly appears (note that only connectors |
| 594 | * can be hotplugged), the first time it is seen, or when its status changes |
| 595 | * in a way which invalidates the potential property values (currently, the |
| 596 | * only case for this is connector hotplug). |
| 597 | * |
| 598 | * This updates the property IDs and enum values within the drm_property_info |
| 599 | * array. |
| 600 | * |
| 601 | * DRM property enum values are dynamic at runtime; the user must query the |
| 602 | * property to find out the desired runtime value for a requested string |
| 603 | * name. Using the 'type' field on planes as an example, there is no single |
| 604 | * hardcoded constant for primary plane types; instead, the property must be |
| 605 | * queried at runtime to find the value associated with the string "Primary". |
| 606 | * |
| 607 | * This helper queries and caches the enum values, to allow us to use a set |
| 608 | * of compile-time-constant enums portably across various implementations. |
| 609 | * The values given in enum_names are searched for, and stored in the |
| 610 | * same-indexed field of the map array. |
| 611 | * |
| 612 | * @param b DRM backend object |
| 613 | * @param src DRM property info array to source from |
| 614 | * @param info DRM property info array to copy into |
| 615 | * @param num_infos Number of entries in the source array |
| 616 | * @param props DRM object properties for the object |
| 617 | */ |
| 618 | static void |
| 619 | drm_property_info_populate(struct drm_backend *b, |
| 620 | const struct drm_property_info *src, |
| 621 | struct drm_property_info *info, |
| 622 | unsigned int num_infos, |
| 623 | drmModeObjectProperties *props) |
| 624 | { |
| 625 | drmModePropertyRes *prop; |
| 626 | unsigned i, j; |
| 627 | |
| 628 | for (i = 0; i < num_infos; i++) { |
| 629 | unsigned int j; |
| 630 | |
| 631 | info[i].name = src[i].name; |
| 632 | info[i].prop_id = 0; |
| 633 | info[i].num_enum_values = src[i].num_enum_values; |
| 634 | |
| 635 | if (src[i].num_enum_values == 0) |
| 636 | continue; |
| 637 | |
| 638 | info[i].enum_values = |
| 639 | malloc(src[i].num_enum_values * |
| 640 | sizeof(*info[i].enum_values)); |
| 641 | assert(info[i].enum_values); |
| 642 | for (j = 0; j < info[i].num_enum_values; j++) { |
| 643 | info[i].enum_values[j].name = src[i].enum_values[j].name; |
| 644 | info[i].enum_values[j].valid = false; |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | for (i = 0; i < props->count_props; i++) { |
| 649 | unsigned int k; |
| 650 | |
| 651 | prop = drmModeGetProperty(b->drm.fd, props->props[i]); |
| 652 | if (!prop) |
| 653 | continue; |
| 654 | |
| 655 | for (j = 0; j < num_infos; j++) { |
| 656 | if (!strcmp(prop->name, info[j].name)) |
| 657 | break; |
| 658 | } |
| 659 | |
| 660 | /* We don't know/care about this property. */ |
| 661 | if (j == num_infos) { |
| 662 | #ifdef DEBUG |
| 663 | weston_log("DRM debug: unrecognized property %u '%s'\n", |
| 664 | prop->prop_id, prop->name); |
| 665 | #endif |
| 666 | drmModeFreeProperty(prop); |
| 667 | continue; |
| 668 | } |
| 669 | |
| 670 | if (info[j].num_enum_values == 0 && |
| 671 | (prop->flags & DRM_MODE_PROP_ENUM)) { |
| 672 | weston_log("DRM: expected property %s to not be an" |
| 673 | " enum, but it is; ignoring\n", prop->name); |
| 674 | drmModeFreeProperty(prop); |
| 675 | continue; |
| 676 | } |
| 677 | |
| 678 | info[j].prop_id = props->props[i]; |
| 679 | |
| 680 | if (info[j].num_enum_values == 0) { |
| 681 | drmModeFreeProperty(prop); |
| 682 | continue; |
| 683 | } |
| 684 | |
| 685 | if (!(prop->flags & DRM_MODE_PROP_ENUM)) { |
| 686 | weston_log("DRM: expected property %s to be an enum," |
| 687 | " but it is not; ignoring\n", prop->name); |
| 688 | drmModeFreeProperty(prop); |
| 689 | info[j].prop_id = 0; |
| 690 | continue; |
| 691 | } |
| 692 | |
| 693 | for (k = 0; k < info[j].num_enum_values; k++) { |
| 694 | int l; |
| 695 | |
| 696 | for (l = 0; l < prop->count_enums; l++) { |
| 697 | if (!strcmp(prop->enums[l].name, |
| 698 | info[j].enum_values[k].name)) |
| 699 | break; |
| 700 | } |
| 701 | |
| 702 | if (l == prop->count_enums) |
| 703 | continue; |
| 704 | |
| 705 | info[j].enum_values[k].valid = true; |
| 706 | info[j].enum_values[k].value = prop->enums[l].value; |
| 707 | } |
| 708 | |
| 709 | drmModeFreeProperty(prop); |
| 710 | } |
| 711 | |
| 712 | #ifdef DEBUG |
| 713 | for (i = 0; i < num_infos; i++) { |
| 714 | if (info[i].prop_id == 0) |
| 715 | weston_log("DRM warning: property '%s' missing\n", |
| 716 | info[i].name); |
| 717 | } |
| 718 | #endif |
| 719 | } |
| 720 | |
| 721 | /** |
| 722 | * Free DRM property information |
| 723 | * |
Pekka Paalanen | 46e4f97 | 2017-09-07 15:32:01 +0300 | [diff] [blame] | 724 | * Frees all memory associated with a DRM property info array and zeroes |
| 725 | * it out, leaving it usable for a further drm_property_info_update() or |
| 726 | * drm_property_info_free(). |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 727 | * |
| 728 | * @param info DRM property info array |
| 729 | * @param num_props Number of entries in array to free |
| 730 | */ |
| 731 | static void |
| 732 | drm_property_info_free(struct drm_property_info *info, int num_props) |
| 733 | { |
| 734 | int i; |
| 735 | |
| 736 | for (i = 0; i < num_props; i++) |
| 737 | free(info[i].enum_values); |
Pekka Paalanen | 46e4f97 | 2017-09-07 15:32:01 +0300 | [diff] [blame] | 738 | |
| 739 | memset(info, 0, sizeof(*info) * num_props); |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 742 | static void |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 743 | drm_output_set_cursor(struct drm_output_state *output_state); |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 744 | |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 745 | static void |
| 746 | drm_output_update_msc(struct drm_output *output, unsigned int seq); |
| 747 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 748 | static void |
| 749 | drm_output_destroy(struct weston_output *output_base); |
| 750 | |
Daniel Stone | 5ff289a | 2017-10-07 12:59:02 +0100 | [diff] [blame] | 751 | /** |
| 752 | * Returns true if the plane can be used on the given output for its current |
| 753 | * repaint cycle. |
| 754 | */ |
| 755 | static bool |
| 756 | drm_plane_is_available(struct drm_plane *plane, struct drm_output *output) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 757 | { |
Daniel Stone | 5ff289a | 2017-10-07 12:59:02 +0100 | [diff] [blame] | 758 | assert(plane->state_cur); |
| 759 | |
| 760 | /* The plane still has a request not yet completed by the kernel. */ |
| 761 | if (!plane->state_cur->complete) |
| 762 | return false; |
| 763 | |
| 764 | /* The plane is still active on another output. */ |
| 765 | if (plane->state_cur->output && plane->state_cur->output != output) |
| 766 | return false; |
| 767 | |
| 768 | /* Check whether the plane can be used with this CRTC; possible_crtcs |
| 769 | * is a bitmask of CRTC indices (pipe), rather than CRTC object ID. */ |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 770 | return !!(plane->possible_crtcs & (1 << output->pipe)); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 771 | } |
| 772 | |
Daniel Stone | 72c0e1b | 2017-02-09 13:49:15 +0000 | [diff] [blame] | 773 | static struct drm_output * |
| 774 | drm_output_find_by_crtc(struct drm_backend *b, uint32_t crtc_id) |
| 775 | { |
| 776 | struct drm_output *output; |
| 777 | |
| 778 | wl_list_for_each(output, &b->compositor->output_list, base.link) { |
| 779 | if (output->crtc_id == crtc_id) |
| 780 | return output; |
| 781 | } |
| 782 | |
| 783 | wl_list_for_each(output, &b->compositor->pending_output_list, |
| 784 | base.link) { |
| 785 | if (output->crtc_id == crtc_id) |
| 786 | return output; |
| 787 | } |
| 788 | |
| 789 | return NULL; |
| 790 | } |
| 791 | |
Daniel Stone | c0ec759 | 2017-02-09 13:58:35 +0000 | [diff] [blame] | 792 | static struct drm_output * |
| 793 | drm_output_find_by_connector(struct drm_backend *b, uint32_t connector_id) |
| 794 | { |
| 795 | struct drm_output *output; |
| 796 | |
| 797 | wl_list_for_each(output, &b->compositor->output_list, base.link) { |
| 798 | if (output->connector_id == connector_id) |
| 799 | return output; |
| 800 | } |
| 801 | |
| 802 | wl_list_for_each(output, &b->compositor->pending_output_list, |
| 803 | base.link) { |
| 804 | if (output->connector_id == connector_id) |
| 805 | return output; |
| 806 | } |
| 807 | |
| 808 | return NULL; |
| 809 | } |
| 810 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 811 | static void |
Tomohito Esaki | 576f42e | 2017-04-04 17:54:24 +0100 | [diff] [blame] | 812 | drm_fb_destroy(struct drm_fb *fb) |
| 813 | { |
| 814 | if (fb->fb_id != 0) |
| 815 | drmModeRmFB(fb->fd, fb->fb_id); |
| 816 | weston_buffer_reference(&fb->buffer_ref, NULL); |
| 817 | free(fb); |
| 818 | } |
| 819 | |
| 820 | static void |
| 821 | drm_fb_destroy_dumb(struct drm_fb *fb) |
| 822 | { |
| 823 | struct drm_mode_destroy_dumb destroy_arg; |
| 824 | |
| 825 | assert(fb->type == BUFFER_PIXMAN_DUMB); |
| 826 | |
| 827 | if (fb->map && fb->size > 0) |
| 828 | munmap(fb->map, fb->size); |
| 829 | |
| 830 | memset(&destroy_arg, 0, sizeof(destroy_arg)); |
| 831 | destroy_arg.handle = fb->handle; |
| 832 | drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg); |
| 833 | |
| 834 | drm_fb_destroy(fb); |
| 835 | } |
| 836 | |
| 837 | static void |
| 838 | drm_fb_destroy_gbm(struct gbm_bo *bo, void *data) |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 839 | { |
| 840 | struct drm_fb *fb = data; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 841 | |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 842 | assert(fb->type == BUFFER_GBM_SURFACE || fb->type == BUFFER_CLIENT || |
| 843 | fb->type == BUFFER_CURSOR); |
Tomohito Esaki | 576f42e | 2017-04-04 17:54:24 +0100 | [diff] [blame] | 844 | drm_fb_destroy(fb); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | static struct drm_fb * |
Daniel Stone | f214fdc | 2016-11-14 17:43:57 +0000 | [diff] [blame] | 848 | drm_fb_create_dumb(struct drm_backend *b, int width, int height, |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 849 | uint32_t format) |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 850 | { |
| 851 | struct drm_fb *fb; |
| 852 | int ret; |
| 853 | |
| 854 | struct drm_mode_create_dumb create_arg; |
| 855 | struct drm_mode_destroy_dumb destroy_arg; |
| 856 | struct drm_mode_map_dumb map_arg; |
| 857 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 858 | fb = zalloc(sizeof *fb); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 859 | if (!fb) |
| 860 | return NULL; |
| 861 | |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 862 | fb->refcnt = 1; |
| 863 | |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 864 | fb->format = pixel_format_get_info(format); |
| 865 | if (!fb->format) { |
| 866 | weston_log("failed to look up format 0x%lx\n", |
| 867 | (unsigned long) format); |
| 868 | goto err_fb; |
| 869 | } |
| 870 | |
| 871 | if (!fb->format->depth || !fb->format->bpp) { |
| 872 | weston_log("format 0x%lx is not compatible with dumb buffers\n", |
| 873 | (unsigned long) format); |
| 874 | goto err_fb; |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 875 | } |
| 876 | |
Kristian Høgsberg | ac6104e | 2013-08-21 22:14:14 -0700 | [diff] [blame] | 877 | memset(&create_arg, 0, sizeof create_arg); |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 878 | create_arg.bpp = fb->format->bpp; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 879 | create_arg.width = width; |
| 880 | create_arg.height = height; |
| 881 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 882 | 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] | 883 | if (ret) |
| 884 | goto err_fb; |
| 885 | |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 886 | fb->type = BUFFER_PIXMAN_DUMB; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 887 | fb->handle = create_arg.handle; |
| 888 | fb->stride = create_arg.pitch; |
| 889 | fb->size = create_arg.size; |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 890 | fb->width = width; |
| 891 | fb->height = height; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 892 | fb->fd = b->drm.fd; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 893 | |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 894 | ret = -1; |
| 895 | |
| 896 | if (!b->no_addfb2) { |
Yong Bakos | 4b6321f | 2016-08-17 17:37:55 -0700 | [diff] [blame] | 897 | uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 }; |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 898 | |
| 899 | handles[0] = fb->handle; |
| 900 | pitches[0] = fb->stride; |
| 901 | offsets[0] = 0; |
| 902 | |
| 903 | ret = drmModeAddFB2(b->drm.fd, width, height, |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 904 | fb->format->format, |
| 905 | handles, pitches, offsets, |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 906 | &fb->fb_id, 0); |
| 907 | if (ret) { |
| 908 | weston_log("addfb2 failed: %m\n"); |
| 909 | b->no_addfb2 = 1; |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | if (ret) { |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 914 | ret = drmModeAddFB(b->drm.fd, width, height, |
| 915 | fb->format->depth, fb->format->bpp, |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 916 | fb->stride, fb->handle, &fb->fb_id); |
| 917 | } |
| 918 | |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 919 | if (ret) |
| 920 | goto err_bo; |
| 921 | |
Kristian Høgsberg | ac6104e | 2013-08-21 22:14:14 -0700 | [diff] [blame] | 922 | memset(&map_arg, 0, sizeof map_arg); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 923 | map_arg.handle = fb->handle; |
Chris Michael | eb2074a | 2013-05-01 21:26:02 -0400 | [diff] [blame] | 924 | 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] | 925 | if (ret) |
| 926 | goto err_add_fb; |
| 927 | |
Chris Michael | 4a7ce1f | 2015-11-10 10:40:37 -0500 | [diff] [blame] | 928 | fb->map = mmap(NULL, fb->size, PROT_WRITE, |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 929 | MAP_SHARED, b->drm.fd, map_arg.offset); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 930 | if (fb->map == MAP_FAILED) |
| 931 | goto err_add_fb; |
| 932 | |
| 933 | return fb; |
| 934 | |
| 935 | err_add_fb: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 936 | drmModeRmFB(b->drm.fd, fb->fb_id); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 937 | err_bo: |
| 938 | memset(&destroy_arg, 0, sizeof(destroy_arg)); |
| 939 | destroy_arg.handle = create_arg.handle; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 940 | 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] | 941 | err_fb: |
| 942 | free(fb); |
| 943 | return NULL; |
| 944 | } |
| 945 | |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 946 | static struct drm_fb * |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 947 | drm_fb_ref(struct drm_fb *fb) |
| 948 | { |
| 949 | fb->refcnt++; |
| 950 | return fb; |
| 951 | } |
| 952 | |
| 953 | static struct drm_fb * |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 954 | drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend, |
| 955 | uint32_t format, enum drm_fb_type type) |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 956 | { |
| 957 | struct drm_fb *fb = gbm_bo_get_user_data(bo); |
Derek Foreman | 482ffdf | 2016-07-08 12:50:57 -0500 | [diff] [blame] | 958 | 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] | 959 | int ret; |
| 960 | |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 961 | if (fb) { |
| 962 | assert(fb->type == type); |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 963 | return drm_fb_ref(fb); |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 964 | } |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 965 | |
Bryce Harrington | de16d89 | 2014-11-20 22:21:57 -0800 | [diff] [blame] | 966 | fb = zalloc(sizeof *fb); |
| 967 | if (fb == NULL) |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 968 | return NULL; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 969 | |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 970 | fb->type = type; |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 971 | fb->refcnt = 1; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 972 | fb->bo = bo; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 973 | |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 974 | fb->width = gbm_bo_get_width(bo); |
| 975 | fb->height = gbm_bo_get_height(bo); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 976 | fb->stride = gbm_bo_get_stride(bo); |
| 977 | fb->handle = gbm_bo_get_handle(bo).u32; |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 978 | fb->format = pixel_format_get_info(format); |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 979 | fb->size = fb->stride * fb->height; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 980 | fb->fd = backend->drm.fd; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 981 | |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 982 | if (!fb->format) { |
| 983 | weston_log("couldn't look up format 0x%lx\n", |
| 984 | (unsigned long) format); |
| 985 | goto err_free; |
| 986 | } |
| 987 | |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 988 | if (backend->min_width > fb->width || |
| 989 | fb->width > backend->max_width || |
| 990 | backend->min_height > fb->height || |
| 991 | fb->height > backend->max_height) { |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 992 | weston_log("bo geometry out of bounds\n"); |
| 993 | goto err_free; |
| 994 | } |
| 995 | |
| 996 | ret = -1; |
| 997 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 998 | if (format && !backend->no_addfb2) { |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 999 | handles[0] = fb->handle; |
| 1000 | pitches[0] = fb->stride; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1001 | offsets[0] = 0; |
| 1002 | |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 1003 | ret = drmModeAddFB2(backend->drm.fd, fb->width, fb->height, |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1004 | format, handles, pitches, offsets, |
| 1005 | &fb->fb_id, 0); |
| 1006 | if (ret) { |
| 1007 | weston_log("addfb2 failed: %m\n"); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1008 | backend->no_addfb2 = 1; |
| 1009 | backend->sprites_are_broken = 1; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1010 | } |
| 1011 | } |
| 1012 | |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 1013 | if (ret && fb->format->depth && fb->format->bpp) |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 1014 | ret = drmModeAddFB(backend->drm.fd, fb->width, fb->height, |
Daniel Stone | 0b70fa4 | 2017-04-04 17:54:23 +0100 | [diff] [blame] | 1015 | fb->format->depth, fb->format->bpp, |
| 1016 | fb->stride, fb->handle, &fb->fb_id); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1017 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1018 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1019 | weston_log("failed to create kms fb: %m\n"); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1020 | goto err_free; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1021 | } |
| 1022 | |
Tomohito Esaki | 576f42e | 2017-04-04 17:54:24 +0100 | [diff] [blame] | 1023 | 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] | 1024 | |
| 1025 | return fb; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1026 | |
| 1027 | err_free: |
| 1028 | free(fb); |
| 1029 | return NULL; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | static void |
Jason Ekstrand | 6bd6294 | 2013-06-20 20:38:23 -0500 | [diff] [blame] | 1033 | 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] | 1034 | { |
Pekka Paalanen | de685b8 | 2012-12-04 15:58:12 +0200 | [diff] [blame] | 1035 | assert(fb->buffer_ref.buffer == NULL); |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 1036 | assert(fb->type == BUFFER_CLIENT); |
Pekka Paalanen | de685b8 | 2012-12-04 15:58:12 +0200 | [diff] [blame] | 1037 | weston_buffer_reference(&fb->buffer_ref, buffer); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1038 | } |
| 1039 | |
Ander Conselvan de Oliveira | 526d461 | 2013-01-25 15:13:03 +0200 | [diff] [blame] | 1040 | static void |
Daniel Stone | 05a5ac2 | 2017-04-04 17:54:25 +0100 | [diff] [blame] | 1041 | drm_fb_unref(struct drm_fb *fb) |
Ander Conselvan de Oliveira | 526d461 | 2013-01-25 15:13:03 +0200 | [diff] [blame] | 1042 | { |
| 1043 | if (!fb) |
| 1044 | return; |
| 1045 | |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 1046 | assert(fb->refcnt > 0); |
| 1047 | if (--fb->refcnt > 0) |
| 1048 | return; |
| 1049 | |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 1050 | switch (fb->type) { |
| 1051 | case BUFFER_PIXMAN_DUMB: |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 1052 | drm_fb_destroy_dumb(fb); |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 1053 | break; |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 1054 | case BUFFER_CURSOR: |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 1055 | case BUFFER_CLIENT: |
| 1056 | gbm_bo_destroy(fb->bo); |
| 1057 | break; |
| 1058 | case BUFFER_GBM_SURFACE: |
Daniel Stone | 05a5ac2 | 2017-04-04 17:54:25 +0100 | [diff] [blame] | 1059 | gbm_surface_release_buffer(fb->gbm_surface, fb->bo); |
Daniel Stone | fc175a7 | 2017-04-04 17:54:22 +0100 | [diff] [blame] | 1060 | break; |
| 1061 | default: |
| 1062 | assert(NULL); |
| 1063 | break; |
Ander Conselvan de Oliveira | 526d461 | 2013-01-25 15:13:03 +0200 | [diff] [blame] | 1064 | } |
| 1065 | } |
| 1066 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1067 | /** |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1068 | * Allocate a new, empty, plane state. |
| 1069 | */ |
| 1070 | static struct drm_plane_state * |
| 1071 | drm_plane_state_alloc(struct drm_output_state *state_output, |
| 1072 | struct drm_plane *plane) |
| 1073 | { |
| 1074 | struct drm_plane_state *state = zalloc(sizeof(*state)); |
| 1075 | |
| 1076 | assert(state); |
| 1077 | state->output_state = state_output; |
| 1078 | state->plane = plane; |
| 1079 | |
| 1080 | /* Here we only add the plane state to the desired link, and not |
| 1081 | * set the member. Having an output pointer set means that the |
| 1082 | * plane will be displayed on the output; this won't be the case |
| 1083 | * when we go to disable a plane. In this case, it must be part of |
| 1084 | * the commit (and thus the output state), but the member must be |
| 1085 | * NULL, as it will not be on any output when the state takes |
| 1086 | * effect. |
| 1087 | */ |
| 1088 | if (state_output) |
| 1089 | wl_list_insert(&state_output->plane_list, &state->link); |
| 1090 | else |
| 1091 | wl_list_init(&state->link); |
| 1092 | |
| 1093 | return state; |
| 1094 | } |
| 1095 | |
| 1096 | /** |
| 1097 | * Free an existing plane state. As a special case, the state will not |
| 1098 | * normally be freed if it is the current state; see drm_plane_set_state. |
| 1099 | */ |
| 1100 | static void |
| 1101 | drm_plane_state_free(struct drm_plane_state *state, bool force) |
| 1102 | { |
| 1103 | if (!state) |
| 1104 | return; |
| 1105 | |
| 1106 | wl_list_remove(&state->link); |
| 1107 | wl_list_init(&state->link); |
| 1108 | state->output_state = NULL; |
| 1109 | |
| 1110 | if (force || state != state->plane->state_cur) { |
| 1111 | drm_fb_unref(state->fb); |
| 1112 | free(state); |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | /** |
| 1117 | * Duplicate an existing plane state into a new plane state, storing it within |
| 1118 | * the given output state. If the output state already contains a plane state |
| 1119 | * for the drm_plane referenced by 'src', that plane state is freed first. |
| 1120 | */ |
| 1121 | static struct drm_plane_state * |
| 1122 | drm_plane_state_duplicate(struct drm_output_state *state_output, |
| 1123 | struct drm_plane_state *src) |
| 1124 | { |
| 1125 | struct drm_plane_state *dst = malloc(sizeof(*dst)); |
| 1126 | struct drm_plane_state *old, *tmp; |
| 1127 | |
| 1128 | assert(src); |
| 1129 | assert(dst); |
| 1130 | *dst = *src; |
| 1131 | wl_list_init(&dst->link); |
| 1132 | |
| 1133 | wl_list_for_each_safe(old, tmp, &state_output->plane_list, link) { |
| 1134 | /* Duplicating a plane state into the same output state, so |
| 1135 | * it can replace itself with an identical copy of itself, |
| 1136 | * makes no sense. */ |
| 1137 | assert(old != src); |
| 1138 | if (old->plane == dst->plane) |
| 1139 | drm_plane_state_free(old, false); |
| 1140 | } |
| 1141 | |
| 1142 | wl_list_insert(&state_output->plane_list, &dst->link); |
| 1143 | if (src->fb) |
| 1144 | dst->fb = drm_fb_ref(src->fb); |
| 1145 | dst->output_state = state_output; |
| 1146 | dst->complete = false; |
| 1147 | |
| 1148 | return dst; |
| 1149 | } |
| 1150 | |
| 1151 | /** |
| 1152 | * Remove a plane state from an output state; if the plane was previously |
| 1153 | * enabled, then replace it with a disabling state. This ensures that the |
| 1154 | * output state was untouched from it was before the plane state was |
| 1155 | * modified by the caller of this function. |
| 1156 | * |
| 1157 | * This is required as drm_output_state_get_plane may either allocate a |
| 1158 | * new plane state, in which case this function will just perform a matching |
| 1159 | * drm_plane_state_free, or it may instead repurpose an existing disabling |
| 1160 | * state (if the plane was previously active), in which case this function |
| 1161 | * will reset it. |
| 1162 | */ |
| 1163 | static void |
| 1164 | drm_plane_state_put_back(struct drm_plane_state *state) |
| 1165 | { |
| 1166 | struct drm_output_state *state_output; |
| 1167 | struct drm_plane *plane; |
| 1168 | |
| 1169 | if (!state) |
| 1170 | return; |
| 1171 | |
| 1172 | state_output = state->output_state; |
| 1173 | plane = state->plane; |
| 1174 | drm_plane_state_free(state, false); |
| 1175 | |
| 1176 | /* Plane was previously disabled; no need to keep this temporary |
| 1177 | * state around. */ |
| 1178 | if (!plane->state_cur->fb) |
| 1179 | return; |
| 1180 | |
| 1181 | (void) drm_plane_state_alloc(state_output, plane); |
| 1182 | } |
| 1183 | |
| 1184 | /** |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 1185 | * Return a plane state from a drm_output_state. |
| 1186 | */ |
| 1187 | static struct drm_plane_state * |
| 1188 | drm_output_state_get_existing_plane(struct drm_output_state *state_output, |
| 1189 | struct drm_plane *plane) |
| 1190 | { |
| 1191 | struct drm_plane_state *ps; |
| 1192 | |
| 1193 | wl_list_for_each(ps, &state_output->plane_list, link) { |
| 1194 | if (ps->plane == plane) |
| 1195 | return ps; |
| 1196 | } |
| 1197 | |
| 1198 | return NULL; |
| 1199 | } |
| 1200 | |
| 1201 | /** |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1202 | * Return a plane state from a drm_output_state, either existing or |
| 1203 | * freshly allocated. |
| 1204 | */ |
| 1205 | static struct drm_plane_state * |
| 1206 | drm_output_state_get_plane(struct drm_output_state *state_output, |
| 1207 | struct drm_plane *plane) |
| 1208 | { |
| 1209 | struct drm_plane_state *ps; |
| 1210 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 1211 | ps = drm_output_state_get_existing_plane(state_output, plane); |
| 1212 | if (ps) |
| 1213 | return ps; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1214 | |
| 1215 | return drm_plane_state_alloc(state_output, plane); |
| 1216 | } |
| 1217 | |
| 1218 | /** |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1219 | * Allocate a new, empty drm_output_state. This should not generally be used |
| 1220 | * in the repaint cycle; see drm_output_state_duplicate. |
| 1221 | */ |
| 1222 | static struct drm_output_state * |
| 1223 | drm_output_state_alloc(struct drm_output *output, |
| 1224 | struct drm_pending_state *pending_state) |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 1225 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1226 | struct drm_output_state *state = zalloc(sizeof(*state)); |
| 1227 | |
| 1228 | assert(state); |
| 1229 | state->output = output; |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1230 | state->dpms = WESTON_DPMS_OFF; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1231 | state->pending_state = pending_state; |
| 1232 | if (pending_state) |
| 1233 | wl_list_insert(&pending_state->output_list, &state->link); |
| 1234 | else |
| 1235 | wl_list_init(&state->link); |
| 1236 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1237 | wl_list_init(&state->plane_list); |
| 1238 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1239 | return state; |
| 1240 | } |
| 1241 | |
| 1242 | /** |
| 1243 | * Duplicate an existing drm_output_state into a new one. This is generally |
| 1244 | * used during the repaint cycle, to capture the existing state of an output |
| 1245 | * and modify it to create a new state to be used. |
| 1246 | * |
| 1247 | * The mode determines whether the output will be reset to an a blank state, |
| 1248 | * or an exact mirror of the current state. |
| 1249 | */ |
| 1250 | static struct drm_output_state * |
| 1251 | drm_output_state_duplicate(struct drm_output_state *src, |
| 1252 | struct drm_pending_state *pending_state, |
| 1253 | enum drm_output_state_duplicate_mode plane_mode) |
| 1254 | { |
| 1255 | struct drm_output_state *dst = malloc(sizeof(*dst)); |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1256 | struct drm_plane_state *ps; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1257 | |
| 1258 | assert(dst); |
| 1259 | |
| 1260 | /* Copy the whole structure, then individually modify the |
| 1261 | * pending_state, as well as the list link into our pending |
| 1262 | * state. */ |
| 1263 | *dst = *src; |
| 1264 | |
| 1265 | dst->pending_state = pending_state; |
| 1266 | if (pending_state) |
| 1267 | wl_list_insert(&pending_state->output_list, &dst->link); |
| 1268 | else |
| 1269 | wl_list_init(&dst->link); |
| 1270 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1271 | wl_list_init(&dst->plane_list); |
| 1272 | |
| 1273 | wl_list_for_each(ps, &src->plane_list, link) { |
| 1274 | /* Don't carry planes which are now disabled; these should be |
| 1275 | * free for other outputs to reuse. */ |
| 1276 | if (!ps->output) |
| 1277 | continue; |
| 1278 | |
| 1279 | if (plane_mode == DRM_OUTPUT_STATE_CLEAR_PLANES) |
| 1280 | (void) drm_plane_state_alloc(dst, ps->plane); |
| 1281 | else |
| 1282 | (void) drm_plane_state_duplicate(dst, ps); |
| 1283 | } |
| 1284 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1285 | return dst; |
| 1286 | } |
| 1287 | |
| 1288 | /** |
| 1289 | * Free an unused drm_output_state. |
| 1290 | */ |
| 1291 | static void |
| 1292 | drm_output_state_free(struct drm_output_state *state) |
| 1293 | { |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1294 | struct drm_plane_state *ps, *next; |
| 1295 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1296 | if (!state) |
| 1297 | return; |
| 1298 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1299 | wl_list_for_each_safe(ps, next, &state->plane_list, link) |
| 1300 | drm_plane_state_free(ps, false); |
| 1301 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1302 | wl_list_remove(&state->link); |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1303 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1304 | free(state); |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 1305 | } |
| 1306 | |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 1307 | /** |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1308 | * Get output state to disable output |
| 1309 | * |
| 1310 | * Returns a pointer to an output_state object which can be used to disable |
| 1311 | * an output (e.g. DPMS off). |
| 1312 | * |
| 1313 | * @param pending_state The pending state object owning this update |
| 1314 | * @param output The output to disable |
| 1315 | * @returns A drm_output_state to disable the output |
| 1316 | */ |
| 1317 | static struct drm_output_state * |
| 1318 | drm_output_get_disable_state(struct drm_pending_state *pending_state, |
| 1319 | struct drm_output *output) |
| 1320 | { |
| 1321 | struct drm_output_state *output_state; |
| 1322 | |
| 1323 | output_state = drm_output_state_duplicate(output->state_cur, |
| 1324 | pending_state, |
| 1325 | DRM_OUTPUT_STATE_CLEAR_PLANES); |
| 1326 | output_state->dpms = WESTON_DPMS_OFF; |
| 1327 | |
| 1328 | return output_state; |
| 1329 | } |
| 1330 | |
| 1331 | /** |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 1332 | * Allocate a new drm_pending_state |
| 1333 | * |
| 1334 | * Allocate a new, empty, 'pending state' structure to be used across a |
| 1335 | * repaint cycle or similar. |
| 1336 | * |
| 1337 | * @param backend DRM backend |
| 1338 | * @returns Newly-allocated pending state structure |
| 1339 | */ |
| 1340 | static struct drm_pending_state * |
| 1341 | drm_pending_state_alloc(struct drm_backend *backend) |
| 1342 | { |
| 1343 | struct drm_pending_state *ret; |
| 1344 | |
| 1345 | ret = calloc(1, sizeof(*ret)); |
| 1346 | if (!ret) |
| 1347 | return NULL; |
| 1348 | |
| 1349 | ret->backend = backend; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1350 | wl_list_init(&ret->output_list); |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 1351 | |
| 1352 | return ret; |
| 1353 | } |
| 1354 | |
| 1355 | /** |
| 1356 | * Free a drm_pending_state structure |
| 1357 | * |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1358 | * Frees a pending_state structure, as well as any output_states connected |
| 1359 | * to this pending state. |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 1360 | * |
| 1361 | * @param pending_state Pending state structure to free |
| 1362 | */ |
| 1363 | static void |
| 1364 | drm_pending_state_free(struct drm_pending_state *pending_state) |
| 1365 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1366 | struct drm_output_state *output_state, *tmp; |
| 1367 | |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 1368 | if (!pending_state) |
| 1369 | return; |
| 1370 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1371 | wl_list_for_each_safe(output_state, tmp, &pending_state->output_list, |
| 1372 | link) { |
| 1373 | drm_output_state_free(output_state); |
| 1374 | } |
| 1375 | |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 1376 | free(pending_state); |
| 1377 | } |
| 1378 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1379 | /** |
| 1380 | * Find an output state in a pending state |
| 1381 | * |
| 1382 | * Given a pending_state structure, find the output_state for a particular |
| 1383 | * output. |
| 1384 | * |
| 1385 | * @param pending_state Pending state structure to search |
| 1386 | * @param output Output to find state for |
| 1387 | * @returns Output state if present, or NULL if not |
| 1388 | */ |
| 1389 | static struct drm_output_state * |
| 1390 | drm_pending_state_get_output(struct drm_pending_state *pending_state, |
| 1391 | struct drm_output *output) |
| 1392 | { |
| 1393 | struct drm_output_state *output_state; |
| 1394 | |
| 1395 | wl_list_for_each(output_state, &pending_state->output_list, link) { |
| 1396 | if (output_state->output == output) |
| 1397 | return output_state; |
| 1398 | } |
| 1399 | |
| 1400 | return NULL; |
| 1401 | } |
| 1402 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1403 | static int drm_pending_state_apply_sync(struct drm_pending_state *state); |
| 1404 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1405 | /** |
| 1406 | * Mark a drm_output_state (the output's last state) as complete. This handles |
| 1407 | * any post-completion actions such as updating the repaint timer, disabling the |
| 1408 | * output, and finally freeing the state. |
| 1409 | */ |
| 1410 | static void |
| 1411 | drm_output_update_complete(struct drm_output *output, uint32_t flags, |
| 1412 | unsigned int sec, unsigned int usec) |
| 1413 | { |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1414 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1415 | struct drm_plane_state *ps; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1416 | struct timespec ts; |
| 1417 | |
| 1418 | /* Stop the pageflip timer instead of rearming it here */ |
| 1419 | if (output->pageflip_timer) |
| 1420 | wl_event_source_timer_update(output->pageflip_timer, 0); |
| 1421 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1422 | wl_list_for_each(ps, &output->state_cur->plane_list, link) |
| 1423 | ps->complete = true; |
| 1424 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1425 | drm_output_state_free(output->state_last); |
| 1426 | output->state_last = NULL; |
| 1427 | |
| 1428 | if (output->destroy_pending) { |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1429 | output->destroy_pending = 0; |
| 1430 | output->disable_pending = 0; |
| 1431 | output->dpms_off_pending = 0; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1432 | drm_output_destroy(&output->base); |
| 1433 | return; |
| 1434 | } else if (output->disable_pending) { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1435 | output->disable_pending = 0; |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1436 | output->dpms_off_pending = 0; |
| 1437 | weston_output_disable(&output->base); |
| 1438 | return; |
| 1439 | } else if (output->dpms_off_pending) { |
| 1440 | struct drm_pending_state *pending = drm_pending_state_alloc(b); |
| 1441 | output->dpms_off_pending = 0; |
| 1442 | drm_output_get_disable_state(pending, output); |
| 1443 | drm_pending_state_apply_sync(pending); |
| 1444 | return; |
| 1445 | } else if (output->state_cur->dpms == WESTON_DPMS_OFF && |
| 1446 | output->base.repaint_status != REPAINT_AWAITING_COMPLETION) { |
| 1447 | /* DPMS can happen to us either in the middle of a repaint |
| 1448 | * cycle (when we have painted fresh content, only to throw it |
| 1449 | * away for DPMS off), or at any other random point. If the |
| 1450 | * latter is true, then we cannot go through finish_frame, |
| 1451 | * because the repaint machinery does not expect this. */ |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1452 | return; |
| 1453 | } |
| 1454 | |
| 1455 | ts.tv_sec = sec; |
| 1456 | ts.tv_nsec = usec * 1000; |
| 1457 | weston_output_finish_frame(&output->base, &ts, flags); |
| 1458 | |
| 1459 | /* We can't call this from frame_notify, because the output's |
| 1460 | * repaint needed flag is cleared just after that */ |
| 1461 | if (output->recorder) |
| 1462 | weston_output_schedule_repaint(&output->base); |
| 1463 | } |
| 1464 | |
| 1465 | /** |
| 1466 | * Mark an output state as current on the output, i.e. it has been |
| 1467 | * submitted to the kernel. The mode argument determines whether this |
| 1468 | * update will be applied synchronously (e.g. when calling drmModeSetCrtc), |
| 1469 | * or asynchronously (in which case we wait for events to complete). |
| 1470 | */ |
| 1471 | static void |
| 1472 | drm_output_assign_state(struct drm_output_state *state, |
| 1473 | enum drm_state_apply_mode mode) |
| 1474 | { |
| 1475 | struct drm_output *output = state->output; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 1476 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1477 | struct drm_plane_state *plane_state; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1478 | |
| 1479 | assert(!output->state_last); |
| 1480 | |
| 1481 | if (mode == DRM_STATE_APPLY_ASYNC) |
| 1482 | output->state_last = output->state_cur; |
| 1483 | else |
| 1484 | drm_output_state_free(output->state_cur); |
| 1485 | |
| 1486 | wl_list_remove(&state->link); |
| 1487 | wl_list_init(&state->link); |
| 1488 | state->pending_state = NULL; |
| 1489 | |
| 1490 | output->state_cur = state; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1491 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 1492 | if (b->atomic_modeset && mode == DRM_STATE_APPLY_ASYNC) |
| 1493 | output->atomic_complete_pending = 1; |
| 1494 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1495 | /* Replace state_cur on each affected plane with the new state, being |
| 1496 | * careful to dispose of orphaned (but only orphaned) previous state. |
| 1497 | * If the previous state is not orphaned (still has an output_state |
| 1498 | * attached), it will be disposed of by freeing the output_state. */ |
| 1499 | wl_list_for_each(plane_state, &state->plane_list, link) { |
| 1500 | struct drm_plane *plane = plane_state->plane; |
| 1501 | |
| 1502 | if (plane->state_cur && !plane->state_cur->output_state) |
| 1503 | drm_plane_state_free(plane->state_cur, true); |
| 1504 | plane->state_cur = plane_state; |
| 1505 | |
| 1506 | if (mode != DRM_STATE_APPLY_ASYNC) { |
| 1507 | plane_state->complete = true; |
| 1508 | continue; |
| 1509 | } |
| 1510 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 1511 | if (b->atomic_modeset) |
| 1512 | continue; |
| 1513 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1514 | if (plane->type == WDRM_PLANE_TYPE_OVERLAY) |
| 1515 | output->vblank_pending++; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1516 | else if (plane->type == WDRM_PLANE_TYPE_PRIMARY) |
| 1517 | output->page_flip_pending = 1; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1518 | } |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1521 | static int |
| 1522 | drm_view_transform_supported(struct weston_view *ev) |
| 1523 | { |
| 1524 | return !ev->transform.enabled || |
| 1525 | (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE); |
| 1526 | } |
| 1527 | |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 1528 | static uint32_t |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 1529 | drm_output_check_scanout_format(struct drm_output *output, |
| 1530 | struct weston_surface *es, struct gbm_bo *bo) |
| 1531 | { |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 1532 | uint32_t format; |
| 1533 | pixman_region32_t r; |
| 1534 | |
| 1535 | format = gbm_bo_get_format(bo); |
| 1536 | |
Kristian Høgsberg | 3f97b34 | 2013-10-16 16:08:57 -0700 | [diff] [blame] | 1537 | if (format == GBM_FORMAT_ARGB8888) { |
| 1538 | /* We can scanout an ARGB buffer if the surface's |
| 1539 | * opaque region covers the whole output, but we have |
| 1540 | * to use XRGB as the KMS format code. */ |
Kristian Høgsberg | 1be87e3 | 2014-01-17 14:22:41 -0800 | [diff] [blame] | 1541 | pixman_region32_init_rect(&r, 0, 0, |
| 1542 | output->base.width, |
| 1543 | output->base.height); |
| 1544 | pixman_region32_subtract(&r, &r, &es->opaque); |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 1545 | |
| 1546 | if (!pixman_region32_not_empty(&r)) |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 1547 | format = GBM_FORMAT_XRGB8888; |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 1548 | |
| 1549 | pixman_region32_fini(&r); |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 1550 | } |
Kristian Høgsberg | 3f97b34 | 2013-10-16 16:08:57 -0700 | [diff] [blame] | 1551 | |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 1552 | if (output->gbm_format == format) |
Kristian Høgsberg | 3f97b34 | 2013-10-16 16:08:57 -0700 | [diff] [blame] | 1553 | return format; |
| 1554 | |
| 1555 | return 0; |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 1556 | } |
| 1557 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1558 | static struct weston_plane * |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1559 | drm_output_prepare_scanout_view(struct drm_output_state *output_state, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1560 | struct weston_view *ev) |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 1561 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1562 | struct drm_output *output = output_state->output; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1563 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1564 | struct drm_plane *scanout_plane = output->scanout_plane; |
| 1565 | struct drm_plane_state *state; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1566 | struct weston_buffer *buffer = ev->surface->buffer_ref.buffer; |
Pekka Paalanen | 952b6c8 | 2014-03-14 14:38:15 +0200 | [diff] [blame] | 1567 | struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1568 | struct gbm_bo *bo; |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 1569 | uint32_t format; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 1570 | |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 1571 | /* Don't import buffers which span multiple outputs. */ |
| 1572 | if (ev->output_mask != (1u << output->base.id)) |
| 1573 | return NULL; |
| 1574 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1575 | /* We use GBM to import buffers. */ |
| 1576 | if (b->gbm == NULL) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1577 | return NULL; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 1578 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1579 | if (buffer == NULL) |
| 1580 | return NULL; |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 1581 | if (wl_shm_buffer_get(buffer->resource)) |
| 1582 | return NULL; |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1583 | |
| 1584 | /* Make sure our view is exactly compatible with the output. */ |
| 1585 | if (ev->geometry.x != output->base.x || |
| 1586 | ev->geometry.y != output->base.y) |
| 1587 | return NULL; |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 1588 | if (buffer->width != output->base.current_mode->width || |
| 1589 | buffer->height != output->base.current_mode->height) |
| 1590 | return NULL; |
| 1591 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1592 | if (ev->transform.enabled) |
| 1593 | return NULL; |
Pekka Paalanen | 5580f22 | 2015-02-17 16:33:18 +0200 | [diff] [blame] | 1594 | if (ev->geometry.scissor_enabled) |
| 1595 | return NULL; |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1596 | if (viewport->buffer.transform != output->base.transform) |
| 1597 | return NULL; |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 1598 | if (viewport->buffer.scale != output->base.current_scale) |
| 1599 | return NULL; |
| 1600 | if (!drm_view_transform_supported(ev)) |
| 1601 | return NULL; |
| 1602 | |
| 1603 | if (ev->alpha != 1.0f) |
| 1604 | return NULL; |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1605 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1606 | state = drm_output_state_get_plane(output_state, scanout_plane); |
| 1607 | if (state->fb) { |
| 1608 | /* If there is already a framebuffer on the scanout plane, |
| 1609 | * a client view has already been placed on the scanout |
| 1610 | * view. In that case, do not free or put back the state, |
| 1611 | * but just leave it in place and quietly exit. */ |
| 1612 | return NULL; |
| 1613 | } |
| 1614 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1615 | bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER, |
Kristian Høgsberg | 6399646 | 2013-09-03 22:27:08 -0700 | [diff] [blame] | 1616 | buffer->resource, GBM_BO_USE_SCANOUT); |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 1617 | |
Rob Bradford | 9b10187 | 2012-09-14 23:25:41 +0100 | [diff] [blame] | 1618 | /* Unable to use the buffer for scanout */ |
| 1619 | if (!bo) |
| 1620 | return NULL; |
| 1621 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1622 | format = drm_output_check_scanout_format(output, ev->surface, bo); |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 1623 | if (format == 0) { |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1624 | drm_plane_state_put_back(state); |
Ander Conselvan de Oliveira | a64b15d | 2012-05-02 16:42:22 +0300 | [diff] [blame] | 1625 | gbm_bo_destroy(bo); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1626 | return NULL; |
Ander Conselvan de Oliveira | a64b15d | 2012-05-02 16:42:22 +0300 | [diff] [blame] | 1627 | } |
| 1628 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1629 | state->fb = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT); |
| 1630 | if (!state->fb) { |
| 1631 | drm_plane_state_put_back(state); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1632 | gbm_bo_destroy(bo); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1633 | return NULL; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1634 | } |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 1635 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1636 | drm_fb_set_buffer(state->fb, buffer); |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 1637 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1638 | state->output = output; |
| 1639 | |
| 1640 | state->src_x = 0; |
| 1641 | state->src_y = 0; |
| 1642 | state->src_w = state->fb->width << 16; |
| 1643 | state->src_h = state->fb->height << 16; |
| 1644 | |
| 1645 | state->dest_x = 0; |
| 1646 | state->dest_y = 0; |
| 1647 | state->dest_w = output->base.current_mode->width; |
| 1648 | state->dest_h = output->base.current_mode->height; |
| 1649 | |
| 1650 | return &scanout_plane->base; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 1651 | } |
| 1652 | |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1653 | static struct drm_fb * |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1654 | 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] | 1655 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1656 | struct drm_output *output = state->output; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1657 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1658 | struct gbm_bo *bo; |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1659 | struct drm_fb *ret; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1660 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1661 | output->base.compositor->renderer->repaint_output(&output->base, |
| 1662 | damage); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1663 | |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 1664 | bo = gbm_surface_lock_front_buffer(output->gbm_surface); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1665 | if (!bo) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1666 | weston_log("failed to lock front buffer: %m\n"); |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1667 | return NULL; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1668 | } |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1669 | |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1670 | ret = drm_fb_get_from_bo(bo, b, output->gbm_format, BUFFER_GBM_SURFACE); |
| 1671 | if (!ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1672 | weston_log("failed to get drm_fb for bo\n"); |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 1673 | gbm_surface_release_buffer(output->gbm_surface, bo); |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1674 | return NULL; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1675 | } |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1676 | ret->gbm_surface = output->gbm_surface; |
| 1677 | |
| 1678 | return ret; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1679 | } |
| 1680 | |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1681 | static struct drm_fb * |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1682 | drm_output_render_pixman(struct drm_output_state *state, |
| 1683 | pixman_region32_t *damage) |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1684 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1685 | struct drm_output *output = state->output; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1686 | struct weston_compositor *ec = output->base.compositor; |
| 1687 | pixman_region32_t total_damage, previous_damage; |
| 1688 | |
| 1689 | pixman_region32_init(&total_damage); |
| 1690 | pixman_region32_init(&previous_damage); |
| 1691 | |
| 1692 | pixman_region32_copy(&previous_damage, damage); |
| 1693 | |
| 1694 | pixman_region32_union(&total_damage, damage, &output->previous_damage); |
| 1695 | pixman_region32_copy(&output->previous_damage, &previous_damage); |
| 1696 | |
| 1697 | output->current_image ^= 1; |
| 1698 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1699 | pixman_renderer_output_set_buffer(&output->base, |
| 1700 | output->image[output->current_image]); |
| 1701 | |
| 1702 | ec->renderer->repaint_output(&output->base, &total_damage); |
| 1703 | |
| 1704 | pixman_region32_fini(&total_damage); |
| 1705 | pixman_region32_fini(&previous_damage); |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1706 | |
| 1707 | return drm_fb_ref(output->dumb[output->current_image]); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | static void |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1711 | 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] | 1712 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1713 | struct drm_output *output = state->output; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1714 | struct weston_compositor *c = output->base.compositor; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1715 | struct drm_plane_state *scanout_state; |
Daniel Stone | e95169b | 2016-11-14 17:46:59 +0000 | [diff] [blame] | 1716 | struct drm_plane *scanout_plane = output->scanout_plane; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1717 | struct drm_backend *b = to_drm_backend(c); |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1718 | struct drm_fb *fb; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1719 | |
Daniel Stone | 4e84f7d | 2017-04-04 17:54:29 +0100 | [diff] [blame] | 1720 | /* If we already have a client buffer promoted to scanout, then we don't |
| 1721 | * want to render. */ |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1722 | scanout_state = drm_output_state_get_plane(state, |
| 1723 | output->scanout_plane); |
| 1724 | if (scanout_state->fb) |
Daniel Stone | 4e84f7d | 2017-04-04 17:54:29 +0100 | [diff] [blame] | 1725 | return; |
| 1726 | |
Daniel Stone | e95169b | 2016-11-14 17:46:59 +0000 | [diff] [blame] | 1727 | if (!pixman_region32_not_empty(damage) && |
| 1728 | scanout_plane->state_cur->fb && |
| 1729 | (scanout_plane->state_cur->fb->type == BUFFER_GBM_SURFACE || |
| 1730 | scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB) && |
| 1731 | scanout_plane->state_cur->fb->width == |
| 1732 | output->base.current_mode->width && |
| 1733 | scanout_plane->state_cur->fb->height == |
| 1734 | output->base.current_mode->height) { |
| 1735 | fb = drm_fb_ref(scanout_plane->state_cur->fb); |
| 1736 | } else if (b->use_pixman) { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1737 | fb = drm_output_render_pixman(state, damage); |
Daniel Stone | e95169b | 2016-11-14 17:46:59 +0000 | [diff] [blame] | 1738 | } else { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1739 | fb = drm_output_render_gl(state, damage); |
Daniel Stone | e95169b | 2016-11-14 17:46:59 +0000 | [diff] [blame] | 1740 | } |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1741 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1742 | if (!fb) { |
| 1743 | drm_plane_state_put_back(scanout_state); |
Daniel Stone | 95d48a2 | 2017-04-04 17:54:30 +0100 | [diff] [blame] | 1744 | return; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
| 1747 | scanout_state->fb = fb; |
| 1748 | scanout_state->output = output; |
| 1749 | |
| 1750 | scanout_state->src_x = 0; |
| 1751 | scanout_state->src_y = 0; |
| 1752 | scanout_state->src_w = output->base.current_mode->width << 16; |
| 1753 | scanout_state->src_h = output->base.current_mode->height << 16; |
| 1754 | |
| 1755 | scanout_state->dest_x = 0; |
| 1756 | scanout_state->dest_y = 0; |
| 1757 | scanout_state->dest_w = scanout_state->src_w >> 16; |
| 1758 | scanout_state->dest_h = scanout_state->src_h >> 16; |
| 1759 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1760 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1761 | pixman_region32_subtract(&c->primary_plane.damage, |
| 1762 | &c->primary_plane.damage, damage); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1763 | } |
| 1764 | |
| 1765 | static void |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 1766 | drm_output_set_gamma(struct weston_output *output_base, |
| 1767 | uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b) |
| 1768 | { |
| 1769 | int rc; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1770 | struct drm_output *output = to_drm_output(output_base); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1771 | struct drm_backend *backend = |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1772 | to_drm_backend(output->base.compositor); |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 1773 | |
| 1774 | /* check */ |
| 1775 | if (output_base->gamma_size != size) |
| 1776 | return; |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 1777 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1778 | rc = drmModeCrtcSetGamma(backend->drm.fd, |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 1779 | output->crtc_id, |
| 1780 | size, r, g, b); |
| 1781 | if (rc) |
| 1782 | weston_log("set gamma failed: %m\n"); |
| 1783 | } |
| 1784 | |
Bryce Harrington | ada4f07 | 2015-06-30 13:25:46 -0700 | [diff] [blame] | 1785 | /* Determine the type of vblank synchronization to use for the output. |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 1786 | * |
Bryce Harrington | ada4f07 | 2015-06-30 13:25:46 -0700 | [diff] [blame] | 1787 | * The pipe parameter indicates which CRTC is in use. Knowing this, we |
| 1788 | * can determine which vblank sequence type to use for it. Traditional |
| 1789 | * cards had only two CRTCs, with CRTC 0 using no special flags, and |
| 1790 | * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe |
| 1791 | * parameter indicates this. |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 1792 | * |
Bryce Harrington | ada4f07 | 2015-06-30 13:25:46 -0700 | [diff] [blame] | 1793 | * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between |
| 1794 | * 0-31. If this is non-zero it indicates we're dealing with a |
| 1795 | * multi-gpu situation and we need to calculate the vblank sync |
| 1796 | * using DRM_BLANK_HIGH_CRTC_MASK. |
| 1797 | */ |
Pekka Paalanen | c8a1ff0 | 2015-07-02 15:06:08 +0300 | [diff] [blame] | 1798 | static unsigned int |
| 1799 | drm_waitvblank_pipe(struct drm_output *output) |
Mario Kleiner | 2ab4f4e | 2015-06-21 21:25:13 +0200 | [diff] [blame] | 1800 | { |
| 1801 | if (output->pipe > 1) |
| 1802 | return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) & |
| 1803 | DRM_VBLANK_HIGH_CRTC_MASK; |
| 1804 | else if (output->pipe > 0) |
| 1805 | return DRM_VBLANK_SECONDARY; |
| 1806 | else |
| 1807 | return 0; |
| 1808 | } |
| 1809 | |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 1810 | static int |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 1811 | drm_output_apply_state_legacy(struct drm_output_state *state) |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1812 | { |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1813 | struct drm_output *output = state->output; |
| 1814 | struct drm_backend *backend = to_drm_backend(output->base.compositor); |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1815 | struct drm_plane *scanout_plane = output->scanout_plane; |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1816 | struct drm_property_info *dpms_prop = |
| 1817 | &output->props_conn[WDRM_CONNECTOR_DPMS]; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1818 | struct drm_plane_state *scanout_state; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1819 | struct drm_plane_state *ps; |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 1820 | struct drm_plane *p; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1821 | struct drm_mode *mode; |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1822 | struct timespec now; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1823 | int ret = 0; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1824 | |
Derek Foreman | 2cd87fe | 2017-04-13 13:48:48 -0500 | [diff] [blame] | 1825 | /* If disable_planes is set then assign_planes() wasn't |
| 1826 | * called for this render, so we could still have a stale |
| 1827 | * cursor plane set up. |
| 1828 | */ |
| 1829 | if (output->base.disable_planes) { |
| 1830 | output->cursor_view = NULL; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 1831 | output->cursor_plane->base.x = INT32_MIN; |
| 1832 | output->cursor_plane->base.y = INT32_MIN; |
Derek Foreman | 2cd87fe | 2017-04-13 13:48:48 -0500 | [diff] [blame] | 1833 | } |
| 1834 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1835 | if (state->dpms != WESTON_DPMS_ON) { |
| 1836 | wl_list_for_each(ps, &state->plane_list, link) { |
| 1837 | p = ps->plane; |
| 1838 | assert(ps->fb == NULL); |
| 1839 | assert(ps->output == NULL); |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1840 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1841 | if (p->type != WDRM_PLANE_TYPE_OVERLAY) |
| 1842 | continue; |
| 1843 | |
| 1844 | ret = drmModeSetPlane(backend->drm.fd, p->plane_id, |
| 1845 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
| 1846 | if (ret) |
| 1847 | weston_log("drmModeSetPlane failed disable: %m\n"); |
| 1848 | } |
| 1849 | |
| 1850 | if (output->cursor_plane) { |
| 1851 | ret = drmModeSetCursor(backend->drm.fd, output->crtc_id, |
| 1852 | 0, 0, 0); |
| 1853 | if (ret) |
| 1854 | weston_log("drmModeSetCursor failed disable: %m\n"); |
| 1855 | } |
| 1856 | |
| 1857 | ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id, 0, 0, 0, |
| 1858 | &output->connector_id, 0, NULL); |
| 1859 | if (ret) |
| 1860 | weston_log("drmModeSetCrtc failed disabling: %m\n"); |
| 1861 | |
| 1862 | drm_output_assign_state(state, DRM_STATE_APPLY_SYNC); |
| 1863 | weston_compositor_read_presentation_clock(output->base.compositor, &now); |
| 1864 | drm_output_update_complete(output, |
| 1865 | WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION, |
| 1866 | now.tv_sec, now.tv_nsec / 1000); |
| 1867 | |
| 1868 | return 0; |
| 1869 | } |
| 1870 | |
| 1871 | scanout_state = |
| 1872 | drm_output_state_get_existing_plane(state, scanout_plane); |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 1873 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1874 | /* The legacy SetCrtc API doesn't allow us to do scaling, and the |
| 1875 | * legacy PageFlip API doesn't allow us to do clipping either. */ |
| 1876 | assert(scanout_state->src_x == 0); |
| 1877 | assert(scanout_state->src_y == 0); |
| 1878 | assert(scanout_state->src_w == |
| 1879 | (unsigned) (output->base.current_mode->width << 16)); |
| 1880 | assert(scanout_state->src_h == |
| 1881 | (unsigned) (output->base.current_mode->height << 16)); |
| 1882 | assert(scanout_state->dest_x == 0); |
| 1883 | assert(scanout_state->dest_y == 0); |
| 1884 | assert(scanout_state->dest_w == scanout_state->src_w >> 16); |
| 1885 | assert(scanout_state->dest_h == scanout_state->src_h >> 16); |
| 1886 | |
Daniel Stone | cb04cc4 | 2016-11-16 11:51:27 +0000 | [diff] [blame] | 1887 | mode = to_drm_mode(output->base.current_mode); |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 1888 | if (backend->state_invalid || !scanout_plane->state_cur->fb || |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1889 | scanout_plane->state_cur->fb->stride != scanout_state->fb->stride) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1890 | ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id, |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1891 | scanout_state->fb->fb_id, |
| 1892 | 0, 0, |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1893 | &output->connector_id, 1, |
| 1894 | &mode->mode_info); |
| 1895 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1896 | weston_log("set mode failed: %m\n"); |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1897 | goto err; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1898 | } |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 1899 | } |
| 1900 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1901 | if (drmModePageFlip(backend->drm.fd, output->crtc_id, |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 1902 | scanout_state->fb->fb_id, |
Kristian Høgsberg | 54f14c3 | 2012-01-18 11:47:41 -0500 | [diff] [blame] | 1903 | DRM_MODE_PAGE_FLIP_EVENT, output) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1904 | weston_log("queueing pageflip failed: %m\n"); |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1905 | goto err; |
Kristian Høgsberg | 54f14c3 | 2012-01-18 11:47:41 -0500 | [diff] [blame] | 1906 | } |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 1907 | |
Daniel Stone | 205c0a0 | 2017-04-04 17:54:33 +0100 | [diff] [blame] | 1908 | assert(!output->page_flip_pending); |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 1909 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 1910 | if (output->pageflip_timer) |
| 1911 | wl_event_source_timer_update(output->pageflip_timer, |
| 1912 | backend->pageflip_timeout); |
| 1913 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 1914 | drm_output_set_cursor(state); |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 1915 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1916 | /* |
| 1917 | * Now, update all the sprite surfaces |
| 1918 | */ |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1919 | wl_list_for_each(ps, &state->plane_list, link) { |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1920 | uint32_t flags = 0, fb_id = 0; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1921 | drmVBlank vbl = { |
| 1922 | .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT, |
| 1923 | .request.sequence = 1, |
| 1924 | }; |
| 1925 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1926 | p = ps->plane; |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 1927 | if (p->type != WDRM_PLANE_TYPE_OVERLAY) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1928 | continue; |
| 1929 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1930 | assert(p->state_cur->complete); |
| 1931 | assert(!!p->state_cur->output == !!p->state_cur->fb); |
| 1932 | assert(!p->state_cur->output || p->state_cur->output == output); |
| 1933 | assert(!ps->complete); |
| 1934 | assert(!ps->output || ps->output == output); |
| 1935 | assert(!!ps->output == !!ps->fb); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1936 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1937 | if (ps->fb && !backend->sprites_hidden) |
| 1938 | fb_id = ps->fb->fb_id; |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 1939 | |
| 1940 | ret = drmModeSetPlane(backend->drm.fd, p->plane_id, |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1941 | output->crtc_id, fb_id, flags, |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1942 | ps->dest_x, ps->dest_y, |
| 1943 | ps->dest_w, ps->dest_h, |
| 1944 | ps->src_x, ps->src_y, |
| 1945 | ps->src_w, ps->src_h); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1946 | if (ret) |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1947 | weston_log("setplane failed: %d: %s\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1948 | ret, strerror(errno)); |
| 1949 | |
Mario Kleiner | 2ab4f4e | 2015-06-21 21:25:13 +0200 | [diff] [blame] | 1950 | vbl.request.type |= drm_waitvblank_pipe(output); |
Rob Clark | 5ca1a47 | 2012-08-08 20:27:37 -0500 | [diff] [blame] | 1951 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1952 | /* |
| 1953 | * Queue a vblank signal so we know when the surface |
| 1954 | * becomes active on the display or has been replaced. |
| 1955 | */ |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 1956 | vbl.request.signal = (unsigned long) ps; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1957 | ret = drmWaitVBlank(backend->drm.fd, &vbl); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1958 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1959 | weston_log("vblank event request failed: %d: %s\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1960 | ret, strerror(errno)); |
| 1961 | } |
| 1962 | } |
| 1963 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1964 | if (dpms_prop->prop_id && state->dpms != output->state_cur->dpms) { |
| 1965 | ret = drmModeConnectorSetProperty(backend->drm.fd, |
| 1966 | output->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 | output->base.name); |
| 1972 | } |
| 1973 | } |
| 1974 | |
| 1975 | drm_output_assign_state(state, DRM_STATE_APPLY_ASYNC); |
| 1976 | |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 1977 | return 0; |
| 1978 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1979 | err: |
Kristian Høgsberg | b3955b0 | 2014-01-23 16:25:06 -0800 | [diff] [blame] | 1980 | output->cursor_view = NULL; |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 1981 | drm_output_state_free(state); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 1982 | return -1; |
| 1983 | } |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 1984 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 1985 | #ifdef HAVE_DRM_ATOMIC |
| 1986 | static int |
| 1987 | crtc_add_prop(drmModeAtomicReq *req, struct drm_output *output, |
| 1988 | enum wdrm_crtc_property prop, uint64_t val) |
| 1989 | { |
| 1990 | struct drm_property_info *info = &output->props_crtc[prop]; |
| 1991 | int ret; |
| 1992 | |
| 1993 | if (info->prop_id == 0) |
| 1994 | return -1; |
| 1995 | |
| 1996 | ret = drmModeAtomicAddProperty(req, output->crtc_id, info->prop_id, |
| 1997 | val); |
| 1998 | return (ret <= 0) ? -1 : 0; |
| 1999 | } |
| 2000 | |
| 2001 | static int |
| 2002 | connector_add_prop(drmModeAtomicReq *req, struct drm_output *output, |
| 2003 | enum wdrm_connector_property prop, uint64_t val) |
| 2004 | { |
| 2005 | struct drm_property_info *info = &output->props_conn[prop]; |
| 2006 | int ret; |
| 2007 | |
| 2008 | if (info->prop_id == 0) |
| 2009 | return -1; |
| 2010 | |
| 2011 | ret = drmModeAtomicAddProperty(req, output->connector_id, |
| 2012 | info->prop_id, val); |
| 2013 | return (ret <= 0) ? -1 : 0; |
| 2014 | } |
| 2015 | |
| 2016 | static int |
| 2017 | plane_add_prop(drmModeAtomicReq *req, struct drm_plane *plane, |
| 2018 | enum wdrm_plane_property prop, uint64_t val) |
| 2019 | { |
| 2020 | struct drm_property_info *info = &plane->props[prop]; |
| 2021 | int ret; |
| 2022 | |
| 2023 | if (info->prop_id == 0) |
| 2024 | return -1; |
| 2025 | |
| 2026 | ret = drmModeAtomicAddProperty(req, plane->plane_id, info->prop_id, |
| 2027 | val); |
| 2028 | return (ret <= 0) ? -1 : 0; |
| 2029 | } |
| 2030 | |
| 2031 | static int |
| 2032 | drm_mode_ensure_blob(struct drm_backend *backend, struct drm_mode *mode) |
| 2033 | { |
| 2034 | int ret; |
| 2035 | |
| 2036 | if (mode->blob_id) |
| 2037 | return 0; |
| 2038 | |
| 2039 | ret = drmModeCreatePropertyBlob(backend->drm.fd, |
| 2040 | &mode->mode_info, |
| 2041 | sizeof(mode->mode_info), |
| 2042 | &mode->blob_id); |
| 2043 | if (ret != 0) |
| 2044 | weston_log("failed to create mode property blob: %m\n"); |
| 2045 | |
| 2046 | return ret; |
| 2047 | } |
| 2048 | |
| 2049 | static int |
| 2050 | drm_output_apply_state_atomic(struct drm_output_state *state, |
| 2051 | drmModeAtomicReq *req, |
| 2052 | uint32_t *flags) |
| 2053 | { |
| 2054 | struct drm_output *output = state->output; |
| 2055 | struct drm_backend *backend = to_drm_backend(output->base.compositor); |
| 2056 | struct drm_plane_state *plane_state; |
| 2057 | struct drm_mode *current_mode = to_drm_mode(output->base.current_mode); |
| 2058 | int ret = 0; |
| 2059 | |
| 2060 | if (state->dpms != output->state_cur->dpms) |
| 2061 | *flags |= DRM_MODE_ATOMIC_ALLOW_MODESET; |
| 2062 | |
| 2063 | if (state->dpms == WESTON_DPMS_ON) { |
| 2064 | ret = drm_mode_ensure_blob(backend, current_mode); |
| 2065 | if (ret != 0) |
| 2066 | return ret; |
| 2067 | |
| 2068 | ret |= crtc_add_prop(req, output, WDRM_CRTC_MODE_ID, |
| 2069 | current_mode->blob_id); |
| 2070 | ret |= crtc_add_prop(req, output, WDRM_CRTC_ACTIVE, 1); |
| 2071 | ret |= connector_add_prop(req, output, WDRM_CONNECTOR_CRTC_ID, |
| 2072 | output->crtc_id); |
| 2073 | } else { |
| 2074 | ret |= crtc_add_prop(req, output, WDRM_CRTC_MODE_ID, 0); |
| 2075 | ret |= crtc_add_prop(req, output, WDRM_CRTC_ACTIVE, 0); |
| 2076 | ret |= connector_add_prop(req, output, WDRM_CONNECTOR_CRTC_ID, |
| 2077 | 0); |
| 2078 | } |
| 2079 | |
| 2080 | if (ret != 0) { |
| 2081 | weston_log("couldn't set atomic CRTC/connector state\n"); |
| 2082 | return ret; |
| 2083 | } |
| 2084 | |
| 2085 | wl_list_for_each(plane_state, &state->plane_list, link) { |
| 2086 | struct drm_plane *plane = plane_state->plane; |
| 2087 | |
| 2088 | ret |= plane_add_prop(req, plane, WDRM_PLANE_FB_ID, |
| 2089 | plane_state->fb ? plane_state->fb->fb_id : 0); |
| 2090 | ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_ID, |
| 2091 | plane_state->fb ? output->crtc_id : 0); |
| 2092 | ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_X, |
| 2093 | plane_state->src_x); |
| 2094 | ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_Y, |
| 2095 | plane_state->src_y); |
| 2096 | ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_W, |
| 2097 | plane_state->src_w); |
| 2098 | ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_H, |
| 2099 | plane_state->src_h); |
| 2100 | ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_X, |
| 2101 | plane_state->dest_x); |
| 2102 | ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_Y, |
| 2103 | plane_state->dest_y); |
| 2104 | ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_W, |
| 2105 | plane_state->dest_w); |
| 2106 | ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_H, |
| 2107 | plane_state->dest_h); |
| 2108 | |
| 2109 | if (ret != 0) { |
| 2110 | weston_log("couldn't set plane state\n"); |
| 2111 | return ret; |
| 2112 | } |
| 2113 | } |
| 2114 | |
| 2115 | return 0; |
| 2116 | } |
| 2117 | |
| 2118 | /** |
| 2119 | * Helper function used only by drm_pending_state_apply, with the same |
| 2120 | * guarantees and constraints as that function. |
| 2121 | */ |
| 2122 | static int |
| 2123 | drm_pending_state_apply_atomic(struct drm_pending_state *pending_state, |
| 2124 | enum drm_state_apply_mode mode) |
| 2125 | { |
| 2126 | struct drm_backend *b = pending_state->backend; |
| 2127 | struct drm_output_state *output_state, *tmp; |
| 2128 | struct drm_plane *plane; |
| 2129 | drmModeAtomicReq *req = drmModeAtomicAlloc(); |
| 2130 | uint32_t flags = 0; |
| 2131 | int ret = 0; |
| 2132 | |
| 2133 | if (!req) |
| 2134 | return -1; |
| 2135 | |
| 2136 | if (b->state_invalid) { |
| 2137 | uint32_t *unused; |
| 2138 | int err; |
| 2139 | |
| 2140 | /* If we need to reset all our state (e.g. because we've |
| 2141 | * just started, or just been VT-switched in), explicitly |
| 2142 | * disable all the CRTCs and connectors we aren't using. */ |
| 2143 | wl_array_for_each(unused, &b->unused_connectors) { |
| 2144 | struct drm_property_info infos[WDRM_CONNECTOR__COUNT]; |
| 2145 | struct drm_property_info *info; |
| 2146 | drmModeObjectProperties *props; |
| 2147 | |
| 2148 | memset(infos, 0, sizeof(infos)); |
| 2149 | |
| 2150 | props = drmModeObjectGetProperties(b->drm.fd, |
| 2151 | *unused, |
| 2152 | DRM_MODE_OBJECT_CONNECTOR); |
| 2153 | if (!props) { |
| 2154 | ret = -1; |
| 2155 | continue; |
| 2156 | } |
| 2157 | |
| 2158 | drm_property_info_populate(b, connector_props, infos, |
| 2159 | WDRM_CONNECTOR__COUNT, |
| 2160 | props); |
| 2161 | drmModeFreeObjectProperties(props); |
| 2162 | |
| 2163 | info = &infos[WDRM_CONNECTOR_CRTC_ID]; |
| 2164 | err = drmModeAtomicAddProperty(req, *unused, |
| 2165 | info->prop_id, 0); |
| 2166 | if (err <= 0) |
| 2167 | ret = -1; |
| 2168 | |
| 2169 | info = &infos[WDRM_CONNECTOR_DPMS]; |
| 2170 | if (info->prop_id > 0) |
| 2171 | err = drmModeAtomicAddProperty(req, *unused, |
| 2172 | info->prop_id, |
| 2173 | DRM_MODE_DPMS_OFF); |
| 2174 | if (err <= 0) |
| 2175 | ret = -1; |
| 2176 | |
| 2177 | drm_property_info_free(infos, WDRM_CONNECTOR__COUNT); |
| 2178 | } |
| 2179 | |
| 2180 | wl_array_for_each(unused, &b->unused_crtcs) { |
| 2181 | struct drm_property_info infos[WDRM_CRTC__COUNT]; |
| 2182 | struct drm_property_info *info; |
| 2183 | drmModeObjectProperties *props; |
| 2184 | uint64_t active; |
| 2185 | |
| 2186 | memset(infos, 0, sizeof(infos)); |
| 2187 | |
| 2188 | /* We can't emit a disable on a CRTC that's already |
| 2189 | * off, as the kernel will refuse to generate an event |
| 2190 | * for an off->off state and fail the commit. |
| 2191 | */ |
| 2192 | props = drmModeObjectGetProperties(b->drm.fd, |
| 2193 | *unused, |
| 2194 | DRM_MODE_OBJECT_CRTC); |
| 2195 | if (!props) { |
| 2196 | ret = -1; |
| 2197 | continue; |
| 2198 | } |
| 2199 | |
| 2200 | drm_property_info_populate(b, crtc_props, infos, |
| 2201 | WDRM_CRTC__COUNT, |
| 2202 | props); |
| 2203 | |
| 2204 | info = &infos[WDRM_CRTC_ACTIVE]; |
| 2205 | active = drm_property_get_value(info, props, 0); |
| 2206 | drmModeFreeObjectProperties(props); |
| 2207 | if (active == 0) { |
| 2208 | drm_property_info_free(infos, WDRM_CRTC__COUNT); |
| 2209 | continue; |
| 2210 | } |
| 2211 | |
| 2212 | err = drmModeAtomicAddProperty(req, *unused, |
| 2213 | info->prop_id, 0); |
| 2214 | if (err <= 0) |
| 2215 | ret = -1; |
| 2216 | |
| 2217 | info = &infos[WDRM_CRTC_MODE_ID]; |
| 2218 | err = drmModeAtomicAddProperty(req, *unused, |
| 2219 | info->prop_id, 0); |
| 2220 | if (err <= 0) |
| 2221 | ret = -1; |
| 2222 | |
| 2223 | drm_property_info_free(infos, WDRM_CRTC__COUNT); |
| 2224 | } |
| 2225 | |
| 2226 | /* Disable all the planes; planes which are being used will |
| 2227 | * override this state in the output-state application. */ |
| 2228 | wl_list_for_each(plane, &b->plane_list, link) { |
| 2229 | plane_add_prop(req, plane, WDRM_PLANE_CRTC_ID, 0); |
| 2230 | plane_add_prop(req, plane, WDRM_PLANE_FB_ID, 0); |
| 2231 | } |
| 2232 | |
| 2233 | flags |= DRM_MODE_ATOMIC_ALLOW_MODESET; |
| 2234 | } |
| 2235 | |
| 2236 | wl_list_for_each(output_state, &pending_state->output_list, link) { |
| 2237 | if (mode == DRM_STATE_APPLY_SYNC) |
| 2238 | assert(output_state->dpms == WESTON_DPMS_OFF); |
| 2239 | ret |= drm_output_apply_state_atomic(output_state, req, &flags); |
| 2240 | } |
| 2241 | |
| 2242 | if (ret != 0) { |
| 2243 | weston_log("atomic: couldn't compile atomic state\n"); |
| 2244 | goto out; |
| 2245 | } |
| 2246 | |
| 2247 | switch (mode) { |
| 2248 | case DRM_STATE_APPLY_SYNC: |
| 2249 | break; |
| 2250 | case DRM_STATE_APPLY_ASYNC: |
| 2251 | flags |= DRM_MODE_PAGE_FLIP_EVENT | DRM_MODE_ATOMIC_NONBLOCK; |
| 2252 | break; |
| 2253 | } |
| 2254 | |
| 2255 | ret = drmModeAtomicCommit(b->drm.fd, req, flags, b); |
| 2256 | if (ret != 0) { |
| 2257 | weston_log("atomic: couldn't commit new state: %m\n"); |
| 2258 | goto out; |
| 2259 | } |
| 2260 | |
| 2261 | wl_list_for_each_safe(output_state, tmp, &pending_state->output_list, |
| 2262 | link) |
| 2263 | drm_output_assign_state(output_state, mode); |
| 2264 | |
| 2265 | b->state_invalid = false; |
| 2266 | |
| 2267 | assert(wl_list_empty(&pending_state->output_list)); |
| 2268 | |
| 2269 | out: |
| 2270 | drmModeAtomicFree(req); |
| 2271 | drm_pending_state_free(pending_state); |
| 2272 | return ret; |
| 2273 | } |
| 2274 | #endif |
| 2275 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2276 | /** |
| 2277 | * Applies all of a pending_state asynchronously: the primary entry point for |
| 2278 | * applying KMS state to a device. Updates the state for all outputs in the |
| 2279 | * pending_state, as well as disabling any unclaimed outputs. |
| 2280 | * |
| 2281 | * Unconditionally takes ownership of pending_state, and clears state_invalid. |
| 2282 | */ |
| 2283 | static int |
| 2284 | drm_pending_state_apply(struct drm_pending_state *pending_state) |
| 2285 | { |
| 2286 | struct drm_backend *b = pending_state->backend; |
| 2287 | struct drm_output_state *output_state, *tmp; |
| 2288 | uint32_t *unused; |
| 2289 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2290 | #ifdef HAVE_DRM_ATOMIC |
| 2291 | if (b->atomic_modeset) |
| 2292 | return drm_pending_state_apply_atomic(pending_state, |
| 2293 | DRM_STATE_APPLY_ASYNC); |
| 2294 | #endif |
| 2295 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2296 | if (b->state_invalid) { |
| 2297 | /* If we need to reset all our state (e.g. because we've |
| 2298 | * just started, or just been VT-switched in), explicitly |
| 2299 | * disable all the CRTCs we aren't using. This also disables |
| 2300 | * all connectors on these CRTCs, so we don't need to do that |
| 2301 | * separately with the pre-atomic API. */ |
| 2302 | wl_array_for_each(unused, &b->unused_crtcs) |
| 2303 | drmModeSetCrtc(b->drm.fd, *unused, 0, 0, 0, NULL, 0, |
| 2304 | NULL); |
| 2305 | } |
| 2306 | |
| 2307 | wl_list_for_each_safe(output_state, tmp, &pending_state->output_list, |
| 2308 | link) { |
| 2309 | struct drm_output *output = output_state->output; |
| 2310 | int ret; |
| 2311 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2312 | ret = drm_output_apply_state_legacy(output_state); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2313 | if (ret != 0) { |
| 2314 | weston_log("Couldn't apply state for output %s\n", |
| 2315 | output->base.name); |
| 2316 | } |
| 2317 | } |
| 2318 | |
| 2319 | b->state_invalid = false; |
| 2320 | |
| 2321 | assert(wl_list_empty(&pending_state->output_list)); |
| 2322 | |
| 2323 | drm_pending_state_free(pending_state); |
| 2324 | |
| 2325 | return 0; |
| 2326 | } |
| 2327 | |
| 2328 | /** |
| 2329 | * The synchronous version of drm_pending_state_apply. May only be used to |
| 2330 | * disable outputs. Does so synchronously: the request is guaranteed to have |
| 2331 | * completed on return, and the output will not be touched afterwards. |
| 2332 | * |
| 2333 | * Unconditionally takes ownership of pending_state, and clears state_invalid. |
| 2334 | */ |
| 2335 | static int |
| 2336 | drm_pending_state_apply_sync(struct drm_pending_state *pending_state) |
| 2337 | { |
| 2338 | struct drm_backend *b = pending_state->backend; |
| 2339 | struct drm_output_state *output_state, *tmp; |
| 2340 | uint32_t *unused; |
| 2341 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2342 | #ifdef HAVE_DRM_ATOMIC |
| 2343 | if (b->atomic_modeset) |
| 2344 | return drm_pending_state_apply_atomic(pending_state, |
| 2345 | DRM_STATE_APPLY_SYNC); |
| 2346 | #endif |
| 2347 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2348 | if (b->state_invalid) { |
| 2349 | /* If we need to reset all our state (e.g. because we've |
| 2350 | * just started, or just been VT-switched in), explicitly |
| 2351 | * disable all the CRTCs we aren't using. This also disables |
| 2352 | * all connectors on these CRTCs, so we don't need to do that |
| 2353 | * separately with the pre-atomic API. */ |
| 2354 | wl_array_for_each(unused, &b->unused_crtcs) |
| 2355 | drmModeSetCrtc(b->drm.fd, *unused, 0, 0, 0, NULL, 0, |
| 2356 | NULL); |
| 2357 | } |
| 2358 | |
| 2359 | wl_list_for_each_safe(output_state, tmp, &pending_state->output_list, |
| 2360 | link) { |
| 2361 | int ret; |
| 2362 | |
| 2363 | assert(output_state->dpms == WESTON_DPMS_OFF); |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2364 | ret = drm_output_apply_state_legacy(output_state); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2365 | if (ret != 0) { |
| 2366 | weston_log("Couldn't apply state for output %s\n", |
| 2367 | output_state->output->base.name); |
| 2368 | } |
| 2369 | } |
| 2370 | |
| 2371 | b->state_invalid = false; |
| 2372 | |
| 2373 | assert(wl_list_empty(&pending_state->output_list)); |
| 2374 | |
| 2375 | drm_pending_state_free(pending_state); |
| 2376 | |
| 2377 | return 0; |
| 2378 | } |
| 2379 | |
| 2380 | static int |
| 2381 | drm_output_repaint(struct weston_output *output_base, |
| 2382 | pixman_region32_t *damage, |
| 2383 | void *repaint_data) |
| 2384 | { |
| 2385 | struct drm_pending_state *pending_state = repaint_data; |
| 2386 | struct drm_output *output = to_drm_output(output_base); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2387 | struct drm_output_state *state = NULL; |
| 2388 | struct drm_plane_state *scanout_state; |
| 2389 | |
| 2390 | if (output->disable_pending || output->destroy_pending) |
| 2391 | goto err; |
| 2392 | |
| 2393 | assert(!output->state_last); |
| 2394 | |
| 2395 | /* If planes have been disabled in the core, we might not have |
| 2396 | * hit assign_planes at all, so might not have valid output state |
| 2397 | * here. */ |
| 2398 | state = drm_pending_state_get_output(pending_state, output); |
| 2399 | if (!state) |
| 2400 | state = drm_output_state_duplicate(output->state_cur, |
| 2401 | pending_state, |
| 2402 | DRM_OUTPUT_STATE_CLEAR_PLANES); |
| 2403 | state->dpms = WESTON_DPMS_ON; |
| 2404 | |
| 2405 | drm_output_render(state, damage); |
| 2406 | scanout_state = drm_output_state_get_plane(state, |
| 2407 | output->scanout_plane); |
| 2408 | if (!scanout_state || !scanout_state->fb) |
| 2409 | goto err; |
| 2410 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2411 | return 0; |
| 2412 | |
| 2413 | err: |
| 2414 | drm_output_state_free(state); |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 2415 | return -1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2416 | } |
| 2417 | |
| 2418 | static void |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2419 | drm_output_start_repaint_loop(struct weston_output *output_base) |
| 2420 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2421 | struct drm_output *output = to_drm_output(output_base); |
Daniel Stone | 8747f95 | 2016-11-29 20:17:32 +0000 | [diff] [blame] | 2422 | struct drm_pending_state *pending_state; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 2423 | struct drm_plane *scanout_plane = output->scanout_plane; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2424 | struct drm_backend *backend = |
| 2425 | to_drm_backend(output_base->compositor); |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 2426 | struct timespec ts, tnow; |
| 2427 | struct timespec vbl2now; |
| 2428 | int64_t refresh_nsec; |
| 2429 | int ret; |
| 2430 | drmVBlank vbl = { |
| 2431 | .request.type = DRM_VBLANK_RELATIVE, |
| 2432 | .request.sequence = 0, |
| 2433 | .request.signal = 0, |
| 2434 | }; |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 2435 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2436 | if (output->disable_pending || output->destroy_pending) |
Xiong Zhang | abd5d47 | 2013-10-11 14:43:07 +0800 | [diff] [blame] | 2437 | return; |
| 2438 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 2439 | if (!output->scanout_plane->state_cur->fb) { |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 2440 | /* We can't page flip if there's no mode set */ |
David Herrmann | 3c688c5 | 2013-10-22 17:11:25 +0200 | [diff] [blame] | 2441 | goto finish_frame; |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 2442 | } |
| 2443 | |
Pekka Paalanen | 6b65d8f | 2017-07-27 13:44:32 +0300 | [diff] [blame] | 2444 | /* Need to smash all state in from scratch; current timings might not |
| 2445 | * be what we want, page flip might not work, etc. |
| 2446 | */ |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 2447 | if (backend->state_invalid) |
Pekka Paalanen | 6b65d8f | 2017-07-27 13:44:32 +0300 | [diff] [blame] | 2448 | goto finish_frame; |
| 2449 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 2450 | assert(scanout_plane->state_cur->output == output); |
| 2451 | |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 2452 | /* Try to get current msc and timestamp via instant query */ |
| 2453 | vbl.request.type |= drm_waitvblank_pipe(output); |
| 2454 | ret = drmWaitVBlank(backend->drm.fd, &vbl); |
| 2455 | |
| 2456 | /* Error ret or zero timestamp means failure to get valid timestamp */ |
| 2457 | if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) { |
| 2458 | ts.tv_sec = vbl.reply.tval_sec; |
| 2459 | ts.tv_nsec = vbl.reply.tval_usec * 1000; |
| 2460 | |
| 2461 | /* Valid timestamp for most recent vblank - not stale? |
| 2462 | * Stale ts could happen on Linux 3.17+, so make sure it |
| 2463 | * is not older than 1 refresh duration since now. |
| 2464 | */ |
| 2465 | weston_compositor_read_presentation_clock(backend->compositor, |
| 2466 | &tnow); |
| 2467 | timespec_sub(&vbl2now, &tnow, &ts); |
| 2468 | refresh_nsec = |
| 2469 | millihz_to_nsec(output->base.current_mode->refresh); |
| 2470 | if (timespec_to_nsec(&vbl2now) < refresh_nsec) { |
| 2471 | drm_output_update_msc(output, vbl.reply.sequence); |
| 2472 | weston_output_finish_frame(output_base, &ts, |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 2473 | WP_PRESENTATION_FEEDBACK_INVALID); |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 2474 | return; |
| 2475 | } |
| 2476 | } |
| 2477 | |
| 2478 | /* Immediate query didn't provide valid timestamp. |
| 2479 | * Use pageflip fallback. |
| 2480 | */ |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2481 | |
Daniel Stone | 205c0a0 | 2017-04-04 17:54:33 +0100 | [diff] [blame] | 2482 | assert(!output->page_flip_pending); |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2483 | assert(!output->state_last); |
| 2484 | |
| 2485 | pending_state = drm_pending_state_alloc(backend); |
Daniel Stone | 8747f95 | 2016-11-29 20:17:32 +0000 | [diff] [blame] | 2486 | drm_output_state_duplicate(output->state_cur, pending_state, |
| 2487 | DRM_OUTPUT_STATE_PRESERVE_PLANES); |
Daniel Stone | 205c0a0 | 2017-04-04 17:54:33 +0100 | [diff] [blame] | 2488 | |
Daniel Stone | 8747f95 | 2016-11-29 20:17:32 +0000 | [diff] [blame] | 2489 | ret = drm_pending_state_apply(pending_state); |
| 2490 | if (ret != 0) { |
| 2491 | weston_log("applying repaint-start state failed: %m\n"); |
David Herrmann | 3c688c5 | 2013-10-22 17:11:25 +0200 | [diff] [blame] | 2492 | goto finish_frame; |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2493 | } |
David Herrmann | 3c688c5 | 2013-10-22 17:11:25 +0200 | [diff] [blame] | 2494 | |
| 2495 | return; |
| 2496 | |
| 2497 | finish_frame: |
| 2498 | /* if we cannot page-flip, immediately finish frame */ |
Daniel Stone | 3615ce1 | 2017-03-01 11:34:05 +0000 | [diff] [blame] | 2499 | weston_output_finish_frame(output_base, NULL, |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 2500 | WP_PRESENTATION_FEEDBACK_INVALID); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2501 | } |
| 2502 | |
| 2503 | static void |
Pekka Paalanen | 641307c | 2014-09-23 22:08:47 -0400 | [diff] [blame] | 2504 | drm_output_update_msc(struct drm_output *output, unsigned int seq) |
| 2505 | { |
| 2506 | uint64_t msc_hi = output->base.msc >> 32; |
| 2507 | |
| 2508 | if (seq < (output->base.msc & 0xffffffff)) |
| 2509 | msc_hi++; |
| 2510 | |
| 2511 | output->base.msc = (msc_hi << 32) + seq; |
| 2512 | } |
| 2513 | |
| 2514 | static void |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2515 | vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec, |
| 2516 | void *data) |
| 2517 | { |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2518 | struct drm_plane_state *ps = (struct drm_plane_state *) data; |
| 2519 | struct drm_output_state *os = ps->output_state; |
| 2520 | struct drm_output *output = os->output; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2521 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 2522 | uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION | |
| 2523 | WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK; |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 2524 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2525 | assert(!b->atomic_modeset); |
| 2526 | |
Pekka Paalanen | 641307c | 2014-09-23 22:08:47 -0400 | [diff] [blame] | 2527 | drm_output_update_msc(output, frame); |
Daniel Stone | 65d87d0 | 2017-04-04 17:54:32 +0100 | [diff] [blame] | 2528 | output->vblank_pending--; |
| 2529 | assert(output->vblank_pending >= 0); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2530 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2531 | assert(ps->fb); |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 2532 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2533 | if (output->page_flip_pending || output->vblank_pending) |
| 2534 | return; |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 2535 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2536 | drm_output_update_complete(output, flags, sec, usec); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2537 | } |
| 2538 | |
| 2539 | static void |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2540 | page_flip_handler(int fd, unsigned int frame, |
| 2541 | unsigned int sec, unsigned int usec, void *data) |
| 2542 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2543 | struct drm_output *output = data; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2544 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 2545 | uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC | |
| 2546 | WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION | |
| 2547 | WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2548 | |
Pekka Paalanen | 641307c | 2014-09-23 22:08:47 -0400 | [diff] [blame] | 2549 | drm_output_update_msc(output, frame); |
| 2550 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2551 | assert(!b->atomic_modeset); |
Daniel Stone | 205c0a0 | 2017-04-04 17:54:33 +0100 | [diff] [blame] | 2552 | assert(output->page_flip_pending); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2553 | output->page_flip_pending = 0; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 2554 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2555 | if (output->vblank_pending) |
| 2556 | return; |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 2557 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2558 | drm_output_update_complete(output, flags, sec, usec); |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 2559 | } |
| 2560 | |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 2561 | /** |
| 2562 | * Begin a new repaint cycle |
| 2563 | * |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2564 | * Called by the core compositor at the beginning of a repaint cycle. Creates |
| 2565 | * a new pending_state structure to own any output state created by individual |
| 2566 | * output repaint functions until the repaint is flushed or cancelled. |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 2567 | */ |
| 2568 | static void * |
| 2569 | drm_repaint_begin(struct weston_compositor *compositor) |
| 2570 | { |
| 2571 | struct drm_backend *b = to_drm_backend(compositor); |
| 2572 | struct drm_pending_state *ret; |
| 2573 | |
| 2574 | ret = drm_pending_state_alloc(b); |
| 2575 | b->repaint_data = ret; |
| 2576 | |
| 2577 | return ret; |
| 2578 | } |
| 2579 | |
| 2580 | /** |
| 2581 | * Flush a repaint set |
| 2582 | * |
| 2583 | * Called by the core compositor when a repaint cycle has been completed |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2584 | * and should be flushed. Frees the pending state, transitioning ownership |
| 2585 | * of the output state from the pending state, to the update itself. When |
| 2586 | * the update completes (see drm_output_update_complete), the output |
| 2587 | * state will be freed. |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 2588 | */ |
| 2589 | static void |
| 2590 | drm_repaint_flush(struct weston_compositor *compositor, void *repaint_data) |
| 2591 | { |
| 2592 | struct drm_backend *b = to_drm_backend(compositor); |
| 2593 | struct drm_pending_state *pending_state = repaint_data; |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 2594 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 2595 | drm_pending_state_apply(pending_state); |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 2596 | b->repaint_data = NULL; |
| 2597 | } |
| 2598 | |
| 2599 | /** |
| 2600 | * Cancel a repaint set |
| 2601 | * |
| 2602 | * Called by the core compositor when a repaint has finished, so the data |
| 2603 | * held across the repaint cycle should be discarded. |
| 2604 | */ |
| 2605 | static void |
| 2606 | drm_repaint_cancel(struct weston_compositor *compositor, void *repaint_data) |
| 2607 | { |
| 2608 | struct drm_backend *b = to_drm_backend(compositor); |
| 2609 | struct drm_pending_state *pending_state = repaint_data; |
| 2610 | |
| 2611 | drm_pending_state_free(pending_state); |
| 2612 | b->repaint_data = NULL; |
| 2613 | } |
| 2614 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 2615 | #ifdef HAVE_DRM_ATOMIC |
| 2616 | static void |
| 2617 | atomic_flip_handler(int fd, unsigned int frame, unsigned int sec, |
| 2618 | unsigned int usec, unsigned int crtc_id, void *data) |
| 2619 | { |
| 2620 | struct drm_backend *b = data; |
| 2621 | struct drm_output *output = drm_output_find_by_crtc(b, crtc_id); |
| 2622 | uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC | |
| 2623 | WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION | |
| 2624 | WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK; |
| 2625 | |
| 2626 | /* During the initial modeset, we can disable CRTCs which we don't |
| 2627 | * actually handle during normal operation; this will give us events |
| 2628 | * for unknown outputs. Ignore them. */ |
| 2629 | if (!output || !output->base.enabled) |
| 2630 | return; |
| 2631 | |
| 2632 | drm_output_update_msc(output, frame); |
| 2633 | |
| 2634 | assert(b->atomic_modeset); |
| 2635 | assert(output->atomic_complete_pending); |
| 2636 | output->atomic_complete_pending = 0; |
| 2637 | |
| 2638 | drm_output_update_complete(output, flags, sec, usec); |
| 2639 | } |
| 2640 | #endif |
| 2641 | |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 2642 | static uint32_t |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2643 | drm_output_check_plane_format(struct drm_plane *p, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2644 | struct weston_view *ev, struct gbm_bo *bo) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2645 | { |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 2646 | uint32_t i, format; |
| 2647 | |
| 2648 | format = gbm_bo_get_format(bo); |
| 2649 | |
| 2650 | if (format == GBM_FORMAT_ARGB8888) { |
| 2651 | pixman_region32_t r; |
| 2652 | |
Kristian Høgsberg | 63093a3 | 2013-03-01 14:29:16 -0500 | [diff] [blame] | 2653 | pixman_region32_init_rect(&r, 0, 0, |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 2654 | ev->surface->width, |
| 2655 | ev->surface->height); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2656 | pixman_region32_subtract(&r, &r, &ev->surface->opaque); |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 2657 | |
| 2658 | if (!pixman_region32_not_empty(&r)) |
| 2659 | format = GBM_FORMAT_XRGB8888; |
| 2660 | |
| 2661 | pixman_region32_fini(&r); |
| 2662 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2663 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2664 | for (i = 0; i < p->count_formats; i++) |
| 2665 | if (p->formats[i] == format) |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 2666 | return format; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2667 | |
| 2668 | return 0; |
| 2669 | } |
| 2670 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 2671 | static struct weston_plane * |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2672 | drm_output_prepare_overlay_view(struct drm_output_state *output_state, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2673 | struct weston_view *ev) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2674 | { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 2675 | struct drm_output *output = output_state->output; |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 2676 | struct weston_compositor *ec = output->base.compositor; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2677 | struct drm_backend *b = to_drm_backend(ec); |
Pekka Paalanen | 952b6c8 | 2014-03-14 14:38:15 +0200 | [diff] [blame] | 2678 | struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport; |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 2679 | struct wl_resource *buffer_resource; |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2680 | struct drm_plane *p; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2681 | struct drm_plane_state *state = NULL; |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 2682 | struct linux_dmabuf_buffer *dmabuf; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2683 | struct gbm_bo *bo; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2684 | pixman_region32_t dest_rect, src_rect; |
Ander Conselvan de Oliveira | 409eebf | 2012-12-05 15:14:04 +0200 | [diff] [blame] | 2685 | pixman_box32_t *box, tbox; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2686 | uint32_t format; |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 2687 | wl_fixed_t sx1, sy1, sx2, sy2; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2688 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2689 | if (b->sprites_are_broken) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 2690 | return NULL; |
Kristian Høgsberg | 65bec24 | 2012-03-05 19:57:35 -0500 | [diff] [blame] | 2691 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 2692 | /* Don't import buffers which span multiple outputs. */ |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 2693 | if (ev->output_mask != (1u << output->base.id)) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 2694 | return NULL; |
Ander Conselvan de Oliveira | d450b19 | 2012-06-26 17:09:12 +0300 | [diff] [blame] | 2695 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 2696 | /* We can only import GBM buffers. */ |
| 2697 | if (b->gbm == NULL) |
| 2698 | return NULL; |
| 2699 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2700 | if (ev->surface->buffer_ref.buffer == NULL) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 2701 | return NULL; |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 2702 | buffer_resource = ev->surface->buffer_ref.buffer->resource; |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 2703 | if (wl_shm_buffer_get(buffer_resource)) |
Rob Clark | 702ffae | 2012-08-09 14:18:27 -0500 | [diff] [blame] | 2704 | return NULL; |
| 2705 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 2706 | if (viewport->buffer.transform != output->base.transform) |
| 2707 | return NULL; |
| 2708 | if (viewport->buffer.scale != output->base.current_scale) |
| 2709 | return NULL; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2710 | if (!drm_view_transform_supported(ev)) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 2711 | return NULL; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2712 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 2713 | if (ev->alpha != 1.0f) |
| 2714 | return NULL; |
| 2715 | |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 2716 | wl_list_for_each(p, &b->plane_list, link) { |
| 2717 | if (p->type != WDRM_PLANE_TYPE_OVERLAY) |
| 2718 | continue; |
| 2719 | |
Daniel Stone | 5ff289a | 2017-10-07 12:59:02 +0100 | [diff] [blame] | 2720 | if (!drm_plane_is_available(p, output)) |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2721 | continue; |
| 2722 | |
| 2723 | state = drm_output_state_get_plane(output_state, p); |
| 2724 | if (state->fb) { |
| 2725 | state = NULL; |
| 2726 | continue; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2727 | } |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2728 | |
| 2729 | break; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2730 | } |
| 2731 | |
| 2732 | /* No sprites available */ |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2733 | if (!state) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 2734 | return NULL; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2735 | |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 2736 | if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) { |
Bryce Harrington | a358207 | 2015-08-14 12:23:13 -0700 | [diff] [blame] | 2737 | #ifdef HAVE_GBM_FD_IMPORT |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 2738 | /* XXX: TODO: |
| 2739 | * |
| 2740 | * Use AddFB2 directly, do not go via GBM. |
| 2741 | * Add support for multiplanar formats. |
| 2742 | * Both require refactoring in the DRM-backend to |
| 2743 | * support a mix of gbm_bos and drmfbs. |
| 2744 | */ |
| 2745 | struct gbm_import_fd_data gbm_dmabuf = { |
Emmanuel Gil Peyrot | c399692 | 2015-11-24 19:28:24 +0000 | [diff] [blame] | 2746 | .fd = dmabuf->attributes.fd[0], |
| 2747 | .width = dmabuf->attributes.width, |
| 2748 | .height = dmabuf->attributes.height, |
| 2749 | .stride = dmabuf->attributes.stride[0], |
| 2750 | .format = dmabuf->attributes.format |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 2751 | }; |
| 2752 | |
Micah Fedke | c889012 | 2017-02-01 15:28:23 -0500 | [diff] [blame] | 2753 | /* XXX: TODO: |
| 2754 | * |
| 2755 | * Currently the buffer is rejected if any dmabuf attribute |
| 2756 | * flag is set. This keeps us from passing an inverted / |
| 2757 | * interlaced / bottom-first buffer (or any other type that may |
| 2758 | * be added in the future) through to an overlay. Ultimately, |
| 2759 | * these types of buffers should be handled through buffer |
| 2760 | * transforms and not as spot-checks requiring specific |
| 2761 | * knowledge. */ |
| 2762 | if (dmabuf->attributes.n_planes != 1 || |
| 2763 | dmabuf->attributes.offset[0] != 0 || |
| 2764 | dmabuf->attributes.flags) |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 2765 | return NULL; |
| 2766 | |
| 2767 | bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf, |
| 2768 | GBM_BO_USE_SCANOUT); |
Bryce Harrington | a358207 | 2015-08-14 12:23:13 -0700 | [diff] [blame] | 2769 | #else |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2770 | goto err; |
Bryce Harrington | a358207 | 2015-08-14 12:23:13 -0700 | [diff] [blame] | 2771 | #endif |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 2772 | } else { |
| 2773 | bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER, |
| 2774 | buffer_resource, GBM_BO_USE_SCANOUT); |
| 2775 | } |
Kristian Høgsberg | 2763a2e | 2012-07-13 22:54:43 -0400 | [diff] [blame] | 2776 | if (!bo) |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2777 | goto err; |
Kristian Høgsberg | 2763a2e | 2012-07-13 22:54:43 -0400 | [diff] [blame] | 2778 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2779 | format = drm_output_check_plane_format(p, ev, bo); |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2780 | if (format == 0) |
| 2781 | goto err; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2782 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2783 | state->fb = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT); |
| 2784 | if (!state->fb) |
| 2785 | goto err; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 2786 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2787 | drm_fb_set_buffer(state->fb, ev->surface->buffer_ref.buffer); |
| 2788 | |
| 2789 | state->output = output; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2790 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2791 | box = pixman_region32_extents(&ev->transform.boundingbox); |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2792 | p->base.x = box->x1; |
| 2793 | p->base.y = box->y1; |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 2794 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2795 | /* |
| 2796 | * Calculate the source & dest rects properly based on actual |
Derek Foreman | 4b1a0a1 | 2014-09-10 15:37:33 -0500 | [diff] [blame] | 2797 | * position (note the caller has called weston_view_update_transform() |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2798 | * for us already). |
| 2799 | */ |
| 2800 | pixman_region32_init(&dest_rect); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2801 | pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox, |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 2802 | &output->base.region); |
| 2803 | pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2804 | box = pixman_region32_extents(&dest_rect); |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 2805 | tbox = weston_transformed_rect(output->base.width, |
| 2806 | output->base.height, |
| 2807 | output->base.transform, |
| 2808 | output->base.current_scale, |
Alexander Larsson | d9a7bb7 | 2013-05-22 14:41:39 +0200 | [diff] [blame] | 2809 | *box); |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2810 | state->dest_x = tbox.x1; |
| 2811 | state->dest_y = tbox.y1; |
| 2812 | state->dest_w = tbox.x2 - tbox.x1; |
| 2813 | state->dest_h = tbox.y2 - tbox.y1; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2814 | pixman_region32_fini(&dest_rect); |
| 2815 | |
| 2816 | pixman_region32_init(&src_rect); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2817 | pixman_region32_intersect(&src_rect, &ev->transform.boundingbox, |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 2818 | &output->base.region); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2819 | box = pixman_region32_extents(&src_rect); |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 2820 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2821 | weston_view_from_global_fixed(ev, |
| 2822 | wl_fixed_from_int(box->x1), |
| 2823 | wl_fixed_from_int(box->y1), |
| 2824 | &sx1, &sy1); |
| 2825 | weston_view_from_global_fixed(ev, |
| 2826 | wl_fixed_from_int(box->x2), |
| 2827 | wl_fixed_from_int(box->y2), |
| 2828 | &sx2, &sy2); |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 2829 | |
| 2830 | if (sx1 < 0) |
| 2831 | sx1 = 0; |
| 2832 | if (sy1 < 0) |
| 2833 | sy1 = 0; |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 2834 | if (sx2 > wl_fixed_from_int(ev->surface->width)) |
| 2835 | sx2 = wl_fixed_from_int(ev->surface->width); |
| 2836 | if (sy2 > wl_fixed_from_int(ev->surface->height)) |
| 2837 | sy2 = wl_fixed_from_int(ev->surface->height); |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 2838 | |
Ander Conselvan de Oliveira | 409eebf | 2012-12-05 15:14:04 +0200 | [diff] [blame] | 2839 | tbox.x1 = sx1; |
| 2840 | tbox.y1 = sy1; |
| 2841 | tbox.x2 = sx2; |
| 2842 | tbox.y2 = sy2; |
| 2843 | |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 2844 | tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width), |
| 2845 | wl_fixed_from_int(ev->surface->height), |
Pekka Paalanen | 952b6c8 | 2014-03-14 14:38:15 +0200 | [diff] [blame] | 2846 | viewport->buffer.transform, |
| 2847 | viewport->buffer.scale, |
Pekka Paalanen | 1fd9c0f | 2013-11-26 18:19:41 +0100 | [diff] [blame] | 2848 | tbox); |
Ander Conselvan de Oliveira | 409eebf | 2012-12-05 15:14:04 +0200 | [diff] [blame] | 2849 | |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2850 | state->src_x = tbox.x1 << 8; |
| 2851 | state->src_y = tbox.y1 << 8; |
| 2852 | state->src_w = (tbox.x2 - tbox.x1) << 8; |
| 2853 | state->src_h = (tbox.y2 - tbox.y1) << 8; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2854 | pixman_region32_fini(&src_rect); |
| 2855 | |
Daniel Stone | 08d4edf | 2017-04-04 17:54:34 +0100 | [diff] [blame] | 2856 | return &p->base; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 2857 | |
| 2858 | err: |
| 2859 | drm_plane_state_put_back(state); |
| 2860 | if (bo) |
| 2861 | gbm_bo_destroy(bo); |
| 2862 | return NULL; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2863 | } |
| 2864 | |
Pekka Paalanen | d0ead48 | 2014-06-16 12:05:40 +0300 | [diff] [blame] | 2865 | /** |
| 2866 | * Update the image for the current cursor surface |
| 2867 | * |
| 2868 | * @param b DRM backend structure |
| 2869 | * @param bo GBM buffer object to write into |
| 2870 | * @param ev View to use for cursor image |
| 2871 | */ |
| 2872 | static void |
| 2873 | cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo, |
| 2874 | struct weston_view *ev) |
| 2875 | { |
| 2876 | struct weston_buffer *buffer = ev->surface->buffer_ref.buffer; |
| 2877 | uint32_t buf[b->cursor_width * b->cursor_height]; |
| 2878 | int32_t stride; |
| 2879 | uint8_t *s; |
| 2880 | int i; |
| 2881 | |
| 2882 | assert(buffer && buffer->shm_buffer); |
| 2883 | assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource)); |
| 2884 | assert(ev->surface->width <= b->cursor_width); |
| 2885 | assert(ev->surface->height <= b->cursor_height); |
| 2886 | |
| 2887 | memset(buf, 0, sizeof buf); |
| 2888 | stride = wl_shm_buffer_get_stride(buffer->shm_buffer); |
| 2889 | s = wl_shm_buffer_get_data(buffer->shm_buffer); |
| 2890 | |
| 2891 | wl_shm_buffer_begin_access(buffer->shm_buffer); |
| 2892 | for (i = 0; i < ev->surface->height; i++) |
| 2893 | memcpy(buf + i * b->cursor_width, |
| 2894 | s + i * stride, |
| 2895 | ev->surface->width * 4); |
| 2896 | wl_shm_buffer_end_access(buffer->shm_buffer); |
| 2897 | |
| 2898 | if (gbm_bo_write(bo, buf, sizeof buf) < 0) |
| 2899 | weston_log("failed update cursor: %m\n"); |
| 2900 | } |
| 2901 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 2902 | static struct weston_plane * |
| 2903 | drm_output_prepare_cursor_view(struct drm_output_state *output_state, |
| 2904 | struct weston_view *ev) |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 2905 | { |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 2906 | struct drm_output *output = output_state->output; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2907 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 2908 | struct drm_plane *plane = output->cursor_plane; |
| 2909 | struct drm_plane_state *plane_state; |
| 2910 | struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport; |
| 2911 | struct wl_shm_buffer *shmbuf; |
| 2912 | bool needs_update = false; |
Derek Foreman | be428b3 | 2015-11-24 11:39:38 -0600 | [diff] [blame] | 2913 | float x, y; |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 2914 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 2915 | if (!plane) |
| 2916 | return NULL; |
| 2917 | |
| 2918 | if (b->cursors_are_broken) |
| 2919 | return NULL; |
| 2920 | |
| 2921 | if (!plane->state_cur->complete) |
| 2922 | return NULL; |
| 2923 | |
| 2924 | if (plane->state_cur->output && plane->state_cur->output != output) |
| 2925 | return NULL; |
| 2926 | |
| 2927 | /* Don't import buffers which span multiple outputs. */ |
| 2928 | if (ev->output_mask != (1u << output->base.id)) |
| 2929 | return NULL; |
| 2930 | |
| 2931 | /* We use GBM to import SHM buffers. */ |
| 2932 | if (b->gbm == NULL) |
| 2933 | return NULL; |
| 2934 | |
| 2935 | if (ev->surface->buffer_ref.buffer == NULL) |
| 2936 | return NULL; |
| 2937 | shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource); |
| 2938 | if (!shmbuf) |
| 2939 | return NULL; |
| 2940 | if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888) |
| 2941 | return NULL; |
| 2942 | |
| 2943 | if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL) |
| 2944 | return NULL; |
| 2945 | if (ev->transform.enabled && |
| 2946 | (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE)) |
| 2947 | return NULL; |
| 2948 | if (viewport->buffer.scale != output->base.current_scale) |
| 2949 | return NULL; |
| 2950 | if (ev->geometry.scissor_enabled) |
| 2951 | return NULL; |
| 2952 | |
| 2953 | if (ev->surface->width > b->cursor_width || |
| 2954 | ev->surface->height > b->cursor_height) |
| 2955 | return NULL; |
| 2956 | |
| 2957 | plane_state = |
| 2958 | drm_output_state_get_plane(output_state, output->cursor_plane); |
| 2959 | |
| 2960 | if (plane_state && plane_state->fb) |
| 2961 | return NULL; |
| 2962 | |
| 2963 | /* Since we're setting plane state up front, we need to work out |
| 2964 | * whether or not we need to upload a new cursor. We can't use the |
| 2965 | * plane damage, since the planes haven't actually been calculated |
| 2966 | * yet: instead try to figure it out directly. KMS cursor planes are |
| 2967 | * pretty unique here, in that they lie partway between a Weston plane |
| 2968 | * (direct scanout) and a renderer. */ |
| 2969 | if (ev != output->cursor_view || |
| 2970 | pixman_region32_not_empty(&ev->surface->damage)) { |
| 2971 | output->current_cursor++; |
| 2972 | output->current_cursor = |
| 2973 | output->current_cursor % |
| 2974 | ARRAY_LENGTH(output->gbm_cursor_fb); |
| 2975 | needs_update = true; |
| 2976 | } |
| 2977 | |
| 2978 | output->cursor_view = ev; |
| 2979 | weston_view_to_global_float(ev, 0, 0, &x, &y); |
| 2980 | plane->base.x = x; |
| 2981 | plane->base.y = y; |
| 2982 | |
| 2983 | plane_state->fb = |
| 2984 | drm_fb_ref(output->gbm_cursor_fb[output->current_cursor]); |
| 2985 | plane_state->output = output; |
| 2986 | plane_state->src_x = 0; |
| 2987 | plane_state->src_y = 0; |
| 2988 | plane_state->src_w = b->cursor_width << 16; |
| 2989 | plane_state->src_h = b->cursor_height << 16; |
| 2990 | plane_state->dest_x = (x - output->base.x) * output->base.current_scale; |
| 2991 | plane_state->dest_y = (y - output->base.y) * output->base.current_scale; |
| 2992 | plane_state->dest_w = b->cursor_width; |
| 2993 | plane_state->dest_h = b->cursor_height; |
| 2994 | |
| 2995 | if (needs_update) |
| 2996 | cursor_bo_update(b, plane_state->fb->bo, ev); |
| 2997 | |
| 2998 | return &plane->base; |
| 2999 | } |
| 3000 | |
| 3001 | static void |
| 3002 | drm_output_set_cursor(struct drm_output_state *output_state) |
| 3003 | { |
| 3004 | struct drm_output *output = output_state->output; |
| 3005 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
| 3006 | struct drm_plane *plane = output->cursor_plane; |
| 3007 | struct drm_plane_state *state; |
| 3008 | EGLint handle; |
| 3009 | struct gbm_bo *bo; |
| 3010 | |
| 3011 | if (!plane) |
| 3012 | return; |
| 3013 | |
| 3014 | state = drm_output_state_get_existing_plane(output_state, plane); |
| 3015 | if (!state) |
| 3016 | return; |
| 3017 | |
| 3018 | if (!state->fb) { |
| 3019 | pixman_region32_fini(&plane->base.damage); |
| 3020 | pixman_region32_init(&plane->base.damage); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3021 | drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0); |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 3022 | return; |
| 3023 | } |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 3024 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3025 | assert(state->fb == output->gbm_cursor_fb[output->current_cursor]); |
| 3026 | assert(!plane->state_cur->output || plane->state_cur->output == output); |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 3027 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3028 | if (plane->state_cur->fb != state->fb) { |
| 3029 | bo = state->fb->bo; |
Kristian Høgsberg | 1f5de35 | 2012-07-18 12:09:58 -0400 | [diff] [blame] | 3030 | handle = gbm_bo_get_handle(bo).s32; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3031 | if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle, |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3032 | b->cursor_width, b->cursor_height)) { |
Pekka Paalanen | ae29da2 | 2012-08-06 14:57:05 +0300 | [diff] [blame] | 3033 | weston_log("failed to set cursor: %m\n"); |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3034 | goto err; |
Rob Clark | ab5b1e3 | 2012-08-09 13:24:45 -0500 | [diff] [blame] | 3035 | } |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 3036 | } |
| 3037 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3038 | pixman_region32_fini(&plane->base.damage); |
| 3039 | pixman_region32_init(&plane->base.damage); |
Pekka Paalanen | 7eaed40 | 2015-11-27 14:20:58 +0200 | [diff] [blame] | 3040 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3041 | if (drmModeMoveCursor(b->drm.fd, output->crtc_id, |
| 3042 | state->dest_x, state->dest_y)) { |
Daniel Stone | a7cba1d | 2017-04-04 17:54:21 +0100 | [diff] [blame] | 3043 | weston_log("failed to move cursor: %m\n"); |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3044 | goto err; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 3045 | } |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3046 | |
| 3047 | return; |
| 3048 | |
| 3049 | err: |
| 3050 | b->cursors_are_broken = 1; |
| 3051 | drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0); |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 3052 | } |
| 3053 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3054 | static void |
Daniel Stone | b1f166d | 2017-03-01 11:34:10 +0000 | [diff] [blame] | 3055 | drm_assign_planes(struct weston_output *output_base, void *repaint_data) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3056 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 3057 | struct drm_backend *b = to_drm_backend(output_base->compositor); |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 3058 | struct drm_pending_state *pending_state = repaint_data; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 3059 | struct drm_output *output = to_drm_output(output_base); |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 3060 | struct drm_output_state *state; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3061 | struct drm_plane_state *plane_state; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 3062 | struct weston_view *ev, *next; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3063 | pixman_region32_t overlap, surface_overlap; |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 3064 | struct weston_plane *primary, *next_plane; |
Matt Hoosier | df57303 | 2017-08-24 09:24:20 -0500 | [diff] [blame] | 3065 | bool picked_scanout = false; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3066 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 3067 | assert(!output->state_last); |
| 3068 | state = drm_output_state_duplicate(output->state_cur, |
| 3069 | pending_state, |
| 3070 | DRM_OUTPUT_STATE_CLEAR_PLANES); |
| 3071 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3072 | /* |
| 3073 | * Find a surface for each sprite in the output using some heuristics: |
| 3074 | * 1) size |
| 3075 | * 2) frequency of update |
| 3076 | * 3) opacity (though some hw might support alpha blending) |
| 3077 | * 4) clipping (this can be fixed with color keys) |
| 3078 | * |
| 3079 | * The idea is to save on blitting since this should save power. |
| 3080 | * If we can get a large video surface on the sprite for example, |
| 3081 | * the main display surface may not need to update at all, and |
| 3082 | * the client buffer can be used directly for the sprite surface |
| 3083 | * as we do for flipping full screen surfaces. |
| 3084 | */ |
| 3085 | pixman_region32_init(&overlap); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3086 | primary = &output_base->compositor->primary_plane; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 3087 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3088 | wl_list_for_each_safe(ev, next, &output_base->compositor->view_list, link) { |
Ander Conselvan de Oliveira | 895b1fd | 2013-11-19 15:22:05 +0200 | [diff] [blame] | 3089 | struct weston_surface *es = ev->surface; |
| 3090 | |
| 3091 | /* Test whether this buffer can ever go into a plane: |
| 3092 | * non-shm, or small enough to be a cursor. |
| 3093 | * |
| 3094 | * Also, keep a reference when using the pixman renderer. |
| 3095 | * That makes it possible to do a seamless switch to the GL |
| 3096 | * renderer and since the pixman renderer keeps a reference |
| 3097 | * to the buffer anyway, there is no side effects. |
Pekka Paalanen | ccfeae2 | 2012-12-04 15:58:14 +0200 | [diff] [blame] | 3098 | */ |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3099 | if (b->use_pixman || |
Ander Conselvan de Oliveira | 895b1fd | 2013-11-19 15:22:05 +0200 | [diff] [blame] | 3100 | (es->buffer_ref.buffer && |
| 3101 | (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) || |
Derek Foreman | 8743047 | 2015-10-15 10:24:48 -0500 | [diff] [blame] | 3102 | (ev->surface->width <= b->cursor_width && |
| 3103 | ev->surface->height <= b->cursor_height)))) |
Derek Foreman | 0fd6d4e | 2014-10-10 09:36:45 -0500 | [diff] [blame] | 3104 | es->keep_buffer = true; |
Ander Conselvan de Oliveira | 895b1fd | 2013-11-19 15:22:05 +0200 | [diff] [blame] | 3105 | else |
Derek Foreman | 0fd6d4e | 2014-10-10 09:36:45 -0500 | [diff] [blame] | 3106 | es->keep_buffer = false; |
Pekka Paalanen | ccfeae2 | 2012-12-04 15:58:14 +0200 | [diff] [blame] | 3107 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3108 | pixman_region32_init(&surface_overlap); |
| 3109 | pixman_region32_intersect(&surface_overlap, &overlap, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 3110 | &ev->transform.boundingbox); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3111 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 3112 | next_plane = NULL; |
Matt Hoosier | df57303 | 2017-08-24 09:24:20 -0500 | [diff] [blame] | 3113 | if (pixman_region32_not_empty(&surface_overlap) || picked_scanout) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 3114 | next_plane = primary; |
| 3115 | if (next_plane == NULL) |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 3116 | next_plane = drm_output_prepare_cursor_view(state, ev); |
Matt Hoosier | df57303 | 2017-08-24 09:24:20 -0500 | [diff] [blame] | 3117 | |
| 3118 | /* If a higher-stacked view already got assigned to scanout, it's incorrect to |
| 3119 | * assign a subsequent (lower-stacked) view to scanout. |
| 3120 | */ |
| 3121 | if (next_plane == NULL) { |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 3122 | next_plane = drm_output_prepare_scanout_view(state, ev); |
Matt Hoosier | df57303 | 2017-08-24 09:24:20 -0500 | [diff] [blame] | 3123 | if (next_plane) |
| 3124 | picked_scanout = true; |
| 3125 | } |
| 3126 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 3127 | if (next_plane == NULL) |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 3128 | next_plane = drm_output_prepare_overlay_view(state, ev); |
Matt Hoosier | df57303 | 2017-08-24 09:24:20 -0500 | [diff] [blame] | 3129 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 3130 | if (next_plane == NULL) |
| 3131 | next_plane = primary; |
Pekka Paalanen | bf0e031 | 2014-12-17 16:20:41 +0200 | [diff] [blame] | 3132 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 3133 | weston_view_move_to_plane(ev, next_plane); |
Pekka Paalanen | bf0e031 | 2014-12-17 16:20:41 +0200 | [diff] [blame] | 3134 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 3135 | if (next_plane == primary) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3136 | pixman_region32_union(&overlap, &overlap, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 3137 | &ev->transform.boundingbox); |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 3138 | |
Pekka Paalanen | bf0e031 | 2014-12-17 16:20:41 +0200 | [diff] [blame] | 3139 | if (next_plane == primary || |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3140 | (output->cursor_plane && |
| 3141 | next_plane == &output->cursor_plane->base)) { |
Pekka Paalanen | bf0e031 | 2014-12-17 16:20:41 +0200 | [diff] [blame] | 3142 | /* cursor plane involves a copy */ |
| 3143 | ev->psf_flags = 0; |
| 3144 | } else { |
| 3145 | /* All other planes are a direct scanout of a |
| 3146 | * single client buffer. |
| 3147 | */ |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 3148 | ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY; |
Pekka Paalanen | bf0e031 | 2014-12-17 16:20:41 +0200 | [diff] [blame] | 3149 | } |
| 3150 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3151 | pixman_region32_fini(&surface_overlap); |
| 3152 | } |
| 3153 | pixman_region32_fini(&overlap); |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3154 | |
| 3155 | /* We rely on output->cursor_view being both an accurate reflection of |
| 3156 | * the cursor plane's state, but also being maintained across repaints |
| 3157 | * to avoid unnecessary damage uploads, per the comment in |
| 3158 | * drm_output_prepare_cursor_view. In the event that we go from having |
| 3159 | * a cursor view to not having a cursor view, we need to clear it. */ |
| 3160 | if (output->cursor_view) { |
| 3161 | plane_state = |
| 3162 | drm_output_state_get_existing_plane(state, |
| 3163 | output->cursor_plane); |
| 3164 | if (!plane_state || !plane_state->fb) |
| 3165 | output->cursor_view = NULL; |
| 3166 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3167 | } |
| 3168 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 3169 | /** |
| 3170 | * Find the closest-matching mode for a given target |
| 3171 | * |
| 3172 | * Given a target mode, find the most suitable mode amongst the output's |
| 3173 | * current mode list to use, preferring the current mode if possible, to |
| 3174 | * avoid an expensive mode switch. |
| 3175 | * |
| 3176 | * @param output DRM output |
| 3177 | * @param target_mode Mode to attempt to match |
| 3178 | * @returns Pointer to a mode from the output's mode list |
| 3179 | */ |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3180 | static struct drm_mode * |
| 3181 | choose_mode (struct drm_output *output, struct weston_mode *target_mode) |
| 3182 | { |
| 3183 | struct drm_mode *tmp_mode = NULL, *mode; |
| 3184 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 3185 | if (output->base.current_mode->width == target_mode->width && |
| 3186 | output->base.current_mode->height == target_mode->height && |
| 3187 | (output->base.current_mode->refresh == target_mode->refresh || |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3188 | target_mode->refresh == 0)) |
Daniel Stone | cb04cc4 | 2016-11-16 11:51:27 +0000 | [diff] [blame] | 3189 | return to_drm_mode(output->base.current_mode); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3190 | |
| 3191 | wl_list_for_each(mode, &output->base.mode_list, base.link) { |
| 3192 | if (mode->mode_info.hdisplay == target_mode->width && |
| 3193 | mode->mode_info.vdisplay == target_mode->height) { |
Mario Kleiner | 872797c | 2015-06-21 21:25:09 +0200 | [diff] [blame] | 3194 | if (mode->base.refresh == target_mode->refresh || |
| 3195 | target_mode->refresh == 0) { |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3196 | return mode; |
Daniel Stone | f556ebe | 2015-05-21 08:28:58 +0100 | [diff] [blame] | 3197 | } else if (!tmp_mode) |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3198 | tmp_mode = mode; |
| 3199 | } |
| 3200 | } |
| 3201 | |
| 3202 | return tmp_mode; |
| 3203 | } |
| 3204 | |
| 3205 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3206 | drm_output_init_egl(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_egl(struct drm_output *output); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3209 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3210 | drm_output_init_pixman(struct drm_output *output, struct drm_backend *b); |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 3211 | static void |
| 3212 | drm_output_fini_pixman(struct drm_output *output); |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 3213 | |
| 3214 | static int |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3215 | drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode) |
| 3216 | { |
Daniel Stone | 02d487a | 2017-10-07 14:01:45 +0100 | [diff] [blame] | 3217 | struct drm_output *output = to_drm_output(output_base); |
| 3218 | struct drm_backend *b = to_drm_backend(output_base->compositor); |
| 3219 | struct drm_mode *drm_mode = choose_mode(output, mode); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3220 | |
| 3221 | if (!drm_mode) { |
Daniel Stone | 02d487a | 2017-10-07 14:01:45 +0100 | [diff] [blame] | 3222 | weston_log("%s: invalid resolution %dx%d\n", |
| 3223 | output_base->name, mode->width, mode->height); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3224 | return -1; |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 3225 | } |
| 3226 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 3227 | if (&drm_mode->base == output->base.current_mode) |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3228 | return 0; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3229 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 3230 | output->base.current_mode->flags = 0; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3231 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 3232 | output->base.current_mode = &drm_mode->base; |
| 3233 | output->base.current_mode->flags = |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3234 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 3235 | |
Daniel Stone | f30a18c | 2017-04-04 17:54:31 +0100 | [diff] [blame] | 3236 | /* XXX: This drops our current buffer too early, before we've started |
| 3237 | * displaying it. Ideally this should be much more atomic and |
| 3238 | * integrated with a full repaint cycle, rather than doing a |
| 3239 | * sledgehammer modeswitch first, and only later showing new |
| 3240 | * content. |
| 3241 | */ |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 3242 | b->state_invalid = true; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3243 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3244 | if (b->use_pixman) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3245 | drm_output_fini_pixman(output); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3246 | if (drm_output_init_pixman(output, b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3247 | weston_log("failed to init output pixman state with " |
| 3248 | "new mode\n"); |
| 3249 | return -1; |
| 3250 | } |
| 3251 | } else { |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 3252 | drm_output_fini_egl(output); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3253 | if (drm_output_init_egl(output, b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3254 | weston_log("failed to init output egl state with " |
| 3255 | "new mode"); |
| 3256 | return -1; |
| 3257 | } |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 3258 | } |
| 3259 | |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3260 | return 0; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 3261 | } |
| 3262 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 3263 | static int |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3264 | on_drm_input(int fd, uint32_t mask, void *data) |
| 3265 | { |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 3266 | #ifdef HAVE_DRM_ATOMIC |
| 3267 | struct drm_backend *b = data; |
| 3268 | #endif |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3269 | drmEventContext evctx; |
| 3270 | |
| 3271 | memset(&evctx, 0, sizeof evctx); |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 3272 | #ifndef HAVE_DRM_ATOMIC |
Emil Velikov | 863e66b | 2017-04-04 18:07:34 +0100 | [diff] [blame] | 3273 | evctx.version = 2; |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 3274 | #else |
| 3275 | evctx.version = 3; |
| 3276 | if (b->atomic_modeset) |
| 3277 | evctx.page_flip_handler2 = atomic_flip_handler; |
| 3278 | else |
| 3279 | #endif |
| 3280 | evctx.page_flip_handler = page_flip_handler; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3281 | evctx.vblank_handler = vblank_handler; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3282 | drmHandleEvent(fd, &evctx); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 3283 | |
| 3284 | return 1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3285 | } |
| 3286 | |
| 3287 | static int |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3288 | init_kms_caps(struct drm_backend *b) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3289 | { |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 3290 | uint64_t cap; |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3291 | int ret; |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 3292 | clockid_t clk_id; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 3293 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3294 | weston_log("using %s\n", b->drm.filename); |
Kristian Høgsberg | b71302e | 2012-05-10 12:28:35 -0400 | [diff] [blame] | 3295 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3296 | ret = drmGetCap(b->drm.fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap); |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 3297 | if (ret == 0 && cap == 1) |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 3298 | clk_id = CLOCK_MONOTONIC; |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 3299 | else |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 3300 | clk_id = CLOCK_REALTIME; |
| 3301 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3302 | if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) { |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 3303 | weston_log("Error: failed to set presentation clock %d.\n", |
| 3304 | clk_id); |
| 3305 | return -1; |
| 3306 | } |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 3307 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3308 | ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_WIDTH, &cap); |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 3309 | if (ret == 0) |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3310 | b->cursor_width = cap; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 3311 | else |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3312 | b->cursor_width = 64; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 3313 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3314 | ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_HEIGHT, &cap); |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 3315 | if (ret == 0) |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3316 | b->cursor_height = cap; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 3317 | else |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3318 | b->cursor_height = 64; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 3319 | |
Daniel Stone | be1090b | 2017-09-06 17:29:57 +0100 | [diff] [blame] | 3320 | if (!getenv("WESTON_DISABLE_UNIVERSAL_PLANES")) { |
| 3321 | ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1); |
| 3322 | b->universal_planes = (ret == 0); |
| 3323 | } |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 3324 | weston_log("DRM: %s universal planes\n", |
| 3325 | b->universal_planes ? "supports" : "does not support"); |
| 3326 | |
Pekka Paalanen | cd011a6 | 2016-11-15 22:07:49 +0000 | [diff] [blame] | 3327 | #ifdef HAVE_DRM_ATOMIC |
| 3328 | if (b->universal_planes && !getenv("WESTON_DISABLE_ATOMIC")) { |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 3329 | ret = drmGetCap(b->drm.fd, DRM_CAP_CRTC_IN_VBLANK_EVENT, &cap); |
| 3330 | if (ret != 0) |
| 3331 | cap = 0; |
Pekka Paalanen | cd011a6 | 2016-11-15 22:07:49 +0000 | [diff] [blame] | 3332 | ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_ATOMIC, 1); |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 3333 | b->atomic_modeset = ((ret == 0) && (cap == 1)); |
Pekka Paalanen | cd011a6 | 2016-11-15 22:07:49 +0000 | [diff] [blame] | 3334 | } |
| 3335 | #endif |
| 3336 | weston_log("DRM: %s atomic modesetting\n", |
| 3337 | b->atomic_modeset ? "supports" : "does not support"); |
| 3338 | |
Ander Conselvan de Oliveira | 2292917 | 2013-01-25 15:13:02 +0200 | [diff] [blame] | 3339 | return 0; |
| 3340 | } |
| 3341 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3342 | static struct gbm_device * |
| 3343 | create_gbm_device(int fd) |
Ander Conselvan de Oliveira | 2292917 | 2013-01-25 15:13:02 +0200 | [diff] [blame] | 3344 | { |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3345 | struct gbm_device *gbm; |
Alexandru DAMIAN | be0ac5b | 2013-10-02 17:51:05 +0100 | [diff] [blame] | 3346 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 3347 | gl_renderer = weston_load_module("gl-renderer.so", |
| 3348 | "gl_renderer_interface"); |
| 3349 | if (!gl_renderer) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3350 | return NULL; |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 3351 | |
| 3352 | /* GBM will load a dri driver, but even though they need symbols from |
| 3353 | * libglapi, in some version of Mesa they are not linked to it. Since |
| 3354 | * only the gl-renderer module links to it, the call above won't make |
| 3355 | * these symbols globally available, and loading the DRI driver fails. |
| 3356 | * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */ |
| 3357 | dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL); |
| 3358 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3359 | gbm = gbm_create_device(fd); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3360 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3361 | return gbm; |
| 3362 | } |
| 3363 | |
Bryce Harrington | c056a98 | 2015-05-19 15:25:18 -0700 | [diff] [blame] | 3364 | /* When initializing EGL, if the preferred buffer format isn't available |
Bryce Harrington | b993998 | 2016-04-15 20:28:26 -0700 | [diff] [blame] | 3365 | * 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] | 3366 | * |
| 3367 | * This returns 0 if substitution isn't possible, but 0 might be a |
| 3368 | * legitimate format for other EGL platforms, so the caller is |
| 3369 | * responsible for checking for 0 before calling gl_renderer->create(). |
| 3370 | * |
| 3371 | * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689 |
| 3372 | * but it's entirely possible we'll see this again on other implementations. |
| 3373 | */ |
| 3374 | static int |
| 3375 | fallback_format_for(uint32_t format) |
| 3376 | { |
| 3377 | switch (format) { |
| 3378 | case GBM_FORMAT_XRGB8888: |
| 3379 | return GBM_FORMAT_ARGB8888; |
| 3380 | case GBM_FORMAT_XRGB2101010: |
| 3381 | return GBM_FORMAT_ARGB2101010; |
| 3382 | default: |
| 3383 | return 0; |
| 3384 | } |
| 3385 | } |
| 3386 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3387 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3388 | drm_backend_create_gl_renderer(struct drm_backend *b) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3389 | { |
Derek Foreman | 6d55637 | 2015-11-04 14:47:33 -0600 | [diff] [blame] | 3390 | EGLint format[3] = { |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 3391 | b->gbm_format, |
| 3392 | fallback_format_for(b->gbm_format), |
Derek Foreman | 6d55637 | 2015-11-04 14:47:33 -0600 | [diff] [blame] | 3393 | 0, |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 3394 | }; |
Derek Foreman | 6d55637 | 2015-11-04 14:47:33 -0600 | [diff] [blame] | 3395 | int n_formats = 2; |
John Kåre Alsaker | ef591aa | 2013-03-02 12:27:39 +0100 | [diff] [blame] | 3396 | |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 3397 | if (format[1]) |
Derek Foreman | 6d55637 | 2015-11-04 14:47:33 -0600 | [diff] [blame] | 3398 | n_formats = 3; |
Miguel A. Vico | dddc670 | 2016-05-18 17:41:07 +0200 | [diff] [blame] | 3399 | if (gl_renderer->display_create(b->compositor, |
| 3400 | EGL_PLATFORM_GBM_KHR, |
| 3401 | (void *)b->gbm, |
Miguel A. Vico | 41700e3 | 2016-05-18 17:47:59 +0200 | [diff] [blame] | 3402 | NULL, |
Miguel A. Vico | dddc670 | 2016-05-18 17:41:07 +0200 | [diff] [blame] | 3403 | gl_renderer->opaque_attribs, |
| 3404 | format, |
| 3405 | n_formats) < 0) { |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3406 | return -1; |
| 3407 | } |
| 3408 | |
| 3409 | return 0; |
| 3410 | } |
| 3411 | |
| 3412 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3413 | init_egl(struct drm_backend *b) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3414 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3415 | b->gbm = create_gbm_device(b->drm.fd); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3416 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3417 | if (!b->gbm) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3418 | return -1; |
| 3419 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3420 | if (drm_backend_create_gl_renderer(b) < 0) { |
| 3421 | gbm_device_destroy(b->gbm); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 3422 | return -1; |
| 3423 | } |
| 3424 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3425 | return 0; |
| 3426 | } |
| 3427 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3428 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3429 | init_pixman(struct drm_backend *b) |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3430 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3431 | return pixman_renderer_init(b->compositor); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3432 | } |
| 3433 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 3434 | /** |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3435 | * Create a drm_plane for a hardware plane |
| 3436 | * |
| 3437 | * Creates one drm_plane structure for a hardware plane, and initialises its |
| 3438 | * properties and formats. |
| 3439 | * |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3440 | * In the absence of universal plane support, where KMS does not explicitly |
| 3441 | * expose the primary and cursor planes to userspace, this may also create |
| 3442 | * an 'internal' plane for internal management. |
| 3443 | * |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3444 | * This function does not add the plane to the list of usable planes in Weston |
| 3445 | * itself; the caller is responsible for this. |
| 3446 | * |
| 3447 | * Call drm_plane_destroy to clean up the plane. |
| 3448 | * |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3449 | * @sa drm_output_find_special_plane |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3450 | * @param b DRM compositor backend |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3451 | * @param kplane DRM plane to create, or NULL if creating internal plane |
| 3452 | * @param output Output to create internal plane for, or NULL |
| 3453 | * @param type Type to use when creating internal plane, or invalid |
| 3454 | * @param format Format to use for internal planes, or 0 |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3455 | */ |
| 3456 | static struct drm_plane * |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3457 | drm_plane_create(struct drm_backend *b, const drmModePlane *kplane, |
| 3458 | struct drm_output *output, enum wdrm_plane_type type, |
| 3459 | uint32_t format) |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3460 | { |
| 3461 | struct drm_plane *plane; |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 3462 | drmModeObjectProperties *props; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3463 | int num_formats = (kplane) ? kplane->count_formats : 1; |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 3464 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3465 | plane = zalloc(sizeof(*plane) + |
| 3466 | (sizeof(uint32_t) * num_formats)); |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3467 | if (!plane) { |
| 3468 | weston_log("%s: out of memory\n", __func__); |
| 3469 | return NULL; |
| 3470 | } |
| 3471 | |
| 3472 | plane->backend = b; |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 3473 | plane->state_cur = drm_plane_state_alloc(NULL, plane); |
| 3474 | plane->state_cur->complete = true; |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3475 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3476 | if (kplane) { |
| 3477 | plane->possible_crtcs = kplane->possible_crtcs; |
| 3478 | plane->plane_id = kplane->plane_id; |
| 3479 | plane->count_formats = kplane->count_formats; |
| 3480 | memcpy(plane->formats, kplane->formats, |
| 3481 | kplane->count_formats * sizeof(kplane->formats[0])); |
| 3482 | |
| 3483 | props = drmModeObjectGetProperties(b->drm.fd, kplane->plane_id, |
| 3484 | DRM_MODE_OBJECT_PLANE); |
| 3485 | if (!props) { |
| 3486 | weston_log("couldn't get plane properties\n"); |
| 3487 | goto err; |
| 3488 | } |
| 3489 | drm_property_info_populate(b, plane_props, plane->props, |
| 3490 | WDRM_PLANE__COUNT, props); |
| 3491 | plane->type = |
| 3492 | drm_property_get_value(&plane->props[WDRM_PLANE_TYPE], |
| 3493 | props, |
| 3494 | WDRM_PLANE_TYPE__COUNT); |
| 3495 | drmModeFreeObjectProperties(props); |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 3496 | } |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3497 | else { |
| 3498 | plane->possible_crtcs = (1 << output->pipe); |
| 3499 | plane->plane_id = 0; |
| 3500 | plane->count_formats = 1; |
| 3501 | plane->formats[0] = format; |
| 3502 | plane->type = type; |
| 3503 | } |
| 3504 | |
| 3505 | if (plane->type == WDRM_PLANE_TYPE__COUNT) |
| 3506 | goto err_props; |
| 3507 | |
| 3508 | /* With universal planes, everything is a DRM plane; without |
| 3509 | * universal planes, the only DRM planes are overlay planes. |
| 3510 | * Everything else is a fake plane. */ |
| 3511 | if (b->universal_planes) { |
| 3512 | assert(kplane); |
| 3513 | } else { |
| 3514 | if (kplane) |
| 3515 | assert(plane->type == WDRM_PLANE_TYPE_OVERLAY); |
| 3516 | else |
| 3517 | assert(plane->type != WDRM_PLANE_TYPE_OVERLAY && |
| 3518 | output); |
| 3519 | } |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 3520 | |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3521 | weston_plane_init(&plane->base, b->compositor, 0, 0); |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 3522 | wl_list_insert(&b->plane_list, &plane->link); |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3523 | |
| 3524 | return plane; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3525 | |
| 3526 | err_props: |
| 3527 | drm_property_info_free(plane->props, WDRM_PLANE__COUNT); |
| 3528 | err: |
| 3529 | drm_plane_state_free(plane->state_cur, true); |
| 3530 | free(plane); |
| 3531 | return NULL; |
| 3532 | } |
| 3533 | |
| 3534 | /** |
| 3535 | * Find, or create, a special-purpose plane |
| 3536 | * |
| 3537 | * Primary and cursor planes are a special case, in that before universal |
| 3538 | * planes, they are driven by non-plane API calls. Without universal plane |
| 3539 | * support, the only way to configure a primary plane is via drmModeSetCrtc, |
| 3540 | * and the only way to configure a cursor plane is drmModeSetCursor2. |
| 3541 | * |
| 3542 | * Although they may actually be regular planes in the hardware, without |
| 3543 | * universal plane support, these planes are not actually exposed to |
| 3544 | * userspace in the regular plane list. |
| 3545 | * |
| 3546 | * However, for ease of internal tracking, we want to manage all planes |
| 3547 | * through the same drm_plane structures. Therefore, when we are running |
| 3548 | * without universal plane support, we create fake drm_plane structures |
| 3549 | * to track these planes. |
| 3550 | * |
| 3551 | * @param b DRM backend |
| 3552 | * @param output Output to use for plane |
| 3553 | * @param type Type of plane |
| 3554 | */ |
| 3555 | static struct drm_plane * |
| 3556 | drm_output_find_special_plane(struct drm_backend *b, struct drm_output *output, |
| 3557 | enum wdrm_plane_type type) |
| 3558 | { |
| 3559 | struct drm_plane *plane; |
| 3560 | |
| 3561 | if (!b->universal_planes) { |
| 3562 | uint32_t format; |
| 3563 | |
| 3564 | switch (type) { |
| 3565 | case WDRM_PLANE_TYPE_CURSOR: |
| 3566 | format = GBM_FORMAT_ARGB8888; |
| 3567 | break; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 3568 | case WDRM_PLANE_TYPE_PRIMARY: |
| 3569 | /* We don't know what formats the primary plane supports |
| 3570 | * before universal planes, so we just assume that the |
| 3571 | * GBM format works; however, this isn't set until after |
| 3572 | * the output is created. */ |
| 3573 | format = 0; |
| 3574 | break; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3575 | default: |
| 3576 | assert(!"invalid type in drm_output_find_special_plane"); |
| 3577 | break; |
| 3578 | } |
| 3579 | |
| 3580 | return drm_plane_create(b, NULL, output, type, format); |
| 3581 | } |
| 3582 | |
| 3583 | wl_list_for_each(plane, &b->plane_list, link) { |
| 3584 | struct drm_output *tmp; |
| 3585 | bool found_elsewhere = false; |
| 3586 | |
| 3587 | if (plane->type != type) |
| 3588 | continue; |
| 3589 | if (!drm_plane_is_available(plane, output)) |
| 3590 | continue; |
| 3591 | |
| 3592 | /* On some platforms, primary/cursor planes can roam |
| 3593 | * between different CRTCs, so make sure we don't claim the |
| 3594 | * same plane for two outputs. */ |
| 3595 | wl_list_for_each(tmp, &b->compositor->pending_output_list, |
| 3596 | base.link) { |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 3597 | if (tmp->cursor_plane == plane || |
| 3598 | tmp->scanout_plane == plane) { |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3599 | found_elsewhere = true; |
| 3600 | break; |
| 3601 | } |
| 3602 | } |
| 3603 | wl_list_for_each(tmp, &b->compositor->output_list, |
| 3604 | base.link) { |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 3605 | if (tmp->cursor_plane == plane || |
| 3606 | tmp->scanout_plane == plane) { |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3607 | found_elsewhere = true; |
| 3608 | break; |
| 3609 | } |
| 3610 | } |
| 3611 | |
| 3612 | if (found_elsewhere) |
| 3613 | continue; |
| 3614 | |
| 3615 | plane->possible_crtcs = (1 << output->pipe); |
| 3616 | return plane; |
| 3617 | } |
| 3618 | |
| 3619 | return NULL; |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3620 | } |
| 3621 | |
| 3622 | /** |
| 3623 | * Destroy one DRM plane |
| 3624 | * |
| 3625 | * Destroy a DRM plane, removing it from screen and releasing its retained |
| 3626 | * buffers in the process. The counterpart to drm_plane_create. |
| 3627 | * |
| 3628 | * @param plane Plane to deallocate (will be freed) |
| 3629 | */ |
| 3630 | static void |
| 3631 | drm_plane_destroy(struct drm_plane *plane) |
| 3632 | { |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3633 | if (plane->type == WDRM_PLANE_TYPE_OVERLAY) |
| 3634 | drmModeSetPlane(plane->backend->drm.fd, plane->plane_id, |
| 3635 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
Daniel Stone | bc15f68 | 2016-11-14 16:57:01 +0000 | [diff] [blame] | 3636 | drm_plane_state_free(plane->state_cur, true); |
Pekka Paalanen | c5de57f | 2015-05-20 23:01:44 +0100 | [diff] [blame] | 3637 | drm_property_info_free(plane->props, WDRM_PLANE__COUNT); |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3638 | weston_plane_release(&plane->base); |
| 3639 | wl_list_remove(&plane->link); |
| 3640 | free(plane); |
| 3641 | } |
| 3642 | |
| 3643 | /** |
| 3644 | * Initialise sprites (overlay planes) |
| 3645 | * |
| 3646 | * Walk the list of provided DRM planes, and add overlay planes. |
| 3647 | * |
| 3648 | * Call destroy_sprites to free these planes. |
| 3649 | * |
| 3650 | * @param b DRM compositor backend |
| 3651 | */ |
| 3652 | static void |
| 3653 | create_sprites(struct drm_backend *b) |
| 3654 | { |
| 3655 | drmModePlaneRes *kplane_res; |
| 3656 | drmModePlane *kplane; |
| 3657 | struct drm_plane *drm_plane; |
| 3658 | uint32_t i; |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3659 | kplane_res = drmModeGetPlaneResources(b->drm.fd); |
| 3660 | if (!kplane_res) { |
| 3661 | weston_log("failed to get plane resources: %s\n", |
| 3662 | strerror(errno)); |
| 3663 | return; |
| 3664 | } |
| 3665 | |
| 3666 | for (i = 0; i < kplane_res->count_planes; i++) { |
| 3667 | kplane = drmModeGetPlane(b->drm.fd, kplane_res->planes[i]); |
| 3668 | if (!kplane) |
| 3669 | continue; |
| 3670 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 3671 | drm_plane = drm_plane_create(b, kplane, NULL, |
| 3672 | WDRM_PLANE_TYPE__COUNT, 0); |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3673 | drmModeFreePlane(kplane); |
| 3674 | if (!drm_plane) |
| 3675 | continue; |
| 3676 | |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 3677 | if (drm_plane->type == WDRM_PLANE_TYPE_OVERLAY) |
| 3678 | weston_compositor_stack_plane(b->compositor, |
| 3679 | &drm_plane->base, |
| 3680 | &b->compositor->primary_plane); |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3681 | } |
| 3682 | |
| 3683 | drmModeFreePlaneResources(kplane_res); |
| 3684 | } |
| 3685 | |
| 3686 | /** |
| 3687 | * Clean up sprites (overlay planes) |
| 3688 | * |
| 3689 | * The counterpart to create_sprites. |
| 3690 | * |
| 3691 | * @param b DRM compositor backend |
| 3692 | */ |
| 3693 | static void |
| 3694 | destroy_sprites(struct drm_backend *b) |
| 3695 | { |
| 3696 | struct drm_plane *plane, *next; |
| 3697 | |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 3698 | wl_list_for_each_safe(plane, next, &b->plane_list, link) |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3699 | drm_plane_destroy(plane); |
| 3700 | } |
| 3701 | |
Pekka Paalanen | dc14fd4 | 2017-11-10 15:31:39 +0200 | [diff] [blame] | 3702 | static uint32_t |
| 3703 | drm_refresh_rate_mHz(const drmModeModeInfo *info) |
| 3704 | { |
| 3705 | uint64_t refresh; |
| 3706 | |
| 3707 | /* Calculate higher precision (mHz) refresh rate */ |
| 3708 | refresh = (info->clock * 1000000LL / info->htotal + |
| 3709 | info->vtotal / 2) / info->vtotal; |
| 3710 | |
| 3711 | if (info->flags & DRM_MODE_FLAG_INTERLACE) |
| 3712 | refresh *= 2; |
| 3713 | if (info->flags & DRM_MODE_FLAG_DBLSCAN) |
| 3714 | refresh /= 2; |
| 3715 | if (info->vscan > 1) |
| 3716 | refresh /= info->vscan; |
| 3717 | |
| 3718 | return refresh; |
| 3719 | } |
| 3720 | |
Pekka Paalanen | ec27271 | 2014-06-05 11:22:25 +0300 | [diff] [blame] | 3721 | /** |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 3722 | * Add a mode to output's mode list |
| 3723 | * |
| 3724 | * Copy the supplied DRM mode into a Weston mode structure, and add it to the |
| 3725 | * output's mode list. |
| 3726 | * |
| 3727 | * @param output DRM output to add mode to |
| 3728 | * @param info DRM mode structure to add |
| 3729 | * @returns Newly-allocated Weston/DRM mode structure |
| 3730 | */ |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 3731 | static struct drm_mode * |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 3732 | drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info) |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 3733 | { |
| 3734 | struct drm_mode *mode; |
| 3735 | |
| 3736 | mode = malloc(sizeof *mode); |
| 3737 | if (mode == NULL) |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 3738 | return NULL; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 3739 | |
| 3740 | mode->base.flags = 0; |
Alexander Larsson | 0b13506 | 2013-05-28 16:23:36 +0200 | [diff] [blame] | 3741 | mode->base.width = info->hdisplay; |
| 3742 | mode->base.height = info->vdisplay; |
Kristian Høgsberg | c4621b0 | 2012-05-10 12:23:53 -0400 | [diff] [blame] | 3743 | |
Pekka Paalanen | dc14fd4 | 2017-11-10 15:31:39 +0200 | [diff] [blame] | 3744 | mode->base.refresh = drm_refresh_rate_mHz(info); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 3745 | mode->mode_info = *info; |
Daniel Stone | d5526cb | 2016-11-16 10:54:10 +0000 | [diff] [blame] | 3746 | mode->blob_id = 0; |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 3747 | |
| 3748 | if (info->type & DRM_MODE_TYPE_PREFERRED) |
| 3749 | mode->base.flags |= WL_OUTPUT_MODE_PREFERRED; |
| 3750 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 3751 | wl_list_insert(output->base.mode_list.prev, &mode->base.link); |
| 3752 | |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 3753 | return mode; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 3754 | } |
| 3755 | |
Daniel Stone | d5526cb | 2016-11-16 10:54:10 +0000 | [diff] [blame] | 3756 | /** |
| 3757 | * Destroys a mode, and removes it from the list. |
| 3758 | */ |
| 3759 | static void |
| 3760 | drm_output_destroy_mode(struct drm_backend *backend, struct drm_mode *mode) |
| 3761 | { |
| 3762 | if (mode->blob_id) |
| 3763 | drmModeDestroyPropertyBlob(backend->drm.fd, mode->blob_id); |
| 3764 | wl_list_remove(&mode->base.link); |
| 3765 | free(mode); |
| 3766 | } |
| 3767 | |
Pekka Paalanen | 383b3af | 2017-09-11 14:40:48 +0300 | [diff] [blame] | 3768 | /** Destroy a list of drm_modes |
| 3769 | * |
| 3770 | * @param backend The backend for releasing mode property blobs. |
| 3771 | * @param mode_list The list linked by drm_mode::base.link. |
| 3772 | */ |
| 3773 | static void |
| 3774 | drm_mode_list_destroy(struct drm_backend *backend, struct wl_list *mode_list) |
| 3775 | { |
| 3776 | struct drm_mode *mode, *next; |
| 3777 | |
| 3778 | wl_list_for_each_safe(mode, next, mode_list, base.link) |
| 3779 | drm_output_destroy_mode(backend, mode); |
| 3780 | } |
| 3781 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 3782 | static int |
| 3783 | drm_subpixel_to_wayland(int drm_value) |
| 3784 | { |
| 3785 | switch (drm_value) { |
| 3786 | default: |
| 3787 | case DRM_MODE_SUBPIXEL_UNKNOWN: |
| 3788 | return WL_OUTPUT_SUBPIXEL_UNKNOWN; |
| 3789 | case DRM_MODE_SUBPIXEL_NONE: |
| 3790 | return WL_OUTPUT_SUBPIXEL_NONE; |
| 3791 | case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB: |
| 3792 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB; |
| 3793 | case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR: |
| 3794 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR; |
| 3795 | case DRM_MODE_SUBPIXEL_VERTICAL_RGB: |
| 3796 | return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB; |
| 3797 | case DRM_MODE_SUBPIXEL_VERTICAL_BGR: |
| 3798 | return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR; |
| 3799 | } |
| 3800 | } |
| 3801 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 3802 | /* returns a value between 0-255 range, where higher is brighter */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3803 | static uint32_t |
| 3804 | drm_get_backlight(struct drm_output *output) |
| 3805 | { |
| 3806 | long brightness, max_brightness, norm; |
| 3807 | |
| 3808 | brightness = backlight_get_brightness(output->backlight); |
| 3809 | max_brightness = backlight_get_max_brightness(output->backlight); |
| 3810 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 3811 | /* convert it on a scale of 0 to 255 */ |
| 3812 | norm = (brightness * 255)/(max_brightness); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3813 | |
| 3814 | return (uint32_t) norm; |
| 3815 | } |
| 3816 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 3817 | /* values accepted are between 0-255 range */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3818 | static void |
| 3819 | drm_set_backlight(struct weston_output *output_base, uint32_t value) |
| 3820 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 3821 | struct drm_output *output = to_drm_output(output_base); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3822 | long max_brightness, new_brightness; |
| 3823 | |
| 3824 | if (!output->backlight) |
| 3825 | return; |
| 3826 | |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 3827 | if (value > 255) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3828 | return; |
| 3829 | |
| 3830 | max_brightness = backlight_get_max_brightness(output->backlight); |
| 3831 | |
| 3832 | /* get denormalized value */ |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 3833 | new_brightness = (value * max_brightness) / 255; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3834 | |
| 3835 | backlight_set_brightness(output->backlight, new_brightness); |
| 3836 | } |
| 3837 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 3838 | /** |
| 3839 | * Power output on or off |
| 3840 | * |
| 3841 | * The DPMS/power level of an output is used to switch it on or off. This |
| 3842 | * is DRM's hook for doing so, which can called either as part of repaint, |
| 3843 | * or independently of the repaint loop. |
| 3844 | * |
| 3845 | * If we are called as part of repaint, we simply set the relevant bit in |
| 3846 | * state and return. |
| 3847 | */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3848 | static void |
| 3849 | drm_set_dpms(struct weston_output *output_base, enum dpms_enum level) |
| 3850 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 3851 | struct drm_output *output = to_drm_output(output_base); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 3852 | struct drm_backend *b = to_drm_backend(output_base->compositor); |
| 3853 | struct drm_pending_state *pending_state = b->repaint_data; |
| 3854 | struct drm_output_state *state; |
Daniel Stone | 36609c7 | 2015-06-18 07:49:02 +0100 | [diff] [blame] | 3855 | int ret; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3856 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 3857 | if (output->state_cur->dpms == level) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3858 | return; |
| 3859 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 3860 | /* If we're being called during the repaint loop, then this is |
| 3861 | * simple: discard any previously-generated state, and create a new |
| 3862 | * state where we disable everything. When we come to flush, this |
| 3863 | * will be applied. |
| 3864 | * |
| 3865 | * However, we need to be careful: we can be called whilst another |
| 3866 | * output is in its repaint cycle (pending_state exists), but our |
| 3867 | * output still has an incomplete state application outstanding. |
| 3868 | * In that case, we need to wait until that completes. */ |
| 3869 | if (pending_state && !output->state_last) { |
| 3870 | /* The repaint loop already sets DPMS on; we don't need to |
| 3871 | * explicitly set it on here, as it will already happen |
| 3872 | * whilst applying the repaint state. */ |
| 3873 | if (level == WESTON_DPMS_ON) |
| 3874 | return; |
| 3875 | |
| 3876 | state = drm_pending_state_get_output(pending_state, output); |
| 3877 | if (state) |
| 3878 | drm_output_state_free(state); |
| 3879 | state = drm_output_get_disable_state(pending_state, output); |
Daniel Stone | 36609c7 | 2015-06-18 07:49:02 +0100 | [diff] [blame] | 3880 | return; |
| 3881 | } |
| 3882 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 3883 | /* As we throw everything away when disabling, just send us back through |
| 3884 | * a repaint cycle. */ |
| 3885 | if (level == WESTON_DPMS_ON) { |
| 3886 | if (output->dpms_off_pending) |
| 3887 | output->dpms_off_pending = 0; |
| 3888 | weston_output_schedule_repaint(output_base); |
| 3889 | return; |
| 3890 | } |
| 3891 | |
| 3892 | /* If we've already got a request in the pipeline, then we need to |
| 3893 | * park our DPMS request until that request has quiesced. */ |
| 3894 | if (output->state_last) { |
| 3895 | output->dpms_off_pending = 1; |
| 3896 | return; |
| 3897 | } |
| 3898 | |
| 3899 | pending_state = drm_pending_state_alloc(b); |
| 3900 | drm_output_get_disable_state(pending_state, output); |
| 3901 | ret = drm_pending_state_apply_sync(pending_state); |
| 3902 | if (ret != 0) |
| 3903 | weston_log("drm_set_dpms: couldn't disable output?\n"); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 3904 | } |
| 3905 | |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 3906 | static const char * const connector_type_names[] = { |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 3907 | [DRM_MODE_CONNECTOR_Unknown] = "Unknown", |
| 3908 | [DRM_MODE_CONNECTOR_VGA] = "VGA", |
| 3909 | [DRM_MODE_CONNECTOR_DVII] = "DVI-I", |
| 3910 | [DRM_MODE_CONNECTOR_DVID] = "DVI-D", |
| 3911 | [DRM_MODE_CONNECTOR_DVIA] = "DVI-A", |
| 3912 | [DRM_MODE_CONNECTOR_Composite] = "Composite", |
| 3913 | [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO", |
| 3914 | [DRM_MODE_CONNECTOR_LVDS] = "LVDS", |
| 3915 | [DRM_MODE_CONNECTOR_Component] = "Component", |
| 3916 | [DRM_MODE_CONNECTOR_9PinDIN] = "DIN", |
| 3917 | [DRM_MODE_CONNECTOR_DisplayPort] = "DP", |
| 3918 | [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A", |
| 3919 | [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B", |
| 3920 | [DRM_MODE_CONNECTOR_TV] = "TV", |
| 3921 | [DRM_MODE_CONNECTOR_eDP] = "eDP", |
Pekka Paalanen | ab81f15 | 2015-08-24 14:27:07 +0300 | [diff] [blame] | 3922 | #ifdef DRM_MODE_CONNECTOR_DSI |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 3923 | [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual", |
| 3924 | [DRM_MODE_CONNECTOR_DSI] = "DSI", |
Pekka Paalanen | ab81f15 | 2015-08-24 14:27:07 +0300 | [diff] [blame] | 3925 | #endif |
Kristian Høgsberg | 2f9ed71 | 2012-07-26 17:57:15 -0400 | [diff] [blame] | 3926 | }; |
| 3927 | |
Pekka Paalanen | 1f21ef1 | 2017-04-03 13:33:26 +0300 | [diff] [blame] | 3928 | /** Create a name given a DRM connector |
| 3929 | * |
| 3930 | * \param con The DRM connector whose type and id form the name. |
| 3931 | * \return A newly allocate string, or NULL on error. Must be free()'d |
| 3932 | * after use. |
| 3933 | * |
| 3934 | * The name does not identify the DRM display device. |
| 3935 | */ |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 3936 | static char * |
| 3937 | make_connector_name(const drmModeConnector *con) |
| 3938 | { |
Pekka Paalanen | 1f21ef1 | 2017-04-03 13:33:26 +0300 | [diff] [blame] | 3939 | char *name; |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 3940 | const char *type_name = NULL; |
Pekka Paalanen | 1f21ef1 | 2017-04-03 13:33:26 +0300 | [diff] [blame] | 3941 | int ret; |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 3942 | |
| 3943 | if (con->connector_type < ARRAY_LENGTH(connector_type_names)) |
| 3944 | type_name = connector_type_names[con->connector_type]; |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 3945 | |
| 3946 | if (!type_name) |
| 3947 | type_name = "UNNAMED"; |
| 3948 | |
Pekka Paalanen | 1f21ef1 | 2017-04-03 13:33:26 +0300 | [diff] [blame] | 3949 | ret = asprintf(&name, "%s-%d", type_name, con->connector_type_id); |
| 3950 | if (ret < 0) |
| 3951 | return NULL; |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 3952 | |
Pekka Paalanen | 1f21ef1 | 2017-04-03 13:33:26 +0300 | [diff] [blame] | 3953 | return name; |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 3954 | } |
| 3955 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3956 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3957 | find_crtc_for_connector(struct drm_backend *b, |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 3958 | drmModeRes *resources, drmModeConnector *connector) |
| 3959 | { |
| 3960 | drmModeEncoder *encoder; |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 3961 | int i, j; |
Daniel Stone | 75487c2 | 2017-01-16 14:33:38 +0000 | [diff] [blame] | 3962 | int ret = -1; |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 3963 | |
| 3964 | for (j = 0; j < connector->count_encoders; j++) { |
Daniel Stone | 75487c2 | 2017-01-16 14:33:38 +0000 | [diff] [blame] | 3965 | uint32_t possible_crtcs, encoder_id, crtc_id; |
| 3966 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3967 | encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]); |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 3968 | if (encoder == NULL) { |
| 3969 | weston_log("Failed to get encoder.\n"); |
Daniel Stone | 75487c2 | 2017-01-16 14:33:38 +0000 | [diff] [blame] | 3970 | continue; |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 3971 | } |
Daniel Stone | 75487c2 | 2017-01-16 14:33:38 +0000 | [diff] [blame] | 3972 | encoder_id = encoder->encoder_id; |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 3973 | possible_crtcs = encoder->possible_crtcs; |
Daniel Stone | 75487c2 | 2017-01-16 14:33:38 +0000 | [diff] [blame] | 3974 | crtc_id = encoder->crtc_id; |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 3975 | drmModeFreeEncoder(encoder); |
| 3976 | |
| 3977 | for (i = 0; i < resources->count_crtcs; i++) { |
Daniel Stone | 72c0e1b | 2017-02-09 13:49:15 +0000 | [diff] [blame] | 3978 | if (!(possible_crtcs & (1 << i))) |
| 3979 | continue; |
| 3980 | |
| 3981 | if (drm_output_find_by_crtc(b, resources->crtcs[i])) |
| 3982 | continue; |
| 3983 | |
Daniel Stone | 75487c2 | 2017-01-16 14:33:38 +0000 | [diff] [blame] | 3984 | /* Try to preserve the existing |
| 3985 | * CRTC -> encoder -> connector routing; it makes |
| 3986 | * initialisation faster, and also since we have a |
| 3987 | * very dumb picking algorithm, may preserve a better |
| 3988 | * choice. */ |
| 3989 | if (!connector->encoder_id || |
| 3990 | (encoder_id == connector->encoder_id && |
| 3991 | crtc_id == resources->crtcs[i])) |
| 3992 | return i; |
| 3993 | |
| 3994 | ret = i; |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 3995 | } |
| 3996 | } |
| 3997 | |
Daniel Stone | 75487c2 | 2017-01-16 14:33:38 +0000 | [diff] [blame] | 3998 | return ret; |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 3999 | } |
| 4000 | |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 4001 | static void drm_output_fini_cursor_egl(struct drm_output *output) |
| 4002 | { |
| 4003 | unsigned int i; |
| 4004 | |
| 4005 | for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) { |
| 4006 | drm_fb_unref(output->gbm_cursor_fb[i]); |
| 4007 | output->gbm_cursor_fb[i] = NULL; |
| 4008 | } |
| 4009 | } |
| 4010 | |
| 4011 | static int |
| 4012 | drm_output_init_cursor_egl(struct drm_output *output, struct drm_backend *b) |
| 4013 | { |
| 4014 | unsigned int i; |
| 4015 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 4016 | /* No point creating cursors if we don't have a plane for them. */ |
| 4017 | if (!output->cursor_plane) |
| 4018 | return 0; |
| 4019 | |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 4020 | for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) { |
| 4021 | struct gbm_bo *bo; |
| 4022 | |
| 4023 | bo = gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height, |
| 4024 | GBM_FORMAT_ARGB8888, |
| 4025 | GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE); |
| 4026 | if (!bo) |
| 4027 | goto err; |
| 4028 | |
| 4029 | output->gbm_cursor_fb[i] = |
| 4030 | drm_fb_get_from_bo(bo, b, GBM_FORMAT_ARGB8888, |
| 4031 | BUFFER_CURSOR); |
| 4032 | if (!output->gbm_cursor_fb[i]) { |
| 4033 | gbm_bo_destroy(bo); |
| 4034 | goto err; |
| 4035 | } |
| 4036 | } |
| 4037 | |
| 4038 | return 0; |
| 4039 | |
| 4040 | err: |
| 4041 | weston_log("cursor buffers unavailable, using gl cursors\n"); |
| 4042 | b->cursors_are_broken = 1; |
| 4043 | drm_output_fini_cursor_egl(output); |
| 4044 | return -1; |
| 4045 | } |
| 4046 | |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 4047 | /* Init output state that depends on gl or gbm */ |
| 4048 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 4049 | 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] | 4050 | { |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 4051 | EGLint format[2] = { |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 4052 | output->gbm_format, |
| 4053 | fallback_format_for(output->gbm_format), |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 4054 | }; |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 4055 | int n_formats = 1; |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 4056 | |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 4057 | output->gbm_surface = gbm_surface_create(b->gbm, |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 4058 | output->base.current_mode->width, |
| 4059 | output->base.current_mode->height, |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 4060 | format[0], |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 4061 | GBM_BO_USE_SCANOUT | |
| 4062 | GBM_BO_USE_RENDERING); |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 4063 | if (!output->gbm_surface) { |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 4064 | weston_log("failed to create gbm surface\n"); |
| 4065 | return -1; |
| 4066 | } |
| 4067 | |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 4068 | if (format[1]) |
| 4069 | n_formats = 2; |
Miguel A. Vico | c095cde | 2016-05-18 17:43:00 +0200 | [diff] [blame] | 4070 | if (gl_renderer->output_window_create(&output->base, |
| 4071 | (EGLNativeWindowType)output->gbm_surface, |
| 4072 | output->gbm_surface, |
| 4073 | gl_renderer->opaque_attribs, |
| 4074 | format, |
| 4075 | n_formats) < 0) { |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 4076 | weston_log("failed to create gl renderer output state\n"); |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 4077 | gbm_surface_destroy(output->gbm_surface); |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 4078 | return -1; |
| 4079 | } |
| 4080 | |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 4081 | drm_output_init_cursor_egl(output, b); |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 4082 | |
| 4083 | return 0; |
| 4084 | } |
| 4085 | |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 4086 | static void |
| 4087 | drm_output_fini_egl(struct drm_output *output) |
| 4088 | { |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 4089 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
| 4090 | |
| 4091 | /* Destroying the GBM surface will destroy all our GBM buffers, |
| 4092 | * regardless of refcount. Ensure we destroy them here. */ |
| 4093 | if (!b->shutting_down && |
| 4094 | output->scanout_plane->state_cur->fb && |
| 4095 | output->scanout_plane->state_cur->fb->type == BUFFER_GBM_SURFACE) { |
| 4096 | drm_plane_state_free(output->scanout_plane->state_cur, true); |
| 4097 | output->scanout_plane->state_cur = |
| 4098 | drm_plane_state_alloc(NULL, output->scanout_plane); |
| 4099 | output->scanout_plane->state_cur->complete = true; |
| 4100 | } |
| 4101 | |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 4102 | gl_renderer->output_destroy(&output->base); |
| 4103 | gbm_surface_destroy(output->gbm_surface); |
Daniel Stone | e425683 | 2017-04-04 17:54:27 +0100 | [diff] [blame] | 4104 | drm_output_fini_cursor_egl(output); |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 4105 | } |
| 4106 | |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 4107 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 4108 | 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] | 4109 | { |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 4110 | int w = output->base.current_mode->width; |
| 4111 | int h = output->base.current_mode->height; |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 4112 | uint32_t format = output->gbm_format; |
| 4113 | uint32_t pixman_format; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4114 | unsigned int i; |
| 4115 | |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 4116 | switch (format) { |
| 4117 | case GBM_FORMAT_XRGB8888: |
| 4118 | pixman_format = PIXMAN_x8r8g8b8; |
| 4119 | break; |
| 4120 | case GBM_FORMAT_RGB565: |
| 4121 | pixman_format = PIXMAN_r5g6b5; |
| 4122 | break; |
| 4123 | default: |
| 4124 | weston_log("Unsupported pixman format 0x%x\n", format); |
| 4125 | return -1; |
| 4126 | } |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4127 | |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 4128 | /* FIXME error checking */ |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4129 | for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) { |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 4130 | 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] | 4131 | if (!output->dumb[i]) |
| 4132 | goto err; |
| 4133 | |
| 4134 | output->image[i] = |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 4135 | pixman_image_create_bits(pixman_format, w, h, |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4136 | output->dumb[i]->map, |
| 4137 | output->dumb[i]->stride); |
| 4138 | if (!output->image[i]) |
| 4139 | goto err; |
| 4140 | } |
| 4141 | |
| 4142 | if (pixman_renderer_output_create(&output->base) < 0) |
| 4143 | goto err; |
| 4144 | |
| 4145 | pixman_region32_init_rect(&output->previous_damage, |
Alexander Larsson | 0b13506 | 2013-05-28 16:23:36 +0200 | [diff] [blame] | 4146 | 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] | 4147 | |
| 4148 | return 0; |
| 4149 | |
| 4150 | err: |
| 4151 | for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) { |
| 4152 | if (output->dumb[i]) |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 4153 | drm_fb_unref(output->dumb[i]); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4154 | if (output->image[i]) |
| 4155 | pixman_image_unref(output->image[i]); |
| 4156 | |
| 4157 | output->dumb[i] = NULL; |
| 4158 | output->image[i] = NULL; |
| 4159 | } |
| 4160 | |
| 4161 | return -1; |
| 4162 | } |
| 4163 | |
| 4164 | static void |
| 4165 | drm_output_fini_pixman(struct drm_output *output) |
| 4166 | { |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 4167 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4168 | unsigned int i; |
| 4169 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 4170 | /* Destroying the Pixman surface will destroy all our buffers, |
| 4171 | * regardless of refcount. Ensure we destroy them here. */ |
| 4172 | if (!b->shutting_down && |
| 4173 | output->scanout_plane->state_cur->fb && |
| 4174 | output->scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB) { |
| 4175 | drm_plane_state_free(output->scanout_plane->state_cur, true); |
| 4176 | output->scanout_plane->state_cur = |
| 4177 | drm_plane_state_alloc(NULL, output->scanout_plane); |
| 4178 | output->scanout_plane->state_cur->complete = true; |
| 4179 | } |
| 4180 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4181 | pixman_renderer_output_destroy(&output->base); |
| 4182 | pixman_region32_fini(&output->previous_damage); |
| 4183 | |
| 4184 | for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4185 | pixman_image_unref(output->image[i]); |
Daniel Stone | 6e7a961 | 2017-04-04 17:54:26 +0100 | [diff] [blame] | 4186 | drm_fb_unref(output->dumb[i]); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4187 | output->dumb[i] = NULL; |
| 4188 | output->image[i] = NULL; |
| 4189 | } |
| 4190 | } |
| 4191 | |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4192 | static void |
| 4193 | edid_parse_string(const uint8_t *data, char text[]) |
| 4194 | { |
| 4195 | int i; |
| 4196 | int replaced = 0; |
| 4197 | |
| 4198 | /* this is always 12 bytes, but we can't guarantee it's null |
| 4199 | * terminated or not junk. */ |
| 4200 | strncpy(text, (const char *) data, 12); |
| 4201 | |
Bryce Harrington | 9c7de16 | 2015-08-28 13:04:26 -0700 | [diff] [blame] | 4202 | /* guarantee our new string is null-terminated */ |
| 4203 | text[12] = '\0'; |
| 4204 | |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4205 | /* remove insane chars */ |
| 4206 | for (i = 0; text[i] != '\0'; i++) { |
| 4207 | if (text[i] == '\n' || |
| 4208 | text[i] == '\r') { |
| 4209 | text[i] = '\0'; |
| 4210 | break; |
| 4211 | } |
| 4212 | } |
| 4213 | |
| 4214 | /* ensure string is printable */ |
| 4215 | for (i = 0; text[i] != '\0'; i++) { |
| 4216 | if (!isprint(text[i])) { |
| 4217 | text[i] = '-'; |
| 4218 | replaced++; |
| 4219 | } |
| 4220 | } |
| 4221 | |
| 4222 | /* if the string is random junk, ignore the string */ |
| 4223 | if (replaced > 4) |
| 4224 | text[0] = '\0'; |
| 4225 | } |
| 4226 | |
| 4227 | #define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe |
| 4228 | #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc |
| 4229 | #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff |
| 4230 | #define EDID_OFFSET_DATA_BLOCKS 0x36 |
| 4231 | #define EDID_OFFSET_LAST_BLOCK 0x6c |
| 4232 | #define EDID_OFFSET_PNPID 0x08 |
| 4233 | #define EDID_OFFSET_SERIAL 0x0c |
| 4234 | |
| 4235 | static int |
| 4236 | edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length) |
| 4237 | { |
| 4238 | int i; |
| 4239 | uint32_t serial_number; |
| 4240 | |
| 4241 | /* check header */ |
| 4242 | if (length < 128) |
| 4243 | return -1; |
| 4244 | if (data[0] != 0x00 || data[1] != 0xff) |
| 4245 | return -1; |
| 4246 | |
| 4247 | /* decode the PNP ID from three 5 bit words packed into 2 bytes |
| 4248 | * /--08--\/--09--\ |
| 4249 | * 7654321076543210 |
| 4250 | * |\---/\---/\---/ |
| 4251 | * R C1 C2 C3 */ |
| 4252 | edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1; |
| 4253 | edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1; |
| 4254 | edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1; |
| 4255 | edid->pnp_id[3] = '\0'; |
| 4256 | |
| 4257 | /* maybe there isn't a ASCII serial number descriptor, so use this instead */ |
| 4258 | serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0]; |
| 4259 | serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100; |
| 4260 | serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000; |
| 4261 | serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000; |
| 4262 | if (serial_number > 0) |
| 4263 | sprintf(edid->serial_number, "%lu", (unsigned long) serial_number); |
| 4264 | |
| 4265 | /* parse EDID data */ |
| 4266 | for (i = EDID_OFFSET_DATA_BLOCKS; |
| 4267 | i <= EDID_OFFSET_LAST_BLOCK; |
| 4268 | i += 18) { |
| 4269 | /* ignore pixel clock data */ |
| 4270 | if (data[i] != 0) |
| 4271 | continue; |
| 4272 | if (data[i+2] != 0) |
| 4273 | continue; |
| 4274 | |
| 4275 | /* any useful blocks? */ |
| 4276 | if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) { |
| 4277 | edid_parse_string(&data[i+5], |
| 4278 | edid->monitor_name); |
| 4279 | } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) { |
| 4280 | edid_parse_string(&data[i+5], |
| 4281 | edid->serial_number); |
| 4282 | } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) { |
| 4283 | edid_parse_string(&data[i+5], |
| 4284 | edid->eisa_id); |
| 4285 | } |
| 4286 | } |
| 4287 | return 0; |
| 4288 | } |
| 4289 | |
Pekka Paalanen | 6f1866b | 2017-04-03 14:22:51 +0300 | [diff] [blame] | 4290 | /** Parse monitor make, model and serial from EDID |
| 4291 | * |
| 4292 | * \param b The backend instance. |
| 4293 | * \param output The output whose \c drm_edid to fill in. |
| 4294 | * \param props The DRM connector properties to get the EDID from. |
| 4295 | * \param make[out] The monitor make (PNP ID). |
| 4296 | * \param model[out] The monitor model (name). |
| 4297 | * \param serial_number[out] The monitor serial number. |
| 4298 | * |
| 4299 | * Each of \c *make, \c *model and \c *serial_number are set only if the |
| 4300 | * information is found in the EDID. The pointers they are set to must not |
| 4301 | * be free()'d explicitly, instead they get implicitly freed when the |
| 4302 | * \c drm_output is destroyed. |
| 4303 | */ |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4304 | static void |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 4305 | find_and_parse_output_edid(struct drm_backend *b, struct drm_output *output, |
Pekka Paalanen | 6f1866b | 2017-04-03 14:22:51 +0300 | [diff] [blame] | 4306 | drmModeObjectPropertiesPtr props, |
| 4307 | const char **make, |
| 4308 | const char **model, |
| 4309 | const char **serial_number) |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4310 | { |
| 4311 | drmModePropertyBlobPtr edid_blob = NULL; |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 4312 | uint32_t blob_id; |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4313 | int rc; |
| 4314 | |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 4315 | blob_id = |
| 4316 | drm_property_get_value(&output->props_conn[WDRM_CONNECTOR_EDID], |
| 4317 | props, 0); |
| 4318 | if (!blob_id) |
| 4319 | return; |
| 4320 | |
| 4321 | edid_blob = drmModeGetPropertyBlob(b->drm.fd, blob_id); |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4322 | if (!edid_blob) |
| 4323 | return; |
| 4324 | |
| 4325 | rc = edid_parse(&output->edid, |
| 4326 | edid_blob->data, |
| 4327 | edid_blob->length); |
| 4328 | if (!rc) { |
| 4329 | weston_log("EDID data '%s', '%s', '%s'\n", |
| 4330 | output->edid.pnp_id, |
| 4331 | output->edid.monitor_name, |
| 4332 | output->edid.serial_number); |
| 4333 | if (output->edid.pnp_id[0] != '\0') |
Pekka Paalanen | 6f1866b | 2017-04-03 14:22:51 +0300 | [diff] [blame] | 4334 | *make = output->edid.pnp_id; |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4335 | if (output->edid.monitor_name[0] != '\0') |
Pekka Paalanen | 6f1866b | 2017-04-03 14:22:51 +0300 | [diff] [blame] | 4336 | *model = output->edid.monitor_name; |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4337 | if (output->edid.serial_number[0] != '\0') |
Pekka Paalanen | 6f1866b | 2017-04-03 14:22:51 +0300 | [diff] [blame] | 4338 | *serial_number = output->edid.serial_number; |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 4339 | } |
| 4340 | drmModeFreePropertyBlob(edid_blob); |
| 4341 | } |
| 4342 | |
Kristian Høgsberg | a30989a | 2013-05-23 17:23:15 -0400 | [diff] [blame] | 4343 | static int |
| 4344 | parse_modeline(const char *s, drmModeModeInfo *mode) |
| 4345 | { |
| 4346 | char hsync[16]; |
| 4347 | char vsync[16]; |
| 4348 | float fclock; |
| 4349 | |
Pekka Paalanen | dc4e3c6 | 2017-12-05 15:37:41 +0200 | [diff] [blame] | 4350 | memset(mode, 0, sizeof *mode); |
| 4351 | |
Kristian Høgsberg | a30989a | 2013-05-23 17:23:15 -0400 | [diff] [blame] | 4352 | mode->type = DRM_MODE_TYPE_USERDEF; |
| 4353 | mode->hskew = 0; |
| 4354 | mode->vscan = 0; |
| 4355 | mode->vrefresh = 0; |
| 4356 | mode->flags = 0; |
| 4357 | |
Rob Bradford | 307e09e | 2013-07-26 16:29:40 +0100 | [diff] [blame] | 4358 | 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] | 4359 | &fclock, |
| 4360 | &mode->hdisplay, |
| 4361 | &mode->hsync_start, |
| 4362 | &mode->hsync_end, |
| 4363 | &mode->htotal, |
| 4364 | &mode->vdisplay, |
| 4365 | &mode->vsync_start, |
| 4366 | &mode->vsync_end, |
| 4367 | &mode->vtotal, hsync, vsync) != 11) |
| 4368 | return -1; |
| 4369 | |
| 4370 | mode->clock = fclock * 1000; |
| 4371 | if (strcmp(hsync, "+hsync") == 0) |
| 4372 | mode->flags |= DRM_MODE_FLAG_PHSYNC; |
| 4373 | else if (strcmp(hsync, "-hsync") == 0) |
| 4374 | mode->flags |= DRM_MODE_FLAG_NHSYNC; |
| 4375 | else |
| 4376 | return -1; |
| 4377 | |
| 4378 | if (strcmp(vsync, "+vsync") == 0) |
| 4379 | mode->flags |= DRM_MODE_FLAG_PVSYNC; |
| 4380 | else if (strcmp(vsync, "-vsync") == 0) |
| 4381 | mode->flags |= DRM_MODE_FLAG_NVSYNC; |
| 4382 | else |
| 4383 | return -1; |
| 4384 | |
Emmanuel Gil Peyrot | a62138b | 2016-05-02 22:40:11 +0100 | [diff] [blame] | 4385 | snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f", |
| 4386 | mode->hdisplay, mode->vdisplay, fclock); |
| 4387 | |
Kristian Høgsberg | a30989a | 2013-05-23 17:23:15 -0400 | [diff] [blame] | 4388 | return 0; |
| 4389 | } |
| 4390 | |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 4391 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 4392 | setup_output_seat_constraint(struct drm_backend *b, |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 4393 | struct weston_output *output, |
| 4394 | const char *s) |
| 4395 | { |
| 4396 | if (strcmp(s, "") != 0) { |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 4397 | struct weston_pointer *pointer; |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 4398 | struct udev_seat *seat; |
| 4399 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 4400 | seat = udev_seat_get_named(&b->input, s); |
Derek Foreman | 0720ea3 | 2015-07-15 13:00:35 -0500 | [diff] [blame] | 4401 | if (!seat) |
| 4402 | return; |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 4403 | |
Derek Foreman | 0720ea3 | 2015-07-15 13:00:35 -0500 | [diff] [blame] | 4404 | seat->base.output = output; |
| 4405 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 4406 | pointer = weston_seat_get_pointer(&seat->base); |
| 4407 | if (pointer) |
| 4408 | weston_pointer_clamp(pointer, |
| 4409 | &pointer->x, |
| 4410 | &pointer->y); |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 4411 | } |
| 4412 | } |
| 4413 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4414 | static int |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4415 | 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] | 4416 | { |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 4417 | int ret = 0; |
| 4418 | |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 4419 | if (s == NULL) |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4420 | *gbm_format = default_value; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 4421 | else if (strcmp(s, "xrgb8888") == 0) |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4422 | *gbm_format = GBM_FORMAT_XRGB8888; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 4423 | else if (strcmp(s, "rgb565") == 0) |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4424 | *gbm_format = GBM_FORMAT_RGB565; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 4425 | else if (strcmp(s, "xrgb2101010") == 0) |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4426 | *gbm_format = GBM_FORMAT_XRGB2101010; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 4427 | else { |
| 4428 | weston_log("fatal: unrecognized pixel format: %s\n", s); |
| 4429 | ret = -1; |
| 4430 | } |
| 4431 | |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 4432 | return ret; |
| 4433 | } |
| 4434 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4435 | /** |
| 4436 | * Choose suitable mode for an output |
| 4437 | * |
| 4438 | * Find the most suitable mode to use for initial setup (or reconfiguration on |
| 4439 | * hotplug etc) for a DRM output. |
| 4440 | * |
| 4441 | * @param output DRM output to choose mode for |
| 4442 | * @param kind Strategy and preference to use when choosing mode |
| 4443 | * @param width Desired width for this output |
| 4444 | * @param height Desired height for this output |
| 4445 | * @param current_mode Mode currently being displayed on this output |
| 4446 | * @param modeline Manually-entered mode (may be NULL) |
| 4447 | * @returns A mode from the output's mode list, or NULL if none available |
| 4448 | */ |
| 4449 | static struct drm_mode * |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4450 | drm_output_choose_initial_mode(struct drm_backend *backend, |
| 4451 | struct drm_output *output, |
| 4452 | enum weston_drm_backend_output_mode mode, |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4453 | const char *modeline, |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4454 | const drmModeModeInfo *current_mode) |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4455 | { |
| 4456 | struct drm_mode *preferred = NULL; |
| 4457 | struct drm_mode *current = NULL; |
| 4458 | struct drm_mode *configured = NULL; |
| 4459 | struct drm_mode *best = NULL; |
| 4460 | struct drm_mode *drm_mode; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4461 | drmModeModeInfo drm_modeline; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4462 | int32_t width = 0; |
| 4463 | int32_t height = 0; |
Fabien Dessenne | 2d66a7d | 2017-01-17 17:17:21 +0100 | [diff] [blame] | 4464 | uint32_t refresh = 0; |
| 4465 | int n; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4466 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4467 | if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) { |
Fabien Dessenne | 2d66a7d | 2017-01-17 17:17:21 +0100 | [diff] [blame] | 4468 | n = sscanf(modeline, "%dx%d@%d", &width, &height, &refresh); |
| 4469 | if (n != 2 && n != 3) { |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4470 | width = -1; |
| 4471 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4472 | if (parse_modeline(modeline, &drm_modeline) == 0) { |
| 4473 | configured = drm_output_add_mode(output, &drm_modeline); |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4474 | if (!configured) |
| 4475 | return NULL; |
| 4476 | } else { |
| 4477 | weston_log("Invalid modeline \"%s\" for output %s\n", |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4478 | modeline, output->base.name); |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4479 | } |
| 4480 | } |
| 4481 | } |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4482 | |
| 4483 | 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] | 4484 | if (width == drm_mode->base.width && |
Fabien Dessenne | 2d66a7d | 2017-01-17 17:17:21 +0100 | [diff] [blame] | 4485 | height == drm_mode->base.height && |
| 4486 | (refresh == 0 || refresh == drm_mode->mode_info.vrefresh)) |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4487 | configured = drm_mode; |
| 4488 | |
comic fans | 7a5c562 | 2016-03-17 14:29:27 +0200 | [diff] [blame] | 4489 | if (memcmp(current_mode, &drm_mode->mode_info, |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4490 | sizeof *current_mode) == 0) |
| 4491 | current = drm_mode; |
| 4492 | |
| 4493 | if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED) |
| 4494 | preferred = drm_mode; |
| 4495 | |
| 4496 | best = drm_mode; |
| 4497 | } |
| 4498 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4499 | if (current == NULL && current_mode->clock != 0) { |
| 4500 | current = drm_output_add_mode(output, current_mode); |
| 4501 | if (!current) |
| 4502 | return NULL; |
| 4503 | } |
| 4504 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4505 | if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT) |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4506 | configured = current; |
| 4507 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4508 | if (configured) |
| 4509 | return configured; |
| 4510 | |
| 4511 | if (preferred) |
| 4512 | return preferred; |
| 4513 | |
| 4514 | if (current) |
| 4515 | return current; |
| 4516 | |
| 4517 | if (best) |
| 4518 | return best; |
| 4519 | |
| 4520 | weston_log("no available modes for %s\n", output->base.name); |
| 4521 | return NULL; |
| 4522 | } |
| 4523 | |
Pekka Paalanen | eee580b | 2014-06-04 16:43:06 +0300 | [diff] [blame] | 4524 | static int |
| 4525 | connector_get_current_mode(drmModeConnector *connector, int drm_fd, |
| 4526 | drmModeModeInfo *mode) |
| 4527 | { |
| 4528 | drmModeEncoder *encoder; |
| 4529 | drmModeCrtc *crtc; |
| 4530 | |
| 4531 | /* Get the current mode on the crtc that's currently driving |
| 4532 | * this connector. */ |
| 4533 | encoder = drmModeGetEncoder(drm_fd, connector->encoder_id); |
| 4534 | memset(mode, 0, sizeof *mode); |
| 4535 | if (encoder != NULL) { |
| 4536 | crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id); |
| 4537 | drmModeFreeEncoder(encoder); |
| 4538 | if (crtc == NULL) |
| 4539 | return -1; |
| 4540 | if (crtc->mode_valid) |
| 4541 | *mode = crtc->mode; |
| 4542 | drmModeFreeCrtc(crtc); |
| 4543 | } |
| 4544 | |
| 4545 | return 0; |
| 4546 | } |
| 4547 | |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 4548 | static int |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4549 | drm_output_set_mode(struct weston_output *base, |
| 4550 | enum weston_drm_backend_output_mode mode, |
| 4551 | const char *modeline) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 4552 | { |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4553 | struct drm_output *output = to_drm_output(base); |
| 4554 | struct drm_backend *b = to_drm_backend(base->compositor); |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4555 | |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 4556 | struct drm_mode *current; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 4557 | drmModeModeInfo crtc_mode; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 4558 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4559 | if (connector_get_current_mode(output->connector, b->drm.fd, &crtc_mode) < 0) |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 4560 | return -1; |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 4561 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4562 | current = drm_output_choose_initial_mode(b, output, mode, modeline, &crtc_mode); |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4563 | if (!current) |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 4564 | return -1; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4565 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 4566 | output->base.current_mode = ¤t->base; |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 4567 | output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT; |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame] | 4568 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4569 | /* Set native_ fields, so weston_output_mode_switch_to_native() works */ |
| 4570 | output->base.native_mode = output->base.current_mode; |
| 4571 | output->base.native_scale = output->base.current_scale; |
| 4572 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4573 | return 0; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4574 | } |
| 4575 | |
| 4576 | static void |
| 4577 | drm_output_set_gbm_format(struct weston_output *base, |
| 4578 | const char *gbm_format) |
| 4579 | { |
| 4580 | struct drm_output *output = to_drm_output(base); |
| 4581 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 4582 | |
| 4583 | if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1) |
| 4584 | output->gbm_format = b->gbm_format; |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 4585 | |
| 4586 | /* Without universal planes, we can't discover which formats are |
| 4587 | * supported by the primary plane; we just hope that the GBM format |
| 4588 | * works. */ |
| 4589 | if (!b->universal_planes) |
| 4590 | output->scanout_plane->formats[0] = output->gbm_format; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4591 | } |
| 4592 | |
| 4593 | static void |
| 4594 | drm_output_set_seat(struct weston_output *base, |
| 4595 | const char *seat) |
| 4596 | { |
| 4597 | struct drm_output *output = to_drm_output(base); |
| 4598 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 4599 | |
| 4600 | setup_output_seat_constraint(b, &output->base, |
| 4601 | seat ? seat : ""); |
| 4602 | } |
| 4603 | |
| 4604 | static int |
Pekka Paalanen | c4db6f7 | 2017-09-05 16:37:03 +0300 | [diff] [blame] | 4605 | drm_output_init_gamma_size(struct drm_output *output) |
| 4606 | { |
| 4607 | struct drm_backend *backend = to_drm_backend(output->base.compositor); |
| 4608 | drmModeCrtc *crtc; |
| 4609 | |
| 4610 | assert(output->base.compositor); |
| 4611 | assert(output->crtc_id != 0); |
| 4612 | crtc = drmModeGetCrtc(backend->drm.fd, output->crtc_id); |
| 4613 | if (!crtc) |
| 4614 | return -1; |
| 4615 | |
| 4616 | output->base.gamma_size = crtc->gamma_size; |
| 4617 | |
| 4618 | drmModeFreeCrtc(crtc); |
| 4619 | |
| 4620 | return 0; |
| 4621 | } |
| 4622 | |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 4623 | /** Allocate a CRTC for the output |
| 4624 | * |
| 4625 | * @param output The output with no allocated CRTC. |
| 4626 | * @param resources DRM KMS resources. |
| 4627 | * @param connector The DRM KMS connector data. |
| 4628 | * @return 0 on success, -1 on failure. |
| 4629 | * |
| 4630 | * Finds a free CRTC that can drive the given connector, reserves the CRTC |
| 4631 | * for the output, and loads the CRTC properties. |
| 4632 | * |
| 4633 | * Populates the cursor and scanout planes. |
| 4634 | * |
| 4635 | * On failure, the output remains without a CRTC. |
| 4636 | */ |
| 4637 | static int |
| 4638 | drm_output_init_crtc(struct drm_output *output, |
| 4639 | drmModeRes *resources, drmModeConnector *connector) |
| 4640 | { |
| 4641 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
| 4642 | drmModeObjectPropertiesPtr props; |
| 4643 | int i; |
| 4644 | |
| 4645 | assert(output->crtc_id == 0); |
| 4646 | |
| 4647 | i = find_crtc_for_connector(b, resources, connector); |
| 4648 | if (i < 0) { |
| 4649 | weston_log("No usable crtc/encoder pair for connector.\n"); |
| 4650 | return -1; |
| 4651 | } |
| 4652 | |
| 4653 | output->crtc_id = resources->crtcs[i]; |
| 4654 | output->pipe = i; |
| 4655 | |
| 4656 | props = drmModeObjectGetProperties(b->drm.fd, output->crtc_id, |
| 4657 | DRM_MODE_OBJECT_CRTC); |
| 4658 | if (!props) { |
| 4659 | weston_log("failed to get CRTC properties\n"); |
| 4660 | goto err_crtc; |
| 4661 | } |
| 4662 | drm_property_info_populate(b, crtc_props, output->props_crtc, |
| 4663 | WDRM_CRTC__COUNT, props); |
| 4664 | drmModeFreeObjectProperties(props); |
| 4665 | |
| 4666 | output->scanout_plane = |
| 4667 | drm_output_find_special_plane(b, output, |
| 4668 | WDRM_PLANE_TYPE_PRIMARY); |
| 4669 | if (!output->scanout_plane) { |
| 4670 | weston_log("Failed to find primary plane for output %s\n", |
| 4671 | output->base.name); |
| 4672 | goto err_crtc; |
| 4673 | } |
| 4674 | |
| 4675 | /* Failing to find a cursor plane is not fatal, as we'll fall back |
| 4676 | * to software cursor. */ |
| 4677 | output->cursor_plane = |
| 4678 | drm_output_find_special_plane(b, output, |
| 4679 | WDRM_PLANE_TYPE_CURSOR); |
| 4680 | |
| 4681 | return 0; |
| 4682 | |
| 4683 | err_crtc: |
| 4684 | output->crtc_id = 0; |
| 4685 | output->pipe = 0; |
| 4686 | |
| 4687 | return -1; |
| 4688 | } |
| 4689 | |
| 4690 | /** Free the CRTC from the output |
| 4691 | * |
| 4692 | * @param output The output whose CRTC to deallocate. |
| 4693 | * |
| 4694 | * The CRTC reserved for the given output becomes free to use again. |
| 4695 | */ |
| 4696 | static void |
| 4697 | drm_output_fini_crtc(struct drm_output *output) |
| 4698 | { |
| 4699 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
| 4700 | |
| 4701 | if (!b->universal_planes && !b->shutting_down) { |
| 4702 | /* With universal planes, the 'special' planes are allocated at |
| 4703 | * startup, freed at shutdown, and live on the plane list in |
| 4704 | * between. We want the planes to continue to exist and be freed |
| 4705 | * up for other outputs. |
| 4706 | * |
| 4707 | * Without universal planes, our special planes are |
| 4708 | * pseudo-planes allocated at output creation, freed at output |
| 4709 | * destruction, and not usable by other outputs. |
| 4710 | * |
| 4711 | * On the other hand, if the compositor is already shutting down, |
| 4712 | * the plane has already been destroyed. |
| 4713 | */ |
| 4714 | if (output->cursor_plane) |
| 4715 | drm_plane_destroy(output->cursor_plane); |
| 4716 | if (output->scanout_plane) |
| 4717 | drm_plane_destroy(output->scanout_plane); |
| 4718 | } |
| 4719 | |
| 4720 | drm_property_info_free(output->props_crtc, WDRM_CRTC__COUNT); |
| 4721 | output->crtc_id = 0; |
| 4722 | output->cursor_plane = NULL; |
| 4723 | output->scanout_plane = NULL; |
| 4724 | } |
| 4725 | |
Pekka Paalanen | c4db6f7 | 2017-09-05 16:37:03 +0300 | [diff] [blame] | 4726 | static int |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4727 | drm_output_enable(struct weston_output *base) |
| 4728 | { |
| 4729 | struct drm_output *output = to_drm_output(base); |
| 4730 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 4731 | struct weston_mode *m; |
| 4732 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 4733 | if (b->pageflip_timeout) |
| 4734 | drm_output_pageflip_timer_create(output); |
| 4735 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 4736 | if (b->use_pixman) { |
| 4737 | if (drm_output_init_pixman(output, b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4738 | weston_log("Failed to init output pixman state\n"); |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 4739 | goto err; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 4740 | } |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 4741 | } else if (drm_output_init_egl(output, b) < 0) { |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 4742 | weston_log("Failed to init output gl state\n"); |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 4743 | goto err; |
Kristian Høgsberg | 1d1e0a5 | 2012-10-21 13:29:26 -0400 | [diff] [blame] | 4744 | } |
Kristian Høgsberg | 8e1f77f | 2012-05-03 11:39:35 -0400 | [diff] [blame] | 4745 | |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 4746 | if (output->backlight) { |
Kristian Høgsberg | 220819f | 2013-05-23 20:29:40 -0400 | [diff] [blame] | 4747 | weston_log("Initialized backlight, device %s\n", |
| 4748 | output->backlight->path); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 4749 | output->base.set_backlight = drm_set_backlight; |
| 4750 | output->base.backlight_current = drm_get_backlight(output); |
Kristian Høgsberg | 220819f | 2013-05-23 20:29:40 -0400 | [diff] [blame] | 4751 | } else { |
| 4752 | weston_log("Failed to initialize backlight\n"); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 4753 | } |
| 4754 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 4755 | output->base.start_repaint_loop = drm_output_start_repaint_loop; |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 4756 | output->base.repaint = drm_output_repaint; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 4757 | output->base.assign_planes = drm_assign_planes; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 4758 | output->base.set_dpms = drm_set_dpms; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 4759 | output->base.switch_mode = drm_output_switch_mode; |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 4760 | output->base.set_gamma = drm_output_set_gamma; |
| 4761 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 4762 | if (output->cursor_plane) |
| 4763 | weston_compositor_stack_plane(b->compositor, |
| 4764 | &output->cursor_plane->base, |
| 4765 | NULL); |
| 4766 | else |
| 4767 | b->cursors_are_broken = 1; |
| 4768 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 4769 | weston_compositor_stack_plane(b->compositor, |
| 4770 | &output->scanout_plane->base, |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 4771 | &b->compositor->primary_plane); |
Ander Conselvan de Oliveira | 8ad1982 | 2013-03-05 17:30:27 +0200 | [diff] [blame] | 4772 | |
Pekka Paalanen | 9068e01 | 2018-02-09 13:31:50 +0200 | [diff] [blame] | 4773 | wl_array_remove_uint32(&b->unused_connectors, output->connector_id); |
| 4774 | wl_array_remove_uint32(&b->unused_crtcs, output->crtc_id); |
| 4775 | |
Kristian Høgsberg | 2f9ed71 | 2012-07-26 17:57:15 -0400 | [diff] [blame] | 4776 | weston_log("Output %s, (connector %d, crtc %d)\n", |
Richard Hughes | afe690c | 2013-05-02 10:10:04 +0100 | [diff] [blame] | 4777 | output->base.name, output->connector_id, output->crtc_id); |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 4778 | wl_list_for_each(m, &output->base.mode_list, link) |
U. Artie Eoff | d3ed6cb | 2014-01-10 10:15:17 -0800 | [diff] [blame] | 4779 | weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n", |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 4780 | m->width, m->height, m->refresh / 1000.0, |
| 4781 | m->flags & WL_OUTPUT_MODE_PREFERRED ? |
| 4782 | ", preferred" : "", |
| 4783 | m->flags & WL_OUTPUT_MODE_CURRENT ? |
| 4784 | ", current" : "", |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4785 | output->connector->count_modes == 0 ? |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 4786 | ", built-in" : ""); |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 4787 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 4788 | return 0; |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 4789 | |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 4790 | err: |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 4791 | return -1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 4792 | } |
| 4793 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 4794 | static void |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4795 | drm_output_deinit(struct weston_output *base) |
| 4796 | { |
| 4797 | struct drm_output *output = to_drm_output(base); |
| 4798 | struct drm_backend *b = to_drm_backend(base->compositor); |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 4799 | uint32_t *unused; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4800 | |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 4801 | if (b->use_pixman) |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4802 | drm_output_fini_pixman(output); |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 4803 | else |
| 4804 | drm_output_fini_egl(output); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4805 | |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 4806 | /* Since our planes are no longer in use anywhere, remove their base |
| 4807 | * weston_plane's link from the plane stacking list, unless we're |
| 4808 | * shutting down, in which case the plane has already been |
| 4809 | * destroyed. */ |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 4810 | if (!b->shutting_down) { |
| 4811 | wl_list_remove(&output->scanout_plane->base.link); |
| 4812 | wl_list_init(&output->scanout_plane->base.link); |
| 4813 | |
| 4814 | if (output->cursor_plane) { |
| 4815 | wl_list_remove(&output->cursor_plane->base.link); |
| 4816 | wl_list_init(&output->cursor_plane->base.link); |
| 4817 | /* Turn off hardware cursor */ |
| 4818 | drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0); |
| 4819 | } |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 4820 | } |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 4821 | |
| 4822 | unused = wl_array_add(&b->unused_connectors, sizeof(*unused)); |
| 4823 | *unused = output->connector_id; |
| 4824 | unused = wl_array_add(&b->unused_crtcs, sizeof(*unused)); |
| 4825 | *unused = output->crtc_id; |
Pekka Paalanen | 5e12b55 | 2018-02-09 12:29:10 +0200 | [diff] [blame] | 4826 | |
| 4827 | /* Force programming unused connectors and crtcs. */ |
| 4828 | b->state_invalid = true; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4829 | } |
| 4830 | |
| 4831 | static void |
| 4832 | drm_output_destroy(struct weston_output *base) |
| 4833 | { |
| 4834 | struct drm_output *output = to_drm_output(base); |
| 4835 | struct drm_backend *b = to_drm_backend(base->compositor); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4836 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 4837 | if (output->page_flip_pending || output->vblank_pending || |
| 4838 | output->atomic_complete_pending) { |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4839 | output->destroy_pending = 1; |
| 4840 | weston_log("destroy output while page flip pending\n"); |
| 4841 | return; |
| 4842 | } |
| 4843 | |
| 4844 | if (output->base.enabled) |
| 4845 | drm_output_deinit(&output->base); |
| 4846 | |
Pekka Paalanen | 383b3af | 2017-09-11 14:40:48 +0300 | [diff] [blame] | 4847 | drm_mode_list_destroy(b, &output->base.mode_list); |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 4848 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 4849 | if (output->pageflip_timer) |
| 4850 | wl_event_source_remove(output->pageflip_timer); |
| 4851 | |
Pekka Paalanen | ae6d35d | 2017-08-16 12:07:14 +0300 | [diff] [blame] | 4852 | weston_output_release(&output->base); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4853 | |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 4854 | drm_output_fini_crtc(output); |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 4855 | |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 4856 | drm_property_info_free(output->props_conn, WDRM_CONNECTOR__COUNT); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4857 | drmModeFreeConnector(output->connector); |
| 4858 | |
| 4859 | if (output->backlight) |
| 4860 | backlight_destroy(output->backlight); |
| 4861 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 4862 | assert(!output->state_last); |
| 4863 | drm_output_state_free(output->state_cur); |
| 4864 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4865 | free(output); |
| 4866 | } |
| 4867 | |
| 4868 | static int |
| 4869 | drm_output_disable(struct weston_output *base) |
| 4870 | { |
| 4871 | struct drm_output *output = to_drm_output(base); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4872 | |
Daniel Stone | 598ee9d | 2016-11-16 11:55:20 +0000 | [diff] [blame] | 4873 | if (output->page_flip_pending || output->vblank_pending || |
| 4874 | output->atomic_complete_pending) { |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4875 | output->disable_pending = 1; |
| 4876 | return -1; |
| 4877 | } |
| 4878 | |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 4879 | weston_log("Disabling output %s\n", output->base.name); |
Daniel Stone | a08512f | 2016-11-08 17:46:10 +0000 | [diff] [blame] | 4880 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4881 | if (output->base.enabled) |
| 4882 | drm_output_deinit(&output->base); |
| 4883 | |
| 4884 | output->disable_pending = 0; |
| 4885 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4886 | return 0; |
| 4887 | } |
| 4888 | |
| 4889 | /** |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 4890 | * Update the list of unused connectors and CRTCs |
| 4891 | * |
| 4892 | * This keeps the unused_connectors and unused_crtcs arrays up to date. |
| 4893 | * |
| 4894 | * @param b Weston backend structure |
| 4895 | * @param resources DRM resources for this device |
| 4896 | */ |
| 4897 | static void |
| 4898 | drm_backend_update_unused_outputs(struct drm_backend *b, drmModeRes *resources) |
| 4899 | { |
| 4900 | int i; |
| 4901 | |
| 4902 | wl_array_release(&b->unused_connectors); |
| 4903 | wl_array_init(&b->unused_connectors); |
| 4904 | |
| 4905 | for (i = 0; i < resources->count_connectors; i++) { |
| 4906 | struct drm_output *output; |
| 4907 | uint32_t *connector_id; |
| 4908 | |
| 4909 | output = drm_output_find_by_connector(b, resources->connectors[i]); |
| 4910 | if (output && output->base.enabled) |
| 4911 | continue; |
| 4912 | |
| 4913 | connector_id = wl_array_add(&b->unused_connectors, |
| 4914 | sizeof(*connector_id)); |
| 4915 | *connector_id = resources->connectors[i]; |
| 4916 | } |
| 4917 | |
| 4918 | wl_array_release(&b->unused_crtcs); |
| 4919 | wl_array_init(&b->unused_crtcs); |
| 4920 | |
| 4921 | for (i = 0; i < resources->count_crtcs; i++) { |
| 4922 | struct drm_output *output; |
| 4923 | uint32_t *crtc_id; |
| 4924 | |
| 4925 | output = drm_output_find_by_crtc(b, resources->crtcs[i]); |
| 4926 | if (output && output->base.enabled) |
| 4927 | continue; |
| 4928 | |
| 4929 | crtc_id = wl_array_add(&b->unused_crtcs, sizeof(*crtc_id)); |
| 4930 | *crtc_id = resources->crtcs[i]; |
| 4931 | } |
| 4932 | } |
| 4933 | |
| 4934 | /** |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4935 | * Create a Weston output structure |
| 4936 | * |
| 4937 | * Given a DRM connector, create a matching drm_output structure and add it |
| 4938 | * to Weston's output list. It also takes ownership of the connector, which |
| 4939 | * is released when output is destroyed. |
| 4940 | * |
| 4941 | * @param b Weston backend structure |
| 4942 | * @param resources DRM resources for this device |
| 4943 | * @param connector DRM connector to use for this new output |
| 4944 | * @param drm_device udev device pointer |
| 4945 | * @returns 0 on success, or -1 on failure |
| 4946 | */ |
| 4947 | static int |
| 4948 | create_output_for_connector(struct drm_backend *b, |
| 4949 | drmModeRes *resources, |
| 4950 | drmModeConnector *connector, |
| 4951 | struct udev_device *drm_device) |
| 4952 | { |
| 4953 | struct drm_output *output; |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 4954 | drmModeObjectPropertiesPtr props; |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 4955 | struct drm_mode *drm_mode; |
Pekka Paalanen | 26ac2e1 | 2017-04-03 13:18:13 +0300 | [diff] [blame] | 4956 | char *name; |
Pekka Paalanen | 6f1866b | 2017-04-03 14:22:51 +0300 | [diff] [blame] | 4957 | const char *make = "unknown"; |
| 4958 | const char *model = "unknown"; |
| 4959 | const char *serial_number = "unknown"; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4960 | int i; |
| 4961 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4962 | output = zalloc(sizeof *output); |
| 4963 | if (output == NULL) |
Daniel Stone | 62c0d63 | 2018-02-05 13:01:02 +0000 | [diff] [blame] | 4964 | goto err_init; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4965 | |
| 4966 | output->connector = connector; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4967 | output->connector_id = connector->connector_id; |
| 4968 | |
| 4969 | output->backlight = backlight_init(drm_device, |
| 4970 | connector->connector_type); |
| 4971 | |
Pekka Paalanen | 26ac2e1 | 2017-04-03 13:18:13 +0300 | [diff] [blame] | 4972 | name = make_connector_name(connector); |
| 4973 | weston_output_init(&output->base, b->compositor, name); |
| 4974 | free(name); |
| 4975 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4976 | output->base.enable = drm_output_enable; |
| 4977 | output->base.destroy = drm_output_destroy; |
| 4978 | output->base.disable = drm_output_disable; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4979 | |
| 4980 | output->destroy_pending = 0; |
| 4981 | output->disable_pending = 0; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 4982 | |
Pekka Paalanen | fc5f5d7 | 2017-09-05 16:11:15 +0300 | [diff] [blame] | 4983 | if (drm_output_init_crtc(output, resources, connector) < 0) |
Pekka Paalanen | cd011a6 | 2016-11-15 22:07:49 +0000 | [diff] [blame] | 4984 | goto err_output; |
Pekka Paalanen | cd011a6 | 2016-11-15 22:07:49 +0000 | [diff] [blame] | 4985 | |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 4986 | props = drmModeObjectGetProperties(b->drm.fd, connector->connector_id, |
| 4987 | DRM_MODE_OBJECT_CONNECTOR); |
| 4988 | if (!props) { |
| 4989 | weston_log("failed to get connector properties\n"); |
Daniel Stone | 62c0d63 | 2018-02-05 13:01:02 +0000 | [diff] [blame] | 4990 | goto err_output; |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 4991 | } |
| 4992 | drm_property_info_populate(b, connector_props, output->props_conn, |
| 4993 | WDRM_CONNECTOR__COUNT, props); |
Pekka Paalanen | 6f1866b | 2017-04-03 14:22:51 +0300 | [diff] [blame] | 4994 | find_and_parse_output_edid(b, output, props, |
| 4995 | &make, &model, &serial_number); |
| 4996 | output->base.make = (char *)make; |
| 4997 | output->base.model = (char *)model; |
| 4998 | output->base.serial_number = (char *)serial_number; |
Pekka Paalanen | a0bfedc | 2017-04-03 14:42:51 +0300 | [diff] [blame] | 4999 | output->base.subpixel = drm_subpixel_to_wayland(output->connector->subpixel); |
| 5000 | |
Daniel Stone | c1d0f47 | 2017-01-27 15:11:33 +0000 | [diff] [blame] | 5001 | drmModeFreeObjectProperties(props); |
| 5002 | |
Pekka Paalanen | a0bfedc | 2017-04-03 14:42:51 +0300 | [diff] [blame] | 5003 | if (output->connector->connector_type == DRM_MODE_CONNECTOR_LVDS || |
| 5004 | output->connector->connector_type == DRM_MODE_CONNECTOR_eDP) |
| 5005 | output->base.connection_internal = true; |
| 5006 | |
Pekka Paalanen | c4db6f7 | 2017-09-05 16:37:03 +0300 | [diff] [blame] | 5007 | if (drm_output_init_gamma_size(output) < 0) |
| 5008 | goto err_output; |
| 5009 | |
Daniel Stone | 7b2ddac | 2016-11-11 19:11:49 +0000 | [diff] [blame] | 5010 | output->state_cur = drm_output_state_alloc(output, NULL); |
| 5011 | |
Pekka Paalanen | a0bfedc | 2017-04-03 14:42:51 +0300 | [diff] [blame] | 5012 | output->base.mm_width = output->connector->mmWidth; |
| 5013 | output->base.mm_height = output->connector->mmHeight; |
| 5014 | |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 5015 | for (i = 0; i < output->connector->count_modes; i++) { |
| 5016 | drm_mode = drm_output_add_mode(output, &output->connector->modes[i]); |
| 5017 | if (!drm_mode) { |
Daniel Stone | 62c0d63 | 2018-02-05 13:01:02 +0000 | [diff] [blame] | 5018 | weston_log("failed to add mode\n"); |
| 5019 | goto err_output; |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 5020 | } |
| 5021 | } |
| 5022 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5023 | weston_compositor_add_pending_output(&output->base, b->compositor); |
| 5024 | |
| 5025 | return 0; |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 5026 | |
Daniel Stone | 62c0d63 | 2018-02-05 13:01:02 +0000 | [diff] [blame] | 5027 | err_output: |
| 5028 | drm_output_destroy(&output->base); |
| 5029 | return -1; |
| 5030 | /* no fallthrough! */ |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 5031 | |
Daniel Stone | 62c0d63 | 2018-02-05 13:01:02 +0000 | [diff] [blame] | 5032 | err_init: |
| 5033 | drmModeFreeConnector(connector); |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 5034 | return -1; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5035 | } |
| 5036 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5037 | static int |
Ucan, Emre (ADITG/SW1) | 9a200d7 | 2017-02-02 14:06:56 +0000 | [diff] [blame] | 5038 | create_outputs(struct drm_backend *b, struct udev_device *drm_device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5039 | { |
| 5040 | drmModeConnector *connector; |
| 5041 | drmModeRes *resources; |
| 5042 | int i; |
| 5043 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5044 | resources = drmModeGetResources(b->drm.fd); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5045 | if (!resources) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 5046 | weston_log("drmModeGetResources failed\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5047 | return -1; |
| 5048 | } |
| 5049 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5050 | b->min_width = resources->min_width; |
| 5051 | b->max_width = resources->max_width; |
| 5052 | b->min_height = resources->min_height; |
| 5053 | b->max_height = resources->max_height; |
Rob Clark | 4339add | 2012-08-09 14:18:28 -0500 | [diff] [blame] | 5054 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5055 | for (i = 0; i < resources->count_connectors; i++) { |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 5056 | int ret; |
| 5057 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5058 | connector = drmModeGetConnector(b->drm.fd, |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 5059 | resources->connectors[i]); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5060 | if (connector == NULL) |
| 5061 | continue; |
| 5062 | |
Ucan, Emre (ADITG/SW1) | 38ea2d2 | 2017-03-08 15:43:18 +0000 | [diff] [blame] | 5063 | if (connector->connection == DRM_MODE_CONNECTED) { |
Daniel Stone | 02cf466 | 2017-03-03 16:19:39 +0000 | [diff] [blame] | 5064 | ret = create_output_for_connector(b, resources, |
| 5065 | connector, drm_device); |
| 5066 | if (ret < 0) |
| 5067 | weston_log("failed to create new connector\n"); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5068 | } else { |
| 5069 | drmModeFreeConnector(connector); |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 5070 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5071 | } |
| 5072 | |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 5073 | drm_backend_update_unused_outputs(b, resources); |
| 5074 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5075 | if (wl_list_empty(&b->compositor->output_list) && |
| 5076 | wl_list_empty(&b->compositor->pending_output_list)) |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 5077 | weston_log("No currently active connector found.\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5078 | |
| 5079 | drmModeFreeResources(resources); |
| 5080 | |
| 5081 | return 0; |
| 5082 | } |
| 5083 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5084 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5085 | update_outputs(struct drm_backend *b, struct udev_device *drm_device) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5086 | { |
| 5087 | drmModeConnector *connector; |
| 5088 | drmModeRes *resources; |
| 5089 | struct drm_output *output, *next; |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 5090 | uint32_t *connected; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5091 | int i; |
| 5092 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5093 | resources = drmModeGetResources(b->drm.fd); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5094 | if (!resources) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 5095 | weston_log("drmModeGetResources failed\n"); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5096 | return; |
| 5097 | } |
| 5098 | |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 5099 | connected = calloc(resources->count_connectors, sizeof(uint32_t)); |
| 5100 | if (!connected) { |
| 5101 | drmModeFreeResources(resources); |
| 5102 | return; |
| 5103 | } |
| 5104 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5105 | /* collect new connects */ |
| 5106 | for (i = 0; i < resources->count_connectors; i++) { |
Ucan, Emre (ADITG/SW1) | 21e4944 | 2017-02-02 14:06:55 +0000 | [diff] [blame] | 5107 | uint32_t connector_id = resources->connectors[i]; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 5108 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5109 | connector = drmModeGetConnector(b->drm.fd, connector_id); |
David Herrmann | 7551cff | 2011-12-08 17:05:43 +0100 | [diff] [blame] | 5110 | if (connector == NULL) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5111 | continue; |
| 5112 | |
David Herrmann | 7551cff | 2011-12-08 17:05:43 +0100 | [diff] [blame] | 5113 | if (connector->connection != DRM_MODE_CONNECTED) { |
| 5114 | drmModeFreeConnector(connector); |
| 5115 | continue; |
| 5116 | } |
| 5117 | |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 5118 | connected[i] = connector_id; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 5119 | |
Daniel Stone | c0ec759 | 2017-02-09 13:58:35 +0000 | [diff] [blame] | 5120 | if (drm_output_find_by_connector(b, connector_id)) { |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5121 | drmModeFreeConnector(connector); |
Daniel Stone | c0ec759 | 2017-02-09 13:58:35 +0000 | [diff] [blame] | 5122 | continue; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5123 | } |
Daniel Stone | c0ec759 | 2017-02-09 13:58:35 +0000 | [diff] [blame] | 5124 | |
| 5125 | create_output_for_connector(b, resources, |
| 5126 | connector, drm_device); |
| 5127 | weston_log("connector %d connected\n", connector_id); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5128 | } |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5129 | |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 5130 | wl_list_for_each_safe(output, next, &b->compositor->output_list, |
| 5131 | base.link) { |
| 5132 | bool disconnected = true; |
| 5133 | |
| 5134 | for (i = 0; i < resources->count_connectors; i++) { |
| 5135 | if (connected[i] == output->connector_id) { |
| 5136 | disconnected = false; |
| 5137 | break; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5138 | } |
| 5139 | } |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5140 | |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 5141 | if (!disconnected) |
| 5142 | continue; |
| 5143 | |
| 5144 | weston_log("connector %d disconnected\n", output->connector_id); |
| 5145 | drm_output_destroy(&output->base); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5146 | } |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 5147 | |
| 5148 | wl_list_for_each_safe(output, next, &b->compositor->pending_output_list, |
| 5149 | base.link) { |
| 5150 | bool disconnected = true; |
| 5151 | |
| 5152 | for (i = 0; i < resources->count_connectors; i++) { |
| 5153 | if (connected[i] == output->connector_id) { |
| 5154 | disconnected = false; |
| 5155 | break; |
| 5156 | } |
| 5157 | } |
| 5158 | |
| 5159 | if (!disconnected) |
| 5160 | continue; |
| 5161 | |
| 5162 | weston_log("connector %d disconnected\n", output->connector_id); |
| 5163 | drm_output_destroy(&output->base); |
| 5164 | } |
| 5165 | |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 5166 | drm_backend_update_unused_outputs(b, resources); |
| 5167 | |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 5168 | free(connected); |
| 5169 | drmModeFreeResources(resources); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5170 | } |
| 5171 | |
| 5172 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5173 | udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5174 | { |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 5175 | const char *sysnum; |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 5176 | const char *val; |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 5177 | |
| 5178 | sysnum = udev_device_get_sysnum(device); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5179 | if (!sysnum || atoi(sysnum) != b->drm.id) |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 5180 | return 0; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 5181 | |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 5182 | val = udev_device_get_property_value(device, "HOTPLUG"); |
| 5183 | if (!val) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5184 | return 0; |
| 5185 | |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 5186 | return strcmp(val, "1") == 0; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5187 | } |
| 5188 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 5189 | static int |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5190 | udev_drm_event(int fd, uint32_t mask, void *data) |
| 5191 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5192 | struct drm_backend *b = data; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5193 | struct udev_device *event; |
| 5194 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5195 | event = udev_monitor_receive_device(b->udev_monitor); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 5196 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5197 | if (udev_event_is_hotplug(b, event)) |
| 5198 | update_outputs(b, event); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5199 | |
| 5200 | udev_device_unref(event); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 5201 | |
| 5202 | return 1; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5203 | } |
| 5204 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 5205 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 5206 | drm_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 5207 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 5208 | struct drm_backend *b = to_drm_backend(ec); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 5209 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5210 | udev_input_destroy(&b->input); |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 5211 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5212 | wl_event_source_remove(b->udev_drm_source); |
| 5213 | wl_event_source_remove(b->drm_source); |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 5214 | |
Daniel Stone | b57c6a0 | 2017-10-05 16:27:21 +0100 | [diff] [blame] | 5215 | b->shutting_down = true; |
| 5216 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5217 | destroy_sprites(b); |
Kristian Høgsberg | 3d64a3e | 2013-05-10 12:36:04 -0400 | [diff] [blame] | 5218 | |
Ander Conselvan de Oliveira | 6b16214 | 2013-10-25 16:26:32 +0300 | [diff] [blame] | 5219 | weston_compositor_shutdown(ec); |
| 5220 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5221 | if (b->gbm) |
| 5222 | gbm_device_destroy(b->gbm); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 5223 | |
Pekka Paalanen | 5b0aa55 | 2017-12-07 16:06:05 +0200 | [diff] [blame^] | 5224 | udev_monitor_unref(b->udev_monitor); |
Pekka Paalanen | 2a0c6c3 | 2017-09-13 16:48:01 +0300 | [diff] [blame] | 5225 | udev_unref(b->udev); |
| 5226 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5227 | weston_launcher_destroy(ec->launcher); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 5228 | |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 5229 | wl_array_release(&b->unused_crtcs); |
| 5230 | wl_array_release(&b->unused_connectors); |
| 5231 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5232 | close(b->drm.fd); |
Pekka Paalanen | 9bf4f37 | 2017-12-07 16:05:29 +0200 | [diff] [blame] | 5233 | free(b->drm.filename); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5234 | free(b); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 5235 | } |
| 5236 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 5237 | static void |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 5238 | session_notify(struct wl_listener *listener, void *data) |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 5239 | { |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 5240 | struct weston_compositor *compositor = data; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 5241 | struct drm_backend *b = to_drm_backend(compositor); |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 5242 | struct drm_plane *plane; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 5243 | struct drm_output *output; |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 5244 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5245 | if (compositor->session_active) { |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 5246 | weston_log("activating session\n"); |
Daniel Stone | f33e104 | 2016-11-05 08:10:13 +0000 | [diff] [blame] | 5247 | weston_compositor_wake(compositor); |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 5248 | weston_compositor_damage_all(compositor); |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 5249 | b->state_invalid = true; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5250 | udev_input_enable(&b->input); |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 5251 | } else { |
| 5252 | weston_log("deactivating session\n"); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5253 | udev_input_disable(&b->input); |
Kristian Høgsberg | 4014a6b | 2012-04-10 00:08:45 -0400 | [diff] [blame] | 5254 | |
Philipp Brüschweiler | 57edf7f | 2013-03-29 13:01:56 +0100 | [diff] [blame] | 5255 | weston_compositor_offscreen(compositor); |
Kristian Høgsberg | d8e181b | 2011-05-06 15:38:28 -0400 | [diff] [blame] | 5256 | |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 5257 | /* If we have a repaint scheduled (either from a |
| 5258 | * pending pageflip or the idle handler), make sure we |
| 5259 | * 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] | 5260 | * vt switched away. The OFFSCREEN state will prevent |
Abdur Rehman | 4dca0e1 | 2017-01-01 19:46:35 +0500 | [diff] [blame] | 5261 | * further attempts at repainting. When we switch |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 5262 | * back, we schedule a repaint, which will process |
| 5263 | * pending frame callbacks. */ |
| 5264 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5265 | wl_list_for_each(output, &compositor->output_list, base.link) { |
Daniel Stone | 09a97e2 | 2017-03-01 11:34:06 +0000 | [diff] [blame] | 5266 | output->base.repaint_needed = false; |
Daniel Stone | 2ba17f4 | 2015-05-19 20:02:41 +0100 | [diff] [blame] | 5267 | if (output->cursor_plane) |
| 5268 | drmModeSetCursor(b->drm.fd, output->crtc_id, |
| 5269 | 0, 0, 0); |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 5270 | } |
| 5271 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5272 | output = container_of(compositor->output_list.next, |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 5273 | struct drm_output, base.link); |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 5274 | |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 5275 | wl_list_for_each(plane, &b->plane_list, link) { |
| 5276 | if (plane->type != WDRM_PLANE_TYPE_OVERLAY) |
| 5277 | continue; |
| 5278 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5279 | drmModeSetPlane(b->drm.fd, |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 5280 | plane->plane_id, |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 5281 | output->crtc_id, 0, 0, |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 5282 | 0, 0, 0, 0, 0, 0, 0, 0); |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 5283 | } |
| 5284 | } |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 5285 | } |
| 5286 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 5287 | /** |
| 5288 | * Determines whether or not a device is capable of modesetting. If successful, |
| 5289 | * sets b->drm.fd and b->drm.filename to the opened device. |
| 5290 | */ |
| 5291 | static bool |
| 5292 | drm_device_is_kms(struct drm_backend *b, struct udev_device *device) |
| 5293 | { |
| 5294 | const char *filename = udev_device_get_devnode(device); |
| 5295 | const char *sysnum = udev_device_get_sysnum(device); |
| 5296 | drmModeRes *res; |
| 5297 | int id, fd; |
| 5298 | |
| 5299 | if (!filename) |
| 5300 | return false; |
| 5301 | |
| 5302 | fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR); |
| 5303 | if (fd < 0) |
| 5304 | return false; |
| 5305 | |
| 5306 | res = drmModeGetResources(fd); |
| 5307 | if (!res) |
| 5308 | goto out_fd; |
| 5309 | |
| 5310 | if (res->count_crtcs <= 0 || res->count_connectors <= 0 || |
| 5311 | res->count_encoders <= 0) |
| 5312 | goto out_res; |
| 5313 | |
| 5314 | if (sysnum) |
| 5315 | id = atoi(sysnum); |
| 5316 | if (!sysnum || id < 0) { |
| 5317 | weston_log("couldn't get sysnum for device %s\n", filename); |
| 5318 | goto out_res; |
| 5319 | } |
| 5320 | |
| 5321 | /* We can be called successfully on multiple devices; if we have, |
| 5322 | * clean up old entries. */ |
| 5323 | if (b->drm.fd >= 0) |
| 5324 | weston_launcher_close(b->compositor->launcher, b->drm.fd); |
| 5325 | free(b->drm.filename); |
| 5326 | |
| 5327 | b->drm.fd = fd; |
| 5328 | b->drm.id = id; |
| 5329 | b->drm.filename = strdup(filename); |
| 5330 | |
Sergi Granell | ceb5981 | 2017-03-28 12:44:04 +0200 | [diff] [blame] | 5331 | drmModeFreeResources(res); |
| 5332 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 5333 | return true; |
| 5334 | |
| 5335 | out_res: |
| 5336 | drmModeFreeResources(res); |
| 5337 | out_fd: |
| 5338 | weston_launcher_close(b->compositor->launcher, fd); |
| 5339 | return false; |
| 5340 | } |
| 5341 | |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5342 | /* |
| 5343 | * Find primary GPU |
| 5344 | * Some systems may have multiple DRM devices attached to a single seat. This |
| 5345 | * function loops over all devices and tries to find a PCI device with the |
| 5346 | * boot_vga sysfs attribute set to 1. |
| 5347 | * 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] | 5348 | * Devices are also vetted to make sure they are are capable of modesetting, |
| 5349 | * rather than pure render nodes (GPU with no display), or pure |
| 5350 | * memory-allocation devices (VGEM). |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5351 | */ |
| 5352 | static struct udev_device* |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5353 | find_primary_gpu(struct drm_backend *b, const char *seat) |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5354 | { |
| 5355 | struct udev_enumerate *e; |
| 5356 | struct udev_list_entry *entry; |
| 5357 | const char *path, *device_seat, *id; |
| 5358 | struct udev_device *device, *drm_device, *pci; |
| 5359 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5360 | e = udev_enumerate_new(b->udev); |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5361 | udev_enumerate_add_match_subsystem(e, "drm"); |
| 5362 | udev_enumerate_add_match_sysname(e, "card[0-9]*"); |
| 5363 | |
| 5364 | udev_enumerate_scan_devices(e); |
| 5365 | drm_device = NULL; |
| 5366 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 5367 | bool is_boot_vga = false; |
| 5368 | |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5369 | path = udev_list_entry_get_name(entry); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5370 | device = udev_device_new_from_syspath(b->udev, path); |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5371 | if (!device) |
| 5372 | continue; |
| 5373 | device_seat = udev_device_get_property_value(device, "ID_SEAT"); |
| 5374 | if (!device_seat) |
| 5375 | device_seat = default_seat; |
| 5376 | if (strcmp(device_seat, seat)) { |
| 5377 | udev_device_unref(device); |
| 5378 | continue; |
| 5379 | } |
| 5380 | |
| 5381 | pci = udev_device_get_parent_with_subsystem_devtype(device, |
| 5382 | "pci", NULL); |
| 5383 | if (pci) { |
| 5384 | id = udev_device_get_sysattr_value(pci, "boot_vga"); |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 5385 | if (id && !strcmp(id, "1")) |
| 5386 | is_boot_vga = true; |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5387 | } |
| 5388 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 5389 | /* If we already have a modesetting-capable device, and this |
| 5390 | * device isn't our boot-VGA device, we aren't going to use |
| 5391 | * it. */ |
| 5392 | if (!is_boot_vga && drm_device) { |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5393 | udev_device_unref(device); |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 5394 | continue; |
| 5395 | } |
| 5396 | |
| 5397 | /* Make sure this device is actually capable of modesetting; |
| 5398 | * if this call succeeds, b->drm.{fd,filename} will be set, |
| 5399 | * and any old values freed. */ |
| 5400 | if (!drm_device_is_kms(b, device)) { |
| 5401 | udev_device_unref(device); |
| 5402 | continue; |
| 5403 | } |
| 5404 | |
| 5405 | /* There can only be one boot_vga device, and we try to use it |
| 5406 | * at all costs. */ |
| 5407 | if (is_boot_vga) { |
| 5408 | if (drm_device) |
| 5409 | udev_device_unref(drm_device); |
| 5410 | drm_device = device; |
| 5411 | break; |
| 5412 | } |
| 5413 | |
| 5414 | /* Per the (!is_boot_vga && drm_device) test above, we only |
| 5415 | * trump existing saved devices with boot-VGA devices, so if |
| 5416 | * we end up here, this must be the first device we've seen. */ |
| 5417 | assert(!drm_device); |
| 5418 | drm_device = device; |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5419 | } |
| 5420 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 5421 | /* If we're returning a device to use, we must have an open FD for |
| 5422 | * it. */ |
| 5423 | assert(!!drm_device == (b->drm.fd >= 0)); |
| 5424 | |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5425 | udev_enumerate_unref(e); |
| 5426 | return drm_device; |
| 5427 | } |
| 5428 | |
Pekka Paalanen | b45ed8b | 2017-03-28 18:04:27 +0300 | [diff] [blame] | 5429 | static struct udev_device * |
| 5430 | open_specific_drm_device(struct drm_backend *b, const char *name) |
| 5431 | { |
| 5432 | struct udev_device *device; |
| 5433 | |
| 5434 | device = udev_device_new_from_subsystem_sysname(b->udev, "drm", name); |
| 5435 | if (!device) { |
| 5436 | weston_log("ERROR: could not open DRM device '%s'\n", name); |
| 5437 | return NULL; |
| 5438 | } |
| 5439 | |
| 5440 | if (!drm_device_is_kms(b, device)) { |
| 5441 | udev_device_unref(device); |
| 5442 | weston_log("ERROR: DRM device '%s' is not a KMS device.\n", name); |
| 5443 | return NULL; |
| 5444 | } |
| 5445 | |
| 5446 | /* If we're returning a device to use, we must have an open FD for |
| 5447 | * it. */ |
| 5448 | assert(b->drm.fd >= 0); |
| 5449 | |
| 5450 | return device; |
| 5451 | } |
| 5452 | |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 5453 | static void |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 5454 | planes_binding(struct weston_keyboard *keyboard, const struct timespec *time, |
| 5455 | uint32_t key, void *data) |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 5456 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5457 | struct drm_backend *b = data; |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 5458 | |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 5459 | switch (key) { |
| 5460 | case KEY_C: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5461 | b->cursors_are_broken ^= 1; |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 5462 | break; |
| 5463 | case KEY_V: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5464 | b->sprites_are_broken ^= 1; |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 5465 | break; |
| 5466 | case KEY_O: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5467 | b->sprites_hidden ^= 1; |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 5468 | break; |
| 5469 | default: |
| 5470 | break; |
| 5471 | } |
Ander Conselvan de Oliveira | 180f42a | 2012-11-21 15:11:37 +0200 | [diff] [blame] | 5472 | } |
| 5473 | |
Kristian Høgsberg | 0eac34a | 2013-08-30 14:28:22 -0700 | [diff] [blame] | 5474 | #ifdef BUILD_VAAPI_RECORDER |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5475 | static void |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 5476 | recorder_destroy(struct drm_output *output) |
| 5477 | { |
| 5478 | vaapi_recorder_destroy(output->recorder); |
| 5479 | output->recorder = NULL; |
| 5480 | |
| 5481 | output->base.disable_planes--; |
| 5482 | |
| 5483 | wl_list_remove(&output->recorder_frame_listener.link); |
| 5484 | weston_log("[libva recorder] done\n"); |
| 5485 | } |
| 5486 | |
| 5487 | static void |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5488 | recorder_frame_notify(struct wl_listener *listener, void *data) |
| 5489 | { |
| 5490 | struct drm_output *output; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5491 | struct drm_backend *b; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5492 | int fd, ret; |
| 5493 | |
| 5494 | output = container_of(listener, struct drm_output, |
| 5495 | recorder_frame_listener); |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 5496 | b = to_drm_backend(output->base.compositor); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5497 | |
| 5498 | if (!output->recorder) |
| 5499 | return; |
| 5500 | |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 5501 | ret = drmPrimeHandleToFD(b->drm.fd, |
| 5502 | output->scanout_plane->state_cur->fb->handle, |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5503 | DRM_CLOEXEC, &fd); |
| 5504 | if (ret) { |
| 5505 | weston_log("[libva recorder] " |
| 5506 | "failed to create prime fd for front buffer\n"); |
| 5507 | return; |
| 5508 | } |
| 5509 | |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 5510 | ret = vaapi_recorder_frame(output->recorder, fd, |
Daniel Stone | e2e8013 | 2018-01-16 15:37:33 +0000 | [diff] [blame] | 5511 | output->scanout_plane->state_cur->fb->stride); |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 5512 | if (ret < 0) { |
| 5513 | weston_log("[libva recorder] aborted: %m\n"); |
| 5514 | recorder_destroy(output); |
| 5515 | } |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5516 | } |
| 5517 | |
| 5518 | static void * |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5519 | create_recorder(struct drm_backend *b, int width, int height, |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5520 | const char *filename) |
| 5521 | { |
| 5522 | int fd; |
| 5523 | drm_magic_t magic; |
| 5524 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5525 | fd = open(b->drm.filename, O_RDWR | O_CLOEXEC); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5526 | if (fd < 0) |
| 5527 | return NULL; |
| 5528 | |
| 5529 | drmGetMagic(fd, &magic); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5530 | drmAuthMagic(b->drm.fd, magic); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5531 | |
| 5532 | return vaapi_recorder_create(fd, width, height, filename); |
| 5533 | } |
| 5534 | |
| 5535 | static void |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 5536 | recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time, |
| 5537 | uint32_t key, void *data) |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5538 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5539 | struct drm_backend *b = data; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5540 | struct drm_output *output; |
| 5541 | int width, height; |
| 5542 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5543 | output = container_of(b->compositor->output_list.next, |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5544 | struct drm_output, base.link); |
| 5545 | |
| 5546 | if (!output->recorder) { |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 5547 | if (output->gbm_format != GBM_FORMAT_XRGB8888) { |
Ander Conselvan de Oliveira | 2ef1cd1 | 2014-05-06 16:49:06 +0300 | [diff] [blame] | 5548 | weston_log("failed to start vaapi recorder: " |
| 5549 | "output format not supported\n"); |
| 5550 | return; |
| 5551 | } |
| 5552 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 5553 | width = output->base.current_mode->width; |
| 5554 | height = output->base.current_mode->height; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5555 | |
| 5556 | output->recorder = |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5557 | create_recorder(b, width, height, "capture.h264"); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5558 | if (!output->recorder) { |
| 5559 | weston_log("failed to create vaapi recorder\n"); |
| 5560 | return; |
| 5561 | } |
| 5562 | |
| 5563 | output->base.disable_planes++; |
| 5564 | |
| 5565 | output->recorder_frame_listener.notify = recorder_frame_notify; |
| 5566 | wl_signal_add(&output->base.frame_signal, |
| 5567 | &output->recorder_frame_listener); |
| 5568 | |
| 5569 | weston_output_schedule_repaint(&output->base); |
| 5570 | |
| 5571 | weston_log("[libva recorder] initialized\n"); |
| 5572 | } else { |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 5573 | recorder_destroy(output); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5574 | } |
| 5575 | } |
| 5576 | #else |
| 5577 | static void |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 5578 | recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time, |
| 5579 | uint32_t key, void *data) |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 5580 | { |
| 5581 | weston_log("Compiled without libva support\n"); |
| 5582 | } |
| 5583 | #endif |
| 5584 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 5585 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5586 | switch_to_gl_renderer(struct drm_backend *b) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 5587 | { |
| 5588 | struct drm_output *output; |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 5589 | bool dmabuf_support_inited; |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 5590 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5591 | if (!b->use_pixman) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 5592 | return; |
| 5593 | |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 5594 | dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf; |
| 5595 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 5596 | weston_log("Switching to GL renderer\n"); |
| 5597 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5598 | b->gbm = create_gbm_device(b->drm.fd); |
| 5599 | if (!b->gbm) { |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 5600 | weston_log("Failed to create gbm device. " |
| 5601 | "Aborting renderer switch\n"); |
| 5602 | return; |
| 5603 | } |
| 5604 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5605 | 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] | 5606 | pixman_renderer_output_destroy(&output->base); |
| 5607 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5608 | b->compositor->renderer->destroy(b->compositor); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 5609 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5610 | if (drm_backend_create_gl_renderer(b) < 0) { |
| 5611 | gbm_device_destroy(b->gbm); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 5612 | weston_log("Failed to create GL renderer. Quitting.\n"); |
| 5613 | /* FIXME: we need a function to shutdown cleanly */ |
| 5614 | assert(0); |
| 5615 | } |
| 5616 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5617 | wl_list_for_each(output, &b->compositor->output_list, base.link) |
| 5618 | drm_output_init_egl(output, b); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 5619 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5620 | b->use_pixman = 0; |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 5621 | |
| 5622 | if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) { |
| 5623 | if (linux_dmabuf_setup(b->compositor) < 0) |
| 5624 | weston_log("Error: initializing dmabuf " |
| 5625 | "support failed.\n"); |
| 5626 | } |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 5627 | } |
| 5628 | |
| 5629 | static void |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 5630 | renderer_switch_binding(struct weston_keyboard *keyboard, |
| 5631 | const struct timespec *time, uint32_t key, void *data) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 5632 | { |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 5633 | struct drm_backend *b = |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 5634 | to_drm_backend(keyboard->seat->compositor); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 5635 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5636 | switch_to_gl_renderer(b); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 5637 | } |
| 5638 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5639 | static const struct weston_drm_output_api api = { |
| 5640 | drm_output_set_mode, |
| 5641 | drm_output_set_gbm_format, |
| 5642 | drm_output_set_seat, |
| 5643 | }; |
| 5644 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5645 | static struct drm_backend * |
| 5646 | drm_backend_create(struct weston_compositor *compositor, |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5647 | struct weston_drm_backend_config *config) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5648 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5649 | struct drm_backend *b; |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 5650 | struct udev_device *drm_device; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5651 | struct wl_event_loop *loop; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5652 | const char *seat_id = default_seat; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5653 | int ret; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5654 | |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 5655 | weston_log("initializing drm backend\n"); |
| 5656 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5657 | b = zalloc(sizeof *b); |
| 5658 | if (b == NULL) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 5659 | return NULL; |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 5660 | |
Daniel Stone | 6020f47 | 2018-02-05 15:46:20 +0000 | [diff] [blame] | 5661 | b->state_invalid = true; |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 5662 | b->drm.fd = -1; |
Daniel Stone | 087ddf0 | 2017-02-14 17:51:30 +0000 | [diff] [blame] | 5663 | wl_array_init(&b->unused_crtcs); |
| 5664 | wl_array_init(&b->unused_connectors); |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 5665 | |
Pekka Paalanen | 6858383 | 2015-05-19 09:53:16 +0300 | [diff] [blame] | 5666 | /* |
| 5667 | * KMS support for hardware planes cannot properly synchronize |
| 5668 | * without nuclear page flip. Without nuclear/atomic, hw plane |
| 5669 | * and cursor plane updates would either tear or cause extra |
| 5670 | * waits for vblanks which means dropping the compositor framerate |
Pekka Paalanen | 3f32a13 | 2015-09-07 15:38:43 +0300 | [diff] [blame] | 5671 | * to a fraction. For cursors, it's not so bad, so they are |
| 5672 | * enabled. |
Pekka Paalanen | 6858383 | 2015-05-19 09:53:16 +0300 | [diff] [blame] | 5673 | * |
| 5674 | * These can be enabled again when nuclear/atomic support lands. |
| 5675 | */ |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5676 | b->sprites_are_broken = 1; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5677 | b->compositor = compositor; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5678 | b->use_pixman = config->use_pixman; |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 5679 | b->pageflip_timeout = config->pageflip_timeout; |
Kristian Høgsberg | 8e6f376 | 2013-10-16 16:31:42 -0700 | [diff] [blame] | 5680 | |
Pekka Paalanen | 7da9a38 | 2017-08-30 11:29:49 +0300 | [diff] [blame] | 5681 | compositor->backend = &b->base; |
| 5682 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5683 | if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0) |
| 5684 | goto err_compositor; |
Kristian Høgsberg | 8e6f376 | 2013-10-16 16:31:42 -0700 | [diff] [blame] | 5685 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5686 | if (config->seat_id) |
| 5687 | seat_id = config->seat_id; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 5688 | |
Rafal Mielniczuk | 32069c0 | 2013-03-27 18:39:28 +0100 | [diff] [blame] | 5689 | /* Check if we run drm-backend using weston-launch */ |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5690 | compositor->launcher = weston_launcher_connect(compositor, config->tty, |
| 5691 | seat_id, true); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5692 | if (compositor->launcher == NULL) { |
Pekka Paalanen | a453f4d | 2017-10-31 10:19:48 +0200 | [diff] [blame] | 5693 | weston_log("fatal: drm backend should be run using " |
| 5694 | "weston-launch binary, or your system should " |
| 5695 | "provide the logind D-Bus API.\n"); |
Rafal Mielniczuk | 32069c0 | 2013-03-27 18:39:28 +0100 | [diff] [blame] | 5696 | goto err_compositor; |
| 5697 | } |
| 5698 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5699 | b->udev = udev_new(); |
| 5700 | if (b->udev == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 5701 | weston_log("failed to initialize udev context\n"); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 5702 | goto err_launcher; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5703 | } |
| 5704 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5705 | b->session_listener.notify = session_notify; |
| 5706 | wl_signal_add(&compositor->session_signal, &b->session_listener); |
Kristian Høgsberg | c5b9ddb | 2012-01-15 14:29:09 -0500 | [diff] [blame] | 5707 | |
Pekka Paalanen | b45ed8b | 2017-03-28 18:04:27 +0300 | [diff] [blame] | 5708 | if (config->specific_device) |
| 5709 | drm_device = open_specific_drm_device(b, config->specific_device); |
| 5710 | else |
| 5711 | drm_device = find_primary_gpu(b, seat_id); |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 5712 | if (drm_device == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 5713 | weston_log("no drm device found\n"); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 5714 | goto err_udev; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 5715 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5716 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 5717 | if (init_kms_caps(b) < 0) { |
Ander Conselvan de Oliveira | 2292917 | 2013-01-25 15:13:02 +0200 | [diff] [blame] | 5718 | weston_log("failed to initialize kms\n"); |
| 5719 | goto err_udev_dev; |
| 5720 | } |
| 5721 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5722 | if (b->use_pixman) { |
| 5723 | if (init_pixman(b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 5724 | weston_log("failed to initialize pixman renderer\n"); |
| 5725 | goto err_udev_dev; |
| 5726 | } |
| 5727 | } else { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5728 | if (init_egl(b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 5729 | weston_log("failed to initialize egl\n"); |
| 5730 | goto err_udev_dev; |
| 5731 | } |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 5732 | } |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 5733 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5734 | b->base.destroy = drm_destroy; |
Daniel Stone | eedf84c | 2017-02-10 18:06:04 +0000 | [diff] [blame] | 5735 | b->base.repaint_begin = drm_repaint_begin; |
| 5736 | b->base.repaint_flush = drm_repaint_flush; |
| 5737 | b->base.repaint_cancel = drm_repaint_cancel; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 5738 | |
Bob Ham | 91880f1 | 2016-01-12 10:21:47 +0000 | [diff] [blame] | 5739 | weston_setup_vt_switch_bindings(compositor); |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 5740 | |
Daniel Stone | 085d2b9 | 2015-05-21 00:00:57 +0100 | [diff] [blame] | 5741 | wl_list_init(&b->plane_list); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5742 | create_sprites(b); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 5743 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5744 | if (udev_input_init(&b->input, |
Giulio Camuffo | 8aedf7b | 2016-06-02 21:48:12 +0300 | [diff] [blame] | 5745 | compositor, b->udev, seat_id, |
| 5746 | config->configure_device) < 0) { |
Ander Conselvan de Oliveira | 4ade0e4 | 2014-04-17 13:08:45 +0300 | [diff] [blame] | 5747 | weston_log("failed to create input devices\n"); |
| 5748 | goto err_sprite; |
| 5749 | } |
| 5750 | |
Ucan, Emre (ADITG/SW1) | 9a200d7 | 2017-02-02 14:06:56 +0000 | [diff] [blame] | 5751 | if (create_outputs(b, drm_device) < 0) { |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 5752 | weston_log("failed to create output for %s\n", b->drm.filename); |
Ander Conselvan de Oliveira | 4ade0e4 | 2014-04-17 13:08:45 +0300 | [diff] [blame] | 5753 | goto err_udev_input; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 5754 | } |
| 5755 | |
Jason Ekstrand | 9fc7151 | 2014-04-02 19:53:46 -0500 | [diff] [blame] | 5756 | /* A this point we have some idea of whether or not we have a working |
| 5757 | * cursor plane. */ |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5758 | if (!b->cursors_are_broken) |
| 5759 | compositor->capabilities |= WESTON_CAP_CURSOR_PLANE; |
Jason Ekstrand | 9fc7151 | 2014-04-02 19:53:46 -0500 | [diff] [blame] | 5760 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5761 | loop = wl_display_get_event_loop(compositor->wl_display); |
| 5762 | b->drm_source = |
| 5763 | wl_event_loop_add_fd(loop, b->drm.fd, |
| 5764 | WL_EVENT_READABLE, on_drm_input, b); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 5765 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5766 | b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev"); |
| 5767 | if (b->udev_monitor == NULL) { |
Abdur Rehman | 4dca0e1 | 2017-01-01 19:46:35 +0500 | [diff] [blame] | 5768 | weston_log("failed to initialize udev monitor\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 5769 | goto err_drm_source; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5770 | } |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5771 | udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor, |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5772 | "drm", NULL); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5773 | b->udev_drm_source = |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 5774 | wl_event_loop_add_fd(loop, |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5775 | udev_monitor_get_fd(b->udev_monitor), |
| 5776 | WL_EVENT_READABLE, udev_drm_event, b); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5777 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5778 | if (udev_monitor_enable_receiving(b->udev_monitor) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 5779 | weston_log("failed to enable udev-monitor receiving\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 5780 | goto err_udev_monitor; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 5781 | } |
| 5782 | |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 5783 | udev_device_unref(drm_device); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 5784 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5785 | weston_compositor_add_debug_binding(compositor, KEY_O, |
| 5786 | planes_binding, b); |
| 5787 | weston_compositor_add_debug_binding(compositor, KEY_C, |
| 5788 | planes_binding, b); |
| 5789 | weston_compositor_add_debug_binding(compositor, KEY_V, |
| 5790 | planes_binding, b); |
| 5791 | weston_compositor_add_debug_binding(compositor, KEY_Q, |
| 5792 | recorder_binding, b); |
| 5793 | weston_compositor_add_debug_binding(compositor, KEY_W, |
| 5794 | renderer_switch_binding, b); |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 5795 | |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 5796 | if (compositor->renderer->import_dmabuf) { |
| 5797 | if (linux_dmabuf_setup(compositor) < 0) |
| 5798 | weston_log("Error: initializing dmabuf " |
| 5799 | "support failed.\n"); |
| 5800 | } |
| 5801 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 5802 | ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME, |
| 5803 | &api, sizeof(api)); |
| 5804 | |
| 5805 | if (ret < 0) { |
| 5806 | weston_log("Failed to register output API.\n"); |
| 5807 | goto err_udev_monitor; |
| 5808 | } |
| 5809 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5810 | return b; |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 5811 | |
| 5812 | err_udev_monitor: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5813 | wl_event_source_remove(b->udev_drm_source); |
| 5814 | udev_monitor_unref(b->udev_monitor); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 5815 | err_drm_source: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5816 | wl_event_source_remove(b->drm_source); |
Ander Conselvan de Oliveira | 4ade0e4 | 2014-04-17 13:08:45 +0300 | [diff] [blame] | 5817 | err_udev_input: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5818 | udev_input_destroy(&b->input); |
Kristian Høgsberg | 2bc5e8e | 2012-09-06 20:51:00 -0400 | [diff] [blame] | 5819 | err_sprite: |
Emmanuel Gil Peyrot | b8347e3 | 2016-05-02 22:40:13 +0100 | [diff] [blame] | 5820 | if (b->gbm) |
| 5821 | gbm_device_destroy(b->gbm); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5822 | destroy_sprites(b); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 5823 | err_udev_dev: |
| 5824 | udev_device_unref(drm_device); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 5825 | err_launcher: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5826 | weston_launcher_destroy(compositor->launcher); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 5827 | err_udev: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5828 | udev_unref(b->udev); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 5829 | err_compositor: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5830 | weston_compositor_shutdown(compositor); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5831 | free(b); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 5832 | return NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 5833 | } |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 5834 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5835 | static void |
| 5836 | config_init_to_defaults(struct weston_drm_backend_config *config) |
| 5837 | { |
| 5838 | } |
| 5839 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5840 | WL_EXPORT int |
Quentin Glidic | 23e1d6f | 2016-12-02 14:08:44 +0100 | [diff] [blame] | 5841 | weston_backend_init(struct weston_compositor *compositor, |
| 5842 | struct weston_backend_config *config_base) |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 5843 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5844 | struct drm_backend *b; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5845 | struct weston_drm_backend_config config = {{ 0, }}; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 5846 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5847 | if (config_base == NULL || |
| 5848 | config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION || |
| 5849 | config_base->struct_size > sizeof(struct weston_drm_backend_config)) { |
| 5850 | weston_log("drm backend config structure is invalid\n"); |
| 5851 | return -1; |
| 5852 | } |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 5853 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5854 | config_init_to_defaults(&config); |
| 5855 | memcpy(&config, config_base, config_base->struct_size); |
Kristian Høgsberg | d8e9833 | 2013-10-16 16:15:11 -0700 | [diff] [blame] | 5856 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5857 | b = drm_backend_create(compositor, &config); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5858 | if (b == NULL) |
| 5859 | return -1; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 5860 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 5861 | return 0; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 5862 | } |