blob: c5f971d296085bf9a2384619bca4a55b211dc519 [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
Pekka Paalanen925788f2018-04-19 14:20:01 +03004 * Copyright © 2017, 2018 Collabora, Ltd.
5 * Copyright © 2017, 2018 General Electric Company
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04006 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07007 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040014 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070015 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial
17 * portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040027 */
28
Daniel Stonec228e232013-05-22 18:03:19 +030029#include "config.h"
Kristian Høgsberg0b9334a2011-04-12 11:34:32 -040030
Jesse Barnes58ef3792012-02-23 09:45:49 -050031#include <errno.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030032#include <stdint.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040033#include <stdlib.h>
Richard Hughes2b2092a2013-04-24 14:58:02 +010034#include <ctype.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040035#include <string.h>
36#include <fcntl.h>
37#include <unistd.h>
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -040038#include <linux/input.h>
Kristian Høgsberg3f495872013-09-18 23:00:17 -070039#include <linux/vt.h>
Ander Conselvan de Oliveirafd1f4c62012-06-26 17:09:14 +030040#include <assert.h>
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +020041#include <sys/mman.h>
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +030042#include <dlfcn.h>
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +030043#include <time.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040044
Benjamin Franzkec649a922011-03-02 11:56:04 +010045#include <xf86drm.h>
46#include <xf86drmMode.h>
Jesse Barnes58ef3792012-02-23 09:45:49 -050047#include <drm_fourcc.h>
Benjamin Franzkec649a922011-03-02 11:56:04 +010048
Benjamin Franzke060cf802011-04-30 09:32:11 +020049#include <gbm.h>
Pekka Paalanen33156972012-08-03 13:30:30 -040050#include <libudev.h>
Benjamin Franzke060cf802011-04-30 09:32:11 +020051
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -070052#include "compositor.h"
53#include "compositor-drm.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070054#include "shared/helpers.h"
Mario Kleinerf507ec32015-06-21 21:25:14 +020055#include "shared/timespec-util.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010056#include "gl-renderer.h"
Vincent Abriouc9506672016-10-05 16:14:07 +020057#include "weston-egl-ext.h"
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +020058#include "pixman-renderer.h"
Daniel Stone0b70fa42017-04-04 17:54:23 +010059#include "pixel-formats.h"
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -070060#include "libbacklight.h"
Peter Hutterer823ad332014-11-26 07:06:31 +100061#include "libinput-seat.h"
Benjamin Franzkebfeda132012-01-30 14:04:04 +010062#include "launcher-util.h"
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +030063#include "vaapi-recorder.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020064#include "presentation-time-server-protocol.h"
Pekka Paalanene4d231e2014-06-12 15:12:48 +030065#include "linux-dmabuf.h"
Micah Fedkec8890122017-02-01 15:28:23 -050066#include "linux-dmabuf-unstable-v1-server-protocol.h"
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040067
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +030068#ifndef DRM_CAP_TIMESTAMP_MONOTONIC
69#define DRM_CAP_TIMESTAMP_MONOTONIC 0x6
70#endif
71
Pekka Paalanenc5de57f2015-05-20 23:01:44 +010072#ifndef DRM_CLIENT_CAP_UNIVERSAL_PLANES
73#define DRM_CLIENT_CAP_UNIVERSAL_PLANES 2
74#endif
75
Ankit Nautiyala21c3932097-03-19 00:24:57 +053076#ifndef DRM_CLIENT_CAP_ASPECT_RATIO
77#define DRM_CLIENT_CAP_ASPECT_RATIO 4
78#endif
79
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -030080#ifndef DRM_CAP_CURSOR_WIDTH
81#define DRM_CAP_CURSOR_WIDTH 0x8
82#endif
83
84#ifndef DRM_CAP_CURSOR_HEIGHT
85#define DRM_CAP_CURSOR_HEIGHT 0x9
86#endif
87
88#ifndef GBM_BO_USE_CURSOR
89#define GBM_BO_USE_CURSOR GBM_BO_USE_CURSOR_64X64
90#endif
91
Pekka Paalanendfc06832017-11-20 14:04:38 +020092#define MAX_CLONED_CONNECTORS 4
Pekka Paalanenc112f002017-08-28 16:27:20 +030093
Daniel Stone02cf4662017-03-03 16:19:39 +000094/**
Ankit Nautiyala21c3932097-03-19 00:24:57 +053095 * aspect ratio info taken from the drmModeModeInfo flag bits 19-22,
96 * which should be used to fill the aspect ratio field in weston_mode.
97 */
98#define DRM_MODE_FLAG_PIC_AR_BITS_POS 19
99#ifndef DRM_MODE_FLAG_PIC_AR_MASK
100#define DRM_MODE_FLAG_PIC_AR_MASK (0xF << DRM_MODE_FLAG_PIC_AR_BITS_POS)
101#endif
102
103/**
Daniel Stone02cf4662017-03-03 16:19:39 +0000104 * Represents the values of an enum-type KMS property
105 */
106struct drm_property_enum_info {
107 const char *name; /**< name as string (static, not freed) */
108 bool valid; /**< true if value is supported; ignore if false */
109 uint64_t value; /**< raw value */
110};
111
112/**
113 * Holds information on a DRM property, including its ID and the enum
114 * values it holds.
115 *
116 * DRM properties are allocated dynamically, and maintained as DRM objects
117 * within the normal object ID space; they thus do not have a stable ID
118 * to refer to. This includes enum values, which must be referred to by
119 * integer values, but these are not stable.
120 *
121 * drm_property_info allows a cache to be maintained where Weston can use
122 * enum values internally to refer to properties, with the mapping to DRM
123 * ID values being maintained internally.
124 */
125struct drm_property_info {
126 const char *name; /**< name as string (static, not freed) */
127 uint32_t prop_id; /**< KMS property object ID */
128 unsigned int num_enum_values; /**< number of enum values */
129 struct drm_property_enum_info *enum_values; /**< array of enum values */
130};
131
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000132/**
Daniel Stone598ee9d2016-11-16 11:55:20 +0000133 * List of properties attached to DRM planes
134 */
135enum wdrm_plane_property {
136 WDRM_PLANE_TYPE = 0,
137 WDRM_PLANE_SRC_X,
138 WDRM_PLANE_SRC_Y,
139 WDRM_PLANE_SRC_W,
140 WDRM_PLANE_SRC_H,
141 WDRM_PLANE_CRTC_X,
142 WDRM_PLANE_CRTC_Y,
143 WDRM_PLANE_CRTC_W,
144 WDRM_PLANE_CRTC_H,
145 WDRM_PLANE_FB_ID,
146 WDRM_PLANE_CRTC_ID,
147 WDRM_PLANE__COUNT
148};
149
150/**
151 * Possible values for the WDRM_PLANE_TYPE property.
152 */
153enum wdrm_plane_type {
154 WDRM_PLANE_TYPE_PRIMARY = 0,
155 WDRM_PLANE_TYPE_CURSOR,
156 WDRM_PLANE_TYPE_OVERLAY,
157 WDRM_PLANE_TYPE__COUNT
158};
159
160static struct drm_property_enum_info plane_type_enums[] = {
161 [WDRM_PLANE_TYPE_PRIMARY] = {
162 .name = "Primary",
163 },
164 [WDRM_PLANE_TYPE_OVERLAY] = {
165 .name = "Overlay",
166 },
167 [WDRM_PLANE_TYPE_CURSOR] = {
168 .name = "Cursor",
169 },
170};
171
172static const struct drm_property_info plane_props[] = {
173 [WDRM_PLANE_TYPE] = {
174 .name = "type",
175 .enum_values = plane_type_enums,
176 .num_enum_values = WDRM_PLANE_TYPE__COUNT,
177 },
178 [WDRM_PLANE_SRC_X] = { .name = "SRC_X", },
179 [WDRM_PLANE_SRC_Y] = { .name = "SRC_Y", },
180 [WDRM_PLANE_SRC_W] = { .name = "SRC_W", },
181 [WDRM_PLANE_SRC_H] = { .name = "SRC_H", },
182 [WDRM_PLANE_CRTC_X] = { .name = "CRTC_X", },
183 [WDRM_PLANE_CRTC_Y] = { .name = "CRTC_Y", },
184 [WDRM_PLANE_CRTC_W] = { .name = "CRTC_W", },
185 [WDRM_PLANE_CRTC_H] = { .name = "CRTC_H", },
186 [WDRM_PLANE_FB_ID] = { .name = "FB_ID", },
187 [WDRM_PLANE_CRTC_ID] = { .name = "CRTC_ID", },
188};
189
190/**
191 * List of properties attached to a DRM connector
192 */
193enum wdrm_connector_property {
194 WDRM_CONNECTOR_EDID = 0,
195 WDRM_CONNECTOR_DPMS,
196 WDRM_CONNECTOR_CRTC_ID,
197 WDRM_CONNECTOR__COUNT
198};
199
200static const struct drm_property_info connector_props[] = {
201 [WDRM_CONNECTOR_EDID] = { .name = "EDID" },
202 [WDRM_CONNECTOR_DPMS] = { .name = "DPMS" },
203 [WDRM_CONNECTOR_CRTC_ID] = { .name = "CRTC_ID", },
204};
205
206/**
Pekka Paalanencd011a62016-11-15 22:07:49 +0000207 * List of properties attached to DRM CRTCs
208 */
209enum wdrm_crtc_property {
210 WDRM_CRTC_MODE_ID = 0,
211 WDRM_CRTC_ACTIVE,
212 WDRM_CRTC__COUNT
213};
214
Daniel Stone598ee9d2016-11-16 11:55:20 +0000215static const struct drm_property_info crtc_props[] = {
216 [WDRM_CRTC_MODE_ID] = { .name = "MODE_ID", },
217 [WDRM_CRTC_ACTIVE] = { .name = "ACTIVE", },
218};
219
Pekka Paalanencd011a62016-11-15 22:07:49 +0000220/**
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000221 * Mode for drm_output_state_duplicate.
222 */
223enum drm_output_state_duplicate_mode {
224 DRM_OUTPUT_STATE_CLEAR_PLANES, /**< reset all planes to off */
225 DRM_OUTPUT_STATE_PRESERVE_PLANES, /**< preserve plane state */
226};
227
228/**
229 * Mode for drm_pending_state_apply and co.
230 */
231enum drm_state_apply_mode {
232 DRM_STATE_APPLY_SYNC, /**< state fully processed */
233 DRM_STATE_APPLY_ASYNC, /**< state pending event delivery */
234};
235
Giulio Camuffo954f1832014-10-11 18:27:30 +0300236struct drm_backend {
237 struct weston_backend base;
238 struct weston_compositor *compositor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400239
240 struct udev *udev;
241 struct wl_event_source *drm_source;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400242
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100243 struct udev_monitor *udev_monitor;
244 struct wl_event_source *udev_drm_source;
245
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100246 struct {
David Herrmannd7488c22012-03-11 20:05:21 +0100247 int id;
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100248 int fd;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300249 char *filename;
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100250 } drm;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200251 struct gbm_device *gbm;
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700252 struct wl_listener session_listener;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100253 uint32_t gbm_format;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200254
Rob Clark4339add2012-08-09 14:18:28 -0500255 /* we need these parameters in order to not fail drmModeAddFB2()
256 * due to out of bounds dimensions, and then mistakenly set
257 * sprites_are_broken:
258 */
Daniel Stonef214fdc2016-11-14 17:43:57 +0000259 int min_width, max_width;
260 int min_height, max_height;
Rob Clark4339add2012-08-09 14:18:28 -0500261
Daniel Stone085d2b92015-05-21 00:00:57 +0100262 struct wl_list plane_list;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500263 int sprites_are_broken;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +0200264 int sprites_hidden;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500265
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000266 void *repaint_data;
267
Daniel Stone6020f472018-02-05 15:46:20 +0000268 bool state_invalid;
269
Pekka Paalaneneacec812017-09-12 13:43:51 +0300270 /* CRTC IDs not used by any enabled output. */
Daniel Stone087ddf02017-02-14 17:51:30 +0000271 struct wl_array unused_crtcs;
272
Rob Clarkab5b1e32012-08-09 13:24:45 -0500273 int cursors_are_broken;
274
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100275 bool universal_planes;
Pekka Paalanencd011a62016-11-15 22:07:49 +0000276 bool atomic_modeset;
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100277
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200278 int use_pixman;
Pekka Paalanendee412d2018-04-23 11:44:58 +0200279 bool use_pixman_shadow;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200280
Rob Bradfordd355b802013-05-31 18:09:55 +0100281 struct udev_input input;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -0300282
Daniel Stone70d337d2015-06-16 18:42:23 +0100283 int32_t cursor_width;
284 int32_t cursor_height;
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +0000285
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000286 uint32_t pageflip_timeout;
Daniel Stoneb57c6a02017-10-05 16:27:21 +0100287
288 bool shutting_down;
Ankit Nautiyala21c3932097-03-19 00:24:57 +0530289
290 bool aspect_ratio_supported;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400291};
292
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400293struct drm_mode {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500294 struct weston_mode base;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400295 drmModeModeInfo mode_info;
Daniel Stoned5526cb2016-11-16 10:54:10 +0000296 uint32_t blob_id;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400297};
298
Daniel Stonefc175a72017-04-04 17:54:22 +0100299enum drm_fb_type {
300 BUFFER_INVALID = 0, /**< never used */
301 BUFFER_CLIENT, /**< directly sourced from client */
302 BUFFER_PIXMAN_DUMB, /**< internal Pixman rendering */
303 BUFFER_GBM_SURFACE, /**< internal EGL rendering */
Daniel Stonee4256832017-04-04 17:54:27 +0100304 BUFFER_CURSOR, /**< internal cursor buffer */
Daniel Stonefc175a72017-04-04 17:54:22 +0100305};
306
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300307struct drm_fb {
Daniel Stonefc175a72017-04-04 17:54:22 +0100308 enum drm_fb_type type;
309
Daniel Stone6e7a9612017-04-04 17:54:26 +0100310 int refcnt;
311
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200312 uint32_t fb_id, stride, handle, size;
Daniel Stone0b70fa42017-04-04 17:54:23 +0100313 const struct pixel_format_info *format;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000314 int width, height;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200315 int fd;
Pekka Paalanende685b82012-12-04 15:58:12 +0200316 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200317
318 /* Used by gbm fbs */
319 struct gbm_bo *bo;
Daniel Stone05a5ac22017-04-04 17:54:25 +0100320 struct gbm_surface *gbm_surface;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200321
322 /* Used by dumb fbs */
323 void *map;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300324};
325
Richard Hughes2b2092a2013-04-24 14:58:02 +0100326struct drm_edid {
327 char eisa_id[13];
328 char monitor_name[13];
329 char pnp_id[5];
330 char serial_number[13];
331};
332
Daniel Stone08d4edf2017-04-04 17:54:34 +0100333/**
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000334 * Pending state holds one or more drm_output_state structures, collected from
335 * performing repaint. This pending state is transient, and only lives between
336 * beginning a repaint group and flushing the results: after flush, each
337 * output state will complete and be retired separately.
338 */
339struct drm_pending_state {
340 struct drm_backend *backend;
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000341 struct wl_list output_list;
342};
343
344/*
345 * Output state holds the dynamic state for one Weston output, i.e. a KMS CRTC,
346 * plus >= 1 each of encoder/connector/plane. Since everything but the planes
347 * is currently statically assigned per-output, we mainly use this to track
348 * plane state.
349 *
350 * pending_state is set when the output state is owned by a pending_state,
351 * i.e. when it is being constructed and has not yet been applied. When the
352 * output state has been applied, the owning pending_state is freed.
353 */
354struct drm_output_state {
355 struct drm_pending_state *pending_state;
356 struct drm_output *output;
357 struct wl_list link;
Daniel Stonea08512f2016-11-08 17:46:10 +0000358 enum dpms_enum dpms;
Daniel Stonebc15f682016-11-14 16:57:01 +0000359 struct wl_list plane_list;
360};
361
362/**
363 * Plane state holds the dynamic state for a plane: where it is positioned,
364 * and which buffer it is currently displaying.
365 *
366 * The plane state is owned by an output state, except when setting an initial
367 * state. See drm_output_state for notes on state object lifetime.
368 */
369struct drm_plane_state {
370 struct drm_plane *plane;
371 struct drm_output *output;
372 struct drm_output_state *output_state;
373
374 struct drm_fb *fb;
375
376 int32_t src_x, src_y;
377 uint32_t src_w, src_h;
378 int32_t dest_x, dest_y;
379 uint32_t dest_w, dest_h;
380
381 bool complete;
382
383 struct wl_list link; /* drm_output_state::plane_list */
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000384};
385
386/**
Daniel Stone08d4edf2017-04-04 17:54:34 +0100387 * A plane represents one buffer, positioned within a CRTC, and stacked
388 * relative to other planes on the same CRTC.
389 *
390 * Each CRTC has a 'primary plane', which use used to display the classic
391 * framebuffer contents, as accessed through the legacy drmModeSetCrtc
392 * call (which combines setting the CRTC's actual physical mode, and the
393 * properties of the primary plane).
394 *
395 * The cursor plane also has its own alternate legacy API.
396 *
397 * Other planes are used opportunistically to display content we do not
398 * wish to blit into the primary plane. These non-primary/cursor planes
399 * are referred to as 'sprites'.
400 */
401struct drm_plane {
Daniel Stone08d4edf2017-04-04 17:54:34 +0100402 struct weston_plane base;
403
Daniel Stone08d4edf2017-04-04 17:54:34 +0100404 struct drm_backend *backend;
405
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100406 enum wdrm_plane_type type;
407
Daniel Stone08d4edf2017-04-04 17:54:34 +0100408 uint32_t possible_crtcs;
409 uint32_t plane_id;
410 uint32_t count_formats;
411
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100412 struct drm_property_info props[WDRM_PLANE__COUNT];
413
Daniel Stonebc15f682016-11-14 16:57:01 +0000414 /* The last state submitted to the kernel for this plane. */
415 struct drm_plane_state *state_cur;
Daniel Stone08d4edf2017-04-04 17:54:34 +0100416
Daniel Stonebc15f682016-11-14 16:57:01 +0000417 struct wl_list link;
Daniel Stone08d4edf2017-04-04 17:54:34 +0100418
419 uint32_t formats[];
420};
421
Pekka Paalanenc112f002017-08-28 16:27:20 +0300422struct drm_head {
423 struct weston_head base;
424 struct drm_backend *backend;
425
Armin Krezović08368132016-09-30 14:11:05 +0200426 drmModeConnector *connector;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400427 uint32_t connector_id;
Richard Hughes2b2092a2013-04-24 14:58:02 +0100428 struct drm_edid edid;
Daniel Stone02cf4662017-03-03 16:19:39 +0000429
430 /* Holds the properties for the connector */
431 struct drm_property_info props_conn[WDRM_CONNECTOR__COUNT];
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +0300432
433 struct backlight *backlight;
Pekka Paalanen13d233e2017-09-11 14:06:11 +0300434
435 drmModeModeInfo inherited_mode; /**< Original mode on the connector */
Pekka Paalanen27cc4812017-11-20 13:31:06 +0200436 uint32_t inherited_crtc_id; /**< Original CRTC assignment */
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +0300437};
438
439struct drm_output {
440 struct weston_output base;
441
442 uint32_t crtc_id; /* object ID to pass to DRM functions */
443 int pipe; /* index of CRTC in resource array / bitmasks */
444
Pekka Paalanencd011a62016-11-15 22:07:49 +0000445 /* Holds the properties for the CRTC */
446 struct drm_property_info props_crtc[WDRM_CRTC__COUNT];
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200447
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300448 int vblank_pending;
449 int page_flip_pending;
Daniel Stone598ee9d2016-11-16 11:55:20 +0000450 int atomic_complete_pending;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800451 int destroy_pending;
Armin Krezović08368132016-09-30 14:11:05 +0200452 int disable_pending;
Daniel Stonea08512f2016-11-08 17:46:10 +0000453 int dpms_off_pending;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300454
Daniel Stonee4256832017-04-04 17:54:27 +0100455 struct drm_fb *gbm_cursor_fb[2];
Daniel Stone2ba17f42015-05-19 20:02:41 +0100456 struct drm_plane *cursor_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500457 struct weston_view *cursor_view;
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400458 int current_cursor;
Daniel Stone5bb8f582017-04-04 17:54:28 +0100459
460 struct gbm_surface *gbm_surface;
461 uint32_t gbm_format;
462
Daniel Stonee2e80132018-01-16 15:37:33 +0000463 /* Plane being displayed directly on the CRTC */
464 struct drm_plane *scanout_plane;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200465
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000466 /* The last state submitted to the kernel for this CRTC. */
467 struct drm_output_state *state_cur;
468 /* The previously-submitted state, where the hardware has not
469 * yet acknowledged completion of state_cur. */
470 struct drm_output_state *state_last;
471
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200472 struct drm_fb *dumb[2];
473 pixman_image_t *image[2];
474 int current_image;
475 pixman_region32_t previous_damage;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300476
477 struct vaapi_recorder *recorder;
478 struct wl_listener recorder_frame_listener;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000479
480 struct wl_event_source *pageflip_timer;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400481};
482
Ankit Nautiyala21c3932097-03-19 00:24:57 +0530483static const char *const aspect_ratio_as_string[] = {
484 [WESTON_MODE_PIC_AR_NONE] = "",
485 [WESTON_MODE_PIC_AR_4_3] = " 4:3",
486 [WESTON_MODE_PIC_AR_16_9] = " 16:9",
487 [WESTON_MODE_PIC_AR_64_27] = " 64:27",
488 [WESTON_MODE_PIC_AR_256_135] = " 256:135",
489};
490
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300491static struct gl_renderer_interface *gl_renderer;
492
Kristian Høgsberg98cfea62013-02-18 16:15:53 -0500493static const char default_seat[] = "seat0";
Pekka Paalanen33156972012-08-03 13:30:30 -0400494
Daniel Stone087ddf02017-02-14 17:51:30 +0000495static void
496wl_array_remove_uint32(struct wl_array *array, uint32_t elm)
497{
498 uint32_t *pos, *end;
499
500 end = (uint32_t *) ((char *) array->data + array->size);
501
502 wl_array_for_each(pos, array) {
503 if (*pos != elm)
504 continue;
505
506 array->size -= sizeof(*pos);
507 if (pos + 1 == end)
508 break;
509
510 memmove(pos, pos + 1, (char *) end - (char *) (pos + 1));
511 break;
512 }
513}
514
Pekka Paalanenc112f002017-08-28 16:27:20 +0300515static inline struct drm_head *
516to_drm_head(struct weston_head *base)
517{
518 return container_of(base, struct drm_head, base);
519}
520
Armin Krezović545dba62016-08-05 15:54:18 +0200521static inline struct drm_output *
522to_drm_output(struct weston_output *base)
523{
524 return container_of(base, struct drm_output, base);
525}
526
527static inline struct drm_backend *
528to_drm_backend(struct weston_compositor *base)
529{
530 return container_of(base->backend, struct drm_backend, base);
531}
532
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000533static int
534pageflip_timeout(void *data) {
535 /*
536 * Our timer just went off, that means we're not receiving drm
537 * page flip events anymore for that output. Let's gracefully exit
538 * weston with a return value so devs can debug what's going on.
539 */
540 struct drm_output *output = data;
541 struct weston_compositor *compositor = output->base.compositor;
542
543 weston_log("Pageflip timeout reached on output %s, your "
544 "driver is probably buggy! Exiting.\n",
545 output->base.name);
546 weston_compositor_exit_with_code(compositor, EXIT_FAILURE);
547
548 return 0;
549}
550
551/* Creates the pageflip timer. Note that it isn't armed by default */
552static int
553drm_output_pageflip_timer_create(struct drm_output *output)
554{
555 struct wl_event_loop *loop = NULL;
556 struct weston_compositor *ec = output->base.compositor;
557
558 loop = wl_display_get_event_loop(ec->wl_display);
559 assert(loop);
560 output->pageflip_timer = wl_event_loop_add_timer(loop,
561 pageflip_timeout,
562 output);
563
564 if (output->pageflip_timer == NULL) {
565 weston_log("creating drm pageflip timer failed: %m\n");
566 return -1;
567 }
568
569 return 0;
570}
571
Daniel Stonecb04cc42016-11-16 11:51:27 +0000572static inline struct drm_mode *
573to_drm_mode(struct weston_mode *base)
574{
575 return container_of(base, struct drm_mode, base);
576}
577
Daniel Stone02cf4662017-03-03 16:19:39 +0000578/**
579 * Get the current value of a KMS property
580 *
581 * Given a drmModeObjectGetProperties return, as well as the drm_property_info
582 * for the target property, return the current value of that property,
583 * with an optional default. If the property is a KMS enum type, the return
584 * value will be translated into the appropriate internal enum.
585 *
586 * If the property is not present, the default value will be returned.
587 *
588 * @param info Internal structure for property to look up
589 * @param props Raw KMS properties for the target object
590 * @param def Value to return if property is not found
591 */
592static uint64_t
593drm_property_get_value(struct drm_property_info *info,
Daniel Stone85eebdf2018-07-05 17:55:43 +0100594 const drmModeObjectProperties *props,
Daniel Stone02cf4662017-03-03 16:19:39 +0000595 uint64_t def)
596{
597 unsigned int i;
598
599 if (info->prop_id == 0)
600 return def;
601
602 for (i = 0; i < props->count_props; i++) {
603 unsigned int j;
604
605 if (props->props[i] != info->prop_id)
606 continue;
607
608 /* Simple (non-enum) types can return the value directly */
609 if (info->num_enum_values == 0)
610 return props->prop_values[i];
611
612 /* Map from raw value to enum value */
613 for (j = 0; j < info->num_enum_values; j++) {
614 if (!info->enum_values[j].valid)
615 continue;
616 if (info->enum_values[j].value != props->prop_values[i])
617 continue;
618
619 return j;
620 }
621
622 /* We don't have a mapping for this enum; return default. */
623 break;
624 }
625
626 return def;
627}
628
629/**
630 * Cache DRM property values
631 *
632 * Update a per-object array of drm_property_info structures, given the
633 * DRM properties of the object.
634 *
635 * Call this every time an object newly appears (note that only connectors
636 * can be hotplugged), the first time it is seen, or when its status changes
637 * in a way which invalidates the potential property values (currently, the
638 * only case for this is connector hotplug).
639 *
640 * This updates the property IDs and enum values within the drm_property_info
641 * array.
642 *
643 * DRM property enum values are dynamic at runtime; the user must query the
644 * property to find out the desired runtime value for a requested string
645 * name. Using the 'type' field on planes as an example, there is no single
646 * hardcoded constant for primary plane types; instead, the property must be
647 * queried at runtime to find the value associated with the string "Primary".
648 *
649 * This helper queries and caches the enum values, to allow us to use a set
650 * of compile-time-constant enums portably across various implementations.
651 * The values given in enum_names are searched for, and stored in the
652 * same-indexed field of the map array.
653 *
654 * @param b DRM backend object
655 * @param src DRM property info array to source from
656 * @param info DRM property info array to copy into
657 * @param num_infos Number of entries in the source array
658 * @param props DRM object properties for the object
659 */
660static void
661drm_property_info_populate(struct drm_backend *b,
662 const struct drm_property_info *src,
663 struct drm_property_info *info,
664 unsigned int num_infos,
665 drmModeObjectProperties *props)
666{
667 drmModePropertyRes *prop;
668 unsigned i, j;
669
670 for (i = 0; i < num_infos; i++) {
671 unsigned int j;
672
673 info[i].name = src[i].name;
674 info[i].prop_id = 0;
675 info[i].num_enum_values = src[i].num_enum_values;
676
677 if (src[i].num_enum_values == 0)
678 continue;
679
680 info[i].enum_values =
681 malloc(src[i].num_enum_values *
682 sizeof(*info[i].enum_values));
683 assert(info[i].enum_values);
684 for (j = 0; j < info[i].num_enum_values; j++) {
685 info[i].enum_values[j].name = src[i].enum_values[j].name;
686 info[i].enum_values[j].valid = false;
687 }
688 }
689
690 for (i = 0; i < props->count_props; i++) {
691 unsigned int k;
692
693 prop = drmModeGetProperty(b->drm.fd, props->props[i]);
694 if (!prop)
695 continue;
696
697 for (j = 0; j < num_infos; j++) {
698 if (!strcmp(prop->name, info[j].name))
699 break;
700 }
701
702 /* We don't know/care about this property. */
703 if (j == num_infos) {
704#ifdef DEBUG
705 weston_log("DRM debug: unrecognized property %u '%s'\n",
706 prop->prop_id, prop->name);
707#endif
708 drmModeFreeProperty(prop);
709 continue;
710 }
711
712 if (info[j].num_enum_values == 0 &&
713 (prop->flags & DRM_MODE_PROP_ENUM)) {
714 weston_log("DRM: expected property %s to not be an"
715 " enum, but it is; ignoring\n", prop->name);
716 drmModeFreeProperty(prop);
717 continue;
718 }
719
720 info[j].prop_id = props->props[i];
721
722 if (info[j].num_enum_values == 0) {
723 drmModeFreeProperty(prop);
724 continue;
725 }
726
727 if (!(prop->flags & DRM_MODE_PROP_ENUM)) {
728 weston_log("DRM: expected property %s to be an enum,"
729 " but it is not; ignoring\n", prop->name);
730 drmModeFreeProperty(prop);
731 info[j].prop_id = 0;
732 continue;
733 }
734
735 for (k = 0; k < info[j].num_enum_values; k++) {
736 int l;
737
738 for (l = 0; l < prop->count_enums; l++) {
739 if (!strcmp(prop->enums[l].name,
740 info[j].enum_values[k].name))
741 break;
742 }
743
744 if (l == prop->count_enums)
745 continue;
746
747 info[j].enum_values[k].valid = true;
748 info[j].enum_values[k].value = prop->enums[l].value;
749 }
750
751 drmModeFreeProperty(prop);
752 }
753
754#ifdef DEBUG
755 for (i = 0; i < num_infos; i++) {
756 if (info[i].prop_id == 0)
757 weston_log("DRM warning: property '%s' missing\n",
758 info[i].name);
759 }
760#endif
761}
762
763/**
764 * Free DRM property information
765 *
Pekka Paalanen46e4f972017-09-07 15:32:01 +0300766 * Frees all memory associated with a DRM property info array and zeroes
767 * it out, leaving it usable for a further drm_property_info_update() or
768 * drm_property_info_free().
Daniel Stone02cf4662017-03-03 16:19:39 +0000769 *
770 * @param info DRM property info array
771 * @param num_props Number of entries in array to free
772 */
773static void
774drm_property_info_free(struct drm_property_info *info, int num_props)
775{
776 int i;
777
778 for (i = 0; i < num_props; i++)
779 free(info[i].enum_values);
Pekka Paalanen46e4f972017-09-07 15:32:01 +0300780
781 memset(info, 0, sizeof(*info) * num_props);
Daniel Stone02cf4662017-03-03 16:19:39 +0000782}
783
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400784static void
Daniel Stone2ba17f42015-05-19 20:02:41 +0100785drm_output_set_cursor(struct drm_output_state *output_state);
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400786
Mario Kleinerf507ec32015-06-21 21:25:14 +0200787static void
788drm_output_update_msc(struct drm_output *output, unsigned int seq);
789
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000790static void
791drm_output_destroy(struct weston_output *output_base);
792
Daniel Stone5ff289a2017-10-07 12:59:02 +0100793/**
794 * Returns true if the plane can be used on the given output for its current
795 * repaint cycle.
796 */
797static bool
798drm_plane_is_available(struct drm_plane *plane, struct drm_output *output)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500799{
Daniel Stone5ff289a2017-10-07 12:59:02 +0100800 assert(plane->state_cur);
801
802 /* The plane still has a request not yet completed by the kernel. */
803 if (!plane->state_cur->complete)
804 return false;
805
806 /* The plane is still active on another output. */
807 if (plane->state_cur->output && plane->state_cur->output != output)
808 return false;
809
810 /* Check whether the plane can be used with this CRTC; possible_crtcs
811 * is a bitmask of CRTC indices (pipe), rather than CRTC object ID. */
Daniel Stone08d4edf2017-04-04 17:54:34 +0100812 return !!(plane->possible_crtcs & (1 << output->pipe));
Jesse Barnes58ef3792012-02-23 09:45:49 -0500813}
814
Daniel Stone72c0e1b2017-02-09 13:49:15 +0000815static struct drm_output *
816drm_output_find_by_crtc(struct drm_backend *b, uint32_t crtc_id)
817{
818 struct drm_output *output;
819
820 wl_list_for_each(output, &b->compositor->output_list, base.link) {
821 if (output->crtc_id == crtc_id)
822 return output;
823 }
824
Daniel Stone72c0e1b2017-02-09 13:49:15 +0000825 return NULL;
826}
827
Pekka Paalanen54cc47c2017-08-31 11:58:41 +0300828static struct drm_head *
829drm_head_find_by_connector(struct drm_backend *backend, uint32_t connector_id)
830{
831 struct weston_head *base;
832 struct drm_head *head;
833
834 wl_list_for_each(base,
835 &backend->compositor->head_list, compositor_link) {
836 head = to_drm_head(base);
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +0300837 if (head->connector_id == connector_id)
Pekka Paalanen54cc47c2017-08-31 11:58:41 +0300838 return head;
839 }
840
841 return NULL;
842}
843
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300844static void
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100845drm_fb_destroy(struct drm_fb *fb)
846{
847 if (fb->fb_id != 0)
848 drmModeRmFB(fb->fd, fb->fb_id);
849 weston_buffer_reference(&fb->buffer_ref, NULL);
850 free(fb);
851}
852
853static void
854drm_fb_destroy_dumb(struct drm_fb *fb)
855{
856 struct drm_mode_destroy_dumb destroy_arg;
857
858 assert(fb->type == BUFFER_PIXMAN_DUMB);
859
860 if (fb->map && fb->size > 0)
861 munmap(fb->map, fb->size);
862
863 memset(&destroy_arg, 0, sizeof(destroy_arg));
864 destroy_arg.handle = fb->handle;
865 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
866
867 drm_fb_destroy(fb);
868}
869
870static void
871drm_fb_destroy_gbm(struct gbm_bo *bo, void *data)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300872{
873 struct drm_fb *fb = data;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300874
Daniel Stonee4256832017-04-04 17:54:27 +0100875 assert(fb->type == BUFFER_GBM_SURFACE || fb->type == BUFFER_CLIENT ||
876 fb->type == BUFFER_CURSOR);
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100877 drm_fb_destroy(fb);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300878}
879
880static struct drm_fb *
Daniel Stonef214fdc2016-11-14 17:43:57 +0000881drm_fb_create_dumb(struct drm_backend *b, int width, int height,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300882 uint32_t format)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200883{
884 struct drm_fb *fb;
885 int ret;
886
887 struct drm_mode_create_dumb create_arg;
888 struct drm_mode_destroy_dumb destroy_arg;
889 struct drm_mode_map_dumb map_arg;
Daniel Stone1de42522016-11-17 18:17:16 +0000890 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200891
Peter Huttererf3d62272013-08-08 11:57:05 +1000892 fb = zalloc(sizeof *fb);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200893 if (!fb)
894 return NULL;
895
Daniel Stone6e7a9612017-04-04 17:54:26 +0100896 fb->refcnt = 1;
897
Daniel Stone0b70fa42017-04-04 17:54:23 +0100898 fb->format = pixel_format_get_info(format);
899 if (!fb->format) {
900 weston_log("failed to look up format 0x%lx\n",
901 (unsigned long) format);
902 goto err_fb;
903 }
904
905 if (!fb->format->depth || !fb->format->bpp) {
906 weston_log("format 0x%lx is not compatible with dumb buffers\n",
907 (unsigned long) format);
908 goto err_fb;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300909 }
910
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700911 memset(&create_arg, 0, sizeof create_arg);
Daniel Stone0b70fa42017-04-04 17:54:23 +0100912 create_arg.bpp = fb->format->bpp;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200913 create_arg.width = width;
914 create_arg.height = height;
915
Giulio Camuffo954f1832014-10-11 18:27:30 +0300916 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200917 if (ret)
918 goto err_fb;
919
Daniel Stonefc175a72017-04-04 17:54:22 +0100920 fb->type = BUFFER_PIXMAN_DUMB;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200921 fb->handle = create_arg.handle;
922 fb->stride = create_arg.pitch;
923 fb->size = create_arg.size;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000924 fb->width = width;
925 fb->height = height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300926 fb->fd = b->drm.fd;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200927
Daniel Stone1de42522016-11-17 18:17:16 +0000928 handles[0] = fb->handle;
929 pitches[0] = fb->stride;
930 offsets[0] = 0;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300931
Daniel Stone1de42522016-11-17 18:17:16 +0000932 ret = drmModeAddFB2(b->drm.fd, width, height, fb->format->format,
933 handles, pitches, offsets, &fb->fb_id, 0);
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300934 if (ret) {
Daniel Stone0b70fa42017-04-04 17:54:23 +0100935 ret = drmModeAddFB(b->drm.fd, width, height,
936 fb->format->depth, fb->format->bpp,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300937 fb->stride, fb->handle, &fb->fb_id);
938 }
939
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200940 if (ret)
941 goto err_bo;
942
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700943 memset(&map_arg, 0, sizeof map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200944 map_arg.handle = fb->handle;
Chris Michaeleb2074a2013-05-01 21:26:02 -0400945 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200946 if (ret)
947 goto err_add_fb;
948
Chris Michael4a7ce1f2015-11-10 10:40:37 -0500949 fb->map = mmap(NULL, fb->size, PROT_WRITE,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300950 MAP_SHARED, b->drm.fd, map_arg.offset);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200951 if (fb->map == MAP_FAILED)
952 goto err_add_fb;
953
954 return fb;
955
956err_add_fb:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300957 drmModeRmFB(b->drm.fd, fb->fb_id);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200958err_bo:
959 memset(&destroy_arg, 0, sizeof(destroy_arg));
960 destroy_arg.handle = create_arg.handle;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300961 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200962err_fb:
963 free(fb);
964 return NULL;
965}
966
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200967static struct drm_fb *
Daniel Stone6e7a9612017-04-04 17:54:26 +0100968drm_fb_ref(struct drm_fb *fb)
969{
970 fb->refcnt++;
971 return fb;
972}
973
974static struct drm_fb *
Daniel Stonefc175a72017-04-04 17:54:22 +0100975drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend,
976 uint32_t format, enum drm_fb_type type)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300977{
978 struct drm_fb *fb = gbm_bo_get_user_data(bo);
Derek Foreman482ffdf2016-07-08 12:50:57 -0500979 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300980 int ret;
981
Daniel Stonefc175a72017-04-04 17:54:22 +0100982 if (fb) {
983 assert(fb->type == type);
Daniel Stone6e7a9612017-04-04 17:54:26 +0100984 return drm_fb_ref(fb);
Daniel Stonefc175a72017-04-04 17:54:22 +0100985 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300986
Daniel Stone1de42522016-11-17 18:17:16 +0000987 assert(format != 0);
988
Bryce Harringtonde16d892014-11-20 22:21:57 -0800989 fb = zalloc(sizeof *fb);
990 if (fb == NULL)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200991 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300992
Daniel Stonefc175a72017-04-04 17:54:22 +0100993 fb->type = type;
Daniel Stone6e7a9612017-04-04 17:54:26 +0100994 fb->refcnt = 1;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300995 fb->bo = bo;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300996
Daniel Stonec8c917c2016-11-14 17:45:58 +0000997 fb->width = gbm_bo_get_width(bo);
998 fb->height = gbm_bo_get_height(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200999 fb->stride = gbm_bo_get_stride(bo);
1000 fb->handle = gbm_bo_get_handle(bo).u32;
Daniel Stone0b70fa42017-04-04 17:54:23 +01001001 fb->format = pixel_format_get_info(format);
Daniel Stonec8c917c2016-11-14 17:45:58 +00001002 fb->size = fb->stride * fb->height;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001003 fb->fd = backend->drm.fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001004
Daniel Stone0b70fa42017-04-04 17:54:23 +01001005 if (!fb->format) {
1006 weston_log("couldn't look up format 0x%lx\n",
1007 (unsigned long) format);
1008 goto err_free;
1009 }
1010
Daniel Stonec8c917c2016-11-14 17:45:58 +00001011 if (backend->min_width > fb->width ||
1012 fb->width > backend->max_width ||
1013 backend->min_height > fb->height ||
1014 fb->height > backend->max_height) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001015 weston_log("bo geometry out of bounds\n");
1016 goto err_free;
1017 }
1018
Daniel Stone1de42522016-11-17 18:17:16 +00001019 handles[0] = fb->handle;
1020 pitches[0] = fb->stride;
1021 offsets[0] = 0;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001022
Daniel Stone1de42522016-11-17 18:17:16 +00001023 ret = drmModeAddFB2(backend->drm.fd, fb->width, fb->height,
1024 fb->format->format, handles, pitches, offsets,
1025 &fb->fb_id, 0);
Daniel Stone0b70fa42017-04-04 17:54:23 +01001026 if (ret && fb->format->depth && fb->format->bpp)
Daniel Stonec8c917c2016-11-14 17:45:58 +00001027 ret = drmModeAddFB(backend->drm.fd, fb->width, fb->height,
Daniel Stone0b70fa42017-04-04 17:54:23 +01001028 fb->format->depth, fb->format->bpp,
1029 fb->stride, fb->handle, &fb->fb_id);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001030
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001031 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +02001032 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001033 goto err_free;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001034 }
1035
Tomohito Esaki576f42e2017-04-04 17:54:24 +01001036 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_gbm);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001037
1038 return fb;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001039
1040err_free:
1041 free(fb);
1042 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001043}
1044
1045static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001046drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001047{
Pekka Paalanende685b82012-12-04 15:58:12 +02001048 assert(fb->buffer_ref.buffer == NULL);
Daniel Stonefc175a72017-04-04 17:54:22 +01001049 assert(fb->type == BUFFER_CLIENT);
Pekka Paalanende685b82012-12-04 15:58:12 +02001050 weston_buffer_reference(&fb->buffer_ref, buffer);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001051}
1052
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02001053static void
Daniel Stone05a5ac22017-04-04 17:54:25 +01001054drm_fb_unref(struct drm_fb *fb)
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02001055{
1056 if (!fb)
1057 return;
1058
Daniel Stone6e7a9612017-04-04 17:54:26 +01001059 assert(fb->refcnt > 0);
1060 if (--fb->refcnt > 0)
1061 return;
1062
Daniel Stonefc175a72017-04-04 17:54:22 +01001063 switch (fb->type) {
1064 case BUFFER_PIXMAN_DUMB:
Daniel Stone6e7a9612017-04-04 17:54:26 +01001065 drm_fb_destroy_dumb(fb);
Daniel Stonefc175a72017-04-04 17:54:22 +01001066 break;
Daniel Stonee4256832017-04-04 17:54:27 +01001067 case BUFFER_CURSOR:
Daniel Stonefc175a72017-04-04 17:54:22 +01001068 case BUFFER_CLIENT:
1069 gbm_bo_destroy(fb->bo);
1070 break;
1071 case BUFFER_GBM_SURFACE:
Daniel Stone05a5ac22017-04-04 17:54:25 +01001072 gbm_surface_release_buffer(fb->gbm_surface, fb->bo);
Daniel Stonefc175a72017-04-04 17:54:22 +01001073 break;
1074 default:
1075 assert(NULL);
1076 break;
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02001077 }
1078}
1079
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001080/**
Daniel Stonebc15f682016-11-14 16:57:01 +00001081 * Allocate a new, empty, plane state.
1082 */
1083static struct drm_plane_state *
1084drm_plane_state_alloc(struct drm_output_state *state_output,
1085 struct drm_plane *plane)
1086{
1087 struct drm_plane_state *state = zalloc(sizeof(*state));
1088
1089 assert(state);
1090 state->output_state = state_output;
1091 state->plane = plane;
1092
1093 /* Here we only add the plane state to the desired link, and not
1094 * set the member. Having an output pointer set means that the
1095 * plane will be displayed on the output; this won't be the case
1096 * when we go to disable a plane. In this case, it must be part of
1097 * the commit (and thus the output state), but the member must be
1098 * NULL, as it will not be on any output when the state takes
1099 * effect.
1100 */
1101 if (state_output)
1102 wl_list_insert(&state_output->plane_list, &state->link);
1103 else
1104 wl_list_init(&state->link);
1105
1106 return state;
1107}
1108
1109/**
1110 * Free an existing plane state. As a special case, the state will not
1111 * normally be freed if it is the current state; see drm_plane_set_state.
1112 */
1113static void
1114drm_plane_state_free(struct drm_plane_state *state, bool force)
1115{
1116 if (!state)
1117 return;
1118
1119 wl_list_remove(&state->link);
1120 wl_list_init(&state->link);
1121 state->output_state = NULL;
1122
1123 if (force || state != state->plane->state_cur) {
1124 drm_fb_unref(state->fb);
1125 free(state);
1126 }
1127}
1128
1129/**
1130 * Duplicate an existing plane state into a new plane state, storing it within
1131 * the given output state. If the output state already contains a plane state
1132 * for the drm_plane referenced by 'src', that plane state is freed first.
1133 */
1134static struct drm_plane_state *
1135drm_plane_state_duplicate(struct drm_output_state *state_output,
1136 struct drm_plane_state *src)
1137{
1138 struct drm_plane_state *dst = malloc(sizeof(*dst));
1139 struct drm_plane_state *old, *tmp;
1140
1141 assert(src);
1142 assert(dst);
1143 *dst = *src;
1144 wl_list_init(&dst->link);
1145
1146 wl_list_for_each_safe(old, tmp, &state_output->plane_list, link) {
1147 /* Duplicating a plane state into the same output state, so
1148 * it can replace itself with an identical copy of itself,
1149 * makes no sense. */
1150 assert(old != src);
1151 if (old->plane == dst->plane)
1152 drm_plane_state_free(old, false);
1153 }
1154
1155 wl_list_insert(&state_output->plane_list, &dst->link);
1156 if (src->fb)
1157 dst->fb = drm_fb_ref(src->fb);
1158 dst->output_state = state_output;
1159 dst->complete = false;
1160
1161 return dst;
1162}
1163
1164/**
1165 * Remove a plane state from an output state; if the plane was previously
1166 * enabled, then replace it with a disabling state. This ensures that the
1167 * output state was untouched from it was before the plane state was
1168 * modified by the caller of this function.
1169 *
1170 * This is required as drm_output_state_get_plane may either allocate a
1171 * new plane state, in which case this function will just perform a matching
1172 * drm_plane_state_free, or it may instead repurpose an existing disabling
1173 * state (if the plane was previously active), in which case this function
1174 * will reset it.
1175 */
1176static void
1177drm_plane_state_put_back(struct drm_plane_state *state)
1178{
1179 struct drm_output_state *state_output;
1180 struct drm_plane *plane;
1181
1182 if (!state)
1183 return;
1184
1185 state_output = state->output_state;
1186 plane = state->plane;
1187 drm_plane_state_free(state, false);
1188
1189 /* Plane was previously disabled; no need to keep this temporary
1190 * state around. */
1191 if (!plane->state_cur->fb)
1192 return;
1193
1194 (void) drm_plane_state_alloc(state_output, plane);
1195}
1196
1197/**
Daniel Stone2ba17f42015-05-19 20:02:41 +01001198 * Return a plane state from a drm_output_state.
1199 */
1200static struct drm_plane_state *
1201drm_output_state_get_existing_plane(struct drm_output_state *state_output,
1202 struct drm_plane *plane)
1203{
1204 struct drm_plane_state *ps;
1205
1206 wl_list_for_each(ps, &state_output->plane_list, link) {
1207 if (ps->plane == plane)
1208 return ps;
1209 }
1210
1211 return NULL;
1212}
1213
1214/**
Daniel Stonebc15f682016-11-14 16:57:01 +00001215 * Return a plane state from a drm_output_state, either existing or
1216 * freshly allocated.
1217 */
1218static struct drm_plane_state *
1219drm_output_state_get_plane(struct drm_output_state *state_output,
1220 struct drm_plane *plane)
1221{
1222 struct drm_plane_state *ps;
1223
Daniel Stone2ba17f42015-05-19 20:02:41 +01001224 ps = drm_output_state_get_existing_plane(state_output, plane);
1225 if (ps)
1226 return ps;
Daniel Stonebc15f682016-11-14 16:57:01 +00001227
1228 return drm_plane_state_alloc(state_output, plane);
1229}
1230
1231/**
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001232 * Allocate a new, empty drm_output_state. This should not generally be used
1233 * in the repaint cycle; see drm_output_state_duplicate.
1234 */
1235static struct drm_output_state *
1236drm_output_state_alloc(struct drm_output *output,
1237 struct drm_pending_state *pending_state)
Daniel Stone90648872016-10-21 18:08:37 +01001238{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001239 struct drm_output_state *state = zalloc(sizeof(*state));
1240
1241 assert(state);
1242 state->output = output;
Daniel Stonea08512f2016-11-08 17:46:10 +00001243 state->dpms = WESTON_DPMS_OFF;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001244 state->pending_state = pending_state;
1245 if (pending_state)
1246 wl_list_insert(&pending_state->output_list, &state->link);
1247 else
1248 wl_list_init(&state->link);
1249
Daniel Stonebc15f682016-11-14 16:57:01 +00001250 wl_list_init(&state->plane_list);
1251
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001252 return state;
1253}
1254
1255/**
1256 * Duplicate an existing drm_output_state into a new one. This is generally
1257 * used during the repaint cycle, to capture the existing state of an output
1258 * and modify it to create a new state to be used.
1259 *
1260 * The mode determines whether the output will be reset to an a blank state,
1261 * or an exact mirror of the current state.
1262 */
1263static struct drm_output_state *
1264drm_output_state_duplicate(struct drm_output_state *src,
1265 struct drm_pending_state *pending_state,
1266 enum drm_output_state_duplicate_mode plane_mode)
1267{
1268 struct drm_output_state *dst = malloc(sizeof(*dst));
Daniel Stonebc15f682016-11-14 16:57:01 +00001269 struct drm_plane_state *ps;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001270
1271 assert(dst);
1272
1273 /* Copy the whole structure, then individually modify the
1274 * pending_state, as well as the list link into our pending
1275 * state. */
1276 *dst = *src;
1277
1278 dst->pending_state = pending_state;
1279 if (pending_state)
1280 wl_list_insert(&pending_state->output_list, &dst->link);
1281 else
1282 wl_list_init(&dst->link);
1283
Daniel Stonebc15f682016-11-14 16:57:01 +00001284 wl_list_init(&dst->plane_list);
1285
1286 wl_list_for_each(ps, &src->plane_list, link) {
1287 /* Don't carry planes which are now disabled; these should be
1288 * free for other outputs to reuse. */
1289 if (!ps->output)
1290 continue;
1291
1292 if (plane_mode == DRM_OUTPUT_STATE_CLEAR_PLANES)
1293 (void) drm_plane_state_alloc(dst, ps->plane);
1294 else
1295 (void) drm_plane_state_duplicate(dst, ps);
1296 }
1297
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001298 return dst;
1299}
1300
1301/**
1302 * Free an unused drm_output_state.
1303 */
1304static void
1305drm_output_state_free(struct drm_output_state *state)
1306{
Daniel Stonebc15f682016-11-14 16:57:01 +00001307 struct drm_plane_state *ps, *next;
1308
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001309 if (!state)
1310 return;
1311
Daniel Stonebc15f682016-11-14 16:57:01 +00001312 wl_list_for_each_safe(ps, next, &state->plane_list, link)
1313 drm_plane_state_free(ps, false);
1314
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001315 wl_list_remove(&state->link);
Daniel Stonebc15f682016-11-14 16:57:01 +00001316
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001317 free(state);
Daniel Stone90648872016-10-21 18:08:37 +01001318}
1319
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001320/**
Daniel Stonea08512f2016-11-08 17:46:10 +00001321 * Get output state to disable output
1322 *
1323 * Returns a pointer to an output_state object which can be used to disable
1324 * an output (e.g. DPMS off).
1325 *
1326 * @param pending_state The pending state object owning this update
1327 * @param output The output to disable
1328 * @returns A drm_output_state to disable the output
1329 */
1330static struct drm_output_state *
1331drm_output_get_disable_state(struct drm_pending_state *pending_state,
1332 struct drm_output *output)
1333{
1334 struct drm_output_state *output_state;
1335
1336 output_state = drm_output_state_duplicate(output->state_cur,
1337 pending_state,
1338 DRM_OUTPUT_STATE_CLEAR_PLANES);
1339 output_state->dpms = WESTON_DPMS_OFF;
1340
1341 return output_state;
1342}
1343
1344/**
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001345 * Allocate a new drm_pending_state
1346 *
1347 * Allocate a new, empty, 'pending state' structure to be used across a
1348 * repaint cycle or similar.
1349 *
1350 * @param backend DRM backend
1351 * @returns Newly-allocated pending state structure
1352 */
1353static struct drm_pending_state *
1354drm_pending_state_alloc(struct drm_backend *backend)
1355{
1356 struct drm_pending_state *ret;
1357
1358 ret = calloc(1, sizeof(*ret));
1359 if (!ret)
1360 return NULL;
1361
1362 ret->backend = backend;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001363 wl_list_init(&ret->output_list);
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001364
1365 return ret;
1366}
1367
1368/**
1369 * Free a drm_pending_state structure
1370 *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001371 * Frees a pending_state structure, as well as any output_states connected
1372 * to this pending state.
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001373 *
1374 * @param pending_state Pending state structure to free
1375 */
1376static void
1377drm_pending_state_free(struct drm_pending_state *pending_state)
1378{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001379 struct drm_output_state *output_state, *tmp;
1380
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001381 if (!pending_state)
1382 return;
1383
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001384 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
1385 link) {
1386 drm_output_state_free(output_state);
1387 }
1388
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001389 free(pending_state);
1390}
1391
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001392/**
1393 * Find an output state in a pending state
1394 *
1395 * Given a pending_state structure, find the output_state for a particular
1396 * output.
1397 *
1398 * @param pending_state Pending state structure to search
1399 * @param output Output to find state for
1400 * @returns Output state if present, or NULL if not
1401 */
1402static struct drm_output_state *
1403drm_pending_state_get_output(struct drm_pending_state *pending_state,
1404 struct drm_output *output)
1405{
1406 struct drm_output_state *output_state;
1407
1408 wl_list_for_each(output_state, &pending_state->output_list, link) {
1409 if (output_state->output == output)
1410 return output_state;
1411 }
1412
1413 return NULL;
1414}
1415
Daniel Stonea08512f2016-11-08 17:46:10 +00001416static int drm_pending_state_apply_sync(struct drm_pending_state *state);
1417
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001418/**
1419 * Mark a drm_output_state (the output's last state) as complete. This handles
1420 * any post-completion actions such as updating the repaint timer, disabling the
1421 * output, and finally freeing the state.
1422 */
1423static void
1424drm_output_update_complete(struct drm_output *output, uint32_t flags,
1425 unsigned int sec, unsigned int usec)
1426{
Daniel Stonea08512f2016-11-08 17:46:10 +00001427 struct drm_backend *b = to_drm_backend(output->base.compositor);
Daniel Stonebc15f682016-11-14 16:57:01 +00001428 struct drm_plane_state *ps;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001429 struct timespec ts;
1430
1431 /* Stop the pageflip timer instead of rearming it here */
1432 if (output->pageflip_timer)
1433 wl_event_source_timer_update(output->pageflip_timer, 0);
1434
Daniel Stonebc15f682016-11-14 16:57:01 +00001435 wl_list_for_each(ps, &output->state_cur->plane_list, link)
1436 ps->complete = true;
1437
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001438 drm_output_state_free(output->state_last);
1439 output->state_last = NULL;
1440
1441 if (output->destroy_pending) {
Daniel Stonea08512f2016-11-08 17:46:10 +00001442 output->destroy_pending = 0;
1443 output->disable_pending = 0;
1444 output->dpms_off_pending = 0;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001445 drm_output_destroy(&output->base);
1446 return;
1447 } else if (output->disable_pending) {
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001448 output->disable_pending = 0;
Daniel Stonea08512f2016-11-08 17:46:10 +00001449 output->dpms_off_pending = 0;
1450 weston_output_disable(&output->base);
1451 return;
1452 } else if (output->dpms_off_pending) {
1453 struct drm_pending_state *pending = drm_pending_state_alloc(b);
1454 output->dpms_off_pending = 0;
1455 drm_output_get_disable_state(pending, output);
1456 drm_pending_state_apply_sync(pending);
1457 return;
1458 } else if (output->state_cur->dpms == WESTON_DPMS_OFF &&
1459 output->base.repaint_status != REPAINT_AWAITING_COMPLETION) {
1460 /* DPMS can happen to us either in the middle of a repaint
1461 * cycle (when we have painted fresh content, only to throw it
1462 * away for DPMS off), or at any other random point. If the
1463 * latter is true, then we cannot go through finish_frame,
1464 * because the repaint machinery does not expect this. */
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001465 return;
1466 }
1467
1468 ts.tv_sec = sec;
1469 ts.tv_nsec = usec * 1000;
1470 weston_output_finish_frame(&output->base, &ts, flags);
1471
1472 /* We can't call this from frame_notify, because the output's
1473 * repaint needed flag is cleared just after that */
1474 if (output->recorder)
1475 weston_output_schedule_repaint(&output->base);
1476}
1477
1478/**
1479 * Mark an output state as current on the output, i.e. it has been
1480 * submitted to the kernel. The mode argument determines whether this
1481 * update will be applied synchronously (e.g. when calling drmModeSetCrtc),
1482 * or asynchronously (in which case we wait for events to complete).
1483 */
1484static void
1485drm_output_assign_state(struct drm_output_state *state,
1486 enum drm_state_apply_mode mode)
1487{
1488 struct drm_output *output = state->output;
Daniel Stone598ee9d2016-11-16 11:55:20 +00001489 struct drm_backend *b = to_drm_backend(output->base.compositor);
Daniel Stonebc15f682016-11-14 16:57:01 +00001490 struct drm_plane_state *plane_state;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001491
1492 assert(!output->state_last);
1493
1494 if (mode == DRM_STATE_APPLY_ASYNC)
1495 output->state_last = output->state_cur;
1496 else
1497 drm_output_state_free(output->state_cur);
1498
1499 wl_list_remove(&state->link);
1500 wl_list_init(&state->link);
1501 state->pending_state = NULL;
1502
1503 output->state_cur = state;
Daniel Stonebc15f682016-11-14 16:57:01 +00001504
Daniel Stone598ee9d2016-11-16 11:55:20 +00001505 if (b->atomic_modeset && mode == DRM_STATE_APPLY_ASYNC)
1506 output->atomic_complete_pending = 1;
1507
Daniel Stonebc15f682016-11-14 16:57:01 +00001508 /* Replace state_cur on each affected plane with the new state, being
1509 * careful to dispose of orphaned (but only orphaned) previous state.
1510 * If the previous state is not orphaned (still has an output_state
1511 * attached), it will be disposed of by freeing the output_state. */
1512 wl_list_for_each(plane_state, &state->plane_list, link) {
1513 struct drm_plane *plane = plane_state->plane;
1514
1515 if (plane->state_cur && !plane->state_cur->output_state)
1516 drm_plane_state_free(plane->state_cur, true);
1517 plane->state_cur = plane_state;
1518
1519 if (mode != DRM_STATE_APPLY_ASYNC) {
1520 plane_state->complete = true;
1521 continue;
1522 }
1523
Daniel Stone598ee9d2016-11-16 11:55:20 +00001524 if (b->atomic_modeset)
1525 continue;
1526
Daniel Stonebc15f682016-11-14 16:57:01 +00001527 if (plane->type == WDRM_PLANE_TYPE_OVERLAY)
1528 output->vblank_pending++;
Daniel Stonee2e80132018-01-16 15:37:33 +00001529 else if (plane->type == WDRM_PLANE_TYPE_PRIMARY)
1530 output->page_flip_pending = 1;
Daniel Stonebc15f682016-11-14 16:57:01 +00001531 }
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001532}
1533
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001534static int
1535drm_view_transform_supported(struct weston_view *ev)
1536{
1537 return !ev->transform.enabled ||
1538 (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
1539}
1540
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001541static uint32_t
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +02001542drm_output_check_scanout_format(struct drm_output *output,
1543 struct weston_surface *es, struct gbm_bo *bo)
1544{
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +02001545 uint32_t format;
1546 pixman_region32_t r;
1547
1548 format = gbm_bo_get_format(bo);
1549
Kristian Høgsberg3f97b342013-10-16 16:08:57 -07001550 if (format == GBM_FORMAT_ARGB8888) {
1551 /* We can scanout an ARGB buffer if the surface's
1552 * opaque region covers the whole output, but we have
1553 * to use XRGB as the KMS format code. */
Kristian Høgsberg1be87e32014-01-17 14:22:41 -08001554 pixman_region32_init_rect(&r, 0, 0,
1555 output->base.width,
1556 output->base.height);
1557 pixman_region32_subtract(&r, &r, &es->opaque);
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +02001558
1559 if (!pixman_region32_not_empty(&r))
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001560 format = GBM_FORMAT_XRGB8888;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +02001561
1562 pixman_region32_fini(&r);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001563 }
Kristian Høgsberg3f97b342013-10-16 16:08:57 -07001564
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001565 if (output->gbm_format == format)
Kristian Høgsberg3f97b342013-10-16 16:08:57 -07001566 return format;
1567
1568 return 0;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +02001569}
1570
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001571static struct weston_plane *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001572drm_output_prepare_scanout_view(struct drm_output_state *output_state,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001573 struct weston_view *ev)
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001574{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001575 struct drm_output *output = output_state->output;
Armin Krezović545dba62016-08-05 15:54:18 +02001576 struct drm_backend *b = to_drm_backend(output->base.compositor);
Daniel Stonee2e80132018-01-16 15:37:33 +00001577 struct drm_plane *scanout_plane = output->scanout_plane;
1578 struct drm_plane_state *state;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001579 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001580 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001581 struct gbm_bo *bo;
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001582 uint32_t format;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001583
Daniel Stone90648872016-10-21 18:08:37 +01001584 /* Don't import buffers which span multiple outputs. */
1585 if (ev->output_mask != (1u << output->base.id))
1586 return NULL;
1587
Daniel Stone296d7a92016-10-21 18:05:37 +01001588 /* We use GBM to import buffers. */
1589 if (b->gbm == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001590 return NULL;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001591
Daniel Stone296d7a92016-10-21 18:05:37 +01001592 if (buffer == NULL)
1593 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +01001594 if (wl_shm_buffer_get(buffer->resource))
1595 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001596
1597 /* Make sure our view is exactly compatible with the output. */
1598 if (ev->geometry.x != output->base.x ||
1599 ev->geometry.y != output->base.y)
1600 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +01001601 if (buffer->width != output->base.current_mode->width ||
1602 buffer->height != output->base.current_mode->height)
1603 return NULL;
1604
Daniel Stone296d7a92016-10-21 18:05:37 +01001605 if (ev->transform.enabled)
1606 return NULL;
Pekka Paalanen5580f222015-02-17 16:33:18 +02001607 if (ev->geometry.scissor_enabled)
1608 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001609 if (viewport->buffer.transform != output->base.transform)
1610 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +01001611 if (viewport->buffer.scale != output->base.current_scale)
1612 return NULL;
1613 if (!drm_view_transform_supported(ev))
1614 return NULL;
1615
1616 if (ev->alpha != 1.0f)
1617 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001618
Daniel Stonee2e80132018-01-16 15:37:33 +00001619 state = drm_output_state_get_plane(output_state, scanout_plane);
1620 if (state->fb) {
1621 /* If there is already a framebuffer on the scanout plane,
1622 * a client view has already been placed on the scanout
1623 * view. In that case, do not free or put back the state,
1624 * but just leave it in place and quietly exit. */
1625 return NULL;
1626 }
1627
Giulio Camuffo954f1832014-10-11 18:27:30 +03001628 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
Kristian Høgsberg63996462013-09-03 22:27:08 -07001629 buffer->resource, GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001630
Rob Bradford9b101872012-09-14 23:25:41 +01001631 /* Unable to use the buffer for scanout */
1632 if (!bo)
1633 return NULL;
1634
Jason Ekstranda7af7042013-10-12 22:38:11 -05001635 format = drm_output_check_scanout_format(output, ev->surface, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001636 if (format == 0) {
Daniel Stonee2e80132018-01-16 15:37:33 +00001637 drm_plane_state_put_back(state);
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +03001638 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001639 return NULL;
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +03001640 }
1641
Daniel Stonee2e80132018-01-16 15:37:33 +00001642 state->fb = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT);
1643 if (!state->fb) {
1644 drm_plane_state_put_back(state);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001645 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001646 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001647 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001648
Daniel Stonee2e80132018-01-16 15:37:33 +00001649 drm_fb_set_buffer(state->fb, buffer);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001650
Daniel Stonee2e80132018-01-16 15:37:33 +00001651 state->output = output;
1652
1653 state->src_x = 0;
1654 state->src_y = 0;
1655 state->src_w = state->fb->width << 16;
1656 state->src_h = state->fb->height << 16;
1657
1658 state->dest_x = 0;
1659 state->dest_y = 0;
1660 state->dest_w = output->base.current_mode->width;
1661 state->dest_h = output->base.current_mode->height;
1662
1663 return &scanout_plane->base;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001664}
1665
Daniel Stone95d48a22017-04-04 17:54:30 +01001666static struct drm_fb *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001667drm_output_render_gl(struct drm_output_state *state, pixman_region32_t *damage)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001668{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001669 struct drm_output *output = state->output;
Armin Krezović545dba62016-08-05 15:54:18 +02001670 struct drm_backend *b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001671 struct gbm_bo *bo;
Daniel Stone95d48a22017-04-04 17:54:30 +01001672 struct drm_fb *ret;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001673
Giulio Camuffo954f1832014-10-11 18:27:30 +03001674 output->base.compositor->renderer->repaint_output(&output->base,
1675 damage);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001676
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001677 bo = gbm_surface_lock_front_buffer(output->gbm_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001678 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +02001679 weston_log("failed to lock front buffer: %m\n");
Daniel Stone95d48a22017-04-04 17:54:30 +01001680 return NULL;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001681 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001682
Daniel Stone95d48a22017-04-04 17:54:30 +01001683 ret = drm_fb_get_from_bo(bo, b, output->gbm_format, BUFFER_GBM_SURFACE);
1684 if (!ret) {
Martin Minarik6d118362012-06-07 18:01:59 +02001685 weston_log("failed to get drm_fb for bo\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001686 gbm_surface_release_buffer(output->gbm_surface, bo);
Daniel Stone95d48a22017-04-04 17:54:30 +01001687 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001688 }
Daniel Stone95d48a22017-04-04 17:54:30 +01001689 ret->gbm_surface = output->gbm_surface;
1690
1691 return ret;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001692}
1693
Daniel Stone95d48a22017-04-04 17:54:30 +01001694static struct drm_fb *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001695drm_output_render_pixman(struct drm_output_state *state,
1696 pixman_region32_t *damage)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001697{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001698 struct drm_output *output = state->output;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001699 struct weston_compositor *ec = output->base.compositor;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001700
1701 output->current_image ^= 1;
1702
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001703 pixman_renderer_output_set_buffer(&output->base,
1704 output->image[output->current_image]);
Pekka Paalanenacf50c32018-04-23 11:44:56 +02001705 pixman_renderer_output_set_hw_extra_damage(&output->base,
1706 &output->previous_damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001707
Pekka Paalanenacf50c32018-04-23 11:44:56 +02001708 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001709
Pekka Paalanenacf50c32018-04-23 11:44:56 +02001710 pixman_region32_copy(&output->previous_damage, damage);
Daniel Stone95d48a22017-04-04 17:54:30 +01001711
1712 return drm_fb_ref(output->dumb[output->current_image]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001713}
1714
1715static void
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001716drm_output_render(struct drm_output_state *state, pixman_region32_t *damage)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001717{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001718 struct drm_output *output = state->output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001719 struct weston_compositor *c = output->base.compositor;
Daniel Stonee2e80132018-01-16 15:37:33 +00001720 struct drm_plane_state *scanout_state;
Daniel Stonee95169b2016-11-14 17:46:59 +00001721 struct drm_plane *scanout_plane = output->scanout_plane;
Armin Krezović545dba62016-08-05 15:54:18 +02001722 struct drm_backend *b = to_drm_backend(c);
Daniel Stone95d48a22017-04-04 17:54:30 +01001723 struct drm_fb *fb;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001724
Daniel Stone4e84f7d2017-04-04 17:54:29 +01001725 /* If we already have a client buffer promoted to scanout, then we don't
1726 * want to render. */
Daniel Stonee2e80132018-01-16 15:37:33 +00001727 scanout_state = drm_output_state_get_plane(state,
1728 output->scanout_plane);
1729 if (scanout_state->fb)
Daniel Stone4e84f7d2017-04-04 17:54:29 +01001730 return;
1731
Daniel Stonee95169b2016-11-14 17:46:59 +00001732 if (!pixman_region32_not_empty(damage) &&
1733 scanout_plane->state_cur->fb &&
1734 (scanout_plane->state_cur->fb->type == BUFFER_GBM_SURFACE ||
1735 scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB) &&
1736 scanout_plane->state_cur->fb->width ==
1737 output->base.current_mode->width &&
1738 scanout_plane->state_cur->fb->height ==
1739 output->base.current_mode->height) {
1740 fb = drm_fb_ref(scanout_plane->state_cur->fb);
1741 } else if (b->use_pixman) {
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001742 fb = drm_output_render_pixman(state, damage);
Daniel Stonee95169b2016-11-14 17:46:59 +00001743 } else {
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001744 fb = drm_output_render_gl(state, damage);
Daniel Stonee95169b2016-11-14 17:46:59 +00001745 }
Daniel Stone95d48a22017-04-04 17:54:30 +01001746
Daniel Stonee2e80132018-01-16 15:37:33 +00001747 if (!fb) {
1748 drm_plane_state_put_back(scanout_state);
Daniel Stone95d48a22017-04-04 17:54:30 +01001749 return;
Daniel Stonee2e80132018-01-16 15:37:33 +00001750 }
1751
1752 scanout_state->fb = fb;
1753 scanout_state->output = output;
1754
1755 scanout_state->src_x = 0;
1756 scanout_state->src_y = 0;
1757 scanout_state->src_w = output->base.current_mode->width << 16;
1758 scanout_state->src_h = output->base.current_mode->height << 16;
1759
1760 scanout_state->dest_x = 0;
1761 scanout_state->dest_y = 0;
1762 scanout_state->dest_w = scanout_state->src_w >> 16;
1763 scanout_state->dest_h = scanout_state->src_h >> 16;
1764
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001765
Giulio Camuffo954f1832014-10-11 18:27:30 +03001766 pixman_region32_subtract(&c->primary_plane.damage,
1767 &c->primary_plane.damage, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001768}
1769
1770static void
Richard Hughese7299962013-05-01 21:52:12 +01001771drm_output_set_gamma(struct weston_output *output_base,
1772 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
1773{
1774 int rc;
Armin Krezović545dba62016-08-05 15:54:18 +02001775 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001776 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +02001777 to_drm_backend(output->base.compositor);
Richard Hughese7299962013-05-01 21:52:12 +01001778
1779 /* check */
1780 if (output_base->gamma_size != size)
1781 return;
Richard Hughese7299962013-05-01 21:52:12 +01001782
Giulio Camuffo954f1832014-10-11 18:27:30 +03001783 rc = drmModeCrtcSetGamma(backend->drm.fd,
Richard Hughese7299962013-05-01 21:52:12 +01001784 output->crtc_id,
1785 size, r, g, b);
1786 if (rc)
1787 weston_log("set gamma failed: %m\n");
1788}
1789
Bryce Harringtonada4f072015-06-30 13:25:46 -07001790/* Determine the type of vblank synchronization to use for the output.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001791 *
Bryce Harringtonada4f072015-06-30 13:25:46 -07001792 * The pipe parameter indicates which CRTC is in use. Knowing this, we
1793 * can determine which vblank sequence type to use for it. Traditional
1794 * cards had only two CRTCs, with CRTC 0 using no special flags, and
1795 * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
1796 * parameter indicates this.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001797 *
Bryce Harringtonada4f072015-06-30 13:25:46 -07001798 * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
1799 * 0-31. If this is non-zero it indicates we're dealing with a
1800 * multi-gpu situation and we need to calculate the vblank sync
1801 * using DRM_BLANK_HIGH_CRTC_MASK.
1802 */
Pekka Paalanenc8a1ff02015-07-02 15:06:08 +03001803static unsigned int
1804drm_waitvblank_pipe(struct drm_output *output)
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +02001805{
1806 if (output->pipe > 1)
1807 return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
1808 DRM_VBLANK_HIGH_CRTC_MASK;
1809 else if (output->pipe > 0)
1810 return DRM_VBLANK_SECONDARY;
1811 else
1812 return 0;
1813}
1814
David Herrmann1edf44c2013-10-22 17:11:26 +02001815static int
Daniel Stone598ee9d2016-11-16 11:55:20 +00001816drm_output_apply_state_legacy(struct drm_output_state *state)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001817{
Daniel Stonea08512f2016-11-08 17:46:10 +00001818 struct drm_output *output = state->output;
1819 struct drm_backend *backend = to_drm_backend(output->base.compositor);
Daniel Stonee2e80132018-01-16 15:37:33 +00001820 struct drm_plane *scanout_plane = output->scanout_plane;
Pekka Paalanen02aeb5c2017-09-12 16:02:01 +03001821 struct drm_property_info *dpms_prop;
Daniel Stonee2e80132018-01-16 15:37:33 +00001822 struct drm_plane_state *scanout_state;
Daniel Stonebc15f682016-11-14 16:57:01 +00001823 struct drm_plane_state *ps;
Daniel Stone085d2b92015-05-21 00:00:57 +01001824 struct drm_plane *p;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001825 struct drm_mode *mode;
Pekka Paalanen02aeb5c2017-09-12 16:02:01 +03001826 struct drm_head *head;
1827 uint32_t connectors[MAX_CLONED_CONNECTORS];
1828 int n_conn = 0;
Daniel Stonea08512f2016-11-08 17:46:10 +00001829 struct timespec now;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001830 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001831
Pekka Paalanen02aeb5c2017-09-12 16:02:01 +03001832 wl_list_for_each(head, &output->base.head_list, base.output_link) {
1833 assert(n_conn < MAX_CLONED_CONNECTORS);
1834 connectors[n_conn++] = head->connector_id;
1835 }
1836
Derek Foreman2cd87fe2017-04-13 13:48:48 -05001837 /* If disable_planes is set then assign_planes() wasn't
1838 * called for this render, so we could still have a stale
1839 * cursor plane set up.
1840 */
1841 if (output->base.disable_planes) {
1842 output->cursor_view = NULL;
Greg V1f781762018-02-19 17:59:42 +03001843 if (output->cursor_plane) {
1844 output->cursor_plane->base.x = INT32_MIN;
1845 output->cursor_plane->base.y = INT32_MIN;
1846 }
Derek Foreman2cd87fe2017-04-13 13:48:48 -05001847 }
1848
Daniel Stonea08512f2016-11-08 17:46:10 +00001849 if (state->dpms != WESTON_DPMS_ON) {
1850 wl_list_for_each(ps, &state->plane_list, link) {
1851 p = ps->plane;
1852 assert(ps->fb == NULL);
1853 assert(ps->output == NULL);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001854
Daniel Stonea08512f2016-11-08 17:46:10 +00001855 if (p->type != WDRM_PLANE_TYPE_OVERLAY)
1856 continue;
1857
1858 ret = drmModeSetPlane(backend->drm.fd, p->plane_id,
1859 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1860 if (ret)
1861 weston_log("drmModeSetPlane failed disable: %m\n");
1862 }
1863
1864 if (output->cursor_plane) {
1865 ret = drmModeSetCursor(backend->drm.fd, output->crtc_id,
1866 0, 0, 0);
1867 if (ret)
1868 weston_log("drmModeSetCursor failed disable: %m\n");
1869 }
1870
1871 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id, 0, 0, 0,
Pekka Paalanen02aeb5c2017-09-12 16:02:01 +03001872 NULL, 0, NULL);
Daniel Stonea08512f2016-11-08 17:46:10 +00001873 if (ret)
1874 weston_log("drmModeSetCrtc failed disabling: %m\n");
1875
1876 drm_output_assign_state(state, DRM_STATE_APPLY_SYNC);
1877 weston_compositor_read_presentation_clock(output->base.compositor, &now);
1878 drm_output_update_complete(output,
1879 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION,
1880 now.tv_sec, now.tv_nsec / 1000);
1881
1882 return 0;
1883 }
1884
1885 scanout_state =
1886 drm_output_state_get_existing_plane(state, scanout_plane);
Daniel Stone087ddf02017-02-14 17:51:30 +00001887
Daniel Stonee2e80132018-01-16 15:37:33 +00001888 /* The legacy SetCrtc API doesn't allow us to do scaling, and the
1889 * legacy PageFlip API doesn't allow us to do clipping either. */
1890 assert(scanout_state->src_x == 0);
1891 assert(scanout_state->src_y == 0);
1892 assert(scanout_state->src_w ==
1893 (unsigned) (output->base.current_mode->width << 16));
1894 assert(scanout_state->src_h ==
1895 (unsigned) (output->base.current_mode->height << 16));
1896 assert(scanout_state->dest_x == 0);
1897 assert(scanout_state->dest_y == 0);
1898 assert(scanout_state->dest_w == scanout_state->src_w >> 16);
1899 assert(scanout_state->dest_h == scanout_state->src_h >> 16);
1900
Daniel Stonecb04cc42016-11-16 11:51:27 +00001901 mode = to_drm_mode(output->base.current_mode);
Daniel Stone6020f472018-02-05 15:46:20 +00001902 if (backend->state_invalid || !scanout_plane->state_cur->fb ||
Daniel Stonee2e80132018-01-16 15:37:33 +00001903 scanout_plane->state_cur->fb->stride != scanout_state->fb->stride) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001904 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Daniel Stonee2e80132018-01-16 15:37:33 +00001905 scanout_state->fb->fb_id,
1906 0, 0,
Pekka Paalanen02aeb5c2017-09-12 16:02:01 +03001907 connectors, n_conn,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001908 &mode->mode_info);
1909 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +02001910 weston_log("set mode failed: %m\n");
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001911 goto err;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001912 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001913 }
1914
Giulio Camuffo954f1832014-10-11 18:27:30 +03001915 if (drmModePageFlip(backend->drm.fd, output->crtc_id,
Daniel Stonee2e80132018-01-16 15:37:33 +00001916 scanout_state->fb->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -05001917 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001918 weston_log("queueing pageflip failed: %m\n");
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001919 goto err;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -05001920 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001921
Daniel Stone205c0a02017-04-04 17:54:33 +01001922 assert(!output->page_flip_pending);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001923
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001924 if (output->pageflip_timer)
1925 wl_event_source_timer_update(output->pageflip_timer,
1926 backend->pageflip_timeout);
1927
Daniel Stone2ba17f42015-05-19 20:02:41 +01001928 drm_output_set_cursor(state);
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001929
Jesse Barnes58ef3792012-02-23 09:45:49 -05001930 /*
1931 * Now, update all the sprite surfaces
1932 */
Daniel Stonebc15f682016-11-14 16:57:01 +00001933 wl_list_for_each(ps, &state->plane_list, link) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001934 uint32_t flags = 0, fb_id = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001935 drmVBlank vbl = {
1936 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
1937 .request.sequence = 1,
1938 };
1939
Daniel Stonebc15f682016-11-14 16:57:01 +00001940 p = ps->plane;
Daniel Stone085d2b92015-05-21 00:00:57 +01001941 if (p->type != WDRM_PLANE_TYPE_OVERLAY)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001942 continue;
1943
Daniel Stonebc15f682016-11-14 16:57:01 +00001944 assert(p->state_cur->complete);
1945 assert(!!p->state_cur->output == !!p->state_cur->fb);
1946 assert(!p->state_cur->output || p->state_cur->output == output);
1947 assert(!ps->complete);
1948 assert(!ps->output || ps->output == output);
1949 assert(!!ps->output == !!ps->fb);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001950
Daniel Stonebc15f682016-11-14 16:57:01 +00001951 if (ps->fb && !backend->sprites_hidden)
1952 fb_id = ps->fb->fb_id;
Daniel Stone085d2b92015-05-21 00:00:57 +01001953
1954 ret = drmModeSetPlane(backend->drm.fd, p->plane_id,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001955 output->crtc_id, fb_id, flags,
Daniel Stonebc15f682016-11-14 16:57:01 +00001956 ps->dest_x, ps->dest_y,
1957 ps->dest_w, ps->dest_h,
1958 ps->src_x, ps->src_y,
1959 ps->src_w, ps->src_h);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001960 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +02001961 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05001962 ret, strerror(errno));
1963
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +02001964 vbl.request.type |= drm_waitvblank_pipe(output);
Rob Clark5ca1a472012-08-08 20:27:37 -05001965
Jesse Barnes58ef3792012-02-23 09:45:49 -05001966 /*
1967 * Queue a vblank signal so we know when the surface
1968 * becomes active on the display or has been replaced.
1969 */
Daniel Stonebc15f682016-11-14 16:57:01 +00001970 vbl.request.signal = (unsigned long) ps;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001971 ret = drmWaitVBlank(backend->drm.fd, &vbl);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001972 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +02001973 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05001974 ret, strerror(errno));
1975 }
1976 }
1977
Pekka Paalanen02aeb5c2017-09-12 16:02:01 +03001978 if (state->dpms != output->state_cur->dpms) {
1979 wl_list_for_each(head, &output->base.head_list, base.output_link) {
1980 dpms_prop = &head->props_conn[WDRM_CONNECTOR_DPMS];
1981 if (dpms_prop->prop_id == 0)
1982 continue;
1983
1984 ret = drmModeConnectorSetProperty(backend->drm.fd,
1985 head->connector_id,
1986 dpms_prop->prop_id,
1987 state->dpms);
1988 if (ret) {
1989 weston_log("DRM: DPMS: failed property set for %s\n",
1990 head->base.name);
1991 }
Daniel Stonea08512f2016-11-08 17:46:10 +00001992 }
1993 }
1994
1995 drm_output_assign_state(state, DRM_STATE_APPLY_ASYNC);
1996
David Herrmann1edf44c2013-10-22 17:11:26 +02001997 return 0;
1998
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001999err:
Kristian Høgsbergb3955b02014-01-23 16:25:06 -08002000 output->cursor_view = NULL;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002001 drm_output_state_free(state);
Daniel Stonea08512f2016-11-08 17:46:10 +00002002 return -1;
2003}
David Herrmann1edf44c2013-10-22 17:11:26 +02002004
Daniel Stone598ee9d2016-11-16 11:55:20 +00002005#ifdef HAVE_DRM_ATOMIC
2006static int
2007crtc_add_prop(drmModeAtomicReq *req, struct drm_output *output,
2008 enum wdrm_crtc_property prop, uint64_t val)
2009{
2010 struct drm_property_info *info = &output->props_crtc[prop];
2011 int ret;
2012
2013 if (info->prop_id == 0)
2014 return -1;
2015
2016 ret = drmModeAtomicAddProperty(req, output->crtc_id, info->prop_id,
2017 val);
2018 return (ret <= 0) ? -1 : 0;
2019}
2020
2021static int
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002022connector_add_prop(drmModeAtomicReq *req, struct drm_head *head,
Daniel Stone598ee9d2016-11-16 11:55:20 +00002023 enum wdrm_connector_property prop, uint64_t val)
2024{
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002025 struct drm_property_info *info = &head->props_conn[prop];
Daniel Stone598ee9d2016-11-16 11:55:20 +00002026 int ret;
2027
2028 if (info->prop_id == 0)
2029 return -1;
2030
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03002031 ret = drmModeAtomicAddProperty(req, head->connector_id,
Daniel Stone598ee9d2016-11-16 11:55:20 +00002032 info->prop_id, val);
2033 return (ret <= 0) ? -1 : 0;
2034}
2035
2036static int
2037plane_add_prop(drmModeAtomicReq *req, struct drm_plane *plane,
2038 enum wdrm_plane_property prop, uint64_t val)
2039{
2040 struct drm_property_info *info = &plane->props[prop];
2041 int ret;
2042
2043 if (info->prop_id == 0)
2044 return -1;
2045
2046 ret = drmModeAtomicAddProperty(req, plane->plane_id, info->prop_id,
2047 val);
2048 return (ret <= 0) ? -1 : 0;
2049}
2050
2051static int
2052drm_mode_ensure_blob(struct drm_backend *backend, struct drm_mode *mode)
2053{
2054 int ret;
2055
2056 if (mode->blob_id)
2057 return 0;
2058
2059 ret = drmModeCreatePropertyBlob(backend->drm.fd,
2060 &mode->mode_info,
2061 sizeof(mode->mode_info),
2062 &mode->blob_id);
2063 if (ret != 0)
2064 weston_log("failed to create mode property blob: %m\n");
2065
2066 return ret;
2067}
2068
2069static int
2070drm_output_apply_state_atomic(struct drm_output_state *state,
2071 drmModeAtomicReq *req,
2072 uint32_t *flags)
2073{
2074 struct drm_output *output = state->output;
2075 struct drm_backend *backend = to_drm_backend(output->base.compositor);
2076 struct drm_plane_state *plane_state;
2077 struct drm_mode *current_mode = to_drm_mode(output->base.current_mode);
Pekka Paalanen2f661302017-09-12 16:07:32 +03002078 struct drm_head *head;
Daniel Stone598ee9d2016-11-16 11:55:20 +00002079 int ret = 0;
2080
2081 if (state->dpms != output->state_cur->dpms)
2082 *flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
2083
2084 if (state->dpms == WESTON_DPMS_ON) {
2085 ret = drm_mode_ensure_blob(backend, current_mode);
2086 if (ret != 0)
2087 return ret;
2088
2089 ret |= crtc_add_prop(req, output, WDRM_CRTC_MODE_ID,
2090 current_mode->blob_id);
2091 ret |= crtc_add_prop(req, output, WDRM_CRTC_ACTIVE, 1);
Pekka Paalanen2f661302017-09-12 16:07:32 +03002092
2093 wl_list_for_each(head, &output->base.head_list, base.output_link) {
2094 ret |= connector_add_prop(req, head, WDRM_CONNECTOR_CRTC_ID,
2095 output->crtc_id);
2096 }
Daniel Stone598ee9d2016-11-16 11:55:20 +00002097 } else {
2098 ret |= crtc_add_prop(req, output, WDRM_CRTC_MODE_ID, 0);
2099 ret |= crtc_add_prop(req, output, WDRM_CRTC_ACTIVE, 0);
Pekka Paalanen2f661302017-09-12 16:07:32 +03002100
2101 wl_list_for_each(head, &output->base.head_list, base.output_link)
2102 ret |= connector_add_prop(req, head, WDRM_CONNECTOR_CRTC_ID, 0);
Daniel Stone598ee9d2016-11-16 11:55:20 +00002103 }
2104
2105 if (ret != 0) {
2106 weston_log("couldn't set atomic CRTC/connector state\n");
2107 return ret;
2108 }
2109
2110 wl_list_for_each(plane_state, &state->plane_list, link) {
2111 struct drm_plane *plane = plane_state->plane;
2112
2113 ret |= plane_add_prop(req, plane, WDRM_PLANE_FB_ID,
2114 plane_state->fb ? plane_state->fb->fb_id : 0);
2115 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_ID,
2116 plane_state->fb ? output->crtc_id : 0);
2117 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_X,
2118 plane_state->src_x);
2119 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_Y,
2120 plane_state->src_y);
2121 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_W,
2122 plane_state->src_w);
2123 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_H,
2124 plane_state->src_h);
2125 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_X,
2126 plane_state->dest_x);
2127 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_Y,
2128 plane_state->dest_y);
2129 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_W,
2130 plane_state->dest_w);
2131 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_H,
2132 plane_state->dest_h);
2133
2134 if (ret != 0) {
2135 weston_log("couldn't set plane state\n");
2136 return ret;
2137 }
2138 }
2139
2140 return 0;
2141}
2142
2143/**
2144 * Helper function used only by drm_pending_state_apply, with the same
2145 * guarantees and constraints as that function.
2146 */
2147static int
2148drm_pending_state_apply_atomic(struct drm_pending_state *pending_state,
2149 enum drm_state_apply_mode mode)
2150{
2151 struct drm_backend *b = pending_state->backend;
2152 struct drm_output_state *output_state, *tmp;
2153 struct drm_plane *plane;
2154 drmModeAtomicReq *req = drmModeAtomicAlloc();
2155 uint32_t flags = 0;
2156 int ret = 0;
2157
2158 if (!req)
2159 return -1;
2160
2161 if (b->state_invalid) {
Pekka Paalaneneacec812017-09-12 13:43:51 +03002162 struct weston_head *head_base;
2163 struct drm_head *head;
Daniel Stone598ee9d2016-11-16 11:55:20 +00002164 uint32_t *unused;
2165 int err;
2166
2167 /* If we need to reset all our state (e.g. because we've
2168 * just started, or just been VT-switched in), explicitly
2169 * disable all the CRTCs and connectors we aren't using. */
Pekka Paalaneneacec812017-09-12 13:43:51 +03002170 wl_list_for_each(head_base,
2171 &b->compositor->head_list, compositor_link) {
Daniel Stone598ee9d2016-11-16 11:55:20 +00002172 struct drm_property_info *info;
Daniel Stone598ee9d2016-11-16 11:55:20 +00002173
Pekka Paalaneneacec812017-09-12 13:43:51 +03002174 if (weston_head_is_enabled(head_base))
Daniel Stone598ee9d2016-11-16 11:55:20 +00002175 continue;
Daniel Stone598ee9d2016-11-16 11:55:20 +00002176
Pekka Paalaneneacec812017-09-12 13:43:51 +03002177 head = to_drm_head(head_base);
Daniel Stone598ee9d2016-11-16 11:55:20 +00002178
Pekka Paalaneneacec812017-09-12 13:43:51 +03002179 info = &head->props_conn[WDRM_CONNECTOR_CRTC_ID];
2180 err = drmModeAtomicAddProperty(req, head->connector_id,
Daniel Stone598ee9d2016-11-16 11:55:20 +00002181 info->prop_id, 0);
2182 if (err <= 0)
2183 ret = -1;
2184
Pekka Paalaneneacec812017-09-12 13:43:51 +03002185 info = &head->props_conn[WDRM_CONNECTOR_DPMS];
Daniel Stone598ee9d2016-11-16 11:55:20 +00002186 if (info->prop_id > 0)
Pekka Paalaneneacec812017-09-12 13:43:51 +03002187 err = drmModeAtomicAddProperty(req, head->connector_id,
Daniel Stone598ee9d2016-11-16 11:55:20 +00002188 info->prop_id,
2189 DRM_MODE_DPMS_OFF);
2190 if (err <= 0)
2191 ret = -1;
Daniel Stone598ee9d2016-11-16 11:55:20 +00002192 }
2193
2194 wl_array_for_each(unused, &b->unused_crtcs) {
2195 struct drm_property_info infos[WDRM_CRTC__COUNT];
2196 struct drm_property_info *info;
2197 drmModeObjectProperties *props;
2198 uint64_t active;
2199
2200 memset(infos, 0, sizeof(infos));
2201
2202 /* We can't emit a disable on a CRTC that's already
2203 * off, as the kernel will refuse to generate an event
2204 * for an off->off state and fail the commit.
2205 */
2206 props = drmModeObjectGetProperties(b->drm.fd,
2207 *unused,
2208 DRM_MODE_OBJECT_CRTC);
2209 if (!props) {
2210 ret = -1;
2211 continue;
2212 }
2213
2214 drm_property_info_populate(b, crtc_props, infos,
2215 WDRM_CRTC__COUNT,
2216 props);
2217
2218 info = &infos[WDRM_CRTC_ACTIVE];
2219 active = drm_property_get_value(info, props, 0);
2220 drmModeFreeObjectProperties(props);
2221 if (active == 0) {
2222 drm_property_info_free(infos, WDRM_CRTC__COUNT);
2223 continue;
2224 }
2225
2226 err = drmModeAtomicAddProperty(req, *unused,
2227 info->prop_id, 0);
2228 if (err <= 0)
2229 ret = -1;
2230
2231 info = &infos[WDRM_CRTC_MODE_ID];
2232 err = drmModeAtomicAddProperty(req, *unused,
2233 info->prop_id, 0);
2234 if (err <= 0)
2235 ret = -1;
2236
2237 drm_property_info_free(infos, WDRM_CRTC__COUNT);
2238 }
2239
2240 /* Disable all the planes; planes which are being used will
2241 * override this state in the output-state application. */
2242 wl_list_for_each(plane, &b->plane_list, link) {
2243 plane_add_prop(req, plane, WDRM_PLANE_CRTC_ID, 0);
2244 plane_add_prop(req, plane, WDRM_PLANE_FB_ID, 0);
2245 }
2246
2247 flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
2248 }
2249
2250 wl_list_for_each(output_state, &pending_state->output_list, link) {
2251 if (mode == DRM_STATE_APPLY_SYNC)
2252 assert(output_state->dpms == WESTON_DPMS_OFF);
2253 ret |= drm_output_apply_state_atomic(output_state, req, &flags);
2254 }
2255
2256 if (ret != 0) {
2257 weston_log("atomic: couldn't compile atomic state\n");
2258 goto out;
2259 }
2260
2261 switch (mode) {
2262 case DRM_STATE_APPLY_SYNC:
2263 break;
2264 case DRM_STATE_APPLY_ASYNC:
2265 flags |= DRM_MODE_PAGE_FLIP_EVENT | DRM_MODE_ATOMIC_NONBLOCK;
2266 break;
2267 }
2268
2269 ret = drmModeAtomicCommit(b->drm.fd, req, flags, b);
2270 if (ret != 0) {
2271 weston_log("atomic: couldn't commit new state: %m\n");
2272 goto out;
2273 }
2274
2275 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
2276 link)
2277 drm_output_assign_state(output_state, mode);
2278
2279 b->state_invalid = false;
2280
2281 assert(wl_list_empty(&pending_state->output_list));
2282
2283out:
2284 drmModeAtomicFree(req);
2285 drm_pending_state_free(pending_state);
2286 return ret;
2287}
2288#endif
2289
Daniel Stonea08512f2016-11-08 17:46:10 +00002290/**
2291 * Applies all of a pending_state asynchronously: the primary entry point for
2292 * applying KMS state to a device. Updates the state for all outputs in the
2293 * pending_state, as well as disabling any unclaimed outputs.
2294 *
2295 * Unconditionally takes ownership of pending_state, and clears state_invalid.
2296 */
2297static int
2298drm_pending_state_apply(struct drm_pending_state *pending_state)
2299{
2300 struct drm_backend *b = pending_state->backend;
2301 struct drm_output_state *output_state, *tmp;
2302 uint32_t *unused;
2303
Daniel Stone598ee9d2016-11-16 11:55:20 +00002304#ifdef HAVE_DRM_ATOMIC
2305 if (b->atomic_modeset)
2306 return drm_pending_state_apply_atomic(pending_state,
2307 DRM_STATE_APPLY_ASYNC);
2308#endif
2309
Daniel Stonea08512f2016-11-08 17:46:10 +00002310 if (b->state_invalid) {
2311 /* If we need to reset all our state (e.g. because we've
2312 * just started, or just been VT-switched in), explicitly
2313 * disable all the CRTCs we aren't using. This also disables
2314 * all connectors on these CRTCs, so we don't need to do that
2315 * separately with the pre-atomic API. */
2316 wl_array_for_each(unused, &b->unused_crtcs)
2317 drmModeSetCrtc(b->drm.fd, *unused, 0, 0, 0, NULL, 0,
2318 NULL);
2319 }
2320
2321 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
2322 link) {
2323 struct drm_output *output = output_state->output;
2324 int ret;
2325
Daniel Stone598ee9d2016-11-16 11:55:20 +00002326 ret = drm_output_apply_state_legacy(output_state);
Daniel Stonea08512f2016-11-08 17:46:10 +00002327 if (ret != 0) {
2328 weston_log("Couldn't apply state for output %s\n",
2329 output->base.name);
2330 }
2331 }
2332
2333 b->state_invalid = false;
2334
2335 assert(wl_list_empty(&pending_state->output_list));
2336
2337 drm_pending_state_free(pending_state);
2338
2339 return 0;
2340}
2341
2342/**
2343 * The synchronous version of drm_pending_state_apply. May only be used to
2344 * disable outputs. Does so synchronously: the request is guaranteed to have
2345 * completed on return, and the output will not be touched afterwards.
2346 *
2347 * Unconditionally takes ownership of pending_state, and clears state_invalid.
2348 */
2349static int
2350drm_pending_state_apply_sync(struct drm_pending_state *pending_state)
2351{
2352 struct drm_backend *b = pending_state->backend;
2353 struct drm_output_state *output_state, *tmp;
2354 uint32_t *unused;
2355
Daniel Stone598ee9d2016-11-16 11:55:20 +00002356#ifdef HAVE_DRM_ATOMIC
2357 if (b->atomic_modeset)
2358 return drm_pending_state_apply_atomic(pending_state,
2359 DRM_STATE_APPLY_SYNC);
2360#endif
2361
Daniel Stonea08512f2016-11-08 17:46:10 +00002362 if (b->state_invalid) {
2363 /* If we need to reset all our state (e.g. because we've
2364 * just started, or just been VT-switched in), explicitly
2365 * disable all the CRTCs we aren't using. This also disables
2366 * all connectors on these CRTCs, so we don't need to do that
2367 * separately with the pre-atomic API. */
2368 wl_array_for_each(unused, &b->unused_crtcs)
2369 drmModeSetCrtc(b->drm.fd, *unused, 0, 0, 0, NULL, 0,
2370 NULL);
2371 }
2372
2373 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
2374 link) {
2375 int ret;
2376
2377 assert(output_state->dpms == WESTON_DPMS_OFF);
Daniel Stone598ee9d2016-11-16 11:55:20 +00002378 ret = drm_output_apply_state_legacy(output_state);
Daniel Stonea08512f2016-11-08 17:46:10 +00002379 if (ret != 0) {
2380 weston_log("Couldn't apply state for output %s\n",
2381 output_state->output->base.name);
2382 }
2383 }
2384
2385 b->state_invalid = false;
2386
2387 assert(wl_list_empty(&pending_state->output_list));
2388
2389 drm_pending_state_free(pending_state);
2390
2391 return 0;
2392}
2393
2394static int
2395drm_output_repaint(struct weston_output *output_base,
2396 pixman_region32_t *damage,
2397 void *repaint_data)
2398{
2399 struct drm_pending_state *pending_state = repaint_data;
2400 struct drm_output *output = to_drm_output(output_base);
Daniel Stonea08512f2016-11-08 17:46:10 +00002401 struct drm_output_state *state = NULL;
2402 struct drm_plane_state *scanout_state;
2403
2404 if (output->disable_pending || output->destroy_pending)
2405 goto err;
2406
2407 assert(!output->state_last);
2408
2409 /* If planes have been disabled in the core, we might not have
2410 * hit assign_planes at all, so might not have valid output state
2411 * here. */
2412 state = drm_pending_state_get_output(pending_state, output);
2413 if (!state)
2414 state = drm_output_state_duplicate(output->state_cur,
2415 pending_state,
2416 DRM_OUTPUT_STATE_CLEAR_PLANES);
2417 state->dpms = WESTON_DPMS_ON;
2418
2419 drm_output_render(state, damage);
2420 scanout_state = drm_output_state_get_plane(state,
2421 output->scanout_plane);
2422 if (!scanout_state || !scanout_state->fb)
2423 goto err;
2424
Daniel Stonea08512f2016-11-08 17:46:10 +00002425 return 0;
2426
2427err:
2428 drm_output_state_free(state);
David Herrmann1edf44c2013-10-22 17:11:26 +02002429 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002430}
2431
2432static void
Jonas Ådahle5a12252013-04-05 23:07:11 +02002433drm_output_start_repaint_loop(struct weston_output *output_base)
2434{
Armin Krezović545dba62016-08-05 15:54:18 +02002435 struct drm_output *output = to_drm_output(output_base);
Daniel Stone8747f952016-11-29 20:17:32 +00002436 struct drm_pending_state *pending_state;
Daniel Stonee2e80132018-01-16 15:37:33 +00002437 struct drm_plane *scanout_plane = output->scanout_plane;
Armin Krezović545dba62016-08-05 15:54:18 +02002438 struct drm_backend *backend =
2439 to_drm_backend(output_base->compositor);
Mario Kleinerf507ec32015-06-21 21:25:14 +02002440 struct timespec ts, tnow;
2441 struct timespec vbl2now;
2442 int64_t refresh_nsec;
2443 int ret;
2444 drmVBlank vbl = {
2445 .request.type = DRM_VBLANK_RELATIVE,
2446 .request.sequence = 0,
2447 .request.signal = 0,
2448 };
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03002449
Armin Krezović08368132016-09-30 14:11:05 +02002450 if (output->disable_pending || output->destroy_pending)
Xiong Zhangabd5d472013-10-11 14:43:07 +08002451 return;
2452
Daniel Stonee2e80132018-01-16 15:37:33 +00002453 if (!output->scanout_plane->state_cur->fb) {
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03002454 /* We can't page flip if there's no mode set */
David Herrmann3c688c52013-10-22 17:11:25 +02002455 goto finish_frame;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03002456 }
2457
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03002458 /* Need to smash all state in from scratch; current timings might not
2459 * be what we want, page flip might not work, etc.
2460 */
Daniel Stone6020f472018-02-05 15:46:20 +00002461 if (backend->state_invalid)
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03002462 goto finish_frame;
2463
Daniel Stonee2e80132018-01-16 15:37:33 +00002464 assert(scanout_plane->state_cur->output == output);
2465
Mario Kleinerf507ec32015-06-21 21:25:14 +02002466 /* Try to get current msc and timestamp via instant query */
2467 vbl.request.type |= drm_waitvblank_pipe(output);
2468 ret = drmWaitVBlank(backend->drm.fd, &vbl);
2469
2470 /* Error ret or zero timestamp means failure to get valid timestamp */
2471 if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
2472 ts.tv_sec = vbl.reply.tval_sec;
2473 ts.tv_nsec = vbl.reply.tval_usec * 1000;
2474
2475 /* Valid timestamp for most recent vblank - not stale?
2476 * Stale ts could happen on Linux 3.17+, so make sure it
2477 * is not older than 1 refresh duration since now.
2478 */
2479 weston_compositor_read_presentation_clock(backend->compositor,
2480 &tnow);
2481 timespec_sub(&vbl2now, &tnow, &ts);
2482 refresh_nsec =
2483 millihz_to_nsec(output->base.current_mode->refresh);
2484 if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
2485 drm_output_update_msc(output, vbl.reply.sequence);
2486 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002487 WP_PRESENTATION_FEEDBACK_INVALID);
Mario Kleinerf507ec32015-06-21 21:25:14 +02002488 return;
2489 }
2490 }
2491
2492 /* Immediate query didn't provide valid timestamp.
2493 * Use pageflip fallback.
2494 */
Jonas Ådahle5a12252013-04-05 23:07:11 +02002495
Daniel Stone205c0a02017-04-04 17:54:33 +01002496 assert(!output->page_flip_pending);
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002497 assert(!output->state_last);
2498
2499 pending_state = drm_pending_state_alloc(backend);
Daniel Stone8747f952016-11-29 20:17:32 +00002500 drm_output_state_duplicate(output->state_cur, pending_state,
2501 DRM_OUTPUT_STATE_PRESERVE_PLANES);
Daniel Stone205c0a02017-04-04 17:54:33 +01002502
Daniel Stone8747f952016-11-29 20:17:32 +00002503 ret = drm_pending_state_apply(pending_state);
2504 if (ret != 0) {
2505 weston_log("applying repaint-start state failed: %m\n");
David Herrmann3c688c52013-10-22 17:11:25 +02002506 goto finish_frame;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002507 }
David Herrmann3c688c52013-10-22 17:11:25 +02002508
2509 return;
2510
2511finish_frame:
2512 /* if we cannot page-flip, immediately finish frame */
Daniel Stone3615ce12017-03-01 11:34:05 +00002513 weston_output_finish_frame(output_base, NULL,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002514 WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002515}
2516
2517static void
Pekka Paalanen641307c2014-09-23 22:08:47 -04002518drm_output_update_msc(struct drm_output *output, unsigned int seq)
2519{
2520 uint64_t msc_hi = output->base.msc >> 32;
2521
2522 if (seq < (output->base.msc & 0xffffffff))
2523 msc_hi++;
2524
2525 output->base.msc = (msc_hi << 32) + seq;
2526}
2527
2528static void
Jesse Barnes58ef3792012-02-23 09:45:49 -05002529vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
2530 void *data)
2531{
Daniel Stonebc15f682016-11-14 16:57:01 +00002532 struct drm_plane_state *ps = (struct drm_plane_state *) data;
2533 struct drm_output_state *os = ps->output_state;
2534 struct drm_output *output = os->output;
Daniel Stone598ee9d2016-11-16 11:55:20 +00002535 struct drm_backend *b = to_drm_backend(output->base.compositor);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002536 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
2537 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03002538
Daniel Stone598ee9d2016-11-16 11:55:20 +00002539 assert(!b->atomic_modeset);
2540
Pekka Paalanen641307c2014-09-23 22:08:47 -04002541 drm_output_update_msc(output, frame);
Daniel Stone65d87d02017-04-04 17:54:32 +01002542 output->vblank_pending--;
2543 assert(output->vblank_pending >= 0);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002544
Daniel Stonebc15f682016-11-14 16:57:01 +00002545 assert(ps->fb);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03002546
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002547 if (output->page_flip_pending || output->vblank_pending)
2548 return;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00002549
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002550 drm_output_update_complete(output, flags, sec, usec);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002551}
2552
2553static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002554page_flip_handler(int fd, unsigned int frame,
2555 unsigned int sec, unsigned int usec, void *data)
2556{
Armin Krezović545dba62016-08-05 15:54:18 +02002557 struct drm_output *output = data;
Daniel Stone598ee9d2016-11-16 11:55:20 +00002558 struct drm_backend *b = to_drm_backend(output->base.compositor);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002559 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
2560 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
2561 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002562
Pekka Paalanen641307c2014-09-23 22:08:47 -04002563 drm_output_update_msc(output, frame);
2564
Daniel Stone598ee9d2016-11-16 11:55:20 +00002565 assert(!b->atomic_modeset);
Daniel Stone205c0a02017-04-04 17:54:33 +01002566 assert(output->page_flip_pending);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002567 output->page_flip_pending = 0;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002568
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002569 if (output->vblank_pending)
2570 return;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00002571
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002572 drm_output_update_complete(output, flags, sec, usec);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02002573}
2574
Daniel Stoneeedf84c2017-02-10 18:06:04 +00002575/**
2576 * Begin a new repaint cycle
2577 *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002578 * Called by the core compositor at the beginning of a repaint cycle. Creates
2579 * a new pending_state structure to own any output state created by individual
2580 * output repaint functions until the repaint is flushed or cancelled.
Daniel Stoneeedf84c2017-02-10 18:06:04 +00002581 */
2582static void *
2583drm_repaint_begin(struct weston_compositor *compositor)
2584{
2585 struct drm_backend *b = to_drm_backend(compositor);
2586 struct drm_pending_state *ret;
2587
2588 ret = drm_pending_state_alloc(b);
2589 b->repaint_data = ret;
2590
2591 return ret;
2592}
2593
2594/**
2595 * Flush a repaint set
2596 *
2597 * Called by the core compositor when a repaint cycle has been completed
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002598 * and should be flushed. Frees the pending state, transitioning ownership
2599 * of the output state from the pending state, to the update itself. When
2600 * the update completes (see drm_output_update_complete), the output
2601 * state will be freed.
Daniel Stoneeedf84c2017-02-10 18:06:04 +00002602 */
2603static void
2604drm_repaint_flush(struct weston_compositor *compositor, void *repaint_data)
2605{
2606 struct drm_backend *b = to_drm_backend(compositor);
2607 struct drm_pending_state *pending_state = repaint_data;
Daniel Stone6020f472018-02-05 15:46:20 +00002608
Daniel Stonea08512f2016-11-08 17:46:10 +00002609 drm_pending_state_apply(pending_state);
Daniel Stoneeedf84c2017-02-10 18:06:04 +00002610 b->repaint_data = NULL;
2611}
2612
2613/**
2614 * Cancel a repaint set
2615 *
2616 * Called by the core compositor when a repaint has finished, so the data
2617 * held across the repaint cycle should be discarded.
2618 */
2619static void
2620drm_repaint_cancel(struct weston_compositor *compositor, void *repaint_data)
2621{
2622 struct drm_backend *b = to_drm_backend(compositor);
2623 struct drm_pending_state *pending_state = repaint_data;
2624
2625 drm_pending_state_free(pending_state);
2626 b->repaint_data = NULL;
2627}
2628
Daniel Stone598ee9d2016-11-16 11:55:20 +00002629#ifdef HAVE_DRM_ATOMIC
2630static void
2631atomic_flip_handler(int fd, unsigned int frame, unsigned int sec,
2632 unsigned int usec, unsigned int crtc_id, void *data)
2633{
2634 struct drm_backend *b = data;
2635 struct drm_output *output = drm_output_find_by_crtc(b, crtc_id);
2636 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
2637 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
2638 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
2639
2640 /* During the initial modeset, we can disable CRTCs which we don't
2641 * actually handle during normal operation; this will give us events
2642 * for unknown outputs. Ignore them. */
2643 if (!output || !output->base.enabled)
2644 return;
2645
2646 drm_output_update_msc(output, frame);
2647
2648 assert(b->atomic_modeset);
2649 assert(output->atomic_complete_pending);
2650 output->atomic_complete_pending = 0;
2651
2652 drm_output_update_complete(output, flags, sec, usec);
2653}
2654#endif
2655
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05002656static uint32_t
Daniel Stone08d4edf2017-04-04 17:54:34 +01002657drm_output_check_plane_format(struct drm_plane *p,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002658 struct weston_view *ev, struct gbm_bo *bo)
Jesse Barnes58ef3792012-02-23 09:45:49 -05002659{
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05002660 uint32_t i, format;
2661
2662 format = gbm_bo_get_format(bo);
2663
2664 if (format == GBM_FORMAT_ARGB8888) {
2665 pixman_region32_t r;
2666
Kristian Høgsberg63093a32013-03-01 14:29:16 -05002667 pixman_region32_init_rect(&r, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002668 ev->surface->width,
2669 ev->surface->height);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002670 pixman_region32_subtract(&r, &r, &ev->surface->opaque);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05002671
2672 if (!pixman_region32_not_empty(&r))
2673 format = GBM_FORMAT_XRGB8888;
2674
2675 pixman_region32_fini(&r);
2676 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05002677
Daniel Stone08d4edf2017-04-04 17:54:34 +01002678 for (i = 0; i < p->count_formats; i++)
2679 if (p->formats[i] == format)
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05002680 return format;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002681
2682 return 0;
2683}
2684
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002685static struct weston_plane *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002686drm_output_prepare_overlay_view(struct drm_output_state *output_state,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002687 struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -05002688{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002689 struct drm_output *output = output_state->output;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02002690 struct weston_compositor *ec = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02002691 struct drm_backend *b = to_drm_backend(ec);
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002692 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002693 struct wl_resource *buffer_resource;
Daniel Stone08d4edf2017-04-04 17:54:34 +01002694 struct drm_plane *p;
Daniel Stonebc15f682016-11-14 16:57:01 +00002695 struct drm_plane_state *state = NULL;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002696 struct linux_dmabuf_buffer *dmabuf;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002697 struct gbm_bo *bo;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002698 pixman_region32_t dest_rect, src_rect;
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02002699 pixman_box32_t *box, tbox;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002700 uint32_t format;
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04002701 wl_fixed_t sx1, sy1, sx2, sy2;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002702
Giulio Camuffo954f1832014-10-11 18:27:30 +03002703 if (b->sprites_are_broken)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002704 return NULL;
Kristian Høgsberg65bec242012-03-05 19:57:35 -05002705
Daniel Stone296d7a92016-10-21 18:05:37 +01002706 /* Don't import buffers which span multiple outputs. */
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02002707 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002708 return NULL;
Ander Conselvan de Oliveirad450b192012-06-26 17:09:12 +03002709
Daniel Stone296d7a92016-10-21 18:05:37 +01002710 /* We can only import GBM buffers. */
2711 if (b->gbm == NULL)
2712 return NULL;
2713
Jason Ekstranda7af7042013-10-12 22:38:11 -05002714 if (ev->surface->buffer_ref.buffer == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002715 return NULL;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002716 buffer_resource = ev->surface->buffer_ref.buffer->resource;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002717 if (wl_shm_buffer_get(buffer_resource))
Rob Clark702ffae2012-08-09 14:18:27 -05002718 return NULL;
2719
Daniel Stone296d7a92016-10-21 18:05:37 +01002720 if (viewport->buffer.transform != output->base.transform)
2721 return NULL;
2722 if (viewport->buffer.scale != output->base.current_scale)
2723 return NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002724 if (!drm_view_transform_supported(ev))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002725 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002726
Daniel Stone296d7a92016-10-21 18:05:37 +01002727 if (ev->alpha != 1.0f)
2728 return NULL;
2729
Daniel Stone085d2b92015-05-21 00:00:57 +01002730 wl_list_for_each(p, &b->plane_list, link) {
2731 if (p->type != WDRM_PLANE_TYPE_OVERLAY)
2732 continue;
2733
Daniel Stone5ff289a2017-10-07 12:59:02 +01002734 if (!drm_plane_is_available(p, output))
Daniel Stonebc15f682016-11-14 16:57:01 +00002735 continue;
2736
2737 state = drm_output_state_get_plane(output_state, p);
2738 if (state->fb) {
2739 state = NULL;
2740 continue;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002741 }
Daniel Stonebc15f682016-11-14 16:57:01 +00002742
2743 break;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002744 }
2745
2746 /* No sprites available */
Daniel Stonebc15f682016-11-14 16:57:01 +00002747 if (!state)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002748 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002749
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002750 if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) {
Bryce Harringtona3582072015-08-14 12:23:13 -07002751#ifdef HAVE_GBM_FD_IMPORT
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002752 /* XXX: TODO:
2753 *
2754 * Use AddFB2 directly, do not go via GBM.
2755 * Add support for multiplanar formats.
2756 * Both require refactoring in the DRM-backend to
2757 * support a mix of gbm_bos and drmfbs.
2758 */
2759 struct gbm_import_fd_data gbm_dmabuf = {
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00002760 .fd = dmabuf->attributes.fd[0],
2761 .width = dmabuf->attributes.width,
2762 .height = dmabuf->attributes.height,
2763 .stride = dmabuf->attributes.stride[0],
2764 .format = dmabuf->attributes.format
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002765 };
2766
Micah Fedkec8890122017-02-01 15:28:23 -05002767 /* XXX: TODO:
2768 *
2769 * Currently the buffer is rejected if any dmabuf attribute
2770 * flag is set. This keeps us from passing an inverted /
2771 * interlaced / bottom-first buffer (or any other type that may
2772 * be added in the future) through to an overlay. Ultimately,
2773 * these types of buffers should be handled through buffer
2774 * transforms and not as spot-checks requiring specific
2775 * knowledge. */
2776 if (dmabuf->attributes.n_planes != 1 ||
2777 dmabuf->attributes.offset[0] != 0 ||
2778 dmabuf->attributes.flags)
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002779 return NULL;
2780
2781 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf,
2782 GBM_BO_USE_SCANOUT);
Bryce Harringtona3582072015-08-14 12:23:13 -07002783#else
Daniel Stonebc15f682016-11-14 16:57:01 +00002784 goto err;
Bryce Harringtona3582072015-08-14 12:23:13 -07002785#endif
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002786 } else {
2787 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
2788 buffer_resource, GBM_BO_USE_SCANOUT);
2789 }
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04002790 if (!bo)
Daniel Stonebc15f682016-11-14 16:57:01 +00002791 goto err;
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04002792
Daniel Stone08d4edf2017-04-04 17:54:34 +01002793 format = drm_output_check_plane_format(p, ev, bo);
Daniel Stonebc15f682016-11-14 16:57:01 +00002794 if (format == 0)
2795 goto err;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002796
Daniel Stonebc15f682016-11-14 16:57:01 +00002797 state->fb = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT);
2798 if (!state->fb)
2799 goto err;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02002800
Daniel Stonebc15f682016-11-14 16:57:01 +00002801 drm_fb_set_buffer(state->fb, ev->surface->buffer_ref.buffer);
2802
2803 state->output = output;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002804
Jason Ekstranda7af7042013-10-12 22:38:11 -05002805 box = pixman_region32_extents(&ev->transform.boundingbox);
Daniel Stone08d4edf2017-04-04 17:54:34 +01002806 p->base.x = box->x1;
2807 p->base.y = box->y1;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002808
Jesse Barnes58ef3792012-02-23 09:45:49 -05002809 /*
2810 * Calculate the source & dest rects properly based on actual
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002811 * position (note the caller has called weston_view_update_transform()
Jesse Barnes58ef3792012-02-23 09:45:49 -05002812 * for us already).
2813 */
2814 pixman_region32_init(&dest_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002815 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02002816 &output->base.region);
2817 pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002818 box = pixman_region32_extents(&dest_rect);
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02002819 tbox = weston_transformed_rect(output->base.width,
2820 output->base.height,
2821 output->base.transform,
2822 output->base.current_scale,
Alexander Larssond9a7bb72013-05-22 14:41:39 +02002823 *box);
Daniel Stonebc15f682016-11-14 16:57:01 +00002824 state->dest_x = tbox.x1;
2825 state->dest_y = tbox.y1;
2826 state->dest_w = tbox.x2 - tbox.x1;
2827 state->dest_h = tbox.y2 - tbox.y1;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002828 pixman_region32_fini(&dest_rect);
2829
2830 pixman_region32_init(&src_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002831 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02002832 &output->base.region);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002833 box = pixman_region32_extents(&src_rect);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04002834
Jason Ekstranda7af7042013-10-12 22:38:11 -05002835 weston_view_from_global_fixed(ev,
2836 wl_fixed_from_int(box->x1),
2837 wl_fixed_from_int(box->y1),
2838 &sx1, &sy1);
2839 weston_view_from_global_fixed(ev,
2840 wl_fixed_from_int(box->x2),
2841 wl_fixed_from_int(box->y2),
2842 &sx2, &sy2);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04002843
2844 if (sx1 < 0)
2845 sx1 = 0;
2846 if (sy1 < 0)
2847 sy1 = 0;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002848 if (sx2 > wl_fixed_from_int(ev->surface->width))
2849 sx2 = wl_fixed_from_int(ev->surface->width);
2850 if (sy2 > wl_fixed_from_int(ev->surface->height))
2851 sy2 = wl_fixed_from_int(ev->surface->height);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04002852
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02002853 tbox.x1 = sx1;
2854 tbox.y1 = sy1;
2855 tbox.x2 = sx2;
2856 tbox.y2 = sy2;
2857
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002858 tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width),
2859 wl_fixed_from_int(ev->surface->height),
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002860 viewport->buffer.transform,
2861 viewport->buffer.scale,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002862 tbox);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02002863
Daniel Stonebc15f682016-11-14 16:57:01 +00002864 state->src_x = tbox.x1 << 8;
2865 state->src_y = tbox.y1 << 8;
2866 state->src_w = (tbox.x2 - tbox.x1) << 8;
2867 state->src_h = (tbox.y2 - tbox.y1) << 8;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002868 pixman_region32_fini(&src_rect);
2869
Daniel Stone08d4edf2017-04-04 17:54:34 +01002870 return &p->base;
Daniel Stonebc15f682016-11-14 16:57:01 +00002871
2872err:
2873 drm_plane_state_put_back(state);
2874 if (bo)
2875 gbm_bo_destroy(bo);
2876 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002877}
2878
Pekka Paalanend0ead482014-06-16 12:05:40 +03002879/**
2880 * Update the image for the current cursor surface
2881 *
2882 * @param b DRM backend structure
2883 * @param bo GBM buffer object to write into
2884 * @param ev View to use for cursor image
2885 */
2886static void
2887cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo,
2888 struct weston_view *ev)
2889{
2890 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
2891 uint32_t buf[b->cursor_width * b->cursor_height];
2892 int32_t stride;
2893 uint8_t *s;
2894 int i;
2895
2896 assert(buffer && buffer->shm_buffer);
2897 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
2898 assert(ev->surface->width <= b->cursor_width);
2899 assert(ev->surface->height <= b->cursor_height);
2900
2901 memset(buf, 0, sizeof buf);
2902 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
2903 s = wl_shm_buffer_get_data(buffer->shm_buffer);
2904
2905 wl_shm_buffer_begin_access(buffer->shm_buffer);
2906 for (i = 0; i < ev->surface->height; i++)
2907 memcpy(buf + i * b->cursor_width,
2908 s + i * stride,
2909 ev->surface->width * 4);
2910 wl_shm_buffer_end_access(buffer->shm_buffer);
2911
2912 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
2913 weston_log("failed update cursor: %m\n");
2914}
2915
Daniel Stone2ba17f42015-05-19 20:02:41 +01002916static struct weston_plane *
2917drm_output_prepare_cursor_view(struct drm_output_state *output_state,
2918 struct weston_view *ev)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04002919{
Daniel Stone2ba17f42015-05-19 20:02:41 +01002920 struct drm_output *output = output_state->output;
Armin Krezović545dba62016-08-05 15:54:18 +02002921 struct drm_backend *b = to_drm_backend(output->base.compositor);
Daniel Stone2ba17f42015-05-19 20:02:41 +01002922 struct drm_plane *plane = output->cursor_plane;
2923 struct drm_plane_state *plane_state;
2924 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
2925 struct wl_shm_buffer *shmbuf;
2926 bool needs_update = false;
Derek Foremanbe428b32015-11-24 11:39:38 -06002927 float x, y;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05002928
Daniel Stone2ba17f42015-05-19 20:02:41 +01002929 if (!plane)
2930 return NULL;
2931
2932 if (b->cursors_are_broken)
2933 return NULL;
2934
2935 if (!plane->state_cur->complete)
2936 return NULL;
2937
2938 if (plane->state_cur->output && plane->state_cur->output != output)
2939 return NULL;
2940
2941 /* Don't import buffers which span multiple outputs. */
2942 if (ev->output_mask != (1u << output->base.id))
2943 return NULL;
2944
2945 /* We use GBM to import SHM buffers. */
2946 if (b->gbm == NULL)
2947 return NULL;
2948
2949 if (ev->surface->buffer_ref.buffer == NULL)
2950 return NULL;
2951 shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource);
2952 if (!shmbuf)
2953 return NULL;
2954 if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888)
2955 return NULL;
2956
2957 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
2958 return NULL;
2959 if (ev->transform.enabled &&
2960 (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE))
2961 return NULL;
2962 if (viewport->buffer.scale != output->base.current_scale)
2963 return NULL;
2964 if (ev->geometry.scissor_enabled)
2965 return NULL;
2966
2967 if (ev->surface->width > b->cursor_width ||
2968 ev->surface->height > b->cursor_height)
2969 return NULL;
2970
2971 plane_state =
2972 drm_output_state_get_plane(output_state, output->cursor_plane);
2973
2974 if (plane_state && plane_state->fb)
2975 return NULL;
2976
2977 /* Since we're setting plane state up front, we need to work out
2978 * whether or not we need to upload a new cursor. We can't use the
2979 * plane damage, since the planes haven't actually been calculated
2980 * yet: instead try to figure it out directly. KMS cursor planes are
2981 * pretty unique here, in that they lie partway between a Weston plane
2982 * (direct scanout) and a renderer. */
2983 if (ev != output->cursor_view ||
2984 pixman_region32_not_empty(&ev->surface->damage)) {
2985 output->current_cursor++;
2986 output->current_cursor =
2987 output->current_cursor %
2988 ARRAY_LENGTH(output->gbm_cursor_fb);
2989 needs_update = true;
2990 }
2991
2992 output->cursor_view = ev;
2993 weston_view_to_global_float(ev, 0, 0, &x, &y);
2994 plane->base.x = x;
2995 plane->base.y = y;
2996
2997 plane_state->fb =
2998 drm_fb_ref(output->gbm_cursor_fb[output->current_cursor]);
2999 plane_state->output = output;
3000 plane_state->src_x = 0;
3001 plane_state->src_y = 0;
3002 plane_state->src_w = b->cursor_width << 16;
3003 plane_state->src_h = b->cursor_height << 16;
3004 plane_state->dest_x = (x - output->base.x) * output->base.current_scale;
3005 plane_state->dest_y = (y - output->base.y) * output->base.current_scale;
3006 plane_state->dest_w = b->cursor_width;
3007 plane_state->dest_h = b->cursor_height;
3008
3009 if (needs_update)
3010 cursor_bo_update(b, plane_state->fb->bo, ev);
3011
3012 return &plane->base;
3013}
3014
3015static void
3016drm_output_set_cursor(struct drm_output_state *output_state)
3017{
3018 struct drm_output *output = output_state->output;
3019 struct drm_backend *b = to_drm_backend(output->base.compositor);
3020 struct drm_plane *plane = output->cursor_plane;
3021 struct drm_plane_state *state;
3022 EGLint handle;
3023 struct gbm_bo *bo;
3024
3025 if (!plane)
3026 return;
3027
3028 state = drm_output_state_get_existing_plane(output_state, plane);
3029 if (!state)
3030 return;
3031
3032 if (!state->fb) {
3033 pixman_region32_fini(&plane->base.damage);
3034 pixman_region32_init(&plane->base.damage);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003035 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg5626d342012-08-03 11:50:05 -04003036 return;
3037 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05003038
Daniel Stone2ba17f42015-05-19 20:02:41 +01003039 assert(state->fb == output->gbm_cursor_fb[output->current_cursor]);
3040 assert(!plane->state_cur->output || plane->state_cur->output == output);
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05003041
Daniel Stone2ba17f42015-05-19 20:02:41 +01003042 if (plane->state_cur->fb != state->fb) {
3043 bo = state->fb->bo;
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04003044 handle = gbm_bo_get_handle(bo).s32;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003045 if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle,
Daniel Stone2ba17f42015-05-19 20:02:41 +01003046 b->cursor_width, b->cursor_height)) {
Pekka Paalanenae29da22012-08-06 14:57:05 +03003047 weston_log("failed to set cursor: %m\n");
Daniel Stone2ba17f42015-05-19 20:02:41 +01003048 goto err;
Rob Clarkab5b1e32012-08-09 13:24:45 -05003049 }
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003050 }
3051
Daniel Stone2ba17f42015-05-19 20:02:41 +01003052 pixman_region32_fini(&plane->base.damage);
3053 pixman_region32_init(&plane->base.damage);
Pekka Paalanen7eaed402015-11-27 14:20:58 +02003054
Daniel Stone2ba17f42015-05-19 20:02:41 +01003055 if (drmModeMoveCursor(b->drm.fd, output->crtc_id,
3056 state->dest_x, state->dest_y)) {
Daniel Stonea7cba1d2017-04-04 17:54:21 +01003057 weston_log("failed to move cursor: %m\n");
Daniel Stone2ba17f42015-05-19 20:02:41 +01003058 goto err;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003059 }
Daniel Stone2ba17f42015-05-19 20:02:41 +01003060
3061 return;
3062
3063err:
3064 b->cursors_are_broken = 1;
3065 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05003066}
3067
Ankit Nautiyala21c3932097-03-19 00:24:57 +05303068/*
3069 * Get the aspect-ratio from drmModeModeInfo mode flags.
3070 *
3071 * @param drm_mode_flags- flags from drmModeModeInfo structure.
3072 * @returns aspect-ratio as encoded in enum 'weston_mode_aspect_ratio'.
3073 */
3074static enum weston_mode_aspect_ratio
3075drm_to_weston_mode_aspect_ratio(uint32_t drm_mode_flags)
3076{
3077 return (drm_mode_flags & DRM_MODE_FLAG_PIC_AR_MASK) >>
3078 DRM_MODE_FLAG_PIC_AR_BITS_POS;
3079}
3080
3081static const char *
3082aspect_ratio_to_string(enum weston_mode_aspect_ratio ratio)
3083{
3084 if (ratio < 0 || ratio >= ARRAY_LENGTH(aspect_ratio_as_string) ||
3085 !aspect_ratio_as_string[ratio])
3086 return " (unknown aspect ratio)";
3087
3088 return aspect_ratio_as_string[ratio];
3089}
3090
Jesse Barnes58ef3792012-02-23 09:45:49 -05003091static void
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003092drm_assign_planes(struct weston_output *output_base, void *repaint_data)
Jesse Barnes58ef3792012-02-23 09:45:49 -05003093{
Armin Krezović545dba62016-08-05 15:54:18 +02003094 struct drm_backend *b = to_drm_backend(output_base->compositor);
Daniel Stone7b2ddac2016-11-11 19:11:49 +00003095 struct drm_pending_state *pending_state = repaint_data;
Armin Krezović545dba62016-08-05 15:54:18 +02003096 struct drm_output *output = to_drm_output(output_base);
Daniel Stone7b2ddac2016-11-11 19:11:49 +00003097 struct drm_output_state *state;
Daniel Stone2ba17f42015-05-19 20:02:41 +01003098 struct drm_plane_state *plane_state;
Daniel Stonec3fcb5b2016-12-01 12:33:54 +00003099 struct weston_view *ev;
Daniel Stone115ed2c2016-11-08 21:19:22 +00003100 pixman_region32_t surface_overlap, renderer_region;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003101 struct weston_plane *primary, *next_plane;
Matt Hoosierdf573032017-08-24 09:24:20 -05003102 bool picked_scanout = false;
Jesse Barnes58ef3792012-02-23 09:45:49 -05003103
Daniel Stone7b2ddac2016-11-11 19:11:49 +00003104 assert(!output->state_last);
3105 state = drm_output_state_duplicate(output->state_cur,
3106 pending_state,
3107 DRM_OUTPUT_STATE_CLEAR_PLANES);
3108
Jesse Barnes58ef3792012-02-23 09:45:49 -05003109 /*
3110 * Find a surface for each sprite in the output using some heuristics:
3111 * 1) size
3112 * 2) frequency of update
3113 * 3) opacity (though some hw might support alpha blending)
3114 * 4) clipping (this can be fixed with color keys)
3115 *
3116 * The idea is to save on blitting since this should save power.
3117 * If we can get a large video surface on the sprite for example,
3118 * the main display surface may not need to update at all, and
3119 * the client buffer can be used directly for the sprite surface
3120 * as we do for flipping full screen surfaces.
3121 */
Daniel Stone115ed2c2016-11-08 21:19:22 +00003122 pixman_region32_init(&renderer_region);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003123 primary = &output_base->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003124
Daniel Stonec3fcb5b2016-12-01 12:33:54 +00003125 wl_list_for_each(ev, &output_base->compositor->view_list, link) {
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02003126 struct weston_surface *es = ev->surface;
3127
3128 /* Test whether this buffer can ever go into a plane:
3129 * non-shm, or small enough to be a cursor.
3130 *
3131 * Also, keep a reference when using the pixman renderer.
3132 * That makes it possible to do a seamless switch to the GL
3133 * renderer and since the pixman renderer keeps a reference
3134 * to the buffer anyway, there is no side effects.
Pekka Paalanenccfeae22012-12-04 15:58:14 +02003135 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003136 if (b->use_pixman ||
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02003137 (es->buffer_ref.buffer &&
3138 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
Derek Foreman87430472015-10-15 10:24:48 -05003139 (ev->surface->width <= b->cursor_width &&
3140 ev->surface->height <= b->cursor_height))))
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05003141 es->keep_buffer = true;
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02003142 else
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05003143 es->keep_buffer = false;
Pekka Paalanenccfeae22012-12-04 15:58:14 +02003144
Jesse Barnes58ef3792012-02-23 09:45:49 -05003145 pixman_region32_init(&surface_overlap);
Daniel Stone115ed2c2016-11-08 21:19:22 +00003146 pixman_region32_intersect(&surface_overlap, &renderer_region,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003147 &ev->transform.boundingbox);
Jesse Barnes58ef3792012-02-23 09:45:49 -05003148
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003149 next_plane = NULL;
Matt Hoosierdf573032017-08-24 09:24:20 -05003150 if (pixman_region32_not_empty(&surface_overlap) || picked_scanout)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003151 next_plane = primary;
3152 if (next_plane == NULL)
Daniel Stone7b2ddac2016-11-11 19:11:49 +00003153 next_plane = drm_output_prepare_cursor_view(state, ev);
Matt Hoosierdf573032017-08-24 09:24:20 -05003154
3155 /* If a higher-stacked view already got assigned to scanout, it's incorrect to
3156 * assign a subsequent (lower-stacked) view to scanout.
3157 */
3158 if (next_plane == NULL) {
Daniel Stone7b2ddac2016-11-11 19:11:49 +00003159 next_plane = drm_output_prepare_scanout_view(state, ev);
Matt Hoosierdf573032017-08-24 09:24:20 -05003160 if (next_plane)
3161 picked_scanout = true;
3162 }
3163
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003164 if (next_plane == NULL)
Daniel Stone7b2ddac2016-11-11 19:11:49 +00003165 next_plane = drm_output_prepare_overlay_view(state, ev);
Matt Hoosierdf573032017-08-24 09:24:20 -05003166
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003167 if (next_plane == NULL)
3168 next_plane = primary;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02003169
Jason Ekstranda7af7042013-10-12 22:38:11 -05003170 weston_view_move_to_plane(ev, next_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02003171
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003172 if (next_plane == primary)
Daniel Stone115ed2c2016-11-08 21:19:22 +00003173 pixman_region32_union(&renderer_region,
3174 &renderer_region,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003175 &ev->transform.boundingbox);
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04003176
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02003177 if (next_plane == primary ||
Daniel Stone2ba17f42015-05-19 20:02:41 +01003178 (output->cursor_plane &&
3179 next_plane == &output->cursor_plane->base)) {
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02003180 /* cursor plane involves a copy */
3181 ev->psf_flags = 0;
3182 } else {
3183 /* All other planes are a direct scanout of a
3184 * single client buffer.
3185 */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02003186 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02003187 }
3188
Jesse Barnes58ef3792012-02-23 09:45:49 -05003189 pixman_region32_fini(&surface_overlap);
3190 }
Daniel Stone115ed2c2016-11-08 21:19:22 +00003191 pixman_region32_fini(&renderer_region);
Daniel Stone2ba17f42015-05-19 20:02:41 +01003192
3193 /* We rely on output->cursor_view being both an accurate reflection of
3194 * the cursor plane's state, but also being maintained across repaints
3195 * to avoid unnecessary damage uploads, per the comment in
3196 * drm_output_prepare_cursor_view. In the event that we go from having
3197 * a cursor view to not having a cursor view, we need to clear it. */
3198 if (output->cursor_view) {
3199 plane_state =
3200 drm_output_state_get_existing_plane(state,
3201 output->cursor_plane);
3202 if (!plane_state || !plane_state->fb)
3203 output->cursor_view = NULL;
3204 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05003205}
3206
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003207/**
3208 * Find the closest-matching mode for a given target
3209 *
3210 * Given a target mode, find the most suitable mode amongst the output's
3211 * current mode list to use, preferring the current mode if possible, to
3212 * avoid an expensive mode switch.
3213 *
3214 * @param output DRM output
3215 * @param target_mode Mode to attempt to match
3216 * @returns Pointer to a mode from the output's mode list
3217 */
Alex Wub7b8bda2012-04-17 17:20:48 +08003218static struct drm_mode *
3219choose_mode (struct drm_output *output, struct weston_mode *target_mode)
3220{
Ankit Nautiyala21c3932097-03-19 00:24:57 +05303221 struct drm_mode *tmp_mode = NULL, *mode_fall_back = NULL, *mode;
3222 enum weston_mode_aspect_ratio src_aspect = WESTON_MODE_PIC_AR_NONE;
3223 enum weston_mode_aspect_ratio target_aspect = WESTON_MODE_PIC_AR_NONE;
3224 struct drm_backend *b;
Alex Wub7b8bda2012-04-17 17:20:48 +08003225
Ankit Nautiyala21c3932097-03-19 00:24:57 +05303226 b = to_drm_backend(output->base.compositor);
3227 target_aspect = target_mode->aspect_ratio;
3228 src_aspect = output->base.current_mode->aspect_ratio;
Hardeningff39efa2013-09-18 23:56:35 +02003229 if (output->base.current_mode->width == target_mode->width &&
3230 output->base.current_mode->height == target_mode->height &&
3231 (output->base.current_mode->refresh == target_mode->refresh ||
Ankit Nautiyala21c3932097-03-19 00:24:57 +05303232 target_mode->refresh == 0)) {
3233 if (!b->aspect_ratio_supported || src_aspect == target_aspect)
3234 return to_drm_mode(output->base.current_mode);
3235 }
Alex Wub7b8bda2012-04-17 17:20:48 +08003236
3237 wl_list_for_each(mode, &output->base.mode_list, base.link) {
Ankit Nautiyala21c3932097-03-19 00:24:57 +05303238
3239 src_aspect = mode->base.aspect_ratio;
Alex Wub7b8bda2012-04-17 17:20:48 +08003240 if (mode->mode_info.hdisplay == target_mode->width &&
3241 mode->mode_info.vdisplay == target_mode->height) {
Mario Kleiner872797c2015-06-21 21:25:09 +02003242 if (mode->base.refresh == target_mode->refresh ||
3243 target_mode->refresh == 0) {
Ankit Nautiyala21c3932097-03-19 00:24:57 +05303244 if (!b->aspect_ratio_supported ||
3245 src_aspect == target_aspect)
3246 return mode;
3247 else if (!mode_fall_back)
3248 mode_fall_back = mode;
3249 } else if (!tmp_mode) {
Alex Wub7b8bda2012-04-17 17:20:48 +08003250 tmp_mode = mode;
Ankit Nautiyala21c3932097-03-19 00:24:57 +05303251 }
Alex Wub7b8bda2012-04-17 17:20:48 +08003252 }
3253 }
3254
Ankit Nautiyala21c3932097-03-19 00:24:57 +05303255 if (mode_fall_back)
3256 return mode_fall_back;
3257
Alex Wub7b8bda2012-04-17 17:20:48 +08003258 return tmp_mode;
3259}
3260
3261static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003262drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00003263static void
3264drm_output_fini_egl(struct drm_output *output);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003265static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003266drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00003267static void
3268drm_output_fini_pixman(struct drm_output *output);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02003269
3270static int
Alex Wub7b8bda2012-04-17 17:20:48 +08003271drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
3272{
Daniel Stone02d487a2017-10-07 14:01:45 +01003273 struct drm_output *output = to_drm_output(output_base);
3274 struct drm_backend *b = to_drm_backend(output_base->compositor);
3275 struct drm_mode *drm_mode = choose_mode(output, mode);
Alex Wub7b8bda2012-04-17 17:20:48 +08003276
3277 if (!drm_mode) {
Daniel Stone02d487a2017-10-07 14:01:45 +01003278 weston_log("%s: invalid resolution %dx%d\n",
3279 output_base->name, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +08003280 return -1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02003281 }
3282
Hardeningff39efa2013-09-18 23:56:35 +02003283 if (&drm_mode->base == output->base.current_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08003284 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08003285
Hardeningff39efa2013-09-18 23:56:35 +02003286 output->base.current_mode->flags = 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08003287
Hardeningff39efa2013-09-18 23:56:35 +02003288 output->base.current_mode = &drm_mode->base;
3289 output->base.current_mode->flags =
Alex Wub7b8bda2012-04-17 17:20:48 +08003290 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
3291
Daniel Stonef30a18c2017-04-04 17:54:31 +01003292 /* XXX: This drops our current buffer too early, before we've started
3293 * displaying it. Ideally this should be much more atomic and
3294 * integrated with a full repaint cycle, rather than doing a
3295 * sledgehammer modeswitch first, and only later showing new
3296 * content.
3297 */
Daniel Stone6020f472018-02-05 15:46:20 +00003298 b->state_invalid = true;
Alex Wub7b8bda2012-04-17 17:20:48 +08003299
Giulio Camuffo954f1832014-10-11 18:27:30 +03003300 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003301 drm_output_fini_pixman(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003302 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003303 weston_log("failed to init output pixman state with "
3304 "new mode\n");
3305 return -1;
3306 }
3307 } else {
Daniel Stone3e661f72016-11-04 17:24:06 +00003308 drm_output_fini_egl(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003309 if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003310 weston_log("failed to init output egl state with "
3311 "new mode");
3312 return -1;
3313 }
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02003314 }
3315
Alex Wub7b8bda2012-04-17 17:20:48 +08003316 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08003317}
3318
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003319static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003320on_drm_input(int fd, uint32_t mask, void *data)
3321{
Daniel Stone598ee9d2016-11-16 11:55:20 +00003322#ifdef HAVE_DRM_ATOMIC
3323 struct drm_backend *b = data;
3324#endif
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003325 drmEventContext evctx;
3326
3327 memset(&evctx, 0, sizeof evctx);
Daniel Stone598ee9d2016-11-16 11:55:20 +00003328#ifndef HAVE_DRM_ATOMIC
Emil Velikov863e66b2017-04-04 18:07:34 +01003329 evctx.version = 2;
Daniel Stone598ee9d2016-11-16 11:55:20 +00003330#else
3331 evctx.version = 3;
3332 if (b->atomic_modeset)
3333 evctx.page_flip_handler2 = atomic_flip_handler;
3334 else
3335#endif
3336 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -05003337 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003338 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003339
3340 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003341}
3342
3343static int
Daniel Stoneefa504f2016-12-19 16:48:20 +00003344init_kms_caps(struct drm_backend *b)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003345{
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03003346 uint64_t cap;
Daniel Stoneefa504f2016-12-19 16:48:20 +00003347 int ret;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003348 clockid_t clk_id;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04003349
Daniel Stoneefa504f2016-12-19 16:48:20 +00003350 weston_log("using %s\n", b->drm.filename);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003351
Daniel Stoneefa504f2016-12-19 16:48:20 +00003352 ret = drmGetCap(b->drm.fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03003353 if (ret == 0 && cap == 1)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003354 clk_id = CLOCK_MONOTONIC;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03003355 else
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003356 clk_id = CLOCK_REALTIME;
3357
Giulio Camuffo954f1832014-10-11 18:27:30 +03003358 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003359 weston_log("Error: failed to set presentation clock %d.\n",
3360 clk_id);
3361 return -1;
3362 }
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +02003363
Daniel Stoneefa504f2016-12-19 16:48:20 +00003364 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_WIDTH, &cap);
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03003365 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03003366 b->cursor_width = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03003367 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03003368 b->cursor_width = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03003369
Daniel Stoneefa504f2016-12-19 16:48:20 +00003370 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_HEIGHT, &cap);
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03003371 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03003372 b->cursor_height = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03003373 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03003374 b->cursor_height = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03003375
Daniel Stonebe1090b2017-09-06 17:29:57 +01003376 if (!getenv("WESTON_DISABLE_UNIVERSAL_PLANES")) {
3377 ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
3378 b->universal_planes = (ret == 0);
3379 }
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01003380 weston_log("DRM: %s universal planes\n",
3381 b->universal_planes ? "supports" : "does not support");
3382
Pekka Paalanencd011a62016-11-15 22:07:49 +00003383#ifdef HAVE_DRM_ATOMIC
3384 if (b->universal_planes && !getenv("WESTON_DISABLE_ATOMIC")) {
Daniel Stone598ee9d2016-11-16 11:55:20 +00003385 ret = drmGetCap(b->drm.fd, DRM_CAP_CRTC_IN_VBLANK_EVENT, &cap);
3386 if (ret != 0)
3387 cap = 0;
Pekka Paalanencd011a62016-11-15 22:07:49 +00003388 ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_ATOMIC, 1);
Daniel Stone598ee9d2016-11-16 11:55:20 +00003389 b->atomic_modeset = ((ret == 0) && (cap == 1));
Pekka Paalanencd011a62016-11-15 22:07:49 +00003390 }
3391#endif
3392 weston_log("DRM: %s atomic modesetting\n",
3393 b->atomic_modeset ? "supports" : "does not support");
3394
Ankit Nautiyala21c3932097-03-19 00:24:57 +05303395 ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_ASPECT_RATIO, 1);
3396 b->aspect_ratio_supported = (ret == 0);
3397 weston_log("DRM: %s picture aspect ratio\n",
3398 b->aspect_ratio_supported ? "supports" : "does not support");
3399
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003400 return 0;
3401}
3402
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003403static struct gbm_device *
3404create_gbm_device(int fd)
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003405{
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003406 struct gbm_device *gbm;
Alexandru DAMIANbe0ac5b2013-10-02 17:51:05 +01003407
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003408 gl_renderer = weston_load_module("gl-renderer.so",
3409 "gl_renderer_interface");
3410 if (!gl_renderer)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003411 return NULL;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003412
3413 /* GBM will load a dri driver, but even though they need symbols from
3414 * libglapi, in some version of Mesa they are not linked to it. Since
3415 * only the gl-renderer module links to it, the call above won't make
3416 * these symbols globally available, and loading the DRI driver fails.
3417 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
3418 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
3419
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003420 gbm = gbm_create_device(fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003421
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003422 return gbm;
3423}
3424
Bryce Harringtonc056a982015-05-19 15:25:18 -07003425/* When initializing EGL, if the preferred buffer format isn't available
Bryce Harringtonb9939982016-04-15 20:28:26 -07003426 * we may be able to substitute an ARGB format for an XRGB one.
Derek Foremanc4cfe852015-05-15 12:12:40 -05003427 *
3428 * This returns 0 if substitution isn't possible, but 0 might be a
3429 * legitimate format for other EGL platforms, so the caller is
3430 * responsible for checking for 0 before calling gl_renderer->create().
3431 *
3432 * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689
3433 * but it's entirely possible we'll see this again on other implementations.
3434 */
3435static int
3436fallback_format_for(uint32_t format)
3437{
3438 switch (format) {
3439 case GBM_FORMAT_XRGB8888:
3440 return GBM_FORMAT_ARGB8888;
3441 case GBM_FORMAT_XRGB2101010:
3442 return GBM_FORMAT_ARGB2101010;
3443 default:
3444 return 0;
3445 }
3446}
3447
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003448static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003449drm_backend_create_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003450{
Derek Foreman6d556372015-11-04 14:47:33 -06003451 EGLint format[3] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01003452 b->gbm_format,
3453 fallback_format_for(b->gbm_format),
Derek Foreman6d556372015-11-04 14:47:33 -06003454 0,
Derek Foremanc4cfe852015-05-15 12:12:40 -05003455 };
Derek Foreman6d556372015-11-04 14:47:33 -06003456 int n_formats = 2;
John Kåre Alsakeref591aa2013-03-02 12:27:39 +01003457
Derek Foremanc4cfe852015-05-15 12:12:40 -05003458 if (format[1])
Derek Foreman6d556372015-11-04 14:47:33 -06003459 n_formats = 3;
Miguel A. Vicodddc6702016-05-18 17:41:07 +02003460 if (gl_renderer->display_create(b->compositor,
3461 EGL_PLATFORM_GBM_KHR,
3462 (void *)b->gbm,
Miguel A. Vico41700e32016-05-18 17:47:59 +02003463 NULL,
Miguel A. Vicodddc6702016-05-18 17:41:07 +02003464 gl_renderer->opaque_attribs,
3465 format,
3466 n_formats) < 0) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003467 return -1;
3468 }
3469
3470 return 0;
3471}
3472
3473static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003474init_egl(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003475{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003476 b->gbm = create_gbm_device(b->drm.fd);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003477
Giulio Camuffo954f1832014-10-11 18:27:30 +03003478 if (!b->gbm)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003479 return -1;
3480
Giulio Camuffo954f1832014-10-11 18:27:30 +03003481 if (drm_backend_create_gl_renderer(b) < 0) {
3482 gbm_device_destroy(b->gbm);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04003483 return -1;
3484 }
3485
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003486 return 0;
3487}
3488
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003489static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003490init_pixman(struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003491{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003492 return pixman_renderer_init(b->compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003493}
3494
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003495/**
Pekka Paalanenec272712014-06-05 11:22:25 +03003496 * Create a drm_plane for a hardware plane
3497 *
3498 * Creates one drm_plane structure for a hardware plane, and initialises its
3499 * properties and formats.
3500 *
Daniel Stone2ba17f42015-05-19 20:02:41 +01003501 * In the absence of universal plane support, where KMS does not explicitly
3502 * expose the primary and cursor planes to userspace, this may also create
3503 * an 'internal' plane for internal management.
3504 *
Pekka Paalanenec272712014-06-05 11:22:25 +03003505 * This function does not add the plane to the list of usable planes in Weston
3506 * itself; the caller is responsible for this.
3507 *
3508 * Call drm_plane_destroy to clean up the plane.
3509 *
Daniel Stone2ba17f42015-05-19 20:02:41 +01003510 * @sa drm_output_find_special_plane
Pekka Paalanenec272712014-06-05 11:22:25 +03003511 * @param b DRM compositor backend
Daniel Stone2ba17f42015-05-19 20:02:41 +01003512 * @param kplane DRM plane to create, or NULL if creating internal plane
3513 * @param output Output to create internal plane for, or NULL
3514 * @param type Type to use when creating internal plane, or invalid
3515 * @param format Format to use for internal planes, or 0
Pekka Paalanenec272712014-06-05 11:22:25 +03003516 */
3517static struct drm_plane *
Daniel Stone2ba17f42015-05-19 20:02:41 +01003518drm_plane_create(struct drm_backend *b, const drmModePlane *kplane,
3519 struct drm_output *output, enum wdrm_plane_type type,
3520 uint32_t format)
Pekka Paalanenec272712014-06-05 11:22:25 +03003521{
3522 struct drm_plane *plane;
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01003523 drmModeObjectProperties *props;
Daniel Stone2ba17f42015-05-19 20:02:41 +01003524 int num_formats = (kplane) ? kplane->count_formats : 1;
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01003525
Daniel Stone2ba17f42015-05-19 20:02:41 +01003526 plane = zalloc(sizeof(*plane) +
3527 (sizeof(uint32_t) * num_formats));
Pekka Paalanenec272712014-06-05 11:22:25 +03003528 if (!plane) {
3529 weston_log("%s: out of memory\n", __func__);
3530 return NULL;
3531 }
3532
3533 plane->backend = b;
Daniel Stonebc15f682016-11-14 16:57:01 +00003534 plane->state_cur = drm_plane_state_alloc(NULL, plane);
3535 plane->state_cur->complete = true;
Pekka Paalanenec272712014-06-05 11:22:25 +03003536
Daniel Stone2ba17f42015-05-19 20:02:41 +01003537 if (kplane) {
3538 plane->possible_crtcs = kplane->possible_crtcs;
3539 plane->plane_id = kplane->plane_id;
3540 plane->count_formats = kplane->count_formats;
3541 memcpy(plane->formats, kplane->formats,
3542 kplane->count_formats * sizeof(kplane->formats[0]));
3543
3544 props = drmModeObjectGetProperties(b->drm.fd, kplane->plane_id,
3545 DRM_MODE_OBJECT_PLANE);
3546 if (!props) {
3547 weston_log("couldn't get plane properties\n");
3548 goto err;
3549 }
3550 drm_property_info_populate(b, plane_props, plane->props,
3551 WDRM_PLANE__COUNT, props);
3552 plane->type =
3553 drm_property_get_value(&plane->props[WDRM_PLANE_TYPE],
3554 props,
3555 WDRM_PLANE_TYPE__COUNT);
3556 drmModeFreeObjectProperties(props);
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01003557 }
Daniel Stone2ba17f42015-05-19 20:02:41 +01003558 else {
3559 plane->possible_crtcs = (1 << output->pipe);
3560 plane->plane_id = 0;
3561 plane->count_formats = 1;
3562 plane->formats[0] = format;
3563 plane->type = type;
3564 }
3565
3566 if (plane->type == WDRM_PLANE_TYPE__COUNT)
3567 goto err_props;
3568
3569 /* With universal planes, everything is a DRM plane; without
3570 * universal planes, the only DRM planes are overlay planes.
3571 * Everything else is a fake plane. */
3572 if (b->universal_planes) {
3573 assert(kplane);
3574 } else {
3575 if (kplane)
3576 assert(plane->type == WDRM_PLANE_TYPE_OVERLAY);
3577 else
3578 assert(plane->type != WDRM_PLANE_TYPE_OVERLAY &&
3579 output);
3580 }
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01003581
Pekka Paalanenec272712014-06-05 11:22:25 +03003582 weston_plane_init(&plane->base, b->compositor, 0, 0);
Daniel Stone085d2b92015-05-21 00:00:57 +01003583 wl_list_insert(&b->plane_list, &plane->link);
Pekka Paalanenec272712014-06-05 11:22:25 +03003584
3585 return plane;
Daniel Stone2ba17f42015-05-19 20:02:41 +01003586
3587err_props:
3588 drm_property_info_free(plane->props, WDRM_PLANE__COUNT);
3589err:
3590 drm_plane_state_free(plane->state_cur, true);
3591 free(plane);
3592 return NULL;
3593}
3594
3595/**
3596 * Find, or create, a special-purpose plane
3597 *
3598 * Primary and cursor planes are a special case, in that before universal
3599 * planes, they are driven by non-plane API calls. Without universal plane
3600 * support, the only way to configure a primary plane is via drmModeSetCrtc,
3601 * and the only way to configure a cursor plane is drmModeSetCursor2.
3602 *
3603 * Although they may actually be regular planes in the hardware, without
3604 * universal plane support, these planes are not actually exposed to
3605 * userspace in the regular plane list.
3606 *
3607 * However, for ease of internal tracking, we want to manage all planes
3608 * through the same drm_plane structures. Therefore, when we are running
3609 * without universal plane support, we create fake drm_plane structures
3610 * to track these planes.
3611 *
3612 * @param b DRM backend
3613 * @param output Output to use for plane
3614 * @param type Type of plane
3615 */
3616static struct drm_plane *
3617drm_output_find_special_plane(struct drm_backend *b, struct drm_output *output,
3618 enum wdrm_plane_type type)
3619{
3620 struct drm_plane *plane;
3621
3622 if (!b->universal_planes) {
3623 uint32_t format;
3624
3625 switch (type) {
3626 case WDRM_PLANE_TYPE_CURSOR:
3627 format = GBM_FORMAT_ARGB8888;
3628 break;
Daniel Stonee2e80132018-01-16 15:37:33 +00003629 case WDRM_PLANE_TYPE_PRIMARY:
3630 /* We don't know what formats the primary plane supports
3631 * before universal planes, so we just assume that the
3632 * GBM format works; however, this isn't set until after
3633 * the output is created. */
3634 format = 0;
3635 break;
Daniel Stone2ba17f42015-05-19 20:02:41 +01003636 default:
3637 assert(!"invalid type in drm_output_find_special_plane");
3638 break;
3639 }
3640
3641 return drm_plane_create(b, NULL, output, type, format);
3642 }
3643
3644 wl_list_for_each(plane, &b->plane_list, link) {
3645 struct drm_output *tmp;
3646 bool found_elsewhere = false;
3647
3648 if (plane->type != type)
3649 continue;
3650 if (!drm_plane_is_available(plane, output))
3651 continue;
3652
3653 /* On some platforms, primary/cursor planes can roam
3654 * between different CRTCs, so make sure we don't claim the
3655 * same plane for two outputs. */
Daniel Stone2ba17f42015-05-19 20:02:41 +01003656 wl_list_for_each(tmp, &b->compositor->output_list,
3657 base.link) {
Daniel Stonee2e80132018-01-16 15:37:33 +00003658 if (tmp->cursor_plane == plane ||
3659 tmp->scanout_plane == plane) {
Daniel Stone2ba17f42015-05-19 20:02:41 +01003660 found_elsewhere = true;
3661 break;
3662 }
3663 }
3664
3665 if (found_elsewhere)
3666 continue;
3667
3668 plane->possible_crtcs = (1 << output->pipe);
3669 return plane;
3670 }
3671
3672 return NULL;
Pekka Paalanenec272712014-06-05 11:22:25 +03003673}
3674
3675/**
3676 * Destroy one DRM plane
3677 *
3678 * Destroy a DRM plane, removing it from screen and releasing its retained
3679 * buffers in the process. The counterpart to drm_plane_create.
3680 *
3681 * @param plane Plane to deallocate (will be freed)
3682 */
3683static void
3684drm_plane_destroy(struct drm_plane *plane)
3685{
Daniel Stone2ba17f42015-05-19 20:02:41 +01003686 if (plane->type == WDRM_PLANE_TYPE_OVERLAY)
3687 drmModeSetPlane(plane->backend->drm.fd, plane->plane_id,
3688 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
Daniel Stonebc15f682016-11-14 16:57:01 +00003689 drm_plane_state_free(plane->state_cur, true);
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01003690 drm_property_info_free(plane->props, WDRM_PLANE__COUNT);
Pekka Paalanenec272712014-06-05 11:22:25 +03003691 weston_plane_release(&plane->base);
3692 wl_list_remove(&plane->link);
3693 free(plane);
3694}
3695
3696/**
3697 * Initialise sprites (overlay planes)
3698 *
3699 * Walk the list of provided DRM planes, and add overlay planes.
3700 *
3701 * Call destroy_sprites to free these planes.
3702 *
3703 * @param b DRM compositor backend
3704 */
3705static void
3706create_sprites(struct drm_backend *b)
3707{
3708 drmModePlaneRes *kplane_res;
3709 drmModePlane *kplane;
3710 struct drm_plane *drm_plane;
3711 uint32_t i;
Pekka Paalanenec272712014-06-05 11:22:25 +03003712 kplane_res = drmModeGetPlaneResources(b->drm.fd);
3713 if (!kplane_res) {
3714 weston_log("failed to get plane resources: %s\n",
3715 strerror(errno));
3716 return;
3717 }
3718
3719 for (i = 0; i < kplane_res->count_planes; i++) {
3720 kplane = drmModeGetPlane(b->drm.fd, kplane_res->planes[i]);
3721 if (!kplane)
3722 continue;
3723
Daniel Stone2ba17f42015-05-19 20:02:41 +01003724 drm_plane = drm_plane_create(b, kplane, NULL,
3725 WDRM_PLANE_TYPE__COUNT, 0);
Pekka Paalanenec272712014-06-05 11:22:25 +03003726 drmModeFreePlane(kplane);
3727 if (!drm_plane)
3728 continue;
3729
Daniel Stone085d2b92015-05-21 00:00:57 +01003730 if (drm_plane->type == WDRM_PLANE_TYPE_OVERLAY)
3731 weston_compositor_stack_plane(b->compositor,
3732 &drm_plane->base,
3733 &b->compositor->primary_plane);
Pekka Paalanenec272712014-06-05 11:22:25 +03003734 }
3735
3736 drmModeFreePlaneResources(kplane_res);
3737}
3738
3739/**
3740 * Clean up sprites (overlay planes)
3741 *
3742 * The counterpart to create_sprites.
3743 *
3744 * @param b DRM compositor backend
3745 */
3746static void
3747destroy_sprites(struct drm_backend *b)
3748{
3749 struct drm_plane *plane, *next;
3750
Daniel Stone085d2b92015-05-21 00:00:57 +01003751 wl_list_for_each_safe(plane, next, &b->plane_list, link)
Pekka Paalanenec272712014-06-05 11:22:25 +03003752 drm_plane_destroy(plane);
3753}
3754
Pekka Paalanendc14fd42017-11-10 15:31:39 +02003755static uint32_t
3756drm_refresh_rate_mHz(const drmModeModeInfo *info)
3757{
3758 uint64_t refresh;
3759
3760 /* Calculate higher precision (mHz) refresh rate */
3761 refresh = (info->clock * 1000000LL / info->htotal +
3762 info->vtotal / 2) / info->vtotal;
3763
3764 if (info->flags & DRM_MODE_FLAG_INTERLACE)
3765 refresh *= 2;
3766 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
3767 refresh /= 2;
3768 if (info->vscan > 1)
3769 refresh /= info->vscan;
3770
3771 return refresh;
3772}
3773
Pekka Paalanenec272712014-06-05 11:22:25 +03003774/**
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003775 * Add a mode to output's mode list
3776 *
3777 * Copy the supplied DRM mode into a Weston mode structure, and add it to the
3778 * output's mode list.
3779 *
3780 * @param output DRM output to add mode to
3781 * @param info DRM mode structure to add
3782 * @returns Newly-allocated Weston/DRM mode structure
3783 */
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03003784static struct drm_mode *
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003785drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003786{
3787 struct drm_mode *mode;
3788
3789 mode = malloc(sizeof *mode);
3790 if (mode == NULL)
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03003791 return NULL;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003792
3793 mode->base.flags = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02003794 mode->base.width = info->hdisplay;
3795 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04003796
Pekka Paalanendc14fd42017-11-10 15:31:39 +02003797 mode->base.refresh = drm_refresh_rate_mHz(info);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003798 mode->mode_info = *info;
Daniel Stoned5526cb2016-11-16 10:54:10 +00003799 mode->blob_id = 0;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04003800
3801 if (info->type & DRM_MODE_TYPE_PREFERRED)
3802 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
3803
Ankit Nautiyala21c3932097-03-19 00:24:57 +05303804 mode->base.aspect_ratio = drm_to_weston_mode_aspect_ratio(info->flags);
3805
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003806 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
3807
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03003808 return mode;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003809}
3810
Daniel Stoned5526cb2016-11-16 10:54:10 +00003811/**
3812 * Destroys a mode, and removes it from the list.
3813 */
3814static void
3815drm_output_destroy_mode(struct drm_backend *backend, struct drm_mode *mode)
3816{
3817 if (mode->blob_id)
3818 drmModeDestroyPropertyBlob(backend->drm.fd, mode->blob_id);
3819 wl_list_remove(&mode->base.link);
3820 free(mode);
3821}
3822
Pekka Paalanen383b3af2017-09-11 14:40:48 +03003823/** Destroy a list of drm_modes
3824 *
3825 * @param backend The backend for releasing mode property blobs.
3826 * @param mode_list The list linked by drm_mode::base.link.
3827 */
3828static void
3829drm_mode_list_destroy(struct drm_backend *backend, struct wl_list *mode_list)
3830{
3831 struct drm_mode *mode, *next;
3832
3833 wl_list_for_each_safe(mode, next, mode_list, base.link)
3834 drm_output_destroy_mode(backend, mode);
3835}
3836
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003837static int
3838drm_subpixel_to_wayland(int drm_value)
3839{
3840 switch (drm_value) {
3841 default:
3842 case DRM_MODE_SUBPIXEL_UNKNOWN:
3843 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
3844 case DRM_MODE_SUBPIXEL_NONE:
3845 return WL_OUTPUT_SUBPIXEL_NONE;
3846 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
3847 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
3848 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
3849 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
3850 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
3851 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
3852 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
3853 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
3854 }
3855}
3856
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003857/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003858static uint32_t
Pekka Paalanence724242017-09-04 12:21:24 +03003859drm_get_backlight(struct drm_head *head)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003860{
3861 long brightness, max_brightness, norm;
3862
Pekka Paalanence724242017-09-04 12:21:24 +03003863 brightness = backlight_get_brightness(head->backlight);
3864 max_brightness = backlight_get_max_brightness(head->backlight);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003865
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003866 /* convert it on a scale of 0 to 255 */
3867 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003868
3869 return (uint32_t) norm;
3870}
3871
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003872/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003873static void
3874drm_set_backlight(struct weston_output *output_base, uint32_t value)
3875{
Pekka Paalanenecc8cce2017-09-12 16:14:31 +03003876 struct drm_output *output = to_drm_output(output_base);
3877 struct drm_head *head;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003878 long max_brightness, new_brightness;
3879
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003880 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003881 return;
3882
Pekka Paalanenecc8cce2017-09-12 16:14:31 +03003883 wl_list_for_each(head, &output->base.head_list, base.output_link) {
3884 if (!head->backlight)
3885 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003886
Pekka Paalanenecc8cce2017-09-12 16:14:31 +03003887 max_brightness = backlight_get_max_brightness(head->backlight);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003888
Pekka Paalanenecc8cce2017-09-12 16:14:31 +03003889 /* get denormalized value */
3890 new_brightness = (value * max_brightness) / 255;
3891
3892 backlight_set_brightness(head->backlight, new_brightness);
3893 }
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003894}
3895
Pekka Paalanenf8b850d2017-11-15 12:51:01 +02003896static void
3897drm_output_init_backlight(struct drm_output *output)
3898{
3899 struct weston_head *base;
3900 struct drm_head *head;
3901
3902 output->base.set_backlight = NULL;
3903
3904 wl_list_for_each(base, &output->base.head_list, output_link) {
3905 head = to_drm_head(base);
3906
3907 if (head->backlight) {
3908 weston_log("Initialized backlight for head '%s', device %s\n",
3909 head->base.name, head->backlight->path);
3910
3911 if (!output->base.set_backlight) {
3912 output->base.set_backlight = drm_set_backlight;
3913 output->base.backlight_current =
3914 drm_get_backlight(head);
3915 }
3916 }
3917 }
3918
3919 if (!output->base.set_backlight) {
3920 weston_log("No backlight control for output '%s'\n",
3921 output->base.name);
3922 }
3923}
3924
Daniel Stonea08512f2016-11-08 17:46:10 +00003925/**
3926 * Power output on or off
3927 *
3928 * The DPMS/power level of an output is used to switch it on or off. This
3929 * is DRM's hook for doing so, which can called either as part of repaint,
3930 * or independently of the repaint loop.
3931 *
3932 * If we are called as part of repaint, we simply set the relevant bit in
3933 * state and return.
3934 */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003935static void
3936drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
3937{
Armin Krezović545dba62016-08-05 15:54:18 +02003938 struct drm_output *output = to_drm_output(output_base);
Daniel Stonea08512f2016-11-08 17:46:10 +00003939 struct drm_backend *b = to_drm_backend(output_base->compositor);
3940 struct drm_pending_state *pending_state = b->repaint_data;
3941 struct drm_output_state *state;
Daniel Stone36609c72015-06-18 07:49:02 +01003942 int ret;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003943
Daniel Stonea08512f2016-11-08 17:46:10 +00003944 if (output->state_cur->dpms == level)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003945 return;
3946
Daniel Stonea08512f2016-11-08 17:46:10 +00003947 /* If we're being called during the repaint loop, then this is
3948 * simple: discard any previously-generated state, and create a new
3949 * state where we disable everything. When we come to flush, this
3950 * will be applied.
3951 *
3952 * However, we need to be careful: we can be called whilst another
3953 * output is in its repaint cycle (pending_state exists), but our
3954 * output still has an incomplete state application outstanding.
3955 * In that case, we need to wait until that completes. */
3956 if (pending_state && !output->state_last) {
3957 /* The repaint loop already sets DPMS on; we don't need to
3958 * explicitly set it on here, as it will already happen
3959 * whilst applying the repaint state. */
3960 if (level == WESTON_DPMS_ON)
3961 return;
3962
3963 state = drm_pending_state_get_output(pending_state, output);
3964 if (state)
3965 drm_output_state_free(state);
3966 state = drm_output_get_disable_state(pending_state, output);
Daniel Stone36609c72015-06-18 07:49:02 +01003967 return;
3968 }
3969
Daniel Stonea08512f2016-11-08 17:46:10 +00003970 /* As we throw everything away when disabling, just send us back through
3971 * a repaint cycle. */
3972 if (level == WESTON_DPMS_ON) {
3973 if (output->dpms_off_pending)
3974 output->dpms_off_pending = 0;
3975 weston_output_schedule_repaint(output_base);
3976 return;
3977 }
3978
3979 /* If we've already got a request in the pipeline, then we need to
3980 * park our DPMS request until that request has quiesced. */
3981 if (output->state_last) {
3982 output->dpms_off_pending = 1;
3983 return;
3984 }
3985
3986 pending_state = drm_pending_state_alloc(b);
3987 drm_output_get_disable_state(pending_state, output);
3988 ret = drm_pending_state_apply_sync(pending_state);
3989 if (ret != 0)
3990 weston_log("drm_set_dpms: couldn't disable output?\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003991}
3992
Pekka Paalanen3ce63622014-06-04 16:29:49 +03003993static const char * const connector_type_names[] = {
Pekka Paalanen89c49b32015-08-19 15:25:57 +03003994 [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
3995 [DRM_MODE_CONNECTOR_VGA] = "VGA",
3996 [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
3997 [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
3998 [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
3999 [DRM_MODE_CONNECTOR_Composite] = "Composite",
4000 [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
4001 [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
4002 [DRM_MODE_CONNECTOR_Component] = "Component",
4003 [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
4004 [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
4005 [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
4006 [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
4007 [DRM_MODE_CONNECTOR_TV] = "TV",
4008 [DRM_MODE_CONNECTOR_eDP] = "eDP",
Pekka Paalanenab81f152015-08-24 14:27:07 +03004009#ifdef DRM_MODE_CONNECTOR_DSI
Pekka Paalanen89c49b32015-08-19 15:25:57 +03004010 [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
4011 [DRM_MODE_CONNECTOR_DSI] = "DSI",
Pekka Paalanenab81f152015-08-24 14:27:07 +03004012#endif
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04004013};
4014
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03004015/** Create a name given a DRM connector
4016 *
4017 * \param con The DRM connector whose type and id form the name.
4018 * \return A newly allocate string, or NULL on error. Must be free()'d
4019 * after use.
4020 *
4021 * The name does not identify the DRM display device.
4022 */
Pekka Paalanen3ce63622014-06-04 16:29:49 +03004023static char *
4024make_connector_name(const drmModeConnector *con)
4025{
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03004026 char *name;
Pekka Paalanen89c49b32015-08-19 15:25:57 +03004027 const char *type_name = NULL;
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03004028 int ret;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03004029
4030 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
4031 type_name = connector_type_names[con->connector_type];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03004032
4033 if (!type_name)
4034 type_name = "UNNAMED";
4035
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03004036 ret = asprintf(&name, "%s-%d", type_name, con->connector_type_id);
4037 if (ret < 0)
4038 return NULL;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03004039
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03004040 return name;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03004041}
4042
Daniel Stonee4256832017-04-04 17:54:27 +01004043static void drm_output_fini_cursor_egl(struct drm_output *output)
4044{
4045 unsigned int i;
4046
4047 for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) {
4048 drm_fb_unref(output->gbm_cursor_fb[i]);
4049 output->gbm_cursor_fb[i] = NULL;
4050 }
4051}
4052
4053static int
4054drm_output_init_cursor_egl(struct drm_output *output, struct drm_backend *b)
4055{
4056 unsigned int i;
4057
Daniel Stone2ba17f42015-05-19 20:02:41 +01004058 /* No point creating cursors if we don't have a plane for them. */
4059 if (!output->cursor_plane)
4060 return 0;
4061
Daniel Stonee4256832017-04-04 17:54:27 +01004062 for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) {
4063 struct gbm_bo *bo;
4064
4065 bo = gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
4066 GBM_FORMAT_ARGB8888,
4067 GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE);
4068 if (!bo)
4069 goto err;
4070
4071 output->gbm_cursor_fb[i] =
4072 drm_fb_get_from_bo(bo, b, GBM_FORMAT_ARGB8888,
4073 BUFFER_CURSOR);
4074 if (!output->gbm_cursor_fb[i]) {
4075 gbm_bo_destroy(bo);
4076 goto err;
4077 }
4078 }
4079
4080 return 0;
4081
4082err:
4083 weston_log("cursor buffers unavailable, using gl cursors\n");
4084 b->cursors_are_broken = 1;
4085 drm_output_fini_cursor_egl(output);
4086 return -1;
4087}
4088
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02004089/* Init output state that depends on gl or gbm */
4090static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03004091drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02004092{
Derek Foremanc4cfe852015-05-15 12:12:40 -05004093 EGLint format[2] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01004094 output->gbm_format,
4095 fallback_format_for(output->gbm_format),
Derek Foremanc4cfe852015-05-15 12:12:40 -05004096 };
Daniel Stonee4256832017-04-04 17:54:27 +01004097 int n_formats = 1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02004098
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01004099 output->gbm_surface = gbm_surface_create(b->gbm,
Hardeningff39efa2013-09-18 23:56:35 +02004100 output->base.current_mode->width,
4101 output->base.current_mode->height,
Derek Foremanc4cfe852015-05-15 12:12:40 -05004102 format[0],
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02004103 GBM_BO_USE_SCANOUT |
4104 GBM_BO_USE_RENDERING);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01004105 if (!output->gbm_surface) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02004106 weston_log("failed to create gbm surface\n");
4107 return -1;
4108 }
4109
Derek Foremanc4cfe852015-05-15 12:12:40 -05004110 if (format[1])
4111 n_formats = 2;
Miguel A. Vicoc095cde2016-05-18 17:43:00 +02004112 if (gl_renderer->output_window_create(&output->base,
4113 (EGLNativeWindowType)output->gbm_surface,
4114 output->gbm_surface,
4115 gl_renderer->opaque_attribs,
4116 format,
4117 n_formats) < 0) {
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02004118 weston_log("failed to create gl renderer output state\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01004119 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02004120 return -1;
4121 }
4122
Daniel Stonee4256832017-04-04 17:54:27 +01004123 drm_output_init_cursor_egl(output, b);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02004124
4125 return 0;
4126}
4127
Daniel Stone3e661f72016-11-04 17:24:06 +00004128static void
4129drm_output_fini_egl(struct drm_output *output)
4130{
Daniel Stonee2e80132018-01-16 15:37:33 +00004131 struct drm_backend *b = to_drm_backend(output->base.compositor);
4132
4133 /* Destroying the GBM surface will destroy all our GBM buffers,
4134 * regardless of refcount. Ensure we destroy them here. */
4135 if (!b->shutting_down &&
4136 output->scanout_plane->state_cur->fb &&
4137 output->scanout_plane->state_cur->fb->type == BUFFER_GBM_SURFACE) {
4138 drm_plane_state_free(output->scanout_plane->state_cur, true);
4139 output->scanout_plane->state_cur =
4140 drm_plane_state_alloc(NULL, output->scanout_plane);
4141 output->scanout_plane->state_cur->complete = true;
4142 }
4143
Daniel Stone3e661f72016-11-04 17:24:06 +00004144 gl_renderer->output_destroy(&output->base);
4145 gbm_surface_destroy(output->gbm_surface);
Daniel Stonee4256832017-04-04 17:54:27 +01004146 drm_output_fini_cursor_egl(output);
Daniel Stone3e661f72016-11-04 17:24:06 +00004147}
4148
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04004149static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03004150drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004151{
Hardeningff39efa2013-09-18 23:56:35 +02004152 int w = output->base.current_mode->width;
4153 int h = output->base.current_mode->height;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03004154 uint32_t format = output->gbm_format;
4155 uint32_t pixman_format;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004156 unsigned int i;
Pekka Paalanendee412d2018-04-23 11:44:58 +02004157 uint32_t flags = 0;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004158
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03004159 switch (format) {
4160 case GBM_FORMAT_XRGB8888:
4161 pixman_format = PIXMAN_x8r8g8b8;
4162 break;
4163 case GBM_FORMAT_RGB565:
4164 pixman_format = PIXMAN_r5g6b5;
4165 break;
4166 default:
4167 weston_log("Unsupported pixman format 0x%x\n", format);
4168 return -1;
4169 }
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004170
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03004171 /* FIXME error checking */
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004172 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03004173 output->dumb[i] = drm_fb_create_dumb(b, w, h, format);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004174 if (!output->dumb[i])
4175 goto err;
4176
4177 output->image[i] =
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03004178 pixman_image_create_bits(pixman_format, w, h,
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004179 output->dumb[i]->map,
4180 output->dumb[i]->stride);
4181 if (!output->image[i])
4182 goto err;
4183 }
4184
Pekka Paalanendee412d2018-04-23 11:44:58 +02004185 if (b->use_pixman_shadow)
4186 flags |= PIXMAN_RENDERER_OUTPUT_USE_SHADOW;
4187
4188 if (pixman_renderer_output_create(&output->base, flags) < 0)
4189 goto err;
Ankit Nautiyala21c3932097-03-19 00:24:57 +05304190
Pekka Paalanendee412d2018-04-23 11:44:58 +02004191 weston_log("DRM: output %s %s shadow framebuffer.\n", output->base.name,
4192 b->use_pixman_shadow ? "uses" : "does not use");
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004193
4194 pixman_region32_init_rect(&output->previous_damage,
Alexander Larsson0b135062013-05-28 16:23:36 +02004195 output->base.x, output->base.y, output->base.width, output->base.height);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004196
4197 return 0;
4198
4199err:
4200 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
4201 if (output->dumb[i])
Daniel Stone6e7a9612017-04-04 17:54:26 +01004202 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004203 if (output->image[i])
4204 pixman_image_unref(output->image[i]);
4205
4206 output->dumb[i] = NULL;
4207 output->image[i] = NULL;
4208 }
4209
4210 return -1;
4211}
4212
4213static void
4214drm_output_fini_pixman(struct drm_output *output)
4215{
Daniel Stonee2e80132018-01-16 15:37:33 +00004216 struct drm_backend *b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004217 unsigned int i;
4218
Daniel Stonee2e80132018-01-16 15:37:33 +00004219 /* Destroying the Pixman surface will destroy all our buffers,
4220 * regardless of refcount. Ensure we destroy them here. */
4221 if (!b->shutting_down &&
4222 output->scanout_plane->state_cur->fb &&
4223 output->scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB) {
4224 drm_plane_state_free(output->scanout_plane->state_cur, true);
4225 output->scanout_plane->state_cur =
4226 drm_plane_state_alloc(NULL, output->scanout_plane);
4227 output->scanout_plane->state_cur->complete = true;
4228 }
4229
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004230 pixman_renderer_output_destroy(&output->base);
4231 pixman_region32_fini(&output->previous_damage);
4232
4233 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004234 pixman_image_unref(output->image[i]);
Daniel Stone6e7a9612017-04-04 17:54:26 +01004235 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004236 output->dumb[i] = NULL;
4237 output->image[i] = NULL;
4238 }
4239}
4240
Richard Hughes2b2092a2013-04-24 14:58:02 +01004241static void
4242edid_parse_string(const uint8_t *data, char text[])
4243{
4244 int i;
4245 int replaced = 0;
4246
4247 /* this is always 12 bytes, but we can't guarantee it's null
4248 * terminated or not junk. */
4249 strncpy(text, (const char *) data, 12);
4250
Bryce Harrington9c7de162015-08-28 13:04:26 -07004251 /* guarantee our new string is null-terminated */
4252 text[12] = '\0';
4253
Richard Hughes2b2092a2013-04-24 14:58:02 +01004254 /* remove insane chars */
4255 for (i = 0; text[i] != '\0'; i++) {
4256 if (text[i] == '\n' ||
4257 text[i] == '\r') {
4258 text[i] = '\0';
4259 break;
4260 }
4261 }
4262
4263 /* ensure string is printable */
4264 for (i = 0; text[i] != '\0'; i++) {
4265 if (!isprint(text[i])) {
4266 text[i] = '-';
4267 replaced++;
4268 }
4269 }
4270
4271 /* if the string is random junk, ignore the string */
4272 if (replaced > 4)
4273 text[0] = '\0';
4274}
4275
4276#define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
4277#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
4278#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
4279#define EDID_OFFSET_DATA_BLOCKS 0x36
4280#define EDID_OFFSET_LAST_BLOCK 0x6c
4281#define EDID_OFFSET_PNPID 0x08
4282#define EDID_OFFSET_SERIAL 0x0c
4283
4284static int
4285edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
4286{
4287 int i;
4288 uint32_t serial_number;
4289
4290 /* check header */
4291 if (length < 128)
4292 return -1;
4293 if (data[0] != 0x00 || data[1] != 0xff)
4294 return -1;
4295
4296 /* decode the PNP ID from three 5 bit words packed into 2 bytes
4297 * /--08--\/--09--\
4298 * 7654321076543210
4299 * |\---/\---/\---/
4300 * R C1 C2 C3 */
4301 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
4302 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
4303 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
4304 edid->pnp_id[3] = '\0';
4305
4306 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
4307 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
4308 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
4309 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
4310 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
4311 if (serial_number > 0)
4312 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
4313
4314 /* parse EDID data */
4315 for (i = EDID_OFFSET_DATA_BLOCKS;
4316 i <= EDID_OFFSET_LAST_BLOCK;
4317 i += 18) {
4318 /* ignore pixel clock data */
4319 if (data[i] != 0)
4320 continue;
4321 if (data[i+2] != 0)
4322 continue;
4323
4324 /* any useful blocks? */
4325 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
4326 edid_parse_string(&data[i+5],
4327 edid->monitor_name);
4328 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
4329 edid_parse_string(&data[i+5],
4330 edid->serial_number);
4331 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
4332 edid_parse_string(&data[i+5],
4333 edid->eisa_id);
4334 }
4335 }
4336 return 0;
4337}
4338
Pekka Paalanen6f1866b2017-04-03 14:22:51 +03004339/** Parse monitor make, model and serial from EDID
4340 *
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03004341 * \param head The head whose \c drm_edid to fill in.
Pekka Paalanen6f1866b2017-04-03 14:22:51 +03004342 * \param props The DRM connector properties to get the EDID from.
4343 * \param make[out] The monitor make (PNP ID).
4344 * \param model[out] The monitor model (name).
4345 * \param serial_number[out] The monitor serial number.
4346 *
4347 * Each of \c *make, \c *model and \c *serial_number are set only if the
4348 * information is found in the EDID. The pointers they are set to must not
4349 * be free()'d explicitly, instead they get implicitly freed when the
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03004350 * \c drm_head is destroyed.
Pekka Paalanen6f1866b2017-04-03 14:22:51 +03004351 */
Richard Hughes2b2092a2013-04-24 14:58:02 +01004352static void
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03004353find_and_parse_output_edid(struct drm_head *head,
Pekka Paalanen6f1866b2017-04-03 14:22:51 +03004354 drmModeObjectPropertiesPtr props,
4355 const char **make,
4356 const char **model,
4357 const char **serial_number)
Richard Hughes2b2092a2013-04-24 14:58:02 +01004358{
4359 drmModePropertyBlobPtr edid_blob = NULL;
Daniel Stone02cf4662017-03-03 16:19:39 +00004360 uint32_t blob_id;
Richard Hughes2b2092a2013-04-24 14:58:02 +01004361 int rc;
4362
Daniel Stone02cf4662017-03-03 16:19:39 +00004363 blob_id =
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03004364 drm_property_get_value(&head->props_conn[WDRM_CONNECTOR_EDID],
Daniel Stone02cf4662017-03-03 16:19:39 +00004365 props, 0);
4366 if (!blob_id)
4367 return;
4368
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03004369 edid_blob = drmModeGetPropertyBlob(head->backend->drm.fd, blob_id);
Richard Hughes2b2092a2013-04-24 14:58:02 +01004370 if (!edid_blob)
4371 return;
4372
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03004373 rc = edid_parse(&head->edid,
Richard Hughes2b2092a2013-04-24 14:58:02 +01004374 edid_blob->data,
4375 edid_blob->length);
4376 if (!rc) {
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03004377 if (head->edid.pnp_id[0] != '\0')
4378 *make = head->edid.pnp_id;
4379 if (head->edid.monitor_name[0] != '\0')
4380 *model = head->edid.monitor_name;
4381 if (head->edid.serial_number[0] != '\0')
4382 *serial_number = head->edid.serial_number;
Richard Hughes2b2092a2013-04-24 14:58:02 +01004383 }
4384 drmModeFreePropertyBlob(edid_blob);
4385}
4386
Kristian Høgsberga30989a2013-05-23 17:23:15 -04004387static int
4388parse_modeline(const char *s, drmModeModeInfo *mode)
4389{
4390 char hsync[16];
4391 char vsync[16];
4392 float fclock;
4393
Pekka Paalanendc4e3c62017-12-05 15:37:41 +02004394 memset(mode, 0, sizeof *mode);
4395
Kristian Høgsberga30989a2013-05-23 17:23:15 -04004396 mode->type = DRM_MODE_TYPE_USERDEF;
4397 mode->hskew = 0;
4398 mode->vscan = 0;
4399 mode->vrefresh = 0;
4400 mode->flags = 0;
4401
Rob Bradford307e09e2013-07-26 16:29:40 +01004402 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
Kristian Høgsberga30989a2013-05-23 17:23:15 -04004403 &fclock,
4404 &mode->hdisplay,
4405 &mode->hsync_start,
4406 &mode->hsync_end,
4407 &mode->htotal,
4408 &mode->vdisplay,
4409 &mode->vsync_start,
4410 &mode->vsync_end,
4411 &mode->vtotal, hsync, vsync) != 11)
4412 return -1;
4413
4414 mode->clock = fclock * 1000;
Guido Günther92278e02018-06-26 20:40:08 +02004415 if (strcasecmp(hsync, "+hsync") == 0)
Kristian Høgsberga30989a2013-05-23 17:23:15 -04004416 mode->flags |= DRM_MODE_FLAG_PHSYNC;
Guido Günther92278e02018-06-26 20:40:08 +02004417 else if (strcasecmp(hsync, "-hsync") == 0)
Kristian Høgsberga30989a2013-05-23 17:23:15 -04004418 mode->flags |= DRM_MODE_FLAG_NHSYNC;
4419 else
4420 return -1;
4421
Guido Günther92278e02018-06-26 20:40:08 +02004422 if (strcasecmp(vsync, "+vsync") == 0)
Kristian Høgsberga30989a2013-05-23 17:23:15 -04004423 mode->flags |= DRM_MODE_FLAG_PVSYNC;
Guido Günther92278e02018-06-26 20:40:08 +02004424 else if (strcasecmp(vsync, "-vsync") == 0)
Kristian Høgsberga30989a2013-05-23 17:23:15 -04004425 mode->flags |= DRM_MODE_FLAG_NVSYNC;
4426 else
4427 return -1;
4428
Emmanuel Gil Peyrota62138b2016-05-02 22:40:11 +01004429 snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f",
4430 mode->hdisplay, mode->vdisplay, fclock);
4431
Kristian Høgsberga30989a2013-05-23 17:23:15 -04004432 return 0;
4433}
4434
Rob Bradford66bd9f52013-06-25 18:56:42 +01004435static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03004436setup_output_seat_constraint(struct drm_backend *b,
Rob Bradford66bd9f52013-06-25 18:56:42 +01004437 struct weston_output *output,
4438 const char *s)
4439{
4440 if (strcmp(s, "") != 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05004441 struct weston_pointer *pointer;
Rob Bradford66bd9f52013-06-25 18:56:42 +01004442 struct udev_seat *seat;
4443
Giulio Camuffo954f1832014-10-11 18:27:30 +03004444 seat = udev_seat_get_named(&b->input, s);
Derek Foreman0720ea32015-07-15 13:00:35 -05004445 if (!seat)
4446 return;
Rob Bradford66bd9f52013-06-25 18:56:42 +01004447
Derek Foreman0720ea32015-07-15 13:00:35 -05004448 seat->base.output = output;
4449
Derek Foreman1281a362015-07-31 16:55:32 -05004450 pointer = weston_seat_get_pointer(&seat->base);
4451 if (pointer)
4452 weston_pointer_clamp(pointer,
4453 &pointer->x,
4454 &pointer->y);
Rob Bradford66bd9f52013-06-25 18:56:42 +01004455 }
4456}
4457
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004458static int
Pekka Paalanenc112f002017-08-28 16:27:20 +03004459drm_output_attach_head(struct weston_output *output_base,
4460 struct weston_head *head_base)
4461{
Pekka Paalanend5f98d82017-12-08 14:45:00 +02004462 struct drm_backend *b = to_drm_backend(output_base->compositor);
4463
Pekka Paalanenc112f002017-08-28 16:27:20 +03004464 if (wl_list_length(&output_base->head_list) >= MAX_CLONED_CONNECTORS)
4465 return -1;
4466
Pekka Paalanend5f98d82017-12-08 14:45:00 +02004467 if (!output_base->enabled)
4468 return 0;
4469
4470 /* XXX: ensure the configuration will work.
4471 * This is actually impossible without major infrastructure
4472 * work. */
4473
4474 /* Need to go through modeset to add connectors. */
4475 /* XXX: Ideally we'd do this per-output, not globally. */
4476 /* XXX: Doing it globally, what guarantees another output's update
4477 * will not clear the flag before this output is updated?
4478 */
4479 b->state_invalid = true;
4480
4481 weston_output_schedule_repaint(output_base);
4482
Pekka Paalanenc112f002017-08-28 16:27:20 +03004483 return 0;
4484}
4485
Pekka Paalanen7f853792017-11-29 14:33:33 +02004486static void
4487drm_output_detach_head(struct weston_output *output_base,
4488 struct weston_head *head_base)
4489{
4490 struct drm_backend *b = to_drm_backend(output_base->compositor);
4491
4492 if (!output_base->enabled)
4493 return;
4494
4495 /* Need to go through modeset to drop connectors that should no longer
4496 * be driven. */
4497 /* XXX: Ideally we'd do this per-output, not globally. */
4498 b->state_invalid = true;
4499
4500 weston_output_schedule_repaint(output_base);
4501}
4502
Pekka Paalanenc112f002017-08-28 16:27:20 +03004503static int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004504parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format)
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004505{
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004506 int ret = 0;
4507
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004508 if (s == NULL)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004509 *gbm_format = default_value;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004510 else if (strcmp(s, "xrgb8888") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004511 *gbm_format = GBM_FORMAT_XRGB8888;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004512 else if (strcmp(s, "rgb565") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004513 *gbm_format = GBM_FORMAT_RGB565;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004514 else if (strcmp(s, "xrgb2101010") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004515 *gbm_format = GBM_FORMAT_XRGB2101010;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004516 else {
4517 weston_log("fatal: unrecognized pixel format: %s\n", s);
4518 ret = -1;
4519 }
4520
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004521 return ret;
4522}
4523
Pekka Paalanenf005f252017-11-10 16:34:39 +02004524static uint32_t
4525u32distance(uint32_t a, uint32_t b)
4526{
4527 if (a < b)
4528 return b - a;
4529 else
4530 return a - b;
4531}
4532
4533/** Choose equivalent mode
4534 *
4535 * If the two modes are not equivalent, return NULL.
4536 * Otherwise return the mode that is more likely to work in place of both.
4537 *
4538 * None of the fuzzy matching criteria in this function have any justification.
4539 *
4540 * typedef struct _drmModeModeInfo {
4541 * uint32_t clock;
4542 * uint16_t hdisplay, hsync_start, hsync_end, htotal, hskew;
4543 * uint16_t vdisplay, vsync_start, vsync_end, vtotal, vscan;
4544 *
4545 * uint32_t vrefresh;
4546 *
4547 * uint32_t flags;
4548 * uint32_t type;
4549 * char name[DRM_DISPLAY_MODE_LEN];
4550 * } drmModeModeInfo, *drmModeModeInfoPtr;
4551 */
4552static const drmModeModeInfo *
4553drm_mode_pick_equivalent(const drmModeModeInfo *a, const drmModeModeInfo *b)
4554{
4555 uint32_t refresh_a, refresh_b;
4556
4557 if (a->hdisplay != b->hdisplay || a->vdisplay != b->vdisplay)
4558 return NULL;
4559
4560 if (a->flags != b->flags)
4561 return NULL;
4562
4563 /* kHz */
4564 if (u32distance(a->clock, b->clock) > 500)
4565 return NULL;
4566
4567 refresh_a = drm_refresh_rate_mHz(a);
4568 refresh_b = drm_refresh_rate_mHz(b);
4569 if (u32distance(refresh_a, refresh_b) > 50)
4570 return NULL;
4571
4572 if ((a->type ^ b->type) & DRM_MODE_TYPE_PREFERRED) {
4573 if (a->type & DRM_MODE_TYPE_PREFERRED)
4574 return a;
4575 else
4576 return b;
4577 }
4578
4579 return a;
4580}
4581
4582/* If the given mode info is not already in the list, add it.
4583 * If it is in the list, either keep the existing or replace it,
4584 * depending on which one is "better".
4585 */
4586static int
4587drm_output_try_add_mode(struct drm_output *output, const drmModeModeInfo *info)
4588{
4589 struct weston_mode *base;
4590 struct drm_mode *mode;
4591 struct drm_backend *backend;
4592 const drmModeModeInfo *chosen = NULL;
4593
4594 assert(info);
4595
4596 wl_list_for_each(base, &output->base.mode_list, link) {
4597 mode = to_drm_mode(base);
4598 chosen = drm_mode_pick_equivalent(&mode->mode_info, info);
4599 if (chosen)
4600 break;
4601 }
4602
4603 if (chosen == info) {
4604 backend = to_drm_backend(output->base.compositor);
4605 drm_output_destroy_mode(backend, mode);
4606 chosen = NULL;
4607 }
4608
4609 if (!chosen) {
4610 mode = drm_output_add_mode(output, info);
4611 if (!mode)
4612 return -1;
4613 }
4614 /* else { the equivalent mode is already in the list } */
4615
4616 return 0;
4617}
4618
Pekka Paalanen4be24852017-09-11 15:01:12 +03004619/** Rewrite the output's mode list
4620 *
4621 * @param output The output.
4622 * @return 0 on success, -1 on failure.
4623 *
4624 * Destroy all existing modes in the list, and reconstruct a new list from
4625 * scratch, based on the currently attached heads.
4626 *
4627 * On failure the output's mode list may contain some modes.
4628 */
4629static int
4630drm_output_update_modelist_from_heads(struct drm_output *output)
4631{
4632 struct drm_backend *backend = to_drm_backend(output->base.compositor);
4633 struct weston_head *head_base;
4634 struct drm_head *head;
Pekka Paalanen4be24852017-09-11 15:01:12 +03004635 int i;
Pekka Paalanenf005f252017-11-10 16:34:39 +02004636 int ret;
Pekka Paalanen4be24852017-09-11 15:01:12 +03004637
4638 assert(!output->base.enabled);
4639
4640 drm_mode_list_destroy(backend, &output->base.mode_list);
4641
Pekka Paalanenf005f252017-11-10 16:34:39 +02004642 wl_list_for_each(head_base, &output->base.head_list, output_link) {
4643 head = to_drm_head(head_base);
4644 for (i = 0; i < head->connector->count_modes; i++) {
4645 ret = drm_output_try_add_mode(output,
4646 &head->connector->modes[i]);
4647 if (ret < 0)
4648 return -1;
4649 }
Pekka Paalanen4be24852017-09-11 15:01:12 +03004650 }
4651
4652 return 0;
4653}
4654
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004655/**
4656 * Choose suitable mode for an output
4657 *
4658 * Find the most suitable mode to use for initial setup (or reconfiguration on
4659 * hotplug etc) for a DRM output.
4660 *
4661 * @param output DRM output to choose mode for
4662 * @param kind Strategy and preference to use when choosing mode
4663 * @param width Desired width for this output
4664 * @param height Desired height for this output
4665 * @param current_mode Mode currently being displayed on this output
4666 * @param modeline Manually-entered mode (may be NULL)
4667 * @returns A mode from the output's mode list, or NULL if none available
4668 */
4669static struct drm_mode *
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004670drm_output_choose_initial_mode(struct drm_backend *backend,
4671 struct drm_output *output,
4672 enum weston_drm_backend_output_mode mode,
Armin Krezović08368132016-09-30 14:11:05 +02004673 const char *modeline,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004674 const drmModeModeInfo *current_mode)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004675{
4676 struct drm_mode *preferred = NULL;
4677 struct drm_mode *current = NULL;
4678 struct drm_mode *configured = NULL;
Ankit Nautiyala21c3932097-03-19 00:24:57 +05304679 struct drm_mode *config_fall_back = NULL;
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004680 struct drm_mode *best = NULL;
4681 struct drm_mode *drm_mode;
Armin Krezović08368132016-09-30 14:11:05 +02004682 drmModeModeInfo drm_modeline;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004683 int32_t width = 0;
4684 int32_t height = 0;
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01004685 uint32_t refresh = 0;
Ankit Nautiyala21c3932097-03-19 00:24:57 +05304686 uint32_t aspect_width = 0;
4687 uint32_t aspect_height = 0;
4688 enum weston_mode_aspect_ratio aspect_ratio = WESTON_MODE_PIC_AR_NONE;
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01004689 int n;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004690
Armin Krezović08368132016-09-30 14:11:05 +02004691 if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) {
Ankit Nautiyala21c3932097-03-19 00:24:57 +05304692 n = sscanf(modeline, "%dx%d@%d %u:%u", &width, &height,
4693 &refresh, &aspect_width, &aspect_height);
4694 if (backend->aspect_ratio_supported && n == 5) {
4695 if (aspect_width == 4 && aspect_height == 3)
4696 aspect_ratio = WESTON_MODE_PIC_AR_4_3;
4697 else if (aspect_width == 16 && aspect_height == 9)
4698 aspect_ratio = WESTON_MODE_PIC_AR_16_9;
4699 else if (aspect_width == 64 && aspect_height == 27)
4700 aspect_ratio = WESTON_MODE_PIC_AR_64_27;
4701 else if (aspect_width == 256 && aspect_height == 135)
4702 aspect_ratio = WESTON_MODE_PIC_AR_256_135;
4703 else
4704 weston_log("Invalid modeline \"%s\" for output %s\n",
4705 modeline, output->base.name);
4706 }
4707 if (n != 2 && n != 3 && n != 5) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004708 width = -1;
4709
Armin Krezović08368132016-09-30 14:11:05 +02004710 if (parse_modeline(modeline, &drm_modeline) == 0) {
4711 configured = drm_output_add_mode(output, &drm_modeline);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004712 if (!configured)
4713 return NULL;
4714 } else {
4715 weston_log("Invalid modeline \"%s\" for output %s\n",
Armin Krezović08368132016-09-30 14:11:05 +02004716 modeline, output->base.name);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004717 }
4718 }
4719 }
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004720
4721 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004722 if (width == drm_mode->base.width &&
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01004723 height == drm_mode->base.height &&
Ankit Nautiyala21c3932097-03-19 00:24:57 +05304724 (refresh == 0 || refresh == drm_mode->mode_info.vrefresh)) {
4725 if (!backend->aspect_ratio_supported ||
4726 aspect_ratio == drm_mode->base.aspect_ratio)
4727 configured = drm_mode;
4728 else
4729 config_fall_back = drm_mode;
4730 }
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004731
comic fans7a5c5622016-03-17 14:29:27 +02004732 if (memcmp(current_mode, &drm_mode->mode_info,
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004733 sizeof *current_mode) == 0)
4734 current = drm_mode;
4735
4736 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
4737 preferred = drm_mode;
4738
4739 best = drm_mode;
4740 }
4741
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004742 if (current == NULL && current_mode->clock != 0) {
4743 current = drm_output_add_mode(output, current_mode);
4744 if (!current)
4745 return NULL;
4746 }
4747
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004748 if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004749 configured = current;
4750
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004751 if (configured)
4752 return configured;
4753
Ankit Nautiyala21c3932097-03-19 00:24:57 +05304754 if (config_fall_back)
4755 return config_fall_back;
4756
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004757 if (preferred)
4758 return preferred;
4759
4760 if (current)
4761 return current;
4762
4763 if (best)
4764 return best;
4765
4766 weston_log("no available modes for %s\n", output->base.name);
4767 return NULL;
4768}
4769
Pekka Paalaneneee580b2014-06-04 16:43:06 +03004770static int
Pekka Paalanen9c03a7c2017-11-28 14:30:10 +02004771drm_head_read_current_setup(struct drm_head *head, struct drm_backend *backend)
Pekka Paalaneneee580b2014-06-04 16:43:06 +03004772{
Pekka Paalanen9c03a7c2017-11-28 14:30:10 +02004773 int drm_fd = backend->drm.fd;
Pekka Paalaneneee580b2014-06-04 16:43:06 +03004774 drmModeEncoder *encoder;
4775 drmModeCrtc *crtc;
4776
4777 /* Get the current mode on the crtc that's currently driving
4778 * this connector. */
Pekka Paalanen9c03a7c2017-11-28 14:30:10 +02004779 encoder = drmModeGetEncoder(drm_fd, head->connector->encoder_id);
Pekka Paalaneneee580b2014-06-04 16:43:06 +03004780 if (encoder != NULL) {
Pekka Paalanen27cc4812017-11-20 13:31:06 +02004781 head->inherited_crtc_id = encoder->crtc_id;
4782
Pekka Paalaneneee580b2014-06-04 16:43:06 +03004783 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
4784 drmModeFreeEncoder(encoder);
Pekka Paalanen27cc4812017-11-20 13:31:06 +02004785
Pekka Paalaneneee580b2014-06-04 16:43:06 +03004786 if (crtc == NULL)
4787 return -1;
4788 if (crtc->mode_valid)
Pekka Paalanen6fae2be2017-11-28 14:33:52 +02004789 head->inherited_mode = crtc->mode;
Pekka Paalaneneee580b2014-06-04 16:43:06 +03004790 drmModeFreeCrtc(crtc);
4791 }
4792
4793 return 0;
4794}
4795
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004796static int
Armin Krezović08368132016-09-30 14:11:05 +02004797drm_output_set_mode(struct weston_output *base,
4798 enum weston_drm_backend_output_mode mode,
4799 const char *modeline)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004800{
Armin Krezović08368132016-09-30 14:11:05 +02004801 struct drm_output *output = to_drm_output(base);
4802 struct drm_backend *b = to_drm_backend(base->compositor);
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03004803 struct drm_head *head = to_drm_head(weston_output_get_first_head(base));
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004804
Armin Krezović445b41b2016-10-09 23:48:16 +02004805 struct drm_mode *current;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004806
Pekka Paalanen4be24852017-09-11 15:01:12 +03004807 if (drm_output_update_modelist_from_heads(output) < 0)
4808 return -1;
4809
Pekka Paalanen13d233e2017-09-11 14:06:11 +03004810 current = drm_output_choose_initial_mode(b, output, mode, modeline,
4811 &head->inherited_mode);
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004812 if (!current)
Armin Krezović445b41b2016-10-09 23:48:16 +02004813 return -1;
Armin Krezović08368132016-09-30 14:11:05 +02004814
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004815 output->base.current_mode = &current->base;
Hardeningff39efa2013-09-18 23:56:35 +02004816 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
Wang Quanxianacb805a2012-07-30 18:09:46 -04004817
Armin Krezović08368132016-09-30 14:11:05 +02004818 /* Set native_ fields, so weston_output_mode_switch_to_native() works */
4819 output->base.native_mode = output->base.current_mode;
4820 output->base.native_scale = output->base.current_scale;
4821
Armin Krezović08368132016-09-30 14:11:05 +02004822 return 0;
Armin Krezović08368132016-09-30 14:11:05 +02004823}
4824
4825static void
4826drm_output_set_gbm_format(struct weston_output *base,
4827 const char *gbm_format)
4828{
4829 struct drm_output *output = to_drm_output(base);
4830 struct drm_backend *b = to_drm_backend(base->compositor);
4831
4832 if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1)
4833 output->gbm_format = b->gbm_format;
Daniel Stonee2e80132018-01-16 15:37:33 +00004834
4835 /* Without universal planes, we can't discover which formats are
4836 * supported by the primary plane; we just hope that the GBM format
4837 * works. */
4838 if (!b->universal_planes)
4839 output->scanout_plane->formats[0] = output->gbm_format;
Armin Krezović08368132016-09-30 14:11:05 +02004840}
4841
4842static void
4843drm_output_set_seat(struct weston_output *base,
4844 const char *seat)
4845{
4846 struct drm_output *output = to_drm_output(base);
4847 struct drm_backend *b = to_drm_backend(base->compositor);
4848
4849 setup_output_seat_constraint(b, &output->base,
4850 seat ? seat : "");
4851}
4852
4853static int
Pekka Paalanenc4db6f72017-09-05 16:37:03 +03004854drm_output_init_gamma_size(struct drm_output *output)
4855{
4856 struct drm_backend *backend = to_drm_backend(output->base.compositor);
4857 drmModeCrtc *crtc;
4858
4859 assert(output->base.compositor);
4860 assert(output->crtc_id != 0);
4861 crtc = drmModeGetCrtc(backend->drm.fd, output->crtc_id);
4862 if (!crtc)
4863 return -1;
4864
4865 output->base.gamma_size = crtc->gamma_size;
4866
4867 drmModeFreeCrtc(crtc);
4868
4869 return 0;
4870}
4871
Pekka Paalanen27cc4812017-11-20 13:31:06 +02004872static uint32_t
4873drm_head_get_possible_crtcs_mask(struct drm_head *head)
4874{
4875 uint32_t possible_crtcs = 0;
4876 drmModeEncoder *encoder;
4877 int i;
4878
4879 for (i = 0; i < head->connector->count_encoders; i++) {
4880 encoder = drmModeGetEncoder(head->backend->drm.fd,
4881 head->connector->encoders[i]);
4882 if (!encoder)
4883 continue;
4884
4885 possible_crtcs |= encoder->possible_crtcs;
4886 drmModeFreeEncoder(encoder);
4887 }
4888
4889 return possible_crtcs;
4890}
4891
4892static int
4893drm_crtc_get_index(drmModeRes *resources, uint32_t crtc_id)
4894{
4895 int i;
4896
4897 for (i = 0; i < resources->count_crtcs; i++) {
4898 if (resources->crtcs[i] == crtc_id)
4899 return i;
4900 }
4901
4902 assert(0 && "unknown crtc id");
4903 return -1;
4904}
4905
4906/** Pick a CRTC that might be able to drive all attached connectors
4907 *
4908 * @param output The output whose attached heads to include.
4909 * @param resources The DRM KMS resources.
4910 * @return CRTC index, or -1 on failure or not found.
4911 */
4912static int
4913drm_output_pick_crtc(struct drm_output *output, drmModeRes *resources)
4914{
4915 struct drm_backend *backend;
4916 struct weston_head *base;
4917 struct drm_head *head;
4918 uint32_t possible_crtcs = 0xffffffff;
4919 int existing_crtc[32];
4920 unsigned j, n = 0;
4921 uint32_t crtc_id;
4922 int best_crtc_index = -1;
Pekka Paalanendb4c7d72017-11-28 16:11:00 +02004923 int fallback_crtc_index = -1;
Pekka Paalanen27cc4812017-11-20 13:31:06 +02004924 int i;
Pekka Paalanendb4c7d72017-11-28 16:11:00 +02004925 bool match;
Pekka Paalanen27cc4812017-11-20 13:31:06 +02004926
4927 backend = to_drm_backend(output->base.compositor);
4928
4929 /* This algorithm ignores drmModeEncoder::possible_clones restriction,
4930 * because it is more often set wrong than not in the kernel. */
4931
4932 /* Accumulate a mask of possible crtcs and find existing routings. */
4933 wl_list_for_each(base, &output->base.head_list, output_link) {
4934 head = to_drm_head(base);
4935
4936 possible_crtcs &= drm_head_get_possible_crtcs_mask(head);
4937
4938 crtc_id = head->inherited_crtc_id;
4939 if (crtc_id > 0 && n < ARRAY_LENGTH(existing_crtc))
4940 existing_crtc[n++] = drm_crtc_get_index(resources,
4941 crtc_id);
4942 }
4943
4944 /* Find a crtc that could drive each connector individually at least,
4945 * and prefer existing routings. */
4946 for (i = 0; i < resources->count_crtcs; i++) {
4947 crtc_id = resources->crtcs[i];
4948
4949 /* Could the crtc not drive each connector? */
4950 if (!(possible_crtcs & (1 << i)))
4951 continue;
4952
4953 /* Is the crtc already in use? */
4954 if (drm_output_find_by_crtc(backend, crtc_id))
4955 continue;
4956
4957 /* Try to preserve the existing CRTC -> connector routing;
4958 * it makes initialisation faster, and also since we have a
4959 * very dumb picking algorithm, may preserve a better
4960 * choice. */
4961 for (j = 0; j < n; j++) {
4962 if (existing_crtc[j] == i)
4963 return i;
4964 }
4965
Pekka Paalanendb4c7d72017-11-28 16:11:00 +02004966 /* Check if any other head had existing routing to this CRTC.
4967 * If they did, this is not the best CRTC as it might be needed
4968 * for another output we haven't enabled yet. */
4969 match = false;
4970 wl_list_for_each(base, &backend->compositor->head_list,
4971 compositor_link) {
4972 head = to_drm_head(base);
4973
4974 if (head->base.output == &output->base)
4975 continue;
4976
4977 if (weston_head_is_enabled(&head->base))
4978 continue;
4979
4980 if (head->inherited_crtc_id == crtc_id) {
4981 match = true;
4982 break;
4983 }
4984 }
4985 if (!match)
4986 best_crtc_index = i;
4987
4988 fallback_crtc_index = i;
Pekka Paalanen27cc4812017-11-20 13:31:06 +02004989 }
4990
4991 if (best_crtc_index != -1)
4992 return best_crtc_index;
4993
Pekka Paalanendb4c7d72017-11-28 16:11:00 +02004994 if (fallback_crtc_index != -1)
4995 return fallback_crtc_index;
4996
Pekka Paalanen27cc4812017-11-20 13:31:06 +02004997 /* Likely possible_crtcs was empty due to asking for clones,
4998 * but since the DRM documentation says the kernel lies, let's
4999 * pick one crtc anyway. Trial and error is the only way to
5000 * be sure if something doesn't work. */
5001
5002 /* First pick any existing assignment. */
5003 for (j = 0; j < n; j++) {
5004 crtc_id = resources->crtcs[existing_crtc[j]];
5005 if (!drm_output_find_by_crtc(backend, crtc_id))
5006 return existing_crtc[j];
5007 }
5008
5009 /* Otherwise pick any available crtc. */
5010 for (i = 0; i < resources->count_crtcs; i++) {
5011 crtc_id = resources->crtcs[i];
5012
5013 if (!drm_output_find_by_crtc(backend, crtc_id))
5014 return i;
5015 }
5016
5017 return -1;
5018}
5019
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03005020/** Allocate a CRTC for the output
5021 *
5022 * @param output The output with no allocated CRTC.
5023 * @param resources DRM KMS resources.
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03005024 * @return 0 on success, -1 on failure.
5025 *
Pekka Paalanen27cc4812017-11-20 13:31:06 +02005026 * Finds a free CRTC that might drive the attached connectors, reserves the CRTC
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03005027 * for the output, and loads the CRTC properties.
5028 *
5029 * Populates the cursor and scanout planes.
5030 *
5031 * On failure, the output remains without a CRTC.
5032 */
5033static int
Pekka Paalanen27cc4812017-11-20 13:31:06 +02005034drm_output_init_crtc(struct drm_output *output, drmModeRes *resources)
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03005035{
5036 struct drm_backend *b = to_drm_backend(output->base.compositor);
5037 drmModeObjectPropertiesPtr props;
5038 int i;
5039
5040 assert(output->crtc_id == 0);
5041
Pekka Paalanen27cc4812017-11-20 13:31:06 +02005042 i = drm_output_pick_crtc(output, resources);
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03005043 if (i < 0) {
Pekka Paalanen27cc4812017-11-20 13:31:06 +02005044 weston_log("Output '%s': No available CRTCs.\n",
5045 output->base.name);
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03005046 return -1;
5047 }
5048
5049 output->crtc_id = resources->crtcs[i];
5050 output->pipe = i;
5051
5052 props = drmModeObjectGetProperties(b->drm.fd, output->crtc_id,
5053 DRM_MODE_OBJECT_CRTC);
5054 if (!props) {
5055 weston_log("failed to get CRTC properties\n");
5056 goto err_crtc;
5057 }
5058 drm_property_info_populate(b, crtc_props, output->props_crtc,
5059 WDRM_CRTC__COUNT, props);
5060 drmModeFreeObjectProperties(props);
5061
5062 output->scanout_plane =
5063 drm_output_find_special_plane(b, output,
5064 WDRM_PLANE_TYPE_PRIMARY);
5065 if (!output->scanout_plane) {
5066 weston_log("Failed to find primary plane for output %s\n",
5067 output->base.name);
5068 goto err_crtc;
5069 }
5070
5071 /* Failing to find a cursor plane is not fatal, as we'll fall back
5072 * to software cursor. */
5073 output->cursor_plane =
5074 drm_output_find_special_plane(b, output,
5075 WDRM_PLANE_TYPE_CURSOR);
5076
Pekka Paalanen663d5e92017-09-08 13:32:40 +03005077 wl_array_remove_uint32(&b->unused_crtcs, output->crtc_id);
5078
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03005079 return 0;
5080
5081err_crtc:
5082 output->crtc_id = 0;
5083 output->pipe = 0;
5084
5085 return -1;
5086}
5087
5088/** Free the CRTC from the output
5089 *
5090 * @param output The output whose CRTC to deallocate.
5091 *
5092 * The CRTC reserved for the given output becomes free to use again.
5093 */
5094static void
5095drm_output_fini_crtc(struct drm_output *output)
5096{
5097 struct drm_backend *b = to_drm_backend(output->base.compositor);
Pekka Paalanen663d5e92017-09-08 13:32:40 +03005098 uint32_t *unused;
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03005099
5100 if (!b->universal_planes && !b->shutting_down) {
5101 /* With universal planes, the 'special' planes are allocated at
5102 * startup, freed at shutdown, and live on the plane list in
5103 * between. We want the planes to continue to exist and be freed
5104 * up for other outputs.
5105 *
5106 * Without universal planes, our special planes are
5107 * pseudo-planes allocated at output creation, freed at output
5108 * destruction, and not usable by other outputs.
5109 *
5110 * On the other hand, if the compositor is already shutting down,
5111 * the plane has already been destroyed.
5112 */
5113 if (output->cursor_plane)
5114 drm_plane_destroy(output->cursor_plane);
5115 if (output->scanout_plane)
5116 drm_plane_destroy(output->scanout_plane);
5117 }
5118
5119 drm_property_info_free(output->props_crtc, WDRM_CRTC__COUNT);
Pekka Paalanen663d5e92017-09-08 13:32:40 +03005120
5121 assert(output->crtc_id != 0);
5122
5123 unused = wl_array_add(&b->unused_crtcs, sizeof(*unused));
5124 *unused = output->crtc_id;
5125
5126 /* Force resetting unused CRTCs */
5127 b->state_invalid = true;
5128
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03005129 output->crtc_id = 0;
5130 output->cursor_plane = NULL;
5131 output->scanout_plane = NULL;
5132}
5133
Pekka Paalanenc0eb2542017-11-15 13:37:18 +02005134static void
5135drm_output_print_modes(struct drm_output *output)
5136{
5137 struct weston_mode *m;
5138 struct drm_mode *dm;
Ankit Nautiyala21c3932097-03-19 00:24:57 +05305139 const char *aspect_ratio;
Pekka Paalanenc0eb2542017-11-15 13:37:18 +02005140
5141 wl_list_for_each(m, &output->base.mode_list, link) {
5142 dm = to_drm_mode(m);
5143
Ankit Nautiyala21c3932097-03-19 00:24:57 +05305144 aspect_ratio = aspect_ratio_to_string(m->aspect_ratio);
5145 weston_log_continue(STAMP_SPACE "%dx%d@%.1f%s%s%s, %.1f MHz\n",
Pekka Paalanenc0eb2542017-11-15 13:37:18 +02005146 m->width, m->height, m->refresh / 1000.0,
Ankit Nautiyala21c3932097-03-19 00:24:57 +05305147 aspect_ratio,
Pekka Paalanenc0eb2542017-11-15 13:37:18 +02005148 m->flags & WL_OUTPUT_MODE_PREFERRED ?
5149 ", preferred" : "",
5150 m->flags & WL_OUTPUT_MODE_CURRENT ?
5151 ", current" : "",
5152 dm->mode_info.clock / 1000.0);
5153 }
5154}
5155
Pekka Paalanenc4db6f72017-09-05 16:37:03 +03005156static int
Armin Krezović08368132016-09-30 14:11:05 +02005157drm_output_enable(struct weston_output *base)
5158{
5159 struct drm_output *output = to_drm_output(base);
5160 struct drm_backend *b = to_drm_backend(base->compositor);
Pekka Paalanen663d5e92017-09-08 13:32:40 +03005161 drmModeRes *resources;
5162 int ret;
5163
5164 resources = drmModeGetResources(b->drm.fd);
5165 if (!resources) {
5166 weston_log("drmModeGetResources failed\n");
5167 return -1;
5168 }
Pekka Paalanen27cc4812017-11-20 13:31:06 +02005169 ret = drm_output_init_crtc(output, resources);
Pekka Paalanen663d5e92017-09-08 13:32:40 +03005170 drmModeFreeResources(resources);
5171 if (ret < 0)
5172 return -1;
5173
5174 if (drm_output_init_gamma_size(output) < 0)
5175 goto err;
Armin Krezović08368132016-09-30 14:11:05 +02005176
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00005177 if (b->pageflip_timeout)
5178 drm_output_pageflip_timer_create(output);
5179
Giulio Camuffo954f1832014-10-11 18:27:30 +03005180 if (b->use_pixman) {
5181 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02005182 weston_log("Failed to init output pixman state\n");
Daniel Stone02cf4662017-03-03 16:19:39 +00005183 goto err;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02005184 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03005185 } else if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02005186 weston_log("Failed to init output gl state\n");
Daniel Stone02cf4662017-03-03 16:19:39 +00005187 goto err;
Kristian Høgsberg1d1e0a52012-10-21 13:29:26 -04005188 }
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04005189
Pekka Paalanenf8b850d2017-11-15 12:51:01 +02005190 drm_output_init_backlight(output);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005191
Jonas Ådahle5a12252013-04-05 23:07:11 +02005192 output->base.start_repaint_loop = drm_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05005193 output->base.repaint = drm_output_repaint;
Jesse Barnes58ef3792012-02-23 09:45:49 -05005194 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005195 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08005196 output->base.switch_mode = drm_output_switch_mode;
Richard Hughese7299962013-05-01 21:52:12 +01005197 output->base.set_gamma = drm_output_set_gamma;
5198
Daniel Stone2ba17f42015-05-19 20:02:41 +01005199 if (output->cursor_plane)
5200 weston_compositor_stack_plane(b->compositor,
5201 &output->cursor_plane->base,
5202 NULL);
5203 else
5204 b->cursors_are_broken = 1;
5205
Daniel Stonee2e80132018-01-16 15:37:33 +00005206 weston_compositor_stack_plane(b->compositor,
5207 &output->scanout_plane->base,
Giulio Camuffo954f1832014-10-11 18:27:30 +03005208 &b->compositor->primary_plane);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005209
Pekka Paalanenc0eb2542017-11-15 13:37:18 +02005210 weston_log("Output %s (crtc %d) video modes:\n",
5211 output->base.name, output->crtc_id);
5212 drm_output_print_modes(output);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04005213
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005214 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01005215
Daniel Stone02cf4662017-03-03 16:19:39 +00005216err:
Pekka Paalanen663d5e92017-09-08 13:32:40 +03005217 drm_output_fini_crtc(output);
5218
David Herrmann0f0d54e2011-12-08 17:05:45 +01005219 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005220}
5221
Jesse Barnes58ef3792012-02-23 09:45:49 -05005222static void
Armin Krezović08368132016-09-30 14:11:05 +02005223drm_output_deinit(struct weston_output *base)
5224{
5225 struct drm_output *output = to_drm_output(base);
5226 struct drm_backend *b = to_drm_backend(base->compositor);
5227
Daniel Stone3e661f72016-11-04 17:24:06 +00005228 if (b->use_pixman)
Armin Krezović08368132016-09-30 14:11:05 +02005229 drm_output_fini_pixman(output);
Daniel Stone3e661f72016-11-04 17:24:06 +00005230 else
5231 drm_output_fini_egl(output);
Armin Krezović08368132016-09-30 14:11:05 +02005232
Daniel Stone2ba17f42015-05-19 20:02:41 +01005233 /* Since our planes are no longer in use anywhere, remove their base
5234 * weston_plane's link from the plane stacking list, unless we're
5235 * shutting down, in which case the plane has already been
5236 * destroyed. */
Daniel Stonee2e80132018-01-16 15:37:33 +00005237 if (!b->shutting_down) {
5238 wl_list_remove(&output->scanout_plane->base.link);
5239 wl_list_init(&output->scanout_plane->base.link);
5240
5241 if (output->cursor_plane) {
5242 wl_list_remove(&output->cursor_plane->base.link);
5243 wl_list_init(&output->cursor_plane->base.link);
5244 /* Turn off hardware cursor */
5245 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
5246 }
Daniel Stone2ba17f42015-05-19 20:02:41 +01005247 }
Daniel Stone087ddf02017-02-14 17:51:30 +00005248
Pekka Paalanen663d5e92017-09-08 13:32:40 +03005249 drm_output_fini_crtc(output);
Armin Krezović08368132016-09-30 14:11:05 +02005250}
5251
5252static void
Pekka Paalanenc112f002017-08-28 16:27:20 +03005253drm_head_destroy(struct drm_head *head);
5254
5255static void
Armin Krezović08368132016-09-30 14:11:05 +02005256drm_output_destroy(struct weston_output *base)
5257{
5258 struct drm_output *output = to_drm_output(base);
5259 struct drm_backend *b = to_drm_backend(base->compositor);
Armin Krezović08368132016-09-30 14:11:05 +02005260
Daniel Stone598ee9d2016-11-16 11:55:20 +00005261 if (output->page_flip_pending || output->vblank_pending ||
5262 output->atomic_complete_pending) {
Armin Krezović08368132016-09-30 14:11:05 +02005263 output->destroy_pending = 1;
5264 weston_log("destroy output while page flip pending\n");
5265 return;
5266 }
5267
5268 if (output->base.enabled)
5269 drm_output_deinit(&output->base);
5270
Pekka Paalanen383b3af2017-09-11 14:40:48 +03005271 drm_mode_list_destroy(b, &output->base.mode_list);
Armin Krezović445b41b2016-10-09 23:48:16 +02005272
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00005273 if (output->pageflip_timer)
5274 wl_event_source_remove(output->pageflip_timer);
5275
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005276 weston_output_release(&output->base);
Armin Krezović08368132016-09-30 14:11:05 +02005277
Daniel Stone7b2ddac2016-11-11 19:11:49 +00005278 assert(!output->state_last);
5279 drm_output_state_free(output->state_cur);
5280
Armin Krezović08368132016-09-30 14:11:05 +02005281 free(output);
5282}
5283
5284static int
5285drm_output_disable(struct weston_output *base)
5286{
5287 struct drm_output *output = to_drm_output(base);
Armin Krezović08368132016-09-30 14:11:05 +02005288
Daniel Stone598ee9d2016-11-16 11:55:20 +00005289 if (output->page_flip_pending || output->vblank_pending ||
5290 output->atomic_complete_pending) {
Armin Krezović08368132016-09-30 14:11:05 +02005291 output->disable_pending = 1;
5292 return -1;
5293 }
5294
Daniel Stonea08512f2016-11-08 17:46:10 +00005295 weston_log("Disabling output %s\n", output->base.name);
Daniel Stonea08512f2016-11-08 17:46:10 +00005296
Armin Krezović08368132016-09-30 14:11:05 +02005297 if (output->base.enabled)
5298 drm_output_deinit(&output->base);
5299
5300 output->disable_pending = 0;
5301
Armin Krezović08368132016-09-30 14:11:05 +02005302 return 0;
5303}
5304
5305/**
Daniel Stone087ddf02017-02-14 17:51:30 +00005306 * Update the list of unused connectors and CRTCs
5307 *
Pekka Paalaneneacec812017-09-12 13:43:51 +03005308 * This keeps the unused_crtc arrays up to date.
Daniel Stone087ddf02017-02-14 17:51:30 +00005309 *
5310 * @param b Weston backend structure
5311 * @param resources DRM resources for this device
5312 */
5313static void
5314drm_backend_update_unused_outputs(struct drm_backend *b, drmModeRes *resources)
5315{
5316 int i;
5317
Daniel Stone087ddf02017-02-14 17:51:30 +00005318 wl_array_release(&b->unused_crtcs);
5319 wl_array_init(&b->unused_crtcs);
5320
5321 for (i = 0; i < resources->count_crtcs; i++) {
5322 struct drm_output *output;
5323 uint32_t *crtc_id;
5324
5325 output = drm_output_find_by_crtc(b, resources->crtcs[i]);
5326 if (output && output->base.enabled)
5327 continue;
5328
5329 crtc_id = wl_array_add(&b->unused_crtcs, sizeof(*crtc_id));
5330 *crtc_id = resources->crtcs[i];
5331 }
5332}
5333
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03005334/** Replace connector data and monitor information
5335 *
5336 * @param head The head to update.
5337 * @param connector The connector data to be owned by the head, must match
5338 * the head's connector ID.
5339 * @return 0 on success, -1 on failure.
5340 *
5341 * Takes ownership of @c connector on success, not on failure.
5342 *
5343 * May schedule a heads changed call.
5344 */
5345static int
5346drm_head_assign_connector_info(struct drm_head *head,
5347 drmModeConnector *connector)
5348{
5349 drmModeObjectProperties *props;
5350 const char *make = "unknown";
5351 const char *model = "unknown";
5352 const char *serial_number = "unknown";
5353
5354 assert(connector);
5355 assert(head->connector_id == connector->connector_id);
5356
5357 props = drmModeObjectGetProperties(head->backend->drm.fd,
5358 head->connector_id,
5359 DRM_MODE_OBJECT_CONNECTOR);
5360 if (!props) {
5361 weston_log("Error: failed to get connector '%s' properties\n",
5362 head->base.name);
5363 return -1;
5364 }
5365
5366 if (head->connector)
5367 drmModeFreeConnector(head->connector);
5368 head->connector = connector;
5369
5370 drm_property_info_populate(head->backend, connector_props,
5371 head->props_conn,
5372 WDRM_CONNECTOR__COUNT, props);
5373 find_and_parse_output_edid(head, props, &make, &model, &serial_number);
5374 weston_head_set_monitor_strings(&head->base, make, model, serial_number);
5375 weston_head_set_subpixel(&head->base,
5376 drm_subpixel_to_wayland(head->connector->subpixel));
5377
5378 weston_head_set_physical_size(&head->base, head->connector->mmWidth,
5379 head->connector->mmHeight);
5380
5381 drmModeFreeObjectProperties(props);
5382
5383 /* Unknown connection status is assumed disconnected. */
5384 weston_head_set_connection_status(&head->base,
5385 head->connector->connection == DRM_MODE_CONNECTED);
5386
5387 return 0;
5388}
5389
Pekka Paalanen456dc732017-11-09 15:10:11 +02005390static void
5391drm_head_log_info(struct drm_head *head, const char *msg)
5392{
5393 if (head->base.connected) {
5394 weston_log("DRM: head '%s' %s, connector %d is connected, "
5395 "EDID make '%s', model '%s', serial '%s'\n",
5396 head->base.name, msg, head->connector_id,
5397 head->base.make, head->base.model,
5398 head->base.serial_number ?: "");
5399 } else {
5400 weston_log("DRM: head '%s' %s, connector %d is disconnected.\n",
5401 head->base.name, msg, head->connector_id);
5402 }
5403}
5404
Pekka Paalanend2e62422017-09-08 15:48:07 +03005405/** Update connector and monitor information
5406 *
5407 * @param head The head to update.
5408 *
5409 * Re-reads the DRM property lists for the connector and updates monitor
5410 * information and connection status. This may schedule a heads changed call
5411 * to the user.
5412 */
5413static void
5414drm_head_update_info(struct drm_head *head)
5415{
5416 drmModeConnector *connector;
5417
5418 connector = drmModeGetConnector(head->backend->drm.fd,
5419 head->connector_id);
5420 if (!connector) {
5421 weston_log("DRM: getting connector info for '%s' failed.\n",
5422 head->base.name);
5423 return;
5424 }
5425
5426 if (drm_head_assign_connector_info(head, connector) < 0)
5427 drmModeFreeConnector(connector);
Pekka Paalanen456dc732017-11-09 15:10:11 +02005428
5429 if (head->base.device_changed)
5430 drm_head_log_info(head, "updated");
Pekka Paalanend2e62422017-09-08 15:48:07 +03005431}
5432
Daniel Stone087ddf02017-02-14 17:51:30 +00005433/**
Pekka Paalanenc112f002017-08-28 16:27:20 +03005434 * Create a Weston head for a connector
5435 *
5436 * Given a DRM connector, create a matching drm_head structure and add it
5437 * to Weston's head list.
5438 *
5439 * @param b Weston backend structure
5440 * @param connector_id DRM connector ID for the head
5441 * @param drm_device udev device pointer
5442 * @returns The new head, or NULL on failure.
5443 */
5444static struct drm_head *
5445drm_head_create(struct drm_backend *backend, uint32_t connector_id,
5446 struct udev_device *drm_device)
5447{
5448 struct drm_head *head;
5449 drmModeConnector *connector;
5450 char *name;
5451
5452 head = zalloc(sizeof *head);
5453 if (!head)
5454 return NULL;
5455
5456 connector = drmModeGetConnector(backend->drm.fd, connector_id);
5457 if (!connector)
5458 goto err_alloc;
5459
5460 name = make_connector_name(connector);
5461 if (!name)
5462 goto err_alloc;
5463
5464 weston_head_init(&head->base, name);
5465 free(name);
5466
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03005467 head->connector_id = connector_id;
Pekka Paalanenc112f002017-08-28 16:27:20 +03005468 head->backend = backend;
5469
Pekka Paalanence724242017-09-04 12:21:24 +03005470 head->backlight = backlight_init(drm_device, connector->connector_type);
5471
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03005472 if (drm_head_assign_connector_info(head, connector) < 0)
5473 goto err_init;
5474
5475 if (head->connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
5476 head->connector->connector_type == DRM_MODE_CONNECTOR_eDP)
5477 weston_head_set_internal(&head->base);
Pekka Paalanenc112f002017-08-28 16:27:20 +03005478
Pekka Paalanen9c03a7c2017-11-28 14:30:10 +02005479 if (drm_head_read_current_setup(head, backend) < 0) {
Pekka Paalanen13d233e2017-09-11 14:06:11 +03005480 weston_log("Failed to retrieve current mode from connector %d.\n",
5481 head->connector_id);
Pekka Paalanen6fae2be2017-11-28 14:33:52 +02005482 /* Not fatal. */
Pekka Paalanen13d233e2017-09-11 14:06:11 +03005483 }
5484
Pekka Paalanenc112f002017-08-28 16:27:20 +03005485 weston_compositor_add_head(backend->compositor, &head->base);
Pekka Paalanen456dc732017-11-09 15:10:11 +02005486 drm_head_log_info(head, "found");
Pekka Paalanenc112f002017-08-28 16:27:20 +03005487
5488 return head;
5489
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03005490err_init:
5491 weston_head_release(&head->base);
5492
Pekka Paalanenc112f002017-08-28 16:27:20 +03005493err_alloc:
5494 if (connector)
5495 drmModeFreeConnector(connector);
5496
5497 free(head);
5498
5499 return NULL;
5500}
5501
5502static void
5503drm_head_destroy(struct drm_head *head)
5504{
5505 weston_head_release(&head->base);
Pekka Paalanence724242017-09-04 12:21:24 +03005506
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03005507 drm_property_info_free(head->props_conn, WDRM_CONNECTOR__COUNT);
5508 drmModeFreeConnector(head->connector);
5509
Pekka Paalanence724242017-09-04 12:21:24 +03005510 if (head->backlight)
5511 backlight_destroy(head->backlight);
5512
Pekka Paalanenc112f002017-08-28 16:27:20 +03005513 free(head);
5514}
5515
5516/**
Armin Krezović08368132016-09-30 14:11:05 +02005517 * Create a Weston output structure
5518 *
Pekka Paalanend2e62422017-09-08 15:48:07 +03005519 * Create an "empty" drm_output. This is the implementation of
5520 * weston_backend::create_output.
Armin Krezović08368132016-09-30 14:11:05 +02005521 *
Pekka Paalanend2e62422017-09-08 15:48:07 +03005522 * Creating an output is usually followed by drm_output_attach_head()
5523 * and drm_output_enable() to make use of it.
5524 *
5525 * @param compositor The compositor instance.
5526 * @param name Name for the new output.
5527 * @returns The output, or NULL on failure.
Armin Krezović08368132016-09-30 14:11:05 +02005528 */
Pekka Paalanend2e62422017-09-08 15:48:07 +03005529static struct weston_output *
5530drm_output_create(struct weston_compositor *compositor, const char *name)
Armin Krezović08368132016-09-30 14:11:05 +02005531{
Pekka Paalanend2e62422017-09-08 15:48:07 +03005532 struct drm_backend *b = to_drm_backend(compositor);
Armin Krezović08368132016-09-30 14:11:05 +02005533 struct drm_output *output;
Armin Krezović08368132016-09-30 14:11:05 +02005534
Armin Krezović08368132016-09-30 14:11:05 +02005535 output = zalloc(sizeof *output);
5536 if (output == NULL)
Pekka Paalanend2e62422017-09-08 15:48:07 +03005537 return NULL;
Armin Krezović08368132016-09-30 14:11:05 +02005538
Pekka Paalanend2e62422017-09-08 15:48:07 +03005539 weston_output_init(&output->base, compositor, name);
Pekka Paalanenc1e89ba2017-08-31 16:18:48 +03005540
Armin Krezović08368132016-09-30 14:11:05 +02005541 output->base.enable = drm_output_enable;
5542 output->base.destroy = drm_output_destroy;
5543 output->base.disable = drm_output_disable;
Pekka Paalanenc112f002017-08-28 16:27:20 +03005544 output->base.attach_head = drm_output_attach_head;
Pekka Paalanen7f853792017-11-29 14:33:33 +02005545 output->base.detach_head = drm_output_detach_head;
Armin Krezović08368132016-09-30 14:11:05 +02005546
5547 output->destroy_pending = 0;
5548 output->disable_pending = 0;
Armin Krezović08368132016-09-30 14:11:05 +02005549
Pekka Paalanen01f60212017-03-24 15:39:24 +02005550 output->state_cur = drm_output_state_alloc(output, NULL);
Pekka Paalanena0bfedc2017-04-03 14:42:51 +03005551
Armin Krezović08368132016-09-30 14:11:05 +02005552 weston_compositor_add_pending_output(&output->base, b->compositor);
5553
Pekka Paalanend2e62422017-09-08 15:48:07 +03005554 return &output->base;
Armin Krezović08368132016-09-30 14:11:05 +02005555}
5556
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005557static int
Pekka Paalanend2e62422017-09-08 15:48:07 +03005558drm_backend_create_heads(struct drm_backend *b, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005559{
Pekka Paalanend2e62422017-09-08 15:48:07 +03005560 struct drm_head *head;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005561 drmModeRes *resources;
5562 int i;
5563
Giulio Camuffo954f1832014-10-11 18:27:30 +03005564 resources = drmModeGetResources(b->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005565 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02005566 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005567 return -1;
5568 }
5569
Giulio Camuffo954f1832014-10-11 18:27:30 +03005570 b->min_width = resources->min_width;
5571 b->max_width = resources->max_width;
5572 b->min_height = resources->min_height;
5573 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05005574
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005575 for (i = 0; i < resources->count_connectors; i++) {
Pekka Paalanend2e62422017-09-08 15:48:07 +03005576 uint32_t connector_id = resources->connectors[i];
Daniel Stone02cf4662017-03-03 16:19:39 +00005577
Pekka Paalanend2e62422017-09-08 15:48:07 +03005578 head = drm_head_create(b, connector_id, drm_device);
5579 if (!head) {
5580 weston_log("DRM: failed to create head for connector %d.\n",
5581 connector_id);
Benjamin Franzke9eaee352011-08-02 13:03:54 +02005582 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005583 }
5584
Daniel Stone087ddf02017-02-14 17:51:30 +00005585 drm_backend_update_unused_outputs(b, resources);
5586
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005587 drmModeFreeResources(resources);
5588
5589 return 0;
5590}
5591
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005592static void
Pekka Paalanend2e62422017-09-08 15:48:07 +03005593drm_backend_update_heads(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005594{
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005595 drmModeRes *resources;
Pekka Paalanena0a37462017-08-31 15:41:57 +03005596 struct weston_head *base, *next;
5597 struct drm_head *head;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005598 int i;
5599
Giulio Camuffo954f1832014-10-11 18:27:30 +03005600 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005601 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02005602 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005603 return;
5604 }
5605
Pekka Paalanend2e62422017-09-08 15:48:07 +03005606 /* collect new connectors that have appeared, e.g. MST */
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005607 for (i = 0; i < resources->count_connectors; i++) {
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +00005608 uint32_t connector_id = resources->connectors[i];
Benjamin Franzke117483d2011-08-30 11:38:26 +02005609
Pekka Paalanend2e62422017-09-08 15:48:07 +03005610 head = drm_head_find_by_connector(b, connector_id);
5611 if (head) {
5612 drm_head_update_info(head);
5613 } else {
5614 head = drm_head_create(b, connector_id, drm_device);
5615 if (!head)
5616 weston_log("DRM: failed to create head for hot-added connector %d.\n",
5617 connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01005618 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005619 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005620
Pekka Paalanend2e62422017-09-08 15:48:07 +03005621 /* Remove connectors that have disappeared. */
Pekka Paalanena0a37462017-08-31 15:41:57 +03005622 wl_list_for_each_safe(base, next,
5623 &b->compositor->head_list, compositor_link) {
Pekka Paalanend2e62422017-09-08 15:48:07 +03005624 bool removed = true;
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00005625
Pekka Paalanena0a37462017-08-31 15:41:57 +03005626 head = to_drm_head(base);
5627
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00005628 for (i = 0; i < resources->count_connectors; i++) {
Pekka Paalanend2e62422017-09-08 15:48:07 +03005629 if (resources->connectors[i] == head->connector_id) {
5630 removed = false;
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00005631 break;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005632 }
5633 }
Armin Krezović08368132016-09-30 14:11:05 +02005634
Pekka Paalanend2e62422017-09-08 15:48:07 +03005635 if (!removed)
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00005636 continue;
5637
Pekka Paalanend2e62422017-09-08 15:48:07 +03005638 weston_log("DRM: head '%s' (connector %d) disappeared.\n",
5639 head->base.name, head->connector_id);
5640 drm_head_destroy(head);
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00005641 }
5642
Daniel Stone087ddf02017-02-14 17:51:30 +00005643 drm_backend_update_unused_outputs(b, resources);
5644
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00005645 drmModeFreeResources(resources);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005646}
5647
5648static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03005649udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005650{
David Herrmannd7488c22012-03-11 20:05:21 +01005651 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01005652 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01005653
5654 sysnum = udev_device_get_sysnum(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005655 if (!sysnum || atoi(sysnum) != b->drm.id)
David Herrmannd7488c22012-03-11 20:05:21 +01005656 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02005657
David Herrmann6ac52db2012-03-11 20:05:22 +01005658 val = udev_device_get_property_value(device, "HOTPLUG");
5659 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005660 return 0;
5661
David Herrmann6ac52db2012-03-11 20:05:22 +01005662 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005663}
5664
Kristian Høgsbergb1868472011-04-22 12:27:57 -04005665static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005666udev_drm_event(int fd, uint32_t mask, void *data)
5667{
Giulio Camuffo954f1832014-10-11 18:27:30 +03005668 struct drm_backend *b = data;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005669 struct udev_device *event;
5670
Giulio Camuffo954f1832014-10-11 18:27:30 +03005671 event = udev_monitor_receive_device(b->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02005672
Giulio Camuffo954f1832014-10-11 18:27:30 +03005673 if (udev_event_is_hotplug(b, event))
Pekka Paalanend2e62422017-09-08 15:48:07 +03005674 drm_backend_update_heads(b, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005675
5676 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04005677
5678 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005679}
5680
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005681static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005682drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005683{
Armin Krezović545dba62016-08-05 15:54:18 +02005684 struct drm_backend *b = to_drm_backend(ec);
Pekka Paalanenc112f002017-08-28 16:27:20 +03005685 struct weston_head *base, *next;
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005686
Giulio Camuffo954f1832014-10-11 18:27:30 +03005687 udev_input_destroy(&b->input);
Jonas Ådahlc97af922012-03-28 22:36:09 +02005688
Giulio Camuffo954f1832014-10-11 18:27:30 +03005689 wl_event_source_remove(b->udev_drm_source);
5690 wl_event_source_remove(b->drm_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02005691
Daniel Stoneb57c6a02017-10-05 16:27:21 +01005692 b->shutting_down = true;
5693
Giulio Camuffo954f1832014-10-11 18:27:30 +03005694 destroy_sprites(b);
Kristian Høgsberg3d64a3e2013-05-10 12:36:04 -04005695
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03005696 weston_compositor_shutdown(ec);
5697
Pekka Paalanenc112f002017-08-28 16:27:20 +03005698 wl_list_for_each_safe(base, next, &ec->head_list, compositor_link)
5699 drm_head_destroy(to_drm_head(base));
5700
Giulio Camuffo954f1832014-10-11 18:27:30 +03005701 if (b->gbm)
5702 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02005703
Pekka Paalanen5b0aa552017-12-07 16:06:05 +02005704 udev_monitor_unref(b->udev_monitor);
Pekka Paalanen2a0c6c32017-09-13 16:48:01 +03005705 udev_unref(b->udev);
5706
Giulio Camuffo954f1832014-10-11 18:27:30 +03005707 weston_launcher_destroy(ec->launcher);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005708
Daniel Stone087ddf02017-02-14 17:51:30 +00005709 wl_array_release(&b->unused_crtcs);
Daniel Stone087ddf02017-02-14 17:51:30 +00005710
Giulio Camuffo954f1832014-10-11 18:27:30 +03005711 close(b->drm.fd);
Pekka Paalanen9bf4f372017-12-07 16:05:29 +02005712 free(b->drm.filename);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005713 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005714}
5715
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04005716static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -07005717session_notify(struct wl_listener *listener, void *data)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04005718{
Kristian Høgsberg61741a22013-09-17 16:02:57 -07005719 struct weston_compositor *compositor = data;
Armin Krezović545dba62016-08-05 15:54:18 +02005720 struct drm_backend *b = to_drm_backend(compositor);
Daniel Stone085d2b92015-05-21 00:00:57 +01005721 struct drm_plane *plane;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04005722 struct drm_output *output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04005723
Giulio Camuffo954f1832014-10-11 18:27:30 +03005724 if (compositor->session_active) {
Kristian Høgsberg61741a22013-09-17 16:02:57 -07005725 weston_log("activating session\n");
Daniel Stonef33e1042016-11-05 08:10:13 +00005726 weston_compositor_wake(compositor);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005727 weston_compositor_damage_all(compositor);
Daniel Stone6020f472018-02-05 15:46:20 +00005728 b->state_invalid = true;
Giulio Camuffo954f1832014-10-11 18:27:30 +03005729 udev_input_enable(&b->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07005730 } else {
5731 weston_log("deactivating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03005732 udev_input_disable(&b->input);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04005733
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005734 weston_compositor_offscreen(compositor);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04005735
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05005736 /* If we have a repaint scheduled (either from a
5737 * pending pageflip or the idle handler), make sure we
5738 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005739 * vt switched away. The OFFSCREEN state will prevent
Abdur Rehman4dca0e12017-01-01 19:46:35 +05005740 * further attempts at repainting. When we switch
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05005741 * back, we schedule a repaint, which will process
5742 * pending frame callbacks. */
5743
Giulio Camuffo954f1832014-10-11 18:27:30 +03005744 wl_list_for_each(output, &compositor->output_list, base.link) {
Daniel Stone09a97e22017-03-01 11:34:06 +00005745 output->base.repaint_needed = false;
Daniel Stone2ba17f42015-05-19 20:02:41 +01005746 if (output->cursor_plane)
5747 drmModeSetCursor(b->drm.fd, output->crtc_id,
5748 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05005749 }
5750
Giulio Camuffo954f1832014-10-11 18:27:30 +03005751 output = container_of(compositor->output_list.next,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04005752 struct drm_output, base.link);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05005753
Daniel Stone085d2b92015-05-21 00:00:57 +01005754 wl_list_for_each(plane, &b->plane_list, link) {
5755 if (plane->type != WDRM_PLANE_TYPE_OVERLAY)
5756 continue;
5757
Giulio Camuffo954f1832014-10-11 18:27:30 +03005758 drmModeSetPlane(b->drm.fd,
Daniel Stone085d2b92015-05-21 00:00:57 +01005759 plane->plane_id,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04005760 output->crtc_id, 0, 0,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05005761 0, 0, 0, 0, 0, 0, 0, 0);
Daniel Stone085d2b92015-05-21 00:00:57 +01005762 }
5763 }
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04005764}
5765
Daniel Stoneefa504f2016-12-19 16:48:20 +00005766/**
5767 * Determines whether or not a device is capable of modesetting. If successful,
5768 * sets b->drm.fd and b->drm.filename to the opened device.
5769 */
5770static bool
5771drm_device_is_kms(struct drm_backend *b, struct udev_device *device)
5772{
5773 const char *filename = udev_device_get_devnode(device);
5774 const char *sysnum = udev_device_get_sysnum(device);
5775 drmModeRes *res;
5776 int id, fd;
5777
5778 if (!filename)
5779 return false;
5780
5781 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
5782 if (fd < 0)
5783 return false;
5784
5785 res = drmModeGetResources(fd);
5786 if (!res)
5787 goto out_fd;
5788
5789 if (res->count_crtcs <= 0 || res->count_connectors <= 0 ||
5790 res->count_encoders <= 0)
5791 goto out_res;
5792
5793 if (sysnum)
5794 id = atoi(sysnum);
5795 if (!sysnum || id < 0) {
5796 weston_log("couldn't get sysnum for device %s\n", filename);
5797 goto out_res;
5798 }
5799
5800 /* We can be called successfully on multiple devices; if we have,
5801 * clean up old entries. */
5802 if (b->drm.fd >= 0)
5803 weston_launcher_close(b->compositor->launcher, b->drm.fd);
5804 free(b->drm.filename);
5805
5806 b->drm.fd = fd;
5807 b->drm.id = id;
5808 b->drm.filename = strdup(filename);
5809
Sergi Granellceb59812017-03-28 12:44:04 +02005810 drmModeFreeResources(res);
5811
Daniel Stoneefa504f2016-12-19 16:48:20 +00005812 return true;
5813
5814out_res:
5815 drmModeFreeResources(res);
5816out_fd:
5817 weston_launcher_close(b->compositor->launcher, fd);
5818 return false;
5819}
5820
David Herrmann0af066f2012-10-29 19:21:16 +01005821/*
5822 * Find primary GPU
5823 * Some systems may have multiple DRM devices attached to a single seat. This
5824 * function loops over all devices and tries to find a PCI device with the
5825 * boot_vga sysfs attribute set to 1.
5826 * If no such device is found, the first DRM device reported by udev is used.
Daniel Stoneefa504f2016-12-19 16:48:20 +00005827 * Devices are also vetted to make sure they are are capable of modesetting,
5828 * rather than pure render nodes (GPU with no display), or pure
5829 * memory-allocation devices (VGEM).
David Herrmann0af066f2012-10-29 19:21:16 +01005830 */
5831static struct udev_device*
Giulio Camuffo954f1832014-10-11 18:27:30 +03005832find_primary_gpu(struct drm_backend *b, const char *seat)
David Herrmann0af066f2012-10-29 19:21:16 +01005833{
5834 struct udev_enumerate *e;
5835 struct udev_list_entry *entry;
5836 const char *path, *device_seat, *id;
5837 struct udev_device *device, *drm_device, *pci;
5838
Giulio Camuffo954f1832014-10-11 18:27:30 +03005839 e = udev_enumerate_new(b->udev);
David Herrmann0af066f2012-10-29 19:21:16 +01005840 udev_enumerate_add_match_subsystem(e, "drm");
5841 udev_enumerate_add_match_sysname(e, "card[0-9]*");
5842
5843 udev_enumerate_scan_devices(e);
5844 drm_device = NULL;
5845 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Daniel Stoneefa504f2016-12-19 16:48:20 +00005846 bool is_boot_vga = false;
5847
David Herrmann0af066f2012-10-29 19:21:16 +01005848 path = udev_list_entry_get_name(entry);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005849 device = udev_device_new_from_syspath(b->udev, path);
David Herrmann0af066f2012-10-29 19:21:16 +01005850 if (!device)
5851 continue;
5852 device_seat = udev_device_get_property_value(device, "ID_SEAT");
5853 if (!device_seat)
5854 device_seat = default_seat;
5855 if (strcmp(device_seat, seat)) {
5856 udev_device_unref(device);
5857 continue;
5858 }
5859
5860 pci = udev_device_get_parent_with_subsystem_devtype(device,
5861 "pci", NULL);
5862 if (pci) {
5863 id = udev_device_get_sysattr_value(pci, "boot_vga");
Daniel Stoneefa504f2016-12-19 16:48:20 +00005864 if (id && !strcmp(id, "1"))
5865 is_boot_vga = true;
David Herrmann0af066f2012-10-29 19:21:16 +01005866 }
5867
Daniel Stoneefa504f2016-12-19 16:48:20 +00005868 /* If we already have a modesetting-capable device, and this
5869 * device isn't our boot-VGA device, we aren't going to use
5870 * it. */
5871 if (!is_boot_vga && drm_device) {
David Herrmann0af066f2012-10-29 19:21:16 +01005872 udev_device_unref(device);
Daniel Stoneefa504f2016-12-19 16:48:20 +00005873 continue;
5874 }
5875
5876 /* Make sure this device is actually capable of modesetting;
5877 * if this call succeeds, b->drm.{fd,filename} will be set,
5878 * and any old values freed. */
5879 if (!drm_device_is_kms(b, device)) {
5880 udev_device_unref(device);
5881 continue;
5882 }
5883
5884 /* There can only be one boot_vga device, and we try to use it
5885 * at all costs. */
5886 if (is_boot_vga) {
5887 if (drm_device)
5888 udev_device_unref(drm_device);
5889 drm_device = device;
5890 break;
5891 }
5892
5893 /* Per the (!is_boot_vga && drm_device) test above, we only
5894 * trump existing saved devices with boot-VGA devices, so if
5895 * we end up here, this must be the first device we've seen. */
5896 assert(!drm_device);
5897 drm_device = device;
David Herrmann0af066f2012-10-29 19:21:16 +01005898 }
5899
Daniel Stoneefa504f2016-12-19 16:48:20 +00005900 /* If we're returning a device to use, we must have an open FD for
5901 * it. */
5902 assert(!!drm_device == (b->drm.fd >= 0));
5903
David Herrmann0af066f2012-10-29 19:21:16 +01005904 udev_enumerate_unref(e);
5905 return drm_device;
5906}
5907
Pekka Paalanenb45ed8b2017-03-28 18:04:27 +03005908static struct udev_device *
5909open_specific_drm_device(struct drm_backend *b, const char *name)
5910{
5911 struct udev_device *device;
5912
5913 device = udev_device_new_from_subsystem_sysname(b->udev, "drm", name);
5914 if (!device) {
5915 weston_log("ERROR: could not open DRM device '%s'\n", name);
5916 return NULL;
5917 }
5918
5919 if (!drm_device_is_kms(b, device)) {
5920 udev_device_unref(device);
5921 weston_log("ERROR: DRM device '%s' is not a KMS device.\n", name);
5922 return NULL;
5923 }
5924
5925 /* If we're returning a device to use, we must have an open FD for
5926 * it. */
5927 assert(b->drm.fd >= 0);
5928
5929 return device;
5930}
5931
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02005932static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02005933planes_binding(struct weston_keyboard *keyboard, const struct timespec *time,
5934 uint32_t key, void *data)
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02005935{
Giulio Camuffo954f1832014-10-11 18:27:30 +03005936 struct drm_backend *b = data;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02005937
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02005938 switch (key) {
5939 case KEY_C:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005940 b->cursors_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02005941 break;
5942 case KEY_V:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005943 b->sprites_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02005944 break;
5945 case KEY_O:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005946 b->sprites_hidden ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02005947 break;
5948 default:
5949 break;
5950 }
Ander Conselvan de Oliveira180f42a2012-11-21 15:11:37 +02005951}
5952
Kristian Høgsberg0eac34a2013-08-30 14:28:22 -07005953#ifdef BUILD_VAAPI_RECORDER
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005954static void
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03005955recorder_destroy(struct drm_output *output)
5956{
5957 vaapi_recorder_destroy(output->recorder);
5958 output->recorder = NULL;
5959
5960 output->base.disable_planes--;
5961
5962 wl_list_remove(&output->recorder_frame_listener.link);
5963 weston_log("[libva recorder] done\n");
5964}
5965
5966static void
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005967recorder_frame_notify(struct wl_listener *listener, void *data)
5968{
5969 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03005970 struct drm_backend *b;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005971 int fd, ret;
5972
5973 output = container_of(listener, struct drm_output,
5974 recorder_frame_listener);
Armin Krezović545dba62016-08-05 15:54:18 +02005975 b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005976
5977 if (!output->recorder)
5978 return;
5979
Daniel Stonee2e80132018-01-16 15:37:33 +00005980 ret = drmPrimeHandleToFD(b->drm.fd,
5981 output->scanout_plane->state_cur->fb->handle,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005982 DRM_CLOEXEC, &fd);
5983 if (ret) {
5984 weston_log("[libva recorder] "
5985 "failed to create prime fd for front buffer\n");
5986 return;
5987 }
5988
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03005989 ret = vaapi_recorder_frame(output->recorder, fd,
Daniel Stonee2e80132018-01-16 15:37:33 +00005990 output->scanout_plane->state_cur->fb->stride);
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03005991 if (ret < 0) {
5992 weston_log("[libva recorder] aborted: %m\n");
5993 recorder_destroy(output);
5994 }
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005995}
5996
5997static void *
Giulio Camuffo954f1832014-10-11 18:27:30 +03005998create_recorder(struct drm_backend *b, int width, int height,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005999 const char *filename)
6000{
6001 int fd;
6002 drm_magic_t magic;
6003
Giulio Camuffo954f1832014-10-11 18:27:30 +03006004 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03006005 if (fd < 0)
6006 return NULL;
6007
6008 drmGetMagic(fd, &magic);
Giulio Camuffo954f1832014-10-11 18:27:30 +03006009 drmAuthMagic(b->drm.fd, magic);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03006010
6011 return vaapi_recorder_create(fd, width, height, filename);
6012}
6013
6014static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02006015recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time,
6016 uint32_t key, void *data)
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03006017{
Giulio Camuffo954f1832014-10-11 18:27:30 +03006018 struct drm_backend *b = data;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03006019 struct drm_output *output;
6020 int width, height;
6021
Giulio Camuffo954f1832014-10-11 18:27:30 +03006022 output = container_of(b->compositor->output_list.next,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03006023 struct drm_output, base.link);
6024
6025 if (!output->recorder) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01006026 if (output->gbm_format != GBM_FORMAT_XRGB8888) {
Ander Conselvan de Oliveira2ef1cd12014-05-06 16:49:06 +03006027 weston_log("failed to start vaapi recorder: "
6028 "output format not supported\n");
6029 return;
6030 }
6031
Hardeningff39efa2013-09-18 23:56:35 +02006032 width = output->base.current_mode->width;
6033 height = output->base.current_mode->height;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03006034
6035 output->recorder =
Giulio Camuffo954f1832014-10-11 18:27:30 +03006036 create_recorder(b, width, height, "capture.h264");
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03006037 if (!output->recorder) {
6038 weston_log("failed to create vaapi recorder\n");
6039 return;
6040 }
6041
6042 output->base.disable_planes++;
6043
6044 output->recorder_frame_listener.notify = recorder_frame_notify;
6045 wl_signal_add(&output->base.frame_signal,
6046 &output->recorder_frame_listener);
6047
6048 weston_output_schedule_repaint(&output->base);
6049
6050 weston_log("[libva recorder] initialized\n");
6051 } else {
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03006052 recorder_destroy(output);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03006053 }
6054}
6055#else
6056static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02006057recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time,
6058 uint32_t key, void *data)
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03006059{
6060 weston_log("Compiled without libva support\n");
6061}
6062#endif
6063
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02006064static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03006065switch_to_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02006066{
6067 struct drm_output *output;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03006068 bool dmabuf_support_inited;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02006069
Giulio Camuffo954f1832014-10-11 18:27:30 +03006070 if (!b->use_pixman)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02006071 return;
6072
Pekka Paalanene4d231e2014-06-12 15:12:48 +03006073 dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf;
6074
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02006075 weston_log("Switching to GL renderer\n");
6076
Giulio Camuffo954f1832014-10-11 18:27:30 +03006077 b->gbm = create_gbm_device(b->drm.fd);
6078 if (!b->gbm) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02006079 weston_log("Failed to create gbm device. "
6080 "Aborting renderer switch\n");
6081 return;
6082 }
6083
Giulio Camuffo954f1832014-10-11 18:27:30 +03006084 wl_list_for_each(output, &b->compositor->output_list, base.link)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02006085 pixman_renderer_output_destroy(&output->base);
6086
Giulio Camuffo954f1832014-10-11 18:27:30 +03006087 b->compositor->renderer->destroy(b->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02006088
Giulio Camuffo954f1832014-10-11 18:27:30 +03006089 if (drm_backend_create_gl_renderer(b) < 0) {
6090 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02006091 weston_log("Failed to create GL renderer. Quitting.\n");
6092 /* FIXME: we need a function to shutdown cleanly */
6093 assert(0);
6094 }
6095
Giulio Camuffo954f1832014-10-11 18:27:30 +03006096 wl_list_for_each(output, &b->compositor->output_list, base.link)
6097 drm_output_init_egl(output, b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02006098
Giulio Camuffo954f1832014-10-11 18:27:30 +03006099 b->use_pixman = 0;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03006100
6101 if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) {
6102 if (linux_dmabuf_setup(b->compositor) < 0)
6103 weston_log("Error: initializing dmabuf "
6104 "support failed.\n");
6105 }
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02006106}
6107
6108static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02006109renderer_switch_binding(struct weston_keyboard *keyboard,
6110 const struct timespec *time, uint32_t key, void *data)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02006111{
Derek Foreman8ae2db52015-07-15 13:00:36 -05006112 struct drm_backend *b =
Armin Krezović545dba62016-08-05 15:54:18 +02006113 to_drm_backend(keyboard->seat->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02006114
Giulio Camuffo954f1832014-10-11 18:27:30 +03006115 switch_to_gl_renderer(b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02006116}
6117
Armin Krezović08368132016-09-30 14:11:05 +02006118static const struct weston_drm_output_api api = {
6119 drm_output_set_mode,
6120 drm_output_set_gbm_format,
6121 drm_output_set_seat,
6122};
6123
Giulio Camuffo954f1832014-10-11 18:27:30 +03006124static struct drm_backend *
6125drm_backend_create(struct weston_compositor *compositor,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07006126 struct weston_drm_backend_config *config)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04006127{
Giulio Camuffo954f1832014-10-11 18:27:30 +03006128 struct drm_backend *b;
David Herrmann0af066f2012-10-29 19:21:16 +01006129 struct udev_device *drm_device;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04006130 struct wl_event_loop *loop;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07006131 const char *seat_id = default_seat;
nerdopolisb16c4ac2018-06-29 08:17:46 -04006132 const char *session_seat;
Armin Krezović08368132016-09-30 14:11:05 +02006133 int ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04006134
nerdopolisb16c4ac2018-06-29 08:17:46 -04006135 session_seat = getenv("XDG_SEAT");
6136 if (session_seat)
6137 seat_id = session_seat;
6138
6139 if (config->seat_id)
6140 seat_id = config->seat_id;
6141
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04006142 weston_log("initializing drm backend\n");
6143
Giulio Camuffo954f1832014-10-11 18:27:30 +03006144 b = zalloc(sizeof *b);
6145 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006146 return NULL;
Daniel Stone725c2c32012-06-22 14:04:36 +01006147
Daniel Stone6020f472018-02-05 15:46:20 +00006148 b->state_invalid = true;
Daniel Stoneefa504f2016-12-19 16:48:20 +00006149 b->drm.fd = -1;
Daniel Stone087ddf02017-02-14 17:51:30 +00006150 wl_array_init(&b->unused_crtcs);
Daniel Stoneefa504f2016-12-19 16:48:20 +00006151
Pekka Paalanen68583832015-05-19 09:53:16 +03006152 /*
6153 * KMS support for hardware planes cannot properly synchronize
6154 * without nuclear page flip. Without nuclear/atomic, hw plane
6155 * and cursor plane updates would either tear or cause extra
6156 * waits for vblanks which means dropping the compositor framerate
Pekka Paalanen3f32a132015-09-07 15:38:43 +03006157 * to a fraction. For cursors, it's not so bad, so they are
6158 * enabled.
Pekka Paalanen68583832015-05-19 09:53:16 +03006159 *
6160 * These can be enabled again when nuclear/atomic support lands.
6161 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03006162 b->sprites_are_broken = 1;
Giulio Camuffo954f1832014-10-11 18:27:30 +03006163 b->compositor = compositor;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07006164 b->use_pixman = config->use_pixman;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00006165 b->pageflip_timeout = config->pageflip_timeout;
Pekka Paalanendee412d2018-04-23 11:44:58 +02006166 b->use_pixman_shadow = config->use_pixman_shadow;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07006167
Pekka Paalanen7da9a382017-08-30 11:29:49 +03006168 compositor->backend = &b->base;
6169
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07006170 if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0)
6171 goto err_compositor;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07006172
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01006173 /* Check if we run drm-backend using weston-launch */
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07006174 compositor->launcher = weston_launcher_connect(compositor, config->tty,
6175 seat_id, true);
Giulio Camuffo954f1832014-10-11 18:27:30 +03006176 if (compositor->launcher == NULL) {
Pekka Paalanena453f4d2017-10-31 10:19:48 +02006177 weston_log("fatal: drm backend should be run using "
6178 "weston-launch binary, or your system should "
6179 "provide the logind D-Bus API.\n");
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01006180 goto err_compositor;
6181 }
6182
Giulio Camuffo954f1832014-10-11 18:27:30 +03006183 b->udev = udev_new();
6184 if (b->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02006185 weston_log("failed to initialize udev context\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07006186 goto err_launcher;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04006187 }
6188
Giulio Camuffo954f1832014-10-11 18:27:30 +03006189 b->session_listener.notify = session_notify;
6190 wl_signal_add(&compositor->session_signal, &b->session_listener);
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05006191
Pekka Paalanenb45ed8b2017-03-28 18:04:27 +03006192 if (config->specific_device)
6193 drm_device = open_specific_drm_device(b, config->specific_device);
6194 else
6195 drm_device = find_primary_gpu(b, seat_id);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04006196 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02006197 weston_log("no drm device found\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07006198 goto err_udev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006199 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04006200
Daniel Stoneefa504f2016-12-19 16:48:20 +00006201 if (init_kms_caps(b) < 0) {
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02006202 weston_log("failed to initialize kms\n");
6203 goto err_udev_dev;
6204 }
6205
Giulio Camuffo954f1832014-10-11 18:27:30 +03006206 if (b->use_pixman) {
6207 if (init_pixman(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02006208 weston_log("failed to initialize pixman renderer\n");
6209 goto err_udev_dev;
6210 }
6211 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +03006212 if (init_egl(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02006213 weston_log("failed to initialize egl\n");
6214 goto err_udev_dev;
6215 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006216 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05006217
Giulio Camuffo954f1832014-10-11 18:27:30 +03006218 b->base.destroy = drm_destroy;
Daniel Stoneeedf84c2017-02-10 18:06:04 +00006219 b->base.repaint_begin = drm_repaint_begin;
6220 b->base.repaint_flush = drm_repaint_flush;
6221 b->base.repaint_cancel = drm_repaint_cancel;
Pekka Paalanenc112f002017-08-28 16:27:20 +03006222 b->base.create_output = drm_output_create;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02006223
Bob Ham91880f12016-01-12 10:21:47 +00006224 weston_setup_vt_switch_bindings(compositor);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04006225
Daniel Stone085d2b92015-05-21 00:00:57 +01006226 wl_list_init(&b->plane_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03006227 create_sprites(b);
Jesse Barnes58ef3792012-02-23 09:45:49 -05006228
Giulio Camuffo954f1832014-10-11 18:27:30 +03006229 if (udev_input_init(&b->input,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03006230 compositor, b->udev, seat_id,
6231 config->configure_device) < 0) {
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03006232 weston_log("failed to create input devices\n");
6233 goto err_sprite;
6234 }
6235
Pekka Paalanend2e62422017-09-08 15:48:07 +03006236 if (drm_backend_create_heads(b, drm_device) < 0) {
6237 weston_log("Failed to create heads for %s\n", b->drm.filename);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03006238 goto err_udev_input;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006239 }
6240
Jason Ekstrand9fc71512014-04-02 19:53:46 -05006241 /* A this point we have some idea of whether or not we have a working
6242 * cursor plane. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03006243 if (!b->cursors_are_broken)
6244 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
Jason Ekstrand9fc71512014-04-02 19:53:46 -05006245
Giulio Camuffo954f1832014-10-11 18:27:30 +03006246 loop = wl_display_get_event_loop(compositor->wl_display);
6247 b->drm_source =
6248 wl_event_loop_add_fd(loop, b->drm.fd,
6249 WL_EVENT_READABLE, on_drm_input, b);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006250
Giulio Camuffo954f1832014-10-11 18:27:30 +03006251 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
6252 if (b->udev_monitor == NULL) {
Abdur Rehman4dca0e12017-01-01 19:46:35 +05006253 weston_log("failed to initialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01006254 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01006255 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03006256 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01006257 "drm", NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03006258 b->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02006259 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03006260 udev_monitor_get_fd(b->udev_monitor),
6261 WL_EVENT_READABLE, udev_drm_event, b);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01006262
Giulio Camuffo954f1832014-10-11 18:27:30 +03006263 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02006264 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01006265 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01006266 }
6267
Daniel Stonea96b93c2012-06-22 14:04:37 +01006268 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01006269
Giulio Camuffo954f1832014-10-11 18:27:30 +03006270 weston_compositor_add_debug_binding(compositor, KEY_O,
6271 planes_binding, b);
6272 weston_compositor_add_debug_binding(compositor, KEY_C,
6273 planes_binding, b);
6274 weston_compositor_add_debug_binding(compositor, KEY_V,
6275 planes_binding, b);
6276 weston_compositor_add_debug_binding(compositor, KEY_Q,
6277 recorder_binding, b);
6278 weston_compositor_add_debug_binding(compositor, KEY_W,
6279 renderer_switch_binding, b);
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02006280
Pekka Paalanene4d231e2014-06-12 15:12:48 +03006281 if (compositor->renderer->import_dmabuf) {
6282 if (linux_dmabuf_setup(compositor) < 0)
6283 weston_log("Error: initializing dmabuf "
6284 "support failed.\n");
6285 }
6286
Armin Krezović08368132016-09-30 14:11:05 +02006287 ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME,
6288 &api, sizeof(api));
6289
6290 if (ret < 0) {
6291 weston_log("Failed to register output API.\n");
6292 goto err_udev_monitor;
6293 }
6294
Giulio Camuffo954f1832014-10-11 18:27:30 +03006295 return b;
Daniel Stonea96b93c2012-06-22 14:04:37 +01006296
6297err_udev_monitor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03006298 wl_event_source_remove(b->udev_drm_source);
6299 udev_monitor_unref(b->udev_monitor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01006300err_drm_source:
Giulio Camuffo954f1832014-10-11 18:27:30 +03006301 wl_event_source_remove(b->drm_source);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03006302err_udev_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03006303 udev_input_destroy(&b->input);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04006304err_sprite:
Emmanuel Gil Peyrotb8347e32016-05-02 22:40:13 +01006305 if (b->gbm)
6306 gbm_device_destroy(b->gbm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03006307 destroy_sprites(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01006308err_udev_dev:
6309 udev_device_unref(drm_device);
Kristian Høgsberg3f495872013-09-18 23:00:17 -07006310err_launcher:
Giulio Camuffo954f1832014-10-11 18:27:30 +03006311 weston_launcher_destroy(compositor->launcher);
Daniel Stonea96b93c2012-06-22 14:04:37 +01006312err_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +03006313 udev_unref(b->udev);
Daniel Stonea96b93c2012-06-22 14:04:37 +01006314err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03006315 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03006316 free(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01006317 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04006318}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006319
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07006320static void
6321config_init_to_defaults(struct weston_drm_backend_config *config)
6322{
Pekka Paalanendee412d2018-04-23 11:44:58 +02006323 config->use_pixman_shadow = true;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07006324}
6325
Giulio Camuffo954f1832014-10-11 18:27:30 +03006326WL_EXPORT int
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01006327weston_backend_init(struct weston_compositor *compositor,
6328 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006329{
Giulio Camuffo954f1832014-10-11 18:27:30 +03006330 struct drm_backend *b;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07006331 struct weston_drm_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006332
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07006333 if (config_base == NULL ||
6334 config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION ||
6335 config_base->struct_size > sizeof(struct weston_drm_backend_config)) {
6336 weston_log("drm backend config structure is invalid\n");
6337 return -1;
6338 }
Benjamin Franzke117483d2011-08-30 11:38:26 +02006339
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07006340 config_init_to_defaults(&config);
6341 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07006342
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07006343 b = drm_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03006344 if (b == NULL)
6345 return -1;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07006346
Giulio Camuffo954f1832014-10-11 18:27:30 +03006347 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006348}