blob: 61c9bf5ec73db44252c8bc305ecd1672befd63f0 [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
Daniel Stone02cf4662017-03-03 16:19:39 +000086/**
Daniel Stone02cf4662017-03-03 16:19:39 +000087 * Represents the values of an enum-type KMS property
88 */
89struct drm_property_enum_info {
90 const char *name; /**< name as string (static, not freed) */
91 bool valid; /**< true if value is supported; ignore if false */
92 uint64_t value; /**< raw value */
93};
94
95/**
96 * Holds information on a DRM property, including its ID and the enum
97 * values it holds.
98 *
99 * DRM properties are allocated dynamically, and maintained as DRM objects
100 * within the normal object ID space; they thus do not have a stable ID
101 * to refer to. This includes enum values, which must be referred to by
102 * integer values, but these are not stable.
103 *
104 * drm_property_info allows a cache to be maintained where Weston can use
105 * enum values internally to refer to properties, with the mapping to DRM
106 * ID values being maintained internally.
107 */
108struct drm_property_info {
109 const char *name; /**< name as string (static, not freed) */
110 uint32_t prop_id; /**< KMS property object ID */
111 unsigned int num_enum_values; /**< number of enum values */
112 struct drm_property_enum_info *enum_values; /**< array of enum values */
113};
114
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000115/**
Daniel Stone598ee9d2016-11-16 11:55:20 +0000116 * List of properties attached to DRM planes
117 */
118enum wdrm_plane_property {
119 WDRM_PLANE_TYPE = 0,
120 WDRM_PLANE_SRC_X,
121 WDRM_PLANE_SRC_Y,
122 WDRM_PLANE_SRC_W,
123 WDRM_PLANE_SRC_H,
124 WDRM_PLANE_CRTC_X,
125 WDRM_PLANE_CRTC_Y,
126 WDRM_PLANE_CRTC_W,
127 WDRM_PLANE_CRTC_H,
128 WDRM_PLANE_FB_ID,
129 WDRM_PLANE_CRTC_ID,
130 WDRM_PLANE__COUNT
131};
132
133/**
134 * Possible values for the WDRM_PLANE_TYPE property.
135 */
136enum wdrm_plane_type {
137 WDRM_PLANE_TYPE_PRIMARY = 0,
138 WDRM_PLANE_TYPE_CURSOR,
139 WDRM_PLANE_TYPE_OVERLAY,
140 WDRM_PLANE_TYPE__COUNT
141};
142
143static struct drm_property_enum_info plane_type_enums[] = {
144 [WDRM_PLANE_TYPE_PRIMARY] = {
145 .name = "Primary",
146 },
147 [WDRM_PLANE_TYPE_OVERLAY] = {
148 .name = "Overlay",
149 },
150 [WDRM_PLANE_TYPE_CURSOR] = {
151 .name = "Cursor",
152 },
153};
154
155static const struct drm_property_info plane_props[] = {
156 [WDRM_PLANE_TYPE] = {
157 .name = "type",
158 .enum_values = plane_type_enums,
159 .num_enum_values = WDRM_PLANE_TYPE__COUNT,
160 },
161 [WDRM_PLANE_SRC_X] = { .name = "SRC_X", },
162 [WDRM_PLANE_SRC_Y] = { .name = "SRC_Y", },
163 [WDRM_PLANE_SRC_W] = { .name = "SRC_W", },
164 [WDRM_PLANE_SRC_H] = { .name = "SRC_H", },
165 [WDRM_PLANE_CRTC_X] = { .name = "CRTC_X", },
166 [WDRM_PLANE_CRTC_Y] = { .name = "CRTC_Y", },
167 [WDRM_PLANE_CRTC_W] = { .name = "CRTC_W", },
168 [WDRM_PLANE_CRTC_H] = { .name = "CRTC_H", },
169 [WDRM_PLANE_FB_ID] = { .name = "FB_ID", },
170 [WDRM_PLANE_CRTC_ID] = { .name = "CRTC_ID", },
171};
172
173/**
174 * List of properties attached to a DRM connector
175 */
176enum wdrm_connector_property {
177 WDRM_CONNECTOR_EDID = 0,
178 WDRM_CONNECTOR_DPMS,
179 WDRM_CONNECTOR_CRTC_ID,
180 WDRM_CONNECTOR__COUNT
181};
182
183static const struct drm_property_info connector_props[] = {
184 [WDRM_CONNECTOR_EDID] = { .name = "EDID" },
185 [WDRM_CONNECTOR_DPMS] = { .name = "DPMS" },
186 [WDRM_CONNECTOR_CRTC_ID] = { .name = "CRTC_ID", },
187};
188
189/**
Pekka Paalanencd011a62016-11-15 22:07:49 +0000190 * List of properties attached to DRM CRTCs
191 */
192enum wdrm_crtc_property {
193 WDRM_CRTC_MODE_ID = 0,
194 WDRM_CRTC_ACTIVE,
195 WDRM_CRTC__COUNT
196};
197
Daniel Stone598ee9d2016-11-16 11:55:20 +0000198static const struct drm_property_info crtc_props[] = {
199 [WDRM_CRTC_MODE_ID] = { .name = "MODE_ID", },
200 [WDRM_CRTC_ACTIVE] = { .name = "ACTIVE", },
201};
202
Pekka Paalanencd011a62016-11-15 22:07:49 +0000203/**
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000204 * Mode for drm_output_state_duplicate.
205 */
206enum drm_output_state_duplicate_mode {
207 DRM_OUTPUT_STATE_CLEAR_PLANES, /**< reset all planes to off */
208 DRM_OUTPUT_STATE_PRESERVE_PLANES, /**< preserve plane state */
209};
210
211/**
212 * Mode for drm_pending_state_apply and co.
213 */
214enum drm_state_apply_mode {
215 DRM_STATE_APPLY_SYNC, /**< state fully processed */
216 DRM_STATE_APPLY_ASYNC, /**< state pending event delivery */
217};
218
Giulio Camuffo954f1832014-10-11 18:27:30 +0300219struct drm_backend {
220 struct weston_backend base;
221 struct weston_compositor *compositor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400222
223 struct udev *udev;
224 struct wl_event_source *drm_source;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400225
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100226 struct udev_monitor *udev_monitor;
227 struct wl_event_source *udev_drm_source;
228
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100229 struct {
David Herrmannd7488c22012-03-11 20:05:21 +0100230 int id;
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100231 int fd;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300232 char *filename;
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100233 } drm;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200234 struct gbm_device *gbm;
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700235 struct wl_listener session_listener;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100236 uint32_t gbm_format;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200237
Rob Clark4339add2012-08-09 14:18:28 -0500238 /* we need these parameters in order to not fail drmModeAddFB2()
239 * due to out of bounds dimensions, and then mistakenly set
240 * sprites_are_broken:
241 */
Daniel Stonef214fdc2016-11-14 17:43:57 +0000242 int min_width, max_width;
243 int min_height, max_height;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200244 int no_addfb2;
Rob Clark4339add2012-08-09 14:18:28 -0500245
Daniel Stone085d2b92015-05-21 00:00:57 +0100246 struct wl_list plane_list;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500247 int sprites_are_broken;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +0200248 int sprites_hidden;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500249
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000250 void *repaint_data;
251
Daniel Stone6020f472018-02-05 15:46:20 +0000252 bool state_invalid;
253
Daniel Stone087ddf02017-02-14 17:51:30 +0000254 /* Connector and CRTC IDs not used by any enabled output. */
255 struct wl_array unused_connectors;
256 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
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400404struct drm_output {
Armin Krezović08368132016-09-30 14:11:05 +0200405 struct weston_output base;
406 drmModeConnector *connector;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400407
Daniel Stone17339232015-11-28 12:09:47 +0000408 uint32_t crtc_id; /* object ID to pass to DRM functions */
409 int pipe; /* index of CRTC in resource array / bitmasks */
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400410 uint32_t connector_id;
Richard Hughes2b2092a2013-04-24 14:58:02 +0100411 struct drm_edid edid;
Daniel Stone02cf4662017-03-03 16:19:39 +0000412
413 /* Holds the properties for the connector */
414 struct drm_property_info props_conn[WDRM_CONNECTOR__COUNT];
Pekka Paalanencd011a62016-11-15 22:07:49 +0000415 /* Holds the properties for the CRTC */
416 struct drm_property_info props_crtc[WDRM_CRTC__COUNT];
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200417
Daniel Stone5bb8f582017-04-04 17:54:28 +0100418 struct backlight *backlight;
Daniel Stone36609c72015-06-18 07:49:02 +0100419
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300420 int vblank_pending;
421 int page_flip_pending;
Daniel Stone598ee9d2016-11-16 11:55:20 +0000422 int atomic_complete_pending;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800423 int destroy_pending;
Armin Krezović08368132016-09-30 14:11:05 +0200424 int disable_pending;
Daniel Stonea08512f2016-11-08 17:46:10 +0000425 int dpms_off_pending;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300426
Daniel Stonee4256832017-04-04 17:54:27 +0100427 struct drm_fb *gbm_cursor_fb[2];
Daniel Stone2ba17f42015-05-19 20:02:41 +0100428 struct drm_plane *cursor_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500429 struct weston_view *cursor_view;
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400430 int current_cursor;
Daniel Stone5bb8f582017-04-04 17:54:28 +0100431
432 struct gbm_surface *gbm_surface;
433 uint32_t gbm_format;
434
Daniel Stonee2e80132018-01-16 15:37:33 +0000435 /* Plane being displayed directly on the CRTC */
436 struct drm_plane *scanout_plane;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200437
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000438 /* The last state submitted to the kernel for this CRTC. */
439 struct drm_output_state *state_cur;
440 /* The previously-submitted state, where the hardware has not
441 * yet acknowledged completion of state_cur. */
442 struct drm_output_state *state_last;
443
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200444 struct drm_fb *dumb[2];
445 pixman_image_t *image[2];
446 int current_image;
447 pixman_region32_t previous_damage;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300448
449 struct vaapi_recorder *recorder;
450 struct wl_listener recorder_frame_listener;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000451
452 struct wl_event_source *pageflip_timer;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400453};
454
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300455static struct gl_renderer_interface *gl_renderer;
456
Kristian Høgsberg98cfea62013-02-18 16:15:53 -0500457static const char default_seat[] = "seat0";
Pekka Paalanen33156972012-08-03 13:30:30 -0400458
Daniel Stone087ddf02017-02-14 17:51:30 +0000459static void
460wl_array_remove_uint32(struct wl_array *array, uint32_t elm)
461{
462 uint32_t *pos, *end;
463
464 end = (uint32_t *) ((char *) array->data + array->size);
465
466 wl_array_for_each(pos, array) {
467 if (*pos != elm)
468 continue;
469
470 array->size -= sizeof(*pos);
471 if (pos + 1 == end)
472 break;
473
474 memmove(pos, pos + 1, (char *) end - (char *) (pos + 1));
475 break;
476 }
477}
478
Armin Krezović545dba62016-08-05 15:54:18 +0200479static inline struct drm_output *
480to_drm_output(struct weston_output *base)
481{
482 return container_of(base, struct drm_output, base);
483}
484
485static inline struct drm_backend *
486to_drm_backend(struct weston_compositor *base)
487{
488 return container_of(base->backend, struct drm_backend, base);
489}
490
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000491static int
492pageflip_timeout(void *data) {
493 /*
494 * Our timer just went off, that means we're not receiving drm
495 * page flip events anymore for that output. Let's gracefully exit
496 * weston with a return value so devs can debug what's going on.
497 */
498 struct drm_output *output = data;
499 struct weston_compositor *compositor = output->base.compositor;
500
501 weston_log("Pageflip timeout reached on output %s, your "
502 "driver is probably buggy! Exiting.\n",
503 output->base.name);
504 weston_compositor_exit_with_code(compositor, EXIT_FAILURE);
505
506 return 0;
507}
508
509/* Creates the pageflip timer. Note that it isn't armed by default */
510static int
511drm_output_pageflip_timer_create(struct drm_output *output)
512{
513 struct wl_event_loop *loop = NULL;
514 struct weston_compositor *ec = output->base.compositor;
515
516 loop = wl_display_get_event_loop(ec->wl_display);
517 assert(loop);
518 output->pageflip_timer = wl_event_loop_add_timer(loop,
519 pageflip_timeout,
520 output);
521
522 if (output->pageflip_timer == NULL) {
523 weston_log("creating drm pageflip timer failed: %m\n");
524 return -1;
525 }
526
527 return 0;
528}
529
Daniel Stonecb04cc42016-11-16 11:51:27 +0000530static inline struct drm_mode *
531to_drm_mode(struct weston_mode *base)
532{
533 return container_of(base, struct drm_mode, base);
534}
535
Daniel Stone02cf4662017-03-03 16:19:39 +0000536/**
537 * Get the current value of a KMS property
538 *
539 * Given a drmModeObjectGetProperties return, as well as the drm_property_info
540 * for the target property, return the current value of that property,
541 * with an optional default. If the property is a KMS enum type, the return
542 * value will be translated into the appropriate internal enum.
543 *
544 * If the property is not present, the default value will be returned.
545 *
546 * @param info Internal structure for property to look up
547 * @param props Raw KMS properties for the target object
548 * @param def Value to return if property is not found
549 */
550static uint64_t
551drm_property_get_value(struct drm_property_info *info,
552 drmModeObjectPropertiesPtr props,
553 uint64_t def)
554{
555 unsigned int i;
556
557 if (info->prop_id == 0)
558 return def;
559
560 for (i = 0; i < props->count_props; i++) {
561 unsigned int j;
562
563 if (props->props[i] != info->prop_id)
564 continue;
565
566 /* Simple (non-enum) types can return the value directly */
567 if (info->num_enum_values == 0)
568 return props->prop_values[i];
569
570 /* Map from raw value to enum value */
571 for (j = 0; j < info->num_enum_values; j++) {
572 if (!info->enum_values[j].valid)
573 continue;
574 if (info->enum_values[j].value != props->prop_values[i])
575 continue;
576
577 return j;
578 }
579
580 /* We don't have a mapping for this enum; return default. */
581 break;
582 }
583
584 return def;
585}
586
587/**
588 * Cache DRM property values
589 *
590 * Update a per-object array of drm_property_info structures, given the
591 * DRM properties of the object.
592 *
593 * Call this every time an object newly appears (note that only connectors
594 * can be hotplugged), the first time it is seen, or when its status changes
595 * in a way which invalidates the potential property values (currently, the
596 * only case for this is connector hotplug).
597 *
598 * This updates the property IDs and enum values within the drm_property_info
599 * array.
600 *
601 * DRM property enum values are dynamic at runtime; the user must query the
602 * property to find out the desired runtime value for a requested string
603 * name. Using the 'type' field on planes as an example, there is no single
604 * hardcoded constant for primary plane types; instead, the property must be
605 * queried at runtime to find the value associated with the string "Primary".
606 *
607 * This helper queries and caches the enum values, to allow us to use a set
608 * of compile-time-constant enums portably across various implementations.
609 * The values given in enum_names are searched for, and stored in the
610 * same-indexed field of the map array.
611 *
612 * @param b DRM backend object
613 * @param src DRM property info array to source from
614 * @param info DRM property info array to copy into
615 * @param num_infos Number of entries in the source array
616 * @param props DRM object properties for the object
617 */
618static void
619drm_property_info_populate(struct drm_backend *b,
620 const struct drm_property_info *src,
621 struct drm_property_info *info,
622 unsigned int num_infos,
623 drmModeObjectProperties *props)
624{
625 drmModePropertyRes *prop;
626 unsigned i, j;
627
628 for (i = 0; i < num_infos; i++) {
629 unsigned int j;
630
631 info[i].name = src[i].name;
632 info[i].prop_id = 0;
633 info[i].num_enum_values = src[i].num_enum_values;
634
635 if (src[i].num_enum_values == 0)
636 continue;
637
638 info[i].enum_values =
639 malloc(src[i].num_enum_values *
640 sizeof(*info[i].enum_values));
641 assert(info[i].enum_values);
642 for (j = 0; j < info[i].num_enum_values; j++) {
643 info[i].enum_values[j].name = src[i].enum_values[j].name;
644 info[i].enum_values[j].valid = false;
645 }
646 }
647
648 for (i = 0; i < props->count_props; i++) {
649 unsigned int k;
650
651 prop = drmModeGetProperty(b->drm.fd, props->props[i]);
652 if (!prop)
653 continue;
654
655 for (j = 0; j < num_infos; j++) {
656 if (!strcmp(prop->name, info[j].name))
657 break;
658 }
659
660 /* We don't know/care about this property. */
661 if (j == num_infos) {
662#ifdef DEBUG
663 weston_log("DRM debug: unrecognized property %u '%s'\n",
664 prop->prop_id, prop->name);
665#endif
666 drmModeFreeProperty(prop);
667 continue;
668 }
669
670 if (info[j].num_enum_values == 0 &&
671 (prop->flags & DRM_MODE_PROP_ENUM)) {
672 weston_log("DRM: expected property %s to not be an"
673 " enum, but it is; ignoring\n", prop->name);
674 drmModeFreeProperty(prop);
675 continue;
676 }
677
678 info[j].prop_id = props->props[i];
679
680 if (info[j].num_enum_values == 0) {
681 drmModeFreeProperty(prop);
682 continue;
683 }
684
685 if (!(prop->flags & DRM_MODE_PROP_ENUM)) {
686 weston_log("DRM: expected property %s to be an enum,"
687 " but it is not; ignoring\n", prop->name);
688 drmModeFreeProperty(prop);
689 info[j].prop_id = 0;
690 continue;
691 }
692
693 for (k = 0; k < info[j].num_enum_values; k++) {
694 int l;
695
696 for (l = 0; l < prop->count_enums; l++) {
697 if (!strcmp(prop->enums[l].name,
698 info[j].enum_values[k].name))
699 break;
700 }
701
702 if (l == prop->count_enums)
703 continue;
704
705 info[j].enum_values[k].valid = true;
706 info[j].enum_values[k].value = prop->enums[l].value;
707 }
708
709 drmModeFreeProperty(prop);
710 }
711
712#ifdef DEBUG
713 for (i = 0; i < num_infos; i++) {
714 if (info[i].prop_id == 0)
715 weston_log("DRM warning: property '%s' missing\n",
716 info[i].name);
717 }
718#endif
719}
720
721/**
722 * Free DRM property information
723 *
724 * Frees all memory associated with a DRM property info array.
725 *
726 * @param info DRM property info array
727 * @param num_props Number of entries in array to free
728 */
729static void
730drm_property_info_free(struct drm_property_info *info, int num_props)
731{
732 int i;
733
734 for (i = 0; i < num_props; i++)
735 free(info[i].enum_values);
736}
737
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400738static void
Daniel Stone2ba17f42015-05-19 20:02:41 +0100739drm_output_set_cursor(struct drm_output_state *output_state);
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400740
Mario Kleinerf507ec32015-06-21 21:25:14 +0200741static void
742drm_output_update_msc(struct drm_output *output, unsigned int seq);
743
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000744static void
745drm_output_destroy(struct weston_output *output_base);
746
Daniel Stone5ff289a2017-10-07 12:59:02 +0100747/**
748 * Returns true if the plane can be used on the given output for its current
749 * repaint cycle.
750 */
751static bool
752drm_plane_is_available(struct drm_plane *plane, struct drm_output *output)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500753{
Daniel Stone5ff289a2017-10-07 12:59:02 +0100754 assert(plane->state_cur);
755
756 /* The plane still has a request not yet completed by the kernel. */
757 if (!plane->state_cur->complete)
758 return false;
759
760 /* The plane is still active on another output. */
761 if (plane->state_cur->output && plane->state_cur->output != output)
762 return false;
763
764 /* Check whether the plane can be used with this CRTC; possible_crtcs
765 * is a bitmask of CRTC indices (pipe), rather than CRTC object ID. */
Daniel Stone08d4edf2017-04-04 17:54:34 +0100766 return !!(plane->possible_crtcs & (1 << output->pipe));
Jesse Barnes58ef3792012-02-23 09:45:49 -0500767}
768
Daniel Stone72c0e1b2017-02-09 13:49:15 +0000769static struct drm_output *
770drm_output_find_by_crtc(struct drm_backend *b, uint32_t crtc_id)
771{
772 struct drm_output *output;
773
774 wl_list_for_each(output, &b->compositor->output_list, base.link) {
775 if (output->crtc_id == crtc_id)
776 return output;
777 }
778
779 wl_list_for_each(output, &b->compositor->pending_output_list,
780 base.link) {
781 if (output->crtc_id == crtc_id)
782 return output;
783 }
784
785 return NULL;
786}
787
Daniel Stonec0ec7592017-02-09 13:58:35 +0000788static struct drm_output *
789drm_output_find_by_connector(struct drm_backend *b, uint32_t connector_id)
790{
791 struct drm_output *output;
792
793 wl_list_for_each(output, &b->compositor->output_list, base.link) {
794 if (output->connector_id == connector_id)
795 return output;
796 }
797
798 wl_list_for_each(output, &b->compositor->pending_output_list,
799 base.link) {
800 if (output->connector_id == connector_id)
801 return output;
802 }
803
804 return NULL;
805}
806
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300807static void
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100808drm_fb_destroy(struct drm_fb *fb)
809{
810 if (fb->fb_id != 0)
811 drmModeRmFB(fb->fd, fb->fb_id);
812 weston_buffer_reference(&fb->buffer_ref, NULL);
813 free(fb);
814}
815
816static void
817drm_fb_destroy_dumb(struct drm_fb *fb)
818{
819 struct drm_mode_destroy_dumb destroy_arg;
820
821 assert(fb->type == BUFFER_PIXMAN_DUMB);
822
823 if (fb->map && fb->size > 0)
824 munmap(fb->map, fb->size);
825
826 memset(&destroy_arg, 0, sizeof(destroy_arg));
827 destroy_arg.handle = fb->handle;
828 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
829
830 drm_fb_destroy(fb);
831}
832
833static void
834drm_fb_destroy_gbm(struct gbm_bo *bo, void *data)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300835{
836 struct drm_fb *fb = data;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300837
Daniel Stonee4256832017-04-04 17:54:27 +0100838 assert(fb->type == BUFFER_GBM_SURFACE || fb->type == BUFFER_CLIENT ||
839 fb->type == BUFFER_CURSOR);
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100840 drm_fb_destroy(fb);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300841}
842
843static struct drm_fb *
Daniel Stonef214fdc2016-11-14 17:43:57 +0000844drm_fb_create_dumb(struct drm_backend *b, int width, int height,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300845 uint32_t format)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200846{
847 struct drm_fb *fb;
848 int ret;
849
850 struct drm_mode_create_dumb create_arg;
851 struct drm_mode_destroy_dumb destroy_arg;
852 struct drm_mode_map_dumb map_arg;
853
Peter Huttererf3d62272013-08-08 11:57:05 +1000854 fb = zalloc(sizeof *fb);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200855 if (!fb)
856 return NULL;
857
Daniel Stone6e7a9612017-04-04 17:54:26 +0100858 fb->refcnt = 1;
859
Daniel Stone0b70fa42017-04-04 17:54:23 +0100860 fb->format = pixel_format_get_info(format);
861 if (!fb->format) {
862 weston_log("failed to look up format 0x%lx\n",
863 (unsigned long) format);
864 goto err_fb;
865 }
866
867 if (!fb->format->depth || !fb->format->bpp) {
868 weston_log("format 0x%lx is not compatible with dumb buffers\n",
869 (unsigned long) format);
870 goto err_fb;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300871 }
872
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700873 memset(&create_arg, 0, sizeof create_arg);
Daniel Stone0b70fa42017-04-04 17:54:23 +0100874 create_arg.bpp = fb->format->bpp;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200875 create_arg.width = width;
876 create_arg.height = height;
877
Giulio Camuffo954f1832014-10-11 18:27:30 +0300878 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200879 if (ret)
880 goto err_fb;
881
Daniel Stonefc175a72017-04-04 17:54:22 +0100882 fb->type = BUFFER_PIXMAN_DUMB;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200883 fb->handle = create_arg.handle;
884 fb->stride = create_arg.pitch;
885 fb->size = create_arg.size;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000886 fb->width = width;
887 fb->height = height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300888 fb->fd = b->drm.fd;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200889
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300890 ret = -1;
891
892 if (!b->no_addfb2) {
Yong Bakos4b6321f2016-08-17 17:37:55 -0700893 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300894
895 handles[0] = fb->handle;
896 pitches[0] = fb->stride;
897 offsets[0] = 0;
898
899 ret = drmModeAddFB2(b->drm.fd, width, height,
Daniel Stone0b70fa42017-04-04 17:54:23 +0100900 fb->format->format,
901 handles, pitches, offsets,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300902 &fb->fb_id, 0);
903 if (ret) {
904 weston_log("addfb2 failed: %m\n");
905 b->no_addfb2 = 1;
906 }
907 }
908
909 if (ret) {
Daniel Stone0b70fa42017-04-04 17:54:23 +0100910 ret = drmModeAddFB(b->drm.fd, width, height,
911 fb->format->depth, fb->format->bpp,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300912 fb->stride, fb->handle, &fb->fb_id);
913 }
914
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200915 if (ret)
916 goto err_bo;
917
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700918 memset(&map_arg, 0, sizeof map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200919 map_arg.handle = fb->handle;
Chris Michaeleb2074a2013-05-01 21:26:02 -0400920 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200921 if (ret)
922 goto err_add_fb;
923
Chris Michael4a7ce1f2015-11-10 10:40:37 -0500924 fb->map = mmap(NULL, fb->size, PROT_WRITE,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300925 MAP_SHARED, b->drm.fd, map_arg.offset);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200926 if (fb->map == MAP_FAILED)
927 goto err_add_fb;
928
929 return fb;
930
931err_add_fb:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300932 drmModeRmFB(b->drm.fd, fb->fb_id);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200933err_bo:
934 memset(&destroy_arg, 0, sizeof(destroy_arg));
935 destroy_arg.handle = create_arg.handle;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300936 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200937err_fb:
938 free(fb);
939 return NULL;
940}
941
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200942static struct drm_fb *
Daniel Stone6e7a9612017-04-04 17:54:26 +0100943drm_fb_ref(struct drm_fb *fb)
944{
945 fb->refcnt++;
946 return fb;
947}
948
949static struct drm_fb *
Daniel Stonefc175a72017-04-04 17:54:22 +0100950drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend,
951 uint32_t format, enum drm_fb_type type)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300952{
953 struct drm_fb *fb = gbm_bo_get_user_data(bo);
Derek Foreman482ffdf2016-07-08 12:50:57 -0500954 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300955 int ret;
956
Daniel Stonefc175a72017-04-04 17:54:22 +0100957 if (fb) {
958 assert(fb->type == type);
Daniel Stone6e7a9612017-04-04 17:54:26 +0100959 return drm_fb_ref(fb);
Daniel Stonefc175a72017-04-04 17:54:22 +0100960 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300961
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
992 ret = -1;
993
Giulio Camuffo954f1832014-10-11 18:27:30 +0300994 if (format && !backend->no_addfb2) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200995 handles[0] = fb->handle;
996 pitches[0] = fb->stride;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200997 offsets[0] = 0;
998
Daniel Stonec8c917c2016-11-14 17:45:58 +0000999 ret = drmModeAddFB2(backend->drm.fd, fb->width, fb->height,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001000 format, handles, pitches, offsets,
1001 &fb->fb_id, 0);
1002 if (ret) {
1003 weston_log("addfb2 failed: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001004 backend->no_addfb2 = 1;
1005 backend->sprites_are_broken = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001006 }
1007 }
1008
Daniel Stone0b70fa42017-04-04 17:54:23 +01001009 if (ret && fb->format->depth && fb->format->bpp)
Daniel Stonec8c917c2016-11-14 17:45:58 +00001010 ret = drmModeAddFB(backend->drm.fd, fb->width, fb->height,
Daniel Stone0b70fa42017-04-04 17:54:23 +01001011 fb->format->depth, fb->format->bpp,
1012 fb->stride, fb->handle, &fb->fb_id);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001013
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001014 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +02001015 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001016 goto err_free;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001017 }
1018
Tomohito Esaki576f42e2017-04-04 17:54:24 +01001019 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_gbm);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001020
1021 return fb;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001022
1023err_free:
1024 free(fb);
1025 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001026}
1027
1028static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001029drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001030{
Pekka Paalanende685b82012-12-04 15:58:12 +02001031 assert(fb->buffer_ref.buffer == NULL);
Daniel Stonefc175a72017-04-04 17:54:22 +01001032 assert(fb->type == BUFFER_CLIENT);
Pekka Paalanende685b82012-12-04 15:58:12 +02001033 weston_buffer_reference(&fb->buffer_ref, buffer);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001034}
1035
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02001036static void
Daniel Stone05a5ac22017-04-04 17:54:25 +01001037drm_fb_unref(struct drm_fb *fb)
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02001038{
1039 if (!fb)
1040 return;
1041
Daniel Stone6e7a9612017-04-04 17:54:26 +01001042 assert(fb->refcnt > 0);
1043 if (--fb->refcnt > 0)
1044 return;
1045
Daniel Stonefc175a72017-04-04 17:54:22 +01001046 switch (fb->type) {
1047 case BUFFER_PIXMAN_DUMB:
Daniel Stone6e7a9612017-04-04 17:54:26 +01001048 drm_fb_destroy_dumb(fb);
Daniel Stonefc175a72017-04-04 17:54:22 +01001049 break;
Daniel Stonee4256832017-04-04 17:54:27 +01001050 case BUFFER_CURSOR:
Daniel Stonefc175a72017-04-04 17:54:22 +01001051 case BUFFER_CLIENT:
1052 gbm_bo_destroy(fb->bo);
1053 break;
1054 case BUFFER_GBM_SURFACE:
Daniel Stone05a5ac22017-04-04 17:54:25 +01001055 gbm_surface_release_buffer(fb->gbm_surface, fb->bo);
Daniel Stonefc175a72017-04-04 17:54:22 +01001056 break;
1057 default:
1058 assert(NULL);
1059 break;
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02001060 }
1061}
1062
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001063/**
Daniel Stonebc15f682016-11-14 16:57:01 +00001064 * Allocate a new, empty, plane state.
1065 */
1066static struct drm_plane_state *
1067drm_plane_state_alloc(struct drm_output_state *state_output,
1068 struct drm_plane *plane)
1069{
1070 struct drm_plane_state *state = zalloc(sizeof(*state));
1071
1072 assert(state);
1073 state->output_state = state_output;
1074 state->plane = plane;
1075
1076 /* Here we only add the plane state to the desired link, and not
1077 * set the member. Having an output pointer set means that the
1078 * plane will be displayed on the output; this won't be the case
1079 * when we go to disable a plane. In this case, it must be part of
1080 * the commit (and thus the output state), but the member must be
1081 * NULL, as it will not be on any output when the state takes
1082 * effect.
1083 */
1084 if (state_output)
1085 wl_list_insert(&state_output->plane_list, &state->link);
1086 else
1087 wl_list_init(&state->link);
1088
1089 return state;
1090}
1091
1092/**
1093 * Free an existing plane state. As a special case, the state will not
1094 * normally be freed if it is the current state; see drm_plane_set_state.
1095 */
1096static void
1097drm_plane_state_free(struct drm_plane_state *state, bool force)
1098{
1099 if (!state)
1100 return;
1101
1102 wl_list_remove(&state->link);
1103 wl_list_init(&state->link);
1104 state->output_state = NULL;
1105
1106 if (force || state != state->plane->state_cur) {
1107 drm_fb_unref(state->fb);
1108 free(state);
1109 }
1110}
1111
1112/**
1113 * Duplicate an existing plane state into a new plane state, storing it within
1114 * the given output state. If the output state already contains a plane state
1115 * for the drm_plane referenced by 'src', that plane state is freed first.
1116 */
1117static struct drm_plane_state *
1118drm_plane_state_duplicate(struct drm_output_state *state_output,
1119 struct drm_plane_state *src)
1120{
1121 struct drm_plane_state *dst = malloc(sizeof(*dst));
1122 struct drm_plane_state *old, *tmp;
1123
1124 assert(src);
1125 assert(dst);
1126 *dst = *src;
1127 wl_list_init(&dst->link);
1128
1129 wl_list_for_each_safe(old, tmp, &state_output->plane_list, link) {
1130 /* Duplicating a plane state into the same output state, so
1131 * it can replace itself with an identical copy of itself,
1132 * makes no sense. */
1133 assert(old != src);
1134 if (old->plane == dst->plane)
1135 drm_plane_state_free(old, false);
1136 }
1137
1138 wl_list_insert(&state_output->plane_list, &dst->link);
1139 if (src->fb)
1140 dst->fb = drm_fb_ref(src->fb);
1141 dst->output_state = state_output;
1142 dst->complete = false;
1143
1144 return dst;
1145}
1146
1147/**
1148 * Remove a plane state from an output state; if the plane was previously
1149 * enabled, then replace it with a disabling state. This ensures that the
1150 * output state was untouched from it was before the plane state was
1151 * modified by the caller of this function.
1152 *
1153 * This is required as drm_output_state_get_plane may either allocate a
1154 * new plane state, in which case this function will just perform a matching
1155 * drm_plane_state_free, or it may instead repurpose an existing disabling
1156 * state (if the plane was previously active), in which case this function
1157 * will reset it.
1158 */
1159static void
1160drm_plane_state_put_back(struct drm_plane_state *state)
1161{
1162 struct drm_output_state *state_output;
1163 struct drm_plane *plane;
1164
1165 if (!state)
1166 return;
1167
1168 state_output = state->output_state;
1169 plane = state->plane;
1170 drm_plane_state_free(state, false);
1171
1172 /* Plane was previously disabled; no need to keep this temporary
1173 * state around. */
1174 if (!plane->state_cur->fb)
1175 return;
1176
1177 (void) drm_plane_state_alloc(state_output, plane);
1178}
1179
1180/**
Daniel Stone2ba17f42015-05-19 20:02:41 +01001181 * Return a plane state from a drm_output_state.
1182 */
1183static struct drm_plane_state *
1184drm_output_state_get_existing_plane(struct drm_output_state *state_output,
1185 struct drm_plane *plane)
1186{
1187 struct drm_plane_state *ps;
1188
1189 wl_list_for_each(ps, &state_output->plane_list, link) {
1190 if (ps->plane == plane)
1191 return ps;
1192 }
1193
1194 return NULL;
1195}
1196
1197/**
Daniel Stonebc15f682016-11-14 16:57:01 +00001198 * Return a plane state from a drm_output_state, either existing or
1199 * freshly allocated.
1200 */
1201static struct drm_plane_state *
1202drm_output_state_get_plane(struct drm_output_state *state_output,
1203 struct drm_plane *plane)
1204{
1205 struct drm_plane_state *ps;
1206
Daniel Stone2ba17f42015-05-19 20:02:41 +01001207 ps = drm_output_state_get_existing_plane(state_output, plane);
1208 if (ps)
1209 return ps;
Daniel Stonebc15f682016-11-14 16:57:01 +00001210
1211 return drm_plane_state_alloc(state_output, plane);
1212}
1213
1214/**
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001215 * Allocate a new, empty drm_output_state. This should not generally be used
1216 * in the repaint cycle; see drm_output_state_duplicate.
1217 */
1218static struct drm_output_state *
1219drm_output_state_alloc(struct drm_output *output,
1220 struct drm_pending_state *pending_state)
Daniel Stone90648872016-10-21 18:08:37 +01001221{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001222 struct drm_output_state *state = zalloc(sizeof(*state));
1223
1224 assert(state);
1225 state->output = output;
Daniel Stonea08512f2016-11-08 17:46:10 +00001226 state->dpms = WESTON_DPMS_OFF;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001227 state->pending_state = pending_state;
1228 if (pending_state)
1229 wl_list_insert(&pending_state->output_list, &state->link);
1230 else
1231 wl_list_init(&state->link);
1232
Daniel Stonebc15f682016-11-14 16:57:01 +00001233 wl_list_init(&state->plane_list);
1234
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001235 return state;
1236}
1237
1238/**
1239 * Duplicate an existing drm_output_state into a new one. This is generally
1240 * used during the repaint cycle, to capture the existing state of an output
1241 * and modify it to create a new state to be used.
1242 *
1243 * The mode determines whether the output will be reset to an a blank state,
1244 * or an exact mirror of the current state.
1245 */
1246static struct drm_output_state *
1247drm_output_state_duplicate(struct drm_output_state *src,
1248 struct drm_pending_state *pending_state,
1249 enum drm_output_state_duplicate_mode plane_mode)
1250{
1251 struct drm_output_state *dst = malloc(sizeof(*dst));
Daniel Stonebc15f682016-11-14 16:57:01 +00001252 struct drm_plane_state *ps;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001253
1254 assert(dst);
1255
1256 /* Copy the whole structure, then individually modify the
1257 * pending_state, as well as the list link into our pending
1258 * state. */
1259 *dst = *src;
1260
1261 dst->pending_state = pending_state;
1262 if (pending_state)
1263 wl_list_insert(&pending_state->output_list, &dst->link);
1264 else
1265 wl_list_init(&dst->link);
1266
Daniel Stonebc15f682016-11-14 16:57:01 +00001267 wl_list_init(&dst->plane_list);
1268
1269 wl_list_for_each(ps, &src->plane_list, link) {
1270 /* Don't carry planes which are now disabled; these should be
1271 * free for other outputs to reuse. */
1272 if (!ps->output)
1273 continue;
1274
1275 if (plane_mode == DRM_OUTPUT_STATE_CLEAR_PLANES)
1276 (void) drm_plane_state_alloc(dst, ps->plane);
1277 else
1278 (void) drm_plane_state_duplicate(dst, ps);
1279 }
1280
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001281 return dst;
1282}
1283
1284/**
1285 * Free an unused drm_output_state.
1286 */
1287static void
1288drm_output_state_free(struct drm_output_state *state)
1289{
Daniel Stonebc15f682016-11-14 16:57:01 +00001290 struct drm_plane_state *ps, *next;
1291
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001292 if (!state)
1293 return;
1294
Daniel Stonebc15f682016-11-14 16:57:01 +00001295 wl_list_for_each_safe(ps, next, &state->plane_list, link)
1296 drm_plane_state_free(ps, false);
1297
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001298 wl_list_remove(&state->link);
Daniel Stonebc15f682016-11-14 16:57:01 +00001299
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001300 free(state);
Daniel Stone90648872016-10-21 18:08:37 +01001301}
1302
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001303/**
Daniel Stonea08512f2016-11-08 17:46:10 +00001304 * Get output state to disable output
1305 *
1306 * Returns a pointer to an output_state object which can be used to disable
1307 * an output (e.g. DPMS off).
1308 *
1309 * @param pending_state The pending state object owning this update
1310 * @param output The output to disable
1311 * @returns A drm_output_state to disable the output
1312 */
1313static struct drm_output_state *
1314drm_output_get_disable_state(struct drm_pending_state *pending_state,
1315 struct drm_output *output)
1316{
1317 struct drm_output_state *output_state;
1318
1319 output_state = drm_output_state_duplicate(output->state_cur,
1320 pending_state,
1321 DRM_OUTPUT_STATE_CLEAR_PLANES);
1322 output_state->dpms = WESTON_DPMS_OFF;
1323
1324 return output_state;
1325}
1326
1327/**
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001328 * Allocate a new drm_pending_state
1329 *
1330 * Allocate a new, empty, 'pending state' structure to be used across a
1331 * repaint cycle or similar.
1332 *
1333 * @param backend DRM backend
1334 * @returns Newly-allocated pending state structure
1335 */
1336static struct drm_pending_state *
1337drm_pending_state_alloc(struct drm_backend *backend)
1338{
1339 struct drm_pending_state *ret;
1340
1341 ret = calloc(1, sizeof(*ret));
1342 if (!ret)
1343 return NULL;
1344
1345 ret->backend = backend;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001346 wl_list_init(&ret->output_list);
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001347
1348 return ret;
1349}
1350
1351/**
1352 * Free a drm_pending_state structure
1353 *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001354 * Frees a pending_state structure, as well as any output_states connected
1355 * to this pending state.
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001356 *
1357 * @param pending_state Pending state structure to free
1358 */
1359static void
1360drm_pending_state_free(struct drm_pending_state *pending_state)
1361{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001362 struct drm_output_state *output_state, *tmp;
1363
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001364 if (!pending_state)
1365 return;
1366
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001367 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
1368 link) {
1369 drm_output_state_free(output_state);
1370 }
1371
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001372 free(pending_state);
1373}
1374
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001375/**
1376 * Find an output state in a pending state
1377 *
1378 * Given a pending_state structure, find the output_state for a particular
1379 * output.
1380 *
1381 * @param pending_state Pending state structure to search
1382 * @param output Output to find state for
1383 * @returns Output state if present, or NULL if not
1384 */
1385static struct drm_output_state *
1386drm_pending_state_get_output(struct drm_pending_state *pending_state,
1387 struct drm_output *output)
1388{
1389 struct drm_output_state *output_state;
1390
1391 wl_list_for_each(output_state, &pending_state->output_list, link) {
1392 if (output_state->output == output)
1393 return output_state;
1394 }
1395
1396 return NULL;
1397}
1398
Daniel Stonea08512f2016-11-08 17:46:10 +00001399static int drm_pending_state_apply_sync(struct drm_pending_state *state);
1400
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001401/**
1402 * Mark a drm_output_state (the output's last state) as complete. This handles
1403 * any post-completion actions such as updating the repaint timer, disabling the
1404 * output, and finally freeing the state.
1405 */
1406static void
1407drm_output_update_complete(struct drm_output *output, uint32_t flags,
1408 unsigned int sec, unsigned int usec)
1409{
Daniel Stonea08512f2016-11-08 17:46:10 +00001410 struct drm_backend *b = to_drm_backend(output->base.compositor);
Daniel Stonebc15f682016-11-14 16:57:01 +00001411 struct drm_plane_state *ps;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001412 struct timespec ts;
1413
1414 /* Stop the pageflip timer instead of rearming it here */
1415 if (output->pageflip_timer)
1416 wl_event_source_timer_update(output->pageflip_timer, 0);
1417
Daniel Stonebc15f682016-11-14 16:57:01 +00001418 wl_list_for_each(ps, &output->state_cur->plane_list, link)
1419 ps->complete = true;
1420
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001421 drm_output_state_free(output->state_last);
1422 output->state_last = NULL;
1423
1424 if (output->destroy_pending) {
Daniel Stonea08512f2016-11-08 17:46:10 +00001425 output->destroy_pending = 0;
1426 output->disable_pending = 0;
1427 output->dpms_off_pending = 0;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001428 drm_output_destroy(&output->base);
1429 return;
1430 } else if (output->disable_pending) {
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001431 output->disable_pending = 0;
Daniel Stonea08512f2016-11-08 17:46:10 +00001432 output->dpms_off_pending = 0;
1433 weston_output_disable(&output->base);
1434 return;
1435 } else if (output->dpms_off_pending) {
1436 struct drm_pending_state *pending = drm_pending_state_alloc(b);
1437 output->dpms_off_pending = 0;
1438 drm_output_get_disable_state(pending, output);
1439 drm_pending_state_apply_sync(pending);
1440 return;
1441 } else if (output->state_cur->dpms == WESTON_DPMS_OFF &&
1442 output->base.repaint_status != REPAINT_AWAITING_COMPLETION) {
1443 /* DPMS can happen to us either in the middle of a repaint
1444 * cycle (when we have painted fresh content, only to throw it
1445 * away for DPMS off), or at any other random point. If the
1446 * latter is true, then we cannot go through finish_frame,
1447 * because the repaint machinery does not expect this. */
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001448 return;
1449 }
1450
1451 ts.tv_sec = sec;
1452 ts.tv_nsec = usec * 1000;
1453 weston_output_finish_frame(&output->base, &ts, flags);
1454
1455 /* We can't call this from frame_notify, because the output's
1456 * repaint needed flag is cleared just after that */
1457 if (output->recorder)
1458 weston_output_schedule_repaint(&output->base);
1459}
1460
1461/**
1462 * Mark an output state as current on the output, i.e. it has been
1463 * submitted to the kernel. The mode argument determines whether this
1464 * update will be applied synchronously (e.g. when calling drmModeSetCrtc),
1465 * or asynchronously (in which case we wait for events to complete).
1466 */
1467static void
1468drm_output_assign_state(struct drm_output_state *state,
1469 enum drm_state_apply_mode mode)
1470{
1471 struct drm_output *output = state->output;
Daniel Stone598ee9d2016-11-16 11:55:20 +00001472 struct drm_backend *b = to_drm_backend(output->base.compositor);
Daniel Stonebc15f682016-11-14 16:57:01 +00001473 struct drm_plane_state *plane_state;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001474
1475 assert(!output->state_last);
1476
1477 if (mode == DRM_STATE_APPLY_ASYNC)
1478 output->state_last = output->state_cur;
1479 else
1480 drm_output_state_free(output->state_cur);
1481
1482 wl_list_remove(&state->link);
1483 wl_list_init(&state->link);
1484 state->pending_state = NULL;
1485
1486 output->state_cur = state;
Daniel Stonebc15f682016-11-14 16:57:01 +00001487
Daniel Stone598ee9d2016-11-16 11:55:20 +00001488 if (b->atomic_modeset && mode == DRM_STATE_APPLY_ASYNC)
1489 output->atomic_complete_pending = 1;
1490
Daniel Stonebc15f682016-11-14 16:57:01 +00001491 /* Replace state_cur on each affected plane with the new state, being
1492 * careful to dispose of orphaned (but only orphaned) previous state.
1493 * If the previous state is not orphaned (still has an output_state
1494 * attached), it will be disposed of by freeing the output_state. */
1495 wl_list_for_each(plane_state, &state->plane_list, link) {
1496 struct drm_plane *plane = plane_state->plane;
1497
1498 if (plane->state_cur && !plane->state_cur->output_state)
1499 drm_plane_state_free(plane->state_cur, true);
1500 plane->state_cur = plane_state;
1501
1502 if (mode != DRM_STATE_APPLY_ASYNC) {
1503 plane_state->complete = true;
1504 continue;
1505 }
1506
Daniel Stone598ee9d2016-11-16 11:55:20 +00001507 if (b->atomic_modeset)
1508 continue;
1509
Daniel Stonebc15f682016-11-14 16:57:01 +00001510 if (plane->type == WDRM_PLANE_TYPE_OVERLAY)
1511 output->vblank_pending++;
Daniel Stonee2e80132018-01-16 15:37:33 +00001512 else if (plane->type == WDRM_PLANE_TYPE_PRIMARY)
1513 output->page_flip_pending = 1;
Daniel Stonebc15f682016-11-14 16:57:01 +00001514 }
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001515}
1516
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001517static int
1518drm_view_transform_supported(struct weston_view *ev)
1519{
1520 return !ev->transform.enabled ||
1521 (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
1522}
1523
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001524static uint32_t
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +02001525drm_output_check_scanout_format(struct drm_output *output,
1526 struct weston_surface *es, struct gbm_bo *bo)
1527{
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +02001528 uint32_t format;
1529 pixman_region32_t r;
1530
1531 format = gbm_bo_get_format(bo);
1532
Kristian Høgsberg3f97b342013-10-16 16:08:57 -07001533 if (format == GBM_FORMAT_ARGB8888) {
1534 /* We can scanout an ARGB buffer if the surface's
1535 * opaque region covers the whole output, but we have
1536 * to use XRGB as the KMS format code. */
Kristian Høgsberg1be87e32014-01-17 14:22:41 -08001537 pixman_region32_init_rect(&r, 0, 0,
1538 output->base.width,
1539 output->base.height);
1540 pixman_region32_subtract(&r, &r, &es->opaque);
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +02001541
1542 if (!pixman_region32_not_empty(&r))
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001543 format = GBM_FORMAT_XRGB8888;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +02001544
1545 pixman_region32_fini(&r);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001546 }
Kristian Høgsberg3f97b342013-10-16 16:08:57 -07001547
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001548 if (output->gbm_format == format)
Kristian Høgsberg3f97b342013-10-16 16:08:57 -07001549 return format;
1550
1551 return 0;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +02001552}
1553
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001554static struct weston_plane *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001555drm_output_prepare_scanout_view(struct drm_output_state *output_state,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001556 struct weston_view *ev)
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001557{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001558 struct drm_output *output = output_state->output;
Armin Krezović545dba62016-08-05 15:54:18 +02001559 struct drm_backend *b = to_drm_backend(output->base.compositor);
Daniel Stonee2e80132018-01-16 15:37:33 +00001560 struct drm_plane *scanout_plane = output->scanout_plane;
1561 struct drm_plane_state *state;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001562 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001563 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001564 struct gbm_bo *bo;
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001565 uint32_t format;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001566
Daniel Stone90648872016-10-21 18:08:37 +01001567 /* Don't import buffers which span multiple outputs. */
1568 if (ev->output_mask != (1u << output->base.id))
1569 return NULL;
1570
Daniel Stone296d7a92016-10-21 18:05:37 +01001571 /* We use GBM to import buffers. */
1572 if (b->gbm == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001573 return NULL;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001574
Daniel Stone296d7a92016-10-21 18:05:37 +01001575 if (buffer == NULL)
1576 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +01001577 if (wl_shm_buffer_get(buffer->resource))
1578 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001579
1580 /* Make sure our view is exactly compatible with the output. */
1581 if (ev->geometry.x != output->base.x ||
1582 ev->geometry.y != output->base.y)
1583 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +01001584 if (buffer->width != output->base.current_mode->width ||
1585 buffer->height != output->base.current_mode->height)
1586 return NULL;
1587
Daniel Stone296d7a92016-10-21 18:05:37 +01001588 if (ev->transform.enabled)
1589 return NULL;
Pekka Paalanen5580f222015-02-17 16:33:18 +02001590 if (ev->geometry.scissor_enabled)
1591 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001592 if (viewport->buffer.transform != output->base.transform)
1593 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +01001594 if (viewport->buffer.scale != output->base.current_scale)
1595 return NULL;
1596 if (!drm_view_transform_supported(ev))
1597 return NULL;
1598
1599 if (ev->alpha != 1.0f)
1600 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001601
Daniel Stonee2e80132018-01-16 15:37:33 +00001602 state = drm_output_state_get_plane(output_state, scanout_plane);
1603 if (state->fb) {
1604 /* If there is already a framebuffer on the scanout plane,
1605 * a client view has already been placed on the scanout
1606 * view. In that case, do not free or put back the state,
1607 * but just leave it in place and quietly exit. */
1608 return NULL;
1609 }
1610
Giulio Camuffo954f1832014-10-11 18:27:30 +03001611 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
Kristian Høgsberg63996462013-09-03 22:27:08 -07001612 buffer->resource, GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001613
Rob Bradford9b101872012-09-14 23:25:41 +01001614 /* Unable to use the buffer for scanout */
1615 if (!bo)
1616 return NULL;
1617
Jason Ekstranda7af7042013-10-12 22:38:11 -05001618 format = drm_output_check_scanout_format(output, ev->surface, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001619 if (format == 0) {
Daniel Stonee2e80132018-01-16 15:37:33 +00001620 drm_plane_state_put_back(state);
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +03001621 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001622 return NULL;
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +03001623 }
1624
Daniel Stonee2e80132018-01-16 15:37:33 +00001625 state->fb = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT);
1626 if (!state->fb) {
1627 drm_plane_state_put_back(state);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001628 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001629 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001630 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001631
Daniel Stonee2e80132018-01-16 15:37:33 +00001632 drm_fb_set_buffer(state->fb, buffer);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001633
Daniel Stonee2e80132018-01-16 15:37:33 +00001634 state->output = output;
1635
1636 state->src_x = 0;
1637 state->src_y = 0;
1638 state->src_w = state->fb->width << 16;
1639 state->src_h = state->fb->height << 16;
1640
1641 state->dest_x = 0;
1642 state->dest_y = 0;
1643 state->dest_w = output->base.current_mode->width;
1644 state->dest_h = output->base.current_mode->height;
1645
1646 return &scanout_plane->base;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001647}
1648
Daniel Stone95d48a22017-04-04 17:54:30 +01001649static struct drm_fb *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001650drm_output_render_gl(struct drm_output_state *state, pixman_region32_t *damage)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001651{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001652 struct drm_output *output = state->output;
Armin Krezović545dba62016-08-05 15:54:18 +02001653 struct drm_backend *b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001654 struct gbm_bo *bo;
Daniel Stone95d48a22017-04-04 17:54:30 +01001655 struct drm_fb *ret;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001656
Giulio Camuffo954f1832014-10-11 18:27:30 +03001657 output->base.compositor->renderer->repaint_output(&output->base,
1658 damage);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001659
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001660 bo = gbm_surface_lock_front_buffer(output->gbm_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001661 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +02001662 weston_log("failed to lock front buffer: %m\n");
Daniel Stone95d48a22017-04-04 17:54:30 +01001663 return NULL;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001664 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001665
Daniel Stone95d48a22017-04-04 17:54:30 +01001666 ret = drm_fb_get_from_bo(bo, b, output->gbm_format, BUFFER_GBM_SURFACE);
1667 if (!ret) {
Martin Minarik6d118362012-06-07 18:01:59 +02001668 weston_log("failed to get drm_fb for bo\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001669 gbm_surface_release_buffer(output->gbm_surface, bo);
Daniel Stone95d48a22017-04-04 17:54:30 +01001670 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001671 }
Daniel Stone95d48a22017-04-04 17:54:30 +01001672 ret->gbm_surface = output->gbm_surface;
1673
1674 return ret;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001675}
1676
Daniel Stone95d48a22017-04-04 17:54:30 +01001677static struct drm_fb *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001678drm_output_render_pixman(struct drm_output_state *state,
1679 pixman_region32_t *damage)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001680{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001681 struct drm_output *output = state->output;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001682 struct weston_compositor *ec = output->base.compositor;
1683 pixman_region32_t total_damage, previous_damage;
1684
1685 pixman_region32_init(&total_damage);
1686 pixman_region32_init(&previous_damage);
1687
1688 pixman_region32_copy(&previous_damage, damage);
1689
1690 pixman_region32_union(&total_damage, damage, &output->previous_damage);
1691 pixman_region32_copy(&output->previous_damage, &previous_damage);
1692
1693 output->current_image ^= 1;
1694
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001695 pixman_renderer_output_set_buffer(&output->base,
1696 output->image[output->current_image]);
1697
1698 ec->renderer->repaint_output(&output->base, &total_damage);
1699
1700 pixman_region32_fini(&total_damage);
1701 pixman_region32_fini(&previous_damage);
Daniel Stone95d48a22017-04-04 17:54:30 +01001702
1703 return drm_fb_ref(output->dumb[output->current_image]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001704}
1705
1706static void
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001707drm_output_render(struct drm_output_state *state, pixman_region32_t *damage)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001708{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001709 struct drm_output *output = state->output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001710 struct weston_compositor *c = output->base.compositor;
Daniel Stonee2e80132018-01-16 15:37:33 +00001711 struct drm_plane_state *scanout_state;
Daniel Stonee95169b2016-11-14 17:46:59 +00001712 struct drm_plane *scanout_plane = output->scanout_plane;
Armin Krezović545dba62016-08-05 15:54:18 +02001713 struct drm_backend *b = to_drm_backend(c);
Daniel Stone95d48a22017-04-04 17:54:30 +01001714 struct drm_fb *fb;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001715
Daniel Stone4e84f7d2017-04-04 17:54:29 +01001716 /* If we already have a client buffer promoted to scanout, then we don't
1717 * want to render. */
Daniel Stonee2e80132018-01-16 15:37:33 +00001718 scanout_state = drm_output_state_get_plane(state,
1719 output->scanout_plane);
1720 if (scanout_state->fb)
Daniel Stone4e84f7d2017-04-04 17:54:29 +01001721 return;
1722
Daniel Stonee95169b2016-11-14 17:46:59 +00001723 if (!pixman_region32_not_empty(damage) &&
1724 scanout_plane->state_cur->fb &&
1725 (scanout_plane->state_cur->fb->type == BUFFER_GBM_SURFACE ||
1726 scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB) &&
1727 scanout_plane->state_cur->fb->width ==
1728 output->base.current_mode->width &&
1729 scanout_plane->state_cur->fb->height ==
1730 output->base.current_mode->height) {
1731 fb = drm_fb_ref(scanout_plane->state_cur->fb);
1732 } else if (b->use_pixman) {
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001733 fb = drm_output_render_pixman(state, damage);
Daniel Stonee95169b2016-11-14 17:46:59 +00001734 } else {
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001735 fb = drm_output_render_gl(state, damage);
Daniel Stonee95169b2016-11-14 17:46:59 +00001736 }
Daniel Stone95d48a22017-04-04 17:54:30 +01001737
Daniel Stonee2e80132018-01-16 15:37:33 +00001738 if (!fb) {
1739 drm_plane_state_put_back(scanout_state);
Daniel Stone95d48a22017-04-04 17:54:30 +01001740 return;
Daniel Stonee2e80132018-01-16 15:37:33 +00001741 }
1742
1743 scanout_state->fb = fb;
1744 scanout_state->output = output;
1745
1746 scanout_state->src_x = 0;
1747 scanout_state->src_y = 0;
1748 scanout_state->src_w = output->base.current_mode->width << 16;
1749 scanout_state->src_h = output->base.current_mode->height << 16;
1750
1751 scanout_state->dest_x = 0;
1752 scanout_state->dest_y = 0;
1753 scanout_state->dest_w = scanout_state->src_w >> 16;
1754 scanout_state->dest_h = scanout_state->src_h >> 16;
1755
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001756
Giulio Camuffo954f1832014-10-11 18:27:30 +03001757 pixman_region32_subtract(&c->primary_plane.damage,
1758 &c->primary_plane.damage, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001759}
1760
1761static void
Richard Hughese7299962013-05-01 21:52:12 +01001762drm_output_set_gamma(struct weston_output *output_base,
1763 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
1764{
1765 int rc;
Armin Krezović545dba62016-08-05 15:54:18 +02001766 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001767 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +02001768 to_drm_backend(output->base.compositor);
Richard Hughese7299962013-05-01 21:52:12 +01001769
1770 /* check */
1771 if (output_base->gamma_size != size)
1772 return;
Richard Hughese7299962013-05-01 21:52:12 +01001773
Giulio Camuffo954f1832014-10-11 18:27:30 +03001774 rc = drmModeCrtcSetGamma(backend->drm.fd,
Richard Hughese7299962013-05-01 21:52:12 +01001775 output->crtc_id,
1776 size, r, g, b);
1777 if (rc)
1778 weston_log("set gamma failed: %m\n");
1779}
1780
Bryce Harringtonada4f072015-06-30 13:25:46 -07001781/* Determine the type of vblank synchronization to use for the output.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001782 *
Bryce Harringtonada4f072015-06-30 13:25:46 -07001783 * The pipe parameter indicates which CRTC is in use. Knowing this, we
1784 * can determine which vblank sequence type to use for it. Traditional
1785 * cards had only two CRTCs, with CRTC 0 using no special flags, and
1786 * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
1787 * parameter indicates this.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001788 *
Bryce Harringtonada4f072015-06-30 13:25:46 -07001789 * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
1790 * 0-31. If this is non-zero it indicates we're dealing with a
1791 * multi-gpu situation and we need to calculate the vblank sync
1792 * using DRM_BLANK_HIGH_CRTC_MASK.
1793 */
Pekka Paalanenc8a1ff02015-07-02 15:06:08 +03001794static unsigned int
1795drm_waitvblank_pipe(struct drm_output *output)
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +02001796{
1797 if (output->pipe > 1)
1798 return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
1799 DRM_VBLANK_HIGH_CRTC_MASK;
1800 else if (output->pipe > 0)
1801 return DRM_VBLANK_SECONDARY;
1802 else
1803 return 0;
1804}
1805
David Herrmann1edf44c2013-10-22 17:11:26 +02001806static int
Daniel Stone598ee9d2016-11-16 11:55:20 +00001807drm_output_apply_state_legacy(struct drm_output_state *state)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001808{
Daniel Stonea08512f2016-11-08 17:46:10 +00001809 struct drm_output *output = state->output;
1810 struct drm_backend *backend = to_drm_backend(output->base.compositor);
Daniel Stonee2e80132018-01-16 15:37:33 +00001811 struct drm_plane *scanout_plane = output->scanout_plane;
Daniel Stonea08512f2016-11-08 17:46:10 +00001812 struct drm_property_info *dpms_prop =
1813 &output->props_conn[WDRM_CONNECTOR_DPMS];
Daniel Stonee2e80132018-01-16 15:37:33 +00001814 struct drm_plane_state *scanout_state;
Daniel Stonebc15f682016-11-14 16:57:01 +00001815 struct drm_plane_state *ps;
Daniel Stone085d2b92015-05-21 00:00:57 +01001816 struct drm_plane *p;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001817 struct drm_mode *mode;
Daniel Stonea08512f2016-11-08 17:46:10 +00001818 struct timespec now;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001819 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001820
Derek Foreman2cd87fe2017-04-13 13:48:48 -05001821 /* If disable_planes is set then assign_planes() wasn't
1822 * called for this render, so we could still have a stale
1823 * cursor plane set up.
1824 */
1825 if (output->base.disable_planes) {
1826 output->cursor_view = NULL;
Daniel Stone2ba17f42015-05-19 20:02:41 +01001827 output->cursor_plane->base.x = INT32_MIN;
1828 output->cursor_plane->base.y = INT32_MIN;
Derek Foreman2cd87fe2017-04-13 13:48:48 -05001829 }
1830
Daniel Stonea08512f2016-11-08 17:46:10 +00001831 if (state->dpms != WESTON_DPMS_ON) {
1832 wl_list_for_each(ps, &state->plane_list, link) {
1833 p = ps->plane;
1834 assert(ps->fb == NULL);
1835 assert(ps->output == NULL);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001836
Daniel Stonea08512f2016-11-08 17:46:10 +00001837 if (p->type != WDRM_PLANE_TYPE_OVERLAY)
1838 continue;
1839
1840 ret = drmModeSetPlane(backend->drm.fd, p->plane_id,
1841 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1842 if (ret)
1843 weston_log("drmModeSetPlane failed disable: %m\n");
1844 }
1845
1846 if (output->cursor_plane) {
1847 ret = drmModeSetCursor(backend->drm.fd, output->crtc_id,
1848 0, 0, 0);
1849 if (ret)
1850 weston_log("drmModeSetCursor failed disable: %m\n");
1851 }
1852
1853 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id, 0, 0, 0,
1854 &output->connector_id, 0, NULL);
1855 if (ret)
1856 weston_log("drmModeSetCrtc failed disabling: %m\n");
1857
1858 drm_output_assign_state(state, DRM_STATE_APPLY_SYNC);
1859 weston_compositor_read_presentation_clock(output->base.compositor, &now);
1860 drm_output_update_complete(output,
1861 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION,
1862 now.tv_sec, now.tv_nsec / 1000);
1863
1864 return 0;
1865 }
1866
1867 scanout_state =
1868 drm_output_state_get_existing_plane(state, scanout_plane);
Daniel Stone087ddf02017-02-14 17:51:30 +00001869
Daniel Stonee2e80132018-01-16 15:37:33 +00001870 /* The legacy SetCrtc API doesn't allow us to do scaling, and the
1871 * legacy PageFlip API doesn't allow us to do clipping either. */
1872 assert(scanout_state->src_x == 0);
1873 assert(scanout_state->src_y == 0);
1874 assert(scanout_state->src_w ==
1875 (unsigned) (output->base.current_mode->width << 16));
1876 assert(scanout_state->src_h ==
1877 (unsigned) (output->base.current_mode->height << 16));
1878 assert(scanout_state->dest_x == 0);
1879 assert(scanout_state->dest_y == 0);
1880 assert(scanout_state->dest_w == scanout_state->src_w >> 16);
1881 assert(scanout_state->dest_h == scanout_state->src_h >> 16);
1882
Daniel Stonecb04cc42016-11-16 11:51:27 +00001883 mode = to_drm_mode(output->base.current_mode);
Daniel Stone6020f472018-02-05 15:46:20 +00001884 if (backend->state_invalid || !scanout_plane->state_cur->fb ||
Daniel Stonee2e80132018-01-16 15:37:33 +00001885 scanout_plane->state_cur->fb->stride != scanout_state->fb->stride) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001886 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Daniel Stonee2e80132018-01-16 15:37:33 +00001887 scanout_state->fb->fb_id,
1888 0, 0,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001889 &output->connector_id, 1,
1890 &mode->mode_info);
1891 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +02001892 weston_log("set mode failed: %m\n");
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001893 goto err;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001894 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001895 }
1896
Giulio Camuffo954f1832014-10-11 18:27:30 +03001897 if (drmModePageFlip(backend->drm.fd, output->crtc_id,
Daniel Stonee2e80132018-01-16 15:37:33 +00001898 scanout_state->fb->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -05001899 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001900 weston_log("queueing pageflip failed: %m\n");
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001901 goto err;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -05001902 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001903
Daniel Stone205c0a02017-04-04 17:54:33 +01001904 assert(!output->page_flip_pending);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001905
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001906 if (output->pageflip_timer)
1907 wl_event_source_timer_update(output->pageflip_timer,
1908 backend->pageflip_timeout);
1909
Daniel Stone2ba17f42015-05-19 20:02:41 +01001910 drm_output_set_cursor(state);
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001911
Jesse Barnes58ef3792012-02-23 09:45:49 -05001912 /*
1913 * Now, update all the sprite surfaces
1914 */
Daniel Stonebc15f682016-11-14 16:57:01 +00001915 wl_list_for_each(ps, &state->plane_list, link) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001916 uint32_t flags = 0, fb_id = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001917 drmVBlank vbl = {
1918 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
1919 .request.sequence = 1,
1920 };
1921
Daniel Stonebc15f682016-11-14 16:57:01 +00001922 p = ps->plane;
Daniel Stone085d2b92015-05-21 00:00:57 +01001923 if (p->type != WDRM_PLANE_TYPE_OVERLAY)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001924 continue;
1925
Daniel Stonebc15f682016-11-14 16:57:01 +00001926 assert(p->state_cur->complete);
1927 assert(!!p->state_cur->output == !!p->state_cur->fb);
1928 assert(!p->state_cur->output || p->state_cur->output == output);
1929 assert(!ps->complete);
1930 assert(!ps->output || ps->output == output);
1931 assert(!!ps->output == !!ps->fb);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001932
Daniel Stonebc15f682016-11-14 16:57:01 +00001933 if (ps->fb && !backend->sprites_hidden)
1934 fb_id = ps->fb->fb_id;
Daniel Stone085d2b92015-05-21 00:00:57 +01001935
1936 ret = drmModeSetPlane(backend->drm.fd, p->plane_id,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001937 output->crtc_id, fb_id, flags,
Daniel Stonebc15f682016-11-14 16:57:01 +00001938 ps->dest_x, ps->dest_y,
1939 ps->dest_w, ps->dest_h,
1940 ps->src_x, ps->src_y,
1941 ps->src_w, ps->src_h);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001942 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +02001943 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05001944 ret, strerror(errno));
1945
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +02001946 vbl.request.type |= drm_waitvblank_pipe(output);
Rob Clark5ca1a472012-08-08 20:27:37 -05001947
Jesse Barnes58ef3792012-02-23 09:45:49 -05001948 /*
1949 * Queue a vblank signal so we know when the surface
1950 * becomes active on the display or has been replaced.
1951 */
Daniel Stonebc15f682016-11-14 16:57:01 +00001952 vbl.request.signal = (unsigned long) ps;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001953 ret = drmWaitVBlank(backend->drm.fd, &vbl);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001954 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +02001955 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05001956 ret, strerror(errno));
1957 }
1958 }
1959
Daniel Stonea08512f2016-11-08 17:46:10 +00001960 if (dpms_prop->prop_id && state->dpms != output->state_cur->dpms) {
1961 ret = drmModeConnectorSetProperty(backend->drm.fd,
1962 output->connector_id,
1963 dpms_prop->prop_id,
1964 state->dpms);
1965 if (ret) {
1966 weston_log("DRM: DPMS: failed property set for %s\n",
1967 output->base.name);
1968 }
1969 }
1970
1971 drm_output_assign_state(state, DRM_STATE_APPLY_ASYNC);
1972
David Herrmann1edf44c2013-10-22 17:11:26 +02001973 return 0;
1974
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001975err:
Kristian Høgsbergb3955b02014-01-23 16:25:06 -08001976 output->cursor_view = NULL;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001977 drm_output_state_free(state);
Daniel Stonea08512f2016-11-08 17:46:10 +00001978 return -1;
1979}
David Herrmann1edf44c2013-10-22 17:11:26 +02001980
Daniel Stone598ee9d2016-11-16 11:55:20 +00001981#ifdef HAVE_DRM_ATOMIC
1982static int
1983crtc_add_prop(drmModeAtomicReq *req, struct drm_output *output,
1984 enum wdrm_crtc_property prop, uint64_t val)
1985{
1986 struct drm_property_info *info = &output->props_crtc[prop];
1987 int ret;
1988
1989 if (info->prop_id == 0)
1990 return -1;
1991
1992 ret = drmModeAtomicAddProperty(req, output->crtc_id, info->prop_id,
1993 val);
1994 return (ret <= 0) ? -1 : 0;
1995}
1996
1997static int
1998connector_add_prop(drmModeAtomicReq *req, struct drm_output *output,
1999 enum wdrm_connector_property prop, uint64_t val)
2000{
2001 struct drm_property_info *info = &output->props_conn[prop];
2002 int ret;
2003
2004 if (info->prop_id == 0)
2005 return -1;
2006
2007 ret = drmModeAtomicAddProperty(req, output->connector_id,
2008 info->prop_id, val);
2009 return (ret <= 0) ? -1 : 0;
2010}
2011
2012static int
2013plane_add_prop(drmModeAtomicReq *req, struct drm_plane *plane,
2014 enum wdrm_plane_property prop, uint64_t val)
2015{
2016 struct drm_property_info *info = &plane->props[prop];
2017 int ret;
2018
2019 if (info->prop_id == 0)
2020 return -1;
2021
2022 ret = drmModeAtomicAddProperty(req, plane->plane_id, info->prop_id,
2023 val);
2024 return (ret <= 0) ? -1 : 0;
2025}
2026
2027static int
2028drm_mode_ensure_blob(struct drm_backend *backend, struct drm_mode *mode)
2029{
2030 int ret;
2031
2032 if (mode->blob_id)
2033 return 0;
2034
2035 ret = drmModeCreatePropertyBlob(backend->drm.fd,
2036 &mode->mode_info,
2037 sizeof(mode->mode_info),
2038 &mode->blob_id);
2039 if (ret != 0)
2040 weston_log("failed to create mode property blob: %m\n");
2041
2042 return ret;
2043}
2044
2045static int
2046drm_output_apply_state_atomic(struct drm_output_state *state,
2047 drmModeAtomicReq *req,
2048 uint32_t *flags)
2049{
2050 struct drm_output *output = state->output;
2051 struct drm_backend *backend = to_drm_backend(output->base.compositor);
2052 struct drm_plane_state *plane_state;
2053 struct drm_mode *current_mode = to_drm_mode(output->base.current_mode);
2054 int ret = 0;
2055
2056 if (state->dpms != output->state_cur->dpms)
2057 *flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
2058
2059 if (state->dpms == WESTON_DPMS_ON) {
2060 ret = drm_mode_ensure_blob(backend, current_mode);
2061 if (ret != 0)
2062 return ret;
2063
2064 ret |= crtc_add_prop(req, output, WDRM_CRTC_MODE_ID,
2065 current_mode->blob_id);
2066 ret |= crtc_add_prop(req, output, WDRM_CRTC_ACTIVE, 1);
2067 ret |= connector_add_prop(req, output, WDRM_CONNECTOR_CRTC_ID,
2068 output->crtc_id);
2069 } else {
2070 ret |= crtc_add_prop(req, output, WDRM_CRTC_MODE_ID, 0);
2071 ret |= crtc_add_prop(req, output, WDRM_CRTC_ACTIVE, 0);
2072 ret |= connector_add_prop(req, output, WDRM_CONNECTOR_CRTC_ID,
2073 0);
2074 }
2075
2076 if (ret != 0) {
2077 weston_log("couldn't set atomic CRTC/connector state\n");
2078 return ret;
2079 }
2080
2081 wl_list_for_each(plane_state, &state->plane_list, link) {
2082 struct drm_plane *plane = plane_state->plane;
2083
2084 ret |= plane_add_prop(req, plane, WDRM_PLANE_FB_ID,
2085 plane_state->fb ? plane_state->fb->fb_id : 0);
2086 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_ID,
2087 plane_state->fb ? output->crtc_id : 0);
2088 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_X,
2089 plane_state->src_x);
2090 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_Y,
2091 plane_state->src_y);
2092 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_W,
2093 plane_state->src_w);
2094 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_H,
2095 plane_state->src_h);
2096 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_X,
2097 plane_state->dest_x);
2098 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_Y,
2099 plane_state->dest_y);
2100 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_W,
2101 plane_state->dest_w);
2102 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_H,
2103 plane_state->dest_h);
2104
2105 if (ret != 0) {
2106 weston_log("couldn't set plane state\n");
2107 return ret;
2108 }
2109 }
2110
2111 return 0;
2112}
2113
2114/**
2115 * Helper function used only by drm_pending_state_apply, with the same
2116 * guarantees and constraints as that function.
2117 */
2118static int
2119drm_pending_state_apply_atomic(struct drm_pending_state *pending_state,
2120 enum drm_state_apply_mode mode)
2121{
2122 struct drm_backend *b = pending_state->backend;
2123 struct drm_output_state *output_state, *tmp;
2124 struct drm_plane *plane;
2125 drmModeAtomicReq *req = drmModeAtomicAlloc();
2126 uint32_t flags = 0;
2127 int ret = 0;
2128
2129 if (!req)
2130 return -1;
2131
2132 if (b->state_invalid) {
2133 uint32_t *unused;
2134 int err;
2135
2136 /* If we need to reset all our state (e.g. because we've
2137 * just started, or just been VT-switched in), explicitly
2138 * disable all the CRTCs and connectors we aren't using. */
2139 wl_array_for_each(unused, &b->unused_connectors) {
2140 struct drm_property_info infos[WDRM_CONNECTOR__COUNT];
2141 struct drm_property_info *info;
2142 drmModeObjectProperties *props;
2143
2144 memset(infos, 0, sizeof(infos));
2145
2146 props = drmModeObjectGetProperties(b->drm.fd,
2147 *unused,
2148 DRM_MODE_OBJECT_CONNECTOR);
2149 if (!props) {
2150 ret = -1;
2151 continue;
2152 }
2153
2154 drm_property_info_populate(b, connector_props, infos,
2155 WDRM_CONNECTOR__COUNT,
2156 props);
2157 drmModeFreeObjectProperties(props);
2158
2159 info = &infos[WDRM_CONNECTOR_CRTC_ID];
2160 err = drmModeAtomicAddProperty(req, *unused,
2161 info->prop_id, 0);
2162 if (err <= 0)
2163 ret = -1;
2164
2165 info = &infos[WDRM_CONNECTOR_DPMS];
2166 if (info->prop_id > 0)
2167 err = drmModeAtomicAddProperty(req, *unused,
2168 info->prop_id,
2169 DRM_MODE_DPMS_OFF);
2170 if (err <= 0)
2171 ret = -1;
2172
2173 drm_property_info_free(infos, WDRM_CONNECTOR__COUNT);
2174 }
2175
2176 wl_array_for_each(unused, &b->unused_crtcs) {
2177 struct drm_property_info infos[WDRM_CRTC__COUNT];
2178 struct drm_property_info *info;
2179 drmModeObjectProperties *props;
2180 uint64_t active;
2181
2182 memset(infos, 0, sizeof(infos));
2183
2184 /* We can't emit a disable on a CRTC that's already
2185 * off, as the kernel will refuse to generate an event
2186 * for an off->off state and fail the commit.
2187 */
2188 props = drmModeObjectGetProperties(b->drm.fd,
2189 *unused,
2190 DRM_MODE_OBJECT_CRTC);
2191 if (!props) {
2192 ret = -1;
2193 continue;
2194 }
2195
2196 drm_property_info_populate(b, crtc_props, infos,
2197 WDRM_CRTC__COUNT,
2198 props);
2199
2200 info = &infos[WDRM_CRTC_ACTIVE];
2201 active = drm_property_get_value(info, props, 0);
2202 drmModeFreeObjectProperties(props);
2203 if (active == 0) {
2204 drm_property_info_free(infos, WDRM_CRTC__COUNT);
2205 continue;
2206 }
2207
2208 err = drmModeAtomicAddProperty(req, *unused,
2209 info->prop_id, 0);
2210 if (err <= 0)
2211 ret = -1;
2212
2213 info = &infos[WDRM_CRTC_MODE_ID];
2214 err = drmModeAtomicAddProperty(req, *unused,
2215 info->prop_id, 0);
2216 if (err <= 0)
2217 ret = -1;
2218
2219 drm_property_info_free(infos, WDRM_CRTC__COUNT);
2220 }
2221
2222 /* Disable all the planes; planes which are being used will
2223 * override this state in the output-state application. */
2224 wl_list_for_each(plane, &b->plane_list, link) {
2225 plane_add_prop(req, plane, WDRM_PLANE_CRTC_ID, 0);
2226 plane_add_prop(req, plane, WDRM_PLANE_FB_ID, 0);
2227 }
2228
2229 flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
2230 }
2231
2232 wl_list_for_each(output_state, &pending_state->output_list, link) {
2233 if (mode == DRM_STATE_APPLY_SYNC)
2234 assert(output_state->dpms == WESTON_DPMS_OFF);
2235 ret |= drm_output_apply_state_atomic(output_state, req, &flags);
2236 }
2237
2238 if (ret != 0) {
2239 weston_log("atomic: couldn't compile atomic state\n");
2240 goto out;
2241 }
2242
2243 switch (mode) {
2244 case DRM_STATE_APPLY_SYNC:
2245 break;
2246 case DRM_STATE_APPLY_ASYNC:
2247 flags |= DRM_MODE_PAGE_FLIP_EVENT | DRM_MODE_ATOMIC_NONBLOCK;
2248 break;
2249 }
2250
2251 ret = drmModeAtomicCommit(b->drm.fd, req, flags, b);
2252 if (ret != 0) {
2253 weston_log("atomic: couldn't commit new state: %m\n");
2254 goto out;
2255 }
2256
2257 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
2258 link)
2259 drm_output_assign_state(output_state, mode);
2260
2261 b->state_invalid = false;
2262
2263 assert(wl_list_empty(&pending_state->output_list));
2264
2265out:
2266 drmModeAtomicFree(req);
2267 drm_pending_state_free(pending_state);
2268 return ret;
2269}
2270#endif
2271
Daniel Stonea08512f2016-11-08 17:46:10 +00002272/**
2273 * Applies all of a pending_state asynchronously: the primary entry point for
2274 * applying KMS state to a device. Updates the state for all outputs in the
2275 * pending_state, as well as disabling any unclaimed outputs.
2276 *
2277 * Unconditionally takes ownership of pending_state, and clears state_invalid.
2278 */
2279static int
2280drm_pending_state_apply(struct drm_pending_state *pending_state)
2281{
2282 struct drm_backend *b = pending_state->backend;
2283 struct drm_output_state *output_state, *tmp;
2284 uint32_t *unused;
2285
Daniel Stone598ee9d2016-11-16 11:55:20 +00002286#ifdef HAVE_DRM_ATOMIC
2287 if (b->atomic_modeset)
2288 return drm_pending_state_apply_atomic(pending_state,
2289 DRM_STATE_APPLY_ASYNC);
2290#endif
2291
Daniel Stonea08512f2016-11-08 17:46:10 +00002292 if (b->state_invalid) {
2293 /* If we need to reset all our state (e.g. because we've
2294 * just started, or just been VT-switched in), explicitly
2295 * disable all the CRTCs we aren't using. This also disables
2296 * all connectors on these CRTCs, so we don't need to do that
2297 * separately with the pre-atomic API. */
2298 wl_array_for_each(unused, &b->unused_crtcs)
2299 drmModeSetCrtc(b->drm.fd, *unused, 0, 0, 0, NULL, 0,
2300 NULL);
2301 }
2302
2303 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
2304 link) {
2305 struct drm_output *output = output_state->output;
2306 int ret;
2307
Daniel Stone598ee9d2016-11-16 11:55:20 +00002308 ret = drm_output_apply_state_legacy(output_state);
Daniel Stonea08512f2016-11-08 17:46:10 +00002309 if (ret != 0) {
2310 weston_log("Couldn't apply state for output %s\n",
2311 output->base.name);
2312 }
2313 }
2314
2315 b->state_invalid = false;
2316
2317 assert(wl_list_empty(&pending_state->output_list));
2318
2319 drm_pending_state_free(pending_state);
2320
2321 return 0;
2322}
2323
2324/**
2325 * The synchronous version of drm_pending_state_apply. May only be used to
2326 * disable outputs. Does so synchronously: the request is guaranteed to have
2327 * completed on return, and the output will not be touched afterwards.
2328 *
2329 * Unconditionally takes ownership of pending_state, and clears state_invalid.
2330 */
2331static int
2332drm_pending_state_apply_sync(struct drm_pending_state *pending_state)
2333{
2334 struct drm_backend *b = pending_state->backend;
2335 struct drm_output_state *output_state, *tmp;
2336 uint32_t *unused;
2337
Daniel Stone598ee9d2016-11-16 11:55:20 +00002338#ifdef HAVE_DRM_ATOMIC
2339 if (b->atomic_modeset)
2340 return drm_pending_state_apply_atomic(pending_state,
2341 DRM_STATE_APPLY_SYNC);
2342#endif
2343
Daniel Stonea08512f2016-11-08 17:46:10 +00002344 if (b->state_invalid) {
2345 /* If we need to reset all our state (e.g. because we've
2346 * just started, or just been VT-switched in), explicitly
2347 * disable all the CRTCs we aren't using. This also disables
2348 * all connectors on these CRTCs, so we don't need to do that
2349 * separately with the pre-atomic API. */
2350 wl_array_for_each(unused, &b->unused_crtcs)
2351 drmModeSetCrtc(b->drm.fd, *unused, 0, 0, 0, NULL, 0,
2352 NULL);
2353 }
2354
2355 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
2356 link) {
2357 int ret;
2358
2359 assert(output_state->dpms == WESTON_DPMS_OFF);
Daniel Stone598ee9d2016-11-16 11:55:20 +00002360 ret = drm_output_apply_state_legacy(output_state);
Daniel Stonea08512f2016-11-08 17:46:10 +00002361 if (ret != 0) {
2362 weston_log("Couldn't apply state for output %s\n",
2363 output_state->output->base.name);
2364 }
2365 }
2366
2367 b->state_invalid = false;
2368
2369 assert(wl_list_empty(&pending_state->output_list));
2370
2371 drm_pending_state_free(pending_state);
2372
2373 return 0;
2374}
2375
2376static int
2377drm_output_repaint(struct weston_output *output_base,
2378 pixman_region32_t *damage,
2379 void *repaint_data)
2380{
2381 struct drm_pending_state *pending_state = repaint_data;
2382 struct drm_output *output = to_drm_output(output_base);
2383 struct drm_backend *backend = to_drm_backend(output_base->compositor);
2384 struct drm_output_state *state = NULL;
2385 struct drm_plane_state *scanout_state;
2386
2387 if (output->disable_pending || output->destroy_pending)
2388 goto err;
2389
2390 assert(!output->state_last);
2391
2392 /* If planes have been disabled in the core, we might not have
2393 * hit assign_planes at all, so might not have valid output state
2394 * here. */
2395 state = drm_pending_state_get_output(pending_state, output);
2396 if (!state)
2397 state = drm_output_state_duplicate(output->state_cur,
2398 pending_state,
2399 DRM_OUTPUT_STATE_CLEAR_PLANES);
2400 state->dpms = WESTON_DPMS_ON;
2401
2402 drm_output_render(state, damage);
2403 scanout_state = drm_output_state_get_plane(state,
2404 output->scanout_plane);
2405 if (!scanout_state || !scanout_state->fb)
2406 goto err;
2407
2408 wl_array_remove_uint32(&backend->unused_connectors,
2409 output->connector_id);
2410 wl_array_remove_uint32(&backend->unused_crtcs, output->crtc_id);
2411
2412 return 0;
2413
2414err:
2415 drm_output_state_free(state);
David Herrmann1edf44c2013-10-22 17:11:26 +02002416 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002417}
2418
2419static void
Jonas Ådahle5a12252013-04-05 23:07:11 +02002420drm_output_start_repaint_loop(struct weston_output *output_base)
2421{
Armin Krezović545dba62016-08-05 15:54:18 +02002422 struct drm_output *output = to_drm_output(output_base);
Daniel Stone8747f952016-11-29 20:17:32 +00002423 struct drm_pending_state *pending_state;
Daniel Stonee2e80132018-01-16 15:37:33 +00002424 struct drm_plane *scanout_plane = output->scanout_plane;
Armin Krezović545dba62016-08-05 15:54:18 +02002425 struct drm_backend *backend =
2426 to_drm_backend(output_base->compositor);
Mario Kleinerf507ec32015-06-21 21:25:14 +02002427 struct timespec ts, tnow;
2428 struct timespec vbl2now;
2429 int64_t refresh_nsec;
2430 int ret;
2431 drmVBlank vbl = {
2432 .request.type = DRM_VBLANK_RELATIVE,
2433 .request.sequence = 0,
2434 .request.signal = 0,
2435 };
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03002436
Armin Krezović08368132016-09-30 14:11:05 +02002437 if (output->disable_pending || output->destroy_pending)
Xiong Zhangabd5d472013-10-11 14:43:07 +08002438 return;
2439
Daniel Stonee2e80132018-01-16 15:37:33 +00002440 if (!output->scanout_plane->state_cur->fb) {
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03002441 /* We can't page flip if there's no mode set */
David Herrmann3c688c52013-10-22 17:11:25 +02002442 goto finish_frame;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03002443 }
2444
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03002445 /* Need to smash all state in from scratch; current timings might not
2446 * be what we want, page flip might not work, etc.
2447 */
Daniel Stone6020f472018-02-05 15:46:20 +00002448 if (backend->state_invalid)
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03002449 goto finish_frame;
2450
Daniel Stonee2e80132018-01-16 15:37:33 +00002451 assert(scanout_plane->state_cur->output == output);
2452
Mario Kleinerf507ec32015-06-21 21:25:14 +02002453 /* Try to get current msc and timestamp via instant query */
2454 vbl.request.type |= drm_waitvblank_pipe(output);
2455 ret = drmWaitVBlank(backend->drm.fd, &vbl);
2456
2457 /* Error ret or zero timestamp means failure to get valid timestamp */
2458 if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
2459 ts.tv_sec = vbl.reply.tval_sec;
2460 ts.tv_nsec = vbl.reply.tval_usec * 1000;
2461
2462 /* Valid timestamp for most recent vblank - not stale?
2463 * Stale ts could happen on Linux 3.17+, so make sure it
2464 * is not older than 1 refresh duration since now.
2465 */
2466 weston_compositor_read_presentation_clock(backend->compositor,
2467 &tnow);
2468 timespec_sub(&vbl2now, &tnow, &ts);
2469 refresh_nsec =
2470 millihz_to_nsec(output->base.current_mode->refresh);
2471 if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
2472 drm_output_update_msc(output, vbl.reply.sequence);
2473 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002474 WP_PRESENTATION_FEEDBACK_INVALID);
Mario Kleinerf507ec32015-06-21 21:25:14 +02002475 return;
2476 }
2477 }
2478
2479 /* Immediate query didn't provide valid timestamp.
2480 * Use pageflip fallback.
2481 */
Jonas Ådahle5a12252013-04-05 23:07:11 +02002482
Daniel Stone205c0a02017-04-04 17:54:33 +01002483 assert(!output->page_flip_pending);
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002484 assert(!output->state_last);
2485
2486 pending_state = drm_pending_state_alloc(backend);
Daniel Stone8747f952016-11-29 20:17:32 +00002487 drm_output_state_duplicate(output->state_cur, pending_state,
2488 DRM_OUTPUT_STATE_PRESERVE_PLANES);
Daniel Stone205c0a02017-04-04 17:54:33 +01002489
Daniel Stone8747f952016-11-29 20:17:32 +00002490 ret = drm_pending_state_apply(pending_state);
2491 if (ret != 0) {
2492 weston_log("applying repaint-start state failed: %m\n");
David Herrmann3c688c52013-10-22 17:11:25 +02002493 goto finish_frame;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002494 }
David Herrmann3c688c52013-10-22 17:11:25 +02002495
2496 return;
2497
2498finish_frame:
2499 /* if we cannot page-flip, immediately finish frame */
Daniel Stone3615ce12017-03-01 11:34:05 +00002500 weston_output_finish_frame(output_base, NULL,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002501 WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002502}
2503
2504static void
Pekka Paalanen641307c2014-09-23 22:08:47 -04002505drm_output_update_msc(struct drm_output *output, unsigned int seq)
2506{
2507 uint64_t msc_hi = output->base.msc >> 32;
2508
2509 if (seq < (output->base.msc & 0xffffffff))
2510 msc_hi++;
2511
2512 output->base.msc = (msc_hi << 32) + seq;
2513}
2514
2515static void
Jesse Barnes58ef3792012-02-23 09:45:49 -05002516vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
2517 void *data)
2518{
Daniel Stonebc15f682016-11-14 16:57:01 +00002519 struct drm_plane_state *ps = (struct drm_plane_state *) data;
2520 struct drm_output_state *os = ps->output_state;
2521 struct drm_output *output = os->output;
Daniel Stone598ee9d2016-11-16 11:55:20 +00002522 struct drm_backend *b = to_drm_backend(output->base.compositor);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002523 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
2524 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03002525
Daniel Stone598ee9d2016-11-16 11:55:20 +00002526 assert(!b->atomic_modeset);
2527
Pekka Paalanen641307c2014-09-23 22:08:47 -04002528 drm_output_update_msc(output, frame);
Daniel Stone65d87d02017-04-04 17:54:32 +01002529 output->vblank_pending--;
2530 assert(output->vblank_pending >= 0);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002531
Daniel Stonebc15f682016-11-14 16:57:01 +00002532 assert(ps->fb);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03002533
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002534 if (output->page_flip_pending || output->vblank_pending)
2535 return;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00002536
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002537 drm_output_update_complete(output, flags, sec, usec);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002538}
2539
2540static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002541page_flip_handler(int fd, unsigned int frame,
2542 unsigned int sec, unsigned int usec, void *data)
2543{
Armin Krezović545dba62016-08-05 15:54:18 +02002544 struct drm_output *output = data;
Daniel Stone598ee9d2016-11-16 11:55:20 +00002545 struct drm_backend *b = to_drm_backend(output->base.compositor);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002546 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
2547 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
2548 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002549
Pekka Paalanen641307c2014-09-23 22:08:47 -04002550 drm_output_update_msc(output, frame);
2551
Daniel Stone598ee9d2016-11-16 11:55:20 +00002552 assert(!b->atomic_modeset);
Daniel Stone205c0a02017-04-04 17:54:33 +01002553 assert(output->page_flip_pending);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002554 output->page_flip_pending = 0;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002555
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002556 if (output->vblank_pending)
2557 return;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00002558
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002559 drm_output_update_complete(output, flags, sec, usec);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02002560}
2561
Daniel Stoneeedf84c2017-02-10 18:06:04 +00002562/**
2563 * Begin a new repaint cycle
2564 *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002565 * Called by the core compositor at the beginning of a repaint cycle. Creates
2566 * a new pending_state structure to own any output state created by individual
2567 * output repaint functions until the repaint is flushed or cancelled.
Daniel Stoneeedf84c2017-02-10 18:06:04 +00002568 */
2569static void *
2570drm_repaint_begin(struct weston_compositor *compositor)
2571{
2572 struct drm_backend *b = to_drm_backend(compositor);
2573 struct drm_pending_state *ret;
2574
2575 ret = drm_pending_state_alloc(b);
2576 b->repaint_data = ret;
2577
2578 return ret;
2579}
2580
2581/**
2582 * Flush a repaint set
2583 *
2584 * Called by the core compositor when a repaint cycle has been completed
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002585 * and should be flushed. Frees the pending state, transitioning ownership
2586 * of the output state from the pending state, to the update itself. When
2587 * the update completes (see drm_output_update_complete), the output
2588 * state will be freed.
Daniel Stoneeedf84c2017-02-10 18:06:04 +00002589 */
2590static void
2591drm_repaint_flush(struct weston_compositor *compositor, void *repaint_data)
2592{
2593 struct drm_backend *b = to_drm_backend(compositor);
2594 struct drm_pending_state *pending_state = repaint_data;
Daniel Stone6020f472018-02-05 15:46:20 +00002595
Daniel Stonea08512f2016-11-08 17:46:10 +00002596 drm_pending_state_apply(pending_state);
Daniel Stoneeedf84c2017-02-10 18:06:04 +00002597 b->repaint_data = NULL;
2598}
2599
2600/**
2601 * Cancel a repaint set
2602 *
2603 * Called by the core compositor when a repaint has finished, so the data
2604 * held across the repaint cycle should be discarded.
2605 */
2606static void
2607drm_repaint_cancel(struct weston_compositor *compositor, void *repaint_data)
2608{
2609 struct drm_backend *b = to_drm_backend(compositor);
2610 struct drm_pending_state *pending_state = repaint_data;
2611
2612 drm_pending_state_free(pending_state);
2613 b->repaint_data = NULL;
2614}
2615
Daniel Stone598ee9d2016-11-16 11:55:20 +00002616#ifdef HAVE_DRM_ATOMIC
2617static void
2618atomic_flip_handler(int fd, unsigned int frame, unsigned int sec,
2619 unsigned int usec, unsigned int crtc_id, void *data)
2620{
2621 struct drm_backend *b = data;
2622 struct drm_output *output = drm_output_find_by_crtc(b, crtc_id);
2623 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
2624 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
2625 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
2626
2627 /* During the initial modeset, we can disable CRTCs which we don't
2628 * actually handle during normal operation; this will give us events
2629 * for unknown outputs. Ignore them. */
2630 if (!output || !output->base.enabled)
2631 return;
2632
2633 drm_output_update_msc(output, frame);
2634
2635 assert(b->atomic_modeset);
2636 assert(output->atomic_complete_pending);
2637 output->atomic_complete_pending = 0;
2638
2639 drm_output_update_complete(output, flags, sec, usec);
2640}
2641#endif
2642
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05002643static uint32_t
Daniel Stone08d4edf2017-04-04 17:54:34 +01002644drm_output_check_plane_format(struct drm_plane *p,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002645 struct weston_view *ev, struct gbm_bo *bo)
Jesse Barnes58ef3792012-02-23 09:45:49 -05002646{
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05002647 uint32_t i, format;
2648
2649 format = gbm_bo_get_format(bo);
2650
2651 if (format == GBM_FORMAT_ARGB8888) {
2652 pixman_region32_t r;
2653
Kristian Høgsberg63093a32013-03-01 14:29:16 -05002654 pixman_region32_init_rect(&r, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002655 ev->surface->width,
2656 ev->surface->height);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002657 pixman_region32_subtract(&r, &r, &ev->surface->opaque);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05002658
2659 if (!pixman_region32_not_empty(&r))
2660 format = GBM_FORMAT_XRGB8888;
2661
2662 pixman_region32_fini(&r);
2663 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05002664
Daniel Stone08d4edf2017-04-04 17:54:34 +01002665 for (i = 0; i < p->count_formats; i++)
2666 if (p->formats[i] == format)
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05002667 return format;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002668
2669 return 0;
2670}
2671
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002672static struct weston_plane *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002673drm_output_prepare_overlay_view(struct drm_output_state *output_state,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002674 struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -05002675{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002676 struct drm_output *output = output_state->output;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02002677 struct weston_compositor *ec = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02002678 struct drm_backend *b = to_drm_backend(ec);
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002679 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002680 struct wl_resource *buffer_resource;
Daniel Stone08d4edf2017-04-04 17:54:34 +01002681 struct drm_plane *p;
Daniel Stonebc15f682016-11-14 16:57:01 +00002682 struct drm_plane_state *state = NULL;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002683 struct linux_dmabuf_buffer *dmabuf;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002684 struct gbm_bo *bo;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002685 pixman_region32_t dest_rect, src_rect;
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02002686 pixman_box32_t *box, tbox;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002687 uint32_t format;
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04002688 wl_fixed_t sx1, sy1, sx2, sy2;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002689
Giulio Camuffo954f1832014-10-11 18:27:30 +03002690 if (b->sprites_are_broken)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002691 return NULL;
Kristian Høgsberg65bec242012-03-05 19:57:35 -05002692
Daniel Stone296d7a92016-10-21 18:05:37 +01002693 /* Don't import buffers which span multiple outputs. */
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02002694 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002695 return NULL;
Ander Conselvan de Oliveirad450b192012-06-26 17:09:12 +03002696
Daniel Stone296d7a92016-10-21 18:05:37 +01002697 /* We can only import GBM buffers. */
2698 if (b->gbm == NULL)
2699 return NULL;
2700
Jason Ekstranda7af7042013-10-12 22:38:11 -05002701 if (ev->surface->buffer_ref.buffer == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002702 return NULL;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002703 buffer_resource = ev->surface->buffer_ref.buffer->resource;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002704 if (wl_shm_buffer_get(buffer_resource))
Rob Clark702ffae2012-08-09 14:18:27 -05002705 return NULL;
2706
Daniel Stone296d7a92016-10-21 18:05:37 +01002707 if (viewport->buffer.transform != output->base.transform)
2708 return NULL;
2709 if (viewport->buffer.scale != output->base.current_scale)
2710 return NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002711 if (!drm_view_transform_supported(ev))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002712 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002713
Daniel Stone296d7a92016-10-21 18:05:37 +01002714 if (ev->alpha != 1.0f)
2715 return NULL;
2716
Daniel Stone085d2b92015-05-21 00:00:57 +01002717 wl_list_for_each(p, &b->plane_list, link) {
2718 if (p->type != WDRM_PLANE_TYPE_OVERLAY)
2719 continue;
2720
Daniel Stone5ff289a2017-10-07 12:59:02 +01002721 if (!drm_plane_is_available(p, output))
Daniel Stonebc15f682016-11-14 16:57:01 +00002722 continue;
2723
2724 state = drm_output_state_get_plane(output_state, p);
2725 if (state->fb) {
2726 state = NULL;
2727 continue;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002728 }
Daniel Stonebc15f682016-11-14 16:57:01 +00002729
2730 break;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002731 }
2732
2733 /* No sprites available */
Daniel Stonebc15f682016-11-14 16:57:01 +00002734 if (!state)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002735 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002736
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002737 if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) {
Bryce Harringtona3582072015-08-14 12:23:13 -07002738#ifdef HAVE_GBM_FD_IMPORT
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002739 /* XXX: TODO:
2740 *
2741 * Use AddFB2 directly, do not go via GBM.
2742 * Add support for multiplanar formats.
2743 * Both require refactoring in the DRM-backend to
2744 * support a mix of gbm_bos and drmfbs.
2745 */
2746 struct gbm_import_fd_data gbm_dmabuf = {
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00002747 .fd = dmabuf->attributes.fd[0],
2748 .width = dmabuf->attributes.width,
2749 .height = dmabuf->attributes.height,
2750 .stride = dmabuf->attributes.stride[0],
2751 .format = dmabuf->attributes.format
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002752 };
2753
Micah Fedkec8890122017-02-01 15:28:23 -05002754 /* XXX: TODO:
2755 *
2756 * Currently the buffer is rejected if any dmabuf attribute
2757 * flag is set. This keeps us from passing an inverted /
2758 * interlaced / bottom-first buffer (or any other type that may
2759 * be added in the future) through to an overlay. Ultimately,
2760 * these types of buffers should be handled through buffer
2761 * transforms and not as spot-checks requiring specific
2762 * knowledge. */
2763 if (dmabuf->attributes.n_planes != 1 ||
2764 dmabuf->attributes.offset[0] != 0 ||
2765 dmabuf->attributes.flags)
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002766 return NULL;
2767
2768 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf,
2769 GBM_BO_USE_SCANOUT);
Bryce Harringtona3582072015-08-14 12:23:13 -07002770#else
Daniel Stonebc15f682016-11-14 16:57:01 +00002771 goto err;
Bryce Harringtona3582072015-08-14 12:23:13 -07002772#endif
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002773 } else {
2774 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
2775 buffer_resource, GBM_BO_USE_SCANOUT);
2776 }
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04002777 if (!bo)
Daniel Stonebc15f682016-11-14 16:57:01 +00002778 goto err;
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04002779
Daniel Stone08d4edf2017-04-04 17:54:34 +01002780 format = drm_output_check_plane_format(p, ev, bo);
Daniel Stonebc15f682016-11-14 16:57:01 +00002781 if (format == 0)
2782 goto err;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002783
Daniel Stonebc15f682016-11-14 16:57:01 +00002784 state->fb = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT);
2785 if (!state->fb)
2786 goto err;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02002787
Daniel Stonebc15f682016-11-14 16:57:01 +00002788 drm_fb_set_buffer(state->fb, ev->surface->buffer_ref.buffer);
2789
2790 state->output = output;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002791
Jason Ekstranda7af7042013-10-12 22:38:11 -05002792 box = pixman_region32_extents(&ev->transform.boundingbox);
Daniel Stone08d4edf2017-04-04 17:54:34 +01002793 p->base.x = box->x1;
2794 p->base.y = box->y1;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002795
Jesse Barnes58ef3792012-02-23 09:45:49 -05002796 /*
2797 * Calculate the source & dest rects properly based on actual
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002798 * position (note the caller has called weston_view_update_transform()
Jesse Barnes58ef3792012-02-23 09:45:49 -05002799 * for us already).
2800 */
2801 pixman_region32_init(&dest_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002802 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02002803 &output->base.region);
2804 pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002805 box = pixman_region32_extents(&dest_rect);
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02002806 tbox = weston_transformed_rect(output->base.width,
2807 output->base.height,
2808 output->base.transform,
2809 output->base.current_scale,
Alexander Larssond9a7bb72013-05-22 14:41:39 +02002810 *box);
Daniel Stonebc15f682016-11-14 16:57:01 +00002811 state->dest_x = tbox.x1;
2812 state->dest_y = tbox.y1;
2813 state->dest_w = tbox.x2 - tbox.x1;
2814 state->dest_h = tbox.y2 - tbox.y1;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002815 pixman_region32_fini(&dest_rect);
2816
2817 pixman_region32_init(&src_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002818 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02002819 &output->base.region);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002820 box = pixman_region32_extents(&src_rect);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04002821
Jason Ekstranda7af7042013-10-12 22:38:11 -05002822 weston_view_from_global_fixed(ev,
2823 wl_fixed_from_int(box->x1),
2824 wl_fixed_from_int(box->y1),
2825 &sx1, &sy1);
2826 weston_view_from_global_fixed(ev,
2827 wl_fixed_from_int(box->x2),
2828 wl_fixed_from_int(box->y2),
2829 &sx2, &sy2);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04002830
2831 if (sx1 < 0)
2832 sx1 = 0;
2833 if (sy1 < 0)
2834 sy1 = 0;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002835 if (sx2 > wl_fixed_from_int(ev->surface->width))
2836 sx2 = wl_fixed_from_int(ev->surface->width);
2837 if (sy2 > wl_fixed_from_int(ev->surface->height))
2838 sy2 = wl_fixed_from_int(ev->surface->height);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04002839
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02002840 tbox.x1 = sx1;
2841 tbox.y1 = sy1;
2842 tbox.x2 = sx2;
2843 tbox.y2 = sy2;
2844
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002845 tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width),
2846 wl_fixed_from_int(ev->surface->height),
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002847 viewport->buffer.transform,
2848 viewport->buffer.scale,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002849 tbox);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02002850
Daniel Stonebc15f682016-11-14 16:57:01 +00002851 state->src_x = tbox.x1 << 8;
2852 state->src_y = tbox.y1 << 8;
2853 state->src_w = (tbox.x2 - tbox.x1) << 8;
2854 state->src_h = (tbox.y2 - tbox.y1) << 8;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002855 pixman_region32_fini(&src_rect);
2856
Daniel Stone08d4edf2017-04-04 17:54:34 +01002857 return &p->base;
Daniel Stonebc15f682016-11-14 16:57:01 +00002858
2859err:
2860 drm_plane_state_put_back(state);
2861 if (bo)
2862 gbm_bo_destroy(bo);
2863 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002864}
2865
Pekka Paalanend0ead482014-06-16 12:05:40 +03002866/**
2867 * Update the image for the current cursor surface
2868 *
2869 * @param b DRM backend structure
2870 * @param bo GBM buffer object to write into
2871 * @param ev View to use for cursor image
2872 */
2873static void
2874cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo,
2875 struct weston_view *ev)
2876{
2877 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
2878 uint32_t buf[b->cursor_width * b->cursor_height];
2879 int32_t stride;
2880 uint8_t *s;
2881 int i;
2882
2883 assert(buffer && buffer->shm_buffer);
2884 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
2885 assert(ev->surface->width <= b->cursor_width);
2886 assert(ev->surface->height <= b->cursor_height);
2887
2888 memset(buf, 0, sizeof buf);
2889 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
2890 s = wl_shm_buffer_get_data(buffer->shm_buffer);
2891
2892 wl_shm_buffer_begin_access(buffer->shm_buffer);
2893 for (i = 0; i < ev->surface->height; i++)
2894 memcpy(buf + i * b->cursor_width,
2895 s + i * stride,
2896 ev->surface->width * 4);
2897 wl_shm_buffer_end_access(buffer->shm_buffer);
2898
2899 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
2900 weston_log("failed update cursor: %m\n");
2901}
2902
Daniel Stone2ba17f42015-05-19 20:02:41 +01002903static struct weston_plane *
2904drm_output_prepare_cursor_view(struct drm_output_state *output_state,
2905 struct weston_view *ev)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04002906{
Daniel Stone2ba17f42015-05-19 20:02:41 +01002907 struct drm_output *output = output_state->output;
Armin Krezović545dba62016-08-05 15:54:18 +02002908 struct drm_backend *b = to_drm_backend(output->base.compositor);
Daniel Stone2ba17f42015-05-19 20:02:41 +01002909 struct drm_plane *plane = output->cursor_plane;
2910 struct drm_plane_state *plane_state;
2911 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
2912 struct wl_shm_buffer *shmbuf;
2913 bool needs_update = false;
Derek Foremanbe428b32015-11-24 11:39:38 -06002914 float x, y;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05002915
Daniel Stone2ba17f42015-05-19 20:02:41 +01002916 if (!plane)
2917 return NULL;
2918
2919 if (b->cursors_are_broken)
2920 return NULL;
2921
2922 if (!plane->state_cur->complete)
2923 return NULL;
2924
2925 if (plane->state_cur->output && plane->state_cur->output != output)
2926 return NULL;
2927
2928 /* Don't import buffers which span multiple outputs. */
2929 if (ev->output_mask != (1u << output->base.id))
2930 return NULL;
2931
2932 /* We use GBM to import SHM buffers. */
2933 if (b->gbm == NULL)
2934 return NULL;
2935
2936 if (ev->surface->buffer_ref.buffer == NULL)
2937 return NULL;
2938 shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource);
2939 if (!shmbuf)
2940 return NULL;
2941 if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888)
2942 return NULL;
2943
2944 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
2945 return NULL;
2946 if (ev->transform.enabled &&
2947 (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE))
2948 return NULL;
2949 if (viewport->buffer.scale != output->base.current_scale)
2950 return NULL;
2951 if (ev->geometry.scissor_enabled)
2952 return NULL;
2953
2954 if (ev->surface->width > b->cursor_width ||
2955 ev->surface->height > b->cursor_height)
2956 return NULL;
2957
2958 plane_state =
2959 drm_output_state_get_plane(output_state, output->cursor_plane);
2960
2961 if (plane_state && plane_state->fb)
2962 return NULL;
2963
2964 /* Since we're setting plane state up front, we need to work out
2965 * whether or not we need to upload a new cursor. We can't use the
2966 * plane damage, since the planes haven't actually been calculated
2967 * yet: instead try to figure it out directly. KMS cursor planes are
2968 * pretty unique here, in that they lie partway between a Weston plane
2969 * (direct scanout) and a renderer. */
2970 if (ev != output->cursor_view ||
2971 pixman_region32_not_empty(&ev->surface->damage)) {
2972 output->current_cursor++;
2973 output->current_cursor =
2974 output->current_cursor %
2975 ARRAY_LENGTH(output->gbm_cursor_fb);
2976 needs_update = true;
2977 }
2978
2979 output->cursor_view = ev;
2980 weston_view_to_global_float(ev, 0, 0, &x, &y);
2981 plane->base.x = x;
2982 plane->base.y = y;
2983
2984 plane_state->fb =
2985 drm_fb_ref(output->gbm_cursor_fb[output->current_cursor]);
2986 plane_state->output = output;
2987 plane_state->src_x = 0;
2988 plane_state->src_y = 0;
2989 plane_state->src_w = b->cursor_width << 16;
2990 plane_state->src_h = b->cursor_height << 16;
2991 plane_state->dest_x = (x - output->base.x) * output->base.current_scale;
2992 plane_state->dest_y = (y - output->base.y) * output->base.current_scale;
2993 plane_state->dest_w = b->cursor_width;
2994 plane_state->dest_h = b->cursor_height;
2995
2996 if (needs_update)
2997 cursor_bo_update(b, plane_state->fb->bo, ev);
2998
2999 return &plane->base;
3000}
3001
3002static void
3003drm_output_set_cursor(struct drm_output_state *output_state)
3004{
3005 struct drm_output *output = output_state->output;
3006 struct drm_backend *b = to_drm_backend(output->base.compositor);
3007 struct drm_plane *plane = output->cursor_plane;
3008 struct drm_plane_state *state;
3009 EGLint handle;
3010 struct gbm_bo *bo;
3011
3012 if (!plane)
3013 return;
3014
3015 state = drm_output_state_get_existing_plane(output_state, plane);
3016 if (!state)
3017 return;
3018
3019 if (!state->fb) {
3020 pixman_region32_fini(&plane->base.damage);
3021 pixman_region32_init(&plane->base.damage);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003022 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg5626d342012-08-03 11:50:05 -04003023 return;
3024 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05003025
Daniel Stone2ba17f42015-05-19 20:02:41 +01003026 assert(state->fb == output->gbm_cursor_fb[output->current_cursor]);
3027 assert(!plane->state_cur->output || plane->state_cur->output == output);
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05003028
Daniel Stone2ba17f42015-05-19 20:02:41 +01003029 if (plane->state_cur->fb != state->fb) {
3030 bo = state->fb->bo;
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04003031 handle = gbm_bo_get_handle(bo).s32;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003032 if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle,
Daniel Stone2ba17f42015-05-19 20:02:41 +01003033 b->cursor_width, b->cursor_height)) {
Pekka Paalanenae29da22012-08-06 14:57:05 +03003034 weston_log("failed to set cursor: %m\n");
Daniel Stone2ba17f42015-05-19 20:02:41 +01003035 goto err;
Rob Clarkab5b1e32012-08-09 13:24:45 -05003036 }
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003037 }
3038
Daniel Stone2ba17f42015-05-19 20:02:41 +01003039 pixman_region32_fini(&plane->base.damage);
3040 pixman_region32_init(&plane->base.damage);
Pekka Paalanen7eaed402015-11-27 14:20:58 +02003041
Daniel Stone2ba17f42015-05-19 20:02:41 +01003042 if (drmModeMoveCursor(b->drm.fd, output->crtc_id,
3043 state->dest_x, state->dest_y)) {
Daniel Stonea7cba1d2017-04-04 17:54:21 +01003044 weston_log("failed to move cursor: %m\n");
Daniel Stone2ba17f42015-05-19 20:02:41 +01003045 goto err;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003046 }
Daniel Stone2ba17f42015-05-19 20:02:41 +01003047
3048 return;
3049
3050err:
3051 b->cursors_are_broken = 1;
3052 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05003053}
3054
Jesse Barnes58ef3792012-02-23 09:45:49 -05003055static void
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003056drm_assign_planes(struct weston_output *output_base, void *repaint_data)
Jesse Barnes58ef3792012-02-23 09:45:49 -05003057{
Armin Krezović545dba62016-08-05 15:54:18 +02003058 struct drm_backend *b = to_drm_backend(output_base->compositor);
Daniel Stone7b2ddac2016-11-11 19:11:49 +00003059 struct drm_pending_state *pending_state = repaint_data;
Armin Krezović545dba62016-08-05 15:54:18 +02003060 struct drm_output *output = to_drm_output(output_base);
Daniel Stone7b2ddac2016-11-11 19:11:49 +00003061 struct drm_output_state *state;
Daniel Stone2ba17f42015-05-19 20:02:41 +01003062 struct drm_plane_state *plane_state;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003063 struct weston_view *ev, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -05003064 pixman_region32_t overlap, surface_overlap;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003065 struct weston_plane *primary, *next_plane;
Matt Hoosierdf573032017-08-24 09:24:20 -05003066 bool picked_scanout = false;
Jesse Barnes58ef3792012-02-23 09:45:49 -05003067
Daniel Stone7b2ddac2016-11-11 19:11:49 +00003068 assert(!output->state_last);
3069 state = drm_output_state_duplicate(output->state_cur,
3070 pending_state,
3071 DRM_OUTPUT_STATE_CLEAR_PLANES);
3072
Jesse Barnes58ef3792012-02-23 09:45:49 -05003073 /*
3074 * Find a surface for each sprite in the output using some heuristics:
3075 * 1) size
3076 * 2) frequency of update
3077 * 3) opacity (though some hw might support alpha blending)
3078 * 4) clipping (this can be fixed with color keys)
3079 *
3080 * The idea is to save on blitting since this should save power.
3081 * If we can get a large video surface on the sprite for example,
3082 * the main display surface may not need to update at all, and
3083 * the client buffer can be used directly for the sprite surface
3084 * as we do for flipping full screen surfaces.
3085 */
3086 pixman_region32_init(&overlap);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003087 primary = &output_base->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003088
Giulio Camuffo954f1832014-10-11 18:27:30 +03003089 wl_list_for_each_safe(ev, next, &output_base->compositor->view_list, link) {
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02003090 struct weston_surface *es = ev->surface;
3091
3092 /* Test whether this buffer can ever go into a plane:
3093 * non-shm, or small enough to be a cursor.
3094 *
3095 * Also, keep a reference when using the pixman renderer.
3096 * That makes it possible to do a seamless switch to the GL
3097 * renderer and since the pixman renderer keeps a reference
3098 * to the buffer anyway, there is no side effects.
Pekka Paalanenccfeae22012-12-04 15:58:14 +02003099 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003100 if (b->use_pixman ||
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02003101 (es->buffer_ref.buffer &&
3102 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
Derek Foreman87430472015-10-15 10:24:48 -05003103 (ev->surface->width <= b->cursor_width &&
3104 ev->surface->height <= b->cursor_height))))
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05003105 es->keep_buffer = true;
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02003106 else
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05003107 es->keep_buffer = false;
Pekka Paalanenccfeae22012-12-04 15:58:14 +02003108
Jesse Barnes58ef3792012-02-23 09:45:49 -05003109 pixman_region32_init(&surface_overlap);
3110 pixman_region32_intersect(&surface_overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003111 &ev->transform.boundingbox);
Jesse Barnes58ef3792012-02-23 09:45:49 -05003112
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003113 next_plane = NULL;
Matt Hoosierdf573032017-08-24 09:24:20 -05003114 if (pixman_region32_not_empty(&surface_overlap) || picked_scanout)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003115 next_plane = primary;
3116 if (next_plane == NULL)
Daniel Stone7b2ddac2016-11-11 19:11:49 +00003117 next_plane = drm_output_prepare_cursor_view(state, ev);
Matt Hoosierdf573032017-08-24 09:24:20 -05003118
3119 /* If a higher-stacked view already got assigned to scanout, it's incorrect to
3120 * assign a subsequent (lower-stacked) view to scanout.
3121 */
3122 if (next_plane == NULL) {
Daniel Stone7b2ddac2016-11-11 19:11:49 +00003123 next_plane = drm_output_prepare_scanout_view(state, ev);
Matt Hoosierdf573032017-08-24 09:24:20 -05003124 if (next_plane)
3125 picked_scanout = true;
3126 }
3127
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003128 if (next_plane == NULL)
Daniel Stone7b2ddac2016-11-11 19:11:49 +00003129 next_plane = drm_output_prepare_overlay_view(state, ev);
Matt Hoosierdf573032017-08-24 09:24:20 -05003130
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003131 if (next_plane == NULL)
3132 next_plane = primary;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02003133
Jason Ekstranda7af7042013-10-12 22:38:11 -05003134 weston_view_move_to_plane(ev, next_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02003135
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003136 if (next_plane == primary)
Jesse Barnes58ef3792012-02-23 09:45:49 -05003137 pixman_region32_union(&overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003138 &ev->transform.boundingbox);
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04003139
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02003140 if (next_plane == primary ||
Daniel Stone2ba17f42015-05-19 20:02:41 +01003141 (output->cursor_plane &&
3142 next_plane == &output->cursor_plane->base)) {
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02003143 /* cursor plane involves a copy */
3144 ev->psf_flags = 0;
3145 } else {
3146 /* All other planes are a direct scanout of a
3147 * single client buffer.
3148 */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02003149 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02003150 }
3151
Jesse Barnes58ef3792012-02-23 09:45:49 -05003152 pixman_region32_fini(&surface_overlap);
3153 }
3154 pixman_region32_fini(&overlap);
Daniel Stone2ba17f42015-05-19 20:02:41 +01003155
3156 /* We rely on output->cursor_view being both an accurate reflection of
3157 * the cursor plane's state, but also being maintained across repaints
3158 * to avoid unnecessary damage uploads, per the comment in
3159 * drm_output_prepare_cursor_view. In the event that we go from having
3160 * a cursor view to not having a cursor view, we need to clear it. */
3161 if (output->cursor_view) {
3162 plane_state =
3163 drm_output_state_get_existing_plane(state,
3164 output->cursor_plane);
3165 if (!plane_state || !plane_state->fb)
3166 output->cursor_view = NULL;
3167 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05003168}
3169
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003170/**
3171 * Find the closest-matching mode for a given target
3172 *
3173 * Given a target mode, find the most suitable mode amongst the output's
3174 * current mode list to use, preferring the current mode if possible, to
3175 * avoid an expensive mode switch.
3176 *
3177 * @param output DRM output
3178 * @param target_mode Mode to attempt to match
3179 * @returns Pointer to a mode from the output's mode list
3180 */
Alex Wub7b8bda2012-04-17 17:20:48 +08003181static struct drm_mode *
3182choose_mode (struct drm_output *output, struct weston_mode *target_mode)
3183{
3184 struct drm_mode *tmp_mode = NULL, *mode;
3185
Hardeningff39efa2013-09-18 23:56:35 +02003186 if (output->base.current_mode->width == target_mode->width &&
3187 output->base.current_mode->height == target_mode->height &&
3188 (output->base.current_mode->refresh == target_mode->refresh ||
Alex Wub7b8bda2012-04-17 17:20:48 +08003189 target_mode->refresh == 0))
Daniel Stonecb04cc42016-11-16 11:51:27 +00003190 return to_drm_mode(output->base.current_mode);
Alex Wub7b8bda2012-04-17 17:20:48 +08003191
3192 wl_list_for_each(mode, &output->base.mode_list, base.link) {
3193 if (mode->mode_info.hdisplay == target_mode->width &&
3194 mode->mode_info.vdisplay == target_mode->height) {
Mario Kleiner872797c2015-06-21 21:25:09 +02003195 if (mode->base.refresh == target_mode->refresh ||
3196 target_mode->refresh == 0) {
Alex Wub7b8bda2012-04-17 17:20:48 +08003197 return mode;
Daniel Stonef556ebe2015-05-21 08:28:58 +01003198 } else if (!tmp_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08003199 tmp_mode = mode;
3200 }
3201 }
3202
3203 return tmp_mode;
3204}
3205
3206static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003207drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00003208static void
3209drm_output_fini_egl(struct drm_output *output);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003210static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003211drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00003212static void
3213drm_output_fini_pixman(struct drm_output *output);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02003214
3215static int
Alex Wub7b8bda2012-04-17 17:20:48 +08003216drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
3217{
Daniel Stone02d487a2017-10-07 14:01:45 +01003218 struct drm_output *output = to_drm_output(output_base);
3219 struct drm_backend *b = to_drm_backend(output_base->compositor);
3220 struct drm_mode *drm_mode = choose_mode(output, mode);
Alex Wub7b8bda2012-04-17 17:20:48 +08003221
3222 if (!drm_mode) {
Daniel Stone02d487a2017-10-07 14:01:45 +01003223 weston_log("%s: invalid resolution %dx%d\n",
3224 output_base->name, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +08003225 return -1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02003226 }
3227
Hardeningff39efa2013-09-18 23:56:35 +02003228 if (&drm_mode->base == output->base.current_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08003229 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08003230
Hardeningff39efa2013-09-18 23:56:35 +02003231 output->base.current_mode->flags = 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08003232
Hardeningff39efa2013-09-18 23:56:35 +02003233 output->base.current_mode = &drm_mode->base;
3234 output->base.current_mode->flags =
Alex Wub7b8bda2012-04-17 17:20:48 +08003235 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
3236
Daniel Stonef30a18c2017-04-04 17:54:31 +01003237 /* XXX: This drops our current buffer too early, before we've started
3238 * displaying it. Ideally this should be much more atomic and
3239 * integrated with a full repaint cycle, rather than doing a
3240 * sledgehammer modeswitch first, and only later showing new
3241 * content.
3242 */
Daniel Stone6020f472018-02-05 15:46:20 +00003243 b->state_invalid = true;
Alex Wub7b8bda2012-04-17 17:20:48 +08003244
Giulio Camuffo954f1832014-10-11 18:27:30 +03003245 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003246 drm_output_fini_pixman(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003247 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003248 weston_log("failed to init output pixman state with "
3249 "new mode\n");
3250 return -1;
3251 }
3252 } else {
Daniel Stone3e661f72016-11-04 17:24:06 +00003253 drm_output_fini_egl(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003254 if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003255 weston_log("failed to init output egl state with "
3256 "new mode");
3257 return -1;
3258 }
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02003259 }
3260
Alex Wub7b8bda2012-04-17 17:20:48 +08003261 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08003262}
3263
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003264static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003265on_drm_input(int fd, uint32_t mask, void *data)
3266{
Daniel Stone598ee9d2016-11-16 11:55:20 +00003267#ifdef HAVE_DRM_ATOMIC
3268 struct drm_backend *b = data;
3269#endif
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003270 drmEventContext evctx;
3271
3272 memset(&evctx, 0, sizeof evctx);
Daniel Stone598ee9d2016-11-16 11:55:20 +00003273#ifndef HAVE_DRM_ATOMIC
Emil Velikov863e66b2017-04-04 18:07:34 +01003274 evctx.version = 2;
Daniel Stone598ee9d2016-11-16 11:55:20 +00003275#else
3276 evctx.version = 3;
3277 if (b->atomic_modeset)
3278 evctx.page_flip_handler2 = atomic_flip_handler;
3279 else
3280#endif
3281 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -05003282 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003283 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003284
3285 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003286}
3287
3288static int
Daniel Stoneefa504f2016-12-19 16:48:20 +00003289init_kms_caps(struct drm_backend *b)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003290{
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03003291 uint64_t cap;
Daniel Stoneefa504f2016-12-19 16:48:20 +00003292 int ret;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003293 clockid_t clk_id;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04003294
Daniel Stoneefa504f2016-12-19 16:48:20 +00003295 weston_log("using %s\n", b->drm.filename);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003296
Daniel Stoneefa504f2016-12-19 16:48:20 +00003297 ret = drmGetCap(b->drm.fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03003298 if (ret == 0 && cap == 1)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003299 clk_id = CLOCK_MONOTONIC;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03003300 else
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003301 clk_id = CLOCK_REALTIME;
3302
Giulio Camuffo954f1832014-10-11 18:27:30 +03003303 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003304 weston_log("Error: failed to set presentation clock %d.\n",
3305 clk_id);
3306 return -1;
3307 }
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +02003308
Daniel Stoneefa504f2016-12-19 16:48:20 +00003309 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_WIDTH, &cap);
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03003310 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03003311 b->cursor_width = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03003312 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03003313 b->cursor_width = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03003314
Daniel Stoneefa504f2016-12-19 16:48:20 +00003315 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_HEIGHT, &cap);
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03003316 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03003317 b->cursor_height = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03003318 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03003319 b->cursor_height = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03003320
Daniel Stonebe1090b2017-09-06 17:29:57 +01003321 if (!getenv("WESTON_DISABLE_UNIVERSAL_PLANES")) {
3322 ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
3323 b->universal_planes = (ret == 0);
3324 }
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01003325 weston_log("DRM: %s universal planes\n",
3326 b->universal_planes ? "supports" : "does not support");
3327
Pekka Paalanencd011a62016-11-15 22:07:49 +00003328#ifdef HAVE_DRM_ATOMIC
3329 if (b->universal_planes && !getenv("WESTON_DISABLE_ATOMIC")) {
Daniel Stone598ee9d2016-11-16 11:55:20 +00003330 ret = drmGetCap(b->drm.fd, DRM_CAP_CRTC_IN_VBLANK_EVENT, &cap);
3331 if (ret != 0)
3332 cap = 0;
Pekka Paalanencd011a62016-11-15 22:07:49 +00003333 ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_ATOMIC, 1);
Daniel Stone598ee9d2016-11-16 11:55:20 +00003334 b->atomic_modeset = ((ret == 0) && (cap == 1));
Pekka Paalanencd011a62016-11-15 22:07:49 +00003335 }
3336#endif
3337 weston_log("DRM: %s atomic modesetting\n",
3338 b->atomic_modeset ? "supports" : "does not support");
3339
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003340 return 0;
3341}
3342
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003343static struct gbm_device *
3344create_gbm_device(int fd)
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003345{
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003346 struct gbm_device *gbm;
Alexandru DAMIANbe0ac5b2013-10-02 17:51:05 +01003347
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003348 gl_renderer = weston_load_module("gl-renderer.so",
3349 "gl_renderer_interface");
3350 if (!gl_renderer)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003351 return NULL;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003352
3353 /* GBM will load a dri driver, but even though they need symbols from
3354 * libglapi, in some version of Mesa they are not linked to it. Since
3355 * only the gl-renderer module links to it, the call above won't make
3356 * these symbols globally available, and loading the DRI driver fails.
3357 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
3358 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
3359
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003360 gbm = gbm_create_device(fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003361
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003362 return gbm;
3363}
3364
Bryce Harringtonc056a982015-05-19 15:25:18 -07003365/* When initializing EGL, if the preferred buffer format isn't available
Bryce Harringtonb9939982016-04-15 20:28:26 -07003366 * we may be able to substitute an ARGB format for an XRGB one.
Derek Foremanc4cfe852015-05-15 12:12:40 -05003367 *
3368 * This returns 0 if substitution isn't possible, but 0 might be a
3369 * legitimate format for other EGL platforms, so the caller is
3370 * responsible for checking for 0 before calling gl_renderer->create().
3371 *
3372 * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689
3373 * but it's entirely possible we'll see this again on other implementations.
3374 */
3375static int
3376fallback_format_for(uint32_t format)
3377{
3378 switch (format) {
3379 case GBM_FORMAT_XRGB8888:
3380 return GBM_FORMAT_ARGB8888;
3381 case GBM_FORMAT_XRGB2101010:
3382 return GBM_FORMAT_ARGB2101010;
3383 default:
3384 return 0;
3385 }
3386}
3387
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003388static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003389drm_backend_create_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003390{
Derek Foreman6d556372015-11-04 14:47:33 -06003391 EGLint format[3] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01003392 b->gbm_format,
3393 fallback_format_for(b->gbm_format),
Derek Foreman6d556372015-11-04 14:47:33 -06003394 0,
Derek Foremanc4cfe852015-05-15 12:12:40 -05003395 };
Derek Foreman6d556372015-11-04 14:47:33 -06003396 int n_formats = 2;
John Kåre Alsakeref591aa2013-03-02 12:27:39 +01003397
Derek Foremanc4cfe852015-05-15 12:12:40 -05003398 if (format[1])
Derek Foreman6d556372015-11-04 14:47:33 -06003399 n_formats = 3;
Miguel A. Vicodddc6702016-05-18 17:41:07 +02003400 if (gl_renderer->display_create(b->compositor,
3401 EGL_PLATFORM_GBM_KHR,
3402 (void *)b->gbm,
Miguel A. Vico41700e32016-05-18 17:47:59 +02003403 NULL,
Miguel A. Vicodddc6702016-05-18 17:41:07 +02003404 gl_renderer->opaque_attribs,
3405 format,
3406 n_formats) < 0) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003407 return -1;
3408 }
3409
3410 return 0;
3411}
3412
3413static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003414init_egl(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003415{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003416 b->gbm = create_gbm_device(b->drm.fd);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003417
Giulio Camuffo954f1832014-10-11 18:27:30 +03003418 if (!b->gbm)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003419 return -1;
3420
Giulio Camuffo954f1832014-10-11 18:27:30 +03003421 if (drm_backend_create_gl_renderer(b) < 0) {
3422 gbm_device_destroy(b->gbm);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04003423 return -1;
3424 }
3425
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003426 return 0;
3427}
3428
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003429static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003430init_pixman(struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003431{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003432 return pixman_renderer_init(b->compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003433}
3434
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003435/**
Pekka Paalanenec272712014-06-05 11:22:25 +03003436 * Create a drm_plane for a hardware plane
3437 *
3438 * Creates one drm_plane structure for a hardware plane, and initialises its
3439 * properties and formats.
3440 *
Daniel Stone2ba17f42015-05-19 20:02:41 +01003441 * In the absence of universal plane support, where KMS does not explicitly
3442 * expose the primary and cursor planes to userspace, this may also create
3443 * an 'internal' plane for internal management.
3444 *
Pekka Paalanenec272712014-06-05 11:22:25 +03003445 * This function does not add the plane to the list of usable planes in Weston
3446 * itself; the caller is responsible for this.
3447 *
3448 * Call drm_plane_destroy to clean up the plane.
3449 *
Daniel Stone2ba17f42015-05-19 20:02:41 +01003450 * @sa drm_output_find_special_plane
Pekka Paalanenec272712014-06-05 11:22:25 +03003451 * @param b DRM compositor backend
Daniel Stone2ba17f42015-05-19 20:02:41 +01003452 * @param kplane DRM plane to create, or NULL if creating internal plane
3453 * @param output Output to create internal plane for, or NULL
3454 * @param type Type to use when creating internal plane, or invalid
3455 * @param format Format to use for internal planes, or 0
Pekka Paalanenec272712014-06-05 11:22:25 +03003456 */
3457static struct drm_plane *
Daniel Stone2ba17f42015-05-19 20:02:41 +01003458drm_plane_create(struct drm_backend *b, const drmModePlane *kplane,
3459 struct drm_output *output, enum wdrm_plane_type type,
3460 uint32_t format)
Pekka Paalanenec272712014-06-05 11:22:25 +03003461{
3462 struct drm_plane *plane;
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01003463 drmModeObjectProperties *props;
Daniel Stone2ba17f42015-05-19 20:02:41 +01003464 int num_formats = (kplane) ? kplane->count_formats : 1;
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01003465
Daniel Stone2ba17f42015-05-19 20:02:41 +01003466 plane = zalloc(sizeof(*plane) +
3467 (sizeof(uint32_t) * num_formats));
Pekka Paalanenec272712014-06-05 11:22:25 +03003468 if (!plane) {
3469 weston_log("%s: out of memory\n", __func__);
3470 return NULL;
3471 }
3472
3473 plane->backend = b;
Daniel Stonebc15f682016-11-14 16:57:01 +00003474 plane->state_cur = drm_plane_state_alloc(NULL, plane);
3475 plane->state_cur->complete = true;
Pekka Paalanenec272712014-06-05 11:22:25 +03003476
Daniel Stone2ba17f42015-05-19 20:02:41 +01003477 if (kplane) {
3478 plane->possible_crtcs = kplane->possible_crtcs;
3479 plane->plane_id = kplane->plane_id;
3480 plane->count_formats = kplane->count_formats;
3481 memcpy(plane->formats, kplane->formats,
3482 kplane->count_formats * sizeof(kplane->formats[0]));
3483
3484 props = drmModeObjectGetProperties(b->drm.fd, kplane->plane_id,
3485 DRM_MODE_OBJECT_PLANE);
3486 if (!props) {
3487 weston_log("couldn't get plane properties\n");
3488 goto err;
3489 }
3490 drm_property_info_populate(b, plane_props, plane->props,
3491 WDRM_PLANE__COUNT, props);
3492 plane->type =
3493 drm_property_get_value(&plane->props[WDRM_PLANE_TYPE],
3494 props,
3495 WDRM_PLANE_TYPE__COUNT);
3496 drmModeFreeObjectProperties(props);
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01003497 }
Daniel Stone2ba17f42015-05-19 20:02:41 +01003498 else {
3499 plane->possible_crtcs = (1 << output->pipe);
3500 plane->plane_id = 0;
3501 plane->count_formats = 1;
3502 plane->formats[0] = format;
3503 plane->type = type;
3504 }
3505
3506 if (plane->type == WDRM_PLANE_TYPE__COUNT)
3507 goto err_props;
3508
3509 /* With universal planes, everything is a DRM plane; without
3510 * universal planes, the only DRM planes are overlay planes.
3511 * Everything else is a fake plane. */
3512 if (b->universal_planes) {
3513 assert(kplane);
3514 } else {
3515 if (kplane)
3516 assert(plane->type == WDRM_PLANE_TYPE_OVERLAY);
3517 else
3518 assert(plane->type != WDRM_PLANE_TYPE_OVERLAY &&
3519 output);
3520 }
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01003521
Pekka Paalanenec272712014-06-05 11:22:25 +03003522 weston_plane_init(&plane->base, b->compositor, 0, 0);
Daniel Stone085d2b92015-05-21 00:00:57 +01003523 wl_list_insert(&b->plane_list, &plane->link);
Pekka Paalanenec272712014-06-05 11:22:25 +03003524
3525 return plane;
Daniel Stone2ba17f42015-05-19 20:02:41 +01003526
3527err_props:
3528 drm_property_info_free(plane->props, WDRM_PLANE__COUNT);
3529err:
3530 drm_plane_state_free(plane->state_cur, true);
3531 free(plane);
3532 return NULL;
3533}
3534
3535/**
3536 * Find, or create, a special-purpose plane
3537 *
3538 * Primary and cursor planes are a special case, in that before universal
3539 * planes, they are driven by non-plane API calls. Without universal plane
3540 * support, the only way to configure a primary plane is via drmModeSetCrtc,
3541 * and the only way to configure a cursor plane is drmModeSetCursor2.
3542 *
3543 * Although they may actually be regular planes in the hardware, without
3544 * universal plane support, these planes are not actually exposed to
3545 * userspace in the regular plane list.
3546 *
3547 * However, for ease of internal tracking, we want to manage all planes
3548 * through the same drm_plane structures. Therefore, when we are running
3549 * without universal plane support, we create fake drm_plane structures
3550 * to track these planes.
3551 *
3552 * @param b DRM backend
3553 * @param output Output to use for plane
3554 * @param type Type of plane
3555 */
3556static struct drm_plane *
3557drm_output_find_special_plane(struct drm_backend *b, struct drm_output *output,
3558 enum wdrm_plane_type type)
3559{
3560 struct drm_plane *plane;
3561
3562 if (!b->universal_planes) {
3563 uint32_t format;
3564
3565 switch (type) {
3566 case WDRM_PLANE_TYPE_CURSOR:
3567 format = GBM_FORMAT_ARGB8888;
3568 break;
Daniel Stonee2e80132018-01-16 15:37:33 +00003569 case WDRM_PLANE_TYPE_PRIMARY:
3570 /* We don't know what formats the primary plane supports
3571 * before universal planes, so we just assume that the
3572 * GBM format works; however, this isn't set until after
3573 * the output is created. */
3574 format = 0;
3575 break;
Daniel Stone2ba17f42015-05-19 20:02:41 +01003576 default:
3577 assert(!"invalid type in drm_output_find_special_plane");
3578 break;
3579 }
3580
3581 return drm_plane_create(b, NULL, output, type, format);
3582 }
3583
3584 wl_list_for_each(plane, &b->plane_list, link) {
3585 struct drm_output *tmp;
3586 bool found_elsewhere = false;
3587
3588 if (plane->type != type)
3589 continue;
3590 if (!drm_plane_is_available(plane, output))
3591 continue;
3592
3593 /* On some platforms, primary/cursor planes can roam
3594 * between different CRTCs, so make sure we don't claim the
3595 * same plane for two outputs. */
3596 wl_list_for_each(tmp, &b->compositor->pending_output_list,
3597 base.link) {
Daniel Stonee2e80132018-01-16 15:37:33 +00003598 if (tmp->cursor_plane == plane ||
3599 tmp->scanout_plane == plane) {
Daniel Stone2ba17f42015-05-19 20:02:41 +01003600 found_elsewhere = true;
3601 break;
3602 }
3603 }
3604 wl_list_for_each(tmp, &b->compositor->output_list,
3605 base.link) {
Daniel Stonee2e80132018-01-16 15:37:33 +00003606 if (tmp->cursor_plane == plane ||
3607 tmp->scanout_plane == plane) {
Daniel Stone2ba17f42015-05-19 20:02:41 +01003608 found_elsewhere = true;
3609 break;
3610 }
3611 }
3612
3613 if (found_elsewhere)
3614 continue;
3615
3616 plane->possible_crtcs = (1 << output->pipe);
3617 return plane;
3618 }
3619
3620 return NULL;
Pekka Paalanenec272712014-06-05 11:22:25 +03003621}
3622
3623/**
3624 * Destroy one DRM plane
3625 *
3626 * Destroy a DRM plane, removing it from screen and releasing its retained
3627 * buffers in the process. The counterpart to drm_plane_create.
3628 *
3629 * @param plane Plane to deallocate (will be freed)
3630 */
3631static void
3632drm_plane_destroy(struct drm_plane *plane)
3633{
Daniel Stone2ba17f42015-05-19 20:02:41 +01003634 if (plane->type == WDRM_PLANE_TYPE_OVERLAY)
3635 drmModeSetPlane(plane->backend->drm.fd, plane->plane_id,
3636 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
Daniel Stonebc15f682016-11-14 16:57:01 +00003637 drm_plane_state_free(plane->state_cur, true);
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01003638 drm_property_info_free(plane->props, WDRM_PLANE__COUNT);
Pekka Paalanenec272712014-06-05 11:22:25 +03003639 weston_plane_release(&plane->base);
3640 wl_list_remove(&plane->link);
3641 free(plane);
3642}
3643
3644/**
3645 * Initialise sprites (overlay planes)
3646 *
3647 * Walk the list of provided DRM planes, and add overlay planes.
3648 *
3649 * Call destroy_sprites to free these planes.
3650 *
3651 * @param b DRM compositor backend
3652 */
3653static void
3654create_sprites(struct drm_backend *b)
3655{
3656 drmModePlaneRes *kplane_res;
3657 drmModePlane *kplane;
3658 struct drm_plane *drm_plane;
3659 uint32_t i;
Pekka Paalanenec272712014-06-05 11:22:25 +03003660 kplane_res = drmModeGetPlaneResources(b->drm.fd);
3661 if (!kplane_res) {
3662 weston_log("failed to get plane resources: %s\n",
3663 strerror(errno));
3664 return;
3665 }
3666
3667 for (i = 0; i < kplane_res->count_planes; i++) {
3668 kplane = drmModeGetPlane(b->drm.fd, kplane_res->planes[i]);
3669 if (!kplane)
3670 continue;
3671
Daniel Stone2ba17f42015-05-19 20:02:41 +01003672 drm_plane = drm_plane_create(b, kplane, NULL,
3673 WDRM_PLANE_TYPE__COUNT, 0);
Pekka Paalanenec272712014-06-05 11:22:25 +03003674 drmModeFreePlane(kplane);
3675 if (!drm_plane)
3676 continue;
3677
Daniel Stone085d2b92015-05-21 00:00:57 +01003678 if (drm_plane->type == WDRM_PLANE_TYPE_OVERLAY)
3679 weston_compositor_stack_plane(b->compositor,
3680 &drm_plane->base,
3681 &b->compositor->primary_plane);
Pekka Paalanenec272712014-06-05 11:22:25 +03003682 }
3683
3684 drmModeFreePlaneResources(kplane_res);
3685}
3686
3687/**
3688 * Clean up sprites (overlay planes)
3689 *
3690 * The counterpart to create_sprites.
3691 *
3692 * @param b DRM compositor backend
3693 */
3694static void
3695destroy_sprites(struct drm_backend *b)
3696{
3697 struct drm_plane *plane, *next;
3698
Daniel Stone085d2b92015-05-21 00:00:57 +01003699 wl_list_for_each_safe(plane, next, &b->plane_list, link)
Pekka Paalanenec272712014-06-05 11:22:25 +03003700 drm_plane_destroy(plane);
3701}
3702
3703/**
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003704 * Add a mode to output's mode list
3705 *
3706 * Copy the supplied DRM mode into a Weston mode structure, and add it to the
3707 * output's mode list.
3708 *
3709 * @param output DRM output to add mode to
3710 * @param info DRM mode structure to add
3711 * @returns Newly-allocated Weston/DRM mode structure
3712 */
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03003713static struct drm_mode *
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003714drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003715{
3716 struct drm_mode *mode;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04003717 uint64_t refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003718
3719 mode = malloc(sizeof *mode);
3720 if (mode == NULL)
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03003721 return NULL;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003722
3723 mode->base.flags = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02003724 mode->base.width = info->hdisplay;
3725 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04003726
3727 /* Calculate higher precision (mHz) refresh rate */
3728 refresh = (info->clock * 1000000LL / info->htotal +
3729 info->vtotal / 2) / info->vtotal;
3730
3731 if (info->flags & DRM_MODE_FLAG_INTERLACE)
3732 refresh *= 2;
3733 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
3734 refresh /= 2;
3735 if (info->vscan > 1)
3736 refresh /= info->vscan;
3737
3738 mode->base.refresh = refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003739 mode->mode_info = *info;
Daniel Stoned5526cb2016-11-16 10:54:10 +00003740 mode->blob_id = 0;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04003741
3742 if (info->type & DRM_MODE_TYPE_PREFERRED)
3743 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
3744
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003745 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
3746
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03003747 return mode;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003748}
3749
Daniel Stoned5526cb2016-11-16 10:54:10 +00003750/**
3751 * Destroys a mode, and removes it from the list.
3752 */
3753static void
3754drm_output_destroy_mode(struct drm_backend *backend, struct drm_mode *mode)
3755{
3756 if (mode->blob_id)
3757 drmModeDestroyPropertyBlob(backend->drm.fd, mode->blob_id);
3758 wl_list_remove(&mode->base.link);
3759 free(mode);
3760}
3761
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003762static int
3763drm_subpixel_to_wayland(int drm_value)
3764{
3765 switch (drm_value) {
3766 default:
3767 case DRM_MODE_SUBPIXEL_UNKNOWN:
3768 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
3769 case DRM_MODE_SUBPIXEL_NONE:
3770 return WL_OUTPUT_SUBPIXEL_NONE;
3771 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
3772 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
3773 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
3774 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
3775 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
3776 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
3777 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
3778 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
3779 }
3780}
3781
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003782/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003783static uint32_t
3784drm_get_backlight(struct drm_output *output)
3785{
3786 long brightness, max_brightness, norm;
3787
3788 brightness = backlight_get_brightness(output->backlight);
3789 max_brightness = backlight_get_max_brightness(output->backlight);
3790
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003791 /* convert it on a scale of 0 to 255 */
3792 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003793
3794 return (uint32_t) norm;
3795}
3796
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003797/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003798static void
3799drm_set_backlight(struct weston_output *output_base, uint32_t value)
3800{
Armin Krezović545dba62016-08-05 15:54:18 +02003801 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003802 long max_brightness, new_brightness;
3803
3804 if (!output->backlight)
3805 return;
3806
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003807 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003808 return;
3809
3810 max_brightness = backlight_get_max_brightness(output->backlight);
3811
3812 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003813 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003814
3815 backlight_set_brightness(output->backlight, new_brightness);
3816}
3817
Daniel Stonea08512f2016-11-08 17:46:10 +00003818/**
3819 * Power output on or off
3820 *
3821 * The DPMS/power level of an output is used to switch it on or off. This
3822 * is DRM's hook for doing so, which can called either as part of repaint,
3823 * or independently of the repaint loop.
3824 *
3825 * If we are called as part of repaint, we simply set the relevant bit in
3826 * state and return.
3827 */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003828static void
3829drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
3830{
Armin Krezović545dba62016-08-05 15:54:18 +02003831 struct drm_output *output = to_drm_output(output_base);
Daniel Stonea08512f2016-11-08 17:46:10 +00003832 struct drm_backend *b = to_drm_backend(output_base->compositor);
3833 struct drm_pending_state *pending_state = b->repaint_data;
3834 struct drm_output_state *state;
Daniel Stone36609c72015-06-18 07:49:02 +01003835 int ret;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003836
Daniel Stonea08512f2016-11-08 17:46:10 +00003837 if (output->state_cur->dpms == level)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003838 return;
3839
Daniel Stonea08512f2016-11-08 17:46:10 +00003840 /* If we're being called during the repaint loop, then this is
3841 * simple: discard any previously-generated state, and create a new
3842 * state where we disable everything. When we come to flush, this
3843 * will be applied.
3844 *
3845 * However, we need to be careful: we can be called whilst another
3846 * output is in its repaint cycle (pending_state exists), but our
3847 * output still has an incomplete state application outstanding.
3848 * In that case, we need to wait until that completes. */
3849 if (pending_state && !output->state_last) {
3850 /* The repaint loop already sets DPMS on; we don't need to
3851 * explicitly set it on here, as it will already happen
3852 * whilst applying the repaint state. */
3853 if (level == WESTON_DPMS_ON)
3854 return;
3855
3856 state = drm_pending_state_get_output(pending_state, output);
3857 if (state)
3858 drm_output_state_free(state);
3859 state = drm_output_get_disable_state(pending_state, output);
Daniel Stone36609c72015-06-18 07:49:02 +01003860 return;
3861 }
3862
Daniel Stonea08512f2016-11-08 17:46:10 +00003863 /* As we throw everything away when disabling, just send us back through
3864 * a repaint cycle. */
3865 if (level == WESTON_DPMS_ON) {
3866 if (output->dpms_off_pending)
3867 output->dpms_off_pending = 0;
3868 weston_output_schedule_repaint(output_base);
3869 return;
3870 }
3871
3872 /* If we've already got a request in the pipeline, then we need to
3873 * park our DPMS request until that request has quiesced. */
3874 if (output->state_last) {
3875 output->dpms_off_pending = 1;
3876 return;
3877 }
3878
3879 pending_state = drm_pending_state_alloc(b);
3880 drm_output_get_disable_state(pending_state, output);
3881 ret = drm_pending_state_apply_sync(pending_state);
3882 if (ret != 0)
3883 weston_log("drm_set_dpms: couldn't disable output?\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003884}
3885
Pekka Paalanen3ce63622014-06-04 16:29:49 +03003886static const char * const connector_type_names[] = {
Pekka Paalanen89c49b32015-08-19 15:25:57 +03003887 [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
3888 [DRM_MODE_CONNECTOR_VGA] = "VGA",
3889 [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
3890 [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
3891 [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
3892 [DRM_MODE_CONNECTOR_Composite] = "Composite",
3893 [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
3894 [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
3895 [DRM_MODE_CONNECTOR_Component] = "Component",
3896 [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
3897 [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
3898 [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
3899 [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
3900 [DRM_MODE_CONNECTOR_TV] = "TV",
3901 [DRM_MODE_CONNECTOR_eDP] = "eDP",
Pekka Paalanenab81f152015-08-24 14:27:07 +03003902#ifdef DRM_MODE_CONNECTOR_DSI
Pekka Paalanen89c49b32015-08-19 15:25:57 +03003903 [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
3904 [DRM_MODE_CONNECTOR_DSI] = "DSI",
Pekka Paalanenab81f152015-08-24 14:27:07 +03003905#endif
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04003906};
3907
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03003908/** Create a name given a DRM connector
3909 *
3910 * \param con The DRM connector whose type and id form the name.
3911 * \return A newly allocate string, or NULL on error. Must be free()'d
3912 * after use.
3913 *
3914 * The name does not identify the DRM display device.
3915 */
Pekka Paalanen3ce63622014-06-04 16:29:49 +03003916static char *
3917make_connector_name(const drmModeConnector *con)
3918{
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03003919 char *name;
Pekka Paalanen89c49b32015-08-19 15:25:57 +03003920 const char *type_name = NULL;
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03003921 int ret;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03003922
3923 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
3924 type_name = connector_type_names[con->connector_type];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03003925
3926 if (!type_name)
3927 type_name = "UNNAMED";
3928
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03003929 ret = asprintf(&name, "%s-%d", type_name, con->connector_type_id);
3930 if (ret < 0)
3931 return NULL;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03003932
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03003933 return name;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03003934}
3935
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003936static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003937find_crtc_for_connector(struct drm_backend *b,
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003938 drmModeRes *resources, drmModeConnector *connector)
3939{
3940 drmModeEncoder *encoder;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003941 int i, j;
Daniel Stone75487c22017-01-16 14:33:38 +00003942 int ret = -1;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003943
3944 for (j = 0; j < connector->count_encoders; j++) {
Daniel Stone75487c22017-01-16 14:33:38 +00003945 uint32_t possible_crtcs, encoder_id, crtc_id;
3946
Giulio Camuffo954f1832014-10-11 18:27:30 +03003947 encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003948 if (encoder == NULL) {
3949 weston_log("Failed to get encoder.\n");
Daniel Stone75487c22017-01-16 14:33:38 +00003950 continue;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003951 }
Daniel Stone75487c22017-01-16 14:33:38 +00003952 encoder_id = encoder->encoder_id;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003953 possible_crtcs = encoder->possible_crtcs;
Daniel Stone75487c22017-01-16 14:33:38 +00003954 crtc_id = encoder->crtc_id;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003955 drmModeFreeEncoder(encoder);
3956
3957 for (i = 0; i < resources->count_crtcs; i++) {
Daniel Stone72c0e1b2017-02-09 13:49:15 +00003958 if (!(possible_crtcs & (1 << i)))
3959 continue;
3960
3961 if (drm_output_find_by_crtc(b, resources->crtcs[i]))
3962 continue;
3963
Daniel Stone75487c22017-01-16 14:33:38 +00003964 /* Try to preserve the existing
3965 * CRTC -> encoder -> connector routing; it makes
3966 * initialisation faster, and also since we have a
3967 * very dumb picking algorithm, may preserve a better
3968 * choice. */
3969 if (!connector->encoder_id ||
3970 (encoder_id == connector->encoder_id &&
3971 crtc_id == resources->crtcs[i]))
3972 return i;
3973
3974 ret = i;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003975 }
3976 }
3977
Daniel Stone75487c22017-01-16 14:33:38 +00003978 return ret;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003979}
3980
Daniel Stonee4256832017-04-04 17:54:27 +01003981static void drm_output_fini_cursor_egl(struct drm_output *output)
3982{
3983 unsigned int i;
3984
3985 for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) {
3986 drm_fb_unref(output->gbm_cursor_fb[i]);
3987 output->gbm_cursor_fb[i] = NULL;
3988 }
3989}
3990
3991static int
3992drm_output_init_cursor_egl(struct drm_output *output, struct drm_backend *b)
3993{
3994 unsigned int i;
3995
Daniel Stone2ba17f42015-05-19 20:02:41 +01003996 /* No point creating cursors if we don't have a plane for them. */
3997 if (!output->cursor_plane)
3998 return 0;
3999
Daniel Stonee4256832017-04-04 17:54:27 +01004000 for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) {
4001 struct gbm_bo *bo;
4002
4003 bo = gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
4004 GBM_FORMAT_ARGB8888,
4005 GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE);
4006 if (!bo)
4007 goto err;
4008
4009 output->gbm_cursor_fb[i] =
4010 drm_fb_get_from_bo(bo, b, GBM_FORMAT_ARGB8888,
4011 BUFFER_CURSOR);
4012 if (!output->gbm_cursor_fb[i]) {
4013 gbm_bo_destroy(bo);
4014 goto err;
4015 }
4016 }
4017
4018 return 0;
4019
4020err:
4021 weston_log("cursor buffers unavailable, using gl cursors\n");
4022 b->cursors_are_broken = 1;
4023 drm_output_fini_cursor_egl(output);
4024 return -1;
4025}
4026
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02004027/* Init output state that depends on gl or gbm */
4028static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03004029drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02004030{
Derek Foremanc4cfe852015-05-15 12:12:40 -05004031 EGLint format[2] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01004032 output->gbm_format,
4033 fallback_format_for(output->gbm_format),
Derek Foremanc4cfe852015-05-15 12:12:40 -05004034 };
Daniel Stonee4256832017-04-04 17:54:27 +01004035 int n_formats = 1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02004036
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01004037 output->gbm_surface = gbm_surface_create(b->gbm,
Hardeningff39efa2013-09-18 23:56:35 +02004038 output->base.current_mode->width,
4039 output->base.current_mode->height,
Derek Foremanc4cfe852015-05-15 12:12:40 -05004040 format[0],
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02004041 GBM_BO_USE_SCANOUT |
4042 GBM_BO_USE_RENDERING);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01004043 if (!output->gbm_surface) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02004044 weston_log("failed to create gbm surface\n");
4045 return -1;
4046 }
4047
Derek Foremanc4cfe852015-05-15 12:12:40 -05004048 if (format[1])
4049 n_formats = 2;
Miguel A. Vicoc095cde2016-05-18 17:43:00 +02004050 if (gl_renderer->output_window_create(&output->base,
4051 (EGLNativeWindowType)output->gbm_surface,
4052 output->gbm_surface,
4053 gl_renderer->opaque_attribs,
4054 format,
4055 n_formats) < 0) {
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02004056 weston_log("failed to create gl renderer output state\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01004057 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02004058 return -1;
4059 }
4060
Daniel Stonee4256832017-04-04 17:54:27 +01004061 drm_output_init_cursor_egl(output, b);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02004062
4063 return 0;
4064}
4065
Daniel Stone3e661f72016-11-04 17:24:06 +00004066static void
4067drm_output_fini_egl(struct drm_output *output)
4068{
Daniel Stonee2e80132018-01-16 15:37:33 +00004069 struct drm_backend *b = to_drm_backend(output->base.compositor);
4070
4071 /* Destroying the GBM surface will destroy all our GBM buffers,
4072 * regardless of refcount. Ensure we destroy them here. */
4073 if (!b->shutting_down &&
4074 output->scanout_plane->state_cur->fb &&
4075 output->scanout_plane->state_cur->fb->type == BUFFER_GBM_SURFACE) {
4076 drm_plane_state_free(output->scanout_plane->state_cur, true);
4077 output->scanout_plane->state_cur =
4078 drm_plane_state_alloc(NULL, output->scanout_plane);
4079 output->scanout_plane->state_cur->complete = true;
4080 }
4081
Daniel Stone3e661f72016-11-04 17:24:06 +00004082 gl_renderer->output_destroy(&output->base);
4083 gbm_surface_destroy(output->gbm_surface);
Daniel Stonee4256832017-04-04 17:54:27 +01004084 drm_output_fini_cursor_egl(output);
Daniel Stone3e661f72016-11-04 17:24:06 +00004085}
4086
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04004087static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03004088drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004089{
Hardeningff39efa2013-09-18 23:56:35 +02004090 int w = output->base.current_mode->width;
4091 int h = output->base.current_mode->height;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03004092 uint32_t format = output->gbm_format;
4093 uint32_t pixman_format;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004094 unsigned int i;
4095
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03004096 switch (format) {
4097 case GBM_FORMAT_XRGB8888:
4098 pixman_format = PIXMAN_x8r8g8b8;
4099 break;
4100 case GBM_FORMAT_RGB565:
4101 pixman_format = PIXMAN_r5g6b5;
4102 break;
4103 default:
4104 weston_log("Unsupported pixman format 0x%x\n", format);
4105 return -1;
4106 }
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004107
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03004108 /* FIXME error checking */
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004109 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03004110 output->dumb[i] = drm_fb_create_dumb(b, w, h, format);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004111 if (!output->dumb[i])
4112 goto err;
4113
4114 output->image[i] =
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03004115 pixman_image_create_bits(pixman_format, w, h,
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004116 output->dumb[i]->map,
4117 output->dumb[i]->stride);
4118 if (!output->image[i])
4119 goto err;
4120 }
4121
4122 if (pixman_renderer_output_create(&output->base) < 0)
4123 goto err;
4124
4125 pixman_region32_init_rect(&output->previous_damage,
Alexander Larsson0b135062013-05-28 16:23:36 +02004126 output->base.x, output->base.y, output->base.width, output->base.height);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004127
4128 return 0;
4129
4130err:
4131 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
4132 if (output->dumb[i])
Daniel Stone6e7a9612017-04-04 17:54:26 +01004133 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004134 if (output->image[i])
4135 pixman_image_unref(output->image[i]);
4136
4137 output->dumb[i] = NULL;
4138 output->image[i] = NULL;
4139 }
4140
4141 return -1;
4142}
4143
4144static void
4145drm_output_fini_pixman(struct drm_output *output)
4146{
Daniel Stonee2e80132018-01-16 15:37:33 +00004147 struct drm_backend *b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004148 unsigned int i;
4149
Daniel Stonee2e80132018-01-16 15:37:33 +00004150 /* Destroying the Pixman surface will destroy all our buffers,
4151 * regardless of refcount. Ensure we destroy them here. */
4152 if (!b->shutting_down &&
4153 output->scanout_plane->state_cur->fb &&
4154 output->scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB) {
4155 drm_plane_state_free(output->scanout_plane->state_cur, true);
4156 output->scanout_plane->state_cur =
4157 drm_plane_state_alloc(NULL, output->scanout_plane);
4158 output->scanout_plane->state_cur->complete = true;
4159 }
4160
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004161 pixman_renderer_output_destroy(&output->base);
4162 pixman_region32_fini(&output->previous_damage);
4163
4164 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004165 pixman_image_unref(output->image[i]);
Daniel Stone6e7a9612017-04-04 17:54:26 +01004166 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004167 output->dumb[i] = NULL;
4168 output->image[i] = NULL;
4169 }
4170}
4171
Richard Hughes2b2092a2013-04-24 14:58:02 +01004172static void
4173edid_parse_string(const uint8_t *data, char text[])
4174{
4175 int i;
4176 int replaced = 0;
4177
4178 /* this is always 12 bytes, but we can't guarantee it's null
4179 * terminated or not junk. */
4180 strncpy(text, (const char *) data, 12);
4181
Bryce Harrington9c7de162015-08-28 13:04:26 -07004182 /* guarantee our new string is null-terminated */
4183 text[12] = '\0';
4184
Richard Hughes2b2092a2013-04-24 14:58:02 +01004185 /* remove insane chars */
4186 for (i = 0; text[i] != '\0'; i++) {
4187 if (text[i] == '\n' ||
4188 text[i] == '\r') {
4189 text[i] = '\0';
4190 break;
4191 }
4192 }
4193
4194 /* ensure string is printable */
4195 for (i = 0; text[i] != '\0'; i++) {
4196 if (!isprint(text[i])) {
4197 text[i] = '-';
4198 replaced++;
4199 }
4200 }
4201
4202 /* if the string is random junk, ignore the string */
4203 if (replaced > 4)
4204 text[0] = '\0';
4205}
4206
4207#define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
4208#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
4209#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
4210#define EDID_OFFSET_DATA_BLOCKS 0x36
4211#define EDID_OFFSET_LAST_BLOCK 0x6c
4212#define EDID_OFFSET_PNPID 0x08
4213#define EDID_OFFSET_SERIAL 0x0c
4214
4215static int
4216edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
4217{
4218 int i;
4219 uint32_t serial_number;
4220
4221 /* check header */
4222 if (length < 128)
4223 return -1;
4224 if (data[0] != 0x00 || data[1] != 0xff)
4225 return -1;
4226
4227 /* decode the PNP ID from three 5 bit words packed into 2 bytes
4228 * /--08--\/--09--\
4229 * 7654321076543210
4230 * |\---/\---/\---/
4231 * R C1 C2 C3 */
4232 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
4233 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
4234 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
4235 edid->pnp_id[3] = '\0';
4236
4237 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
4238 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
4239 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
4240 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
4241 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
4242 if (serial_number > 0)
4243 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
4244
4245 /* parse EDID data */
4246 for (i = EDID_OFFSET_DATA_BLOCKS;
4247 i <= EDID_OFFSET_LAST_BLOCK;
4248 i += 18) {
4249 /* ignore pixel clock data */
4250 if (data[i] != 0)
4251 continue;
4252 if (data[i+2] != 0)
4253 continue;
4254
4255 /* any useful blocks? */
4256 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
4257 edid_parse_string(&data[i+5],
4258 edid->monitor_name);
4259 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
4260 edid_parse_string(&data[i+5],
4261 edid->serial_number);
4262 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
4263 edid_parse_string(&data[i+5],
4264 edid->eisa_id);
4265 }
4266 }
4267 return 0;
4268}
4269
Pekka Paalanen6f1866b2017-04-03 14:22:51 +03004270/** Parse monitor make, model and serial from EDID
4271 *
4272 * \param b The backend instance.
4273 * \param output The output whose \c drm_edid to fill in.
4274 * \param props The DRM connector properties to get the EDID from.
4275 * \param make[out] The monitor make (PNP ID).
4276 * \param model[out] The monitor model (name).
4277 * \param serial_number[out] The monitor serial number.
4278 *
4279 * Each of \c *make, \c *model and \c *serial_number are set only if the
4280 * information is found in the EDID. The pointers they are set to must not
4281 * be free()'d explicitly, instead they get implicitly freed when the
4282 * \c drm_output is destroyed.
4283 */
Richard Hughes2b2092a2013-04-24 14:58:02 +01004284static void
Daniel Stone02cf4662017-03-03 16:19:39 +00004285find_and_parse_output_edid(struct drm_backend *b, struct drm_output *output,
Pekka Paalanen6f1866b2017-04-03 14:22:51 +03004286 drmModeObjectPropertiesPtr props,
4287 const char **make,
4288 const char **model,
4289 const char **serial_number)
Richard Hughes2b2092a2013-04-24 14:58:02 +01004290{
4291 drmModePropertyBlobPtr edid_blob = NULL;
Daniel Stone02cf4662017-03-03 16:19:39 +00004292 uint32_t blob_id;
Richard Hughes2b2092a2013-04-24 14:58:02 +01004293 int rc;
4294
Daniel Stone02cf4662017-03-03 16:19:39 +00004295 blob_id =
4296 drm_property_get_value(&output->props_conn[WDRM_CONNECTOR_EDID],
4297 props, 0);
4298 if (!blob_id)
4299 return;
4300
4301 edid_blob = drmModeGetPropertyBlob(b->drm.fd, blob_id);
Richard Hughes2b2092a2013-04-24 14:58:02 +01004302 if (!edid_blob)
4303 return;
4304
4305 rc = edid_parse(&output->edid,
4306 edid_blob->data,
4307 edid_blob->length);
4308 if (!rc) {
4309 weston_log("EDID data '%s', '%s', '%s'\n",
4310 output->edid.pnp_id,
4311 output->edid.monitor_name,
4312 output->edid.serial_number);
4313 if (output->edid.pnp_id[0] != '\0')
Pekka Paalanen6f1866b2017-04-03 14:22:51 +03004314 *make = output->edid.pnp_id;
Richard Hughes2b2092a2013-04-24 14:58:02 +01004315 if (output->edid.monitor_name[0] != '\0')
Pekka Paalanen6f1866b2017-04-03 14:22:51 +03004316 *model = output->edid.monitor_name;
Richard Hughes2b2092a2013-04-24 14:58:02 +01004317 if (output->edid.serial_number[0] != '\0')
Pekka Paalanen6f1866b2017-04-03 14:22:51 +03004318 *serial_number = output->edid.serial_number;
Richard Hughes2b2092a2013-04-24 14:58:02 +01004319 }
4320 drmModeFreePropertyBlob(edid_blob);
4321}
4322
Kristian Høgsberga30989a2013-05-23 17:23:15 -04004323static int
4324parse_modeline(const char *s, drmModeModeInfo *mode)
4325{
4326 char hsync[16];
4327 char vsync[16];
4328 float fclock;
4329
4330 mode->type = DRM_MODE_TYPE_USERDEF;
4331 mode->hskew = 0;
4332 mode->vscan = 0;
4333 mode->vrefresh = 0;
4334 mode->flags = 0;
4335
Rob Bradford307e09e2013-07-26 16:29:40 +01004336 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
Kristian Høgsberga30989a2013-05-23 17:23:15 -04004337 &fclock,
4338 &mode->hdisplay,
4339 &mode->hsync_start,
4340 &mode->hsync_end,
4341 &mode->htotal,
4342 &mode->vdisplay,
4343 &mode->vsync_start,
4344 &mode->vsync_end,
4345 &mode->vtotal, hsync, vsync) != 11)
4346 return -1;
4347
4348 mode->clock = fclock * 1000;
4349 if (strcmp(hsync, "+hsync") == 0)
4350 mode->flags |= DRM_MODE_FLAG_PHSYNC;
4351 else if (strcmp(hsync, "-hsync") == 0)
4352 mode->flags |= DRM_MODE_FLAG_NHSYNC;
4353 else
4354 return -1;
4355
4356 if (strcmp(vsync, "+vsync") == 0)
4357 mode->flags |= DRM_MODE_FLAG_PVSYNC;
4358 else if (strcmp(vsync, "-vsync") == 0)
4359 mode->flags |= DRM_MODE_FLAG_NVSYNC;
4360 else
4361 return -1;
4362
Emmanuel Gil Peyrota62138b2016-05-02 22:40:11 +01004363 snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f",
4364 mode->hdisplay, mode->vdisplay, fclock);
4365
Kristian Høgsberga30989a2013-05-23 17:23:15 -04004366 return 0;
4367}
4368
Rob Bradford66bd9f52013-06-25 18:56:42 +01004369static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03004370setup_output_seat_constraint(struct drm_backend *b,
Rob Bradford66bd9f52013-06-25 18:56:42 +01004371 struct weston_output *output,
4372 const char *s)
4373{
4374 if (strcmp(s, "") != 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05004375 struct weston_pointer *pointer;
Rob Bradford66bd9f52013-06-25 18:56:42 +01004376 struct udev_seat *seat;
4377
Giulio Camuffo954f1832014-10-11 18:27:30 +03004378 seat = udev_seat_get_named(&b->input, s);
Derek Foreman0720ea32015-07-15 13:00:35 -05004379 if (!seat)
4380 return;
Rob Bradford66bd9f52013-06-25 18:56:42 +01004381
Derek Foreman0720ea32015-07-15 13:00:35 -05004382 seat->base.output = output;
4383
Derek Foreman1281a362015-07-31 16:55:32 -05004384 pointer = weston_seat_get_pointer(&seat->base);
4385 if (pointer)
4386 weston_pointer_clamp(pointer,
4387 &pointer->x,
4388 &pointer->y);
Rob Bradford66bd9f52013-06-25 18:56:42 +01004389 }
4390}
4391
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004392static int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004393parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format)
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004394{
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004395 int ret = 0;
4396
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004397 if (s == NULL)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004398 *gbm_format = default_value;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004399 else if (strcmp(s, "xrgb8888") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004400 *gbm_format = GBM_FORMAT_XRGB8888;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004401 else if (strcmp(s, "rgb565") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004402 *gbm_format = GBM_FORMAT_RGB565;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004403 else if (strcmp(s, "xrgb2101010") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004404 *gbm_format = GBM_FORMAT_XRGB2101010;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004405 else {
4406 weston_log("fatal: unrecognized pixel format: %s\n", s);
4407 ret = -1;
4408 }
4409
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004410 return ret;
4411}
4412
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004413/**
4414 * Choose suitable mode for an output
4415 *
4416 * Find the most suitable mode to use for initial setup (or reconfiguration on
4417 * hotplug etc) for a DRM output.
4418 *
4419 * @param output DRM output to choose mode for
4420 * @param kind Strategy and preference to use when choosing mode
4421 * @param width Desired width for this output
4422 * @param height Desired height for this output
4423 * @param current_mode Mode currently being displayed on this output
4424 * @param modeline Manually-entered mode (may be NULL)
4425 * @returns A mode from the output's mode list, or NULL if none available
4426 */
4427static struct drm_mode *
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004428drm_output_choose_initial_mode(struct drm_backend *backend,
4429 struct drm_output *output,
4430 enum weston_drm_backend_output_mode mode,
Armin Krezović08368132016-09-30 14:11:05 +02004431 const char *modeline,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004432 const drmModeModeInfo *current_mode)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004433{
4434 struct drm_mode *preferred = NULL;
4435 struct drm_mode *current = NULL;
4436 struct drm_mode *configured = NULL;
4437 struct drm_mode *best = NULL;
4438 struct drm_mode *drm_mode;
Armin Krezović08368132016-09-30 14:11:05 +02004439 drmModeModeInfo drm_modeline;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004440 int32_t width = 0;
4441 int32_t height = 0;
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01004442 uint32_t refresh = 0;
4443 int n;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004444
Armin Krezović08368132016-09-30 14:11:05 +02004445 if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) {
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01004446 n = sscanf(modeline, "%dx%d@%d", &width, &height, &refresh);
4447 if (n != 2 && n != 3) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004448 width = -1;
4449
Armin Krezović08368132016-09-30 14:11:05 +02004450 if (parse_modeline(modeline, &drm_modeline) == 0) {
4451 configured = drm_output_add_mode(output, &drm_modeline);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004452 if (!configured)
4453 return NULL;
4454 } else {
4455 weston_log("Invalid modeline \"%s\" for output %s\n",
Armin Krezović08368132016-09-30 14:11:05 +02004456 modeline, output->base.name);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004457 }
4458 }
4459 }
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004460
4461 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004462 if (width == drm_mode->base.width &&
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01004463 height == drm_mode->base.height &&
4464 (refresh == 0 || refresh == drm_mode->mode_info.vrefresh))
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004465 configured = drm_mode;
4466
comic fans7a5c5622016-03-17 14:29:27 +02004467 if (memcmp(current_mode, &drm_mode->mode_info,
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004468 sizeof *current_mode) == 0)
4469 current = drm_mode;
4470
4471 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
4472 preferred = drm_mode;
4473
4474 best = drm_mode;
4475 }
4476
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004477 if (current == NULL && current_mode->clock != 0) {
4478 current = drm_output_add_mode(output, current_mode);
4479 if (!current)
4480 return NULL;
4481 }
4482
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004483 if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004484 configured = current;
4485
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004486 if (configured)
4487 return configured;
4488
4489 if (preferred)
4490 return preferred;
4491
4492 if (current)
4493 return current;
4494
4495 if (best)
4496 return best;
4497
4498 weston_log("no available modes for %s\n", output->base.name);
4499 return NULL;
4500}
4501
Pekka Paalaneneee580b2014-06-04 16:43:06 +03004502static int
4503connector_get_current_mode(drmModeConnector *connector, int drm_fd,
4504 drmModeModeInfo *mode)
4505{
4506 drmModeEncoder *encoder;
4507 drmModeCrtc *crtc;
4508
4509 /* Get the current mode on the crtc that's currently driving
4510 * this connector. */
4511 encoder = drmModeGetEncoder(drm_fd, connector->encoder_id);
4512 memset(mode, 0, sizeof *mode);
4513 if (encoder != NULL) {
4514 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
4515 drmModeFreeEncoder(encoder);
4516 if (crtc == NULL)
4517 return -1;
4518 if (crtc->mode_valid)
4519 *mode = crtc->mode;
4520 drmModeFreeCrtc(crtc);
4521 }
4522
4523 return 0;
4524}
4525
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004526static int
Armin Krezović08368132016-09-30 14:11:05 +02004527drm_output_set_mode(struct weston_output *base,
4528 enum weston_drm_backend_output_mode mode,
4529 const char *modeline)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004530{
Armin Krezović08368132016-09-30 14:11:05 +02004531 struct drm_output *output = to_drm_output(base);
4532 struct drm_backend *b = to_drm_backend(base->compositor);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004533
Armin Krezović445b41b2016-10-09 23:48:16 +02004534 struct drm_mode *current;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004535 drmModeModeInfo crtc_mode;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004536
Armin Krezović08368132016-09-30 14:11:05 +02004537 if (connector_get_current_mode(output->connector, b->drm.fd, &crtc_mode) < 0)
Armin Krezović445b41b2016-10-09 23:48:16 +02004538 return -1;
David Herrmann0f0d54e2011-12-08 17:05:45 +01004539
Armin Krezović08368132016-09-30 14:11:05 +02004540 current = drm_output_choose_initial_mode(b, output, mode, modeline, &crtc_mode);
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004541 if (!current)
Armin Krezović445b41b2016-10-09 23:48:16 +02004542 return -1;
Armin Krezović08368132016-09-30 14:11:05 +02004543
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004544 output->base.current_mode = &current->base;
Hardeningff39efa2013-09-18 23:56:35 +02004545 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
Wang Quanxianacb805a2012-07-30 18:09:46 -04004546
Armin Krezović08368132016-09-30 14:11:05 +02004547 /* Set native_ fields, so weston_output_mode_switch_to_native() works */
4548 output->base.native_mode = output->base.current_mode;
4549 output->base.native_scale = output->base.current_scale;
4550
Armin Krezović08368132016-09-30 14:11:05 +02004551 return 0;
Armin Krezović08368132016-09-30 14:11:05 +02004552}
4553
4554static void
4555drm_output_set_gbm_format(struct weston_output *base,
4556 const char *gbm_format)
4557{
4558 struct drm_output *output = to_drm_output(base);
4559 struct drm_backend *b = to_drm_backend(base->compositor);
4560
4561 if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1)
4562 output->gbm_format = b->gbm_format;
Daniel Stonee2e80132018-01-16 15:37:33 +00004563
4564 /* Without universal planes, we can't discover which formats are
4565 * supported by the primary plane; we just hope that the GBM format
4566 * works. */
4567 if (!b->universal_planes)
4568 output->scanout_plane->formats[0] = output->gbm_format;
Armin Krezović08368132016-09-30 14:11:05 +02004569}
4570
4571static void
4572drm_output_set_seat(struct weston_output *base,
4573 const char *seat)
4574{
4575 struct drm_output *output = to_drm_output(base);
4576 struct drm_backend *b = to_drm_backend(base->compositor);
4577
4578 setup_output_seat_constraint(b, &output->base,
4579 seat ? seat : "");
4580}
4581
4582static int
4583drm_output_enable(struct weston_output *base)
4584{
4585 struct drm_output *output = to_drm_output(base);
4586 struct drm_backend *b = to_drm_backend(base->compositor);
4587 struct weston_mode *m;
4588
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00004589 if (b->pageflip_timeout)
4590 drm_output_pageflip_timer_create(output);
4591
Giulio Camuffo954f1832014-10-11 18:27:30 +03004592 if (b->use_pixman) {
4593 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004594 weston_log("Failed to init output pixman state\n");
Daniel Stone02cf4662017-03-03 16:19:39 +00004595 goto err;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004596 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03004597 } else if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02004598 weston_log("Failed to init output gl state\n");
Daniel Stone02cf4662017-03-03 16:19:39 +00004599 goto err;
Kristian Høgsberg1d1e0a52012-10-21 13:29:26 -04004600 }
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04004601
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004602 if (output->backlight) {
Kristian Høgsberg220819f2013-05-23 20:29:40 -04004603 weston_log("Initialized backlight, device %s\n",
4604 output->backlight->path);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004605 output->base.set_backlight = drm_set_backlight;
4606 output->base.backlight_current = drm_get_backlight(output);
Kristian Høgsberg220819f2013-05-23 20:29:40 -04004607 } else {
4608 weston_log("Failed to initialize backlight\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004609 }
4610
Jonas Ådahle5a12252013-04-05 23:07:11 +02004611 output->base.start_repaint_loop = drm_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05004612 output->base.repaint = drm_output_repaint;
Jesse Barnes58ef3792012-02-23 09:45:49 -05004613 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004614 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08004615 output->base.switch_mode = drm_output_switch_mode;
Richard Hughese7299962013-05-01 21:52:12 +01004616 output->base.set_gamma = drm_output_set_gamma;
4617
Daniel Stone2ba17f42015-05-19 20:02:41 +01004618 if (output->cursor_plane)
4619 weston_compositor_stack_plane(b->compositor,
4620 &output->cursor_plane->base,
4621 NULL);
4622 else
4623 b->cursors_are_broken = 1;
4624
Daniel Stonee2e80132018-01-16 15:37:33 +00004625 weston_compositor_stack_plane(b->compositor,
4626 &output->scanout_plane->base,
Giulio Camuffo954f1832014-10-11 18:27:30 +03004627 &b->compositor->primary_plane);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004628
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04004629 weston_log("Output %s, (connector %d, crtc %d)\n",
Richard Hughesafe690c2013-05-02 10:10:04 +01004630 output->base.name, output->connector_id, output->crtc_id);
Kristian Høgsberg061c4252012-06-28 11:28:15 -04004631 wl_list_for_each(m, &output->base.mode_list, link)
U. Artie Eoffd3ed6cb2014-01-10 10:15:17 -08004632 weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n",
Kristian Høgsberg061c4252012-06-28 11:28:15 -04004633 m->width, m->height, m->refresh / 1000.0,
4634 m->flags & WL_OUTPUT_MODE_PREFERRED ?
4635 ", preferred" : "",
4636 m->flags & WL_OUTPUT_MODE_CURRENT ?
4637 ", current" : "",
Armin Krezović08368132016-09-30 14:11:05 +02004638 output->connector->count_modes == 0 ?
Kristian Høgsberg061c4252012-06-28 11:28:15 -04004639 ", built-in" : "");
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04004640
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004641 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01004642
Daniel Stone02cf4662017-03-03 16:19:39 +00004643err:
David Herrmann0f0d54e2011-12-08 17:05:45 +01004644 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004645}
4646
Jesse Barnes58ef3792012-02-23 09:45:49 -05004647static void
Armin Krezović08368132016-09-30 14:11:05 +02004648drm_output_deinit(struct weston_output *base)
4649{
4650 struct drm_output *output = to_drm_output(base);
4651 struct drm_backend *b = to_drm_backend(base->compositor);
Daniel Stone087ddf02017-02-14 17:51:30 +00004652 uint32_t *unused;
Armin Krezović08368132016-09-30 14:11:05 +02004653
Daniel Stone3e661f72016-11-04 17:24:06 +00004654 if (b->use_pixman)
Armin Krezović08368132016-09-30 14:11:05 +02004655 drm_output_fini_pixman(output);
Daniel Stone3e661f72016-11-04 17:24:06 +00004656 else
4657 drm_output_fini_egl(output);
Armin Krezović08368132016-09-30 14:11:05 +02004658
Daniel Stone2ba17f42015-05-19 20:02:41 +01004659 /* Since our planes are no longer in use anywhere, remove their base
4660 * weston_plane's link from the plane stacking list, unless we're
4661 * shutting down, in which case the plane has already been
4662 * destroyed. */
Daniel Stonee2e80132018-01-16 15:37:33 +00004663 if (!b->shutting_down) {
4664 wl_list_remove(&output->scanout_plane->base.link);
4665 wl_list_init(&output->scanout_plane->base.link);
4666
4667 if (output->cursor_plane) {
4668 wl_list_remove(&output->cursor_plane->base.link);
4669 wl_list_init(&output->cursor_plane->base.link);
4670 /* Turn off hardware cursor */
4671 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
4672 }
Daniel Stone2ba17f42015-05-19 20:02:41 +01004673 }
Daniel Stone087ddf02017-02-14 17:51:30 +00004674
4675 unused = wl_array_add(&b->unused_connectors, sizeof(*unused));
4676 *unused = output->connector_id;
4677 unused = wl_array_add(&b->unused_crtcs, sizeof(*unused));
4678 *unused = output->crtc_id;
Armin Krezović08368132016-09-30 14:11:05 +02004679}
4680
4681static void
4682drm_output_destroy(struct weston_output *base)
4683{
4684 struct drm_output *output = to_drm_output(base);
4685 struct drm_backend *b = to_drm_backend(base->compositor);
Armin Krezović445b41b2016-10-09 23:48:16 +02004686 struct drm_mode *drm_mode, *next;
Armin Krezović08368132016-09-30 14:11:05 +02004687
Daniel Stone598ee9d2016-11-16 11:55:20 +00004688 if (output->page_flip_pending || output->vblank_pending ||
4689 output->atomic_complete_pending) {
Armin Krezović08368132016-09-30 14:11:05 +02004690 output->destroy_pending = 1;
4691 weston_log("destroy output while page flip pending\n");
4692 return;
4693 }
4694
4695 if (output->base.enabled)
4696 drm_output_deinit(&output->base);
4697
Daniel Stone2ba17f42015-05-19 20:02:41 +01004698 if (!b->universal_planes && !b->shutting_down) {
4699 /* With universal planes, the 'special' planes are allocated at
4700 * startup, freed at shutdown, and live on the plane list in
4701 * between. We want the planes to continue to exist and be freed
4702 * up for other outputs.
4703 *
4704 * Without universal planes, our special planes are
4705 * pseudo-planes allocated at output creation, freed at output
4706 * destruction, and not usable by other outputs.
4707 *
4708 * On the other hand, if the compositor is already shutting down,
4709 * the plane has already been destroyed.
4710 */
4711 if (output->cursor_plane)
4712 drm_plane_destroy(output->cursor_plane);
Daniel Stone62c0d632018-02-05 13:01:02 +00004713 if (output->scanout_plane)
4714 drm_plane_destroy(output->scanout_plane);
Daniel Stone2ba17f42015-05-19 20:02:41 +01004715 }
4716
Armin Krezović445b41b2016-10-09 23:48:16 +02004717 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
Daniel Stoned5526cb2016-11-16 10:54:10 +00004718 base.link)
4719 drm_output_destroy_mode(b, drm_mode);
Armin Krezović445b41b2016-10-09 23:48:16 +02004720
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00004721 if (output->pageflip_timer)
4722 wl_event_source_remove(output->pageflip_timer);
4723
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03004724 weston_output_release(&output->base);
Armin Krezović08368132016-09-30 14:11:05 +02004725
Daniel Stone02cf4662017-03-03 16:19:39 +00004726 drm_property_info_free(output->props_conn, WDRM_CONNECTOR__COUNT);
Pekka Paalanencd011a62016-11-15 22:07:49 +00004727 drm_property_info_free(output->props_crtc, WDRM_CRTC__COUNT);
Daniel Stone02cf4662017-03-03 16:19:39 +00004728
Armin Krezović08368132016-09-30 14:11:05 +02004729 drmModeFreeConnector(output->connector);
4730
4731 if (output->backlight)
4732 backlight_destroy(output->backlight);
4733
Daniel Stone7b2ddac2016-11-11 19:11:49 +00004734 assert(!output->state_last);
4735 drm_output_state_free(output->state_cur);
4736
Armin Krezović08368132016-09-30 14:11:05 +02004737 free(output);
4738}
4739
4740static int
4741drm_output_disable(struct weston_output *base)
4742{
4743 struct drm_output *output = to_drm_output(base);
4744 struct drm_backend *b = to_drm_backend(base->compositor);
Daniel Stonea08512f2016-11-08 17:46:10 +00004745 struct drm_pending_state *pending_state;
4746 int ret;
Armin Krezović08368132016-09-30 14:11:05 +02004747
Daniel Stone598ee9d2016-11-16 11:55:20 +00004748 if (output->page_flip_pending || output->vblank_pending ||
4749 output->atomic_complete_pending) {
Armin Krezović08368132016-09-30 14:11:05 +02004750 output->disable_pending = 1;
4751 return -1;
4752 }
4753
Daniel Stonea08512f2016-11-08 17:46:10 +00004754 weston_log("Disabling output %s\n", output->base.name);
4755 pending_state = drm_pending_state_alloc(b);
4756 drm_output_get_disable_state(pending_state, output);
4757 ret = drm_pending_state_apply_sync(pending_state);
4758 if (ret)
4759 weston_log("Couldn't disable output %s\n", output->base.name);
4760
Armin Krezović08368132016-09-30 14:11:05 +02004761 if (output->base.enabled)
4762 drm_output_deinit(&output->base);
4763
4764 output->disable_pending = 0;
4765
Armin Krezović08368132016-09-30 14:11:05 +02004766 return 0;
4767}
4768
4769/**
Daniel Stone087ddf02017-02-14 17:51:30 +00004770 * Update the list of unused connectors and CRTCs
4771 *
4772 * This keeps the unused_connectors and unused_crtcs arrays up to date.
4773 *
4774 * @param b Weston backend structure
4775 * @param resources DRM resources for this device
4776 */
4777static void
4778drm_backend_update_unused_outputs(struct drm_backend *b, drmModeRes *resources)
4779{
4780 int i;
4781
4782 wl_array_release(&b->unused_connectors);
4783 wl_array_init(&b->unused_connectors);
4784
4785 for (i = 0; i < resources->count_connectors; i++) {
4786 struct drm_output *output;
4787 uint32_t *connector_id;
4788
4789 output = drm_output_find_by_connector(b, resources->connectors[i]);
4790 if (output && output->base.enabled)
4791 continue;
4792
4793 connector_id = wl_array_add(&b->unused_connectors,
4794 sizeof(*connector_id));
4795 *connector_id = resources->connectors[i];
4796 }
4797
4798 wl_array_release(&b->unused_crtcs);
4799 wl_array_init(&b->unused_crtcs);
4800
4801 for (i = 0; i < resources->count_crtcs; i++) {
4802 struct drm_output *output;
4803 uint32_t *crtc_id;
4804
4805 output = drm_output_find_by_crtc(b, resources->crtcs[i]);
4806 if (output && output->base.enabled)
4807 continue;
4808
4809 crtc_id = wl_array_add(&b->unused_crtcs, sizeof(*crtc_id));
4810 *crtc_id = resources->crtcs[i];
4811 }
4812}
4813
4814/**
Armin Krezović08368132016-09-30 14:11:05 +02004815 * Create a Weston output structure
4816 *
4817 * Given a DRM connector, create a matching drm_output structure and add it
4818 * to Weston's output list. It also takes ownership of the connector, which
4819 * is released when output is destroyed.
4820 *
4821 * @param b Weston backend structure
4822 * @param resources DRM resources for this device
4823 * @param connector DRM connector to use for this new output
4824 * @param drm_device udev device pointer
4825 * @returns 0 on success, or -1 on failure
4826 */
4827static int
4828create_output_for_connector(struct drm_backend *b,
4829 drmModeRes *resources,
4830 drmModeConnector *connector,
4831 struct udev_device *drm_device)
4832{
4833 struct drm_output *output;
Daniel Stone02cf4662017-03-03 16:19:39 +00004834 drmModeObjectPropertiesPtr props;
Armin Krezović445b41b2016-10-09 23:48:16 +02004835 struct drm_mode *drm_mode;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03004836 char *name;
Pekka Paalanen6f1866b2017-04-03 14:22:51 +03004837 const char *make = "unknown";
4838 const char *model = "unknown";
4839 const char *serial_number = "unknown";
Daniel Stonec1d0f472017-01-27 15:11:33 +00004840 drmModeCrtcPtr origcrtc;
Armin Krezović08368132016-09-30 14:11:05 +02004841 int i;
4842
4843 i = find_crtc_for_connector(b, resources, connector);
4844 if (i < 0) {
4845 weston_log("No usable crtc/encoder pair for connector.\n");
Daniel Stone62c0d632018-02-05 13:01:02 +00004846 goto err_init;
Armin Krezović08368132016-09-30 14:11:05 +02004847 }
4848
4849 output = zalloc(sizeof *output);
4850 if (output == NULL)
Daniel Stone62c0d632018-02-05 13:01:02 +00004851 goto err_init;
Armin Krezović08368132016-09-30 14:11:05 +02004852
4853 output->connector = connector;
4854 output->crtc_id = resources->crtcs[i];
4855 output->pipe = i;
4856 output->connector_id = connector->connector_id;
4857
4858 output->backlight = backlight_init(drm_device,
4859 connector->connector_type);
4860
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03004861 name = make_connector_name(connector);
4862 weston_output_init(&output->base, b->compositor, name);
4863 free(name);
4864
Armin Krezović08368132016-09-30 14:11:05 +02004865 output->base.enable = drm_output_enable;
4866 output->base.destroy = drm_output_destroy;
4867 output->base.disable = drm_output_disable;
Armin Krezović08368132016-09-30 14:11:05 +02004868
Daniel Stonec1d0f472017-01-27 15:11:33 +00004869 origcrtc = drmModeGetCrtc(b->drm.fd, output->crtc_id);
4870 if (origcrtc == NULL)
4871 goto err_output;
4872
4873 output->base.gamma_size = origcrtc->gamma_size;
4874 drmModeFreeCrtc(origcrtc);
4875
Armin Krezović08368132016-09-30 14:11:05 +02004876 output->destroy_pending = 0;
4877 output->disable_pending = 0;
Armin Krezović08368132016-09-30 14:11:05 +02004878
Pekka Paalanencd011a62016-11-15 22:07:49 +00004879 props = drmModeObjectGetProperties(b->drm.fd, output->crtc_id,
4880 DRM_MODE_OBJECT_CRTC);
4881 if (!props) {
4882 weston_log("failed to get CRTC properties\n");
4883 goto err_output;
4884 }
4885 drm_property_info_populate(b, crtc_props, output->props_crtc,
4886 WDRM_CRTC__COUNT, props);
4887 drmModeFreeObjectProperties(props);
4888
Daniel Stone02cf4662017-03-03 16:19:39 +00004889 props = drmModeObjectGetProperties(b->drm.fd, connector->connector_id,
4890 DRM_MODE_OBJECT_CONNECTOR);
4891 if (!props) {
4892 weston_log("failed to get connector properties\n");
Daniel Stone62c0d632018-02-05 13:01:02 +00004893 goto err_output;
Daniel Stone02cf4662017-03-03 16:19:39 +00004894 }
4895 drm_property_info_populate(b, connector_props, output->props_conn,
4896 WDRM_CONNECTOR__COUNT, props);
Pekka Paalanen6f1866b2017-04-03 14:22:51 +03004897 find_and_parse_output_edid(b, output, props,
4898 &make, &model, &serial_number);
4899 output->base.make = (char *)make;
4900 output->base.model = (char *)model;
4901 output->base.serial_number = (char *)serial_number;
Pekka Paalanena0bfedc2017-04-03 14:42:51 +03004902 output->base.subpixel = drm_subpixel_to_wayland(output->connector->subpixel);
4903
Daniel Stonec1d0f472017-01-27 15:11:33 +00004904 drmModeFreeObjectProperties(props);
4905
Pekka Paalanena0bfedc2017-04-03 14:42:51 +03004906 if (output->connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
4907 output->connector->connector_type == DRM_MODE_CONNECTOR_eDP)
4908 output->base.connection_internal = true;
4909
Daniel Stone7b2ddac2016-11-11 19:11:49 +00004910 output->state_cur = drm_output_state_alloc(output, NULL);
4911
Pekka Paalanena0bfedc2017-04-03 14:42:51 +03004912 output->base.mm_width = output->connector->mmWidth;
4913 output->base.mm_height = output->connector->mmHeight;
4914
Armin Krezović445b41b2016-10-09 23:48:16 +02004915 for (i = 0; i < output->connector->count_modes; i++) {
4916 drm_mode = drm_output_add_mode(output, &output->connector->modes[i]);
4917 if (!drm_mode) {
Daniel Stone62c0d632018-02-05 13:01:02 +00004918 weston_log("failed to add mode\n");
4919 goto err_output;
Armin Krezović445b41b2016-10-09 23:48:16 +02004920 }
4921 }
4922
Daniel Stonee2e80132018-01-16 15:37:33 +00004923 output->scanout_plane =
4924 drm_output_find_special_plane(b, output,
4925 WDRM_PLANE_TYPE_PRIMARY);
4926 if (!output->scanout_plane) {
4927 weston_log("Failed to find primary plane for output %s\n",
4928 output->base.name);
Daniel Stone62c0d632018-02-05 13:01:02 +00004929 goto err_output;
Daniel Stonee2e80132018-01-16 15:37:33 +00004930 }
4931
Daniel Stone2ba17f42015-05-19 20:02:41 +01004932 /* Failing to find a cursor plane is not fatal, as we'll fall back
4933 * to software cursor. */
4934 output->cursor_plane =
4935 drm_output_find_special_plane(b, output,
4936 WDRM_PLANE_TYPE_CURSOR);
4937
Armin Krezović08368132016-09-30 14:11:05 +02004938 weston_compositor_add_pending_output(&output->base, b->compositor);
4939
4940 return 0;
Armin Krezović445b41b2016-10-09 23:48:16 +02004941
Daniel Stone62c0d632018-02-05 13:01:02 +00004942err_output:
4943 drm_output_destroy(&output->base);
4944 return -1;
4945 /* no fallthrough! */
Armin Krezović445b41b2016-10-09 23:48:16 +02004946
Daniel Stone62c0d632018-02-05 13:01:02 +00004947err_init:
4948 drmModeFreeConnector(connector);
Armin Krezović445b41b2016-10-09 23:48:16 +02004949 return -1;
Armin Krezović08368132016-09-30 14:11:05 +02004950}
4951
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004952static int
Ucan, Emre (ADITG/SW1)9a200d72017-02-02 14:06:56 +00004953create_outputs(struct drm_backend *b, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004954{
4955 drmModeConnector *connector;
4956 drmModeRes *resources;
4957 int i;
4958
Giulio Camuffo954f1832014-10-11 18:27:30 +03004959 resources = drmModeGetResources(b->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004960 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02004961 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004962 return -1;
4963 }
4964
Giulio Camuffo954f1832014-10-11 18:27:30 +03004965 b->min_width = resources->min_width;
4966 b->max_width = resources->max_width;
4967 b->min_height = resources->min_height;
4968 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05004969
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004970 for (i = 0; i < resources->count_connectors; i++) {
Daniel Stone02cf4662017-03-03 16:19:39 +00004971 int ret;
4972
Giulio Camuffo954f1832014-10-11 18:27:30 +03004973 connector = drmModeGetConnector(b->drm.fd,
Benjamin Franzke117483d2011-08-30 11:38:26 +02004974 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004975 if (connector == NULL)
4976 continue;
4977
Ucan, Emre (ADITG/SW1)38ea2d22017-03-08 15:43:18 +00004978 if (connector->connection == DRM_MODE_CONNECTED) {
Daniel Stone02cf4662017-03-03 16:19:39 +00004979 ret = create_output_for_connector(b, resources,
4980 connector, drm_device);
4981 if (ret < 0)
4982 weston_log("failed to create new connector\n");
Armin Krezović08368132016-09-30 14:11:05 +02004983 } else {
4984 drmModeFreeConnector(connector);
Benjamin Franzke9eaee352011-08-02 13:03:54 +02004985 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004986 }
4987
Daniel Stone087ddf02017-02-14 17:51:30 +00004988 drm_backend_update_unused_outputs(b, resources);
4989
Armin Krezović08368132016-09-30 14:11:05 +02004990 if (wl_list_empty(&b->compositor->output_list) &&
4991 wl_list_empty(&b->compositor->pending_output_list))
Martin Minarik6d118362012-06-07 18:01:59 +02004992 weston_log("No currently active connector found.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004993
4994 drmModeFreeResources(resources);
4995
4996 return 0;
4997}
4998
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004999static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03005000update_outputs(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005001{
5002 drmModeConnector *connector;
5003 drmModeRes *resources;
5004 struct drm_output *output, *next;
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00005005 uint32_t *connected;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005006 int i;
5007
Giulio Camuffo954f1832014-10-11 18:27:30 +03005008 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005009 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02005010 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005011 return;
5012 }
5013
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00005014 connected = calloc(resources->count_connectors, sizeof(uint32_t));
5015 if (!connected) {
5016 drmModeFreeResources(resources);
5017 return;
5018 }
5019
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005020 /* collect new connects */
5021 for (i = 0; i < resources->count_connectors; i++) {
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +00005022 uint32_t connector_id = resources->connectors[i];
Benjamin Franzke117483d2011-08-30 11:38:26 +02005023
Giulio Camuffo954f1832014-10-11 18:27:30 +03005024 connector = drmModeGetConnector(b->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01005025 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005026 continue;
5027
David Herrmann7551cff2011-12-08 17:05:43 +01005028 if (connector->connection != DRM_MODE_CONNECTED) {
5029 drmModeFreeConnector(connector);
5030 continue;
5031 }
5032
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00005033 connected[i] = connector_id;
Benjamin Franzke117483d2011-08-30 11:38:26 +02005034
Daniel Stonec0ec7592017-02-09 13:58:35 +00005035 if (drm_output_find_by_connector(b, connector_id)) {
Armin Krezović08368132016-09-30 14:11:05 +02005036 drmModeFreeConnector(connector);
Daniel Stonec0ec7592017-02-09 13:58:35 +00005037 continue;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005038 }
Daniel Stonec0ec7592017-02-09 13:58:35 +00005039
5040 create_output_for_connector(b, resources,
5041 connector, drm_device);
5042 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005043 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005044
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00005045 wl_list_for_each_safe(output, next, &b->compositor->output_list,
5046 base.link) {
5047 bool disconnected = true;
5048
5049 for (i = 0; i < resources->count_connectors; i++) {
5050 if (connected[i] == output->connector_id) {
5051 disconnected = false;
5052 break;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005053 }
5054 }
Armin Krezović08368132016-09-30 14:11:05 +02005055
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00005056 if (!disconnected)
5057 continue;
5058
5059 weston_log("connector %d disconnected\n", output->connector_id);
5060 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005061 }
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00005062
5063 wl_list_for_each_safe(output, next, &b->compositor->pending_output_list,
5064 base.link) {
5065 bool disconnected = true;
5066
5067 for (i = 0; i < resources->count_connectors; i++) {
5068 if (connected[i] == output->connector_id) {
5069 disconnected = false;
5070 break;
5071 }
5072 }
5073
5074 if (!disconnected)
5075 continue;
5076
5077 weston_log("connector %d disconnected\n", output->connector_id);
5078 drm_output_destroy(&output->base);
5079 }
5080
Daniel Stone087ddf02017-02-14 17:51:30 +00005081 drm_backend_update_unused_outputs(b, resources);
5082
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00005083 free(connected);
5084 drmModeFreeResources(resources);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005085}
5086
5087static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03005088udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005089{
David Herrmannd7488c22012-03-11 20:05:21 +01005090 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01005091 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01005092
5093 sysnum = udev_device_get_sysnum(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005094 if (!sysnum || atoi(sysnum) != b->drm.id)
David Herrmannd7488c22012-03-11 20:05:21 +01005095 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02005096
David Herrmann6ac52db2012-03-11 20:05:22 +01005097 val = udev_device_get_property_value(device, "HOTPLUG");
5098 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005099 return 0;
5100
David Herrmann6ac52db2012-03-11 20:05:22 +01005101 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005102}
5103
Kristian Høgsbergb1868472011-04-22 12:27:57 -04005104static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005105udev_drm_event(int fd, uint32_t mask, void *data)
5106{
Giulio Camuffo954f1832014-10-11 18:27:30 +03005107 struct drm_backend *b = data;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005108 struct udev_device *event;
5109
Giulio Camuffo954f1832014-10-11 18:27:30 +03005110 event = udev_monitor_receive_device(b->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02005111
Giulio Camuffo954f1832014-10-11 18:27:30 +03005112 if (udev_event_is_hotplug(b, event))
5113 update_outputs(b, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005114
5115 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04005116
5117 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005118}
5119
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005120static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04005121drm_restore(struct weston_compositor *ec)
5122{
Kristian Høgsberg3f495872013-09-18 23:00:17 -07005123 weston_launcher_restore(ec->launcher);
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04005124}
5125
5126static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005127drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005128{
Armin Krezović545dba62016-08-05 15:54:18 +02005129 struct drm_backend *b = to_drm_backend(ec);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005130
Giulio Camuffo954f1832014-10-11 18:27:30 +03005131 udev_input_destroy(&b->input);
Jonas Ådahlc97af922012-03-28 22:36:09 +02005132
Giulio Camuffo954f1832014-10-11 18:27:30 +03005133 wl_event_source_remove(b->udev_drm_source);
5134 wl_event_source_remove(b->drm_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02005135
Daniel Stoneb57c6a02017-10-05 16:27:21 +01005136 b->shutting_down = true;
5137
Giulio Camuffo954f1832014-10-11 18:27:30 +03005138 destroy_sprites(b);
Kristian Høgsberg3d64a3e2013-05-10 12:36:04 -04005139
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03005140 weston_compositor_shutdown(ec);
5141
Giulio Camuffo954f1832014-10-11 18:27:30 +03005142 if (b->gbm)
5143 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02005144
Pekka Paalanen2a0c6c32017-09-13 16:48:01 +03005145 udev_unref(b->udev);
5146
Giulio Camuffo954f1832014-10-11 18:27:30 +03005147 weston_launcher_destroy(ec->launcher);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005148
Daniel Stone087ddf02017-02-14 17:51:30 +00005149 wl_array_release(&b->unused_crtcs);
5150 wl_array_release(&b->unused_connectors);
5151
Giulio Camuffo954f1832014-10-11 18:27:30 +03005152 close(b->drm.fd);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005153 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005154}
5155
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04005156static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -07005157session_notify(struct wl_listener *listener, void *data)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04005158{
Kristian Høgsberg61741a22013-09-17 16:02:57 -07005159 struct weston_compositor *compositor = data;
Armin Krezović545dba62016-08-05 15:54:18 +02005160 struct drm_backend *b = to_drm_backend(compositor);
Daniel Stone085d2b92015-05-21 00:00:57 +01005161 struct drm_plane *plane;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04005162 struct drm_output *output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04005163
Giulio Camuffo954f1832014-10-11 18:27:30 +03005164 if (compositor->session_active) {
Kristian Høgsberg61741a22013-09-17 16:02:57 -07005165 weston_log("activating session\n");
Daniel Stonef33e1042016-11-05 08:10:13 +00005166 weston_compositor_wake(compositor);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005167 weston_compositor_damage_all(compositor);
Daniel Stone6020f472018-02-05 15:46:20 +00005168 b->state_invalid = true;
Giulio Camuffo954f1832014-10-11 18:27:30 +03005169 udev_input_enable(&b->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07005170 } else {
5171 weston_log("deactivating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03005172 udev_input_disable(&b->input);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04005173
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005174 weston_compositor_offscreen(compositor);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04005175
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05005176 /* If we have a repaint scheduled (either from a
5177 * pending pageflip or the idle handler), make sure we
5178 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005179 * vt switched away. The OFFSCREEN state will prevent
Abdur Rehman4dca0e12017-01-01 19:46:35 +05005180 * further attempts at repainting. When we switch
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05005181 * back, we schedule a repaint, which will process
5182 * pending frame callbacks. */
5183
Giulio Camuffo954f1832014-10-11 18:27:30 +03005184 wl_list_for_each(output, &compositor->output_list, base.link) {
Daniel Stone09a97e22017-03-01 11:34:06 +00005185 output->base.repaint_needed = false;
Daniel Stone2ba17f42015-05-19 20:02:41 +01005186 if (output->cursor_plane)
5187 drmModeSetCursor(b->drm.fd, output->crtc_id,
5188 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05005189 }
5190
Giulio Camuffo954f1832014-10-11 18:27:30 +03005191 output = container_of(compositor->output_list.next,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04005192 struct drm_output, base.link);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05005193
Daniel Stone085d2b92015-05-21 00:00:57 +01005194 wl_list_for_each(plane, &b->plane_list, link) {
5195 if (plane->type != WDRM_PLANE_TYPE_OVERLAY)
5196 continue;
5197
Giulio Camuffo954f1832014-10-11 18:27:30 +03005198 drmModeSetPlane(b->drm.fd,
Daniel Stone085d2b92015-05-21 00:00:57 +01005199 plane->plane_id,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04005200 output->crtc_id, 0, 0,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05005201 0, 0, 0, 0, 0, 0, 0, 0);
Daniel Stone085d2b92015-05-21 00:00:57 +01005202 }
5203 }
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04005204}
5205
Daniel Stoneefa504f2016-12-19 16:48:20 +00005206/**
5207 * Determines whether or not a device is capable of modesetting. If successful,
5208 * sets b->drm.fd and b->drm.filename to the opened device.
5209 */
5210static bool
5211drm_device_is_kms(struct drm_backend *b, struct udev_device *device)
5212{
5213 const char *filename = udev_device_get_devnode(device);
5214 const char *sysnum = udev_device_get_sysnum(device);
5215 drmModeRes *res;
5216 int id, fd;
5217
5218 if (!filename)
5219 return false;
5220
5221 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
5222 if (fd < 0)
5223 return false;
5224
5225 res = drmModeGetResources(fd);
5226 if (!res)
5227 goto out_fd;
5228
5229 if (res->count_crtcs <= 0 || res->count_connectors <= 0 ||
5230 res->count_encoders <= 0)
5231 goto out_res;
5232
5233 if (sysnum)
5234 id = atoi(sysnum);
5235 if (!sysnum || id < 0) {
5236 weston_log("couldn't get sysnum for device %s\n", filename);
5237 goto out_res;
5238 }
5239
5240 /* We can be called successfully on multiple devices; if we have,
5241 * clean up old entries. */
5242 if (b->drm.fd >= 0)
5243 weston_launcher_close(b->compositor->launcher, b->drm.fd);
5244 free(b->drm.filename);
5245
5246 b->drm.fd = fd;
5247 b->drm.id = id;
5248 b->drm.filename = strdup(filename);
5249
Sergi Granellceb59812017-03-28 12:44:04 +02005250 drmModeFreeResources(res);
5251
Daniel Stoneefa504f2016-12-19 16:48:20 +00005252 return true;
5253
5254out_res:
5255 drmModeFreeResources(res);
5256out_fd:
5257 weston_launcher_close(b->compositor->launcher, fd);
5258 return false;
5259}
5260
David Herrmann0af066f2012-10-29 19:21:16 +01005261/*
5262 * Find primary GPU
5263 * Some systems may have multiple DRM devices attached to a single seat. This
5264 * function loops over all devices and tries to find a PCI device with the
5265 * boot_vga sysfs attribute set to 1.
5266 * If no such device is found, the first DRM device reported by udev is used.
Daniel Stoneefa504f2016-12-19 16:48:20 +00005267 * Devices are also vetted to make sure they are are capable of modesetting,
5268 * rather than pure render nodes (GPU with no display), or pure
5269 * memory-allocation devices (VGEM).
David Herrmann0af066f2012-10-29 19:21:16 +01005270 */
5271static struct udev_device*
Giulio Camuffo954f1832014-10-11 18:27:30 +03005272find_primary_gpu(struct drm_backend *b, const char *seat)
David Herrmann0af066f2012-10-29 19:21:16 +01005273{
5274 struct udev_enumerate *e;
5275 struct udev_list_entry *entry;
5276 const char *path, *device_seat, *id;
5277 struct udev_device *device, *drm_device, *pci;
5278
Giulio Camuffo954f1832014-10-11 18:27:30 +03005279 e = udev_enumerate_new(b->udev);
David Herrmann0af066f2012-10-29 19:21:16 +01005280 udev_enumerate_add_match_subsystem(e, "drm");
5281 udev_enumerate_add_match_sysname(e, "card[0-9]*");
5282
5283 udev_enumerate_scan_devices(e);
5284 drm_device = NULL;
5285 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Daniel Stoneefa504f2016-12-19 16:48:20 +00005286 bool is_boot_vga = false;
5287
David Herrmann0af066f2012-10-29 19:21:16 +01005288 path = udev_list_entry_get_name(entry);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005289 device = udev_device_new_from_syspath(b->udev, path);
David Herrmann0af066f2012-10-29 19:21:16 +01005290 if (!device)
5291 continue;
5292 device_seat = udev_device_get_property_value(device, "ID_SEAT");
5293 if (!device_seat)
5294 device_seat = default_seat;
5295 if (strcmp(device_seat, seat)) {
5296 udev_device_unref(device);
5297 continue;
5298 }
5299
5300 pci = udev_device_get_parent_with_subsystem_devtype(device,
5301 "pci", NULL);
5302 if (pci) {
5303 id = udev_device_get_sysattr_value(pci, "boot_vga");
Daniel Stoneefa504f2016-12-19 16:48:20 +00005304 if (id && !strcmp(id, "1"))
5305 is_boot_vga = true;
David Herrmann0af066f2012-10-29 19:21:16 +01005306 }
5307
Daniel Stoneefa504f2016-12-19 16:48:20 +00005308 /* If we already have a modesetting-capable device, and this
5309 * device isn't our boot-VGA device, we aren't going to use
5310 * it. */
5311 if (!is_boot_vga && drm_device) {
David Herrmann0af066f2012-10-29 19:21:16 +01005312 udev_device_unref(device);
Daniel Stoneefa504f2016-12-19 16:48:20 +00005313 continue;
5314 }
5315
5316 /* Make sure this device is actually capable of modesetting;
5317 * if this call succeeds, b->drm.{fd,filename} will be set,
5318 * and any old values freed. */
5319 if (!drm_device_is_kms(b, device)) {
5320 udev_device_unref(device);
5321 continue;
5322 }
5323
5324 /* There can only be one boot_vga device, and we try to use it
5325 * at all costs. */
5326 if (is_boot_vga) {
5327 if (drm_device)
5328 udev_device_unref(drm_device);
5329 drm_device = device;
5330 break;
5331 }
5332
5333 /* Per the (!is_boot_vga && drm_device) test above, we only
5334 * trump existing saved devices with boot-VGA devices, so if
5335 * we end up here, this must be the first device we've seen. */
5336 assert(!drm_device);
5337 drm_device = device;
David Herrmann0af066f2012-10-29 19:21:16 +01005338 }
5339
Daniel Stoneefa504f2016-12-19 16:48:20 +00005340 /* If we're returning a device to use, we must have an open FD for
5341 * it. */
5342 assert(!!drm_device == (b->drm.fd >= 0));
5343
David Herrmann0af066f2012-10-29 19:21:16 +01005344 udev_enumerate_unref(e);
5345 return drm_device;
5346}
5347
Pekka Paalanenb45ed8b2017-03-28 18:04:27 +03005348static struct udev_device *
5349open_specific_drm_device(struct drm_backend *b, const char *name)
5350{
5351 struct udev_device *device;
5352
5353 device = udev_device_new_from_subsystem_sysname(b->udev, "drm", name);
5354 if (!device) {
5355 weston_log("ERROR: could not open DRM device '%s'\n", name);
5356 return NULL;
5357 }
5358
5359 if (!drm_device_is_kms(b, device)) {
5360 udev_device_unref(device);
5361 weston_log("ERROR: DRM device '%s' is not a KMS device.\n", name);
5362 return NULL;
5363 }
5364
5365 /* If we're returning a device to use, we must have an open FD for
5366 * it. */
5367 assert(b->drm.fd >= 0);
5368
5369 return device;
5370}
5371
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02005372static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02005373planes_binding(struct weston_keyboard *keyboard, const struct timespec *time,
5374 uint32_t key, void *data)
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02005375{
Giulio Camuffo954f1832014-10-11 18:27:30 +03005376 struct drm_backend *b = data;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02005377
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02005378 switch (key) {
5379 case KEY_C:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005380 b->cursors_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02005381 break;
5382 case KEY_V:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005383 b->sprites_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02005384 break;
5385 case KEY_O:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005386 b->sprites_hidden ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02005387 break;
5388 default:
5389 break;
5390 }
Ander Conselvan de Oliveira180f42a2012-11-21 15:11:37 +02005391}
5392
Kristian Høgsberg0eac34a2013-08-30 14:28:22 -07005393#ifdef BUILD_VAAPI_RECORDER
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005394static void
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03005395recorder_destroy(struct drm_output *output)
5396{
5397 vaapi_recorder_destroy(output->recorder);
5398 output->recorder = NULL;
5399
5400 output->base.disable_planes--;
5401
5402 wl_list_remove(&output->recorder_frame_listener.link);
5403 weston_log("[libva recorder] done\n");
5404}
5405
5406static void
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005407recorder_frame_notify(struct wl_listener *listener, void *data)
5408{
5409 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03005410 struct drm_backend *b;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005411 int fd, ret;
5412
5413 output = container_of(listener, struct drm_output,
5414 recorder_frame_listener);
Armin Krezović545dba62016-08-05 15:54:18 +02005415 b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005416
5417 if (!output->recorder)
5418 return;
5419
Daniel Stonee2e80132018-01-16 15:37:33 +00005420 ret = drmPrimeHandleToFD(b->drm.fd,
5421 output->scanout_plane->state_cur->fb->handle,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005422 DRM_CLOEXEC, &fd);
5423 if (ret) {
5424 weston_log("[libva recorder] "
5425 "failed to create prime fd for front buffer\n");
5426 return;
5427 }
5428
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03005429 ret = vaapi_recorder_frame(output->recorder, fd,
Daniel Stonee2e80132018-01-16 15:37:33 +00005430 output->scanout_plane->state_cur->fb->stride);
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03005431 if (ret < 0) {
5432 weston_log("[libva recorder] aborted: %m\n");
5433 recorder_destroy(output);
5434 }
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005435}
5436
5437static void *
Giulio Camuffo954f1832014-10-11 18:27:30 +03005438create_recorder(struct drm_backend *b, int width, int height,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005439 const char *filename)
5440{
5441 int fd;
5442 drm_magic_t magic;
5443
Giulio Camuffo954f1832014-10-11 18:27:30 +03005444 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005445 if (fd < 0)
5446 return NULL;
5447
5448 drmGetMagic(fd, &magic);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005449 drmAuthMagic(b->drm.fd, magic);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005450
5451 return vaapi_recorder_create(fd, width, height, filename);
5452}
5453
5454static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02005455recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time,
5456 uint32_t key, void *data)
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005457{
Giulio Camuffo954f1832014-10-11 18:27:30 +03005458 struct drm_backend *b = data;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005459 struct drm_output *output;
5460 int width, height;
5461
Giulio Camuffo954f1832014-10-11 18:27:30 +03005462 output = container_of(b->compositor->output_list.next,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005463 struct drm_output, base.link);
5464
5465 if (!output->recorder) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01005466 if (output->gbm_format != GBM_FORMAT_XRGB8888) {
Ander Conselvan de Oliveira2ef1cd12014-05-06 16:49:06 +03005467 weston_log("failed to start vaapi recorder: "
5468 "output format not supported\n");
5469 return;
5470 }
5471
Hardeningff39efa2013-09-18 23:56:35 +02005472 width = output->base.current_mode->width;
5473 height = output->base.current_mode->height;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005474
5475 output->recorder =
Giulio Camuffo954f1832014-10-11 18:27:30 +03005476 create_recorder(b, width, height, "capture.h264");
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005477 if (!output->recorder) {
5478 weston_log("failed to create vaapi recorder\n");
5479 return;
5480 }
5481
5482 output->base.disable_planes++;
5483
5484 output->recorder_frame_listener.notify = recorder_frame_notify;
5485 wl_signal_add(&output->base.frame_signal,
5486 &output->recorder_frame_listener);
5487
5488 weston_output_schedule_repaint(&output->base);
5489
5490 weston_log("[libva recorder] initialized\n");
5491 } else {
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03005492 recorder_destroy(output);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005493 }
5494}
5495#else
5496static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02005497recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time,
5498 uint32_t key, void *data)
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005499{
5500 weston_log("Compiled without libva support\n");
5501}
5502#endif
5503
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005504static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03005505switch_to_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005506{
5507 struct drm_output *output;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03005508 bool dmabuf_support_inited;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005509
Giulio Camuffo954f1832014-10-11 18:27:30 +03005510 if (!b->use_pixman)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005511 return;
5512
Pekka Paalanene4d231e2014-06-12 15:12:48 +03005513 dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf;
5514
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005515 weston_log("Switching to GL renderer\n");
5516
Giulio Camuffo954f1832014-10-11 18:27:30 +03005517 b->gbm = create_gbm_device(b->drm.fd);
5518 if (!b->gbm) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005519 weston_log("Failed to create gbm device. "
5520 "Aborting renderer switch\n");
5521 return;
5522 }
5523
Giulio Camuffo954f1832014-10-11 18:27:30 +03005524 wl_list_for_each(output, &b->compositor->output_list, base.link)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005525 pixman_renderer_output_destroy(&output->base);
5526
Giulio Camuffo954f1832014-10-11 18:27:30 +03005527 b->compositor->renderer->destroy(b->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005528
Giulio Camuffo954f1832014-10-11 18:27:30 +03005529 if (drm_backend_create_gl_renderer(b) < 0) {
5530 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005531 weston_log("Failed to create GL renderer. Quitting.\n");
5532 /* FIXME: we need a function to shutdown cleanly */
5533 assert(0);
5534 }
5535
Giulio Camuffo954f1832014-10-11 18:27:30 +03005536 wl_list_for_each(output, &b->compositor->output_list, base.link)
5537 drm_output_init_egl(output, b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005538
Giulio Camuffo954f1832014-10-11 18:27:30 +03005539 b->use_pixman = 0;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03005540
5541 if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) {
5542 if (linux_dmabuf_setup(b->compositor) < 0)
5543 weston_log("Error: initializing dmabuf "
5544 "support failed.\n");
5545 }
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005546}
5547
5548static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02005549renderer_switch_binding(struct weston_keyboard *keyboard,
5550 const struct timespec *time, uint32_t key, void *data)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005551{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005552 struct drm_backend *b =
Armin Krezović545dba62016-08-05 15:54:18 +02005553 to_drm_backend(keyboard->seat->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005554
Giulio Camuffo954f1832014-10-11 18:27:30 +03005555 switch_to_gl_renderer(b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005556}
5557
Armin Krezović08368132016-09-30 14:11:05 +02005558static const struct weston_drm_output_api api = {
5559 drm_output_set_mode,
5560 drm_output_set_gbm_format,
5561 drm_output_set_seat,
5562};
5563
Giulio Camuffo954f1832014-10-11 18:27:30 +03005564static struct drm_backend *
5565drm_backend_create(struct weston_compositor *compositor,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005566 struct weston_drm_backend_config *config)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005567{
Giulio Camuffo954f1832014-10-11 18:27:30 +03005568 struct drm_backend *b;
David Herrmann0af066f2012-10-29 19:21:16 +01005569 struct udev_device *drm_device;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005570 struct wl_event_loop *loop;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005571 const char *seat_id = default_seat;
Armin Krezović08368132016-09-30 14:11:05 +02005572 int ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005573
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04005574 weston_log("initializing drm backend\n");
5575
Giulio Camuffo954f1832014-10-11 18:27:30 +03005576 b = zalloc(sizeof *b);
5577 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005578 return NULL;
Daniel Stone725c2c32012-06-22 14:04:36 +01005579
Daniel Stone6020f472018-02-05 15:46:20 +00005580 b->state_invalid = true;
Daniel Stoneefa504f2016-12-19 16:48:20 +00005581 b->drm.fd = -1;
Daniel Stone087ddf02017-02-14 17:51:30 +00005582 wl_array_init(&b->unused_crtcs);
5583 wl_array_init(&b->unused_connectors);
Daniel Stoneefa504f2016-12-19 16:48:20 +00005584
Pekka Paalanen68583832015-05-19 09:53:16 +03005585 /*
5586 * KMS support for hardware planes cannot properly synchronize
5587 * without nuclear page flip. Without nuclear/atomic, hw plane
5588 * and cursor plane updates would either tear or cause extra
5589 * waits for vblanks which means dropping the compositor framerate
Pekka Paalanen3f32a132015-09-07 15:38:43 +03005590 * to a fraction. For cursors, it's not so bad, so they are
5591 * enabled.
Pekka Paalanen68583832015-05-19 09:53:16 +03005592 *
5593 * These can be enabled again when nuclear/atomic support lands.
5594 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03005595 b->sprites_are_broken = 1;
Giulio Camuffo954f1832014-10-11 18:27:30 +03005596 b->compositor = compositor;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005597 b->use_pixman = config->use_pixman;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00005598 b->pageflip_timeout = config->pageflip_timeout;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07005599
Pekka Paalanen7da9a382017-08-30 11:29:49 +03005600 compositor->backend = &b->base;
5601
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005602 if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0)
5603 goto err_compositor;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07005604
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005605 if (config->seat_id)
5606 seat_id = config->seat_id;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02005607
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01005608 /* Check if we run drm-backend using weston-launch */
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005609 compositor->launcher = weston_launcher_connect(compositor, config->tty,
5610 seat_id, true);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005611 if (compositor->launcher == NULL) {
Pekka Paalanena453f4d2017-10-31 10:19:48 +02005612 weston_log("fatal: drm backend should be run using "
5613 "weston-launch binary, or your system should "
5614 "provide the logind D-Bus API.\n");
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01005615 goto err_compositor;
5616 }
5617
Giulio Camuffo954f1832014-10-11 18:27:30 +03005618 b->udev = udev_new();
5619 if (b->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02005620 weston_log("failed to initialize udev context\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07005621 goto err_launcher;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005622 }
5623
Giulio Camuffo954f1832014-10-11 18:27:30 +03005624 b->session_listener.notify = session_notify;
5625 wl_signal_add(&compositor->session_signal, &b->session_listener);
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05005626
Pekka Paalanenb45ed8b2017-03-28 18:04:27 +03005627 if (config->specific_device)
5628 drm_device = open_specific_drm_device(b, config->specific_device);
5629 else
5630 drm_device = find_primary_gpu(b, seat_id);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04005631 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02005632 weston_log("no drm device found\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07005633 goto err_udev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005634 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005635
Daniel Stoneefa504f2016-12-19 16:48:20 +00005636 if (init_kms_caps(b) < 0) {
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02005637 weston_log("failed to initialize kms\n");
5638 goto err_udev_dev;
5639 }
5640
Giulio Camuffo954f1832014-10-11 18:27:30 +03005641 if (b->use_pixman) {
5642 if (init_pixman(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02005643 weston_log("failed to initialize pixman renderer\n");
5644 goto err_udev_dev;
5645 }
5646 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +03005647 if (init_egl(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02005648 weston_log("failed to initialize egl\n");
5649 goto err_udev_dev;
5650 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005651 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05005652
Giulio Camuffo954f1832014-10-11 18:27:30 +03005653 b->base.destroy = drm_destroy;
5654 b->base.restore = drm_restore;
Daniel Stoneeedf84c2017-02-10 18:06:04 +00005655 b->base.repaint_begin = drm_repaint_begin;
5656 b->base.repaint_flush = drm_repaint_flush;
5657 b->base.repaint_cancel = drm_repaint_cancel;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02005658
Bob Ham91880f12016-01-12 10:21:47 +00005659 weston_setup_vt_switch_bindings(compositor);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04005660
Daniel Stone085d2b92015-05-21 00:00:57 +01005661 wl_list_init(&b->plane_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005662 create_sprites(b);
Jesse Barnes58ef3792012-02-23 09:45:49 -05005663
Giulio Camuffo954f1832014-10-11 18:27:30 +03005664 if (udev_input_init(&b->input,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03005665 compositor, b->udev, seat_id,
5666 config->configure_device) < 0) {
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03005667 weston_log("failed to create input devices\n");
5668 goto err_sprite;
5669 }
5670
Ucan, Emre (ADITG/SW1)9a200d72017-02-02 14:06:56 +00005671 if (create_outputs(b, drm_device) < 0) {
Daniel Stoneefa504f2016-12-19 16:48:20 +00005672 weston_log("failed to create output for %s\n", b->drm.filename);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03005673 goto err_udev_input;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005674 }
5675
Jason Ekstrand9fc71512014-04-02 19:53:46 -05005676 /* A this point we have some idea of whether or not we have a working
5677 * cursor plane. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03005678 if (!b->cursors_are_broken)
5679 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
Jason Ekstrand9fc71512014-04-02 19:53:46 -05005680
Giulio Camuffo954f1832014-10-11 18:27:30 +03005681 loop = wl_display_get_event_loop(compositor->wl_display);
5682 b->drm_source =
5683 wl_event_loop_add_fd(loop, b->drm.fd,
5684 WL_EVENT_READABLE, on_drm_input, b);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005685
Giulio Camuffo954f1832014-10-11 18:27:30 +03005686 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
5687 if (b->udev_monitor == NULL) {
Abdur Rehman4dca0e12017-01-01 19:46:35 +05005688 weston_log("failed to initialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01005689 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005690 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03005691 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005692 "drm", NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005693 b->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02005694 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03005695 udev_monitor_get_fd(b->udev_monitor),
5696 WL_EVENT_READABLE, udev_drm_event, b);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005697
Giulio Camuffo954f1832014-10-11 18:27:30 +03005698 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02005699 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01005700 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005701 }
5702
Daniel Stonea96b93c2012-06-22 14:04:37 +01005703 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01005704
Giulio Camuffo954f1832014-10-11 18:27:30 +03005705 weston_compositor_add_debug_binding(compositor, KEY_O,
5706 planes_binding, b);
5707 weston_compositor_add_debug_binding(compositor, KEY_C,
5708 planes_binding, b);
5709 weston_compositor_add_debug_binding(compositor, KEY_V,
5710 planes_binding, b);
5711 weston_compositor_add_debug_binding(compositor, KEY_Q,
5712 recorder_binding, b);
5713 weston_compositor_add_debug_binding(compositor, KEY_W,
5714 renderer_switch_binding, b);
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02005715
Pekka Paalanene4d231e2014-06-12 15:12:48 +03005716 if (compositor->renderer->import_dmabuf) {
5717 if (linux_dmabuf_setup(compositor) < 0)
5718 weston_log("Error: initializing dmabuf "
5719 "support failed.\n");
5720 }
5721
Armin Krezović08368132016-09-30 14:11:05 +02005722 ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME,
5723 &api, sizeof(api));
5724
5725 if (ret < 0) {
5726 weston_log("Failed to register output API.\n");
5727 goto err_udev_monitor;
5728 }
5729
Giulio Camuffo954f1832014-10-11 18:27:30 +03005730 return b;
Daniel Stonea96b93c2012-06-22 14:04:37 +01005731
5732err_udev_monitor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005733 wl_event_source_remove(b->udev_drm_source);
5734 udev_monitor_unref(b->udev_monitor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01005735err_drm_source:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005736 wl_event_source_remove(b->drm_source);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03005737err_udev_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005738 udev_input_destroy(&b->input);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04005739err_sprite:
Emmanuel Gil Peyrotb8347e32016-05-02 22:40:13 +01005740 if (b->gbm)
5741 gbm_device_destroy(b->gbm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005742 destroy_sprites(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01005743err_udev_dev:
5744 udev_device_unref(drm_device);
Kristian Høgsberg3f495872013-09-18 23:00:17 -07005745err_launcher:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005746 weston_launcher_destroy(compositor->launcher);
Daniel Stonea96b93c2012-06-22 14:04:37 +01005747err_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005748 udev_unref(b->udev);
Daniel Stonea96b93c2012-06-22 14:04:37 +01005749err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005750 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005751 free(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01005752 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005753}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005754
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005755static void
5756config_init_to_defaults(struct weston_drm_backend_config *config)
5757{
5758}
5759
Giulio Camuffo954f1832014-10-11 18:27:30 +03005760WL_EXPORT int
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01005761weston_backend_init(struct weston_compositor *compositor,
5762 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005763{
Giulio Camuffo954f1832014-10-11 18:27:30 +03005764 struct drm_backend *b;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005765 struct weston_drm_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005766
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005767 if (config_base == NULL ||
5768 config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION ||
5769 config_base->struct_size > sizeof(struct weston_drm_backend_config)) {
5770 weston_log("drm backend config structure is invalid\n");
5771 return -1;
5772 }
Benjamin Franzke117483d2011-08-30 11:38:26 +02005773
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005774 config_init_to_defaults(&config);
5775 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07005776
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005777 b = drm_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005778 if (b == NULL)
5779 return -1;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005780
Giulio Camuffo954f1832014-10-11 18:27:30 +03005781 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005782}