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