blob: 32b9182a3058b16fb77a2f64d8aa33a7812de0ec [file] [log] [blame]
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07005 * 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øgsbergfc783d42010-06-11 12:56:24 -040012 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070013 * 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øgsbergfc783d42010-06-11 12:56:24 -040025 */
26
Daniel Stonec228e232013-05-22 18:03:19 +030027#include "config.h"
Kristian Høgsberg0b9334a2011-04-12 11:34:32 -040028
Jesse Barnes58ef3792012-02-23 09:45:49 -050029#include <errno.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030030#include <stdint.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040031#include <stdlib.h>
Richard Hughes2b2092a2013-04-24 14:58:02 +010032#include <ctype.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040033#include <string.h>
34#include <fcntl.h>
35#include <unistd.h>
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -040036#include <linux/input.h>
Kristian Høgsberg3f495872013-09-18 23:00:17 -070037#include <linux/vt.h>
Ander Conselvan de Oliveirafd1f4c62012-06-26 17:09:14 +030038#include <assert.h>
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +020039#include <sys/mman.h>
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +030040#include <dlfcn.h>
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +030041#include <time.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040042
Benjamin Franzkec649a922011-03-02 11:56:04 +010043#include <xf86drm.h>
44#include <xf86drmMode.h>
Jesse Barnes58ef3792012-02-23 09:45:49 -050045#include <drm_fourcc.h>
Benjamin Franzkec649a922011-03-02 11:56:04 +010046
Benjamin Franzke060cf802011-04-30 09:32:11 +020047#include <gbm.h>
Pekka Paalanen33156972012-08-03 13:30:30 -040048#include <libudev.h>
Benjamin Franzke060cf802011-04-30 09:32:11 +020049
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -070050#include "compositor.h"
51#include "compositor-drm.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070052#include "shared/helpers.h"
Mario Kleinerf507ec32015-06-21 21:25:14 +020053#include "shared/timespec-util.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010054#include "gl-renderer.h"
Vincent Abriouc9506672016-10-05 16:14:07 +020055#include "weston-egl-ext.h"
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +020056#include "pixman-renderer.h"
Daniel Stone0b70fa42017-04-04 17:54:23 +010057#include "pixel-formats.h"
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -070058#include "libbacklight.h"
Peter Hutterer823ad332014-11-26 07:06:31 +100059#include "libinput-seat.h"
Benjamin Franzkebfeda132012-01-30 14:04:04 +010060#include "launcher-util.h"
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +030061#include "vaapi-recorder.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020062#include "presentation-time-server-protocol.h"
Pekka Paalanene4d231e2014-06-12 15:12:48 +030063#include "linux-dmabuf.h"
Micah Fedkec8890122017-02-01 15:28:23 -050064#include "linux-dmabuf-unstable-v1-server-protocol.h"
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040065
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +030066#ifndef DRM_CAP_TIMESTAMP_MONOTONIC
67#define DRM_CAP_TIMESTAMP_MONOTONIC 0x6
68#endif
69
Pekka Paalanenc5de57f2015-05-20 23:01:44 +010070#ifndef DRM_CLIENT_CAP_UNIVERSAL_PLANES
71#define DRM_CLIENT_CAP_UNIVERSAL_PLANES 2
72#endif
73
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -030074#ifndef DRM_CAP_CURSOR_WIDTH
75#define DRM_CAP_CURSOR_WIDTH 0x8
76#endif
77
78#ifndef DRM_CAP_CURSOR_HEIGHT
79#define DRM_CAP_CURSOR_HEIGHT 0x9
80#endif
81
82#ifndef GBM_BO_USE_CURSOR
83#define GBM_BO_USE_CURSOR GBM_BO_USE_CURSOR_64X64
84#endif
85
Pekka Paalanenc112f002017-08-28 16:27:20 +030086#define MAX_CLONED_CONNECTORS 1
87
Daniel Stone02cf4662017-03-03 16:19:39 +000088/**
Daniel Stone02cf4662017-03-03 16:19:39 +000089 * Represents the values of an enum-type KMS property
90 */
91struct drm_property_enum_info {
92 const char *name; /**< name as string (static, not freed) */
93 bool valid; /**< true if value is supported; ignore if false */
94 uint64_t value; /**< raw value */
95};
96
97/**
98 * Holds information on a DRM property, including its ID and the enum
99 * values it holds.
100 *
101 * DRM properties are allocated dynamically, and maintained as DRM objects
102 * within the normal object ID space; they thus do not have a stable ID
103 * to refer to. This includes enum values, which must be referred to by
104 * integer values, but these are not stable.
105 *
106 * drm_property_info allows a cache to be maintained where Weston can use
107 * enum values internally to refer to properties, with the mapping to DRM
108 * ID values being maintained internally.
109 */
110struct drm_property_info {
111 const char *name; /**< name as string (static, not freed) */
112 uint32_t prop_id; /**< KMS property object ID */
113 unsigned int num_enum_values; /**< number of enum values */
114 struct drm_property_enum_info *enum_values; /**< array of enum values */
115};
116
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000117/**
Daniel Stone598ee9d2016-11-16 11:55:20 +0000118 * List of properties attached to DRM planes
119 */
120enum wdrm_plane_property {
121 WDRM_PLANE_TYPE = 0,
122 WDRM_PLANE_SRC_X,
123 WDRM_PLANE_SRC_Y,
124 WDRM_PLANE_SRC_W,
125 WDRM_PLANE_SRC_H,
126 WDRM_PLANE_CRTC_X,
127 WDRM_PLANE_CRTC_Y,
128 WDRM_PLANE_CRTC_W,
129 WDRM_PLANE_CRTC_H,
130 WDRM_PLANE_FB_ID,
131 WDRM_PLANE_CRTC_ID,
132 WDRM_PLANE__COUNT
133};
134
135/**
136 * Possible values for the WDRM_PLANE_TYPE property.
137 */
138enum wdrm_plane_type {
139 WDRM_PLANE_TYPE_PRIMARY = 0,
140 WDRM_PLANE_TYPE_CURSOR,
141 WDRM_PLANE_TYPE_OVERLAY,
142 WDRM_PLANE_TYPE__COUNT
143};
144
145static struct drm_property_enum_info plane_type_enums[] = {
146 [WDRM_PLANE_TYPE_PRIMARY] = {
147 .name = "Primary",
148 },
149 [WDRM_PLANE_TYPE_OVERLAY] = {
150 .name = "Overlay",
151 },
152 [WDRM_PLANE_TYPE_CURSOR] = {
153 .name = "Cursor",
154 },
155};
156
157static const struct drm_property_info plane_props[] = {
158 [WDRM_PLANE_TYPE] = {
159 .name = "type",
160 .enum_values = plane_type_enums,
161 .num_enum_values = WDRM_PLANE_TYPE__COUNT,
162 },
163 [WDRM_PLANE_SRC_X] = { .name = "SRC_X", },
164 [WDRM_PLANE_SRC_Y] = { .name = "SRC_Y", },
165 [WDRM_PLANE_SRC_W] = { .name = "SRC_W", },
166 [WDRM_PLANE_SRC_H] = { .name = "SRC_H", },
167 [WDRM_PLANE_CRTC_X] = { .name = "CRTC_X", },
168 [WDRM_PLANE_CRTC_Y] = { .name = "CRTC_Y", },
169 [WDRM_PLANE_CRTC_W] = { .name = "CRTC_W", },
170 [WDRM_PLANE_CRTC_H] = { .name = "CRTC_H", },
171 [WDRM_PLANE_FB_ID] = { .name = "FB_ID", },
172 [WDRM_PLANE_CRTC_ID] = { .name = "CRTC_ID", },
173};
174
175/**
176 * List of properties attached to a DRM connector
177 */
178enum wdrm_connector_property {
179 WDRM_CONNECTOR_EDID = 0,
180 WDRM_CONNECTOR_DPMS,
181 WDRM_CONNECTOR_CRTC_ID,
182 WDRM_CONNECTOR__COUNT
183};
184
185static const struct drm_property_info connector_props[] = {
186 [WDRM_CONNECTOR_EDID] = { .name = "EDID" },
187 [WDRM_CONNECTOR_DPMS] = { .name = "DPMS" },
188 [WDRM_CONNECTOR_CRTC_ID] = { .name = "CRTC_ID", },
189};
190
191/**
Pekka Paalanencd011a62016-11-15 22:07:49 +0000192 * List of properties attached to DRM CRTCs
193 */
194enum wdrm_crtc_property {
195 WDRM_CRTC_MODE_ID = 0,
196 WDRM_CRTC_ACTIVE,
197 WDRM_CRTC__COUNT
198};
199
Daniel Stone598ee9d2016-11-16 11:55:20 +0000200static const struct drm_property_info crtc_props[] = {
201 [WDRM_CRTC_MODE_ID] = { .name = "MODE_ID", },
202 [WDRM_CRTC_ACTIVE] = { .name = "ACTIVE", },
203};
204
Pekka Paalanencd011a62016-11-15 22:07:49 +0000205/**
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000206 * Mode for drm_output_state_duplicate.
207 */
208enum drm_output_state_duplicate_mode {
209 DRM_OUTPUT_STATE_CLEAR_PLANES, /**< reset all planes to off */
210 DRM_OUTPUT_STATE_PRESERVE_PLANES, /**< preserve plane state */
211};
212
213/**
214 * Mode for drm_pending_state_apply and co.
215 */
216enum drm_state_apply_mode {
217 DRM_STATE_APPLY_SYNC, /**< state fully processed */
218 DRM_STATE_APPLY_ASYNC, /**< state pending event delivery */
219};
220
Giulio Camuffo954f1832014-10-11 18:27:30 +0300221struct drm_backend {
222 struct weston_backend base;
223 struct weston_compositor *compositor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400224
225 struct udev *udev;
226 struct wl_event_source *drm_source;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400227
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100228 struct udev_monitor *udev_monitor;
229 struct wl_event_source *udev_drm_source;
230
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100231 struct {
David Herrmannd7488c22012-03-11 20:05:21 +0100232 int id;
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100233 int fd;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300234 char *filename;
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100235 } drm;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200236 struct gbm_device *gbm;
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700237 struct wl_listener session_listener;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100238 uint32_t gbm_format;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200239
Rob Clark4339add2012-08-09 14:18:28 -0500240 /* we need these parameters in order to not fail drmModeAddFB2()
241 * due to out of bounds dimensions, and then mistakenly set
242 * sprites_are_broken:
243 */
Daniel Stonef214fdc2016-11-14 17:43:57 +0000244 int min_width, max_width;
245 int min_height, max_height;
Rob Clark4339add2012-08-09 14:18:28 -0500246
Daniel Stone085d2b92015-05-21 00:00:57 +0100247 struct wl_list plane_list;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500248 int sprites_are_broken;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +0200249 int sprites_hidden;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500250
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000251 void *repaint_data;
252
Daniel Stone6020f472018-02-05 15:46:20 +0000253 bool state_invalid;
254
Pekka Paalaneneacec812017-09-12 13:43:51 +0300255 /* CRTC IDs not used by any enabled output. */
Daniel Stone087ddf02017-02-14 17:51:30 +0000256 struct wl_array unused_crtcs;
257
Rob Clarkab5b1e32012-08-09 13:24:45 -0500258 int cursors_are_broken;
259
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100260 bool universal_planes;
Pekka Paalanencd011a62016-11-15 22:07:49 +0000261 bool atomic_modeset;
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100262
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200263 int use_pixman;
264
Rob Bradfordd355b802013-05-31 18:09:55 +0100265 struct udev_input input;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -0300266
Daniel Stone70d337d2015-06-16 18:42:23 +0100267 int32_t cursor_width;
268 int32_t cursor_height;
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +0000269
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000270 uint32_t pageflip_timeout;
Daniel Stoneb57c6a02017-10-05 16:27:21 +0100271
272 bool shutting_down;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400273};
274
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400275struct drm_mode {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500276 struct weston_mode base;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400277 drmModeModeInfo mode_info;
Daniel Stoned5526cb2016-11-16 10:54:10 +0000278 uint32_t blob_id;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400279};
280
Daniel Stonefc175a72017-04-04 17:54:22 +0100281enum 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 Stonee4256832017-04-04 17:54:27 +0100286 BUFFER_CURSOR, /**< internal cursor buffer */
Daniel Stonefc175a72017-04-04 17:54:22 +0100287};
288
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300289struct drm_fb {
Daniel Stonefc175a72017-04-04 17:54:22 +0100290 enum drm_fb_type type;
291
Daniel Stone6e7a9612017-04-04 17:54:26 +0100292 int refcnt;
293
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200294 uint32_t fb_id, stride, handle, size;
Daniel Stone0b70fa42017-04-04 17:54:23 +0100295 const struct pixel_format_info *format;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000296 int width, height;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200297 int fd;
Pekka Paalanende685b82012-12-04 15:58:12 +0200298 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200299
300 /* Used by gbm fbs */
301 struct gbm_bo *bo;
Daniel Stone05a5ac22017-04-04 17:54:25 +0100302 struct gbm_surface *gbm_surface;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200303
304 /* Used by dumb fbs */
305 void *map;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300306};
307
Richard Hughes2b2092a2013-04-24 14:58:02 +0100308struct 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 Stone08d4edf2017-04-04 17:54:34 +0100315/**
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000316 * 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 */
321struct drm_pending_state {
322 struct drm_backend *backend;
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000323 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 */
336struct drm_output_state {
337 struct drm_pending_state *pending_state;
338 struct drm_output *output;
339 struct wl_list link;
Daniel Stonea08512f2016-11-08 17:46:10 +0000340 enum dpms_enum dpms;
Daniel Stonebc15f682016-11-14 16:57:01 +0000341 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 */
351struct 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 Stoneeedf84c2017-02-10 18:06:04 +0000366};
367
368/**
Daniel Stone08d4edf2017-04-04 17:54:34 +0100369 * 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 */
383struct drm_plane {
Daniel Stone08d4edf2017-04-04 17:54:34 +0100384 struct weston_plane base;
385
Daniel Stone08d4edf2017-04-04 17:54:34 +0100386 struct drm_backend *backend;
387
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100388 enum wdrm_plane_type type;
389
Daniel Stone08d4edf2017-04-04 17:54:34 +0100390 uint32_t possible_crtcs;
391 uint32_t plane_id;
392 uint32_t count_formats;
393
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100394 struct drm_property_info props[WDRM_PLANE__COUNT];
395
Daniel Stonebc15f682016-11-14 16:57:01 +0000396 /* The last state submitted to the kernel for this plane. */
397 struct drm_plane_state *state_cur;
Daniel Stone08d4edf2017-04-04 17:54:34 +0100398
Daniel Stonebc15f682016-11-14 16:57:01 +0000399 struct wl_list link;
Daniel Stone08d4edf2017-04-04 17:54:34 +0100400
401 uint32_t formats[];
402};
403
Pekka Paalanenc112f002017-08-28 16:27:20 +0300404struct drm_head {
405 struct weston_head base;
406 struct drm_backend *backend;
407
Armin Krezović08368132016-09-30 14:11:05 +0200408 drmModeConnector *connector;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400409 uint32_t connector_id;
Richard Hughes2b2092a2013-04-24 14:58:02 +0100410 struct drm_edid edid;
Daniel Stone02cf4662017-03-03 16:19:39 +0000411
412 /* Holds the properties for the connector */
413 struct drm_property_info props_conn[WDRM_CONNECTOR__COUNT];
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +0300414
415 struct backlight *backlight;
Pekka Paalanen13d233e2017-09-11 14:06:11 +0300416
417 drmModeModeInfo inherited_mode; /**< Original mode on the connector */
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +0300418};
419
420struct drm_output {
421 struct weston_output base;
422
423 uint32_t crtc_id; /* object ID to pass to DRM functions */
424 int pipe; /* index of CRTC in resource array / bitmasks */
425
Pekka Paalanencd011a62016-11-15 22:07:49 +0000426 /* Holds the properties for the CRTC */
427 struct drm_property_info props_crtc[WDRM_CRTC__COUNT];
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200428
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300429 int vblank_pending;
430 int page_flip_pending;
Daniel Stone598ee9d2016-11-16 11:55:20 +0000431 int atomic_complete_pending;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800432 int destroy_pending;
Armin Krezović08368132016-09-30 14:11:05 +0200433 int disable_pending;
Daniel Stonea08512f2016-11-08 17:46:10 +0000434 int dpms_off_pending;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300435
Daniel Stonee4256832017-04-04 17:54:27 +0100436 struct drm_fb *gbm_cursor_fb[2];
Daniel Stone2ba17f42015-05-19 20:02:41 +0100437 struct drm_plane *cursor_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500438 struct weston_view *cursor_view;
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400439 int current_cursor;
Daniel Stone5bb8f582017-04-04 17:54:28 +0100440
441 struct gbm_surface *gbm_surface;
442 uint32_t gbm_format;
443
Daniel Stonee2e80132018-01-16 15:37:33 +0000444 /* Plane being displayed directly on the CRTC */
445 struct drm_plane *scanout_plane;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200446
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000447 /* The last state submitted to the kernel for this CRTC. */
448 struct drm_output_state *state_cur;
449 /* The previously-submitted state, where the hardware has not
450 * yet acknowledged completion of state_cur. */
451 struct drm_output_state *state_last;
452
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200453 struct drm_fb *dumb[2];
454 pixman_image_t *image[2];
455 int current_image;
456 pixman_region32_t previous_damage;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300457
458 struct vaapi_recorder *recorder;
459 struct wl_listener recorder_frame_listener;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000460
461 struct wl_event_source *pageflip_timer;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400462};
463
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300464static struct gl_renderer_interface *gl_renderer;
465
Kristian Høgsberg98cfea62013-02-18 16:15:53 -0500466static const char default_seat[] = "seat0";
Pekka Paalanen33156972012-08-03 13:30:30 -0400467
Daniel Stone087ddf02017-02-14 17:51:30 +0000468static void
469wl_array_remove_uint32(struct wl_array *array, uint32_t elm)
470{
471 uint32_t *pos, *end;
472
473 end = (uint32_t *) ((char *) array->data + array->size);
474
475 wl_array_for_each(pos, array) {
476 if (*pos != elm)
477 continue;
478
479 array->size -= sizeof(*pos);
480 if (pos + 1 == end)
481 break;
482
483 memmove(pos, pos + 1, (char *) end - (char *) (pos + 1));
484 break;
485 }
486}
487
Pekka Paalanenc112f002017-08-28 16:27:20 +0300488static inline struct drm_head *
489to_drm_head(struct weston_head *base)
490{
491 return container_of(base, struct drm_head, base);
492}
493
Armin Krezović545dba62016-08-05 15:54:18 +0200494static inline struct drm_output *
495to_drm_output(struct weston_output *base)
496{
497 return container_of(base, struct drm_output, base);
498}
499
500static inline struct drm_backend *
501to_drm_backend(struct weston_compositor *base)
502{
503 return container_of(base->backend, struct drm_backend, base);
504}
505
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000506static int
507pageflip_timeout(void *data) {
508 /*
509 * Our timer just went off, that means we're not receiving drm
510 * page flip events anymore for that output. Let's gracefully exit
511 * weston with a return value so devs can debug what's going on.
512 */
513 struct drm_output *output = data;
514 struct weston_compositor *compositor = output->base.compositor;
515
516 weston_log("Pageflip timeout reached on output %s, your "
517 "driver is probably buggy! Exiting.\n",
518 output->base.name);
519 weston_compositor_exit_with_code(compositor, EXIT_FAILURE);
520
521 return 0;
522}
523
524/* Creates the pageflip timer. Note that it isn't armed by default */
525static int
526drm_output_pageflip_timer_create(struct drm_output *output)
527{
528 struct wl_event_loop *loop = NULL;
529 struct weston_compositor *ec = output->base.compositor;
530
531 loop = wl_display_get_event_loop(ec->wl_display);
532 assert(loop);
533 output->pageflip_timer = wl_event_loop_add_timer(loop,
534 pageflip_timeout,
535 output);
536
537 if (output->pageflip_timer == NULL) {
538 weston_log("creating drm pageflip timer failed: %m\n");
539 return -1;
540 }
541
542 return 0;
543}
544
Daniel Stonecb04cc42016-11-16 11:51:27 +0000545static inline struct drm_mode *
546to_drm_mode(struct weston_mode *base)
547{
548 return container_of(base, struct drm_mode, base);
549}
550
Daniel Stone02cf4662017-03-03 16:19:39 +0000551/**
552 * Get the current value of a KMS property
553 *
554 * Given a drmModeObjectGetProperties return, as well as the drm_property_info
555 * for the target property, return the current value of that property,
556 * with an optional default. If the property is a KMS enum type, the return
557 * value will be translated into the appropriate internal enum.
558 *
559 * If the property is not present, the default value will be returned.
560 *
561 * @param info Internal structure for property to look up
562 * @param props Raw KMS properties for the target object
563 * @param def Value to return if property is not found
564 */
565static uint64_t
566drm_property_get_value(struct drm_property_info *info,
567 drmModeObjectPropertiesPtr props,
568 uint64_t def)
569{
570 unsigned int i;
571
572 if (info->prop_id == 0)
573 return def;
574
575 for (i = 0; i < props->count_props; i++) {
576 unsigned int j;
577
578 if (props->props[i] != info->prop_id)
579 continue;
580
581 /* Simple (non-enum) types can return the value directly */
582 if (info->num_enum_values == 0)
583 return props->prop_values[i];
584
585 /* Map from raw value to enum value */
586 for (j = 0; j < info->num_enum_values; j++) {
587 if (!info->enum_values[j].valid)
588 continue;
589 if (info->enum_values[j].value != props->prop_values[i])
590 continue;
591
592 return j;
593 }
594
595 /* We don't have a mapping for this enum; return default. */
596 break;
597 }
598
599 return def;
600}
601
602/**
603 * Cache DRM property values
604 *
605 * Update a per-object array of drm_property_info structures, given the
606 * DRM properties of the object.
607 *
608 * Call this every time an object newly appears (note that only connectors
609 * can be hotplugged), the first time it is seen, or when its status changes
610 * in a way which invalidates the potential property values (currently, the
611 * only case for this is connector hotplug).
612 *
613 * This updates the property IDs and enum values within the drm_property_info
614 * array.
615 *
616 * DRM property enum values are dynamic at runtime; the user must query the
617 * property to find out the desired runtime value for a requested string
618 * name. Using the 'type' field on planes as an example, there is no single
619 * hardcoded constant for primary plane types; instead, the property must be
620 * queried at runtime to find the value associated with the string "Primary".
621 *
622 * This helper queries and caches the enum values, to allow us to use a set
623 * of compile-time-constant enums portably across various implementations.
624 * The values given in enum_names are searched for, and stored in the
625 * same-indexed field of the map array.
626 *
627 * @param b DRM backend object
628 * @param src DRM property info array to source from
629 * @param info DRM property info array to copy into
630 * @param num_infos Number of entries in the source array
631 * @param props DRM object properties for the object
632 */
633static void
634drm_property_info_populate(struct drm_backend *b,
635 const struct drm_property_info *src,
636 struct drm_property_info *info,
637 unsigned int num_infos,
638 drmModeObjectProperties *props)
639{
640 drmModePropertyRes *prop;
641 unsigned i, j;
642
643 for (i = 0; i < num_infos; i++) {
644 unsigned int j;
645
646 info[i].name = src[i].name;
647 info[i].prop_id = 0;
648 info[i].num_enum_values = src[i].num_enum_values;
649
650 if (src[i].num_enum_values == 0)
651 continue;
652
653 info[i].enum_values =
654 malloc(src[i].num_enum_values *
655 sizeof(*info[i].enum_values));
656 assert(info[i].enum_values);
657 for (j = 0; j < info[i].num_enum_values; j++) {
658 info[i].enum_values[j].name = src[i].enum_values[j].name;
659 info[i].enum_values[j].valid = false;
660 }
661 }
662
663 for (i = 0; i < props->count_props; i++) {
664 unsigned int k;
665
666 prop = drmModeGetProperty(b->drm.fd, props->props[i]);
667 if (!prop)
668 continue;
669
670 for (j = 0; j < num_infos; j++) {
671 if (!strcmp(prop->name, info[j].name))
672 break;
673 }
674
675 /* We don't know/care about this property. */
676 if (j == num_infos) {
677#ifdef DEBUG
678 weston_log("DRM debug: unrecognized property %u '%s'\n",
679 prop->prop_id, prop->name);
680#endif
681 drmModeFreeProperty(prop);
682 continue;
683 }
684
685 if (info[j].num_enum_values == 0 &&
686 (prop->flags & DRM_MODE_PROP_ENUM)) {
687 weston_log("DRM: expected property %s to not be an"
688 " enum, but it is; ignoring\n", prop->name);
689 drmModeFreeProperty(prop);
690 continue;
691 }
692
693 info[j].prop_id = props->props[i];
694
695 if (info[j].num_enum_values == 0) {
696 drmModeFreeProperty(prop);
697 continue;
698 }
699
700 if (!(prop->flags & DRM_MODE_PROP_ENUM)) {
701 weston_log("DRM: expected property %s to be an enum,"
702 " but it is not; ignoring\n", prop->name);
703 drmModeFreeProperty(prop);
704 info[j].prop_id = 0;
705 continue;
706 }
707
708 for (k = 0; k < info[j].num_enum_values; k++) {
709 int l;
710
711 for (l = 0; l < prop->count_enums; l++) {
712 if (!strcmp(prop->enums[l].name,
713 info[j].enum_values[k].name))
714 break;
715 }
716
717 if (l == prop->count_enums)
718 continue;
719
720 info[j].enum_values[k].valid = true;
721 info[j].enum_values[k].value = prop->enums[l].value;
722 }
723
724 drmModeFreeProperty(prop);
725 }
726
727#ifdef DEBUG
728 for (i = 0; i < num_infos; i++) {
729 if (info[i].prop_id == 0)
730 weston_log("DRM warning: property '%s' missing\n",
731 info[i].name);
732 }
733#endif
734}
735
736/**
737 * Free DRM property information
738 *
Pekka Paalanen46e4f972017-09-07 15:32:01 +0300739 * Frees all memory associated with a DRM property info array and zeroes
740 * it out, leaving it usable for a further drm_property_info_update() or
741 * drm_property_info_free().
Daniel Stone02cf4662017-03-03 16:19:39 +0000742 *
743 * @param info DRM property info array
744 * @param num_props Number of entries in array to free
745 */
746static void
747drm_property_info_free(struct drm_property_info *info, int num_props)
748{
749 int i;
750
751 for (i = 0; i < num_props; i++)
752 free(info[i].enum_values);
Pekka Paalanen46e4f972017-09-07 15:32:01 +0300753
754 memset(info, 0, sizeof(*info) * num_props);
Daniel Stone02cf4662017-03-03 16:19:39 +0000755}
756
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400757static void
Daniel Stone2ba17f42015-05-19 20:02:41 +0100758drm_output_set_cursor(struct drm_output_state *output_state);
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400759
Mario Kleinerf507ec32015-06-21 21:25:14 +0200760static void
761drm_output_update_msc(struct drm_output *output, unsigned int seq);
762
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000763static void
764drm_output_destroy(struct weston_output *output_base);
765
Daniel Stone5ff289a2017-10-07 12:59:02 +0100766/**
767 * Returns true if the plane can be used on the given output for its current
768 * repaint cycle.
769 */
770static bool
771drm_plane_is_available(struct drm_plane *plane, struct drm_output *output)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500772{
Daniel Stone5ff289a2017-10-07 12:59:02 +0100773 assert(plane->state_cur);
774
775 /* The plane still has a request not yet completed by the kernel. */
776 if (!plane->state_cur->complete)
777 return false;
778
779 /* The plane is still active on another output. */
780 if (plane->state_cur->output && plane->state_cur->output != output)
781 return false;
782
783 /* Check whether the plane can be used with this CRTC; possible_crtcs
784 * is a bitmask of CRTC indices (pipe), rather than CRTC object ID. */
Daniel Stone08d4edf2017-04-04 17:54:34 +0100785 return !!(plane->possible_crtcs & (1 << output->pipe));
Jesse Barnes58ef3792012-02-23 09:45:49 -0500786}
787
Daniel Stone72c0e1b2017-02-09 13:49:15 +0000788static struct drm_output *
789drm_output_find_by_crtc(struct drm_backend *b, uint32_t crtc_id)
790{
791 struct drm_output *output;
792
793 wl_list_for_each(output, &b->compositor->output_list, base.link) {
794 if (output->crtc_id == crtc_id)
795 return output;
796 }
797
Daniel Stone72c0e1b2017-02-09 13:49:15 +0000798 return NULL;
799}
800
Pekka Paalanen54cc47c2017-08-31 11:58:41 +0300801static struct drm_head *
802drm_head_find_by_connector(struct drm_backend *backend, uint32_t connector_id)
803{
804 struct weston_head *base;
805 struct drm_head *head;
806
807 wl_list_for_each(base,
808 &backend->compositor->head_list, compositor_link) {
809 head = to_drm_head(base);
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +0300810 if (head->connector_id == connector_id)
Pekka Paalanen54cc47c2017-08-31 11:58:41 +0300811 return head;
812 }
813
814 return NULL;
815}
816
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300817static void
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100818drm_fb_destroy(struct drm_fb *fb)
819{
820 if (fb->fb_id != 0)
821 drmModeRmFB(fb->fd, fb->fb_id);
822 weston_buffer_reference(&fb->buffer_ref, NULL);
823 free(fb);
824}
825
826static void
827drm_fb_destroy_dumb(struct drm_fb *fb)
828{
829 struct drm_mode_destroy_dumb destroy_arg;
830
831 assert(fb->type == BUFFER_PIXMAN_DUMB);
832
833 if (fb->map && fb->size > 0)
834 munmap(fb->map, fb->size);
835
836 memset(&destroy_arg, 0, sizeof(destroy_arg));
837 destroy_arg.handle = fb->handle;
838 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
839
840 drm_fb_destroy(fb);
841}
842
843static void
844drm_fb_destroy_gbm(struct gbm_bo *bo, void *data)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300845{
846 struct drm_fb *fb = data;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300847
Daniel Stonee4256832017-04-04 17:54:27 +0100848 assert(fb->type == BUFFER_GBM_SURFACE || fb->type == BUFFER_CLIENT ||
849 fb->type == BUFFER_CURSOR);
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100850 drm_fb_destroy(fb);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300851}
852
853static struct drm_fb *
Daniel Stonef214fdc2016-11-14 17:43:57 +0000854drm_fb_create_dumb(struct drm_backend *b, int width, int height,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300855 uint32_t format)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200856{
857 struct drm_fb *fb;
858 int ret;
859
860 struct drm_mode_create_dumb create_arg;
861 struct drm_mode_destroy_dumb destroy_arg;
862 struct drm_mode_map_dumb map_arg;
Daniel Stone1de42522016-11-17 18:17:16 +0000863 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200864
Peter Huttererf3d62272013-08-08 11:57:05 +1000865 fb = zalloc(sizeof *fb);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200866 if (!fb)
867 return NULL;
868
Daniel Stone6e7a9612017-04-04 17:54:26 +0100869 fb->refcnt = 1;
870
Daniel Stone0b70fa42017-04-04 17:54:23 +0100871 fb->format = pixel_format_get_info(format);
872 if (!fb->format) {
873 weston_log("failed to look up format 0x%lx\n",
874 (unsigned long) format);
875 goto err_fb;
876 }
877
878 if (!fb->format->depth || !fb->format->bpp) {
879 weston_log("format 0x%lx is not compatible with dumb buffers\n",
880 (unsigned long) format);
881 goto err_fb;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300882 }
883
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700884 memset(&create_arg, 0, sizeof create_arg);
Daniel Stone0b70fa42017-04-04 17:54:23 +0100885 create_arg.bpp = fb->format->bpp;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200886 create_arg.width = width;
887 create_arg.height = height;
888
Giulio Camuffo954f1832014-10-11 18:27:30 +0300889 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200890 if (ret)
891 goto err_fb;
892
Daniel Stonefc175a72017-04-04 17:54:22 +0100893 fb->type = BUFFER_PIXMAN_DUMB;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200894 fb->handle = create_arg.handle;
895 fb->stride = create_arg.pitch;
896 fb->size = create_arg.size;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000897 fb->width = width;
898 fb->height = height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300899 fb->fd = b->drm.fd;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200900
Daniel Stone1de42522016-11-17 18:17:16 +0000901 handles[0] = fb->handle;
902 pitches[0] = fb->stride;
903 offsets[0] = 0;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300904
Daniel Stone1de42522016-11-17 18:17:16 +0000905 ret = drmModeAddFB2(b->drm.fd, width, height, fb->format->format,
906 handles, pitches, offsets, &fb->fb_id, 0);
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300907 if (ret) {
Daniel Stone0b70fa42017-04-04 17:54:23 +0100908 ret = drmModeAddFB(b->drm.fd, width, height,
909 fb->format->depth, fb->format->bpp,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300910 fb->stride, fb->handle, &fb->fb_id);
911 }
912
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200913 if (ret)
914 goto err_bo;
915
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700916 memset(&map_arg, 0, sizeof map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200917 map_arg.handle = fb->handle;
Chris Michaeleb2074a2013-05-01 21:26:02 -0400918 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200919 if (ret)
920 goto err_add_fb;
921
Chris Michael4a7ce1f2015-11-10 10:40:37 -0500922 fb->map = mmap(NULL, fb->size, PROT_WRITE,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300923 MAP_SHARED, b->drm.fd, map_arg.offset);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200924 if (fb->map == MAP_FAILED)
925 goto err_add_fb;
926
927 return fb;
928
929err_add_fb:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300930 drmModeRmFB(b->drm.fd, fb->fb_id);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200931err_bo:
932 memset(&destroy_arg, 0, sizeof(destroy_arg));
933 destroy_arg.handle = create_arg.handle;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300934 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200935err_fb:
936 free(fb);
937 return NULL;
938}
939
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200940static struct drm_fb *
Daniel Stone6e7a9612017-04-04 17:54:26 +0100941drm_fb_ref(struct drm_fb *fb)
942{
943 fb->refcnt++;
944 return fb;
945}
946
947static struct drm_fb *
Daniel Stonefc175a72017-04-04 17:54:22 +0100948drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend,
949 uint32_t format, enum drm_fb_type type)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300950{
951 struct drm_fb *fb = gbm_bo_get_user_data(bo);
Derek Foreman482ffdf2016-07-08 12:50:57 -0500952 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300953 int ret;
954
Daniel Stonefc175a72017-04-04 17:54:22 +0100955 if (fb) {
956 assert(fb->type == type);
Daniel Stone6e7a9612017-04-04 17:54:26 +0100957 return drm_fb_ref(fb);
Daniel Stonefc175a72017-04-04 17:54:22 +0100958 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300959
Daniel Stone1de42522016-11-17 18:17:16 +0000960 assert(format != 0);
961
Bryce Harringtonde16d892014-11-20 22:21:57 -0800962 fb = zalloc(sizeof *fb);
963 if (fb == NULL)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200964 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300965
Daniel Stonefc175a72017-04-04 17:54:22 +0100966 fb->type = type;
Daniel Stone6e7a9612017-04-04 17:54:26 +0100967 fb->refcnt = 1;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300968 fb->bo = bo;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300969
Daniel Stonec8c917c2016-11-14 17:45:58 +0000970 fb->width = gbm_bo_get_width(bo);
971 fb->height = gbm_bo_get_height(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200972 fb->stride = gbm_bo_get_stride(bo);
973 fb->handle = gbm_bo_get_handle(bo).u32;
Daniel Stone0b70fa42017-04-04 17:54:23 +0100974 fb->format = pixel_format_get_info(format);
Daniel Stonec8c917c2016-11-14 17:45:58 +0000975 fb->size = fb->stride * fb->height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300976 fb->fd = backend->drm.fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300977
Daniel Stone0b70fa42017-04-04 17:54:23 +0100978 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 Stonec8c917c2016-11-14 17:45:58 +0000984 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 Oliveira8d360b42012-11-09 14:19:05 +0200988 weston_log("bo geometry out of bounds\n");
989 goto err_free;
990 }
991
Daniel Stone1de42522016-11-17 18:17:16 +0000992 handles[0] = fb->handle;
993 pitches[0] = fb->stride;
994 offsets[0] = 0;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200995
Daniel Stone1de42522016-11-17 18:17:16 +0000996 ret = drmModeAddFB2(backend->drm.fd, fb->width, fb->height,
997 fb->format->format, handles, pitches, offsets,
998 &fb->fb_id, 0);
Daniel Stone0b70fa42017-04-04 17:54:23 +0100999 if (ret && fb->format->depth && fb->format->bpp)
Daniel Stonec8c917c2016-11-14 17:45:58 +00001000 ret = drmModeAddFB(backend->drm.fd, fb->width, fb->height,
Daniel Stone0b70fa42017-04-04 17:54:23 +01001001 fb->format->depth, fb->format->bpp,
1002 fb->stride, fb->handle, &fb->fb_id);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001003
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001004 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +02001005 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001006 goto err_free;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001007 }
1008
Tomohito Esaki576f42e2017-04-04 17:54:24 +01001009 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_gbm);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001010
1011 return fb;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001012
1013err_free:
1014 free(fb);
1015 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001016}
1017
1018static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001019drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001020{
Pekka Paalanende685b82012-12-04 15:58:12 +02001021 assert(fb->buffer_ref.buffer == NULL);
Daniel Stonefc175a72017-04-04 17:54:22 +01001022 assert(fb->type == BUFFER_CLIENT);
Pekka Paalanende685b82012-12-04 15:58:12 +02001023 weston_buffer_reference(&fb->buffer_ref, buffer);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001024}
1025
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02001026static void
Daniel Stone05a5ac22017-04-04 17:54:25 +01001027drm_fb_unref(struct drm_fb *fb)
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02001028{
1029 if (!fb)
1030 return;
1031
Daniel Stone6e7a9612017-04-04 17:54:26 +01001032 assert(fb->refcnt > 0);
1033 if (--fb->refcnt > 0)
1034 return;
1035
Daniel Stonefc175a72017-04-04 17:54:22 +01001036 switch (fb->type) {
1037 case BUFFER_PIXMAN_DUMB:
Daniel Stone6e7a9612017-04-04 17:54:26 +01001038 drm_fb_destroy_dumb(fb);
Daniel Stonefc175a72017-04-04 17:54:22 +01001039 break;
Daniel Stonee4256832017-04-04 17:54:27 +01001040 case BUFFER_CURSOR:
Daniel Stonefc175a72017-04-04 17:54:22 +01001041 case BUFFER_CLIENT:
1042 gbm_bo_destroy(fb->bo);
1043 break;
1044 case BUFFER_GBM_SURFACE:
Daniel Stone05a5ac22017-04-04 17:54:25 +01001045 gbm_surface_release_buffer(fb->gbm_surface, fb->bo);
Daniel Stonefc175a72017-04-04 17:54:22 +01001046 break;
1047 default:
1048 assert(NULL);
1049 break;
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02001050 }
1051}
1052
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001053/**
Daniel Stonebc15f682016-11-14 16:57:01 +00001054 * Allocate a new, empty, plane state.
1055 */
1056static struct drm_plane_state *
1057drm_plane_state_alloc(struct drm_output_state *state_output,
1058 struct drm_plane *plane)
1059{
1060 struct drm_plane_state *state = zalloc(sizeof(*state));
1061
1062 assert(state);
1063 state->output_state = state_output;
1064 state->plane = plane;
1065
1066 /* Here we only add the plane state to the desired link, and not
1067 * set the member. Having an output pointer set means that the
1068 * plane will be displayed on the output; this won't be the case
1069 * when we go to disable a plane. In this case, it must be part of
1070 * the commit (and thus the output state), but the member must be
1071 * NULL, as it will not be on any output when the state takes
1072 * effect.
1073 */
1074 if (state_output)
1075 wl_list_insert(&state_output->plane_list, &state->link);
1076 else
1077 wl_list_init(&state->link);
1078
1079 return state;
1080}
1081
1082/**
1083 * Free an existing plane state. As a special case, the state will not
1084 * normally be freed if it is the current state; see drm_plane_set_state.
1085 */
1086static void
1087drm_plane_state_free(struct drm_plane_state *state, bool force)
1088{
1089 if (!state)
1090 return;
1091
1092 wl_list_remove(&state->link);
1093 wl_list_init(&state->link);
1094 state->output_state = NULL;
1095
1096 if (force || state != state->plane->state_cur) {
1097 drm_fb_unref(state->fb);
1098 free(state);
1099 }
1100}
1101
1102/**
1103 * Duplicate an existing plane state into a new plane state, storing it within
1104 * the given output state. If the output state already contains a plane state
1105 * for the drm_plane referenced by 'src', that plane state is freed first.
1106 */
1107static struct drm_plane_state *
1108drm_plane_state_duplicate(struct drm_output_state *state_output,
1109 struct drm_plane_state *src)
1110{
1111 struct drm_plane_state *dst = malloc(sizeof(*dst));
1112 struct drm_plane_state *old, *tmp;
1113
1114 assert(src);
1115 assert(dst);
1116 *dst = *src;
1117 wl_list_init(&dst->link);
1118
1119 wl_list_for_each_safe(old, tmp, &state_output->plane_list, link) {
1120 /* Duplicating a plane state into the same output state, so
1121 * it can replace itself with an identical copy of itself,
1122 * makes no sense. */
1123 assert(old != src);
1124 if (old->plane == dst->plane)
1125 drm_plane_state_free(old, false);
1126 }
1127
1128 wl_list_insert(&state_output->plane_list, &dst->link);
1129 if (src->fb)
1130 dst->fb = drm_fb_ref(src->fb);
1131 dst->output_state = state_output;
1132 dst->complete = false;
1133
1134 return dst;
1135}
1136
1137/**
1138 * Remove a plane state from an output state; if the plane was previously
1139 * enabled, then replace it with a disabling state. This ensures that the
1140 * output state was untouched from it was before the plane state was
1141 * modified by the caller of this function.
1142 *
1143 * This is required as drm_output_state_get_plane may either allocate a
1144 * new plane state, in which case this function will just perform a matching
1145 * drm_plane_state_free, or it may instead repurpose an existing disabling
1146 * state (if the plane was previously active), in which case this function
1147 * will reset it.
1148 */
1149static void
1150drm_plane_state_put_back(struct drm_plane_state *state)
1151{
1152 struct drm_output_state *state_output;
1153 struct drm_plane *plane;
1154
1155 if (!state)
1156 return;
1157
1158 state_output = state->output_state;
1159 plane = state->plane;
1160 drm_plane_state_free(state, false);
1161
1162 /* Plane was previously disabled; no need to keep this temporary
1163 * state around. */
1164 if (!plane->state_cur->fb)
1165 return;
1166
1167 (void) drm_plane_state_alloc(state_output, plane);
1168}
1169
1170/**
Daniel Stone2ba17f42015-05-19 20:02:41 +01001171 * Return a plane state from a drm_output_state.
1172 */
1173static struct drm_plane_state *
1174drm_output_state_get_existing_plane(struct drm_output_state *state_output,
1175 struct drm_plane *plane)
1176{
1177 struct drm_plane_state *ps;
1178
1179 wl_list_for_each(ps, &state_output->plane_list, link) {
1180 if (ps->plane == plane)
1181 return ps;
1182 }
1183
1184 return NULL;
1185}
1186
1187/**
Daniel Stonebc15f682016-11-14 16:57:01 +00001188 * Return a plane state from a drm_output_state, either existing or
1189 * freshly allocated.
1190 */
1191static struct drm_plane_state *
1192drm_output_state_get_plane(struct drm_output_state *state_output,
1193 struct drm_plane *plane)
1194{
1195 struct drm_plane_state *ps;
1196
Daniel Stone2ba17f42015-05-19 20:02:41 +01001197 ps = drm_output_state_get_existing_plane(state_output, plane);
1198 if (ps)
1199 return ps;
Daniel Stonebc15f682016-11-14 16:57:01 +00001200
1201 return drm_plane_state_alloc(state_output, plane);
1202}
1203
1204/**
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001205 * Allocate a new, empty drm_output_state. This should not generally be used
1206 * in the repaint cycle; see drm_output_state_duplicate.
1207 */
1208static struct drm_output_state *
1209drm_output_state_alloc(struct drm_output *output,
1210 struct drm_pending_state *pending_state)
Daniel Stone90648872016-10-21 18:08:37 +01001211{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001212 struct drm_output_state *state = zalloc(sizeof(*state));
1213
1214 assert(state);
1215 state->output = output;
Daniel Stonea08512f2016-11-08 17:46:10 +00001216 state->dpms = WESTON_DPMS_OFF;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001217 state->pending_state = pending_state;
1218 if (pending_state)
1219 wl_list_insert(&pending_state->output_list, &state->link);
1220 else
1221 wl_list_init(&state->link);
1222
Daniel Stonebc15f682016-11-14 16:57:01 +00001223 wl_list_init(&state->plane_list);
1224
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001225 return state;
1226}
1227
1228/**
1229 * Duplicate an existing drm_output_state into a new one. This is generally
1230 * used during the repaint cycle, to capture the existing state of an output
1231 * and modify it to create a new state to be used.
1232 *
1233 * The mode determines whether the output will be reset to an a blank state,
1234 * or an exact mirror of the current state.
1235 */
1236static struct drm_output_state *
1237drm_output_state_duplicate(struct drm_output_state *src,
1238 struct drm_pending_state *pending_state,
1239 enum drm_output_state_duplicate_mode plane_mode)
1240{
1241 struct drm_output_state *dst = malloc(sizeof(*dst));
Daniel Stonebc15f682016-11-14 16:57:01 +00001242 struct drm_plane_state *ps;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001243
1244 assert(dst);
1245
1246 /* Copy the whole structure, then individually modify the
1247 * pending_state, as well as the list link into our pending
1248 * state. */
1249 *dst = *src;
1250
1251 dst->pending_state = pending_state;
1252 if (pending_state)
1253 wl_list_insert(&pending_state->output_list, &dst->link);
1254 else
1255 wl_list_init(&dst->link);
1256
Daniel Stonebc15f682016-11-14 16:57:01 +00001257 wl_list_init(&dst->plane_list);
1258
1259 wl_list_for_each(ps, &src->plane_list, link) {
1260 /* Don't carry planes which are now disabled; these should be
1261 * free for other outputs to reuse. */
1262 if (!ps->output)
1263 continue;
1264
1265 if (plane_mode == DRM_OUTPUT_STATE_CLEAR_PLANES)
1266 (void) drm_plane_state_alloc(dst, ps->plane);
1267 else
1268 (void) drm_plane_state_duplicate(dst, ps);
1269 }
1270
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001271 return dst;
1272}
1273
1274/**
1275 * Free an unused drm_output_state.
1276 */
1277static void
1278drm_output_state_free(struct drm_output_state *state)
1279{
Daniel Stonebc15f682016-11-14 16:57:01 +00001280 struct drm_plane_state *ps, *next;
1281
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001282 if (!state)
1283 return;
1284
Daniel Stonebc15f682016-11-14 16:57:01 +00001285 wl_list_for_each_safe(ps, next, &state->plane_list, link)
1286 drm_plane_state_free(ps, false);
1287
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001288 wl_list_remove(&state->link);
Daniel Stonebc15f682016-11-14 16:57:01 +00001289
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001290 free(state);
Daniel Stone90648872016-10-21 18:08:37 +01001291}
1292
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001293/**
Daniel Stonea08512f2016-11-08 17:46:10 +00001294 * Get output state to disable output
1295 *
1296 * Returns a pointer to an output_state object which can be used to disable
1297 * an output (e.g. DPMS off).
1298 *
1299 * @param pending_state The pending state object owning this update
1300 * @param output The output to disable
1301 * @returns A drm_output_state to disable the output
1302 */
1303static struct drm_output_state *
1304drm_output_get_disable_state(struct drm_pending_state *pending_state,
1305 struct drm_output *output)
1306{
1307 struct drm_output_state *output_state;
1308
1309 output_state = drm_output_state_duplicate(output->state_cur,
1310 pending_state,
1311 DRM_OUTPUT_STATE_CLEAR_PLANES);
1312 output_state->dpms = WESTON_DPMS_OFF;
1313
1314 return output_state;
1315}
1316
1317/**
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001318 * Allocate a new drm_pending_state
1319 *
1320 * Allocate a new, empty, 'pending state' structure to be used across a
1321 * repaint cycle or similar.
1322 *
1323 * @param backend DRM backend
1324 * @returns Newly-allocated pending state structure
1325 */
1326static struct drm_pending_state *
1327drm_pending_state_alloc(struct drm_backend *backend)
1328{
1329 struct drm_pending_state *ret;
1330
1331 ret = calloc(1, sizeof(*ret));
1332 if (!ret)
1333 return NULL;
1334
1335 ret->backend = backend;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001336 wl_list_init(&ret->output_list);
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001337
1338 return ret;
1339}
1340
1341/**
1342 * Free a drm_pending_state structure
1343 *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001344 * Frees a pending_state structure, as well as any output_states connected
1345 * to this pending state.
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001346 *
1347 * @param pending_state Pending state structure to free
1348 */
1349static void
1350drm_pending_state_free(struct drm_pending_state *pending_state)
1351{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001352 struct drm_output_state *output_state, *tmp;
1353
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001354 if (!pending_state)
1355 return;
1356
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001357 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
1358 link) {
1359 drm_output_state_free(output_state);
1360 }
1361
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001362 free(pending_state);
1363}
1364
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001365/**
1366 * Find an output state in a pending state
1367 *
1368 * Given a pending_state structure, find the output_state for a particular
1369 * output.
1370 *
1371 * @param pending_state Pending state structure to search
1372 * @param output Output to find state for
1373 * @returns Output state if present, or NULL if not
1374 */
1375static struct drm_output_state *
1376drm_pending_state_get_output(struct drm_pending_state *pending_state,
1377 struct drm_output *output)
1378{
1379 struct drm_output_state *output_state;
1380
1381 wl_list_for_each(output_state, &pending_state->output_list, link) {
1382 if (output_state->output == output)
1383 return output_state;
1384 }
1385
1386 return NULL;
1387}
1388
Daniel Stonea08512f2016-11-08 17:46:10 +00001389static int drm_pending_state_apply_sync(struct drm_pending_state *state);
1390
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001391/**
1392 * Mark a drm_output_state (the output's last state) as complete. This handles
1393 * any post-completion actions such as updating the repaint timer, disabling the
1394 * output, and finally freeing the state.
1395 */
1396static void
1397drm_output_update_complete(struct drm_output *output, uint32_t flags,
1398 unsigned int sec, unsigned int usec)
1399{
Daniel Stonea08512f2016-11-08 17:46:10 +00001400 struct drm_backend *b = to_drm_backend(output->base.compositor);
Daniel Stonebc15f682016-11-14 16:57:01 +00001401 struct drm_plane_state *ps;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001402 struct timespec ts;
1403
1404 /* Stop the pageflip timer instead of rearming it here */
1405 if (output->pageflip_timer)
1406 wl_event_source_timer_update(output->pageflip_timer, 0);
1407
Daniel Stonebc15f682016-11-14 16:57:01 +00001408 wl_list_for_each(ps, &output->state_cur->plane_list, link)
1409 ps->complete = true;
1410
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001411 drm_output_state_free(output->state_last);
1412 output->state_last = NULL;
1413
1414 if (output->destroy_pending) {
Daniel Stonea08512f2016-11-08 17:46:10 +00001415 output->destroy_pending = 0;
1416 output->disable_pending = 0;
1417 output->dpms_off_pending = 0;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001418 drm_output_destroy(&output->base);
1419 return;
1420 } else if (output->disable_pending) {
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001421 output->disable_pending = 0;
Daniel Stonea08512f2016-11-08 17:46:10 +00001422 output->dpms_off_pending = 0;
1423 weston_output_disable(&output->base);
1424 return;
1425 } else if (output->dpms_off_pending) {
1426 struct drm_pending_state *pending = drm_pending_state_alloc(b);
1427 output->dpms_off_pending = 0;
1428 drm_output_get_disable_state(pending, output);
1429 drm_pending_state_apply_sync(pending);
1430 return;
1431 } else if (output->state_cur->dpms == WESTON_DPMS_OFF &&
1432 output->base.repaint_status != REPAINT_AWAITING_COMPLETION) {
1433 /* DPMS can happen to us either in the middle of a repaint
1434 * cycle (when we have painted fresh content, only to throw it
1435 * away for DPMS off), or at any other random point. If the
1436 * latter is true, then we cannot go through finish_frame,
1437 * because the repaint machinery does not expect this. */
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001438 return;
1439 }
1440
1441 ts.tv_sec = sec;
1442 ts.tv_nsec = usec * 1000;
1443 weston_output_finish_frame(&output->base, &ts, flags);
1444
1445 /* We can't call this from frame_notify, because the output's
1446 * repaint needed flag is cleared just after that */
1447 if (output->recorder)
1448 weston_output_schedule_repaint(&output->base);
1449}
1450
1451/**
1452 * Mark an output state as current on the output, i.e. it has been
1453 * submitted to the kernel. The mode argument determines whether this
1454 * update will be applied synchronously (e.g. when calling drmModeSetCrtc),
1455 * or asynchronously (in which case we wait for events to complete).
1456 */
1457static void
1458drm_output_assign_state(struct drm_output_state *state,
1459 enum drm_state_apply_mode mode)
1460{
1461 struct drm_output *output = state->output;
Daniel Stone598ee9d2016-11-16 11:55:20 +00001462 struct drm_backend *b = to_drm_backend(output->base.compositor);
Daniel Stonebc15f682016-11-14 16:57:01 +00001463 struct drm_plane_state *plane_state;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001464
1465 assert(!output->state_last);
1466
1467 if (mode == DRM_STATE_APPLY_ASYNC)
1468 output->state_last = output->state_cur;
1469 else
1470 drm_output_state_free(output->state_cur);
1471
1472 wl_list_remove(&state->link);
1473 wl_list_init(&state->link);
1474 state->pending_state = NULL;
1475
1476 output->state_cur = state;
Daniel Stonebc15f682016-11-14 16:57:01 +00001477
Daniel Stone598ee9d2016-11-16 11:55:20 +00001478 if (b->atomic_modeset && mode == DRM_STATE_APPLY_ASYNC)
1479 output->atomic_complete_pending = 1;
1480
Daniel Stonebc15f682016-11-14 16:57:01 +00001481 /* Replace state_cur on each affected plane with the new state, being
1482 * careful to dispose of orphaned (but only orphaned) previous state.
1483 * If the previous state is not orphaned (still has an output_state
1484 * attached), it will be disposed of by freeing the output_state. */
1485 wl_list_for_each(plane_state, &state->plane_list, link) {
1486 struct drm_plane *plane = plane_state->plane;
1487
1488 if (plane->state_cur && !plane->state_cur->output_state)
1489 drm_plane_state_free(plane->state_cur, true);
1490 plane->state_cur = plane_state;
1491
1492 if (mode != DRM_STATE_APPLY_ASYNC) {
1493 plane_state->complete = true;
1494 continue;
1495 }
1496
Daniel Stone598ee9d2016-11-16 11:55:20 +00001497 if (b->atomic_modeset)
1498 continue;
1499
Daniel Stonebc15f682016-11-14 16:57:01 +00001500 if (plane->type == WDRM_PLANE_TYPE_OVERLAY)
1501 output->vblank_pending++;
Daniel Stonee2e80132018-01-16 15:37:33 +00001502 else if (plane->type == WDRM_PLANE_TYPE_PRIMARY)
1503 output->page_flip_pending = 1;
Daniel Stonebc15f682016-11-14 16:57:01 +00001504 }
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001505}
1506
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001507static int
1508drm_view_transform_supported(struct weston_view *ev)
1509{
1510 return !ev->transform.enabled ||
1511 (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
1512}
1513
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001514static uint32_t
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +02001515drm_output_check_scanout_format(struct drm_output *output,
1516 struct weston_surface *es, struct gbm_bo *bo)
1517{
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +02001518 uint32_t format;
1519 pixman_region32_t r;
1520
1521 format = gbm_bo_get_format(bo);
1522
Kristian Høgsberg3f97b342013-10-16 16:08:57 -07001523 if (format == GBM_FORMAT_ARGB8888) {
1524 /* We can scanout an ARGB buffer if the surface's
1525 * opaque region covers the whole output, but we have
1526 * to use XRGB as the KMS format code. */
Kristian Høgsberg1be87e32014-01-17 14:22:41 -08001527 pixman_region32_init_rect(&r, 0, 0,
1528 output->base.width,
1529 output->base.height);
1530 pixman_region32_subtract(&r, &r, &es->opaque);
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +02001531
1532 if (!pixman_region32_not_empty(&r))
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001533 format = GBM_FORMAT_XRGB8888;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +02001534
1535 pixman_region32_fini(&r);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001536 }
Kristian Høgsberg3f97b342013-10-16 16:08:57 -07001537
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001538 if (output->gbm_format == format)
Kristian Høgsberg3f97b342013-10-16 16:08:57 -07001539 return format;
1540
1541 return 0;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +02001542}
1543
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001544static struct weston_plane *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001545drm_output_prepare_scanout_view(struct drm_output_state *output_state,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001546 struct weston_view *ev)
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001547{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001548 struct drm_output *output = output_state->output;
Armin Krezović545dba62016-08-05 15:54:18 +02001549 struct drm_backend *b = to_drm_backend(output->base.compositor);
Daniel Stonee2e80132018-01-16 15:37:33 +00001550 struct drm_plane *scanout_plane = output->scanout_plane;
1551 struct drm_plane_state *state;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001552 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001553 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001554 struct gbm_bo *bo;
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001555 uint32_t format;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001556
Daniel Stone90648872016-10-21 18:08:37 +01001557 /* Don't import buffers which span multiple outputs. */
1558 if (ev->output_mask != (1u << output->base.id))
1559 return NULL;
1560
Daniel Stone296d7a92016-10-21 18:05:37 +01001561 /* We use GBM to import buffers. */
1562 if (b->gbm == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001563 return NULL;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001564
Daniel Stone296d7a92016-10-21 18:05:37 +01001565 if (buffer == NULL)
1566 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +01001567 if (wl_shm_buffer_get(buffer->resource))
1568 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001569
1570 /* Make sure our view is exactly compatible with the output. */
1571 if (ev->geometry.x != output->base.x ||
1572 ev->geometry.y != output->base.y)
1573 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +01001574 if (buffer->width != output->base.current_mode->width ||
1575 buffer->height != output->base.current_mode->height)
1576 return NULL;
1577
Daniel Stone296d7a92016-10-21 18:05:37 +01001578 if (ev->transform.enabled)
1579 return NULL;
Pekka Paalanen5580f222015-02-17 16:33:18 +02001580 if (ev->geometry.scissor_enabled)
1581 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001582 if (viewport->buffer.transform != output->base.transform)
1583 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +01001584 if (viewport->buffer.scale != output->base.current_scale)
1585 return NULL;
1586 if (!drm_view_transform_supported(ev))
1587 return NULL;
1588
1589 if (ev->alpha != 1.0f)
1590 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001591
Daniel Stonee2e80132018-01-16 15:37:33 +00001592 state = drm_output_state_get_plane(output_state, scanout_plane);
1593 if (state->fb) {
1594 /* If there is already a framebuffer on the scanout plane,
1595 * a client view has already been placed on the scanout
1596 * view. In that case, do not free or put back the state,
1597 * but just leave it in place and quietly exit. */
1598 return NULL;
1599 }
1600
Giulio Camuffo954f1832014-10-11 18:27:30 +03001601 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
Kristian Høgsberg63996462013-09-03 22:27:08 -07001602 buffer->resource, GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001603
Rob Bradford9b101872012-09-14 23:25:41 +01001604 /* Unable to use the buffer for scanout */
1605 if (!bo)
1606 return NULL;
1607
Jason Ekstranda7af7042013-10-12 22:38:11 -05001608 format = drm_output_check_scanout_format(output, ev->surface, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001609 if (format == 0) {
Daniel Stonee2e80132018-01-16 15:37:33 +00001610 drm_plane_state_put_back(state);
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +03001611 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001612 return NULL;
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +03001613 }
1614
Daniel Stonee2e80132018-01-16 15:37:33 +00001615 state->fb = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT);
1616 if (!state->fb) {
1617 drm_plane_state_put_back(state);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001618 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001619 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001620 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001621
Daniel Stonee2e80132018-01-16 15:37:33 +00001622 drm_fb_set_buffer(state->fb, buffer);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001623
Daniel Stonee2e80132018-01-16 15:37:33 +00001624 state->output = output;
1625
1626 state->src_x = 0;
1627 state->src_y = 0;
1628 state->src_w = state->fb->width << 16;
1629 state->src_h = state->fb->height << 16;
1630
1631 state->dest_x = 0;
1632 state->dest_y = 0;
1633 state->dest_w = output->base.current_mode->width;
1634 state->dest_h = output->base.current_mode->height;
1635
1636 return &scanout_plane->base;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001637}
1638
Daniel Stone95d48a22017-04-04 17:54:30 +01001639static struct drm_fb *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001640drm_output_render_gl(struct drm_output_state *state, pixman_region32_t *damage)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001641{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001642 struct drm_output *output = state->output;
Armin Krezović545dba62016-08-05 15:54:18 +02001643 struct drm_backend *b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001644 struct gbm_bo *bo;
Daniel Stone95d48a22017-04-04 17:54:30 +01001645 struct drm_fb *ret;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001646
Giulio Camuffo954f1832014-10-11 18:27:30 +03001647 output->base.compositor->renderer->repaint_output(&output->base,
1648 damage);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001649
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001650 bo = gbm_surface_lock_front_buffer(output->gbm_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001651 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +02001652 weston_log("failed to lock front buffer: %m\n");
Daniel Stone95d48a22017-04-04 17:54:30 +01001653 return NULL;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001654 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001655
Daniel Stone95d48a22017-04-04 17:54:30 +01001656 ret = drm_fb_get_from_bo(bo, b, output->gbm_format, BUFFER_GBM_SURFACE);
1657 if (!ret) {
Martin Minarik6d118362012-06-07 18:01:59 +02001658 weston_log("failed to get drm_fb for bo\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001659 gbm_surface_release_buffer(output->gbm_surface, bo);
Daniel Stone95d48a22017-04-04 17:54:30 +01001660 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001661 }
Daniel Stone95d48a22017-04-04 17:54:30 +01001662 ret->gbm_surface = output->gbm_surface;
1663
1664 return ret;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001665}
1666
Daniel Stone95d48a22017-04-04 17:54:30 +01001667static struct drm_fb *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001668drm_output_render_pixman(struct drm_output_state *state,
1669 pixman_region32_t *damage)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001670{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001671 struct drm_output *output = state->output;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001672 struct weston_compositor *ec = output->base.compositor;
1673 pixman_region32_t total_damage, previous_damage;
1674
1675 pixman_region32_init(&total_damage);
1676 pixman_region32_init(&previous_damage);
1677
1678 pixman_region32_copy(&previous_damage, damage);
1679
1680 pixman_region32_union(&total_damage, damage, &output->previous_damage);
1681 pixman_region32_copy(&output->previous_damage, &previous_damage);
1682
1683 output->current_image ^= 1;
1684
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001685 pixman_renderer_output_set_buffer(&output->base,
1686 output->image[output->current_image]);
1687
1688 ec->renderer->repaint_output(&output->base, &total_damage);
1689
1690 pixman_region32_fini(&total_damage);
1691 pixman_region32_fini(&previous_damage);
Daniel Stone95d48a22017-04-04 17:54:30 +01001692
1693 return drm_fb_ref(output->dumb[output->current_image]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001694}
1695
1696static void
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001697drm_output_render(struct drm_output_state *state, pixman_region32_t *damage)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001698{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001699 struct drm_output *output = state->output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001700 struct weston_compositor *c = output->base.compositor;
Daniel Stonee2e80132018-01-16 15:37:33 +00001701 struct drm_plane_state *scanout_state;
Daniel Stonee95169b2016-11-14 17:46:59 +00001702 struct drm_plane *scanout_plane = output->scanout_plane;
Armin Krezović545dba62016-08-05 15:54:18 +02001703 struct drm_backend *b = to_drm_backend(c);
Daniel Stone95d48a22017-04-04 17:54:30 +01001704 struct drm_fb *fb;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001705
Daniel Stone4e84f7d2017-04-04 17:54:29 +01001706 /* If we already have a client buffer promoted to scanout, then we don't
1707 * want to render. */
Daniel Stonee2e80132018-01-16 15:37:33 +00001708 scanout_state = drm_output_state_get_plane(state,
1709 output->scanout_plane);
1710 if (scanout_state->fb)
Daniel Stone4e84f7d2017-04-04 17:54:29 +01001711 return;
1712
Daniel Stonee95169b2016-11-14 17:46:59 +00001713 if (!pixman_region32_not_empty(damage) &&
1714 scanout_plane->state_cur->fb &&
1715 (scanout_plane->state_cur->fb->type == BUFFER_GBM_SURFACE ||
1716 scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB) &&
1717 scanout_plane->state_cur->fb->width ==
1718 output->base.current_mode->width &&
1719 scanout_plane->state_cur->fb->height ==
1720 output->base.current_mode->height) {
1721 fb = drm_fb_ref(scanout_plane->state_cur->fb);
1722 } else if (b->use_pixman) {
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001723 fb = drm_output_render_pixman(state, damage);
Daniel Stonee95169b2016-11-14 17:46:59 +00001724 } else {
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001725 fb = drm_output_render_gl(state, damage);
Daniel Stonee95169b2016-11-14 17:46:59 +00001726 }
Daniel Stone95d48a22017-04-04 17:54:30 +01001727
Daniel Stonee2e80132018-01-16 15:37:33 +00001728 if (!fb) {
1729 drm_plane_state_put_back(scanout_state);
Daniel Stone95d48a22017-04-04 17:54:30 +01001730 return;
Daniel Stonee2e80132018-01-16 15:37:33 +00001731 }
1732
1733 scanout_state->fb = fb;
1734 scanout_state->output = output;
1735
1736 scanout_state->src_x = 0;
1737 scanout_state->src_y = 0;
1738 scanout_state->src_w = output->base.current_mode->width << 16;
1739 scanout_state->src_h = output->base.current_mode->height << 16;
1740
1741 scanout_state->dest_x = 0;
1742 scanout_state->dest_y = 0;
1743 scanout_state->dest_w = scanout_state->src_w >> 16;
1744 scanout_state->dest_h = scanout_state->src_h >> 16;
1745
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001746
Giulio Camuffo954f1832014-10-11 18:27:30 +03001747 pixman_region32_subtract(&c->primary_plane.damage,
1748 &c->primary_plane.damage, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001749}
1750
1751static void
Richard Hughese7299962013-05-01 21:52:12 +01001752drm_output_set_gamma(struct weston_output *output_base,
1753 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
1754{
1755 int rc;
Armin Krezović545dba62016-08-05 15:54:18 +02001756 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001757 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +02001758 to_drm_backend(output->base.compositor);
Richard Hughese7299962013-05-01 21:52:12 +01001759
1760 /* check */
1761 if (output_base->gamma_size != size)
1762 return;
Richard Hughese7299962013-05-01 21:52:12 +01001763
Giulio Camuffo954f1832014-10-11 18:27:30 +03001764 rc = drmModeCrtcSetGamma(backend->drm.fd,
Richard Hughese7299962013-05-01 21:52:12 +01001765 output->crtc_id,
1766 size, r, g, b);
1767 if (rc)
1768 weston_log("set gamma failed: %m\n");
1769}
1770
Bryce Harringtonada4f072015-06-30 13:25:46 -07001771/* Determine the type of vblank synchronization to use for the output.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001772 *
Bryce Harringtonada4f072015-06-30 13:25:46 -07001773 * The pipe parameter indicates which CRTC is in use. Knowing this, we
1774 * can determine which vblank sequence type to use for it. Traditional
1775 * cards had only two CRTCs, with CRTC 0 using no special flags, and
1776 * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
1777 * parameter indicates this.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001778 *
Bryce Harringtonada4f072015-06-30 13:25:46 -07001779 * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
1780 * 0-31. If this is non-zero it indicates we're dealing with a
1781 * multi-gpu situation and we need to calculate the vblank sync
1782 * using DRM_BLANK_HIGH_CRTC_MASK.
1783 */
Pekka Paalanenc8a1ff02015-07-02 15:06:08 +03001784static unsigned int
1785drm_waitvblank_pipe(struct drm_output *output)
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +02001786{
1787 if (output->pipe > 1)
1788 return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
1789 DRM_VBLANK_HIGH_CRTC_MASK;
1790 else if (output->pipe > 0)
1791 return DRM_VBLANK_SECONDARY;
1792 else
1793 return 0;
1794}
1795
David Herrmann1edf44c2013-10-22 17:11:26 +02001796static int
Daniel Stone598ee9d2016-11-16 11:55:20 +00001797drm_output_apply_state_legacy(struct drm_output_state *state)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001798{
Daniel Stonea08512f2016-11-08 17:46:10 +00001799 struct drm_output *output = state->output;
1800 struct drm_backend *backend = to_drm_backend(output->base.compositor);
Daniel Stonee2e80132018-01-16 15:37:33 +00001801 struct drm_plane *scanout_plane = output->scanout_plane;
Pekka Paalanen02aeb5c2017-09-12 16:02:01 +03001802 struct drm_property_info *dpms_prop;
Daniel Stonee2e80132018-01-16 15:37:33 +00001803 struct drm_plane_state *scanout_state;
Daniel Stonebc15f682016-11-14 16:57:01 +00001804 struct drm_plane_state *ps;
Daniel Stone085d2b92015-05-21 00:00:57 +01001805 struct drm_plane *p;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001806 struct drm_mode *mode;
Pekka Paalanen02aeb5c2017-09-12 16:02:01 +03001807 struct drm_head *head;
1808 uint32_t connectors[MAX_CLONED_CONNECTORS];
1809 int n_conn = 0;
Daniel Stonea08512f2016-11-08 17:46:10 +00001810 struct timespec now;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001811 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001812
Pekka Paalanen02aeb5c2017-09-12 16:02:01 +03001813 wl_list_for_each(head, &output->base.head_list, base.output_link) {
1814 assert(n_conn < MAX_CLONED_CONNECTORS);
1815 connectors[n_conn++] = head->connector_id;
1816 }
1817
Derek Foreman2cd87fe2017-04-13 13:48:48 -05001818 /* If disable_planes is set then assign_planes() wasn't
1819 * called for this render, so we could still have a stale
1820 * cursor plane set up.
1821 */
1822 if (output->base.disable_planes) {
1823 output->cursor_view = NULL;
Greg V1f781762018-02-19 17:59:42 +03001824 if (output->cursor_plane) {
1825 output->cursor_plane->base.x = INT32_MIN;
1826 output->cursor_plane->base.y = INT32_MIN;
1827 }
Derek Foreman2cd87fe2017-04-13 13:48:48 -05001828 }
1829
Daniel Stonea08512f2016-11-08 17:46:10 +00001830 if (state->dpms != WESTON_DPMS_ON) {
1831 wl_list_for_each(ps, &state->plane_list, link) {
1832 p = ps->plane;
1833 assert(ps->fb == NULL);
1834 assert(ps->output == NULL);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001835
Daniel Stonea08512f2016-11-08 17:46:10 +00001836 if (p->type != WDRM_PLANE_TYPE_OVERLAY)
1837 continue;
1838
1839 ret = drmModeSetPlane(backend->drm.fd, p->plane_id,
1840 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1841 if (ret)
1842 weston_log("drmModeSetPlane failed disable: %m\n");
1843 }
1844
1845 if (output->cursor_plane) {
1846 ret = drmModeSetCursor(backend->drm.fd, output->crtc_id,
1847 0, 0, 0);
1848 if (ret)
1849 weston_log("drmModeSetCursor failed disable: %m\n");
1850 }
1851
1852 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id, 0, 0, 0,
Pekka Paalanen02aeb5c2017-09-12 16:02:01 +03001853 NULL, 0, NULL);
Daniel Stonea08512f2016-11-08 17:46:10 +00001854 if (ret)
1855 weston_log("drmModeSetCrtc failed disabling: %m\n");
1856
1857 drm_output_assign_state(state, DRM_STATE_APPLY_SYNC);
1858 weston_compositor_read_presentation_clock(output->base.compositor, &now);
1859 drm_output_update_complete(output,
1860 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION,
1861 now.tv_sec, now.tv_nsec / 1000);
1862
1863 return 0;
1864 }
1865
1866 scanout_state =
1867 drm_output_state_get_existing_plane(state, scanout_plane);
Daniel Stone087ddf02017-02-14 17:51:30 +00001868
Daniel Stonee2e80132018-01-16 15:37:33 +00001869 /* The legacy SetCrtc API doesn't allow us to do scaling, and the
1870 * legacy PageFlip API doesn't allow us to do clipping either. */
1871 assert(scanout_state->src_x == 0);
1872 assert(scanout_state->src_y == 0);
1873 assert(scanout_state->src_w ==
1874 (unsigned) (output->base.current_mode->width << 16));
1875 assert(scanout_state->src_h ==
1876 (unsigned) (output->base.current_mode->height << 16));
1877 assert(scanout_state->dest_x == 0);
1878 assert(scanout_state->dest_y == 0);
1879 assert(scanout_state->dest_w == scanout_state->src_w >> 16);
1880 assert(scanout_state->dest_h == scanout_state->src_h >> 16);
1881
Daniel Stonecb04cc42016-11-16 11:51:27 +00001882 mode = to_drm_mode(output->base.current_mode);
Daniel Stone6020f472018-02-05 15:46:20 +00001883 if (backend->state_invalid || !scanout_plane->state_cur->fb ||
Daniel Stonee2e80132018-01-16 15:37:33 +00001884 scanout_plane->state_cur->fb->stride != scanout_state->fb->stride) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001885 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Daniel Stonee2e80132018-01-16 15:37:33 +00001886 scanout_state->fb->fb_id,
1887 0, 0,
Pekka Paalanen02aeb5c2017-09-12 16:02:01 +03001888 connectors, n_conn,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001889 &mode->mode_info);
1890 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +02001891 weston_log("set mode failed: %m\n");
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001892 goto err;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001893 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001894 }
1895
Giulio Camuffo954f1832014-10-11 18:27:30 +03001896 if (drmModePageFlip(backend->drm.fd, output->crtc_id,
Daniel Stonee2e80132018-01-16 15:37:33 +00001897 scanout_state->fb->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -05001898 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001899 weston_log("queueing pageflip failed: %m\n");
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001900 goto err;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -05001901 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001902
Daniel Stone205c0a02017-04-04 17:54:33 +01001903 assert(!output->page_flip_pending);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001904
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001905 if (output->pageflip_timer)
1906 wl_event_source_timer_update(output->pageflip_timer,
1907 backend->pageflip_timeout);
1908
Daniel Stone2ba17f42015-05-19 20:02:41 +01001909 drm_output_set_cursor(state);
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001910
Jesse Barnes58ef3792012-02-23 09:45:49 -05001911 /*
1912 * Now, update all the sprite surfaces
1913 */
Daniel Stonebc15f682016-11-14 16:57:01 +00001914 wl_list_for_each(ps, &state->plane_list, link) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001915 uint32_t flags = 0, fb_id = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001916 drmVBlank vbl = {
1917 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
1918 .request.sequence = 1,
1919 };
1920
Daniel Stonebc15f682016-11-14 16:57:01 +00001921 p = ps->plane;
Daniel Stone085d2b92015-05-21 00:00:57 +01001922 if (p->type != WDRM_PLANE_TYPE_OVERLAY)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001923 continue;
1924
Daniel Stonebc15f682016-11-14 16:57:01 +00001925 assert(p->state_cur->complete);
1926 assert(!!p->state_cur->output == !!p->state_cur->fb);
1927 assert(!p->state_cur->output || p->state_cur->output == output);
1928 assert(!ps->complete);
1929 assert(!ps->output || ps->output == output);
1930 assert(!!ps->output == !!ps->fb);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001931
Daniel Stonebc15f682016-11-14 16:57:01 +00001932 if (ps->fb && !backend->sprites_hidden)
1933 fb_id = ps->fb->fb_id;
Daniel Stone085d2b92015-05-21 00:00:57 +01001934
1935 ret = drmModeSetPlane(backend->drm.fd, p->plane_id,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001936 output->crtc_id, fb_id, flags,
Daniel Stonebc15f682016-11-14 16:57:01 +00001937 ps->dest_x, ps->dest_y,
1938 ps->dest_w, ps->dest_h,
1939 ps->src_x, ps->src_y,
1940 ps->src_w, ps->src_h);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001941 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +02001942 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05001943 ret, strerror(errno));
1944
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +02001945 vbl.request.type |= drm_waitvblank_pipe(output);
Rob Clark5ca1a472012-08-08 20:27:37 -05001946
Jesse Barnes58ef3792012-02-23 09:45:49 -05001947 /*
1948 * Queue a vblank signal so we know when the surface
1949 * becomes active on the display or has been replaced.
1950 */
Daniel Stonebc15f682016-11-14 16:57:01 +00001951 vbl.request.signal = (unsigned long) ps;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001952 ret = drmWaitVBlank(backend->drm.fd, &vbl);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001953 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +02001954 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05001955 ret, strerror(errno));
1956 }
1957 }
1958
Pekka Paalanen02aeb5c2017-09-12 16:02:01 +03001959 if (state->dpms != output->state_cur->dpms) {
1960 wl_list_for_each(head, &output->base.head_list, base.output_link) {
1961 dpms_prop = &head->props_conn[WDRM_CONNECTOR_DPMS];
1962 if (dpms_prop->prop_id == 0)
1963 continue;
1964
1965 ret = drmModeConnectorSetProperty(backend->drm.fd,
1966 head->connector_id,
1967 dpms_prop->prop_id,
1968 state->dpms);
1969 if (ret) {
1970 weston_log("DRM: DPMS: failed property set for %s\n",
1971 head->base.name);
1972 }
Daniel Stonea08512f2016-11-08 17:46:10 +00001973 }
1974 }
1975
1976 drm_output_assign_state(state, DRM_STATE_APPLY_ASYNC);
1977
David Herrmann1edf44c2013-10-22 17:11:26 +02001978 return 0;
1979
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001980err:
Kristian Høgsbergb3955b02014-01-23 16:25:06 -08001981 output->cursor_view = NULL;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001982 drm_output_state_free(state);
Daniel Stonea08512f2016-11-08 17:46:10 +00001983 return -1;
1984}
David Herrmann1edf44c2013-10-22 17:11:26 +02001985
Daniel Stone598ee9d2016-11-16 11:55:20 +00001986#ifdef HAVE_DRM_ATOMIC
1987static int
1988crtc_add_prop(drmModeAtomicReq *req, struct drm_output *output,
1989 enum wdrm_crtc_property prop, uint64_t val)
1990{
1991 struct drm_property_info *info = &output->props_crtc[prop];
1992 int ret;
1993
1994 if (info->prop_id == 0)
1995 return -1;
1996
1997 ret = drmModeAtomicAddProperty(req, output->crtc_id, info->prop_id,
1998 val);
1999 return (ret <= 0) ? -1 : 0;
2000}
2001
2002static int
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002003connector_add_prop(drmModeAtomicReq *req, struct drm_head *head,
Daniel Stone598ee9d2016-11-16 11:55:20 +00002004 enum wdrm_connector_property prop, uint64_t val)
2005{
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002006 struct drm_property_info *info = &head->props_conn[prop];
Daniel Stone598ee9d2016-11-16 11:55:20 +00002007 int ret;
2008
2009 if (info->prop_id == 0)
2010 return -1;
2011
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002012 ret = drmModeAtomicAddProperty(req, head->connector_id,
Daniel Stone598ee9d2016-11-16 11:55:20 +00002013 info->prop_id, val);
2014 return (ret <= 0) ? -1 : 0;
2015}
2016
2017static int
2018plane_add_prop(drmModeAtomicReq *req, struct drm_plane *plane,
2019 enum wdrm_plane_property prop, uint64_t val)
2020{
2021 struct drm_property_info *info = &plane->props[prop];
2022 int ret;
2023
2024 if (info->prop_id == 0)
2025 return -1;
2026
2027 ret = drmModeAtomicAddProperty(req, plane->plane_id, info->prop_id,
2028 val);
2029 return (ret <= 0) ? -1 : 0;
2030}
2031
2032static int
2033drm_mode_ensure_blob(struct drm_backend *backend, struct drm_mode *mode)
2034{
2035 int ret;
2036
2037 if (mode->blob_id)
2038 return 0;
2039
2040 ret = drmModeCreatePropertyBlob(backend->drm.fd,
2041 &mode->mode_info,
2042 sizeof(mode->mode_info),
2043 &mode->blob_id);
2044 if (ret != 0)
2045 weston_log("failed to create mode property blob: %m\n");
2046
2047 return ret;
2048}
2049
2050static int
2051drm_output_apply_state_atomic(struct drm_output_state *state,
2052 drmModeAtomicReq *req,
2053 uint32_t *flags)
2054{
2055 struct drm_output *output = state->output;
2056 struct drm_backend *backend = to_drm_backend(output->base.compositor);
2057 struct drm_plane_state *plane_state;
2058 struct drm_mode *current_mode = to_drm_mode(output->base.current_mode);
Pekka Paalanen2f661302017-09-12 16:07:32 +03002059 struct drm_head *head;
Daniel Stone598ee9d2016-11-16 11:55:20 +00002060 int ret = 0;
2061
2062 if (state->dpms != output->state_cur->dpms)
2063 *flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
2064
2065 if (state->dpms == WESTON_DPMS_ON) {
2066 ret = drm_mode_ensure_blob(backend, current_mode);
2067 if (ret != 0)
2068 return ret;
2069
2070 ret |= crtc_add_prop(req, output, WDRM_CRTC_MODE_ID,
2071 current_mode->blob_id);
2072 ret |= crtc_add_prop(req, output, WDRM_CRTC_ACTIVE, 1);
Pekka Paalanen2f661302017-09-12 16:07:32 +03002073
2074 wl_list_for_each(head, &output->base.head_list, base.output_link) {
2075 ret |= connector_add_prop(req, head, WDRM_CONNECTOR_CRTC_ID,
2076 output->crtc_id);
2077 }
Daniel Stone598ee9d2016-11-16 11:55:20 +00002078 } else {
2079 ret |= crtc_add_prop(req, output, WDRM_CRTC_MODE_ID, 0);
2080 ret |= crtc_add_prop(req, output, WDRM_CRTC_ACTIVE, 0);
Pekka Paalanen2f661302017-09-12 16:07:32 +03002081
2082 wl_list_for_each(head, &output->base.head_list, base.output_link)
2083 ret |= connector_add_prop(req, head, WDRM_CONNECTOR_CRTC_ID, 0);
Daniel Stone598ee9d2016-11-16 11:55:20 +00002084 }
2085
2086 if (ret != 0) {
2087 weston_log("couldn't set atomic CRTC/connector state\n");
2088 return ret;
2089 }
2090
2091 wl_list_for_each(plane_state, &state->plane_list, link) {
2092 struct drm_plane *plane = plane_state->plane;
2093
2094 ret |= plane_add_prop(req, plane, WDRM_PLANE_FB_ID,
2095 plane_state->fb ? plane_state->fb->fb_id : 0);
2096 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_ID,
2097 plane_state->fb ? output->crtc_id : 0);
2098 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_X,
2099 plane_state->src_x);
2100 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_Y,
2101 plane_state->src_y);
2102 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_W,
2103 plane_state->src_w);
2104 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_H,
2105 plane_state->src_h);
2106 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_X,
2107 plane_state->dest_x);
2108 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_Y,
2109 plane_state->dest_y);
2110 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_W,
2111 plane_state->dest_w);
2112 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_H,
2113 plane_state->dest_h);
2114
2115 if (ret != 0) {
2116 weston_log("couldn't set plane state\n");
2117 return ret;
2118 }
2119 }
2120
2121 return 0;
2122}
2123
2124/**
2125 * Helper function used only by drm_pending_state_apply, with the same
2126 * guarantees and constraints as that function.
2127 */
2128static int
2129drm_pending_state_apply_atomic(struct drm_pending_state *pending_state,
2130 enum drm_state_apply_mode mode)
2131{
2132 struct drm_backend *b = pending_state->backend;
2133 struct drm_output_state *output_state, *tmp;
2134 struct drm_plane *plane;
2135 drmModeAtomicReq *req = drmModeAtomicAlloc();
2136 uint32_t flags = 0;
2137 int ret = 0;
2138
2139 if (!req)
2140 return -1;
2141
2142 if (b->state_invalid) {
Pekka Paalaneneacec812017-09-12 13:43:51 +03002143 struct weston_head *head_base;
2144 struct drm_head *head;
Daniel Stone598ee9d2016-11-16 11:55:20 +00002145 uint32_t *unused;
2146 int err;
2147
2148 /* If we need to reset all our state (e.g. because we've
2149 * just started, or just been VT-switched in), explicitly
2150 * disable all the CRTCs and connectors we aren't using. */
Pekka Paalaneneacec812017-09-12 13:43:51 +03002151 wl_list_for_each(head_base,
2152 &b->compositor->head_list, compositor_link) {
Daniel Stone598ee9d2016-11-16 11:55:20 +00002153 struct drm_property_info *info;
Daniel Stone598ee9d2016-11-16 11:55:20 +00002154
Pekka Paalaneneacec812017-09-12 13:43:51 +03002155 if (weston_head_is_enabled(head_base))
Daniel Stone598ee9d2016-11-16 11:55:20 +00002156 continue;
Daniel Stone598ee9d2016-11-16 11:55:20 +00002157
Pekka Paalaneneacec812017-09-12 13:43:51 +03002158 head = to_drm_head(head_base);
Daniel Stone598ee9d2016-11-16 11:55:20 +00002159
Pekka Paalaneneacec812017-09-12 13:43:51 +03002160 info = &head->props_conn[WDRM_CONNECTOR_CRTC_ID];
2161 err = drmModeAtomicAddProperty(req, head->connector_id,
Daniel Stone598ee9d2016-11-16 11:55:20 +00002162 info->prop_id, 0);
2163 if (err <= 0)
2164 ret = -1;
2165
Pekka Paalaneneacec812017-09-12 13:43:51 +03002166 info = &head->props_conn[WDRM_CONNECTOR_DPMS];
Daniel Stone598ee9d2016-11-16 11:55:20 +00002167 if (info->prop_id > 0)
Pekka Paalaneneacec812017-09-12 13:43:51 +03002168 err = drmModeAtomicAddProperty(req, head->connector_id,
Daniel Stone598ee9d2016-11-16 11:55:20 +00002169 info->prop_id,
2170 DRM_MODE_DPMS_OFF);
2171 if (err <= 0)
2172 ret = -1;
Daniel Stone598ee9d2016-11-16 11:55:20 +00002173 }
2174
2175 wl_array_for_each(unused, &b->unused_crtcs) {
2176 struct drm_property_info infos[WDRM_CRTC__COUNT];
2177 struct drm_property_info *info;
2178 drmModeObjectProperties *props;
2179 uint64_t active;
2180
2181 memset(infos, 0, sizeof(infos));
2182
2183 /* We can't emit a disable on a CRTC that's already
2184 * off, as the kernel will refuse to generate an event
2185 * for an off->off state and fail the commit.
2186 */
2187 props = drmModeObjectGetProperties(b->drm.fd,
2188 *unused,
2189 DRM_MODE_OBJECT_CRTC);
2190 if (!props) {
2191 ret = -1;
2192 continue;
2193 }
2194
2195 drm_property_info_populate(b, crtc_props, infos,
2196 WDRM_CRTC__COUNT,
2197 props);
2198
2199 info = &infos[WDRM_CRTC_ACTIVE];
2200 active = drm_property_get_value(info, props, 0);
2201 drmModeFreeObjectProperties(props);
2202 if (active == 0) {
2203 drm_property_info_free(infos, WDRM_CRTC__COUNT);
2204 continue;
2205 }
2206
2207 err = drmModeAtomicAddProperty(req, *unused,
2208 info->prop_id, 0);
2209 if (err <= 0)
2210 ret = -1;
2211
2212 info = &infos[WDRM_CRTC_MODE_ID];
2213 err = drmModeAtomicAddProperty(req, *unused,
2214 info->prop_id, 0);
2215 if (err <= 0)
2216 ret = -1;
2217
2218 drm_property_info_free(infos, WDRM_CRTC__COUNT);
2219 }
2220
2221 /* Disable all the planes; planes which are being used will
2222 * override this state in the output-state application. */
2223 wl_list_for_each(plane, &b->plane_list, link) {
2224 plane_add_prop(req, plane, WDRM_PLANE_CRTC_ID, 0);
2225 plane_add_prop(req, plane, WDRM_PLANE_FB_ID, 0);
2226 }
2227
2228 flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
2229 }
2230
2231 wl_list_for_each(output_state, &pending_state->output_list, link) {
2232 if (mode == DRM_STATE_APPLY_SYNC)
2233 assert(output_state->dpms == WESTON_DPMS_OFF);
2234 ret |= drm_output_apply_state_atomic(output_state, req, &flags);
2235 }
2236
2237 if (ret != 0) {
2238 weston_log("atomic: couldn't compile atomic state\n");
2239 goto out;
2240 }
2241
2242 switch (mode) {
2243 case DRM_STATE_APPLY_SYNC:
2244 break;
2245 case DRM_STATE_APPLY_ASYNC:
2246 flags |= DRM_MODE_PAGE_FLIP_EVENT | DRM_MODE_ATOMIC_NONBLOCK;
2247 break;
2248 }
2249
2250 ret = drmModeAtomicCommit(b->drm.fd, req, flags, b);
2251 if (ret != 0) {
2252 weston_log("atomic: couldn't commit new state: %m\n");
2253 goto out;
2254 }
2255
2256 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
2257 link)
2258 drm_output_assign_state(output_state, mode);
2259
2260 b->state_invalid = false;
2261
2262 assert(wl_list_empty(&pending_state->output_list));
2263
2264out:
2265 drmModeAtomicFree(req);
2266 drm_pending_state_free(pending_state);
2267 return ret;
2268}
2269#endif
2270
Daniel Stonea08512f2016-11-08 17:46:10 +00002271/**
2272 * Applies all of a pending_state asynchronously: the primary entry point for
2273 * applying KMS state to a device. Updates the state for all outputs in the
2274 * pending_state, as well as disabling any unclaimed outputs.
2275 *
2276 * Unconditionally takes ownership of pending_state, and clears state_invalid.
2277 */
2278static int
2279drm_pending_state_apply(struct drm_pending_state *pending_state)
2280{
2281 struct drm_backend *b = pending_state->backend;
2282 struct drm_output_state *output_state, *tmp;
2283 uint32_t *unused;
2284
Daniel Stone598ee9d2016-11-16 11:55:20 +00002285#ifdef HAVE_DRM_ATOMIC
2286 if (b->atomic_modeset)
2287 return drm_pending_state_apply_atomic(pending_state,
2288 DRM_STATE_APPLY_ASYNC);
2289#endif
2290
Daniel Stonea08512f2016-11-08 17:46:10 +00002291 if (b->state_invalid) {
2292 /* If we need to reset all our state (e.g. because we've
2293 * just started, or just been VT-switched in), explicitly
2294 * disable all the CRTCs we aren't using. This also disables
2295 * all connectors on these CRTCs, so we don't need to do that
2296 * separately with the pre-atomic API. */
2297 wl_array_for_each(unused, &b->unused_crtcs)
2298 drmModeSetCrtc(b->drm.fd, *unused, 0, 0, 0, NULL, 0,
2299 NULL);
2300 }
2301
2302 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
2303 link) {
2304 struct drm_output *output = output_state->output;
2305 int ret;
2306
Daniel Stone598ee9d2016-11-16 11:55:20 +00002307 ret = drm_output_apply_state_legacy(output_state);
Daniel Stonea08512f2016-11-08 17:46:10 +00002308 if (ret != 0) {
2309 weston_log("Couldn't apply state for output %s\n",
2310 output->base.name);
2311 }
2312 }
2313
2314 b->state_invalid = false;
2315
2316 assert(wl_list_empty(&pending_state->output_list));
2317
2318 drm_pending_state_free(pending_state);
2319
2320 return 0;
2321}
2322
2323/**
2324 * The synchronous version of drm_pending_state_apply. May only be used to
2325 * disable outputs. Does so synchronously: the request is guaranteed to have
2326 * completed on return, and the output will not be touched afterwards.
2327 *
2328 * Unconditionally takes ownership of pending_state, and clears state_invalid.
2329 */
2330static int
2331drm_pending_state_apply_sync(struct drm_pending_state *pending_state)
2332{
2333 struct drm_backend *b = pending_state->backend;
2334 struct drm_output_state *output_state, *tmp;
2335 uint32_t *unused;
2336
Daniel Stone598ee9d2016-11-16 11:55:20 +00002337#ifdef HAVE_DRM_ATOMIC
2338 if (b->atomic_modeset)
2339 return drm_pending_state_apply_atomic(pending_state,
2340 DRM_STATE_APPLY_SYNC);
2341#endif
2342
Daniel Stonea08512f2016-11-08 17:46:10 +00002343 if (b->state_invalid) {
2344 /* If we need to reset all our state (e.g. because we've
2345 * just started, or just been VT-switched in), explicitly
2346 * disable all the CRTCs we aren't using. This also disables
2347 * all connectors on these CRTCs, so we don't need to do that
2348 * separately with the pre-atomic API. */
2349 wl_array_for_each(unused, &b->unused_crtcs)
2350 drmModeSetCrtc(b->drm.fd, *unused, 0, 0, 0, NULL, 0,
2351 NULL);
2352 }
2353
2354 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
2355 link) {
2356 int ret;
2357
2358 assert(output_state->dpms == WESTON_DPMS_OFF);
Daniel Stone598ee9d2016-11-16 11:55:20 +00002359 ret = drm_output_apply_state_legacy(output_state);
Daniel Stonea08512f2016-11-08 17:46:10 +00002360 if (ret != 0) {
2361 weston_log("Couldn't apply state for output %s\n",
2362 output_state->output->base.name);
2363 }
2364 }
2365
2366 b->state_invalid = false;
2367
2368 assert(wl_list_empty(&pending_state->output_list));
2369
2370 drm_pending_state_free(pending_state);
2371
2372 return 0;
2373}
2374
2375static int
2376drm_output_repaint(struct weston_output *output_base,
2377 pixman_region32_t *damage,
2378 void *repaint_data)
2379{
2380 struct drm_pending_state *pending_state = repaint_data;
2381 struct drm_output *output = to_drm_output(output_base);
Daniel Stonea08512f2016-11-08 17:46:10 +00002382 struct drm_output_state *state = NULL;
2383 struct drm_plane_state *scanout_state;
2384
2385 if (output->disable_pending || output->destroy_pending)
2386 goto err;
2387
2388 assert(!output->state_last);
2389
2390 /* If planes have been disabled in the core, we might not have
2391 * hit assign_planes at all, so might not have valid output state
2392 * here. */
2393 state = drm_pending_state_get_output(pending_state, output);
2394 if (!state)
2395 state = drm_output_state_duplicate(output->state_cur,
2396 pending_state,
2397 DRM_OUTPUT_STATE_CLEAR_PLANES);
2398 state->dpms = WESTON_DPMS_ON;
2399
2400 drm_output_render(state, damage);
2401 scanout_state = drm_output_state_get_plane(state,
2402 output->scanout_plane);
2403 if (!scanout_state || !scanout_state->fb)
2404 goto err;
2405
Daniel Stonea08512f2016-11-08 17:46:10 +00002406 return 0;
2407
2408err:
2409 drm_output_state_free(state);
David Herrmann1edf44c2013-10-22 17:11:26 +02002410 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002411}
2412
2413static void
Jonas Ådahle5a12252013-04-05 23:07:11 +02002414drm_output_start_repaint_loop(struct weston_output *output_base)
2415{
Armin Krezović545dba62016-08-05 15:54:18 +02002416 struct drm_output *output = to_drm_output(output_base);
Daniel Stone8747f952016-11-29 20:17:32 +00002417 struct drm_pending_state *pending_state;
Daniel Stonee2e80132018-01-16 15:37:33 +00002418 struct drm_plane *scanout_plane = output->scanout_plane;
Armin Krezović545dba62016-08-05 15:54:18 +02002419 struct drm_backend *backend =
2420 to_drm_backend(output_base->compositor);
Mario Kleinerf507ec32015-06-21 21:25:14 +02002421 struct timespec ts, tnow;
2422 struct timespec vbl2now;
2423 int64_t refresh_nsec;
2424 int ret;
2425 drmVBlank vbl = {
2426 .request.type = DRM_VBLANK_RELATIVE,
2427 .request.sequence = 0,
2428 .request.signal = 0,
2429 };
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03002430
Armin Krezović08368132016-09-30 14:11:05 +02002431 if (output->disable_pending || output->destroy_pending)
Xiong Zhangabd5d472013-10-11 14:43:07 +08002432 return;
2433
Daniel Stonee2e80132018-01-16 15:37:33 +00002434 if (!output->scanout_plane->state_cur->fb) {
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03002435 /* We can't page flip if there's no mode set */
David Herrmann3c688c52013-10-22 17:11:25 +02002436 goto finish_frame;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03002437 }
2438
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03002439 /* Need to smash all state in from scratch; current timings might not
2440 * be what we want, page flip might not work, etc.
2441 */
Daniel Stone6020f472018-02-05 15:46:20 +00002442 if (backend->state_invalid)
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03002443 goto finish_frame;
2444
Daniel Stonee2e80132018-01-16 15:37:33 +00002445 assert(scanout_plane->state_cur->output == output);
2446
Mario Kleinerf507ec32015-06-21 21:25:14 +02002447 /* Try to get current msc and timestamp via instant query */
2448 vbl.request.type |= drm_waitvblank_pipe(output);
2449 ret = drmWaitVBlank(backend->drm.fd, &vbl);
2450
2451 /* Error ret or zero timestamp means failure to get valid timestamp */
2452 if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
2453 ts.tv_sec = vbl.reply.tval_sec;
2454 ts.tv_nsec = vbl.reply.tval_usec * 1000;
2455
2456 /* Valid timestamp for most recent vblank - not stale?
2457 * Stale ts could happen on Linux 3.17+, so make sure it
2458 * is not older than 1 refresh duration since now.
2459 */
2460 weston_compositor_read_presentation_clock(backend->compositor,
2461 &tnow);
2462 timespec_sub(&vbl2now, &tnow, &ts);
2463 refresh_nsec =
2464 millihz_to_nsec(output->base.current_mode->refresh);
2465 if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
2466 drm_output_update_msc(output, vbl.reply.sequence);
2467 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002468 WP_PRESENTATION_FEEDBACK_INVALID);
Mario Kleinerf507ec32015-06-21 21:25:14 +02002469 return;
2470 }
2471 }
2472
2473 /* Immediate query didn't provide valid timestamp.
2474 * Use pageflip fallback.
2475 */
Jonas Ådahle5a12252013-04-05 23:07:11 +02002476
Daniel Stone205c0a02017-04-04 17:54:33 +01002477 assert(!output->page_flip_pending);
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002478 assert(!output->state_last);
2479
2480 pending_state = drm_pending_state_alloc(backend);
Daniel Stone8747f952016-11-29 20:17:32 +00002481 drm_output_state_duplicate(output->state_cur, pending_state,
2482 DRM_OUTPUT_STATE_PRESERVE_PLANES);
Daniel Stone205c0a02017-04-04 17:54:33 +01002483
Daniel Stone8747f952016-11-29 20:17:32 +00002484 ret = drm_pending_state_apply(pending_state);
2485 if (ret != 0) {
2486 weston_log("applying repaint-start state failed: %m\n");
David Herrmann3c688c52013-10-22 17:11:25 +02002487 goto finish_frame;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002488 }
David Herrmann3c688c52013-10-22 17:11:25 +02002489
2490 return;
2491
2492finish_frame:
2493 /* if we cannot page-flip, immediately finish frame */
Daniel Stone3615ce12017-03-01 11:34:05 +00002494 weston_output_finish_frame(output_base, NULL,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002495 WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002496}
2497
2498static void
Pekka Paalanen641307c2014-09-23 22:08:47 -04002499drm_output_update_msc(struct drm_output *output, unsigned int seq)
2500{
2501 uint64_t msc_hi = output->base.msc >> 32;
2502
2503 if (seq < (output->base.msc & 0xffffffff))
2504 msc_hi++;
2505
2506 output->base.msc = (msc_hi << 32) + seq;
2507}
2508
2509static void
Jesse Barnes58ef3792012-02-23 09:45:49 -05002510vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
2511 void *data)
2512{
Daniel Stonebc15f682016-11-14 16:57:01 +00002513 struct drm_plane_state *ps = (struct drm_plane_state *) data;
2514 struct drm_output_state *os = ps->output_state;
2515 struct drm_output *output = os->output;
Daniel Stone598ee9d2016-11-16 11:55:20 +00002516 struct drm_backend *b = to_drm_backend(output->base.compositor);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002517 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
2518 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03002519
Daniel Stone598ee9d2016-11-16 11:55:20 +00002520 assert(!b->atomic_modeset);
2521
Pekka Paalanen641307c2014-09-23 22:08:47 -04002522 drm_output_update_msc(output, frame);
Daniel Stone65d87d02017-04-04 17:54:32 +01002523 output->vblank_pending--;
2524 assert(output->vblank_pending >= 0);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002525
Daniel Stonebc15f682016-11-14 16:57:01 +00002526 assert(ps->fb);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03002527
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002528 if (output->page_flip_pending || output->vblank_pending)
2529 return;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00002530
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002531 drm_output_update_complete(output, flags, sec, usec);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002532}
2533
2534static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002535page_flip_handler(int fd, unsigned int frame,
2536 unsigned int sec, unsigned int usec, void *data)
2537{
Armin Krezović545dba62016-08-05 15:54:18 +02002538 struct drm_output *output = data;
Daniel Stone598ee9d2016-11-16 11:55:20 +00002539 struct drm_backend *b = to_drm_backend(output->base.compositor);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002540 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
2541 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
2542 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002543
Pekka Paalanen641307c2014-09-23 22:08:47 -04002544 drm_output_update_msc(output, frame);
2545
Daniel Stone598ee9d2016-11-16 11:55:20 +00002546 assert(!b->atomic_modeset);
Daniel Stone205c0a02017-04-04 17:54:33 +01002547 assert(output->page_flip_pending);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002548 output->page_flip_pending = 0;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002549
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002550 if (output->vblank_pending)
2551 return;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00002552
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002553 drm_output_update_complete(output, flags, sec, usec);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02002554}
2555
Daniel Stoneeedf84c2017-02-10 18:06:04 +00002556/**
2557 * Begin a new repaint cycle
2558 *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002559 * Called by the core compositor at the beginning of a repaint cycle. Creates
2560 * a new pending_state structure to own any output state created by individual
2561 * output repaint functions until the repaint is flushed or cancelled.
Daniel Stoneeedf84c2017-02-10 18:06:04 +00002562 */
2563static void *
2564drm_repaint_begin(struct weston_compositor *compositor)
2565{
2566 struct drm_backend *b = to_drm_backend(compositor);
2567 struct drm_pending_state *ret;
2568
2569 ret = drm_pending_state_alloc(b);
2570 b->repaint_data = ret;
2571
2572 return ret;
2573}
2574
2575/**
2576 * Flush a repaint set
2577 *
2578 * Called by the core compositor when a repaint cycle has been completed
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002579 * and should be flushed. Frees the pending state, transitioning ownership
2580 * of the output state from the pending state, to the update itself. When
2581 * the update completes (see drm_output_update_complete), the output
2582 * state will be freed.
Daniel Stoneeedf84c2017-02-10 18:06:04 +00002583 */
2584static void
2585drm_repaint_flush(struct weston_compositor *compositor, void *repaint_data)
2586{
2587 struct drm_backend *b = to_drm_backend(compositor);
2588 struct drm_pending_state *pending_state = repaint_data;
Daniel Stone6020f472018-02-05 15:46:20 +00002589
Daniel Stonea08512f2016-11-08 17:46:10 +00002590 drm_pending_state_apply(pending_state);
Daniel Stoneeedf84c2017-02-10 18:06:04 +00002591 b->repaint_data = NULL;
2592}
2593
2594/**
2595 * Cancel a repaint set
2596 *
2597 * Called by the core compositor when a repaint has finished, so the data
2598 * held across the repaint cycle should be discarded.
2599 */
2600static void
2601drm_repaint_cancel(struct weston_compositor *compositor, void *repaint_data)
2602{
2603 struct drm_backend *b = to_drm_backend(compositor);
2604 struct drm_pending_state *pending_state = repaint_data;
2605
2606 drm_pending_state_free(pending_state);
2607 b->repaint_data = NULL;
2608}
2609
Daniel Stone598ee9d2016-11-16 11:55:20 +00002610#ifdef HAVE_DRM_ATOMIC
2611static void
2612atomic_flip_handler(int fd, unsigned int frame, unsigned int sec,
2613 unsigned int usec, unsigned int crtc_id, void *data)
2614{
2615 struct drm_backend *b = data;
2616 struct drm_output *output = drm_output_find_by_crtc(b, crtc_id);
2617 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
2618 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
2619 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
2620
2621 /* During the initial modeset, we can disable CRTCs which we don't
2622 * actually handle during normal operation; this will give us events
2623 * for unknown outputs. Ignore them. */
2624 if (!output || !output->base.enabled)
2625 return;
2626
2627 drm_output_update_msc(output, frame);
2628
2629 assert(b->atomic_modeset);
2630 assert(output->atomic_complete_pending);
2631 output->atomic_complete_pending = 0;
2632
2633 drm_output_update_complete(output, flags, sec, usec);
2634}
2635#endif
2636
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05002637static uint32_t
Daniel Stone08d4edf2017-04-04 17:54:34 +01002638drm_output_check_plane_format(struct drm_plane *p,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002639 struct weston_view *ev, struct gbm_bo *bo)
Jesse Barnes58ef3792012-02-23 09:45:49 -05002640{
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05002641 uint32_t i, format;
2642
2643 format = gbm_bo_get_format(bo);
2644
2645 if (format == GBM_FORMAT_ARGB8888) {
2646 pixman_region32_t r;
2647
Kristian Høgsberg63093a32013-03-01 14:29:16 -05002648 pixman_region32_init_rect(&r, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002649 ev->surface->width,
2650 ev->surface->height);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002651 pixman_region32_subtract(&r, &r, &ev->surface->opaque);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05002652
2653 if (!pixman_region32_not_empty(&r))
2654 format = GBM_FORMAT_XRGB8888;
2655
2656 pixman_region32_fini(&r);
2657 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05002658
Daniel Stone08d4edf2017-04-04 17:54:34 +01002659 for (i = 0; i < p->count_formats; i++)
2660 if (p->formats[i] == format)
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05002661 return format;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002662
2663 return 0;
2664}
2665
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002666static struct weston_plane *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002667drm_output_prepare_overlay_view(struct drm_output_state *output_state,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002668 struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -05002669{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002670 struct drm_output *output = output_state->output;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02002671 struct weston_compositor *ec = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02002672 struct drm_backend *b = to_drm_backend(ec);
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002673 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002674 struct wl_resource *buffer_resource;
Daniel Stone08d4edf2017-04-04 17:54:34 +01002675 struct drm_plane *p;
Daniel Stonebc15f682016-11-14 16:57:01 +00002676 struct drm_plane_state *state = NULL;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002677 struct linux_dmabuf_buffer *dmabuf;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002678 struct gbm_bo *bo;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002679 pixman_region32_t dest_rect, src_rect;
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02002680 pixman_box32_t *box, tbox;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002681 uint32_t format;
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04002682 wl_fixed_t sx1, sy1, sx2, sy2;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002683
Giulio Camuffo954f1832014-10-11 18:27:30 +03002684 if (b->sprites_are_broken)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002685 return NULL;
Kristian Høgsberg65bec242012-03-05 19:57:35 -05002686
Daniel Stone296d7a92016-10-21 18:05:37 +01002687 /* Don't import buffers which span multiple outputs. */
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02002688 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002689 return NULL;
Ander Conselvan de Oliveirad450b192012-06-26 17:09:12 +03002690
Daniel Stone296d7a92016-10-21 18:05:37 +01002691 /* We can only import GBM buffers. */
2692 if (b->gbm == NULL)
2693 return NULL;
2694
Jason Ekstranda7af7042013-10-12 22:38:11 -05002695 if (ev->surface->buffer_ref.buffer == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002696 return NULL;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002697 buffer_resource = ev->surface->buffer_ref.buffer->resource;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002698 if (wl_shm_buffer_get(buffer_resource))
Rob Clark702ffae2012-08-09 14:18:27 -05002699 return NULL;
2700
Daniel Stone296d7a92016-10-21 18:05:37 +01002701 if (viewport->buffer.transform != output->base.transform)
2702 return NULL;
2703 if (viewport->buffer.scale != output->base.current_scale)
2704 return NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002705 if (!drm_view_transform_supported(ev))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002706 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002707
Daniel Stone296d7a92016-10-21 18:05:37 +01002708 if (ev->alpha != 1.0f)
2709 return NULL;
2710
Daniel Stone085d2b92015-05-21 00:00:57 +01002711 wl_list_for_each(p, &b->plane_list, link) {
2712 if (p->type != WDRM_PLANE_TYPE_OVERLAY)
2713 continue;
2714
Daniel Stone5ff289a2017-10-07 12:59:02 +01002715 if (!drm_plane_is_available(p, output))
Daniel Stonebc15f682016-11-14 16:57:01 +00002716 continue;
2717
2718 state = drm_output_state_get_plane(output_state, p);
2719 if (state->fb) {
2720 state = NULL;
2721 continue;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002722 }
Daniel Stonebc15f682016-11-14 16:57:01 +00002723
2724 break;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002725 }
2726
2727 /* No sprites available */
Daniel Stonebc15f682016-11-14 16:57:01 +00002728 if (!state)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002729 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002730
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002731 if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) {
Bryce Harringtona3582072015-08-14 12:23:13 -07002732#ifdef HAVE_GBM_FD_IMPORT
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002733 /* XXX: TODO:
2734 *
2735 * Use AddFB2 directly, do not go via GBM.
2736 * Add support for multiplanar formats.
2737 * Both require refactoring in the DRM-backend to
2738 * support a mix of gbm_bos and drmfbs.
2739 */
2740 struct gbm_import_fd_data gbm_dmabuf = {
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00002741 .fd = dmabuf->attributes.fd[0],
2742 .width = dmabuf->attributes.width,
2743 .height = dmabuf->attributes.height,
2744 .stride = dmabuf->attributes.stride[0],
2745 .format = dmabuf->attributes.format
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002746 };
2747
Micah Fedkec8890122017-02-01 15:28:23 -05002748 /* XXX: TODO:
2749 *
2750 * Currently the buffer is rejected if any dmabuf attribute
2751 * flag is set. This keeps us from passing an inverted /
2752 * interlaced / bottom-first buffer (or any other type that may
2753 * be added in the future) through to an overlay. Ultimately,
2754 * these types of buffers should be handled through buffer
2755 * transforms and not as spot-checks requiring specific
2756 * knowledge. */
2757 if (dmabuf->attributes.n_planes != 1 ||
2758 dmabuf->attributes.offset[0] != 0 ||
2759 dmabuf->attributes.flags)
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002760 return NULL;
2761
2762 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf,
2763 GBM_BO_USE_SCANOUT);
Bryce Harringtona3582072015-08-14 12:23:13 -07002764#else
Daniel Stonebc15f682016-11-14 16:57:01 +00002765 goto err;
Bryce Harringtona3582072015-08-14 12:23:13 -07002766#endif
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002767 } else {
2768 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
2769 buffer_resource, GBM_BO_USE_SCANOUT);
2770 }
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04002771 if (!bo)
Daniel Stonebc15f682016-11-14 16:57:01 +00002772 goto err;
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04002773
Daniel Stone08d4edf2017-04-04 17:54:34 +01002774 format = drm_output_check_plane_format(p, ev, bo);
Daniel Stonebc15f682016-11-14 16:57:01 +00002775 if (format == 0)
2776 goto err;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002777
Daniel Stonebc15f682016-11-14 16:57:01 +00002778 state->fb = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT);
2779 if (!state->fb)
2780 goto err;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02002781
Daniel Stonebc15f682016-11-14 16:57:01 +00002782 drm_fb_set_buffer(state->fb, ev->surface->buffer_ref.buffer);
2783
2784 state->output = output;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002785
Jason Ekstranda7af7042013-10-12 22:38:11 -05002786 box = pixman_region32_extents(&ev->transform.boundingbox);
Daniel Stone08d4edf2017-04-04 17:54:34 +01002787 p->base.x = box->x1;
2788 p->base.y = box->y1;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002789
Jesse Barnes58ef3792012-02-23 09:45:49 -05002790 /*
2791 * Calculate the source & dest rects properly based on actual
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002792 * position (note the caller has called weston_view_update_transform()
Jesse Barnes58ef3792012-02-23 09:45:49 -05002793 * for us already).
2794 */
2795 pixman_region32_init(&dest_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002796 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02002797 &output->base.region);
2798 pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002799 box = pixman_region32_extents(&dest_rect);
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02002800 tbox = weston_transformed_rect(output->base.width,
2801 output->base.height,
2802 output->base.transform,
2803 output->base.current_scale,
Alexander Larssond9a7bb72013-05-22 14:41:39 +02002804 *box);
Daniel Stonebc15f682016-11-14 16:57:01 +00002805 state->dest_x = tbox.x1;
2806 state->dest_y = tbox.y1;
2807 state->dest_w = tbox.x2 - tbox.x1;
2808 state->dest_h = tbox.y2 - tbox.y1;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002809 pixman_region32_fini(&dest_rect);
2810
2811 pixman_region32_init(&src_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002812 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02002813 &output->base.region);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002814 box = pixman_region32_extents(&src_rect);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04002815
Jason Ekstranda7af7042013-10-12 22:38:11 -05002816 weston_view_from_global_fixed(ev,
2817 wl_fixed_from_int(box->x1),
2818 wl_fixed_from_int(box->y1),
2819 &sx1, &sy1);
2820 weston_view_from_global_fixed(ev,
2821 wl_fixed_from_int(box->x2),
2822 wl_fixed_from_int(box->y2),
2823 &sx2, &sy2);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04002824
2825 if (sx1 < 0)
2826 sx1 = 0;
2827 if (sy1 < 0)
2828 sy1 = 0;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002829 if (sx2 > wl_fixed_from_int(ev->surface->width))
2830 sx2 = wl_fixed_from_int(ev->surface->width);
2831 if (sy2 > wl_fixed_from_int(ev->surface->height))
2832 sy2 = wl_fixed_from_int(ev->surface->height);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04002833
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02002834 tbox.x1 = sx1;
2835 tbox.y1 = sy1;
2836 tbox.x2 = sx2;
2837 tbox.y2 = sy2;
2838
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002839 tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width),
2840 wl_fixed_from_int(ev->surface->height),
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002841 viewport->buffer.transform,
2842 viewport->buffer.scale,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002843 tbox);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02002844
Daniel Stonebc15f682016-11-14 16:57:01 +00002845 state->src_x = tbox.x1 << 8;
2846 state->src_y = tbox.y1 << 8;
2847 state->src_w = (tbox.x2 - tbox.x1) << 8;
2848 state->src_h = (tbox.y2 - tbox.y1) << 8;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002849 pixman_region32_fini(&src_rect);
2850
Daniel Stone08d4edf2017-04-04 17:54:34 +01002851 return &p->base;
Daniel Stonebc15f682016-11-14 16:57:01 +00002852
2853err:
2854 drm_plane_state_put_back(state);
2855 if (bo)
2856 gbm_bo_destroy(bo);
2857 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002858}
2859
Pekka Paalanend0ead482014-06-16 12:05:40 +03002860/**
2861 * Update the image for the current cursor surface
2862 *
2863 * @param b DRM backend structure
2864 * @param bo GBM buffer object to write into
2865 * @param ev View to use for cursor image
2866 */
2867static void
2868cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo,
2869 struct weston_view *ev)
2870{
2871 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
2872 uint32_t buf[b->cursor_width * b->cursor_height];
2873 int32_t stride;
2874 uint8_t *s;
2875 int i;
2876
2877 assert(buffer && buffer->shm_buffer);
2878 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
2879 assert(ev->surface->width <= b->cursor_width);
2880 assert(ev->surface->height <= b->cursor_height);
2881
2882 memset(buf, 0, sizeof buf);
2883 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
2884 s = wl_shm_buffer_get_data(buffer->shm_buffer);
2885
2886 wl_shm_buffer_begin_access(buffer->shm_buffer);
2887 for (i = 0; i < ev->surface->height; i++)
2888 memcpy(buf + i * b->cursor_width,
2889 s + i * stride,
2890 ev->surface->width * 4);
2891 wl_shm_buffer_end_access(buffer->shm_buffer);
2892
2893 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
2894 weston_log("failed update cursor: %m\n");
2895}
2896
Daniel Stone2ba17f42015-05-19 20:02:41 +01002897static struct weston_plane *
2898drm_output_prepare_cursor_view(struct drm_output_state *output_state,
2899 struct weston_view *ev)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04002900{
Daniel Stone2ba17f42015-05-19 20:02:41 +01002901 struct drm_output *output = output_state->output;
Armin Krezović545dba62016-08-05 15:54:18 +02002902 struct drm_backend *b = to_drm_backend(output->base.compositor);
Daniel Stone2ba17f42015-05-19 20:02:41 +01002903 struct drm_plane *plane = output->cursor_plane;
2904 struct drm_plane_state *plane_state;
2905 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
2906 struct wl_shm_buffer *shmbuf;
2907 bool needs_update = false;
Derek Foremanbe428b32015-11-24 11:39:38 -06002908 float x, y;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05002909
Daniel Stone2ba17f42015-05-19 20:02:41 +01002910 if (!plane)
2911 return NULL;
2912
2913 if (b->cursors_are_broken)
2914 return NULL;
2915
2916 if (!plane->state_cur->complete)
2917 return NULL;
2918
2919 if (plane->state_cur->output && plane->state_cur->output != output)
2920 return NULL;
2921
2922 /* Don't import buffers which span multiple outputs. */
2923 if (ev->output_mask != (1u << output->base.id))
2924 return NULL;
2925
2926 /* We use GBM to import SHM buffers. */
2927 if (b->gbm == NULL)
2928 return NULL;
2929
2930 if (ev->surface->buffer_ref.buffer == NULL)
2931 return NULL;
2932 shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource);
2933 if (!shmbuf)
2934 return NULL;
2935 if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888)
2936 return NULL;
2937
2938 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
2939 return NULL;
2940 if (ev->transform.enabled &&
2941 (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE))
2942 return NULL;
2943 if (viewport->buffer.scale != output->base.current_scale)
2944 return NULL;
2945 if (ev->geometry.scissor_enabled)
2946 return NULL;
2947
2948 if (ev->surface->width > b->cursor_width ||
2949 ev->surface->height > b->cursor_height)
2950 return NULL;
2951
2952 plane_state =
2953 drm_output_state_get_plane(output_state, output->cursor_plane);
2954
2955 if (plane_state && plane_state->fb)
2956 return NULL;
2957
2958 /* Since we're setting plane state up front, we need to work out
2959 * whether or not we need to upload a new cursor. We can't use the
2960 * plane damage, since the planes haven't actually been calculated
2961 * yet: instead try to figure it out directly. KMS cursor planes are
2962 * pretty unique here, in that they lie partway between a Weston plane
2963 * (direct scanout) and a renderer. */
2964 if (ev != output->cursor_view ||
2965 pixman_region32_not_empty(&ev->surface->damage)) {
2966 output->current_cursor++;
2967 output->current_cursor =
2968 output->current_cursor %
2969 ARRAY_LENGTH(output->gbm_cursor_fb);
2970 needs_update = true;
2971 }
2972
2973 output->cursor_view = ev;
2974 weston_view_to_global_float(ev, 0, 0, &x, &y);
2975 plane->base.x = x;
2976 plane->base.y = y;
2977
2978 plane_state->fb =
2979 drm_fb_ref(output->gbm_cursor_fb[output->current_cursor]);
2980 plane_state->output = output;
2981 plane_state->src_x = 0;
2982 plane_state->src_y = 0;
2983 plane_state->src_w = b->cursor_width << 16;
2984 plane_state->src_h = b->cursor_height << 16;
2985 plane_state->dest_x = (x - output->base.x) * output->base.current_scale;
2986 plane_state->dest_y = (y - output->base.y) * output->base.current_scale;
2987 plane_state->dest_w = b->cursor_width;
2988 plane_state->dest_h = b->cursor_height;
2989
2990 if (needs_update)
2991 cursor_bo_update(b, plane_state->fb->bo, ev);
2992
2993 return &plane->base;
2994}
2995
2996static void
2997drm_output_set_cursor(struct drm_output_state *output_state)
2998{
2999 struct drm_output *output = output_state->output;
3000 struct drm_backend *b = to_drm_backend(output->base.compositor);
3001 struct drm_plane *plane = output->cursor_plane;
3002 struct drm_plane_state *state;
3003 EGLint handle;
3004 struct gbm_bo *bo;
3005
3006 if (!plane)
3007 return;
3008
3009 state = drm_output_state_get_existing_plane(output_state, plane);
3010 if (!state)
3011 return;
3012
3013 if (!state->fb) {
3014 pixman_region32_fini(&plane->base.damage);
3015 pixman_region32_init(&plane->base.damage);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003016 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg5626d342012-08-03 11:50:05 -04003017 return;
3018 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05003019
Daniel Stone2ba17f42015-05-19 20:02:41 +01003020 assert(state->fb == output->gbm_cursor_fb[output->current_cursor]);
3021 assert(!plane->state_cur->output || plane->state_cur->output == output);
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05003022
Daniel Stone2ba17f42015-05-19 20:02:41 +01003023 if (plane->state_cur->fb != state->fb) {
3024 bo = state->fb->bo;
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04003025 handle = gbm_bo_get_handle(bo).s32;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003026 if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle,
Daniel Stone2ba17f42015-05-19 20:02:41 +01003027 b->cursor_width, b->cursor_height)) {
Pekka Paalanenae29da22012-08-06 14:57:05 +03003028 weston_log("failed to set cursor: %m\n");
Daniel Stone2ba17f42015-05-19 20:02:41 +01003029 goto err;
Rob Clarkab5b1e32012-08-09 13:24:45 -05003030 }
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003031 }
3032
Daniel Stone2ba17f42015-05-19 20:02:41 +01003033 pixman_region32_fini(&plane->base.damage);
3034 pixman_region32_init(&plane->base.damage);
Pekka Paalanen7eaed402015-11-27 14:20:58 +02003035
Daniel Stone2ba17f42015-05-19 20:02:41 +01003036 if (drmModeMoveCursor(b->drm.fd, output->crtc_id,
3037 state->dest_x, state->dest_y)) {
Daniel Stonea7cba1d2017-04-04 17:54:21 +01003038 weston_log("failed to move cursor: %m\n");
Daniel Stone2ba17f42015-05-19 20:02:41 +01003039 goto err;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003040 }
Daniel Stone2ba17f42015-05-19 20:02:41 +01003041
3042 return;
3043
3044err:
3045 b->cursors_are_broken = 1;
3046 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05003047}
3048
Jesse Barnes58ef3792012-02-23 09:45:49 -05003049static void
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003050drm_assign_planes(struct weston_output *output_base, void *repaint_data)
Jesse Barnes58ef3792012-02-23 09:45:49 -05003051{
Armin Krezović545dba62016-08-05 15:54:18 +02003052 struct drm_backend *b = to_drm_backend(output_base->compositor);
Daniel Stone7b2ddac2016-11-11 19:11:49 +00003053 struct drm_pending_state *pending_state = repaint_data;
Armin Krezović545dba62016-08-05 15:54:18 +02003054 struct drm_output *output = to_drm_output(output_base);
Daniel Stone7b2ddac2016-11-11 19:11:49 +00003055 struct drm_output_state *state;
Daniel Stone2ba17f42015-05-19 20:02:41 +01003056 struct drm_plane_state *plane_state;
Daniel Stonec3fcb5b2016-12-01 12:33:54 +00003057 struct weston_view *ev;
Daniel Stone115ed2c2016-11-08 21:19:22 +00003058 pixman_region32_t surface_overlap, renderer_region;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003059 struct weston_plane *primary, *next_plane;
Matt Hoosierdf573032017-08-24 09:24:20 -05003060 bool picked_scanout = false;
Jesse Barnes58ef3792012-02-23 09:45:49 -05003061
Daniel Stone7b2ddac2016-11-11 19:11:49 +00003062 assert(!output->state_last);
3063 state = drm_output_state_duplicate(output->state_cur,
3064 pending_state,
3065 DRM_OUTPUT_STATE_CLEAR_PLANES);
3066
Jesse Barnes58ef3792012-02-23 09:45:49 -05003067 /*
3068 * Find a surface for each sprite in the output using some heuristics:
3069 * 1) size
3070 * 2) frequency of update
3071 * 3) opacity (though some hw might support alpha blending)
3072 * 4) clipping (this can be fixed with color keys)
3073 *
3074 * The idea is to save on blitting since this should save power.
3075 * If we can get a large video surface on the sprite for example,
3076 * the main display surface may not need to update at all, and
3077 * the client buffer can be used directly for the sprite surface
3078 * as we do for flipping full screen surfaces.
3079 */
Daniel Stone115ed2c2016-11-08 21:19:22 +00003080 pixman_region32_init(&renderer_region);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003081 primary = &output_base->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003082
Daniel Stonec3fcb5b2016-12-01 12:33:54 +00003083 wl_list_for_each(ev, &output_base->compositor->view_list, link) {
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02003084 struct weston_surface *es = ev->surface;
3085
3086 /* Test whether this buffer can ever go into a plane:
3087 * non-shm, or small enough to be a cursor.
3088 *
3089 * Also, keep a reference when using the pixman renderer.
3090 * That makes it possible to do a seamless switch to the GL
3091 * renderer and since the pixman renderer keeps a reference
3092 * to the buffer anyway, there is no side effects.
Pekka Paalanenccfeae22012-12-04 15:58:14 +02003093 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003094 if (b->use_pixman ||
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02003095 (es->buffer_ref.buffer &&
3096 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
Derek Foreman87430472015-10-15 10:24:48 -05003097 (ev->surface->width <= b->cursor_width &&
3098 ev->surface->height <= b->cursor_height))))
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05003099 es->keep_buffer = true;
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02003100 else
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05003101 es->keep_buffer = false;
Pekka Paalanenccfeae22012-12-04 15:58:14 +02003102
Jesse Barnes58ef3792012-02-23 09:45:49 -05003103 pixman_region32_init(&surface_overlap);
Daniel Stone115ed2c2016-11-08 21:19:22 +00003104 pixman_region32_intersect(&surface_overlap, &renderer_region,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003105 &ev->transform.boundingbox);
Jesse Barnes58ef3792012-02-23 09:45:49 -05003106
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003107 next_plane = NULL;
Matt Hoosierdf573032017-08-24 09:24:20 -05003108 if (pixman_region32_not_empty(&surface_overlap) || picked_scanout)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003109 next_plane = primary;
3110 if (next_plane == NULL)
Daniel Stone7b2ddac2016-11-11 19:11:49 +00003111 next_plane = drm_output_prepare_cursor_view(state, ev);
Matt Hoosierdf573032017-08-24 09:24:20 -05003112
3113 /* If a higher-stacked view already got assigned to scanout, it's incorrect to
3114 * assign a subsequent (lower-stacked) view to scanout.
3115 */
3116 if (next_plane == NULL) {
Daniel Stone7b2ddac2016-11-11 19:11:49 +00003117 next_plane = drm_output_prepare_scanout_view(state, ev);
Matt Hoosierdf573032017-08-24 09:24:20 -05003118 if (next_plane)
3119 picked_scanout = true;
3120 }
3121
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003122 if (next_plane == NULL)
Daniel Stone7b2ddac2016-11-11 19:11:49 +00003123 next_plane = drm_output_prepare_overlay_view(state, ev);
Matt Hoosierdf573032017-08-24 09:24:20 -05003124
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003125 if (next_plane == NULL)
3126 next_plane = primary;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02003127
Jason Ekstranda7af7042013-10-12 22:38:11 -05003128 weston_view_move_to_plane(ev, next_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02003129
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003130 if (next_plane == primary)
Daniel Stone115ed2c2016-11-08 21:19:22 +00003131 pixman_region32_union(&renderer_region,
3132 &renderer_region,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003133 &ev->transform.boundingbox);
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04003134
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02003135 if (next_plane == primary ||
Daniel Stone2ba17f42015-05-19 20:02:41 +01003136 (output->cursor_plane &&
3137 next_plane == &output->cursor_plane->base)) {
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02003138 /* cursor plane involves a copy */
3139 ev->psf_flags = 0;
3140 } else {
3141 /* All other planes are a direct scanout of a
3142 * single client buffer.
3143 */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02003144 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02003145 }
3146
Jesse Barnes58ef3792012-02-23 09:45:49 -05003147 pixman_region32_fini(&surface_overlap);
3148 }
Daniel Stone115ed2c2016-11-08 21:19:22 +00003149 pixman_region32_fini(&renderer_region);
Daniel Stone2ba17f42015-05-19 20:02:41 +01003150
3151 /* We rely on output->cursor_view being both an accurate reflection of
3152 * the cursor plane's state, but also being maintained across repaints
3153 * to avoid unnecessary damage uploads, per the comment in
3154 * drm_output_prepare_cursor_view. In the event that we go from having
3155 * a cursor view to not having a cursor view, we need to clear it. */
3156 if (output->cursor_view) {
3157 plane_state =
3158 drm_output_state_get_existing_plane(state,
3159 output->cursor_plane);
3160 if (!plane_state || !plane_state->fb)
3161 output->cursor_view = NULL;
3162 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05003163}
3164
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003165/**
3166 * Find the closest-matching mode for a given target
3167 *
3168 * Given a target mode, find the most suitable mode amongst the output's
3169 * current mode list to use, preferring the current mode if possible, to
3170 * avoid an expensive mode switch.
3171 *
3172 * @param output DRM output
3173 * @param target_mode Mode to attempt to match
3174 * @returns Pointer to a mode from the output's mode list
3175 */
Alex Wub7b8bda2012-04-17 17:20:48 +08003176static struct drm_mode *
3177choose_mode (struct drm_output *output, struct weston_mode *target_mode)
3178{
3179 struct drm_mode *tmp_mode = NULL, *mode;
3180
Hardeningff39efa2013-09-18 23:56:35 +02003181 if (output->base.current_mode->width == target_mode->width &&
3182 output->base.current_mode->height == target_mode->height &&
3183 (output->base.current_mode->refresh == target_mode->refresh ||
Alex Wub7b8bda2012-04-17 17:20:48 +08003184 target_mode->refresh == 0))
Daniel Stonecb04cc42016-11-16 11:51:27 +00003185 return to_drm_mode(output->base.current_mode);
Alex Wub7b8bda2012-04-17 17:20:48 +08003186
3187 wl_list_for_each(mode, &output->base.mode_list, base.link) {
3188 if (mode->mode_info.hdisplay == target_mode->width &&
3189 mode->mode_info.vdisplay == target_mode->height) {
Mario Kleiner872797c2015-06-21 21:25:09 +02003190 if (mode->base.refresh == target_mode->refresh ||
3191 target_mode->refresh == 0) {
Alex Wub7b8bda2012-04-17 17:20:48 +08003192 return mode;
Daniel Stonef556ebe2015-05-21 08:28:58 +01003193 } else if (!tmp_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08003194 tmp_mode = mode;
3195 }
3196 }
3197
3198 return tmp_mode;
3199}
3200
3201static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003202drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00003203static void
3204drm_output_fini_egl(struct drm_output *output);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003205static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003206drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00003207static void
3208drm_output_fini_pixman(struct drm_output *output);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02003209
3210static int
Alex Wub7b8bda2012-04-17 17:20:48 +08003211drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
3212{
Daniel Stone02d487a2017-10-07 14:01:45 +01003213 struct drm_output *output = to_drm_output(output_base);
3214 struct drm_backend *b = to_drm_backend(output_base->compositor);
3215 struct drm_mode *drm_mode = choose_mode(output, mode);
Alex Wub7b8bda2012-04-17 17:20:48 +08003216
3217 if (!drm_mode) {
Daniel Stone02d487a2017-10-07 14:01:45 +01003218 weston_log("%s: invalid resolution %dx%d\n",
3219 output_base->name, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +08003220 return -1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02003221 }
3222
Hardeningff39efa2013-09-18 23:56:35 +02003223 if (&drm_mode->base == output->base.current_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08003224 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08003225
Hardeningff39efa2013-09-18 23:56:35 +02003226 output->base.current_mode->flags = 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08003227
Hardeningff39efa2013-09-18 23:56:35 +02003228 output->base.current_mode = &drm_mode->base;
3229 output->base.current_mode->flags =
Alex Wub7b8bda2012-04-17 17:20:48 +08003230 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
3231
Daniel Stonef30a18c2017-04-04 17:54:31 +01003232 /* XXX: This drops our current buffer too early, before we've started
3233 * displaying it. Ideally this should be much more atomic and
3234 * integrated with a full repaint cycle, rather than doing a
3235 * sledgehammer modeswitch first, and only later showing new
3236 * content.
3237 */
Daniel Stone6020f472018-02-05 15:46:20 +00003238 b->state_invalid = true;
Alex Wub7b8bda2012-04-17 17:20:48 +08003239
Giulio Camuffo954f1832014-10-11 18:27:30 +03003240 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003241 drm_output_fini_pixman(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003242 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003243 weston_log("failed to init output pixman state with "
3244 "new mode\n");
3245 return -1;
3246 }
3247 } else {
Daniel Stone3e661f72016-11-04 17:24:06 +00003248 drm_output_fini_egl(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003249 if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003250 weston_log("failed to init output egl state with "
3251 "new mode");
3252 return -1;
3253 }
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02003254 }
3255
Alex Wub7b8bda2012-04-17 17:20:48 +08003256 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08003257}
3258
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003259static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003260on_drm_input(int fd, uint32_t mask, void *data)
3261{
Daniel Stone598ee9d2016-11-16 11:55:20 +00003262#ifdef HAVE_DRM_ATOMIC
3263 struct drm_backend *b = data;
3264#endif
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003265 drmEventContext evctx;
3266
3267 memset(&evctx, 0, sizeof evctx);
Daniel Stone598ee9d2016-11-16 11:55:20 +00003268#ifndef HAVE_DRM_ATOMIC
Emil Velikov863e66b2017-04-04 18:07:34 +01003269 evctx.version = 2;
Daniel Stone598ee9d2016-11-16 11:55:20 +00003270#else
3271 evctx.version = 3;
3272 if (b->atomic_modeset)
3273 evctx.page_flip_handler2 = atomic_flip_handler;
3274 else
3275#endif
3276 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -05003277 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003278 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003279
3280 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003281}
3282
3283static int
Daniel Stoneefa504f2016-12-19 16:48:20 +00003284init_kms_caps(struct drm_backend *b)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003285{
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03003286 uint64_t cap;
Daniel Stoneefa504f2016-12-19 16:48:20 +00003287 int ret;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003288 clockid_t clk_id;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04003289
Daniel Stoneefa504f2016-12-19 16:48:20 +00003290 weston_log("using %s\n", b->drm.filename);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003291
Daniel Stoneefa504f2016-12-19 16:48:20 +00003292 ret = drmGetCap(b->drm.fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03003293 if (ret == 0 && cap == 1)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003294 clk_id = CLOCK_MONOTONIC;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03003295 else
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003296 clk_id = CLOCK_REALTIME;
3297
Giulio Camuffo954f1832014-10-11 18:27:30 +03003298 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003299 weston_log("Error: failed to set presentation clock %d.\n",
3300 clk_id);
3301 return -1;
3302 }
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +02003303
Daniel Stoneefa504f2016-12-19 16:48:20 +00003304 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_WIDTH, &cap);
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03003305 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03003306 b->cursor_width = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03003307 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03003308 b->cursor_width = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03003309
Daniel Stoneefa504f2016-12-19 16:48:20 +00003310 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_HEIGHT, &cap);
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03003311 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03003312 b->cursor_height = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03003313 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03003314 b->cursor_height = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03003315
Daniel Stonebe1090b2017-09-06 17:29:57 +01003316 if (!getenv("WESTON_DISABLE_UNIVERSAL_PLANES")) {
3317 ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
3318 b->universal_planes = (ret == 0);
3319 }
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01003320 weston_log("DRM: %s universal planes\n",
3321 b->universal_planes ? "supports" : "does not support");
3322
Pekka Paalanencd011a62016-11-15 22:07:49 +00003323#ifdef HAVE_DRM_ATOMIC
3324 if (b->universal_planes && !getenv("WESTON_DISABLE_ATOMIC")) {
Daniel Stone598ee9d2016-11-16 11:55:20 +00003325 ret = drmGetCap(b->drm.fd, DRM_CAP_CRTC_IN_VBLANK_EVENT, &cap);
3326 if (ret != 0)
3327 cap = 0;
Pekka Paalanencd011a62016-11-15 22:07:49 +00003328 ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_ATOMIC, 1);
Daniel Stone598ee9d2016-11-16 11:55:20 +00003329 b->atomic_modeset = ((ret == 0) && (cap == 1));
Pekka Paalanencd011a62016-11-15 22:07:49 +00003330 }
3331#endif
3332 weston_log("DRM: %s atomic modesetting\n",
3333 b->atomic_modeset ? "supports" : "does not support");
3334
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003335 return 0;
3336}
3337
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003338static struct gbm_device *
3339create_gbm_device(int fd)
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003340{
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003341 struct gbm_device *gbm;
Alexandru DAMIANbe0ac5b2013-10-02 17:51:05 +01003342
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003343 gl_renderer = weston_load_module("gl-renderer.so",
3344 "gl_renderer_interface");
3345 if (!gl_renderer)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003346 return NULL;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003347
3348 /* GBM will load a dri driver, but even though they need symbols from
3349 * libglapi, in some version of Mesa they are not linked to it. Since
3350 * only the gl-renderer module links to it, the call above won't make
3351 * these symbols globally available, and loading the DRI driver fails.
3352 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
3353 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
3354
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003355 gbm = gbm_create_device(fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003356
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003357 return gbm;
3358}
3359
Bryce Harringtonc056a982015-05-19 15:25:18 -07003360/* When initializing EGL, if the preferred buffer format isn't available
Bryce Harringtonb9939982016-04-15 20:28:26 -07003361 * we may be able to substitute an ARGB format for an XRGB one.
Derek Foremanc4cfe852015-05-15 12:12:40 -05003362 *
3363 * This returns 0 if substitution isn't possible, but 0 might be a
3364 * legitimate format for other EGL platforms, so the caller is
3365 * responsible for checking for 0 before calling gl_renderer->create().
3366 *
3367 * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689
3368 * but it's entirely possible we'll see this again on other implementations.
3369 */
3370static int
3371fallback_format_for(uint32_t format)
3372{
3373 switch (format) {
3374 case GBM_FORMAT_XRGB8888:
3375 return GBM_FORMAT_ARGB8888;
3376 case GBM_FORMAT_XRGB2101010:
3377 return GBM_FORMAT_ARGB2101010;
3378 default:
3379 return 0;
3380 }
3381}
3382
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003383static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003384drm_backend_create_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003385{
Derek Foreman6d556372015-11-04 14:47:33 -06003386 EGLint format[3] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01003387 b->gbm_format,
3388 fallback_format_for(b->gbm_format),
Derek Foreman6d556372015-11-04 14:47:33 -06003389 0,
Derek Foremanc4cfe852015-05-15 12:12:40 -05003390 };
Derek Foreman6d556372015-11-04 14:47:33 -06003391 int n_formats = 2;
John Kåre Alsakeref591aa2013-03-02 12:27:39 +01003392
Derek Foremanc4cfe852015-05-15 12:12:40 -05003393 if (format[1])
Derek Foreman6d556372015-11-04 14:47:33 -06003394 n_formats = 3;
Miguel A. Vicodddc6702016-05-18 17:41:07 +02003395 if (gl_renderer->display_create(b->compositor,
3396 EGL_PLATFORM_GBM_KHR,
3397 (void *)b->gbm,
Miguel A. Vico41700e32016-05-18 17:47:59 +02003398 NULL,
Miguel A. Vicodddc6702016-05-18 17:41:07 +02003399 gl_renderer->opaque_attribs,
3400 format,
3401 n_formats) < 0) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003402 return -1;
3403 }
3404
3405 return 0;
3406}
3407
3408static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003409init_egl(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003410{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003411 b->gbm = create_gbm_device(b->drm.fd);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003412
Giulio Camuffo954f1832014-10-11 18:27:30 +03003413 if (!b->gbm)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003414 return -1;
3415
Giulio Camuffo954f1832014-10-11 18:27:30 +03003416 if (drm_backend_create_gl_renderer(b) < 0) {
3417 gbm_device_destroy(b->gbm);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04003418 return -1;
3419 }
3420
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003421 return 0;
3422}
3423
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003424static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003425init_pixman(struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003426{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003427 return pixman_renderer_init(b->compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003428}
3429
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003430/**
Pekka Paalanenec272712014-06-05 11:22:25 +03003431 * Create a drm_plane for a hardware plane
3432 *
3433 * Creates one drm_plane structure for a hardware plane, and initialises its
3434 * properties and formats.
3435 *
Daniel Stone2ba17f42015-05-19 20:02:41 +01003436 * In the absence of universal plane support, where KMS does not explicitly
3437 * expose the primary and cursor planes to userspace, this may also create
3438 * an 'internal' plane for internal management.
3439 *
Pekka Paalanenec272712014-06-05 11:22:25 +03003440 * This function does not add the plane to the list of usable planes in Weston
3441 * itself; the caller is responsible for this.
3442 *
3443 * Call drm_plane_destroy to clean up the plane.
3444 *
Daniel Stone2ba17f42015-05-19 20:02:41 +01003445 * @sa drm_output_find_special_plane
Pekka Paalanenec272712014-06-05 11:22:25 +03003446 * @param b DRM compositor backend
Daniel Stone2ba17f42015-05-19 20:02:41 +01003447 * @param kplane DRM plane to create, or NULL if creating internal plane
3448 * @param output Output to create internal plane for, or NULL
3449 * @param type Type to use when creating internal plane, or invalid
3450 * @param format Format to use for internal planes, or 0
Pekka Paalanenec272712014-06-05 11:22:25 +03003451 */
3452static struct drm_plane *
Daniel Stone2ba17f42015-05-19 20:02:41 +01003453drm_plane_create(struct drm_backend *b, const drmModePlane *kplane,
3454 struct drm_output *output, enum wdrm_plane_type type,
3455 uint32_t format)
Pekka Paalanenec272712014-06-05 11:22:25 +03003456{
3457 struct drm_plane *plane;
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01003458 drmModeObjectProperties *props;
Daniel Stone2ba17f42015-05-19 20:02:41 +01003459 int num_formats = (kplane) ? kplane->count_formats : 1;
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01003460
Daniel Stone2ba17f42015-05-19 20:02:41 +01003461 plane = zalloc(sizeof(*plane) +
3462 (sizeof(uint32_t) * num_formats));
Pekka Paalanenec272712014-06-05 11:22:25 +03003463 if (!plane) {
3464 weston_log("%s: out of memory\n", __func__);
3465 return NULL;
3466 }
3467
3468 plane->backend = b;
Daniel Stonebc15f682016-11-14 16:57:01 +00003469 plane->state_cur = drm_plane_state_alloc(NULL, plane);
3470 plane->state_cur->complete = true;
Pekka Paalanenec272712014-06-05 11:22:25 +03003471
Daniel Stone2ba17f42015-05-19 20:02:41 +01003472 if (kplane) {
3473 plane->possible_crtcs = kplane->possible_crtcs;
3474 plane->plane_id = kplane->plane_id;
3475 plane->count_formats = kplane->count_formats;
3476 memcpy(plane->formats, kplane->formats,
3477 kplane->count_formats * sizeof(kplane->formats[0]));
3478
3479 props = drmModeObjectGetProperties(b->drm.fd, kplane->plane_id,
3480 DRM_MODE_OBJECT_PLANE);
3481 if (!props) {
3482 weston_log("couldn't get plane properties\n");
3483 goto err;
3484 }
3485 drm_property_info_populate(b, plane_props, plane->props,
3486 WDRM_PLANE__COUNT, props);
3487 plane->type =
3488 drm_property_get_value(&plane->props[WDRM_PLANE_TYPE],
3489 props,
3490 WDRM_PLANE_TYPE__COUNT);
3491 drmModeFreeObjectProperties(props);
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01003492 }
Daniel Stone2ba17f42015-05-19 20:02:41 +01003493 else {
3494 plane->possible_crtcs = (1 << output->pipe);
3495 plane->plane_id = 0;
3496 plane->count_formats = 1;
3497 plane->formats[0] = format;
3498 plane->type = type;
3499 }
3500
3501 if (plane->type == WDRM_PLANE_TYPE__COUNT)
3502 goto err_props;
3503
3504 /* With universal planes, everything is a DRM plane; without
3505 * universal planes, the only DRM planes are overlay planes.
3506 * Everything else is a fake plane. */
3507 if (b->universal_planes) {
3508 assert(kplane);
3509 } else {
3510 if (kplane)
3511 assert(plane->type == WDRM_PLANE_TYPE_OVERLAY);
3512 else
3513 assert(plane->type != WDRM_PLANE_TYPE_OVERLAY &&
3514 output);
3515 }
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01003516
Pekka Paalanenec272712014-06-05 11:22:25 +03003517 weston_plane_init(&plane->base, b->compositor, 0, 0);
Daniel Stone085d2b92015-05-21 00:00:57 +01003518 wl_list_insert(&b->plane_list, &plane->link);
Pekka Paalanenec272712014-06-05 11:22:25 +03003519
3520 return plane;
Daniel Stone2ba17f42015-05-19 20:02:41 +01003521
3522err_props:
3523 drm_property_info_free(plane->props, WDRM_PLANE__COUNT);
3524err:
3525 drm_plane_state_free(plane->state_cur, true);
3526 free(plane);
3527 return NULL;
3528}
3529
3530/**
3531 * Find, or create, a special-purpose plane
3532 *
3533 * Primary and cursor planes are a special case, in that before universal
3534 * planes, they are driven by non-plane API calls. Without universal plane
3535 * support, the only way to configure a primary plane is via drmModeSetCrtc,
3536 * and the only way to configure a cursor plane is drmModeSetCursor2.
3537 *
3538 * Although they may actually be regular planes in the hardware, without
3539 * universal plane support, these planes are not actually exposed to
3540 * userspace in the regular plane list.
3541 *
3542 * However, for ease of internal tracking, we want to manage all planes
3543 * through the same drm_plane structures. Therefore, when we are running
3544 * without universal plane support, we create fake drm_plane structures
3545 * to track these planes.
3546 *
3547 * @param b DRM backend
3548 * @param output Output to use for plane
3549 * @param type Type of plane
3550 */
3551static struct drm_plane *
3552drm_output_find_special_plane(struct drm_backend *b, struct drm_output *output,
3553 enum wdrm_plane_type type)
3554{
3555 struct drm_plane *plane;
3556
3557 if (!b->universal_planes) {
3558 uint32_t format;
3559
3560 switch (type) {
3561 case WDRM_PLANE_TYPE_CURSOR:
3562 format = GBM_FORMAT_ARGB8888;
3563 break;
Daniel Stonee2e80132018-01-16 15:37:33 +00003564 case WDRM_PLANE_TYPE_PRIMARY:
3565 /* We don't know what formats the primary plane supports
3566 * before universal planes, so we just assume that the
3567 * GBM format works; however, this isn't set until after
3568 * the output is created. */
3569 format = 0;
3570 break;
Daniel Stone2ba17f42015-05-19 20:02:41 +01003571 default:
3572 assert(!"invalid type in drm_output_find_special_plane");
3573 break;
3574 }
3575
3576 return drm_plane_create(b, NULL, output, type, format);
3577 }
3578
3579 wl_list_for_each(plane, &b->plane_list, link) {
3580 struct drm_output *tmp;
3581 bool found_elsewhere = false;
3582
3583 if (plane->type != type)
3584 continue;
3585 if (!drm_plane_is_available(plane, output))
3586 continue;
3587
3588 /* On some platforms, primary/cursor planes can roam
3589 * between different CRTCs, so make sure we don't claim the
3590 * same plane for two outputs. */
Daniel Stone2ba17f42015-05-19 20:02:41 +01003591 wl_list_for_each(tmp, &b->compositor->output_list,
3592 base.link) {
Daniel Stonee2e80132018-01-16 15:37:33 +00003593 if (tmp->cursor_plane == plane ||
3594 tmp->scanout_plane == plane) {
Daniel Stone2ba17f42015-05-19 20:02:41 +01003595 found_elsewhere = true;
3596 break;
3597 }
3598 }
3599
3600 if (found_elsewhere)
3601 continue;
3602
3603 plane->possible_crtcs = (1 << output->pipe);
3604 return plane;
3605 }
3606
3607 return NULL;
Pekka Paalanenec272712014-06-05 11:22:25 +03003608}
3609
3610/**
3611 * Destroy one DRM plane
3612 *
3613 * Destroy a DRM plane, removing it from screen and releasing its retained
3614 * buffers in the process. The counterpart to drm_plane_create.
3615 *
3616 * @param plane Plane to deallocate (will be freed)
3617 */
3618static void
3619drm_plane_destroy(struct drm_plane *plane)
3620{
Daniel Stone2ba17f42015-05-19 20:02:41 +01003621 if (plane->type == WDRM_PLANE_TYPE_OVERLAY)
3622 drmModeSetPlane(plane->backend->drm.fd, plane->plane_id,
3623 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
Daniel Stonebc15f682016-11-14 16:57:01 +00003624 drm_plane_state_free(plane->state_cur, true);
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01003625 drm_property_info_free(plane->props, WDRM_PLANE__COUNT);
Pekka Paalanenec272712014-06-05 11:22:25 +03003626 weston_plane_release(&plane->base);
3627 wl_list_remove(&plane->link);
3628 free(plane);
3629}
3630
3631/**
3632 * Initialise sprites (overlay planes)
3633 *
3634 * Walk the list of provided DRM planes, and add overlay planes.
3635 *
3636 * Call destroy_sprites to free these planes.
3637 *
3638 * @param b DRM compositor backend
3639 */
3640static void
3641create_sprites(struct drm_backend *b)
3642{
3643 drmModePlaneRes *kplane_res;
3644 drmModePlane *kplane;
3645 struct drm_plane *drm_plane;
3646 uint32_t i;
Pekka Paalanenec272712014-06-05 11:22:25 +03003647 kplane_res = drmModeGetPlaneResources(b->drm.fd);
3648 if (!kplane_res) {
3649 weston_log("failed to get plane resources: %s\n",
3650 strerror(errno));
3651 return;
3652 }
3653
3654 for (i = 0; i < kplane_res->count_planes; i++) {
3655 kplane = drmModeGetPlane(b->drm.fd, kplane_res->planes[i]);
3656 if (!kplane)
3657 continue;
3658
Daniel Stone2ba17f42015-05-19 20:02:41 +01003659 drm_plane = drm_plane_create(b, kplane, NULL,
3660 WDRM_PLANE_TYPE__COUNT, 0);
Pekka Paalanenec272712014-06-05 11:22:25 +03003661 drmModeFreePlane(kplane);
3662 if (!drm_plane)
3663 continue;
3664
Daniel Stone085d2b92015-05-21 00:00:57 +01003665 if (drm_plane->type == WDRM_PLANE_TYPE_OVERLAY)
3666 weston_compositor_stack_plane(b->compositor,
3667 &drm_plane->base,
3668 &b->compositor->primary_plane);
Pekka Paalanenec272712014-06-05 11:22:25 +03003669 }
3670
3671 drmModeFreePlaneResources(kplane_res);
3672}
3673
3674/**
3675 * Clean up sprites (overlay planes)
3676 *
3677 * The counterpart to create_sprites.
3678 *
3679 * @param b DRM compositor backend
3680 */
3681static void
3682destroy_sprites(struct drm_backend *b)
3683{
3684 struct drm_plane *plane, *next;
3685
Daniel Stone085d2b92015-05-21 00:00:57 +01003686 wl_list_for_each_safe(plane, next, &b->plane_list, link)
Pekka Paalanenec272712014-06-05 11:22:25 +03003687 drm_plane_destroy(plane);
3688}
3689
Pekka Paalanendc14fd42017-11-10 15:31:39 +02003690static uint32_t
3691drm_refresh_rate_mHz(const drmModeModeInfo *info)
3692{
3693 uint64_t refresh;
3694
3695 /* Calculate higher precision (mHz) refresh rate */
3696 refresh = (info->clock * 1000000LL / info->htotal +
3697 info->vtotal / 2) / info->vtotal;
3698
3699 if (info->flags & DRM_MODE_FLAG_INTERLACE)
3700 refresh *= 2;
3701 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
3702 refresh /= 2;
3703 if (info->vscan > 1)
3704 refresh /= info->vscan;
3705
3706 return refresh;
3707}
3708
Pekka Paalanenec272712014-06-05 11:22:25 +03003709/**
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003710 * Add a mode to output's mode list
3711 *
3712 * Copy the supplied DRM mode into a Weston mode structure, and add it to the
3713 * output's mode list.
3714 *
3715 * @param output DRM output to add mode to
3716 * @param info DRM mode structure to add
3717 * @returns Newly-allocated Weston/DRM mode structure
3718 */
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03003719static struct drm_mode *
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003720drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003721{
3722 struct drm_mode *mode;
3723
3724 mode = malloc(sizeof *mode);
3725 if (mode == NULL)
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03003726 return NULL;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003727
3728 mode->base.flags = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02003729 mode->base.width = info->hdisplay;
3730 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04003731
Pekka Paalanendc14fd42017-11-10 15:31:39 +02003732 mode->base.refresh = drm_refresh_rate_mHz(info);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003733 mode->mode_info = *info;
Daniel Stoned5526cb2016-11-16 10:54:10 +00003734 mode->blob_id = 0;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04003735
3736 if (info->type & DRM_MODE_TYPE_PREFERRED)
3737 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
3738
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003739 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
3740
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03003741 return mode;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003742}
3743
Daniel Stoned5526cb2016-11-16 10:54:10 +00003744/**
3745 * Destroys a mode, and removes it from the list.
3746 */
3747static void
3748drm_output_destroy_mode(struct drm_backend *backend, struct drm_mode *mode)
3749{
3750 if (mode->blob_id)
3751 drmModeDestroyPropertyBlob(backend->drm.fd, mode->blob_id);
3752 wl_list_remove(&mode->base.link);
3753 free(mode);
3754}
3755
Pekka Paalanen383b3af2017-09-11 14:40:48 +03003756/** Destroy a list of drm_modes
3757 *
3758 * @param backend The backend for releasing mode property blobs.
3759 * @param mode_list The list linked by drm_mode::base.link.
3760 */
3761static void
3762drm_mode_list_destroy(struct drm_backend *backend, struct wl_list *mode_list)
3763{
3764 struct drm_mode *mode, *next;
3765
3766 wl_list_for_each_safe(mode, next, mode_list, base.link)
3767 drm_output_destroy_mode(backend, mode);
3768}
3769
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003770static int
3771drm_subpixel_to_wayland(int drm_value)
3772{
3773 switch (drm_value) {
3774 default:
3775 case DRM_MODE_SUBPIXEL_UNKNOWN:
3776 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
3777 case DRM_MODE_SUBPIXEL_NONE:
3778 return WL_OUTPUT_SUBPIXEL_NONE;
3779 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
3780 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
3781 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
3782 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
3783 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
3784 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
3785 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
3786 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
3787 }
3788}
3789
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003790/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003791static uint32_t
Pekka Paalanence724242017-09-04 12:21:24 +03003792drm_get_backlight(struct drm_head *head)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003793{
3794 long brightness, max_brightness, norm;
3795
Pekka Paalanence724242017-09-04 12:21:24 +03003796 brightness = backlight_get_brightness(head->backlight);
3797 max_brightness = backlight_get_max_brightness(head->backlight);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003798
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003799 /* convert it on a scale of 0 to 255 */
3800 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003801
3802 return (uint32_t) norm;
3803}
3804
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003805/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003806static void
3807drm_set_backlight(struct weston_output *output_base, uint32_t value)
3808{
Pekka Paalanenecc8cce2017-09-12 16:14:31 +03003809 struct drm_output *output = to_drm_output(output_base);
3810 struct drm_head *head;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003811 long max_brightness, new_brightness;
3812
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003813 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003814 return;
3815
Pekka Paalanenecc8cce2017-09-12 16:14:31 +03003816 wl_list_for_each(head, &output->base.head_list, base.output_link) {
3817 if (!head->backlight)
3818 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003819
Pekka Paalanenecc8cce2017-09-12 16:14:31 +03003820 max_brightness = backlight_get_max_brightness(head->backlight);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003821
Pekka Paalanenecc8cce2017-09-12 16:14:31 +03003822 /* get denormalized value */
3823 new_brightness = (value * max_brightness) / 255;
3824
3825 backlight_set_brightness(head->backlight, new_brightness);
3826 }
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003827}
3828
Pekka Paalanenf8b850d2017-11-15 12:51:01 +02003829static void
3830drm_output_init_backlight(struct drm_output *output)
3831{
3832 struct weston_head *base;
3833 struct drm_head *head;
3834
3835 output->base.set_backlight = NULL;
3836
3837 wl_list_for_each(base, &output->base.head_list, output_link) {
3838 head = to_drm_head(base);
3839
3840 if (head->backlight) {
3841 weston_log("Initialized backlight for head '%s', device %s\n",
3842 head->base.name, head->backlight->path);
3843
3844 if (!output->base.set_backlight) {
3845 output->base.set_backlight = drm_set_backlight;
3846 output->base.backlight_current =
3847 drm_get_backlight(head);
3848 }
3849 }
3850 }
3851
3852 if (!output->base.set_backlight) {
3853 weston_log("No backlight control for output '%s'\n",
3854 output->base.name);
3855 }
3856}
3857
Daniel Stonea08512f2016-11-08 17:46:10 +00003858/**
3859 * Power output on or off
3860 *
3861 * The DPMS/power level of an output is used to switch it on or off. This
3862 * is DRM's hook for doing so, which can called either as part of repaint,
3863 * or independently of the repaint loop.
3864 *
3865 * If we are called as part of repaint, we simply set the relevant bit in
3866 * state and return.
3867 */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003868static void
3869drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
3870{
Armin Krezović545dba62016-08-05 15:54:18 +02003871 struct drm_output *output = to_drm_output(output_base);
Daniel Stonea08512f2016-11-08 17:46:10 +00003872 struct drm_backend *b = to_drm_backend(output_base->compositor);
3873 struct drm_pending_state *pending_state = b->repaint_data;
3874 struct drm_output_state *state;
Daniel Stone36609c72015-06-18 07:49:02 +01003875 int ret;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003876
Daniel Stonea08512f2016-11-08 17:46:10 +00003877 if (output->state_cur->dpms == level)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003878 return;
3879
Daniel Stonea08512f2016-11-08 17:46:10 +00003880 /* If we're being called during the repaint loop, then this is
3881 * simple: discard any previously-generated state, and create a new
3882 * state where we disable everything. When we come to flush, this
3883 * will be applied.
3884 *
3885 * However, we need to be careful: we can be called whilst another
3886 * output is in its repaint cycle (pending_state exists), but our
3887 * output still has an incomplete state application outstanding.
3888 * In that case, we need to wait until that completes. */
3889 if (pending_state && !output->state_last) {
3890 /* The repaint loop already sets DPMS on; we don't need to
3891 * explicitly set it on here, as it will already happen
3892 * whilst applying the repaint state. */
3893 if (level == WESTON_DPMS_ON)
3894 return;
3895
3896 state = drm_pending_state_get_output(pending_state, output);
3897 if (state)
3898 drm_output_state_free(state);
3899 state = drm_output_get_disable_state(pending_state, output);
Daniel Stone36609c72015-06-18 07:49:02 +01003900 return;
3901 }
3902
Daniel Stonea08512f2016-11-08 17:46:10 +00003903 /* As we throw everything away when disabling, just send us back through
3904 * a repaint cycle. */
3905 if (level == WESTON_DPMS_ON) {
3906 if (output->dpms_off_pending)
3907 output->dpms_off_pending = 0;
3908 weston_output_schedule_repaint(output_base);
3909 return;
3910 }
3911
3912 /* If we've already got a request in the pipeline, then we need to
3913 * park our DPMS request until that request has quiesced. */
3914 if (output->state_last) {
3915 output->dpms_off_pending = 1;
3916 return;
3917 }
3918
3919 pending_state = drm_pending_state_alloc(b);
3920 drm_output_get_disable_state(pending_state, output);
3921 ret = drm_pending_state_apply_sync(pending_state);
3922 if (ret != 0)
3923 weston_log("drm_set_dpms: couldn't disable output?\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003924}
3925
Pekka Paalanen3ce63622014-06-04 16:29:49 +03003926static const char * const connector_type_names[] = {
Pekka Paalanen89c49b32015-08-19 15:25:57 +03003927 [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
3928 [DRM_MODE_CONNECTOR_VGA] = "VGA",
3929 [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
3930 [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
3931 [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
3932 [DRM_MODE_CONNECTOR_Composite] = "Composite",
3933 [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
3934 [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
3935 [DRM_MODE_CONNECTOR_Component] = "Component",
3936 [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
3937 [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
3938 [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
3939 [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
3940 [DRM_MODE_CONNECTOR_TV] = "TV",
3941 [DRM_MODE_CONNECTOR_eDP] = "eDP",
Pekka Paalanenab81f152015-08-24 14:27:07 +03003942#ifdef DRM_MODE_CONNECTOR_DSI
Pekka Paalanen89c49b32015-08-19 15:25:57 +03003943 [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
3944 [DRM_MODE_CONNECTOR_DSI] = "DSI",
Pekka Paalanenab81f152015-08-24 14:27:07 +03003945#endif
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04003946};
3947
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03003948/** Create a name given a DRM connector
3949 *
3950 * \param con The DRM connector whose type and id form the name.
3951 * \return A newly allocate string, or NULL on error. Must be free()'d
3952 * after use.
3953 *
3954 * The name does not identify the DRM display device.
3955 */
Pekka Paalanen3ce63622014-06-04 16:29:49 +03003956static char *
3957make_connector_name(const drmModeConnector *con)
3958{
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03003959 char *name;
Pekka Paalanen89c49b32015-08-19 15:25:57 +03003960 const char *type_name = NULL;
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03003961 int ret;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03003962
3963 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
3964 type_name = connector_type_names[con->connector_type];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03003965
3966 if (!type_name)
3967 type_name = "UNNAMED";
3968
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03003969 ret = asprintf(&name, "%s-%d", type_name, con->connector_type_id);
3970 if (ret < 0)
3971 return NULL;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03003972
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03003973 return name;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03003974}
3975
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003976static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003977find_crtc_for_connector(struct drm_backend *b,
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003978 drmModeRes *resources, drmModeConnector *connector)
3979{
3980 drmModeEncoder *encoder;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003981 int i, j;
Daniel Stone75487c22017-01-16 14:33:38 +00003982 int ret = -1;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003983
3984 for (j = 0; j < connector->count_encoders; j++) {
Daniel Stone75487c22017-01-16 14:33:38 +00003985 uint32_t possible_crtcs, encoder_id, crtc_id;
3986
Giulio Camuffo954f1832014-10-11 18:27:30 +03003987 encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003988 if (encoder == NULL) {
3989 weston_log("Failed to get encoder.\n");
Daniel Stone75487c22017-01-16 14:33:38 +00003990 continue;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003991 }
Daniel Stone75487c22017-01-16 14:33:38 +00003992 encoder_id = encoder->encoder_id;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003993 possible_crtcs = encoder->possible_crtcs;
Daniel Stone75487c22017-01-16 14:33:38 +00003994 crtc_id = encoder->crtc_id;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003995 drmModeFreeEncoder(encoder);
3996
3997 for (i = 0; i < resources->count_crtcs; i++) {
Daniel Stone72c0e1b2017-02-09 13:49:15 +00003998 if (!(possible_crtcs & (1 << i)))
3999 continue;
4000
4001 if (drm_output_find_by_crtc(b, resources->crtcs[i]))
4002 continue;
4003
Daniel Stone75487c22017-01-16 14:33:38 +00004004 /* Try to preserve the existing
4005 * CRTC -> encoder -> connector routing; it makes
4006 * initialisation faster, and also since we have a
4007 * very dumb picking algorithm, may preserve a better
4008 * choice. */
4009 if (!connector->encoder_id ||
4010 (encoder_id == connector->encoder_id &&
4011 crtc_id == resources->crtcs[i]))
4012 return i;
4013
4014 ret = i;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04004015 }
4016 }
4017
Daniel Stone75487c22017-01-16 14:33:38 +00004018 return ret;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04004019}
4020
Daniel Stonee4256832017-04-04 17:54:27 +01004021static void drm_output_fini_cursor_egl(struct drm_output *output)
4022{
4023 unsigned int i;
4024
4025 for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) {
4026 drm_fb_unref(output->gbm_cursor_fb[i]);
4027 output->gbm_cursor_fb[i] = NULL;
4028 }
4029}
4030
4031static int
4032drm_output_init_cursor_egl(struct drm_output *output, struct drm_backend *b)
4033{
4034 unsigned int i;
4035
Daniel Stone2ba17f42015-05-19 20:02:41 +01004036 /* No point creating cursors if we don't have a plane for them. */
4037 if (!output->cursor_plane)
4038 return 0;
4039
Daniel Stonee4256832017-04-04 17:54:27 +01004040 for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) {
4041 struct gbm_bo *bo;
4042
4043 bo = gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
4044 GBM_FORMAT_ARGB8888,
4045 GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE);
4046 if (!bo)
4047 goto err;
4048
4049 output->gbm_cursor_fb[i] =
4050 drm_fb_get_from_bo(bo, b, GBM_FORMAT_ARGB8888,
4051 BUFFER_CURSOR);
4052 if (!output->gbm_cursor_fb[i]) {
4053 gbm_bo_destroy(bo);
4054 goto err;
4055 }
4056 }
4057
4058 return 0;
4059
4060err:
4061 weston_log("cursor buffers unavailable, using gl cursors\n");
4062 b->cursors_are_broken = 1;
4063 drm_output_fini_cursor_egl(output);
4064 return -1;
4065}
4066
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02004067/* Init output state that depends on gl or gbm */
4068static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03004069drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02004070{
Derek Foremanc4cfe852015-05-15 12:12:40 -05004071 EGLint format[2] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01004072 output->gbm_format,
4073 fallback_format_for(output->gbm_format),
Derek Foremanc4cfe852015-05-15 12:12:40 -05004074 };
Daniel Stonee4256832017-04-04 17:54:27 +01004075 int n_formats = 1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02004076
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01004077 output->gbm_surface = gbm_surface_create(b->gbm,
Hardeningff39efa2013-09-18 23:56:35 +02004078 output->base.current_mode->width,
4079 output->base.current_mode->height,
Derek Foremanc4cfe852015-05-15 12:12:40 -05004080 format[0],
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02004081 GBM_BO_USE_SCANOUT |
4082 GBM_BO_USE_RENDERING);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01004083 if (!output->gbm_surface) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02004084 weston_log("failed to create gbm surface\n");
4085 return -1;
4086 }
4087
Derek Foremanc4cfe852015-05-15 12:12:40 -05004088 if (format[1])
4089 n_formats = 2;
Miguel A. Vicoc095cde2016-05-18 17:43:00 +02004090 if (gl_renderer->output_window_create(&output->base,
4091 (EGLNativeWindowType)output->gbm_surface,
4092 output->gbm_surface,
4093 gl_renderer->opaque_attribs,
4094 format,
4095 n_formats) < 0) {
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02004096 weston_log("failed to create gl renderer output state\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01004097 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02004098 return -1;
4099 }
4100
Daniel Stonee4256832017-04-04 17:54:27 +01004101 drm_output_init_cursor_egl(output, b);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02004102
4103 return 0;
4104}
4105
Daniel Stone3e661f72016-11-04 17:24:06 +00004106static void
4107drm_output_fini_egl(struct drm_output *output)
4108{
Daniel Stonee2e80132018-01-16 15:37:33 +00004109 struct drm_backend *b = to_drm_backend(output->base.compositor);
4110
4111 /* Destroying the GBM surface will destroy all our GBM buffers,
4112 * regardless of refcount. Ensure we destroy them here. */
4113 if (!b->shutting_down &&
4114 output->scanout_plane->state_cur->fb &&
4115 output->scanout_plane->state_cur->fb->type == BUFFER_GBM_SURFACE) {
4116 drm_plane_state_free(output->scanout_plane->state_cur, true);
4117 output->scanout_plane->state_cur =
4118 drm_plane_state_alloc(NULL, output->scanout_plane);
4119 output->scanout_plane->state_cur->complete = true;
4120 }
4121
Daniel Stone3e661f72016-11-04 17:24:06 +00004122 gl_renderer->output_destroy(&output->base);
4123 gbm_surface_destroy(output->gbm_surface);
Daniel Stonee4256832017-04-04 17:54:27 +01004124 drm_output_fini_cursor_egl(output);
Daniel Stone3e661f72016-11-04 17:24:06 +00004125}
4126
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04004127static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03004128drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004129{
Hardeningff39efa2013-09-18 23:56:35 +02004130 int w = output->base.current_mode->width;
4131 int h = output->base.current_mode->height;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03004132 uint32_t format = output->gbm_format;
4133 uint32_t pixman_format;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004134 unsigned int i;
4135
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03004136 switch (format) {
4137 case GBM_FORMAT_XRGB8888:
4138 pixman_format = PIXMAN_x8r8g8b8;
4139 break;
4140 case GBM_FORMAT_RGB565:
4141 pixman_format = PIXMAN_r5g6b5;
4142 break;
4143 default:
4144 weston_log("Unsupported pixman format 0x%x\n", format);
4145 return -1;
4146 }
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004147
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03004148 /* FIXME error checking */
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004149 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03004150 output->dumb[i] = drm_fb_create_dumb(b, w, h, format);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004151 if (!output->dumb[i])
4152 goto err;
4153
4154 output->image[i] =
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03004155 pixman_image_create_bits(pixman_format, w, h,
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004156 output->dumb[i]->map,
4157 output->dumb[i]->stride);
4158 if (!output->image[i])
4159 goto err;
4160 }
4161
4162 if (pixman_renderer_output_create(&output->base) < 0)
4163 goto err;
4164
4165 pixman_region32_init_rect(&output->previous_damage,
Alexander Larsson0b135062013-05-28 16:23:36 +02004166 output->base.x, output->base.y, output->base.width, output->base.height);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004167
4168 return 0;
4169
4170err:
4171 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
4172 if (output->dumb[i])
Daniel Stone6e7a9612017-04-04 17:54:26 +01004173 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004174 if (output->image[i])
4175 pixman_image_unref(output->image[i]);
4176
4177 output->dumb[i] = NULL;
4178 output->image[i] = NULL;
4179 }
4180
4181 return -1;
4182}
4183
4184static void
4185drm_output_fini_pixman(struct drm_output *output)
4186{
Daniel Stonee2e80132018-01-16 15:37:33 +00004187 struct drm_backend *b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004188 unsigned int i;
4189
Daniel Stonee2e80132018-01-16 15:37:33 +00004190 /* Destroying the Pixman surface will destroy all our buffers,
4191 * regardless of refcount. Ensure we destroy them here. */
4192 if (!b->shutting_down &&
4193 output->scanout_plane->state_cur->fb &&
4194 output->scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB) {
4195 drm_plane_state_free(output->scanout_plane->state_cur, true);
4196 output->scanout_plane->state_cur =
4197 drm_plane_state_alloc(NULL, output->scanout_plane);
4198 output->scanout_plane->state_cur->complete = true;
4199 }
4200
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004201 pixman_renderer_output_destroy(&output->base);
4202 pixman_region32_fini(&output->previous_damage);
4203
4204 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004205 pixman_image_unref(output->image[i]);
Daniel Stone6e7a9612017-04-04 17:54:26 +01004206 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004207 output->dumb[i] = NULL;
4208 output->image[i] = NULL;
4209 }
4210}
4211
Richard Hughes2b2092a2013-04-24 14:58:02 +01004212static void
4213edid_parse_string(const uint8_t *data, char text[])
4214{
4215 int i;
4216 int replaced = 0;
4217
4218 /* this is always 12 bytes, but we can't guarantee it's null
4219 * terminated or not junk. */
4220 strncpy(text, (const char *) data, 12);
4221
Bryce Harrington9c7de162015-08-28 13:04:26 -07004222 /* guarantee our new string is null-terminated */
4223 text[12] = '\0';
4224
Richard Hughes2b2092a2013-04-24 14:58:02 +01004225 /* remove insane chars */
4226 for (i = 0; text[i] != '\0'; i++) {
4227 if (text[i] == '\n' ||
4228 text[i] == '\r') {
4229 text[i] = '\0';
4230 break;
4231 }
4232 }
4233
4234 /* ensure string is printable */
4235 for (i = 0; text[i] != '\0'; i++) {
4236 if (!isprint(text[i])) {
4237 text[i] = '-';
4238 replaced++;
4239 }
4240 }
4241
4242 /* if the string is random junk, ignore the string */
4243 if (replaced > 4)
4244 text[0] = '\0';
4245}
4246
4247#define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
4248#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
4249#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
4250#define EDID_OFFSET_DATA_BLOCKS 0x36
4251#define EDID_OFFSET_LAST_BLOCK 0x6c
4252#define EDID_OFFSET_PNPID 0x08
4253#define EDID_OFFSET_SERIAL 0x0c
4254
4255static int
4256edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
4257{
4258 int i;
4259 uint32_t serial_number;
4260
4261 /* check header */
4262 if (length < 128)
4263 return -1;
4264 if (data[0] != 0x00 || data[1] != 0xff)
4265 return -1;
4266
4267 /* decode the PNP ID from three 5 bit words packed into 2 bytes
4268 * /--08--\/--09--\
4269 * 7654321076543210
4270 * |\---/\---/\---/
4271 * R C1 C2 C3 */
4272 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
4273 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
4274 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
4275 edid->pnp_id[3] = '\0';
4276
4277 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
4278 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
4279 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
4280 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
4281 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
4282 if (serial_number > 0)
4283 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
4284
4285 /* parse EDID data */
4286 for (i = EDID_OFFSET_DATA_BLOCKS;
4287 i <= EDID_OFFSET_LAST_BLOCK;
4288 i += 18) {
4289 /* ignore pixel clock data */
4290 if (data[i] != 0)
4291 continue;
4292 if (data[i+2] != 0)
4293 continue;
4294
4295 /* any useful blocks? */
4296 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
4297 edid_parse_string(&data[i+5],
4298 edid->monitor_name);
4299 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
4300 edid_parse_string(&data[i+5],
4301 edid->serial_number);
4302 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
4303 edid_parse_string(&data[i+5],
4304 edid->eisa_id);
4305 }
4306 }
4307 return 0;
4308}
4309
Pekka Paalanen6f1866b2017-04-03 14:22:51 +03004310/** Parse monitor make, model and serial from EDID
4311 *
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03004312 * \param head The head whose \c drm_edid to fill in.
Pekka Paalanen6f1866b2017-04-03 14:22:51 +03004313 * \param props The DRM connector properties to get the EDID from.
4314 * \param make[out] The monitor make (PNP ID).
4315 * \param model[out] The monitor model (name).
4316 * \param serial_number[out] The monitor serial number.
4317 *
4318 * Each of \c *make, \c *model and \c *serial_number are set only if the
4319 * information is found in the EDID. The pointers they are set to must not
4320 * be free()'d explicitly, instead they get implicitly freed when the
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03004321 * \c drm_head is destroyed.
Pekka Paalanen6f1866b2017-04-03 14:22:51 +03004322 */
Richard Hughes2b2092a2013-04-24 14:58:02 +01004323static void
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03004324find_and_parse_output_edid(struct drm_head *head,
Pekka Paalanen6f1866b2017-04-03 14:22:51 +03004325 drmModeObjectPropertiesPtr props,
4326 const char **make,
4327 const char **model,
4328 const char **serial_number)
Richard Hughes2b2092a2013-04-24 14:58:02 +01004329{
4330 drmModePropertyBlobPtr edid_blob = NULL;
Daniel Stone02cf4662017-03-03 16:19:39 +00004331 uint32_t blob_id;
Richard Hughes2b2092a2013-04-24 14:58:02 +01004332 int rc;
4333
Daniel Stone02cf4662017-03-03 16:19:39 +00004334 blob_id =
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03004335 drm_property_get_value(&head->props_conn[WDRM_CONNECTOR_EDID],
Daniel Stone02cf4662017-03-03 16:19:39 +00004336 props, 0);
4337 if (!blob_id)
4338 return;
4339
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03004340 edid_blob = drmModeGetPropertyBlob(head->backend->drm.fd, blob_id);
Richard Hughes2b2092a2013-04-24 14:58:02 +01004341 if (!edid_blob)
4342 return;
4343
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03004344 rc = edid_parse(&head->edid,
Richard Hughes2b2092a2013-04-24 14:58:02 +01004345 edid_blob->data,
4346 edid_blob->length);
4347 if (!rc) {
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03004348 if (head->edid.pnp_id[0] != '\0')
4349 *make = head->edid.pnp_id;
4350 if (head->edid.monitor_name[0] != '\0')
4351 *model = head->edid.monitor_name;
4352 if (head->edid.serial_number[0] != '\0')
4353 *serial_number = head->edid.serial_number;
Richard Hughes2b2092a2013-04-24 14:58:02 +01004354 }
4355 drmModeFreePropertyBlob(edid_blob);
4356}
4357
Kristian Høgsberga30989a2013-05-23 17:23:15 -04004358static int
4359parse_modeline(const char *s, drmModeModeInfo *mode)
4360{
4361 char hsync[16];
4362 char vsync[16];
4363 float fclock;
4364
Pekka Paalanendc4e3c62017-12-05 15:37:41 +02004365 memset(mode, 0, sizeof *mode);
4366
Kristian Høgsberga30989a2013-05-23 17:23:15 -04004367 mode->type = DRM_MODE_TYPE_USERDEF;
4368 mode->hskew = 0;
4369 mode->vscan = 0;
4370 mode->vrefresh = 0;
4371 mode->flags = 0;
4372
Rob Bradford307e09e2013-07-26 16:29:40 +01004373 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
Kristian Høgsberga30989a2013-05-23 17:23:15 -04004374 &fclock,
4375 &mode->hdisplay,
4376 &mode->hsync_start,
4377 &mode->hsync_end,
4378 &mode->htotal,
4379 &mode->vdisplay,
4380 &mode->vsync_start,
4381 &mode->vsync_end,
4382 &mode->vtotal, hsync, vsync) != 11)
4383 return -1;
4384
4385 mode->clock = fclock * 1000;
4386 if (strcmp(hsync, "+hsync") == 0)
4387 mode->flags |= DRM_MODE_FLAG_PHSYNC;
4388 else if (strcmp(hsync, "-hsync") == 0)
4389 mode->flags |= DRM_MODE_FLAG_NHSYNC;
4390 else
4391 return -1;
4392
4393 if (strcmp(vsync, "+vsync") == 0)
4394 mode->flags |= DRM_MODE_FLAG_PVSYNC;
4395 else if (strcmp(vsync, "-vsync") == 0)
4396 mode->flags |= DRM_MODE_FLAG_NVSYNC;
4397 else
4398 return -1;
4399
Emmanuel Gil Peyrota62138b2016-05-02 22:40:11 +01004400 snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f",
4401 mode->hdisplay, mode->vdisplay, fclock);
4402
Kristian Høgsberga30989a2013-05-23 17:23:15 -04004403 return 0;
4404}
4405
Rob Bradford66bd9f52013-06-25 18:56:42 +01004406static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03004407setup_output_seat_constraint(struct drm_backend *b,
Rob Bradford66bd9f52013-06-25 18:56:42 +01004408 struct weston_output *output,
4409 const char *s)
4410{
4411 if (strcmp(s, "") != 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05004412 struct weston_pointer *pointer;
Rob Bradford66bd9f52013-06-25 18:56:42 +01004413 struct udev_seat *seat;
4414
Giulio Camuffo954f1832014-10-11 18:27:30 +03004415 seat = udev_seat_get_named(&b->input, s);
Derek Foreman0720ea32015-07-15 13:00:35 -05004416 if (!seat)
4417 return;
Rob Bradford66bd9f52013-06-25 18:56:42 +01004418
Derek Foreman0720ea32015-07-15 13:00:35 -05004419 seat->base.output = output;
4420
Derek Foreman1281a362015-07-31 16:55:32 -05004421 pointer = weston_seat_get_pointer(&seat->base);
4422 if (pointer)
4423 weston_pointer_clamp(pointer,
4424 &pointer->x,
4425 &pointer->y);
Rob Bradford66bd9f52013-06-25 18:56:42 +01004426 }
4427}
4428
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004429static int
Pekka Paalanenc112f002017-08-28 16:27:20 +03004430drm_output_attach_head(struct weston_output *output_base,
4431 struct weston_head *head_base)
4432{
4433 if (wl_list_length(&output_base->head_list) >= MAX_CLONED_CONNECTORS)
4434 return -1;
4435
4436 return 0;
4437}
4438
4439static int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004440parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format)
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004441{
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004442 int ret = 0;
4443
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004444 if (s == NULL)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004445 *gbm_format = default_value;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004446 else if (strcmp(s, "xrgb8888") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004447 *gbm_format = GBM_FORMAT_XRGB8888;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004448 else if (strcmp(s, "rgb565") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004449 *gbm_format = GBM_FORMAT_RGB565;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004450 else if (strcmp(s, "xrgb2101010") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004451 *gbm_format = GBM_FORMAT_XRGB2101010;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004452 else {
4453 weston_log("fatal: unrecognized pixel format: %s\n", s);
4454 ret = -1;
4455 }
4456
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004457 return ret;
4458}
4459
Pekka Paalanenf005f252017-11-10 16:34:39 +02004460static uint32_t
4461u32distance(uint32_t a, uint32_t b)
4462{
4463 if (a < b)
4464 return b - a;
4465 else
4466 return a - b;
4467}
4468
4469/** Choose equivalent mode
4470 *
4471 * If the two modes are not equivalent, return NULL.
4472 * Otherwise return the mode that is more likely to work in place of both.
4473 *
4474 * None of the fuzzy matching criteria in this function have any justification.
4475 *
4476 * typedef struct _drmModeModeInfo {
4477 * uint32_t clock;
4478 * uint16_t hdisplay, hsync_start, hsync_end, htotal, hskew;
4479 * uint16_t vdisplay, vsync_start, vsync_end, vtotal, vscan;
4480 *
4481 * uint32_t vrefresh;
4482 *
4483 * uint32_t flags;
4484 * uint32_t type;
4485 * char name[DRM_DISPLAY_MODE_LEN];
4486 * } drmModeModeInfo, *drmModeModeInfoPtr;
4487 */
4488static const drmModeModeInfo *
4489drm_mode_pick_equivalent(const drmModeModeInfo *a, const drmModeModeInfo *b)
4490{
4491 uint32_t refresh_a, refresh_b;
4492
4493 if (a->hdisplay != b->hdisplay || a->vdisplay != b->vdisplay)
4494 return NULL;
4495
4496 if (a->flags != b->flags)
4497 return NULL;
4498
4499 /* kHz */
4500 if (u32distance(a->clock, b->clock) > 500)
4501 return NULL;
4502
4503 refresh_a = drm_refresh_rate_mHz(a);
4504 refresh_b = drm_refresh_rate_mHz(b);
4505 if (u32distance(refresh_a, refresh_b) > 50)
4506 return NULL;
4507
4508 if ((a->type ^ b->type) & DRM_MODE_TYPE_PREFERRED) {
4509 if (a->type & DRM_MODE_TYPE_PREFERRED)
4510 return a;
4511 else
4512 return b;
4513 }
4514
4515 return a;
4516}
4517
4518/* If the given mode info is not already in the list, add it.
4519 * If it is in the list, either keep the existing or replace it,
4520 * depending on which one is "better".
4521 */
4522static int
4523drm_output_try_add_mode(struct drm_output *output, const drmModeModeInfo *info)
4524{
4525 struct weston_mode *base;
4526 struct drm_mode *mode;
4527 struct drm_backend *backend;
4528 const drmModeModeInfo *chosen = NULL;
4529
4530 assert(info);
4531
4532 wl_list_for_each(base, &output->base.mode_list, link) {
4533 mode = to_drm_mode(base);
4534 chosen = drm_mode_pick_equivalent(&mode->mode_info, info);
4535 if (chosen)
4536 break;
4537 }
4538
4539 if (chosen == info) {
4540 backend = to_drm_backend(output->base.compositor);
4541 drm_output_destroy_mode(backend, mode);
4542 chosen = NULL;
4543 }
4544
4545 if (!chosen) {
4546 mode = drm_output_add_mode(output, info);
4547 if (!mode)
4548 return -1;
4549 }
4550 /* else { the equivalent mode is already in the list } */
4551
4552 return 0;
4553}
4554
Pekka Paalanen4be24852017-09-11 15:01:12 +03004555/** Rewrite the output's mode list
4556 *
4557 * @param output The output.
4558 * @return 0 on success, -1 on failure.
4559 *
4560 * Destroy all existing modes in the list, and reconstruct a new list from
4561 * scratch, based on the currently attached heads.
4562 *
4563 * On failure the output's mode list may contain some modes.
4564 */
4565static int
4566drm_output_update_modelist_from_heads(struct drm_output *output)
4567{
4568 struct drm_backend *backend = to_drm_backend(output->base.compositor);
4569 struct weston_head *head_base;
4570 struct drm_head *head;
Pekka Paalanen4be24852017-09-11 15:01:12 +03004571 int i;
Pekka Paalanenf005f252017-11-10 16:34:39 +02004572 int ret;
Pekka Paalanen4be24852017-09-11 15:01:12 +03004573
4574 assert(!output->base.enabled);
4575
4576 drm_mode_list_destroy(backend, &output->base.mode_list);
4577
Pekka Paalanenf005f252017-11-10 16:34:39 +02004578 wl_list_for_each(head_base, &output->base.head_list, output_link) {
4579 head = to_drm_head(head_base);
4580 for (i = 0; i < head->connector->count_modes; i++) {
4581 ret = drm_output_try_add_mode(output,
4582 &head->connector->modes[i]);
4583 if (ret < 0)
4584 return -1;
4585 }
Pekka Paalanen4be24852017-09-11 15:01:12 +03004586 }
4587
4588 return 0;
4589}
4590
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004591/**
4592 * Choose suitable mode for an output
4593 *
4594 * Find the most suitable mode to use for initial setup (or reconfiguration on
4595 * hotplug etc) for a DRM output.
4596 *
4597 * @param output DRM output to choose mode for
4598 * @param kind Strategy and preference to use when choosing mode
4599 * @param width Desired width for this output
4600 * @param height Desired height for this output
4601 * @param current_mode Mode currently being displayed on this output
4602 * @param modeline Manually-entered mode (may be NULL)
4603 * @returns A mode from the output's mode list, or NULL if none available
4604 */
4605static struct drm_mode *
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004606drm_output_choose_initial_mode(struct drm_backend *backend,
4607 struct drm_output *output,
4608 enum weston_drm_backend_output_mode mode,
Armin Krezović08368132016-09-30 14:11:05 +02004609 const char *modeline,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004610 const drmModeModeInfo *current_mode)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004611{
4612 struct drm_mode *preferred = NULL;
4613 struct drm_mode *current = NULL;
4614 struct drm_mode *configured = NULL;
4615 struct drm_mode *best = NULL;
4616 struct drm_mode *drm_mode;
Armin Krezović08368132016-09-30 14:11:05 +02004617 drmModeModeInfo drm_modeline;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004618 int32_t width = 0;
4619 int32_t height = 0;
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01004620 uint32_t refresh = 0;
4621 int n;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004622
Armin Krezović08368132016-09-30 14:11:05 +02004623 if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) {
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01004624 n = sscanf(modeline, "%dx%d@%d", &width, &height, &refresh);
4625 if (n != 2 && n != 3) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004626 width = -1;
4627
Armin Krezović08368132016-09-30 14:11:05 +02004628 if (parse_modeline(modeline, &drm_modeline) == 0) {
4629 configured = drm_output_add_mode(output, &drm_modeline);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004630 if (!configured)
4631 return NULL;
4632 } else {
4633 weston_log("Invalid modeline \"%s\" for output %s\n",
Armin Krezović08368132016-09-30 14:11:05 +02004634 modeline, output->base.name);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004635 }
4636 }
4637 }
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004638
4639 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004640 if (width == drm_mode->base.width &&
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01004641 height == drm_mode->base.height &&
4642 (refresh == 0 || refresh == drm_mode->mode_info.vrefresh))
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004643 configured = drm_mode;
4644
comic fans7a5c5622016-03-17 14:29:27 +02004645 if (memcmp(current_mode, &drm_mode->mode_info,
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004646 sizeof *current_mode) == 0)
4647 current = drm_mode;
4648
4649 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
4650 preferred = drm_mode;
4651
4652 best = drm_mode;
4653 }
4654
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004655 if (current == NULL && current_mode->clock != 0) {
4656 current = drm_output_add_mode(output, current_mode);
4657 if (!current)
4658 return NULL;
4659 }
4660
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004661 if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004662 configured = current;
4663
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004664 if (configured)
4665 return configured;
4666
4667 if (preferred)
4668 return preferred;
4669
4670 if (current)
4671 return current;
4672
4673 if (best)
4674 return best;
4675
4676 weston_log("no available modes for %s\n", output->base.name);
4677 return NULL;
4678}
4679
Pekka Paalaneneee580b2014-06-04 16:43:06 +03004680static int
4681connector_get_current_mode(drmModeConnector *connector, int drm_fd,
4682 drmModeModeInfo *mode)
4683{
4684 drmModeEncoder *encoder;
4685 drmModeCrtc *crtc;
4686
4687 /* Get the current mode on the crtc that's currently driving
4688 * this connector. */
4689 encoder = drmModeGetEncoder(drm_fd, connector->encoder_id);
4690 memset(mode, 0, sizeof *mode);
4691 if (encoder != NULL) {
4692 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
4693 drmModeFreeEncoder(encoder);
4694 if (crtc == NULL)
4695 return -1;
4696 if (crtc->mode_valid)
4697 *mode = crtc->mode;
4698 drmModeFreeCrtc(crtc);
4699 }
4700
4701 return 0;
4702}
4703
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004704static int
Armin Krezović08368132016-09-30 14:11:05 +02004705drm_output_set_mode(struct weston_output *base,
4706 enum weston_drm_backend_output_mode mode,
4707 const char *modeline)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004708{
Armin Krezović08368132016-09-30 14:11:05 +02004709 struct drm_output *output = to_drm_output(base);
4710 struct drm_backend *b = to_drm_backend(base->compositor);
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03004711 struct drm_head *head = to_drm_head(weston_output_get_first_head(base));
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004712
Armin Krezović445b41b2016-10-09 23:48:16 +02004713 struct drm_mode *current;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004714
Pekka Paalanen4be24852017-09-11 15:01:12 +03004715 if (drm_output_update_modelist_from_heads(output) < 0)
4716 return -1;
4717
Pekka Paalanen13d233e2017-09-11 14:06:11 +03004718 current = drm_output_choose_initial_mode(b, output, mode, modeline,
4719 &head->inherited_mode);
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004720 if (!current)
Armin Krezović445b41b2016-10-09 23:48:16 +02004721 return -1;
Armin Krezović08368132016-09-30 14:11:05 +02004722
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004723 output->base.current_mode = &current->base;
Hardeningff39efa2013-09-18 23:56:35 +02004724 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
Wang Quanxianacb805a2012-07-30 18:09:46 -04004725
Armin Krezović08368132016-09-30 14:11:05 +02004726 /* Set native_ fields, so weston_output_mode_switch_to_native() works */
4727 output->base.native_mode = output->base.current_mode;
4728 output->base.native_scale = output->base.current_scale;
4729
Armin Krezović08368132016-09-30 14:11:05 +02004730 return 0;
Armin Krezović08368132016-09-30 14:11:05 +02004731}
4732
4733static void
4734drm_output_set_gbm_format(struct weston_output *base,
4735 const char *gbm_format)
4736{
4737 struct drm_output *output = to_drm_output(base);
4738 struct drm_backend *b = to_drm_backend(base->compositor);
4739
4740 if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1)
4741 output->gbm_format = b->gbm_format;
Daniel Stonee2e80132018-01-16 15:37:33 +00004742
4743 /* Without universal planes, we can't discover which formats are
4744 * supported by the primary plane; we just hope that the GBM format
4745 * works. */
4746 if (!b->universal_planes)
4747 output->scanout_plane->formats[0] = output->gbm_format;
Armin Krezović08368132016-09-30 14:11:05 +02004748}
4749
4750static void
4751drm_output_set_seat(struct weston_output *base,
4752 const char *seat)
4753{
4754 struct drm_output *output = to_drm_output(base);
4755 struct drm_backend *b = to_drm_backend(base->compositor);
4756
4757 setup_output_seat_constraint(b, &output->base,
4758 seat ? seat : "");
4759}
4760
4761static int
Pekka Paalanenc4db6f72017-09-05 16:37:03 +03004762drm_output_init_gamma_size(struct drm_output *output)
4763{
4764 struct drm_backend *backend = to_drm_backend(output->base.compositor);
4765 drmModeCrtc *crtc;
4766
4767 assert(output->base.compositor);
4768 assert(output->crtc_id != 0);
4769 crtc = drmModeGetCrtc(backend->drm.fd, output->crtc_id);
4770 if (!crtc)
4771 return -1;
4772
4773 output->base.gamma_size = crtc->gamma_size;
4774
4775 drmModeFreeCrtc(crtc);
4776
4777 return 0;
4778}
4779
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03004780/** Allocate a CRTC for the output
4781 *
4782 * @param output The output with no allocated CRTC.
4783 * @param resources DRM KMS resources.
4784 * @param connector The DRM KMS connector data.
4785 * @return 0 on success, -1 on failure.
4786 *
4787 * Finds a free CRTC that can drive the given connector, reserves the CRTC
4788 * for the output, and loads the CRTC properties.
4789 *
4790 * Populates the cursor and scanout planes.
4791 *
4792 * On failure, the output remains without a CRTC.
4793 */
4794static int
4795drm_output_init_crtc(struct drm_output *output,
4796 drmModeRes *resources, drmModeConnector *connector)
4797{
4798 struct drm_backend *b = to_drm_backend(output->base.compositor);
4799 drmModeObjectPropertiesPtr props;
4800 int i;
4801
4802 assert(output->crtc_id == 0);
4803
4804 i = find_crtc_for_connector(b, resources, connector);
4805 if (i < 0) {
4806 weston_log("No usable crtc/encoder pair for connector.\n");
4807 return -1;
4808 }
4809
4810 output->crtc_id = resources->crtcs[i];
4811 output->pipe = i;
4812
4813 props = drmModeObjectGetProperties(b->drm.fd, output->crtc_id,
4814 DRM_MODE_OBJECT_CRTC);
4815 if (!props) {
4816 weston_log("failed to get CRTC properties\n");
4817 goto err_crtc;
4818 }
4819 drm_property_info_populate(b, crtc_props, output->props_crtc,
4820 WDRM_CRTC__COUNT, props);
4821 drmModeFreeObjectProperties(props);
4822
4823 output->scanout_plane =
4824 drm_output_find_special_plane(b, output,
4825 WDRM_PLANE_TYPE_PRIMARY);
4826 if (!output->scanout_plane) {
4827 weston_log("Failed to find primary plane for output %s\n",
4828 output->base.name);
4829 goto err_crtc;
4830 }
4831
4832 /* Failing to find a cursor plane is not fatal, as we'll fall back
4833 * to software cursor. */
4834 output->cursor_plane =
4835 drm_output_find_special_plane(b, output,
4836 WDRM_PLANE_TYPE_CURSOR);
4837
Pekka Paalanen663d5e92017-09-08 13:32:40 +03004838 wl_array_remove_uint32(&b->unused_crtcs, output->crtc_id);
4839
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03004840 return 0;
4841
4842err_crtc:
4843 output->crtc_id = 0;
4844 output->pipe = 0;
4845
4846 return -1;
4847}
4848
4849/** Free the CRTC from the output
4850 *
4851 * @param output The output whose CRTC to deallocate.
4852 *
4853 * The CRTC reserved for the given output becomes free to use again.
4854 */
4855static void
4856drm_output_fini_crtc(struct drm_output *output)
4857{
4858 struct drm_backend *b = to_drm_backend(output->base.compositor);
Pekka Paalanen663d5e92017-09-08 13:32:40 +03004859 uint32_t *unused;
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03004860
4861 if (!b->universal_planes && !b->shutting_down) {
4862 /* With universal planes, the 'special' planes are allocated at
4863 * startup, freed at shutdown, and live on the plane list in
4864 * between. We want the planes to continue to exist and be freed
4865 * up for other outputs.
4866 *
4867 * Without universal planes, our special planes are
4868 * pseudo-planes allocated at output creation, freed at output
4869 * destruction, and not usable by other outputs.
4870 *
4871 * On the other hand, if the compositor is already shutting down,
4872 * the plane has already been destroyed.
4873 */
4874 if (output->cursor_plane)
4875 drm_plane_destroy(output->cursor_plane);
4876 if (output->scanout_plane)
4877 drm_plane_destroy(output->scanout_plane);
4878 }
4879
4880 drm_property_info_free(output->props_crtc, WDRM_CRTC__COUNT);
Pekka Paalanen663d5e92017-09-08 13:32:40 +03004881
4882 assert(output->crtc_id != 0);
4883
4884 unused = wl_array_add(&b->unused_crtcs, sizeof(*unused));
4885 *unused = output->crtc_id;
4886
4887 /* Force resetting unused CRTCs */
4888 b->state_invalid = true;
4889
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03004890 output->crtc_id = 0;
4891 output->cursor_plane = NULL;
4892 output->scanout_plane = NULL;
4893}
4894
Pekka Paalanenc0eb2542017-11-15 13:37:18 +02004895static void
4896drm_output_print_modes(struct drm_output *output)
4897{
4898 struct weston_mode *m;
4899 struct drm_mode *dm;
4900
4901 wl_list_for_each(m, &output->base.mode_list, link) {
4902 dm = to_drm_mode(m);
4903
4904 weston_log_continue(STAMP_SPACE "%dx%d@%.1f%s%s, %.1f MHz\n",
4905 m->width, m->height, m->refresh / 1000.0,
4906 m->flags & WL_OUTPUT_MODE_PREFERRED ?
4907 ", preferred" : "",
4908 m->flags & WL_OUTPUT_MODE_CURRENT ?
4909 ", current" : "",
4910 dm->mode_info.clock / 1000.0);
4911 }
4912}
4913
Pekka Paalanenc4db6f72017-09-05 16:37:03 +03004914static int
Armin Krezović08368132016-09-30 14:11:05 +02004915drm_output_enable(struct weston_output *base)
4916{
4917 struct drm_output *output = to_drm_output(base);
4918 struct drm_backend *b = to_drm_backend(base->compositor);
Pekka Paalanence724242017-09-04 12:21:24 +03004919 struct drm_head *head = to_drm_head(weston_output_get_first_head(base));
Pekka Paalanen663d5e92017-09-08 13:32:40 +03004920 drmModeRes *resources;
4921 int ret;
4922
4923 resources = drmModeGetResources(b->drm.fd);
4924 if (!resources) {
4925 weston_log("drmModeGetResources failed\n");
4926 return -1;
4927 }
4928 ret = drm_output_init_crtc(output, resources, head->connector);
4929 drmModeFreeResources(resources);
4930 if (ret < 0)
4931 return -1;
4932
4933 if (drm_output_init_gamma_size(output) < 0)
4934 goto err;
Armin Krezović08368132016-09-30 14:11:05 +02004935
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00004936 if (b->pageflip_timeout)
4937 drm_output_pageflip_timer_create(output);
4938
Giulio Camuffo954f1832014-10-11 18:27:30 +03004939 if (b->use_pixman) {
4940 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004941 weston_log("Failed to init output pixman state\n");
Daniel Stone02cf4662017-03-03 16:19:39 +00004942 goto err;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004943 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03004944 } else if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02004945 weston_log("Failed to init output gl state\n");
Daniel Stone02cf4662017-03-03 16:19:39 +00004946 goto err;
Kristian Høgsberg1d1e0a52012-10-21 13:29:26 -04004947 }
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04004948
Pekka Paalanenf8b850d2017-11-15 12:51:01 +02004949 drm_output_init_backlight(output);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004950
Jonas Ådahle5a12252013-04-05 23:07:11 +02004951 output->base.start_repaint_loop = drm_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05004952 output->base.repaint = drm_output_repaint;
Jesse Barnes58ef3792012-02-23 09:45:49 -05004953 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004954 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08004955 output->base.switch_mode = drm_output_switch_mode;
Richard Hughese7299962013-05-01 21:52:12 +01004956 output->base.set_gamma = drm_output_set_gamma;
4957
Daniel Stone2ba17f42015-05-19 20:02:41 +01004958 if (output->cursor_plane)
4959 weston_compositor_stack_plane(b->compositor,
4960 &output->cursor_plane->base,
4961 NULL);
4962 else
4963 b->cursors_are_broken = 1;
4964
Daniel Stonee2e80132018-01-16 15:37:33 +00004965 weston_compositor_stack_plane(b->compositor,
4966 &output->scanout_plane->base,
Giulio Camuffo954f1832014-10-11 18:27:30 +03004967 &b->compositor->primary_plane);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004968
Pekka Paalanenc0eb2542017-11-15 13:37:18 +02004969 weston_log("Output %s (crtc %d) video modes:\n",
4970 output->base.name, output->crtc_id);
4971 drm_output_print_modes(output);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04004972
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004973 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01004974
Daniel Stone02cf4662017-03-03 16:19:39 +00004975err:
Pekka Paalanen663d5e92017-09-08 13:32:40 +03004976 drm_output_fini_crtc(output);
4977
David Herrmann0f0d54e2011-12-08 17:05:45 +01004978 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004979}
4980
Jesse Barnes58ef3792012-02-23 09:45:49 -05004981static void
Armin Krezović08368132016-09-30 14:11:05 +02004982drm_output_deinit(struct weston_output *base)
4983{
4984 struct drm_output *output = to_drm_output(base);
4985 struct drm_backend *b = to_drm_backend(base->compositor);
4986
Daniel Stone3e661f72016-11-04 17:24:06 +00004987 if (b->use_pixman)
Armin Krezović08368132016-09-30 14:11:05 +02004988 drm_output_fini_pixman(output);
Daniel Stone3e661f72016-11-04 17:24:06 +00004989 else
4990 drm_output_fini_egl(output);
Armin Krezović08368132016-09-30 14:11:05 +02004991
Daniel Stone2ba17f42015-05-19 20:02:41 +01004992 /* Since our planes are no longer in use anywhere, remove their base
4993 * weston_plane's link from the plane stacking list, unless we're
4994 * shutting down, in which case the plane has already been
4995 * destroyed. */
Daniel Stonee2e80132018-01-16 15:37:33 +00004996 if (!b->shutting_down) {
4997 wl_list_remove(&output->scanout_plane->base.link);
4998 wl_list_init(&output->scanout_plane->base.link);
4999
5000 if (output->cursor_plane) {
5001 wl_list_remove(&output->cursor_plane->base.link);
5002 wl_list_init(&output->cursor_plane->base.link);
5003 /* Turn off hardware cursor */
5004 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
5005 }
Daniel Stone2ba17f42015-05-19 20:02:41 +01005006 }
Daniel Stone087ddf02017-02-14 17:51:30 +00005007
Pekka Paalanen663d5e92017-09-08 13:32:40 +03005008 drm_output_fini_crtc(output);
Armin Krezović08368132016-09-30 14:11:05 +02005009}
5010
5011static void
Pekka Paalanenc112f002017-08-28 16:27:20 +03005012drm_head_destroy(struct drm_head *head);
5013
5014static void
Armin Krezović08368132016-09-30 14:11:05 +02005015drm_output_destroy(struct weston_output *base)
5016{
5017 struct drm_output *output = to_drm_output(base);
5018 struct drm_backend *b = to_drm_backend(base->compositor);
Armin Krezović08368132016-09-30 14:11:05 +02005019
Daniel Stone598ee9d2016-11-16 11:55:20 +00005020 if (output->page_flip_pending || output->vblank_pending ||
5021 output->atomic_complete_pending) {
Armin Krezović08368132016-09-30 14:11:05 +02005022 output->destroy_pending = 1;
5023 weston_log("destroy output while page flip pending\n");
5024 return;
5025 }
5026
5027 if (output->base.enabled)
5028 drm_output_deinit(&output->base);
5029
Pekka Paalanen383b3af2017-09-11 14:40:48 +03005030 drm_mode_list_destroy(b, &output->base.mode_list);
Armin Krezović445b41b2016-10-09 23:48:16 +02005031
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00005032 if (output->pageflip_timer)
5033 wl_event_source_remove(output->pageflip_timer);
5034
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005035 weston_output_release(&output->base);
Armin Krezović08368132016-09-30 14:11:05 +02005036
Daniel Stone7b2ddac2016-11-11 19:11:49 +00005037 assert(!output->state_last);
5038 drm_output_state_free(output->state_cur);
5039
Armin Krezović08368132016-09-30 14:11:05 +02005040 free(output);
5041}
5042
5043static int
5044drm_output_disable(struct weston_output *base)
5045{
5046 struct drm_output *output = to_drm_output(base);
Armin Krezović08368132016-09-30 14:11:05 +02005047
Daniel Stone598ee9d2016-11-16 11:55:20 +00005048 if (output->page_flip_pending || output->vblank_pending ||
5049 output->atomic_complete_pending) {
Armin Krezović08368132016-09-30 14:11:05 +02005050 output->disable_pending = 1;
5051 return -1;
5052 }
5053
Daniel Stonea08512f2016-11-08 17:46:10 +00005054 weston_log("Disabling output %s\n", output->base.name);
Daniel Stonea08512f2016-11-08 17:46:10 +00005055
Armin Krezović08368132016-09-30 14:11:05 +02005056 if (output->base.enabled)
5057 drm_output_deinit(&output->base);
5058
5059 output->disable_pending = 0;
5060
Armin Krezović08368132016-09-30 14:11:05 +02005061 return 0;
5062}
5063
5064/**
Daniel Stone087ddf02017-02-14 17:51:30 +00005065 * Update the list of unused connectors and CRTCs
5066 *
Pekka Paalaneneacec812017-09-12 13:43:51 +03005067 * This keeps the unused_crtc arrays up to date.
Daniel Stone087ddf02017-02-14 17:51:30 +00005068 *
5069 * @param b Weston backend structure
5070 * @param resources DRM resources for this device
5071 */
5072static void
5073drm_backend_update_unused_outputs(struct drm_backend *b, drmModeRes *resources)
5074{
5075 int i;
5076
Daniel Stone087ddf02017-02-14 17:51:30 +00005077 wl_array_release(&b->unused_crtcs);
5078 wl_array_init(&b->unused_crtcs);
5079
5080 for (i = 0; i < resources->count_crtcs; i++) {
5081 struct drm_output *output;
5082 uint32_t *crtc_id;
5083
5084 output = drm_output_find_by_crtc(b, resources->crtcs[i]);
5085 if (output && output->base.enabled)
5086 continue;
5087
5088 crtc_id = wl_array_add(&b->unused_crtcs, sizeof(*crtc_id));
5089 *crtc_id = resources->crtcs[i];
5090 }
5091}
5092
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03005093/** Replace connector data and monitor information
5094 *
5095 * @param head The head to update.
5096 * @param connector The connector data to be owned by the head, must match
5097 * the head's connector ID.
5098 * @return 0 on success, -1 on failure.
5099 *
5100 * Takes ownership of @c connector on success, not on failure.
5101 *
5102 * May schedule a heads changed call.
5103 */
5104static int
5105drm_head_assign_connector_info(struct drm_head *head,
5106 drmModeConnector *connector)
5107{
5108 drmModeObjectProperties *props;
5109 const char *make = "unknown";
5110 const char *model = "unknown";
5111 const char *serial_number = "unknown";
5112
5113 assert(connector);
5114 assert(head->connector_id == connector->connector_id);
5115
5116 props = drmModeObjectGetProperties(head->backend->drm.fd,
5117 head->connector_id,
5118 DRM_MODE_OBJECT_CONNECTOR);
5119 if (!props) {
5120 weston_log("Error: failed to get connector '%s' properties\n",
5121 head->base.name);
5122 return -1;
5123 }
5124
5125 if (head->connector)
5126 drmModeFreeConnector(head->connector);
5127 head->connector = connector;
5128
5129 drm_property_info_populate(head->backend, connector_props,
5130 head->props_conn,
5131 WDRM_CONNECTOR__COUNT, props);
5132 find_and_parse_output_edid(head, props, &make, &model, &serial_number);
5133 weston_head_set_monitor_strings(&head->base, make, model, serial_number);
5134 weston_head_set_subpixel(&head->base,
5135 drm_subpixel_to_wayland(head->connector->subpixel));
5136
5137 weston_head_set_physical_size(&head->base, head->connector->mmWidth,
5138 head->connector->mmHeight);
5139
5140 drmModeFreeObjectProperties(props);
5141
5142 /* Unknown connection status is assumed disconnected. */
5143 weston_head_set_connection_status(&head->base,
5144 head->connector->connection == DRM_MODE_CONNECTED);
5145
5146 return 0;
5147}
5148
Pekka Paalanen456dc732017-11-09 15:10:11 +02005149static void
5150drm_head_log_info(struct drm_head *head, const char *msg)
5151{
5152 if (head->base.connected) {
5153 weston_log("DRM: head '%s' %s, connector %d is connected, "
5154 "EDID make '%s', model '%s', serial '%s'\n",
5155 head->base.name, msg, head->connector_id,
5156 head->base.make, head->base.model,
5157 head->base.serial_number ?: "");
5158 } else {
5159 weston_log("DRM: head '%s' %s, connector %d is disconnected.\n",
5160 head->base.name, msg, head->connector_id);
5161 }
5162}
5163
Pekka Paalanend2e62422017-09-08 15:48:07 +03005164/** Update connector and monitor information
5165 *
5166 * @param head The head to update.
5167 *
5168 * Re-reads the DRM property lists for the connector and updates monitor
5169 * information and connection status. This may schedule a heads changed call
5170 * to the user.
5171 */
5172static void
5173drm_head_update_info(struct drm_head *head)
5174{
5175 drmModeConnector *connector;
5176
5177 connector = drmModeGetConnector(head->backend->drm.fd,
5178 head->connector_id);
5179 if (!connector) {
5180 weston_log("DRM: getting connector info for '%s' failed.\n",
5181 head->base.name);
5182 return;
5183 }
5184
5185 if (drm_head_assign_connector_info(head, connector) < 0)
5186 drmModeFreeConnector(connector);
Pekka Paalanen456dc732017-11-09 15:10:11 +02005187
5188 if (head->base.device_changed)
5189 drm_head_log_info(head, "updated");
Pekka Paalanend2e62422017-09-08 15:48:07 +03005190}
5191
Daniel Stone087ddf02017-02-14 17:51:30 +00005192/**
Pekka Paalanenc112f002017-08-28 16:27:20 +03005193 * Create a Weston head for a connector
5194 *
5195 * Given a DRM connector, create a matching drm_head structure and add it
5196 * to Weston's head list.
5197 *
5198 * @param b Weston backend structure
5199 * @param connector_id DRM connector ID for the head
5200 * @param drm_device udev device pointer
5201 * @returns The new head, or NULL on failure.
5202 */
5203static struct drm_head *
5204drm_head_create(struct drm_backend *backend, uint32_t connector_id,
5205 struct udev_device *drm_device)
5206{
5207 struct drm_head *head;
5208 drmModeConnector *connector;
5209 char *name;
5210
5211 head = zalloc(sizeof *head);
5212 if (!head)
5213 return NULL;
5214
5215 connector = drmModeGetConnector(backend->drm.fd, connector_id);
5216 if (!connector)
5217 goto err_alloc;
5218
5219 name = make_connector_name(connector);
5220 if (!name)
5221 goto err_alloc;
5222
5223 weston_head_init(&head->base, name);
5224 free(name);
5225
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03005226 head->connector_id = connector_id;
Pekka Paalanenc112f002017-08-28 16:27:20 +03005227 head->backend = backend;
5228
Pekka Paalanence724242017-09-04 12:21:24 +03005229 head->backlight = backlight_init(drm_device, connector->connector_type);
5230
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03005231 if (drm_head_assign_connector_info(head, connector) < 0)
5232 goto err_init;
5233
5234 if (head->connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
5235 head->connector->connector_type == DRM_MODE_CONNECTOR_eDP)
5236 weston_head_set_internal(&head->base);
Pekka Paalanenc112f002017-08-28 16:27:20 +03005237
Pekka Paalanen13d233e2017-09-11 14:06:11 +03005238 if (connector_get_current_mode(head->connector, backend->drm.fd,
5239 &head->inherited_mode) < 0) {
5240 weston_log("Failed to retrieve current mode from connector %d.\n",
5241 head->connector_id);
5242 /* Continue, inherited_mode was memset to zero. */
5243 }
5244
Pekka Paalanenc112f002017-08-28 16:27:20 +03005245 weston_compositor_add_head(backend->compositor, &head->base);
Pekka Paalanen456dc732017-11-09 15:10:11 +02005246 drm_head_log_info(head, "found");
Pekka Paalanenc112f002017-08-28 16:27:20 +03005247
5248 return head;
5249
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03005250err_init:
5251 weston_head_release(&head->base);
5252
Pekka Paalanenc112f002017-08-28 16:27:20 +03005253err_alloc:
5254 if (connector)
5255 drmModeFreeConnector(connector);
5256
5257 free(head);
5258
5259 return NULL;
5260}
5261
5262static void
5263drm_head_destroy(struct drm_head *head)
5264{
5265 weston_head_release(&head->base);
Pekka Paalanence724242017-09-04 12:21:24 +03005266
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03005267 drm_property_info_free(head->props_conn, WDRM_CONNECTOR__COUNT);
5268 drmModeFreeConnector(head->connector);
5269
Pekka Paalanence724242017-09-04 12:21:24 +03005270 if (head->backlight)
5271 backlight_destroy(head->backlight);
5272
Pekka Paalanenc112f002017-08-28 16:27:20 +03005273 free(head);
5274}
5275
5276/**
Armin Krezović08368132016-09-30 14:11:05 +02005277 * Create a Weston output structure
5278 *
Pekka Paalanend2e62422017-09-08 15:48:07 +03005279 * Create an "empty" drm_output. This is the implementation of
5280 * weston_backend::create_output.
Armin Krezović08368132016-09-30 14:11:05 +02005281 *
Pekka Paalanend2e62422017-09-08 15:48:07 +03005282 * Creating an output is usually followed by drm_output_attach_head()
5283 * and drm_output_enable() to make use of it.
5284 *
5285 * @param compositor The compositor instance.
5286 * @param name Name for the new output.
5287 * @returns The output, or NULL on failure.
Armin Krezović08368132016-09-30 14:11:05 +02005288 */
Pekka Paalanend2e62422017-09-08 15:48:07 +03005289static struct weston_output *
5290drm_output_create(struct weston_compositor *compositor, const char *name)
Armin Krezović08368132016-09-30 14:11:05 +02005291{
Pekka Paalanend2e62422017-09-08 15:48:07 +03005292 struct drm_backend *b = to_drm_backend(compositor);
Armin Krezović08368132016-09-30 14:11:05 +02005293 struct drm_output *output;
Armin Krezović08368132016-09-30 14:11:05 +02005294
Armin Krezović08368132016-09-30 14:11:05 +02005295 output = zalloc(sizeof *output);
5296 if (output == NULL)
Pekka Paalanend2e62422017-09-08 15:48:07 +03005297 return NULL;
Armin Krezović08368132016-09-30 14:11:05 +02005298
Pekka Paalanend2e62422017-09-08 15:48:07 +03005299 weston_output_init(&output->base, compositor, name);
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03005300
Armin Krezović08368132016-09-30 14:11:05 +02005301 output->base.enable = drm_output_enable;
5302 output->base.destroy = drm_output_destroy;
5303 output->base.disable = drm_output_disable;
Pekka Paalanenc112f002017-08-28 16:27:20 +03005304 output->base.attach_head = drm_output_attach_head;
Armin Krezović08368132016-09-30 14:11:05 +02005305
5306 output->destroy_pending = 0;
5307 output->disable_pending = 0;
Armin Krezović08368132016-09-30 14:11:05 +02005308
Pekka Paalanen01f60212017-03-24 15:39:24 +02005309 output->state_cur = drm_output_state_alloc(output, NULL);
Pekka Paalanena0bfedc2017-04-03 14:42:51 +03005310
Armin Krezović08368132016-09-30 14:11:05 +02005311 weston_compositor_add_pending_output(&output->base, b->compositor);
5312
Pekka Paalanend2e62422017-09-08 15:48:07 +03005313 return &output->base;
Armin Krezović08368132016-09-30 14:11:05 +02005314}
5315
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005316static int
Pekka Paalanend2e62422017-09-08 15:48:07 +03005317drm_backend_create_heads(struct drm_backend *b, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005318{
Pekka Paalanend2e62422017-09-08 15:48:07 +03005319 struct drm_head *head;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005320 drmModeRes *resources;
5321 int i;
5322
Giulio Camuffo954f1832014-10-11 18:27:30 +03005323 resources = drmModeGetResources(b->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005324 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02005325 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005326 return -1;
5327 }
5328
Giulio Camuffo954f1832014-10-11 18:27:30 +03005329 b->min_width = resources->min_width;
5330 b->max_width = resources->max_width;
5331 b->min_height = resources->min_height;
5332 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05005333
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005334 for (i = 0; i < resources->count_connectors; i++) {
Pekka Paalanend2e62422017-09-08 15:48:07 +03005335 uint32_t connector_id = resources->connectors[i];
Daniel Stone02cf4662017-03-03 16:19:39 +00005336
Pekka Paalanend2e62422017-09-08 15:48:07 +03005337 head = drm_head_create(b, connector_id, drm_device);
5338 if (!head) {
5339 weston_log("DRM: failed to create head for connector %d.\n",
5340 connector_id);
Benjamin Franzke9eaee352011-08-02 13:03:54 +02005341 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005342 }
5343
Daniel Stone087ddf02017-02-14 17:51:30 +00005344 drm_backend_update_unused_outputs(b, resources);
5345
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005346 drmModeFreeResources(resources);
5347
5348 return 0;
5349}
5350
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005351static void
Pekka Paalanend2e62422017-09-08 15:48:07 +03005352drm_backend_update_heads(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005353{
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005354 drmModeRes *resources;
Pekka Paalanena0a37462017-08-31 15:41:57 +03005355 struct weston_head *base, *next;
5356 struct drm_head *head;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005357 int i;
5358
Giulio Camuffo954f1832014-10-11 18:27:30 +03005359 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005360 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02005361 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005362 return;
5363 }
5364
Pekka Paalanend2e62422017-09-08 15:48:07 +03005365 /* collect new connectors that have appeared, e.g. MST */
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005366 for (i = 0; i < resources->count_connectors; i++) {
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +00005367 uint32_t connector_id = resources->connectors[i];
Benjamin Franzke117483d2011-08-30 11:38:26 +02005368
Pekka Paalanend2e62422017-09-08 15:48:07 +03005369 head = drm_head_find_by_connector(b, connector_id);
5370 if (head) {
5371 drm_head_update_info(head);
5372 } else {
5373 head = drm_head_create(b, connector_id, drm_device);
5374 if (!head)
5375 weston_log("DRM: failed to create head for hot-added connector %d.\n",
5376 connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01005377 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005378 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005379
Pekka Paalanend2e62422017-09-08 15:48:07 +03005380 /* Remove connectors that have disappeared. */
Pekka Paalanena0a37462017-08-31 15:41:57 +03005381 wl_list_for_each_safe(base, next,
5382 &b->compositor->head_list, compositor_link) {
Pekka Paalanend2e62422017-09-08 15:48:07 +03005383 bool removed = true;
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00005384
Pekka Paalanena0a37462017-08-31 15:41:57 +03005385 head = to_drm_head(base);
5386
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00005387 for (i = 0; i < resources->count_connectors; i++) {
Pekka Paalanend2e62422017-09-08 15:48:07 +03005388 if (resources->connectors[i] == head->connector_id) {
5389 removed = false;
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00005390 break;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005391 }
5392 }
Armin Krezović08368132016-09-30 14:11:05 +02005393
Pekka Paalanend2e62422017-09-08 15:48:07 +03005394 if (!removed)
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00005395 continue;
5396
Pekka Paalanend2e62422017-09-08 15:48:07 +03005397 weston_log("DRM: head '%s' (connector %d) disappeared.\n",
5398 head->base.name, head->connector_id);
5399 drm_head_destroy(head);
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00005400 }
5401
Daniel Stone087ddf02017-02-14 17:51:30 +00005402 drm_backend_update_unused_outputs(b, resources);
5403
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00005404 drmModeFreeResources(resources);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005405}
5406
5407static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03005408udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005409{
David Herrmannd7488c22012-03-11 20:05:21 +01005410 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01005411 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01005412
5413 sysnum = udev_device_get_sysnum(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005414 if (!sysnum || atoi(sysnum) != b->drm.id)
David Herrmannd7488c22012-03-11 20:05:21 +01005415 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02005416
David Herrmann6ac52db2012-03-11 20:05:22 +01005417 val = udev_device_get_property_value(device, "HOTPLUG");
5418 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005419 return 0;
5420
David Herrmann6ac52db2012-03-11 20:05:22 +01005421 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005422}
5423
Kristian Høgsbergb1868472011-04-22 12:27:57 -04005424static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005425udev_drm_event(int fd, uint32_t mask, void *data)
5426{
Giulio Camuffo954f1832014-10-11 18:27:30 +03005427 struct drm_backend *b = data;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005428 struct udev_device *event;
5429
Giulio Camuffo954f1832014-10-11 18:27:30 +03005430 event = udev_monitor_receive_device(b->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02005431
Giulio Camuffo954f1832014-10-11 18:27:30 +03005432 if (udev_event_is_hotplug(b, event))
Pekka Paalanend2e62422017-09-08 15:48:07 +03005433 drm_backend_update_heads(b, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005434
5435 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04005436
5437 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005438}
5439
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005440static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005441drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005442{
Armin Krezović545dba62016-08-05 15:54:18 +02005443 struct drm_backend *b = to_drm_backend(ec);
Pekka Paalanenc112f002017-08-28 16:27:20 +03005444 struct weston_head *base, *next;
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005445
Giulio Camuffo954f1832014-10-11 18:27:30 +03005446 udev_input_destroy(&b->input);
Jonas Ådahlc97af922012-03-28 22:36:09 +02005447
Giulio Camuffo954f1832014-10-11 18:27:30 +03005448 wl_event_source_remove(b->udev_drm_source);
5449 wl_event_source_remove(b->drm_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02005450
Daniel Stoneb57c6a02017-10-05 16:27:21 +01005451 b->shutting_down = true;
5452
Giulio Camuffo954f1832014-10-11 18:27:30 +03005453 destroy_sprites(b);
Kristian Høgsberg3d64a3e2013-05-10 12:36:04 -04005454
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03005455 weston_compositor_shutdown(ec);
5456
Pekka Paalanenc112f002017-08-28 16:27:20 +03005457 wl_list_for_each_safe(base, next, &ec->head_list, compositor_link)
5458 drm_head_destroy(to_drm_head(base));
5459
Giulio Camuffo954f1832014-10-11 18:27:30 +03005460 if (b->gbm)
5461 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02005462
Pekka Paalanen5b0aa552017-12-07 16:06:05 +02005463 udev_monitor_unref(b->udev_monitor);
Pekka Paalanen2a0c6c32017-09-13 16:48:01 +03005464 udev_unref(b->udev);
5465
Giulio Camuffo954f1832014-10-11 18:27:30 +03005466 weston_launcher_destroy(ec->launcher);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005467
Daniel Stone087ddf02017-02-14 17:51:30 +00005468 wl_array_release(&b->unused_crtcs);
Daniel Stone087ddf02017-02-14 17:51:30 +00005469
Giulio Camuffo954f1832014-10-11 18:27:30 +03005470 close(b->drm.fd);
Pekka Paalanen9bf4f372017-12-07 16:05:29 +02005471 free(b->drm.filename);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005472 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005473}
5474
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04005475static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -07005476session_notify(struct wl_listener *listener, void *data)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04005477{
Kristian Høgsberg61741a22013-09-17 16:02:57 -07005478 struct weston_compositor *compositor = data;
Armin Krezović545dba62016-08-05 15:54:18 +02005479 struct drm_backend *b = to_drm_backend(compositor);
Daniel Stone085d2b92015-05-21 00:00:57 +01005480 struct drm_plane *plane;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04005481 struct drm_output *output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04005482
Giulio Camuffo954f1832014-10-11 18:27:30 +03005483 if (compositor->session_active) {
Kristian Høgsberg61741a22013-09-17 16:02:57 -07005484 weston_log("activating session\n");
Daniel Stonef33e1042016-11-05 08:10:13 +00005485 weston_compositor_wake(compositor);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005486 weston_compositor_damage_all(compositor);
Daniel Stone6020f472018-02-05 15:46:20 +00005487 b->state_invalid = true;
Giulio Camuffo954f1832014-10-11 18:27:30 +03005488 udev_input_enable(&b->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07005489 } else {
5490 weston_log("deactivating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03005491 udev_input_disable(&b->input);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04005492
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005493 weston_compositor_offscreen(compositor);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04005494
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05005495 /* If we have a repaint scheduled (either from a
5496 * pending pageflip or the idle handler), make sure we
5497 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005498 * vt switched away. The OFFSCREEN state will prevent
Abdur Rehman4dca0e12017-01-01 19:46:35 +05005499 * further attempts at repainting. When we switch
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05005500 * back, we schedule a repaint, which will process
5501 * pending frame callbacks. */
5502
Giulio Camuffo954f1832014-10-11 18:27:30 +03005503 wl_list_for_each(output, &compositor->output_list, base.link) {
Daniel Stone09a97e22017-03-01 11:34:06 +00005504 output->base.repaint_needed = false;
Daniel Stone2ba17f42015-05-19 20:02:41 +01005505 if (output->cursor_plane)
5506 drmModeSetCursor(b->drm.fd, output->crtc_id,
5507 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05005508 }
5509
Giulio Camuffo954f1832014-10-11 18:27:30 +03005510 output = container_of(compositor->output_list.next,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04005511 struct drm_output, base.link);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05005512
Daniel Stone085d2b92015-05-21 00:00:57 +01005513 wl_list_for_each(plane, &b->plane_list, link) {
5514 if (plane->type != WDRM_PLANE_TYPE_OVERLAY)
5515 continue;
5516
Giulio Camuffo954f1832014-10-11 18:27:30 +03005517 drmModeSetPlane(b->drm.fd,
Daniel Stone085d2b92015-05-21 00:00:57 +01005518 plane->plane_id,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04005519 output->crtc_id, 0, 0,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05005520 0, 0, 0, 0, 0, 0, 0, 0);
Daniel Stone085d2b92015-05-21 00:00:57 +01005521 }
5522 }
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04005523}
5524
Daniel Stoneefa504f2016-12-19 16:48:20 +00005525/**
5526 * Determines whether or not a device is capable of modesetting. If successful,
5527 * sets b->drm.fd and b->drm.filename to the opened device.
5528 */
5529static bool
5530drm_device_is_kms(struct drm_backend *b, struct udev_device *device)
5531{
5532 const char *filename = udev_device_get_devnode(device);
5533 const char *sysnum = udev_device_get_sysnum(device);
5534 drmModeRes *res;
5535 int id, fd;
5536
5537 if (!filename)
5538 return false;
5539
5540 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
5541 if (fd < 0)
5542 return false;
5543
5544 res = drmModeGetResources(fd);
5545 if (!res)
5546 goto out_fd;
5547
5548 if (res->count_crtcs <= 0 || res->count_connectors <= 0 ||
5549 res->count_encoders <= 0)
5550 goto out_res;
5551
5552 if (sysnum)
5553 id = atoi(sysnum);
5554 if (!sysnum || id < 0) {
5555 weston_log("couldn't get sysnum for device %s\n", filename);
5556 goto out_res;
5557 }
5558
5559 /* We can be called successfully on multiple devices; if we have,
5560 * clean up old entries. */
5561 if (b->drm.fd >= 0)
5562 weston_launcher_close(b->compositor->launcher, b->drm.fd);
5563 free(b->drm.filename);
5564
5565 b->drm.fd = fd;
5566 b->drm.id = id;
5567 b->drm.filename = strdup(filename);
5568
Sergi Granellceb59812017-03-28 12:44:04 +02005569 drmModeFreeResources(res);
5570
Daniel Stoneefa504f2016-12-19 16:48:20 +00005571 return true;
5572
5573out_res:
5574 drmModeFreeResources(res);
5575out_fd:
5576 weston_launcher_close(b->compositor->launcher, fd);
5577 return false;
5578}
5579
David Herrmann0af066f2012-10-29 19:21:16 +01005580/*
5581 * Find primary GPU
5582 * Some systems may have multiple DRM devices attached to a single seat. This
5583 * function loops over all devices and tries to find a PCI device with the
5584 * boot_vga sysfs attribute set to 1.
5585 * If no such device is found, the first DRM device reported by udev is used.
Daniel Stoneefa504f2016-12-19 16:48:20 +00005586 * Devices are also vetted to make sure they are are capable of modesetting,
5587 * rather than pure render nodes (GPU with no display), or pure
5588 * memory-allocation devices (VGEM).
David Herrmann0af066f2012-10-29 19:21:16 +01005589 */
5590static struct udev_device*
Giulio Camuffo954f1832014-10-11 18:27:30 +03005591find_primary_gpu(struct drm_backend *b, const char *seat)
David Herrmann0af066f2012-10-29 19:21:16 +01005592{
5593 struct udev_enumerate *e;
5594 struct udev_list_entry *entry;
5595 const char *path, *device_seat, *id;
5596 struct udev_device *device, *drm_device, *pci;
5597
Giulio Camuffo954f1832014-10-11 18:27:30 +03005598 e = udev_enumerate_new(b->udev);
David Herrmann0af066f2012-10-29 19:21:16 +01005599 udev_enumerate_add_match_subsystem(e, "drm");
5600 udev_enumerate_add_match_sysname(e, "card[0-9]*");
5601
5602 udev_enumerate_scan_devices(e);
5603 drm_device = NULL;
5604 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Daniel Stoneefa504f2016-12-19 16:48:20 +00005605 bool is_boot_vga = false;
5606
David Herrmann0af066f2012-10-29 19:21:16 +01005607 path = udev_list_entry_get_name(entry);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005608 device = udev_device_new_from_syspath(b->udev, path);
David Herrmann0af066f2012-10-29 19:21:16 +01005609 if (!device)
5610 continue;
5611 device_seat = udev_device_get_property_value(device, "ID_SEAT");
5612 if (!device_seat)
5613 device_seat = default_seat;
5614 if (strcmp(device_seat, seat)) {
5615 udev_device_unref(device);
5616 continue;
5617 }
5618
5619 pci = udev_device_get_parent_with_subsystem_devtype(device,
5620 "pci", NULL);
5621 if (pci) {
5622 id = udev_device_get_sysattr_value(pci, "boot_vga");
Daniel Stoneefa504f2016-12-19 16:48:20 +00005623 if (id && !strcmp(id, "1"))
5624 is_boot_vga = true;
David Herrmann0af066f2012-10-29 19:21:16 +01005625 }
5626
Daniel Stoneefa504f2016-12-19 16:48:20 +00005627 /* If we already have a modesetting-capable device, and this
5628 * device isn't our boot-VGA device, we aren't going to use
5629 * it. */
5630 if (!is_boot_vga && drm_device) {
David Herrmann0af066f2012-10-29 19:21:16 +01005631 udev_device_unref(device);
Daniel Stoneefa504f2016-12-19 16:48:20 +00005632 continue;
5633 }
5634
5635 /* Make sure this device is actually capable of modesetting;
5636 * if this call succeeds, b->drm.{fd,filename} will be set,
5637 * and any old values freed. */
5638 if (!drm_device_is_kms(b, device)) {
5639 udev_device_unref(device);
5640 continue;
5641 }
5642
5643 /* There can only be one boot_vga device, and we try to use it
5644 * at all costs. */
5645 if (is_boot_vga) {
5646 if (drm_device)
5647 udev_device_unref(drm_device);
5648 drm_device = device;
5649 break;
5650 }
5651
5652 /* Per the (!is_boot_vga && drm_device) test above, we only
5653 * trump existing saved devices with boot-VGA devices, so if
5654 * we end up here, this must be the first device we've seen. */
5655 assert(!drm_device);
5656 drm_device = device;
David Herrmann0af066f2012-10-29 19:21:16 +01005657 }
5658
Daniel Stoneefa504f2016-12-19 16:48:20 +00005659 /* If we're returning a device to use, we must have an open FD for
5660 * it. */
5661 assert(!!drm_device == (b->drm.fd >= 0));
5662
David Herrmann0af066f2012-10-29 19:21:16 +01005663 udev_enumerate_unref(e);
5664 return drm_device;
5665}
5666
Pekka Paalanenb45ed8b2017-03-28 18:04:27 +03005667static struct udev_device *
5668open_specific_drm_device(struct drm_backend *b, const char *name)
5669{
5670 struct udev_device *device;
5671
5672 device = udev_device_new_from_subsystem_sysname(b->udev, "drm", name);
5673 if (!device) {
5674 weston_log("ERROR: could not open DRM device '%s'\n", name);
5675 return NULL;
5676 }
5677
5678 if (!drm_device_is_kms(b, device)) {
5679 udev_device_unref(device);
5680 weston_log("ERROR: DRM device '%s' is not a KMS device.\n", name);
5681 return NULL;
5682 }
5683
5684 /* If we're returning a device to use, we must have an open FD for
5685 * it. */
5686 assert(b->drm.fd >= 0);
5687
5688 return device;
5689}
5690
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02005691static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02005692planes_binding(struct weston_keyboard *keyboard, const struct timespec *time,
5693 uint32_t key, void *data)
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02005694{
Giulio Camuffo954f1832014-10-11 18:27:30 +03005695 struct drm_backend *b = data;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02005696
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02005697 switch (key) {
5698 case KEY_C:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005699 b->cursors_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02005700 break;
5701 case KEY_V:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005702 b->sprites_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02005703 break;
5704 case KEY_O:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005705 b->sprites_hidden ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02005706 break;
5707 default:
5708 break;
5709 }
Ander Conselvan de Oliveira180f42a2012-11-21 15:11:37 +02005710}
5711
Kristian Høgsberg0eac34a2013-08-30 14:28:22 -07005712#ifdef BUILD_VAAPI_RECORDER
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005713static void
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03005714recorder_destroy(struct drm_output *output)
5715{
5716 vaapi_recorder_destroy(output->recorder);
5717 output->recorder = NULL;
5718
5719 output->base.disable_planes--;
5720
5721 wl_list_remove(&output->recorder_frame_listener.link);
5722 weston_log("[libva recorder] done\n");
5723}
5724
5725static void
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005726recorder_frame_notify(struct wl_listener *listener, void *data)
5727{
5728 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03005729 struct drm_backend *b;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005730 int fd, ret;
5731
5732 output = container_of(listener, struct drm_output,
5733 recorder_frame_listener);
Armin Krezović545dba62016-08-05 15:54:18 +02005734 b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005735
5736 if (!output->recorder)
5737 return;
5738
Daniel Stonee2e80132018-01-16 15:37:33 +00005739 ret = drmPrimeHandleToFD(b->drm.fd,
5740 output->scanout_plane->state_cur->fb->handle,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005741 DRM_CLOEXEC, &fd);
5742 if (ret) {
5743 weston_log("[libva recorder] "
5744 "failed to create prime fd for front buffer\n");
5745 return;
5746 }
5747
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03005748 ret = vaapi_recorder_frame(output->recorder, fd,
Daniel Stonee2e80132018-01-16 15:37:33 +00005749 output->scanout_plane->state_cur->fb->stride);
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03005750 if (ret < 0) {
5751 weston_log("[libva recorder] aborted: %m\n");
5752 recorder_destroy(output);
5753 }
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005754}
5755
5756static void *
Giulio Camuffo954f1832014-10-11 18:27:30 +03005757create_recorder(struct drm_backend *b, int width, int height,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005758 const char *filename)
5759{
5760 int fd;
5761 drm_magic_t magic;
5762
Giulio Camuffo954f1832014-10-11 18:27:30 +03005763 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005764 if (fd < 0)
5765 return NULL;
5766
5767 drmGetMagic(fd, &magic);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005768 drmAuthMagic(b->drm.fd, magic);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005769
5770 return vaapi_recorder_create(fd, width, height, filename);
5771}
5772
5773static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02005774recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time,
5775 uint32_t key, void *data)
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005776{
Giulio Camuffo954f1832014-10-11 18:27:30 +03005777 struct drm_backend *b = data;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005778 struct drm_output *output;
5779 int width, height;
5780
Giulio Camuffo954f1832014-10-11 18:27:30 +03005781 output = container_of(b->compositor->output_list.next,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005782 struct drm_output, base.link);
5783
5784 if (!output->recorder) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01005785 if (output->gbm_format != GBM_FORMAT_XRGB8888) {
Ander Conselvan de Oliveira2ef1cd12014-05-06 16:49:06 +03005786 weston_log("failed to start vaapi recorder: "
5787 "output format not supported\n");
5788 return;
5789 }
5790
Hardeningff39efa2013-09-18 23:56:35 +02005791 width = output->base.current_mode->width;
5792 height = output->base.current_mode->height;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005793
5794 output->recorder =
Giulio Camuffo954f1832014-10-11 18:27:30 +03005795 create_recorder(b, width, height, "capture.h264");
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005796 if (!output->recorder) {
5797 weston_log("failed to create vaapi recorder\n");
5798 return;
5799 }
5800
5801 output->base.disable_planes++;
5802
5803 output->recorder_frame_listener.notify = recorder_frame_notify;
5804 wl_signal_add(&output->base.frame_signal,
5805 &output->recorder_frame_listener);
5806
5807 weston_output_schedule_repaint(&output->base);
5808
5809 weston_log("[libva recorder] initialized\n");
5810 } else {
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03005811 recorder_destroy(output);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005812 }
5813}
5814#else
5815static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02005816recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time,
5817 uint32_t key, void *data)
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005818{
5819 weston_log("Compiled without libva support\n");
5820}
5821#endif
5822
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005823static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03005824switch_to_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005825{
5826 struct drm_output *output;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03005827 bool dmabuf_support_inited;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005828
Giulio Camuffo954f1832014-10-11 18:27:30 +03005829 if (!b->use_pixman)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005830 return;
5831
Pekka Paalanene4d231e2014-06-12 15:12:48 +03005832 dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf;
5833
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005834 weston_log("Switching to GL renderer\n");
5835
Giulio Camuffo954f1832014-10-11 18:27:30 +03005836 b->gbm = create_gbm_device(b->drm.fd);
5837 if (!b->gbm) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005838 weston_log("Failed to create gbm device. "
5839 "Aborting renderer switch\n");
5840 return;
5841 }
5842
Giulio Camuffo954f1832014-10-11 18:27:30 +03005843 wl_list_for_each(output, &b->compositor->output_list, base.link)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005844 pixman_renderer_output_destroy(&output->base);
5845
Giulio Camuffo954f1832014-10-11 18:27:30 +03005846 b->compositor->renderer->destroy(b->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005847
Giulio Camuffo954f1832014-10-11 18:27:30 +03005848 if (drm_backend_create_gl_renderer(b) < 0) {
5849 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005850 weston_log("Failed to create GL renderer. Quitting.\n");
5851 /* FIXME: we need a function to shutdown cleanly */
5852 assert(0);
5853 }
5854
Giulio Camuffo954f1832014-10-11 18:27:30 +03005855 wl_list_for_each(output, &b->compositor->output_list, base.link)
5856 drm_output_init_egl(output, b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005857
Giulio Camuffo954f1832014-10-11 18:27:30 +03005858 b->use_pixman = 0;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03005859
5860 if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) {
5861 if (linux_dmabuf_setup(b->compositor) < 0)
5862 weston_log("Error: initializing dmabuf "
5863 "support failed.\n");
5864 }
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005865}
5866
5867static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02005868renderer_switch_binding(struct weston_keyboard *keyboard,
5869 const struct timespec *time, uint32_t key, void *data)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005870{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005871 struct drm_backend *b =
Armin Krezović545dba62016-08-05 15:54:18 +02005872 to_drm_backend(keyboard->seat->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005873
Giulio Camuffo954f1832014-10-11 18:27:30 +03005874 switch_to_gl_renderer(b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005875}
5876
Armin Krezović08368132016-09-30 14:11:05 +02005877static const struct weston_drm_output_api api = {
5878 drm_output_set_mode,
5879 drm_output_set_gbm_format,
5880 drm_output_set_seat,
5881};
5882
Giulio Camuffo954f1832014-10-11 18:27:30 +03005883static struct drm_backend *
5884drm_backend_create(struct weston_compositor *compositor,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005885 struct weston_drm_backend_config *config)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005886{
Giulio Camuffo954f1832014-10-11 18:27:30 +03005887 struct drm_backend *b;
David Herrmann0af066f2012-10-29 19:21:16 +01005888 struct udev_device *drm_device;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005889 struct wl_event_loop *loop;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005890 const char *seat_id = default_seat;
Armin Krezović08368132016-09-30 14:11:05 +02005891 int ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005892
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04005893 weston_log("initializing drm backend\n");
5894
Giulio Camuffo954f1832014-10-11 18:27:30 +03005895 b = zalloc(sizeof *b);
5896 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005897 return NULL;
Daniel Stone725c2c32012-06-22 14:04:36 +01005898
Daniel Stone6020f472018-02-05 15:46:20 +00005899 b->state_invalid = true;
Daniel Stoneefa504f2016-12-19 16:48:20 +00005900 b->drm.fd = -1;
Daniel Stone087ddf02017-02-14 17:51:30 +00005901 wl_array_init(&b->unused_crtcs);
Daniel Stoneefa504f2016-12-19 16:48:20 +00005902
Pekka Paalanen68583832015-05-19 09:53:16 +03005903 /*
5904 * KMS support for hardware planes cannot properly synchronize
5905 * without nuclear page flip. Without nuclear/atomic, hw plane
5906 * and cursor plane updates would either tear or cause extra
5907 * waits for vblanks which means dropping the compositor framerate
Pekka Paalanen3f32a132015-09-07 15:38:43 +03005908 * to a fraction. For cursors, it's not so bad, so they are
5909 * enabled.
Pekka Paalanen68583832015-05-19 09:53:16 +03005910 *
5911 * These can be enabled again when nuclear/atomic support lands.
5912 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03005913 b->sprites_are_broken = 1;
Giulio Camuffo954f1832014-10-11 18:27:30 +03005914 b->compositor = compositor;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005915 b->use_pixman = config->use_pixman;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00005916 b->pageflip_timeout = config->pageflip_timeout;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07005917
Pekka Paalanen7da9a382017-08-30 11:29:49 +03005918 compositor->backend = &b->base;
5919
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005920 if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0)
5921 goto err_compositor;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07005922
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005923 if (config->seat_id)
5924 seat_id = config->seat_id;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02005925
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01005926 /* Check if we run drm-backend using weston-launch */
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005927 compositor->launcher = weston_launcher_connect(compositor, config->tty,
5928 seat_id, true);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005929 if (compositor->launcher == NULL) {
Pekka Paalanena453f4d2017-10-31 10:19:48 +02005930 weston_log("fatal: drm backend should be run using "
5931 "weston-launch binary, or your system should "
5932 "provide the logind D-Bus API.\n");
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01005933 goto err_compositor;
5934 }
5935
Giulio Camuffo954f1832014-10-11 18:27:30 +03005936 b->udev = udev_new();
5937 if (b->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02005938 weston_log("failed to initialize udev context\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07005939 goto err_launcher;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005940 }
5941
Giulio Camuffo954f1832014-10-11 18:27:30 +03005942 b->session_listener.notify = session_notify;
5943 wl_signal_add(&compositor->session_signal, &b->session_listener);
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05005944
Pekka Paalanenb45ed8b2017-03-28 18:04:27 +03005945 if (config->specific_device)
5946 drm_device = open_specific_drm_device(b, config->specific_device);
5947 else
5948 drm_device = find_primary_gpu(b, seat_id);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04005949 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02005950 weston_log("no drm device found\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07005951 goto err_udev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005952 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005953
Daniel Stoneefa504f2016-12-19 16:48:20 +00005954 if (init_kms_caps(b) < 0) {
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02005955 weston_log("failed to initialize kms\n");
5956 goto err_udev_dev;
5957 }
5958
Giulio Camuffo954f1832014-10-11 18:27:30 +03005959 if (b->use_pixman) {
5960 if (init_pixman(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02005961 weston_log("failed to initialize pixman renderer\n");
5962 goto err_udev_dev;
5963 }
5964 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +03005965 if (init_egl(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02005966 weston_log("failed to initialize egl\n");
5967 goto err_udev_dev;
5968 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005969 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05005970
Giulio Camuffo954f1832014-10-11 18:27:30 +03005971 b->base.destroy = drm_destroy;
Daniel Stoneeedf84c2017-02-10 18:06:04 +00005972 b->base.repaint_begin = drm_repaint_begin;
5973 b->base.repaint_flush = drm_repaint_flush;
5974 b->base.repaint_cancel = drm_repaint_cancel;
Pekka Paalanenc112f002017-08-28 16:27:20 +03005975 b->base.create_output = drm_output_create;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02005976
Bob Ham91880f12016-01-12 10:21:47 +00005977 weston_setup_vt_switch_bindings(compositor);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04005978
Daniel Stone085d2b92015-05-21 00:00:57 +01005979 wl_list_init(&b->plane_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005980 create_sprites(b);
Jesse Barnes58ef3792012-02-23 09:45:49 -05005981
Giulio Camuffo954f1832014-10-11 18:27:30 +03005982 if (udev_input_init(&b->input,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03005983 compositor, b->udev, seat_id,
5984 config->configure_device) < 0) {
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03005985 weston_log("failed to create input devices\n");
5986 goto err_sprite;
5987 }
5988
Pekka Paalanend2e62422017-09-08 15:48:07 +03005989 if (drm_backend_create_heads(b, drm_device) < 0) {
5990 weston_log("Failed to create heads for %s\n", b->drm.filename);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03005991 goto err_udev_input;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005992 }
5993
Jason Ekstrand9fc71512014-04-02 19:53:46 -05005994 /* A this point we have some idea of whether or not we have a working
5995 * cursor plane. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03005996 if (!b->cursors_are_broken)
5997 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
Jason Ekstrand9fc71512014-04-02 19:53:46 -05005998
Giulio Camuffo954f1832014-10-11 18:27:30 +03005999 loop = wl_display_get_event_loop(compositor->wl_display);
6000 b->drm_source =
6001 wl_event_loop_add_fd(loop, b->drm.fd,
6002 WL_EVENT_READABLE, on_drm_input, b);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006003
Giulio Camuffo954f1832014-10-11 18:27:30 +03006004 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
6005 if (b->udev_monitor == NULL) {
Abdur Rehman4dca0e12017-01-01 19:46:35 +05006006 weston_log("failed to initialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01006007 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01006008 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03006009 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01006010 "drm", NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03006011 b->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02006012 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03006013 udev_monitor_get_fd(b->udev_monitor),
6014 WL_EVENT_READABLE, udev_drm_event, b);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01006015
Giulio Camuffo954f1832014-10-11 18:27:30 +03006016 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02006017 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01006018 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01006019 }
6020
Daniel Stonea96b93c2012-06-22 14:04:37 +01006021 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01006022
Giulio Camuffo954f1832014-10-11 18:27:30 +03006023 weston_compositor_add_debug_binding(compositor, KEY_O,
6024 planes_binding, b);
6025 weston_compositor_add_debug_binding(compositor, KEY_C,
6026 planes_binding, b);
6027 weston_compositor_add_debug_binding(compositor, KEY_V,
6028 planes_binding, b);
6029 weston_compositor_add_debug_binding(compositor, KEY_Q,
6030 recorder_binding, b);
6031 weston_compositor_add_debug_binding(compositor, KEY_W,
6032 renderer_switch_binding, b);
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02006033
Pekka Paalanene4d231e2014-06-12 15:12:48 +03006034 if (compositor->renderer->import_dmabuf) {
6035 if (linux_dmabuf_setup(compositor) < 0)
6036 weston_log("Error: initializing dmabuf "
6037 "support failed.\n");
6038 }
6039
Armin Krezović08368132016-09-30 14:11:05 +02006040 ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME,
6041 &api, sizeof(api));
6042
6043 if (ret < 0) {
6044 weston_log("Failed to register output API.\n");
6045 goto err_udev_monitor;
6046 }
6047
Giulio Camuffo954f1832014-10-11 18:27:30 +03006048 return b;
Daniel Stonea96b93c2012-06-22 14:04:37 +01006049
6050err_udev_monitor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03006051 wl_event_source_remove(b->udev_drm_source);
6052 udev_monitor_unref(b->udev_monitor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01006053err_drm_source:
Giulio Camuffo954f1832014-10-11 18:27:30 +03006054 wl_event_source_remove(b->drm_source);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03006055err_udev_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03006056 udev_input_destroy(&b->input);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04006057err_sprite:
Emmanuel Gil Peyrotb8347e32016-05-02 22:40:13 +01006058 if (b->gbm)
6059 gbm_device_destroy(b->gbm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03006060 destroy_sprites(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01006061err_udev_dev:
6062 udev_device_unref(drm_device);
Kristian Høgsberg3f495872013-09-18 23:00:17 -07006063err_launcher:
Giulio Camuffo954f1832014-10-11 18:27:30 +03006064 weston_launcher_destroy(compositor->launcher);
Daniel Stonea96b93c2012-06-22 14:04:37 +01006065err_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +03006066 udev_unref(b->udev);
Daniel Stonea96b93c2012-06-22 14:04:37 +01006067err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03006068 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03006069 free(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01006070 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04006071}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006072
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07006073static void
6074config_init_to_defaults(struct weston_drm_backend_config *config)
6075{
6076}
6077
Giulio Camuffo954f1832014-10-11 18:27:30 +03006078WL_EXPORT int
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01006079weston_backend_init(struct weston_compositor *compositor,
6080 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006081{
Giulio Camuffo954f1832014-10-11 18:27:30 +03006082 struct drm_backend *b;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07006083 struct weston_drm_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006084
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07006085 if (config_base == NULL ||
6086 config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION ||
6087 config_base->struct_size > sizeof(struct weston_drm_backend_config)) {
6088 weston_log("drm backend config structure is invalid\n");
6089 return -1;
6090 }
Benjamin Franzke117483d2011-08-30 11:38:26 +02006091
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07006092 config_init_to_defaults(&config);
6093 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07006094
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07006095 b = drm_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03006096 if (b == NULL)
6097 return -1;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07006098
Giulio Camuffo954f1832014-10-11 18:27:30 +03006099 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006100}