blob: 321ee1915bf2cf7e11605416afb40dd600b45ad3 [file] [log] [blame]
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07005 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040012 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070013 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040025 */
26
Daniel Stonec228e232013-05-22 18:03:19 +030027#include "config.h"
Kristian Høgsberg0b9334a2011-04-12 11:34:32 -040028
Jesse Barnes58ef3792012-02-23 09:45:49 -050029#include <errno.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030030#include <stdint.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040031#include <stdlib.h>
Richard Hughes2b2092a2013-04-24 14:58:02 +010032#include <ctype.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040033#include <string.h>
34#include <fcntl.h>
35#include <unistd.h>
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -040036#include <linux/input.h>
Kristian Høgsberg3f495872013-09-18 23:00:17 -070037#include <linux/vt.h>
Ander Conselvan de Oliveirafd1f4c62012-06-26 17:09:14 +030038#include <assert.h>
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +020039#include <sys/mman.h>
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +030040#include <dlfcn.h>
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +030041#include <time.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040042
Benjamin Franzkec649a922011-03-02 11:56:04 +010043#include <xf86drm.h>
44#include <xf86drmMode.h>
Jesse Barnes58ef3792012-02-23 09:45:49 -050045#include <drm_fourcc.h>
Benjamin Franzkec649a922011-03-02 11:56:04 +010046
Benjamin Franzke060cf802011-04-30 09:32:11 +020047#include <gbm.h>
Pekka Paalanen33156972012-08-03 13:30:30 -040048#include <libudev.h>
Benjamin Franzke060cf802011-04-30 09:32:11 +020049
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -070050#include "compositor.h"
51#include "compositor-drm.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070052#include "shared/helpers.h"
Mario Kleinerf507ec32015-06-21 21:25:14 +020053#include "shared/timespec-util.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010054#include "gl-renderer.h"
Vincent Abriouc9506672016-10-05 16:14:07 +020055#include "weston-egl-ext.h"
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +020056#include "pixman-renderer.h"
Daniel Stone0b70fa42017-04-04 17:54:23 +010057#include "pixel-formats.h"
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -070058#include "libbacklight.h"
Peter Hutterer823ad332014-11-26 07:06:31 +100059#include "libinput-seat.h"
Benjamin Franzkebfeda132012-01-30 14:04:04 +010060#include "launcher-util.h"
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +030061#include "vaapi-recorder.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020062#include "presentation-time-server-protocol.h"
Pekka Paalanene4d231e2014-06-12 15:12:48 +030063#include "linux-dmabuf.h"
Micah Fedkec8890122017-02-01 15:28:23 -050064#include "linux-dmabuf-unstable-v1-server-protocol.h"
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040065
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +030066#ifndef DRM_CAP_TIMESTAMP_MONOTONIC
67#define DRM_CAP_TIMESTAMP_MONOTONIC 0x6
68#endif
69
Pekka Paalanenc5de57f2015-05-20 23:01:44 +010070#ifndef DRM_CLIENT_CAP_UNIVERSAL_PLANES
71#define DRM_CLIENT_CAP_UNIVERSAL_PLANES 2
72#endif
73
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -030074#ifndef DRM_CAP_CURSOR_WIDTH
75#define DRM_CAP_CURSOR_WIDTH 0x8
76#endif
77
78#ifndef DRM_CAP_CURSOR_HEIGHT
79#define DRM_CAP_CURSOR_HEIGHT 0x9
80#endif
81
82#ifndef GBM_BO_USE_CURSOR
83#define GBM_BO_USE_CURSOR GBM_BO_USE_CURSOR_64X64
84#endif
85
Daniel Stone02cf4662017-03-03 16:19:39 +000086/**
Daniel Stone02cf4662017-03-03 16:19:39 +000087 * Represents the values of an enum-type KMS property
88 */
89struct drm_property_enum_info {
90 const char *name; /**< name as string (static, not freed) */
91 bool valid; /**< true if value is supported; ignore if false */
92 uint64_t value; /**< raw value */
93};
94
95/**
96 * Holds information on a DRM property, including its ID and the enum
97 * values it holds.
98 *
99 * DRM properties are allocated dynamically, and maintained as DRM objects
100 * within the normal object ID space; they thus do not have a stable ID
101 * to refer to. This includes enum values, which must be referred to by
102 * integer values, but these are not stable.
103 *
104 * drm_property_info allows a cache to be maintained where Weston can use
105 * enum values internally to refer to properties, with the mapping to DRM
106 * ID values being maintained internally.
107 */
108struct drm_property_info {
109 const char *name; /**< name as string (static, not freed) */
110 uint32_t prop_id; /**< KMS property object ID */
111 unsigned int num_enum_values; /**< number of enum values */
112 struct drm_property_enum_info *enum_values; /**< array of enum values */
113};
114
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000115/**
Daniel Stone598ee9d2016-11-16 11:55:20 +0000116 * List of properties attached to DRM planes
117 */
118enum wdrm_plane_property {
119 WDRM_PLANE_TYPE = 0,
120 WDRM_PLANE_SRC_X,
121 WDRM_PLANE_SRC_Y,
122 WDRM_PLANE_SRC_W,
123 WDRM_PLANE_SRC_H,
124 WDRM_PLANE_CRTC_X,
125 WDRM_PLANE_CRTC_Y,
126 WDRM_PLANE_CRTC_W,
127 WDRM_PLANE_CRTC_H,
128 WDRM_PLANE_FB_ID,
129 WDRM_PLANE_CRTC_ID,
130 WDRM_PLANE__COUNT
131};
132
133/**
134 * Possible values for the WDRM_PLANE_TYPE property.
135 */
136enum wdrm_plane_type {
137 WDRM_PLANE_TYPE_PRIMARY = 0,
138 WDRM_PLANE_TYPE_CURSOR,
139 WDRM_PLANE_TYPE_OVERLAY,
140 WDRM_PLANE_TYPE__COUNT
141};
142
143static struct drm_property_enum_info plane_type_enums[] = {
144 [WDRM_PLANE_TYPE_PRIMARY] = {
145 .name = "Primary",
146 },
147 [WDRM_PLANE_TYPE_OVERLAY] = {
148 .name = "Overlay",
149 },
150 [WDRM_PLANE_TYPE_CURSOR] = {
151 .name = "Cursor",
152 },
153};
154
155static const struct drm_property_info plane_props[] = {
156 [WDRM_PLANE_TYPE] = {
157 .name = "type",
158 .enum_values = plane_type_enums,
159 .num_enum_values = WDRM_PLANE_TYPE__COUNT,
160 },
161 [WDRM_PLANE_SRC_X] = { .name = "SRC_X", },
162 [WDRM_PLANE_SRC_Y] = { .name = "SRC_Y", },
163 [WDRM_PLANE_SRC_W] = { .name = "SRC_W", },
164 [WDRM_PLANE_SRC_H] = { .name = "SRC_H", },
165 [WDRM_PLANE_CRTC_X] = { .name = "CRTC_X", },
166 [WDRM_PLANE_CRTC_Y] = { .name = "CRTC_Y", },
167 [WDRM_PLANE_CRTC_W] = { .name = "CRTC_W", },
168 [WDRM_PLANE_CRTC_H] = { .name = "CRTC_H", },
169 [WDRM_PLANE_FB_ID] = { .name = "FB_ID", },
170 [WDRM_PLANE_CRTC_ID] = { .name = "CRTC_ID", },
171};
172
173/**
174 * List of properties attached to a DRM connector
175 */
176enum wdrm_connector_property {
177 WDRM_CONNECTOR_EDID = 0,
178 WDRM_CONNECTOR_DPMS,
179 WDRM_CONNECTOR_CRTC_ID,
180 WDRM_CONNECTOR__COUNT
181};
182
183static const struct drm_property_info connector_props[] = {
184 [WDRM_CONNECTOR_EDID] = { .name = "EDID" },
185 [WDRM_CONNECTOR_DPMS] = { .name = "DPMS" },
186 [WDRM_CONNECTOR_CRTC_ID] = { .name = "CRTC_ID", },
187};
188
189/**
Pekka Paalanencd011a62016-11-15 22:07:49 +0000190 * List of properties attached to DRM CRTCs
191 */
192enum wdrm_crtc_property {
193 WDRM_CRTC_MODE_ID = 0,
194 WDRM_CRTC_ACTIVE,
195 WDRM_CRTC__COUNT
196};
197
Daniel Stone598ee9d2016-11-16 11:55:20 +0000198static const struct drm_property_info crtc_props[] = {
199 [WDRM_CRTC_MODE_ID] = { .name = "MODE_ID", },
200 [WDRM_CRTC_ACTIVE] = { .name = "ACTIVE", },
201};
202
Pekka Paalanencd011a62016-11-15 22:07:49 +0000203/**
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000204 * Mode for drm_output_state_duplicate.
205 */
206enum drm_output_state_duplicate_mode {
207 DRM_OUTPUT_STATE_CLEAR_PLANES, /**< reset all planes to off */
208 DRM_OUTPUT_STATE_PRESERVE_PLANES, /**< preserve plane state */
209};
210
211/**
212 * Mode for drm_pending_state_apply and co.
213 */
214enum drm_state_apply_mode {
215 DRM_STATE_APPLY_SYNC, /**< state fully processed */
216 DRM_STATE_APPLY_ASYNC, /**< state pending event delivery */
217};
218
Giulio Camuffo954f1832014-10-11 18:27:30 +0300219struct drm_backend {
220 struct weston_backend base;
221 struct weston_compositor *compositor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400222
223 struct udev *udev;
224 struct wl_event_source *drm_source;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400225
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100226 struct udev_monitor *udev_monitor;
227 struct wl_event_source *udev_drm_source;
228
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100229 struct {
David Herrmannd7488c22012-03-11 20:05:21 +0100230 int id;
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100231 int fd;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300232 char *filename;
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100233 } drm;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200234 struct gbm_device *gbm;
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700235 struct wl_listener session_listener;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100236 uint32_t gbm_format;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200237
Rob Clark4339add2012-08-09 14:18:28 -0500238 /* we need these parameters in order to not fail drmModeAddFB2()
239 * due to out of bounds dimensions, and then mistakenly set
240 * sprites_are_broken:
241 */
Daniel Stonef214fdc2016-11-14 17:43:57 +0000242 int min_width, max_width;
243 int min_height, max_height;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200244 int no_addfb2;
Rob Clark4339add2012-08-09 14:18:28 -0500245
Daniel Stone085d2b92015-05-21 00:00:57 +0100246 struct wl_list plane_list;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500247 int sprites_are_broken;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +0200248 int sprites_hidden;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500249
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000250 void *repaint_data;
251
Daniel Stone6020f472018-02-05 15:46:20 +0000252 bool state_invalid;
253
Daniel Stone087ddf02017-02-14 17:51:30 +0000254 /* Connector and CRTC IDs not used by any enabled output. */
255 struct wl_array unused_connectors;
256 struct wl_array unused_crtcs;
257
Rob Clarkab5b1e32012-08-09 13:24:45 -0500258 int cursors_are_broken;
259
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100260 bool universal_planes;
Pekka Paalanencd011a62016-11-15 22:07:49 +0000261 bool atomic_modeset;
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100262
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200263 int use_pixman;
264
Rob Bradfordd355b802013-05-31 18:09:55 +0100265 struct udev_input input;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -0300266
Daniel Stone70d337d2015-06-16 18:42:23 +0100267 int32_t cursor_width;
268 int32_t cursor_height;
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +0000269
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000270 uint32_t pageflip_timeout;
Daniel Stoneb57c6a02017-10-05 16:27:21 +0100271
272 bool shutting_down;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400273};
274
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400275struct drm_mode {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500276 struct weston_mode base;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400277 drmModeModeInfo mode_info;
Daniel Stoned5526cb2016-11-16 10:54:10 +0000278 uint32_t blob_id;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400279};
280
Daniel Stonefc175a72017-04-04 17:54:22 +0100281enum drm_fb_type {
282 BUFFER_INVALID = 0, /**< never used */
283 BUFFER_CLIENT, /**< directly sourced from client */
284 BUFFER_PIXMAN_DUMB, /**< internal Pixman rendering */
285 BUFFER_GBM_SURFACE, /**< internal EGL rendering */
Daniel Stonee4256832017-04-04 17:54:27 +0100286 BUFFER_CURSOR, /**< internal cursor buffer */
Daniel Stonefc175a72017-04-04 17:54:22 +0100287};
288
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300289struct drm_fb {
Daniel Stonefc175a72017-04-04 17:54:22 +0100290 enum drm_fb_type type;
291
Daniel Stone6e7a9612017-04-04 17:54:26 +0100292 int refcnt;
293
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200294 uint32_t fb_id, stride, handle, size;
Daniel Stone0b70fa42017-04-04 17:54:23 +0100295 const struct pixel_format_info *format;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000296 int width, height;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200297 int fd;
Pekka Paalanende685b82012-12-04 15:58:12 +0200298 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200299
300 /* Used by gbm fbs */
301 struct gbm_bo *bo;
Daniel Stone05a5ac22017-04-04 17:54:25 +0100302 struct gbm_surface *gbm_surface;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200303
304 /* Used by dumb fbs */
305 void *map;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300306};
307
Richard Hughes2b2092a2013-04-24 14:58:02 +0100308struct drm_edid {
309 char eisa_id[13];
310 char monitor_name[13];
311 char pnp_id[5];
312 char serial_number[13];
313};
314
Daniel Stone08d4edf2017-04-04 17:54:34 +0100315/**
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000316 * Pending state holds one or more drm_output_state structures, collected from
317 * performing repaint. This pending state is transient, and only lives between
318 * beginning a repaint group and flushing the results: after flush, each
319 * output state will complete and be retired separately.
320 */
321struct drm_pending_state {
322 struct drm_backend *backend;
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000323 struct wl_list output_list;
324};
325
326/*
327 * Output state holds the dynamic state for one Weston output, i.e. a KMS CRTC,
328 * plus >= 1 each of encoder/connector/plane. Since everything but the planes
329 * is currently statically assigned per-output, we mainly use this to track
330 * plane state.
331 *
332 * pending_state is set when the output state is owned by a pending_state,
333 * i.e. when it is being constructed and has not yet been applied. When the
334 * output state has been applied, the owning pending_state is freed.
335 */
336struct drm_output_state {
337 struct drm_pending_state *pending_state;
338 struct drm_output *output;
339 struct wl_list link;
Daniel Stonea08512f2016-11-08 17:46:10 +0000340 enum dpms_enum dpms;
Daniel Stonebc15f682016-11-14 16:57:01 +0000341 struct wl_list plane_list;
342};
343
344/**
345 * Plane state holds the dynamic state for a plane: where it is positioned,
346 * and which buffer it is currently displaying.
347 *
348 * The plane state is owned by an output state, except when setting an initial
349 * state. See drm_output_state for notes on state object lifetime.
350 */
351struct drm_plane_state {
352 struct drm_plane *plane;
353 struct drm_output *output;
354 struct drm_output_state *output_state;
355
356 struct drm_fb *fb;
357
358 int32_t src_x, src_y;
359 uint32_t src_w, src_h;
360 int32_t dest_x, dest_y;
361 uint32_t dest_w, dest_h;
362
363 bool complete;
364
365 struct wl_list link; /* drm_output_state::plane_list */
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000366};
367
368/**
Daniel Stone08d4edf2017-04-04 17:54:34 +0100369 * A plane represents one buffer, positioned within a CRTC, and stacked
370 * relative to other planes on the same CRTC.
371 *
372 * Each CRTC has a 'primary plane', which use used to display the classic
373 * framebuffer contents, as accessed through the legacy drmModeSetCrtc
374 * call (which combines setting the CRTC's actual physical mode, and the
375 * properties of the primary plane).
376 *
377 * The cursor plane also has its own alternate legacy API.
378 *
379 * Other planes are used opportunistically to display content we do not
380 * wish to blit into the primary plane. These non-primary/cursor planes
381 * are referred to as 'sprites'.
382 */
383struct drm_plane {
Daniel Stone08d4edf2017-04-04 17:54:34 +0100384 struct weston_plane base;
385
Daniel Stone08d4edf2017-04-04 17:54:34 +0100386 struct drm_backend *backend;
387
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100388 enum wdrm_plane_type type;
389
Daniel Stone08d4edf2017-04-04 17:54:34 +0100390 uint32_t possible_crtcs;
391 uint32_t plane_id;
392 uint32_t count_formats;
393
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100394 struct drm_property_info props[WDRM_PLANE__COUNT];
395
Daniel Stonebc15f682016-11-14 16:57:01 +0000396 /* The last state submitted to the kernel for this plane. */
397 struct drm_plane_state *state_cur;
Daniel Stone08d4edf2017-04-04 17:54:34 +0100398
Daniel Stonebc15f682016-11-14 16:57:01 +0000399 struct wl_list link;
Daniel Stone08d4edf2017-04-04 17:54:34 +0100400
401 uint32_t formats[];
402};
403
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400404struct drm_output {
Armin Krezović08368132016-09-30 14:11:05 +0200405 struct weston_output base;
406 drmModeConnector *connector;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400407
Daniel Stone17339232015-11-28 12:09:47 +0000408 uint32_t crtc_id; /* object ID to pass to DRM functions */
409 int pipe; /* index of CRTC in resource array / bitmasks */
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400410 uint32_t connector_id;
Richard Hughes2b2092a2013-04-24 14:58:02 +0100411 struct drm_edid edid;
Daniel Stone02cf4662017-03-03 16:19:39 +0000412
413 /* Holds the properties for the connector */
414 struct drm_property_info props_conn[WDRM_CONNECTOR__COUNT];
Pekka Paalanencd011a62016-11-15 22:07:49 +0000415 /* Holds the properties for the CRTC */
416 struct drm_property_info props_crtc[WDRM_CRTC__COUNT];
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200417
Daniel Stone5bb8f582017-04-04 17:54:28 +0100418 struct backlight *backlight;
Daniel Stone36609c72015-06-18 07:49:02 +0100419
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300420 int vblank_pending;
421 int page_flip_pending;
Daniel Stone598ee9d2016-11-16 11:55:20 +0000422 int atomic_complete_pending;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800423 int destroy_pending;
Armin Krezović08368132016-09-30 14:11:05 +0200424 int disable_pending;
Daniel Stonea08512f2016-11-08 17:46:10 +0000425 int dpms_off_pending;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300426
Daniel Stonee4256832017-04-04 17:54:27 +0100427 struct drm_fb *gbm_cursor_fb[2];
Daniel Stone2ba17f42015-05-19 20:02:41 +0100428 struct drm_plane *cursor_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500429 struct weston_view *cursor_view;
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400430 int current_cursor;
Daniel Stone5bb8f582017-04-04 17:54:28 +0100431
432 struct gbm_surface *gbm_surface;
433 uint32_t gbm_format;
434
Daniel Stonee2e80132018-01-16 15:37:33 +0000435 /* Plane being displayed directly on the CRTC */
436 struct drm_plane *scanout_plane;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200437
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000438 /* The last state submitted to the kernel for this CRTC. */
439 struct drm_output_state *state_cur;
440 /* The previously-submitted state, where the hardware has not
441 * yet acknowledged completion of state_cur. */
442 struct drm_output_state *state_last;
443
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200444 struct drm_fb *dumb[2];
445 pixman_image_t *image[2];
446 int current_image;
447 pixman_region32_t previous_damage;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300448
449 struct vaapi_recorder *recorder;
450 struct wl_listener recorder_frame_listener;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000451
452 struct wl_event_source *pageflip_timer;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400453};
454
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300455static struct gl_renderer_interface *gl_renderer;
456
Kristian Høgsberg98cfea62013-02-18 16:15:53 -0500457static const char default_seat[] = "seat0";
Pekka Paalanen33156972012-08-03 13:30:30 -0400458
Daniel Stone087ddf02017-02-14 17:51:30 +0000459static void
460wl_array_remove_uint32(struct wl_array *array, uint32_t elm)
461{
462 uint32_t *pos, *end;
463
464 end = (uint32_t *) ((char *) array->data + array->size);
465
466 wl_array_for_each(pos, array) {
467 if (*pos != elm)
468 continue;
469
470 array->size -= sizeof(*pos);
471 if (pos + 1 == end)
472 break;
473
474 memmove(pos, pos + 1, (char *) end - (char *) (pos + 1));
475 break;
476 }
477}
478
Armin Krezović545dba62016-08-05 15:54:18 +0200479static inline struct drm_output *
480to_drm_output(struct weston_output *base)
481{
482 return container_of(base, struct drm_output, base);
483}
484
485static inline struct drm_backend *
486to_drm_backend(struct weston_compositor *base)
487{
488 return container_of(base->backend, struct drm_backend, base);
489}
490
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000491static int
492pageflip_timeout(void *data) {
493 /*
494 * Our timer just went off, that means we're not receiving drm
495 * page flip events anymore for that output. Let's gracefully exit
496 * weston with a return value so devs can debug what's going on.
497 */
498 struct drm_output *output = data;
499 struct weston_compositor *compositor = output->base.compositor;
500
501 weston_log("Pageflip timeout reached on output %s, your "
502 "driver is probably buggy! Exiting.\n",
503 output->base.name);
504 weston_compositor_exit_with_code(compositor, EXIT_FAILURE);
505
506 return 0;
507}
508
509/* Creates the pageflip timer. Note that it isn't armed by default */
510static int
511drm_output_pageflip_timer_create(struct drm_output *output)
512{
513 struct wl_event_loop *loop = NULL;
514 struct weston_compositor *ec = output->base.compositor;
515
516 loop = wl_display_get_event_loop(ec->wl_display);
517 assert(loop);
518 output->pageflip_timer = wl_event_loop_add_timer(loop,
519 pageflip_timeout,
520 output);
521
522 if (output->pageflip_timer == NULL) {
523 weston_log("creating drm pageflip timer failed: %m\n");
524 return -1;
525 }
526
527 return 0;
528}
529
Daniel Stonecb04cc42016-11-16 11:51:27 +0000530static inline struct drm_mode *
531to_drm_mode(struct weston_mode *base)
532{
533 return container_of(base, struct drm_mode, base);
534}
535
Daniel Stone02cf4662017-03-03 16:19:39 +0000536/**
537 * Get the current value of a KMS property
538 *
539 * Given a drmModeObjectGetProperties return, as well as the drm_property_info
540 * for the target property, return the current value of that property,
541 * with an optional default. If the property is a KMS enum type, the return
542 * value will be translated into the appropriate internal enum.
543 *
544 * If the property is not present, the default value will be returned.
545 *
546 * @param info Internal structure for property to look up
547 * @param props Raw KMS properties for the target object
548 * @param def Value to return if property is not found
549 */
550static uint64_t
551drm_property_get_value(struct drm_property_info *info,
552 drmModeObjectPropertiesPtr props,
553 uint64_t def)
554{
555 unsigned int i;
556
557 if (info->prop_id == 0)
558 return def;
559
560 for (i = 0; i < props->count_props; i++) {
561 unsigned int j;
562
563 if (props->props[i] != info->prop_id)
564 continue;
565
566 /* Simple (non-enum) types can return the value directly */
567 if (info->num_enum_values == 0)
568 return props->prop_values[i];
569
570 /* Map from raw value to enum value */
571 for (j = 0; j < info->num_enum_values; j++) {
572 if (!info->enum_values[j].valid)
573 continue;
574 if (info->enum_values[j].value != props->prop_values[i])
575 continue;
576
577 return j;
578 }
579
580 /* We don't have a mapping for this enum; return default. */
581 break;
582 }
583
584 return def;
585}
586
587/**
588 * Cache DRM property values
589 *
590 * Update a per-object array of drm_property_info structures, given the
591 * DRM properties of the object.
592 *
593 * Call this every time an object newly appears (note that only connectors
594 * can be hotplugged), the first time it is seen, or when its status changes
595 * in a way which invalidates the potential property values (currently, the
596 * only case for this is connector hotplug).
597 *
598 * This updates the property IDs and enum values within the drm_property_info
599 * array.
600 *
601 * DRM property enum values are dynamic at runtime; the user must query the
602 * property to find out the desired runtime value for a requested string
603 * name. Using the 'type' field on planes as an example, there is no single
604 * hardcoded constant for primary plane types; instead, the property must be
605 * queried at runtime to find the value associated with the string "Primary".
606 *
607 * This helper queries and caches the enum values, to allow us to use a set
608 * of compile-time-constant enums portably across various implementations.
609 * The values given in enum_names are searched for, and stored in the
610 * same-indexed field of the map array.
611 *
612 * @param b DRM backend object
613 * @param src DRM property info array to source from
614 * @param info DRM property info array to copy into
615 * @param num_infos Number of entries in the source array
616 * @param props DRM object properties for the object
617 */
618static void
619drm_property_info_populate(struct drm_backend *b,
620 const struct drm_property_info *src,
621 struct drm_property_info *info,
622 unsigned int num_infos,
623 drmModeObjectProperties *props)
624{
625 drmModePropertyRes *prop;
626 unsigned i, j;
627
628 for (i = 0; i < num_infos; i++) {
629 unsigned int j;
630
631 info[i].name = src[i].name;
632 info[i].prop_id = 0;
633 info[i].num_enum_values = src[i].num_enum_values;
634
635 if (src[i].num_enum_values == 0)
636 continue;
637
638 info[i].enum_values =
639 malloc(src[i].num_enum_values *
640 sizeof(*info[i].enum_values));
641 assert(info[i].enum_values);
642 for (j = 0; j < info[i].num_enum_values; j++) {
643 info[i].enum_values[j].name = src[i].enum_values[j].name;
644 info[i].enum_values[j].valid = false;
645 }
646 }
647
648 for (i = 0; i < props->count_props; i++) {
649 unsigned int k;
650
651 prop = drmModeGetProperty(b->drm.fd, props->props[i]);
652 if (!prop)
653 continue;
654
655 for (j = 0; j < num_infos; j++) {
656 if (!strcmp(prop->name, info[j].name))
657 break;
658 }
659
660 /* We don't know/care about this property. */
661 if (j == num_infos) {
662#ifdef DEBUG
663 weston_log("DRM debug: unrecognized property %u '%s'\n",
664 prop->prop_id, prop->name);
665#endif
666 drmModeFreeProperty(prop);
667 continue;
668 }
669
670 if (info[j].num_enum_values == 0 &&
671 (prop->flags & DRM_MODE_PROP_ENUM)) {
672 weston_log("DRM: expected property %s to not be an"
673 " enum, but it is; ignoring\n", prop->name);
674 drmModeFreeProperty(prop);
675 continue;
676 }
677
678 info[j].prop_id = props->props[i];
679
680 if (info[j].num_enum_values == 0) {
681 drmModeFreeProperty(prop);
682 continue;
683 }
684
685 if (!(prop->flags & DRM_MODE_PROP_ENUM)) {
686 weston_log("DRM: expected property %s to be an enum,"
687 " but it is not; ignoring\n", prop->name);
688 drmModeFreeProperty(prop);
689 info[j].prop_id = 0;
690 continue;
691 }
692
693 for (k = 0; k < info[j].num_enum_values; k++) {
694 int l;
695
696 for (l = 0; l < prop->count_enums; l++) {
697 if (!strcmp(prop->enums[l].name,
698 info[j].enum_values[k].name))
699 break;
700 }
701
702 if (l == prop->count_enums)
703 continue;
704
705 info[j].enum_values[k].valid = true;
706 info[j].enum_values[k].value = prop->enums[l].value;
707 }
708
709 drmModeFreeProperty(prop);
710 }
711
712#ifdef DEBUG
713 for (i = 0; i < num_infos; i++) {
714 if (info[i].prop_id == 0)
715 weston_log("DRM warning: property '%s' missing\n",
716 info[i].name);
717 }
718#endif
719}
720
721/**
722 * Free DRM property information
723 *
Pekka Paalanen46e4f972017-09-07 15:32:01 +0300724 * Frees all memory associated with a DRM property info array and zeroes
725 * it out, leaving it usable for a further drm_property_info_update() or
726 * drm_property_info_free().
Daniel Stone02cf4662017-03-03 16:19:39 +0000727 *
728 * @param info DRM property info array
729 * @param num_props Number of entries in array to free
730 */
731static void
732drm_property_info_free(struct drm_property_info *info, int num_props)
733{
734 int i;
735
736 for (i = 0; i < num_props; i++)
737 free(info[i].enum_values);
Pekka Paalanen46e4f972017-09-07 15:32:01 +0300738
739 memset(info, 0, sizeof(*info) * num_props);
Daniel Stone02cf4662017-03-03 16:19:39 +0000740}
741
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400742static void
Daniel Stone2ba17f42015-05-19 20:02:41 +0100743drm_output_set_cursor(struct drm_output_state *output_state);
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400744
Mario Kleinerf507ec32015-06-21 21:25:14 +0200745static void
746drm_output_update_msc(struct drm_output *output, unsigned int seq);
747
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000748static void
749drm_output_destroy(struct weston_output *output_base);
750
Daniel Stone5ff289a2017-10-07 12:59:02 +0100751/**
752 * Returns true if the plane can be used on the given output for its current
753 * repaint cycle.
754 */
755static bool
756drm_plane_is_available(struct drm_plane *plane, struct drm_output *output)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500757{
Daniel Stone5ff289a2017-10-07 12:59:02 +0100758 assert(plane->state_cur);
759
760 /* The plane still has a request not yet completed by the kernel. */
761 if (!plane->state_cur->complete)
762 return false;
763
764 /* The plane is still active on another output. */
765 if (plane->state_cur->output && plane->state_cur->output != output)
766 return false;
767
768 /* Check whether the plane can be used with this CRTC; possible_crtcs
769 * is a bitmask of CRTC indices (pipe), rather than CRTC object ID. */
Daniel Stone08d4edf2017-04-04 17:54:34 +0100770 return !!(plane->possible_crtcs & (1 << output->pipe));
Jesse Barnes58ef3792012-02-23 09:45:49 -0500771}
772
Daniel Stone72c0e1b2017-02-09 13:49:15 +0000773static struct drm_output *
774drm_output_find_by_crtc(struct drm_backend *b, uint32_t crtc_id)
775{
776 struct drm_output *output;
777
778 wl_list_for_each(output, &b->compositor->output_list, base.link) {
779 if (output->crtc_id == crtc_id)
780 return output;
781 }
782
783 wl_list_for_each(output, &b->compositor->pending_output_list,
784 base.link) {
785 if (output->crtc_id == crtc_id)
786 return output;
787 }
788
789 return NULL;
790}
791
Daniel Stonec0ec7592017-02-09 13:58:35 +0000792static struct drm_output *
793drm_output_find_by_connector(struct drm_backend *b, uint32_t connector_id)
794{
795 struct drm_output *output;
796
797 wl_list_for_each(output, &b->compositor->output_list, base.link) {
798 if (output->connector_id == connector_id)
799 return output;
800 }
801
802 wl_list_for_each(output, &b->compositor->pending_output_list,
803 base.link) {
804 if (output->connector_id == connector_id)
805 return output;
806 }
807
808 return NULL;
809}
810
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300811static void
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100812drm_fb_destroy(struct drm_fb *fb)
813{
814 if (fb->fb_id != 0)
815 drmModeRmFB(fb->fd, fb->fb_id);
816 weston_buffer_reference(&fb->buffer_ref, NULL);
817 free(fb);
818}
819
820static void
821drm_fb_destroy_dumb(struct drm_fb *fb)
822{
823 struct drm_mode_destroy_dumb destroy_arg;
824
825 assert(fb->type == BUFFER_PIXMAN_DUMB);
826
827 if (fb->map && fb->size > 0)
828 munmap(fb->map, fb->size);
829
830 memset(&destroy_arg, 0, sizeof(destroy_arg));
831 destroy_arg.handle = fb->handle;
832 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
833
834 drm_fb_destroy(fb);
835}
836
837static void
838drm_fb_destroy_gbm(struct gbm_bo *bo, void *data)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300839{
840 struct drm_fb *fb = data;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300841
Daniel Stonee4256832017-04-04 17:54:27 +0100842 assert(fb->type == BUFFER_GBM_SURFACE || fb->type == BUFFER_CLIENT ||
843 fb->type == BUFFER_CURSOR);
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100844 drm_fb_destroy(fb);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300845}
846
847static struct drm_fb *
Daniel Stonef214fdc2016-11-14 17:43:57 +0000848drm_fb_create_dumb(struct drm_backend *b, int width, int height,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300849 uint32_t format)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200850{
851 struct drm_fb *fb;
852 int ret;
853
854 struct drm_mode_create_dumb create_arg;
855 struct drm_mode_destroy_dumb destroy_arg;
856 struct drm_mode_map_dumb map_arg;
857
Peter Huttererf3d62272013-08-08 11:57:05 +1000858 fb = zalloc(sizeof *fb);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200859 if (!fb)
860 return NULL;
861
Daniel Stone6e7a9612017-04-04 17:54:26 +0100862 fb->refcnt = 1;
863
Daniel Stone0b70fa42017-04-04 17:54:23 +0100864 fb->format = pixel_format_get_info(format);
865 if (!fb->format) {
866 weston_log("failed to look up format 0x%lx\n",
867 (unsigned long) format);
868 goto err_fb;
869 }
870
871 if (!fb->format->depth || !fb->format->bpp) {
872 weston_log("format 0x%lx is not compatible with dumb buffers\n",
873 (unsigned long) format);
874 goto err_fb;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300875 }
876
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700877 memset(&create_arg, 0, sizeof create_arg);
Daniel Stone0b70fa42017-04-04 17:54:23 +0100878 create_arg.bpp = fb->format->bpp;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200879 create_arg.width = width;
880 create_arg.height = height;
881
Giulio Camuffo954f1832014-10-11 18:27:30 +0300882 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200883 if (ret)
884 goto err_fb;
885
Daniel Stonefc175a72017-04-04 17:54:22 +0100886 fb->type = BUFFER_PIXMAN_DUMB;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200887 fb->handle = create_arg.handle;
888 fb->stride = create_arg.pitch;
889 fb->size = create_arg.size;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000890 fb->width = width;
891 fb->height = height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300892 fb->fd = b->drm.fd;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200893
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300894 ret = -1;
895
896 if (!b->no_addfb2) {
Yong Bakos4b6321f2016-08-17 17:37:55 -0700897 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300898
899 handles[0] = fb->handle;
900 pitches[0] = fb->stride;
901 offsets[0] = 0;
902
903 ret = drmModeAddFB2(b->drm.fd, width, height,
Daniel Stone0b70fa42017-04-04 17:54:23 +0100904 fb->format->format,
905 handles, pitches, offsets,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300906 &fb->fb_id, 0);
907 if (ret) {
908 weston_log("addfb2 failed: %m\n");
909 b->no_addfb2 = 1;
910 }
911 }
912
913 if (ret) {
Daniel Stone0b70fa42017-04-04 17:54:23 +0100914 ret = drmModeAddFB(b->drm.fd, width, height,
915 fb->format->depth, fb->format->bpp,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300916 fb->stride, fb->handle, &fb->fb_id);
917 }
918
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200919 if (ret)
920 goto err_bo;
921
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700922 memset(&map_arg, 0, sizeof map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200923 map_arg.handle = fb->handle;
Chris Michaeleb2074a2013-05-01 21:26:02 -0400924 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200925 if (ret)
926 goto err_add_fb;
927
Chris Michael4a7ce1f2015-11-10 10:40:37 -0500928 fb->map = mmap(NULL, fb->size, PROT_WRITE,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300929 MAP_SHARED, b->drm.fd, map_arg.offset);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200930 if (fb->map == MAP_FAILED)
931 goto err_add_fb;
932
933 return fb;
934
935err_add_fb:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300936 drmModeRmFB(b->drm.fd, fb->fb_id);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200937err_bo:
938 memset(&destroy_arg, 0, sizeof(destroy_arg));
939 destroy_arg.handle = create_arg.handle;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300940 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200941err_fb:
942 free(fb);
943 return NULL;
944}
945
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200946static struct drm_fb *
Daniel Stone6e7a9612017-04-04 17:54:26 +0100947drm_fb_ref(struct drm_fb *fb)
948{
949 fb->refcnt++;
950 return fb;
951}
952
953static struct drm_fb *
Daniel Stonefc175a72017-04-04 17:54:22 +0100954drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend,
955 uint32_t format, enum drm_fb_type type)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300956{
957 struct drm_fb *fb = gbm_bo_get_user_data(bo);
Derek Foreman482ffdf2016-07-08 12:50:57 -0500958 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300959 int ret;
960
Daniel Stonefc175a72017-04-04 17:54:22 +0100961 if (fb) {
962 assert(fb->type == type);
Daniel Stone6e7a9612017-04-04 17:54:26 +0100963 return drm_fb_ref(fb);
Daniel Stonefc175a72017-04-04 17:54:22 +0100964 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300965
Bryce Harringtonde16d892014-11-20 22:21:57 -0800966 fb = zalloc(sizeof *fb);
967 if (fb == NULL)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200968 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300969
Daniel Stonefc175a72017-04-04 17:54:22 +0100970 fb->type = type;
Daniel Stone6e7a9612017-04-04 17:54:26 +0100971 fb->refcnt = 1;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300972 fb->bo = bo;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300973
Daniel Stonec8c917c2016-11-14 17:45:58 +0000974 fb->width = gbm_bo_get_width(bo);
975 fb->height = gbm_bo_get_height(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200976 fb->stride = gbm_bo_get_stride(bo);
977 fb->handle = gbm_bo_get_handle(bo).u32;
Daniel Stone0b70fa42017-04-04 17:54:23 +0100978 fb->format = pixel_format_get_info(format);
Daniel Stonec8c917c2016-11-14 17:45:58 +0000979 fb->size = fb->stride * fb->height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300980 fb->fd = backend->drm.fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300981
Daniel Stone0b70fa42017-04-04 17:54:23 +0100982 if (!fb->format) {
983 weston_log("couldn't look up format 0x%lx\n",
984 (unsigned long) format);
985 goto err_free;
986 }
987
Daniel Stonec8c917c2016-11-14 17:45:58 +0000988 if (backend->min_width > fb->width ||
989 fb->width > backend->max_width ||
990 backend->min_height > fb->height ||
991 fb->height > backend->max_height) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200992 weston_log("bo geometry out of bounds\n");
993 goto err_free;
994 }
995
996 ret = -1;
997
Giulio Camuffo954f1832014-10-11 18:27:30 +0300998 if (format && !backend->no_addfb2) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200999 handles[0] = fb->handle;
1000 pitches[0] = fb->stride;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001001 offsets[0] = 0;
1002
Daniel Stonec8c917c2016-11-14 17:45:58 +00001003 ret = drmModeAddFB2(backend->drm.fd, fb->width, fb->height,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001004 format, handles, pitches, offsets,
1005 &fb->fb_id, 0);
1006 if (ret) {
1007 weston_log("addfb2 failed: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001008 backend->no_addfb2 = 1;
1009 backend->sprites_are_broken = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001010 }
1011 }
1012
Daniel Stone0b70fa42017-04-04 17:54:23 +01001013 if (ret && fb->format->depth && fb->format->bpp)
Daniel Stonec8c917c2016-11-14 17:45:58 +00001014 ret = drmModeAddFB(backend->drm.fd, fb->width, fb->height,
Daniel Stone0b70fa42017-04-04 17:54:23 +01001015 fb->format->depth, fb->format->bpp,
1016 fb->stride, fb->handle, &fb->fb_id);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001017
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001018 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +02001019 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001020 goto err_free;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001021 }
1022
Tomohito Esaki576f42e2017-04-04 17:54:24 +01001023 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_gbm);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001024
1025 return fb;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001026
1027err_free:
1028 free(fb);
1029 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001030}
1031
1032static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001033drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001034{
Pekka Paalanende685b82012-12-04 15:58:12 +02001035 assert(fb->buffer_ref.buffer == NULL);
Daniel Stonefc175a72017-04-04 17:54:22 +01001036 assert(fb->type == BUFFER_CLIENT);
Pekka Paalanende685b82012-12-04 15:58:12 +02001037 weston_buffer_reference(&fb->buffer_ref, buffer);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001038}
1039
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02001040static void
Daniel Stone05a5ac22017-04-04 17:54:25 +01001041drm_fb_unref(struct drm_fb *fb)
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02001042{
1043 if (!fb)
1044 return;
1045
Daniel Stone6e7a9612017-04-04 17:54:26 +01001046 assert(fb->refcnt > 0);
1047 if (--fb->refcnt > 0)
1048 return;
1049
Daniel Stonefc175a72017-04-04 17:54:22 +01001050 switch (fb->type) {
1051 case BUFFER_PIXMAN_DUMB:
Daniel Stone6e7a9612017-04-04 17:54:26 +01001052 drm_fb_destroy_dumb(fb);
Daniel Stonefc175a72017-04-04 17:54:22 +01001053 break;
Daniel Stonee4256832017-04-04 17:54:27 +01001054 case BUFFER_CURSOR:
Daniel Stonefc175a72017-04-04 17:54:22 +01001055 case BUFFER_CLIENT:
1056 gbm_bo_destroy(fb->bo);
1057 break;
1058 case BUFFER_GBM_SURFACE:
Daniel Stone05a5ac22017-04-04 17:54:25 +01001059 gbm_surface_release_buffer(fb->gbm_surface, fb->bo);
Daniel Stonefc175a72017-04-04 17:54:22 +01001060 break;
1061 default:
1062 assert(NULL);
1063 break;
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02001064 }
1065}
1066
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001067/**
Daniel Stonebc15f682016-11-14 16:57:01 +00001068 * Allocate a new, empty, plane state.
1069 */
1070static struct drm_plane_state *
1071drm_plane_state_alloc(struct drm_output_state *state_output,
1072 struct drm_plane *plane)
1073{
1074 struct drm_plane_state *state = zalloc(sizeof(*state));
1075
1076 assert(state);
1077 state->output_state = state_output;
1078 state->plane = plane;
1079
1080 /* Here we only add the plane state to the desired link, and not
1081 * set the member. Having an output pointer set means that the
1082 * plane will be displayed on the output; this won't be the case
1083 * when we go to disable a plane. In this case, it must be part of
1084 * the commit (and thus the output state), but the member must be
1085 * NULL, as it will not be on any output when the state takes
1086 * effect.
1087 */
1088 if (state_output)
1089 wl_list_insert(&state_output->plane_list, &state->link);
1090 else
1091 wl_list_init(&state->link);
1092
1093 return state;
1094}
1095
1096/**
1097 * Free an existing plane state. As a special case, the state will not
1098 * normally be freed if it is the current state; see drm_plane_set_state.
1099 */
1100static void
1101drm_plane_state_free(struct drm_plane_state *state, bool force)
1102{
1103 if (!state)
1104 return;
1105
1106 wl_list_remove(&state->link);
1107 wl_list_init(&state->link);
1108 state->output_state = NULL;
1109
1110 if (force || state != state->plane->state_cur) {
1111 drm_fb_unref(state->fb);
1112 free(state);
1113 }
1114}
1115
1116/**
1117 * Duplicate an existing plane state into a new plane state, storing it within
1118 * the given output state. If the output state already contains a plane state
1119 * for the drm_plane referenced by 'src', that plane state is freed first.
1120 */
1121static struct drm_plane_state *
1122drm_plane_state_duplicate(struct drm_output_state *state_output,
1123 struct drm_plane_state *src)
1124{
1125 struct drm_plane_state *dst = malloc(sizeof(*dst));
1126 struct drm_plane_state *old, *tmp;
1127
1128 assert(src);
1129 assert(dst);
1130 *dst = *src;
1131 wl_list_init(&dst->link);
1132
1133 wl_list_for_each_safe(old, tmp, &state_output->plane_list, link) {
1134 /* Duplicating a plane state into the same output state, so
1135 * it can replace itself with an identical copy of itself,
1136 * makes no sense. */
1137 assert(old != src);
1138 if (old->plane == dst->plane)
1139 drm_plane_state_free(old, false);
1140 }
1141
1142 wl_list_insert(&state_output->plane_list, &dst->link);
1143 if (src->fb)
1144 dst->fb = drm_fb_ref(src->fb);
1145 dst->output_state = state_output;
1146 dst->complete = false;
1147
1148 return dst;
1149}
1150
1151/**
1152 * Remove a plane state from an output state; if the plane was previously
1153 * enabled, then replace it with a disabling state. This ensures that the
1154 * output state was untouched from it was before the plane state was
1155 * modified by the caller of this function.
1156 *
1157 * This is required as drm_output_state_get_plane may either allocate a
1158 * new plane state, in which case this function will just perform a matching
1159 * drm_plane_state_free, or it may instead repurpose an existing disabling
1160 * state (if the plane was previously active), in which case this function
1161 * will reset it.
1162 */
1163static void
1164drm_plane_state_put_back(struct drm_plane_state *state)
1165{
1166 struct drm_output_state *state_output;
1167 struct drm_plane *plane;
1168
1169 if (!state)
1170 return;
1171
1172 state_output = state->output_state;
1173 plane = state->plane;
1174 drm_plane_state_free(state, false);
1175
1176 /* Plane was previously disabled; no need to keep this temporary
1177 * state around. */
1178 if (!plane->state_cur->fb)
1179 return;
1180
1181 (void) drm_plane_state_alloc(state_output, plane);
1182}
1183
1184/**
Daniel Stone2ba17f42015-05-19 20:02:41 +01001185 * Return a plane state from a drm_output_state.
1186 */
1187static struct drm_plane_state *
1188drm_output_state_get_existing_plane(struct drm_output_state *state_output,
1189 struct drm_plane *plane)
1190{
1191 struct drm_plane_state *ps;
1192
1193 wl_list_for_each(ps, &state_output->plane_list, link) {
1194 if (ps->plane == plane)
1195 return ps;
1196 }
1197
1198 return NULL;
1199}
1200
1201/**
Daniel Stonebc15f682016-11-14 16:57:01 +00001202 * Return a plane state from a drm_output_state, either existing or
1203 * freshly allocated.
1204 */
1205static struct drm_plane_state *
1206drm_output_state_get_plane(struct drm_output_state *state_output,
1207 struct drm_plane *plane)
1208{
1209 struct drm_plane_state *ps;
1210
Daniel Stone2ba17f42015-05-19 20:02:41 +01001211 ps = drm_output_state_get_existing_plane(state_output, plane);
1212 if (ps)
1213 return ps;
Daniel Stonebc15f682016-11-14 16:57:01 +00001214
1215 return drm_plane_state_alloc(state_output, plane);
1216}
1217
1218/**
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001219 * Allocate a new, empty drm_output_state. This should not generally be used
1220 * in the repaint cycle; see drm_output_state_duplicate.
1221 */
1222static struct drm_output_state *
1223drm_output_state_alloc(struct drm_output *output,
1224 struct drm_pending_state *pending_state)
Daniel Stone90648872016-10-21 18:08:37 +01001225{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001226 struct drm_output_state *state = zalloc(sizeof(*state));
1227
1228 assert(state);
1229 state->output = output;
Daniel Stonea08512f2016-11-08 17:46:10 +00001230 state->dpms = WESTON_DPMS_OFF;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001231 state->pending_state = pending_state;
1232 if (pending_state)
1233 wl_list_insert(&pending_state->output_list, &state->link);
1234 else
1235 wl_list_init(&state->link);
1236
Daniel Stonebc15f682016-11-14 16:57:01 +00001237 wl_list_init(&state->plane_list);
1238
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001239 return state;
1240}
1241
1242/**
1243 * Duplicate an existing drm_output_state into a new one. This is generally
1244 * used during the repaint cycle, to capture the existing state of an output
1245 * and modify it to create a new state to be used.
1246 *
1247 * The mode determines whether the output will be reset to an a blank state,
1248 * or an exact mirror of the current state.
1249 */
1250static struct drm_output_state *
1251drm_output_state_duplicate(struct drm_output_state *src,
1252 struct drm_pending_state *pending_state,
1253 enum drm_output_state_duplicate_mode plane_mode)
1254{
1255 struct drm_output_state *dst = malloc(sizeof(*dst));
Daniel Stonebc15f682016-11-14 16:57:01 +00001256 struct drm_plane_state *ps;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001257
1258 assert(dst);
1259
1260 /* Copy the whole structure, then individually modify the
1261 * pending_state, as well as the list link into our pending
1262 * state. */
1263 *dst = *src;
1264
1265 dst->pending_state = pending_state;
1266 if (pending_state)
1267 wl_list_insert(&pending_state->output_list, &dst->link);
1268 else
1269 wl_list_init(&dst->link);
1270
Daniel Stonebc15f682016-11-14 16:57:01 +00001271 wl_list_init(&dst->plane_list);
1272
1273 wl_list_for_each(ps, &src->plane_list, link) {
1274 /* Don't carry planes which are now disabled; these should be
1275 * free for other outputs to reuse. */
1276 if (!ps->output)
1277 continue;
1278
1279 if (plane_mode == DRM_OUTPUT_STATE_CLEAR_PLANES)
1280 (void) drm_plane_state_alloc(dst, ps->plane);
1281 else
1282 (void) drm_plane_state_duplicate(dst, ps);
1283 }
1284
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001285 return dst;
1286}
1287
1288/**
1289 * Free an unused drm_output_state.
1290 */
1291static void
1292drm_output_state_free(struct drm_output_state *state)
1293{
Daniel Stonebc15f682016-11-14 16:57:01 +00001294 struct drm_plane_state *ps, *next;
1295
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001296 if (!state)
1297 return;
1298
Daniel Stonebc15f682016-11-14 16:57:01 +00001299 wl_list_for_each_safe(ps, next, &state->plane_list, link)
1300 drm_plane_state_free(ps, false);
1301
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001302 wl_list_remove(&state->link);
Daniel Stonebc15f682016-11-14 16:57:01 +00001303
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001304 free(state);
Daniel Stone90648872016-10-21 18:08:37 +01001305}
1306
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001307/**
Daniel Stonea08512f2016-11-08 17:46:10 +00001308 * Get output state to disable output
1309 *
1310 * Returns a pointer to an output_state object which can be used to disable
1311 * an output (e.g. DPMS off).
1312 *
1313 * @param pending_state The pending state object owning this update
1314 * @param output The output to disable
1315 * @returns A drm_output_state to disable the output
1316 */
1317static struct drm_output_state *
1318drm_output_get_disable_state(struct drm_pending_state *pending_state,
1319 struct drm_output *output)
1320{
1321 struct drm_output_state *output_state;
1322
1323 output_state = drm_output_state_duplicate(output->state_cur,
1324 pending_state,
1325 DRM_OUTPUT_STATE_CLEAR_PLANES);
1326 output_state->dpms = WESTON_DPMS_OFF;
1327
1328 return output_state;
1329}
1330
1331/**
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001332 * Allocate a new drm_pending_state
1333 *
1334 * Allocate a new, empty, 'pending state' structure to be used across a
1335 * repaint cycle or similar.
1336 *
1337 * @param backend DRM backend
1338 * @returns Newly-allocated pending state structure
1339 */
1340static struct drm_pending_state *
1341drm_pending_state_alloc(struct drm_backend *backend)
1342{
1343 struct drm_pending_state *ret;
1344
1345 ret = calloc(1, sizeof(*ret));
1346 if (!ret)
1347 return NULL;
1348
1349 ret->backend = backend;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001350 wl_list_init(&ret->output_list);
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001351
1352 return ret;
1353}
1354
1355/**
1356 * Free a drm_pending_state structure
1357 *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001358 * Frees a pending_state structure, as well as any output_states connected
1359 * to this pending state.
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001360 *
1361 * @param pending_state Pending state structure to free
1362 */
1363static void
1364drm_pending_state_free(struct drm_pending_state *pending_state)
1365{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001366 struct drm_output_state *output_state, *tmp;
1367
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001368 if (!pending_state)
1369 return;
1370
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001371 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
1372 link) {
1373 drm_output_state_free(output_state);
1374 }
1375
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001376 free(pending_state);
1377}
1378
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001379/**
1380 * Find an output state in a pending state
1381 *
1382 * Given a pending_state structure, find the output_state for a particular
1383 * output.
1384 *
1385 * @param pending_state Pending state structure to search
1386 * @param output Output to find state for
1387 * @returns Output state if present, or NULL if not
1388 */
1389static struct drm_output_state *
1390drm_pending_state_get_output(struct drm_pending_state *pending_state,
1391 struct drm_output *output)
1392{
1393 struct drm_output_state *output_state;
1394
1395 wl_list_for_each(output_state, &pending_state->output_list, link) {
1396 if (output_state->output == output)
1397 return output_state;
1398 }
1399
1400 return NULL;
1401}
1402
Daniel Stonea08512f2016-11-08 17:46:10 +00001403static int drm_pending_state_apply_sync(struct drm_pending_state *state);
1404
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001405/**
1406 * Mark a drm_output_state (the output's last state) as complete. This handles
1407 * any post-completion actions such as updating the repaint timer, disabling the
1408 * output, and finally freeing the state.
1409 */
1410static void
1411drm_output_update_complete(struct drm_output *output, uint32_t flags,
1412 unsigned int sec, unsigned int usec)
1413{
Daniel Stonea08512f2016-11-08 17:46:10 +00001414 struct drm_backend *b = to_drm_backend(output->base.compositor);
Daniel Stonebc15f682016-11-14 16:57:01 +00001415 struct drm_plane_state *ps;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001416 struct timespec ts;
1417
1418 /* Stop the pageflip timer instead of rearming it here */
1419 if (output->pageflip_timer)
1420 wl_event_source_timer_update(output->pageflip_timer, 0);
1421
Daniel Stonebc15f682016-11-14 16:57:01 +00001422 wl_list_for_each(ps, &output->state_cur->plane_list, link)
1423 ps->complete = true;
1424
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001425 drm_output_state_free(output->state_last);
1426 output->state_last = NULL;
1427
1428 if (output->destroy_pending) {
Daniel Stonea08512f2016-11-08 17:46:10 +00001429 output->destroy_pending = 0;
1430 output->disable_pending = 0;
1431 output->dpms_off_pending = 0;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001432 drm_output_destroy(&output->base);
1433 return;
1434 } else if (output->disable_pending) {
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001435 output->disable_pending = 0;
Daniel Stonea08512f2016-11-08 17:46:10 +00001436 output->dpms_off_pending = 0;
1437 weston_output_disable(&output->base);
1438 return;
1439 } else if (output->dpms_off_pending) {
1440 struct drm_pending_state *pending = drm_pending_state_alloc(b);
1441 output->dpms_off_pending = 0;
1442 drm_output_get_disable_state(pending, output);
1443 drm_pending_state_apply_sync(pending);
1444 return;
1445 } else if (output->state_cur->dpms == WESTON_DPMS_OFF &&
1446 output->base.repaint_status != REPAINT_AWAITING_COMPLETION) {
1447 /* DPMS can happen to us either in the middle of a repaint
1448 * cycle (when we have painted fresh content, only to throw it
1449 * away for DPMS off), or at any other random point. If the
1450 * latter is true, then we cannot go through finish_frame,
1451 * because the repaint machinery does not expect this. */
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001452 return;
1453 }
1454
1455 ts.tv_sec = sec;
1456 ts.tv_nsec = usec * 1000;
1457 weston_output_finish_frame(&output->base, &ts, flags);
1458
1459 /* We can't call this from frame_notify, because the output's
1460 * repaint needed flag is cleared just after that */
1461 if (output->recorder)
1462 weston_output_schedule_repaint(&output->base);
1463}
1464
1465/**
1466 * Mark an output state as current on the output, i.e. it has been
1467 * submitted to the kernel. The mode argument determines whether this
1468 * update will be applied synchronously (e.g. when calling drmModeSetCrtc),
1469 * or asynchronously (in which case we wait for events to complete).
1470 */
1471static void
1472drm_output_assign_state(struct drm_output_state *state,
1473 enum drm_state_apply_mode mode)
1474{
1475 struct drm_output *output = state->output;
Daniel Stone598ee9d2016-11-16 11:55:20 +00001476 struct drm_backend *b = to_drm_backend(output->base.compositor);
Daniel Stonebc15f682016-11-14 16:57:01 +00001477 struct drm_plane_state *plane_state;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001478
1479 assert(!output->state_last);
1480
1481 if (mode == DRM_STATE_APPLY_ASYNC)
1482 output->state_last = output->state_cur;
1483 else
1484 drm_output_state_free(output->state_cur);
1485
1486 wl_list_remove(&state->link);
1487 wl_list_init(&state->link);
1488 state->pending_state = NULL;
1489
1490 output->state_cur = state;
Daniel Stonebc15f682016-11-14 16:57:01 +00001491
Daniel Stone598ee9d2016-11-16 11:55:20 +00001492 if (b->atomic_modeset && mode == DRM_STATE_APPLY_ASYNC)
1493 output->atomic_complete_pending = 1;
1494
Daniel Stonebc15f682016-11-14 16:57:01 +00001495 /* Replace state_cur on each affected plane with the new state, being
1496 * careful to dispose of orphaned (but only orphaned) previous state.
1497 * If the previous state is not orphaned (still has an output_state
1498 * attached), it will be disposed of by freeing the output_state. */
1499 wl_list_for_each(plane_state, &state->plane_list, link) {
1500 struct drm_plane *plane = plane_state->plane;
1501
1502 if (plane->state_cur && !plane->state_cur->output_state)
1503 drm_plane_state_free(plane->state_cur, true);
1504 plane->state_cur = plane_state;
1505
1506 if (mode != DRM_STATE_APPLY_ASYNC) {
1507 plane_state->complete = true;
1508 continue;
1509 }
1510
Daniel Stone598ee9d2016-11-16 11:55:20 +00001511 if (b->atomic_modeset)
1512 continue;
1513
Daniel Stonebc15f682016-11-14 16:57:01 +00001514 if (plane->type == WDRM_PLANE_TYPE_OVERLAY)
1515 output->vblank_pending++;
Daniel Stonee2e80132018-01-16 15:37:33 +00001516 else if (plane->type == WDRM_PLANE_TYPE_PRIMARY)
1517 output->page_flip_pending = 1;
Daniel Stonebc15f682016-11-14 16:57:01 +00001518 }
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001519}
1520
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001521static int
1522drm_view_transform_supported(struct weston_view *ev)
1523{
1524 return !ev->transform.enabled ||
1525 (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
1526}
1527
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001528static uint32_t
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +02001529drm_output_check_scanout_format(struct drm_output *output,
1530 struct weston_surface *es, struct gbm_bo *bo)
1531{
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +02001532 uint32_t format;
1533 pixman_region32_t r;
1534
1535 format = gbm_bo_get_format(bo);
1536
Kristian Høgsberg3f97b342013-10-16 16:08:57 -07001537 if (format == GBM_FORMAT_ARGB8888) {
1538 /* We can scanout an ARGB buffer if the surface's
1539 * opaque region covers the whole output, but we have
1540 * to use XRGB as the KMS format code. */
Kristian Høgsberg1be87e32014-01-17 14:22:41 -08001541 pixman_region32_init_rect(&r, 0, 0,
1542 output->base.width,
1543 output->base.height);
1544 pixman_region32_subtract(&r, &r, &es->opaque);
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +02001545
1546 if (!pixman_region32_not_empty(&r))
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001547 format = GBM_FORMAT_XRGB8888;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +02001548
1549 pixman_region32_fini(&r);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001550 }
Kristian Høgsberg3f97b342013-10-16 16:08:57 -07001551
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001552 if (output->gbm_format == format)
Kristian Høgsberg3f97b342013-10-16 16:08:57 -07001553 return format;
1554
1555 return 0;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +02001556}
1557
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001558static struct weston_plane *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001559drm_output_prepare_scanout_view(struct drm_output_state *output_state,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001560 struct weston_view *ev)
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001561{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001562 struct drm_output *output = output_state->output;
Armin Krezović545dba62016-08-05 15:54:18 +02001563 struct drm_backend *b = to_drm_backend(output->base.compositor);
Daniel Stonee2e80132018-01-16 15:37:33 +00001564 struct drm_plane *scanout_plane = output->scanout_plane;
1565 struct drm_plane_state *state;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001566 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001567 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001568 struct gbm_bo *bo;
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001569 uint32_t format;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001570
Daniel Stone90648872016-10-21 18:08:37 +01001571 /* Don't import buffers which span multiple outputs. */
1572 if (ev->output_mask != (1u << output->base.id))
1573 return NULL;
1574
Daniel Stone296d7a92016-10-21 18:05:37 +01001575 /* We use GBM to import buffers. */
1576 if (b->gbm == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001577 return NULL;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001578
Daniel Stone296d7a92016-10-21 18:05:37 +01001579 if (buffer == NULL)
1580 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +01001581 if (wl_shm_buffer_get(buffer->resource))
1582 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001583
1584 /* Make sure our view is exactly compatible with the output. */
1585 if (ev->geometry.x != output->base.x ||
1586 ev->geometry.y != output->base.y)
1587 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +01001588 if (buffer->width != output->base.current_mode->width ||
1589 buffer->height != output->base.current_mode->height)
1590 return NULL;
1591
Daniel Stone296d7a92016-10-21 18:05:37 +01001592 if (ev->transform.enabled)
1593 return NULL;
Pekka Paalanen5580f222015-02-17 16:33:18 +02001594 if (ev->geometry.scissor_enabled)
1595 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001596 if (viewport->buffer.transform != output->base.transform)
1597 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +01001598 if (viewport->buffer.scale != output->base.current_scale)
1599 return NULL;
1600 if (!drm_view_transform_supported(ev))
1601 return NULL;
1602
1603 if (ev->alpha != 1.0f)
1604 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001605
Daniel Stonee2e80132018-01-16 15:37:33 +00001606 state = drm_output_state_get_plane(output_state, scanout_plane);
1607 if (state->fb) {
1608 /* If there is already a framebuffer on the scanout plane,
1609 * a client view has already been placed on the scanout
1610 * view. In that case, do not free or put back the state,
1611 * but just leave it in place and quietly exit. */
1612 return NULL;
1613 }
1614
Giulio Camuffo954f1832014-10-11 18:27:30 +03001615 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
Kristian Høgsberg63996462013-09-03 22:27:08 -07001616 buffer->resource, GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001617
Rob Bradford9b101872012-09-14 23:25:41 +01001618 /* Unable to use the buffer for scanout */
1619 if (!bo)
1620 return NULL;
1621
Jason Ekstranda7af7042013-10-12 22:38:11 -05001622 format = drm_output_check_scanout_format(output, ev->surface, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001623 if (format == 0) {
Daniel Stonee2e80132018-01-16 15:37:33 +00001624 drm_plane_state_put_back(state);
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +03001625 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001626 return NULL;
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +03001627 }
1628
Daniel Stonee2e80132018-01-16 15:37:33 +00001629 state->fb = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT);
1630 if (!state->fb) {
1631 drm_plane_state_put_back(state);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001632 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001633 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001634 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001635
Daniel Stonee2e80132018-01-16 15:37:33 +00001636 drm_fb_set_buffer(state->fb, buffer);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001637
Daniel Stonee2e80132018-01-16 15:37:33 +00001638 state->output = output;
1639
1640 state->src_x = 0;
1641 state->src_y = 0;
1642 state->src_w = state->fb->width << 16;
1643 state->src_h = state->fb->height << 16;
1644
1645 state->dest_x = 0;
1646 state->dest_y = 0;
1647 state->dest_w = output->base.current_mode->width;
1648 state->dest_h = output->base.current_mode->height;
1649
1650 return &scanout_plane->base;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001651}
1652
Daniel Stone95d48a22017-04-04 17:54:30 +01001653static struct drm_fb *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001654drm_output_render_gl(struct drm_output_state *state, pixman_region32_t *damage)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001655{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001656 struct drm_output *output = state->output;
Armin Krezović545dba62016-08-05 15:54:18 +02001657 struct drm_backend *b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001658 struct gbm_bo *bo;
Daniel Stone95d48a22017-04-04 17:54:30 +01001659 struct drm_fb *ret;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001660
Giulio Camuffo954f1832014-10-11 18:27:30 +03001661 output->base.compositor->renderer->repaint_output(&output->base,
1662 damage);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001663
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001664 bo = gbm_surface_lock_front_buffer(output->gbm_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001665 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +02001666 weston_log("failed to lock front buffer: %m\n");
Daniel Stone95d48a22017-04-04 17:54:30 +01001667 return NULL;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001668 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001669
Daniel Stone95d48a22017-04-04 17:54:30 +01001670 ret = drm_fb_get_from_bo(bo, b, output->gbm_format, BUFFER_GBM_SURFACE);
1671 if (!ret) {
Martin Minarik6d118362012-06-07 18:01:59 +02001672 weston_log("failed to get drm_fb for bo\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001673 gbm_surface_release_buffer(output->gbm_surface, bo);
Daniel Stone95d48a22017-04-04 17:54:30 +01001674 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001675 }
Daniel Stone95d48a22017-04-04 17:54:30 +01001676 ret->gbm_surface = output->gbm_surface;
1677
1678 return ret;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001679}
1680
Daniel Stone95d48a22017-04-04 17:54:30 +01001681static struct drm_fb *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001682drm_output_render_pixman(struct drm_output_state *state,
1683 pixman_region32_t *damage)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001684{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001685 struct drm_output *output = state->output;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001686 struct weston_compositor *ec = output->base.compositor;
1687 pixman_region32_t total_damage, previous_damage;
1688
1689 pixman_region32_init(&total_damage);
1690 pixman_region32_init(&previous_damage);
1691
1692 pixman_region32_copy(&previous_damage, damage);
1693
1694 pixman_region32_union(&total_damage, damage, &output->previous_damage);
1695 pixman_region32_copy(&output->previous_damage, &previous_damage);
1696
1697 output->current_image ^= 1;
1698
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001699 pixman_renderer_output_set_buffer(&output->base,
1700 output->image[output->current_image]);
1701
1702 ec->renderer->repaint_output(&output->base, &total_damage);
1703
1704 pixman_region32_fini(&total_damage);
1705 pixman_region32_fini(&previous_damage);
Daniel Stone95d48a22017-04-04 17:54:30 +01001706
1707 return drm_fb_ref(output->dumb[output->current_image]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001708}
1709
1710static void
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001711drm_output_render(struct drm_output_state *state, pixman_region32_t *damage)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001712{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001713 struct drm_output *output = state->output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001714 struct weston_compositor *c = output->base.compositor;
Daniel Stonee2e80132018-01-16 15:37:33 +00001715 struct drm_plane_state *scanout_state;
Daniel Stonee95169b2016-11-14 17:46:59 +00001716 struct drm_plane *scanout_plane = output->scanout_plane;
Armin Krezović545dba62016-08-05 15:54:18 +02001717 struct drm_backend *b = to_drm_backend(c);
Daniel Stone95d48a22017-04-04 17:54:30 +01001718 struct drm_fb *fb;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001719
Daniel Stone4e84f7d2017-04-04 17:54:29 +01001720 /* If we already have a client buffer promoted to scanout, then we don't
1721 * want to render. */
Daniel Stonee2e80132018-01-16 15:37:33 +00001722 scanout_state = drm_output_state_get_plane(state,
1723 output->scanout_plane);
1724 if (scanout_state->fb)
Daniel Stone4e84f7d2017-04-04 17:54:29 +01001725 return;
1726
Daniel Stonee95169b2016-11-14 17:46:59 +00001727 if (!pixman_region32_not_empty(damage) &&
1728 scanout_plane->state_cur->fb &&
1729 (scanout_plane->state_cur->fb->type == BUFFER_GBM_SURFACE ||
1730 scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB) &&
1731 scanout_plane->state_cur->fb->width ==
1732 output->base.current_mode->width &&
1733 scanout_plane->state_cur->fb->height ==
1734 output->base.current_mode->height) {
1735 fb = drm_fb_ref(scanout_plane->state_cur->fb);
1736 } else if (b->use_pixman) {
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001737 fb = drm_output_render_pixman(state, damage);
Daniel Stonee95169b2016-11-14 17:46:59 +00001738 } else {
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001739 fb = drm_output_render_gl(state, damage);
Daniel Stonee95169b2016-11-14 17:46:59 +00001740 }
Daniel Stone95d48a22017-04-04 17:54:30 +01001741
Daniel Stonee2e80132018-01-16 15:37:33 +00001742 if (!fb) {
1743 drm_plane_state_put_back(scanout_state);
Daniel Stone95d48a22017-04-04 17:54:30 +01001744 return;
Daniel Stonee2e80132018-01-16 15:37:33 +00001745 }
1746
1747 scanout_state->fb = fb;
1748 scanout_state->output = output;
1749
1750 scanout_state->src_x = 0;
1751 scanout_state->src_y = 0;
1752 scanout_state->src_w = output->base.current_mode->width << 16;
1753 scanout_state->src_h = output->base.current_mode->height << 16;
1754
1755 scanout_state->dest_x = 0;
1756 scanout_state->dest_y = 0;
1757 scanout_state->dest_w = scanout_state->src_w >> 16;
1758 scanout_state->dest_h = scanout_state->src_h >> 16;
1759
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001760
Giulio Camuffo954f1832014-10-11 18:27:30 +03001761 pixman_region32_subtract(&c->primary_plane.damage,
1762 &c->primary_plane.damage, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001763}
1764
1765static void
Richard Hughese7299962013-05-01 21:52:12 +01001766drm_output_set_gamma(struct weston_output *output_base,
1767 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
1768{
1769 int rc;
Armin Krezović545dba62016-08-05 15:54:18 +02001770 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001771 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +02001772 to_drm_backend(output->base.compositor);
Richard Hughese7299962013-05-01 21:52:12 +01001773
1774 /* check */
1775 if (output_base->gamma_size != size)
1776 return;
Richard Hughese7299962013-05-01 21:52:12 +01001777
Giulio Camuffo954f1832014-10-11 18:27:30 +03001778 rc = drmModeCrtcSetGamma(backend->drm.fd,
Richard Hughese7299962013-05-01 21:52:12 +01001779 output->crtc_id,
1780 size, r, g, b);
1781 if (rc)
1782 weston_log("set gamma failed: %m\n");
1783}
1784
Bryce Harringtonada4f072015-06-30 13:25:46 -07001785/* Determine the type of vblank synchronization to use for the output.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001786 *
Bryce Harringtonada4f072015-06-30 13:25:46 -07001787 * The pipe parameter indicates which CRTC is in use. Knowing this, we
1788 * can determine which vblank sequence type to use for it. Traditional
1789 * cards had only two CRTCs, with CRTC 0 using no special flags, and
1790 * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
1791 * parameter indicates this.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001792 *
Bryce Harringtonada4f072015-06-30 13:25:46 -07001793 * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
1794 * 0-31. If this is non-zero it indicates we're dealing with a
1795 * multi-gpu situation and we need to calculate the vblank sync
1796 * using DRM_BLANK_HIGH_CRTC_MASK.
1797 */
Pekka Paalanenc8a1ff02015-07-02 15:06:08 +03001798static unsigned int
1799drm_waitvblank_pipe(struct drm_output *output)
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +02001800{
1801 if (output->pipe > 1)
1802 return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
1803 DRM_VBLANK_HIGH_CRTC_MASK;
1804 else if (output->pipe > 0)
1805 return DRM_VBLANK_SECONDARY;
1806 else
1807 return 0;
1808}
1809
David Herrmann1edf44c2013-10-22 17:11:26 +02001810static int
Daniel Stone598ee9d2016-11-16 11:55:20 +00001811drm_output_apply_state_legacy(struct drm_output_state *state)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001812{
Daniel Stonea08512f2016-11-08 17:46:10 +00001813 struct drm_output *output = state->output;
1814 struct drm_backend *backend = to_drm_backend(output->base.compositor);
Daniel Stonee2e80132018-01-16 15:37:33 +00001815 struct drm_plane *scanout_plane = output->scanout_plane;
Daniel Stonea08512f2016-11-08 17:46:10 +00001816 struct drm_property_info *dpms_prop =
1817 &output->props_conn[WDRM_CONNECTOR_DPMS];
Daniel Stonee2e80132018-01-16 15:37:33 +00001818 struct drm_plane_state *scanout_state;
Daniel Stonebc15f682016-11-14 16:57:01 +00001819 struct drm_plane_state *ps;
Daniel Stone085d2b92015-05-21 00:00:57 +01001820 struct drm_plane *p;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001821 struct drm_mode *mode;
Daniel Stonea08512f2016-11-08 17:46:10 +00001822 struct timespec now;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001823 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001824
Derek Foreman2cd87fe2017-04-13 13:48:48 -05001825 /* If disable_planes is set then assign_planes() wasn't
1826 * called for this render, so we could still have a stale
1827 * cursor plane set up.
1828 */
1829 if (output->base.disable_planes) {
1830 output->cursor_view = NULL;
Daniel Stone2ba17f42015-05-19 20:02:41 +01001831 output->cursor_plane->base.x = INT32_MIN;
1832 output->cursor_plane->base.y = INT32_MIN;
Derek Foreman2cd87fe2017-04-13 13:48:48 -05001833 }
1834
Daniel Stonea08512f2016-11-08 17:46:10 +00001835 if (state->dpms != WESTON_DPMS_ON) {
1836 wl_list_for_each(ps, &state->plane_list, link) {
1837 p = ps->plane;
1838 assert(ps->fb == NULL);
1839 assert(ps->output == NULL);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001840
Daniel Stonea08512f2016-11-08 17:46:10 +00001841 if (p->type != WDRM_PLANE_TYPE_OVERLAY)
1842 continue;
1843
1844 ret = drmModeSetPlane(backend->drm.fd, p->plane_id,
1845 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1846 if (ret)
1847 weston_log("drmModeSetPlane failed disable: %m\n");
1848 }
1849
1850 if (output->cursor_plane) {
1851 ret = drmModeSetCursor(backend->drm.fd, output->crtc_id,
1852 0, 0, 0);
1853 if (ret)
1854 weston_log("drmModeSetCursor failed disable: %m\n");
1855 }
1856
1857 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id, 0, 0, 0,
1858 &output->connector_id, 0, NULL);
1859 if (ret)
1860 weston_log("drmModeSetCrtc failed disabling: %m\n");
1861
1862 drm_output_assign_state(state, DRM_STATE_APPLY_SYNC);
1863 weston_compositor_read_presentation_clock(output->base.compositor, &now);
1864 drm_output_update_complete(output,
1865 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION,
1866 now.tv_sec, now.tv_nsec / 1000);
1867
1868 return 0;
1869 }
1870
1871 scanout_state =
1872 drm_output_state_get_existing_plane(state, scanout_plane);
Daniel Stone087ddf02017-02-14 17:51:30 +00001873
Daniel Stonee2e80132018-01-16 15:37:33 +00001874 /* The legacy SetCrtc API doesn't allow us to do scaling, and the
1875 * legacy PageFlip API doesn't allow us to do clipping either. */
1876 assert(scanout_state->src_x == 0);
1877 assert(scanout_state->src_y == 0);
1878 assert(scanout_state->src_w ==
1879 (unsigned) (output->base.current_mode->width << 16));
1880 assert(scanout_state->src_h ==
1881 (unsigned) (output->base.current_mode->height << 16));
1882 assert(scanout_state->dest_x == 0);
1883 assert(scanout_state->dest_y == 0);
1884 assert(scanout_state->dest_w == scanout_state->src_w >> 16);
1885 assert(scanout_state->dest_h == scanout_state->src_h >> 16);
1886
Daniel Stonecb04cc42016-11-16 11:51:27 +00001887 mode = to_drm_mode(output->base.current_mode);
Daniel Stone6020f472018-02-05 15:46:20 +00001888 if (backend->state_invalid || !scanout_plane->state_cur->fb ||
Daniel Stonee2e80132018-01-16 15:37:33 +00001889 scanout_plane->state_cur->fb->stride != scanout_state->fb->stride) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001890 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Daniel Stonee2e80132018-01-16 15:37:33 +00001891 scanout_state->fb->fb_id,
1892 0, 0,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001893 &output->connector_id, 1,
1894 &mode->mode_info);
1895 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +02001896 weston_log("set mode failed: %m\n");
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001897 goto err;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001898 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001899 }
1900
Giulio Camuffo954f1832014-10-11 18:27:30 +03001901 if (drmModePageFlip(backend->drm.fd, output->crtc_id,
Daniel Stonee2e80132018-01-16 15:37:33 +00001902 scanout_state->fb->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -05001903 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001904 weston_log("queueing pageflip failed: %m\n");
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001905 goto err;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -05001906 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001907
Daniel Stone205c0a02017-04-04 17:54:33 +01001908 assert(!output->page_flip_pending);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001909
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001910 if (output->pageflip_timer)
1911 wl_event_source_timer_update(output->pageflip_timer,
1912 backend->pageflip_timeout);
1913
Daniel Stone2ba17f42015-05-19 20:02:41 +01001914 drm_output_set_cursor(state);
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001915
Jesse Barnes58ef3792012-02-23 09:45:49 -05001916 /*
1917 * Now, update all the sprite surfaces
1918 */
Daniel Stonebc15f682016-11-14 16:57:01 +00001919 wl_list_for_each(ps, &state->plane_list, link) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001920 uint32_t flags = 0, fb_id = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001921 drmVBlank vbl = {
1922 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
1923 .request.sequence = 1,
1924 };
1925
Daniel Stonebc15f682016-11-14 16:57:01 +00001926 p = ps->plane;
Daniel Stone085d2b92015-05-21 00:00:57 +01001927 if (p->type != WDRM_PLANE_TYPE_OVERLAY)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001928 continue;
1929
Daniel Stonebc15f682016-11-14 16:57:01 +00001930 assert(p->state_cur->complete);
1931 assert(!!p->state_cur->output == !!p->state_cur->fb);
1932 assert(!p->state_cur->output || p->state_cur->output == output);
1933 assert(!ps->complete);
1934 assert(!ps->output || ps->output == output);
1935 assert(!!ps->output == !!ps->fb);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001936
Daniel Stonebc15f682016-11-14 16:57:01 +00001937 if (ps->fb && !backend->sprites_hidden)
1938 fb_id = ps->fb->fb_id;
Daniel Stone085d2b92015-05-21 00:00:57 +01001939
1940 ret = drmModeSetPlane(backend->drm.fd, p->plane_id,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001941 output->crtc_id, fb_id, flags,
Daniel Stonebc15f682016-11-14 16:57:01 +00001942 ps->dest_x, ps->dest_y,
1943 ps->dest_w, ps->dest_h,
1944 ps->src_x, ps->src_y,
1945 ps->src_w, ps->src_h);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001946 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +02001947 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05001948 ret, strerror(errno));
1949
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +02001950 vbl.request.type |= drm_waitvblank_pipe(output);
Rob Clark5ca1a472012-08-08 20:27:37 -05001951
Jesse Barnes58ef3792012-02-23 09:45:49 -05001952 /*
1953 * Queue a vblank signal so we know when the surface
1954 * becomes active on the display or has been replaced.
1955 */
Daniel Stonebc15f682016-11-14 16:57:01 +00001956 vbl.request.signal = (unsigned long) ps;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001957 ret = drmWaitVBlank(backend->drm.fd, &vbl);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001958 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +02001959 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05001960 ret, strerror(errno));
1961 }
1962 }
1963
Daniel Stonea08512f2016-11-08 17:46:10 +00001964 if (dpms_prop->prop_id && state->dpms != output->state_cur->dpms) {
1965 ret = drmModeConnectorSetProperty(backend->drm.fd,
1966 output->connector_id,
1967 dpms_prop->prop_id,
1968 state->dpms);
1969 if (ret) {
1970 weston_log("DRM: DPMS: failed property set for %s\n",
1971 output->base.name);
1972 }
1973 }
1974
1975 drm_output_assign_state(state, DRM_STATE_APPLY_ASYNC);
1976
David Herrmann1edf44c2013-10-22 17:11:26 +02001977 return 0;
1978
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001979err:
Kristian Høgsbergb3955b02014-01-23 16:25:06 -08001980 output->cursor_view = NULL;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001981 drm_output_state_free(state);
Daniel Stonea08512f2016-11-08 17:46:10 +00001982 return -1;
1983}
David Herrmann1edf44c2013-10-22 17:11:26 +02001984
Daniel Stone598ee9d2016-11-16 11:55:20 +00001985#ifdef HAVE_DRM_ATOMIC
1986static int
1987crtc_add_prop(drmModeAtomicReq *req, struct drm_output *output,
1988 enum wdrm_crtc_property prop, uint64_t val)
1989{
1990 struct drm_property_info *info = &output->props_crtc[prop];
1991 int ret;
1992
1993 if (info->prop_id == 0)
1994 return -1;
1995
1996 ret = drmModeAtomicAddProperty(req, output->crtc_id, info->prop_id,
1997 val);
1998 return (ret <= 0) ? -1 : 0;
1999}
2000
2001static int
2002connector_add_prop(drmModeAtomicReq *req, struct drm_output *output,
2003 enum wdrm_connector_property prop, uint64_t val)
2004{
2005 struct drm_property_info *info = &output->props_conn[prop];
2006 int ret;
2007
2008 if (info->prop_id == 0)
2009 return -1;
2010
2011 ret = drmModeAtomicAddProperty(req, output->connector_id,
2012 info->prop_id, val);
2013 return (ret <= 0) ? -1 : 0;
2014}
2015
2016static int
2017plane_add_prop(drmModeAtomicReq *req, struct drm_plane *plane,
2018 enum wdrm_plane_property prop, uint64_t val)
2019{
2020 struct drm_property_info *info = &plane->props[prop];
2021 int ret;
2022
2023 if (info->prop_id == 0)
2024 return -1;
2025
2026 ret = drmModeAtomicAddProperty(req, plane->plane_id, info->prop_id,
2027 val);
2028 return (ret <= 0) ? -1 : 0;
2029}
2030
2031static int
2032drm_mode_ensure_blob(struct drm_backend *backend, struct drm_mode *mode)
2033{
2034 int ret;
2035
2036 if (mode->blob_id)
2037 return 0;
2038
2039 ret = drmModeCreatePropertyBlob(backend->drm.fd,
2040 &mode->mode_info,
2041 sizeof(mode->mode_info),
2042 &mode->blob_id);
2043 if (ret != 0)
2044 weston_log("failed to create mode property blob: %m\n");
2045
2046 return ret;
2047}
2048
2049static int
2050drm_output_apply_state_atomic(struct drm_output_state *state,
2051 drmModeAtomicReq *req,
2052 uint32_t *flags)
2053{
2054 struct drm_output *output = state->output;
2055 struct drm_backend *backend = to_drm_backend(output->base.compositor);
2056 struct drm_plane_state *plane_state;
2057 struct drm_mode *current_mode = to_drm_mode(output->base.current_mode);
2058 int ret = 0;
2059
2060 if (state->dpms != output->state_cur->dpms)
2061 *flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
2062
2063 if (state->dpms == WESTON_DPMS_ON) {
2064 ret = drm_mode_ensure_blob(backend, current_mode);
2065 if (ret != 0)
2066 return ret;
2067
2068 ret |= crtc_add_prop(req, output, WDRM_CRTC_MODE_ID,
2069 current_mode->blob_id);
2070 ret |= crtc_add_prop(req, output, WDRM_CRTC_ACTIVE, 1);
2071 ret |= connector_add_prop(req, output, WDRM_CONNECTOR_CRTC_ID,
2072 output->crtc_id);
2073 } else {
2074 ret |= crtc_add_prop(req, output, WDRM_CRTC_MODE_ID, 0);
2075 ret |= crtc_add_prop(req, output, WDRM_CRTC_ACTIVE, 0);
2076 ret |= connector_add_prop(req, output, WDRM_CONNECTOR_CRTC_ID,
2077 0);
2078 }
2079
2080 if (ret != 0) {
2081 weston_log("couldn't set atomic CRTC/connector state\n");
2082 return ret;
2083 }
2084
2085 wl_list_for_each(plane_state, &state->plane_list, link) {
2086 struct drm_plane *plane = plane_state->plane;
2087
2088 ret |= plane_add_prop(req, plane, WDRM_PLANE_FB_ID,
2089 plane_state->fb ? plane_state->fb->fb_id : 0);
2090 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_ID,
2091 plane_state->fb ? output->crtc_id : 0);
2092 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_X,
2093 plane_state->src_x);
2094 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_Y,
2095 plane_state->src_y);
2096 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_W,
2097 plane_state->src_w);
2098 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_H,
2099 plane_state->src_h);
2100 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_X,
2101 plane_state->dest_x);
2102 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_Y,
2103 plane_state->dest_y);
2104 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_W,
2105 plane_state->dest_w);
2106 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_H,
2107 plane_state->dest_h);
2108
2109 if (ret != 0) {
2110 weston_log("couldn't set plane state\n");
2111 return ret;
2112 }
2113 }
2114
2115 return 0;
2116}
2117
2118/**
2119 * Helper function used only by drm_pending_state_apply, with the same
2120 * guarantees and constraints as that function.
2121 */
2122static int
2123drm_pending_state_apply_atomic(struct drm_pending_state *pending_state,
2124 enum drm_state_apply_mode mode)
2125{
2126 struct drm_backend *b = pending_state->backend;
2127 struct drm_output_state *output_state, *tmp;
2128 struct drm_plane *plane;
2129 drmModeAtomicReq *req = drmModeAtomicAlloc();
2130 uint32_t flags = 0;
2131 int ret = 0;
2132
2133 if (!req)
2134 return -1;
2135
2136 if (b->state_invalid) {
2137 uint32_t *unused;
2138 int err;
2139
2140 /* If we need to reset all our state (e.g. because we've
2141 * just started, or just been VT-switched in), explicitly
2142 * disable all the CRTCs and connectors we aren't using. */
2143 wl_array_for_each(unused, &b->unused_connectors) {
2144 struct drm_property_info infos[WDRM_CONNECTOR__COUNT];
2145 struct drm_property_info *info;
2146 drmModeObjectProperties *props;
2147
2148 memset(infos, 0, sizeof(infos));
2149
2150 props = drmModeObjectGetProperties(b->drm.fd,
2151 *unused,
2152 DRM_MODE_OBJECT_CONNECTOR);
2153 if (!props) {
2154 ret = -1;
2155 continue;
2156 }
2157
2158 drm_property_info_populate(b, connector_props, infos,
2159 WDRM_CONNECTOR__COUNT,
2160 props);
2161 drmModeFreeObjectProperties(props);
2162
2163 info = &infos[WDRM_CONNECTOR_CRTC_ID];
2164 err = drmModeAtomicAddProperty(req, *unused,
2165 info->prop_id, 0);
2166 if (err <= 0)
2167 ret = -1;
2168
2169 info = &infos[WDRM_CONNECTOR_DPMS];
2170 if (info->prop_id > 0)
2171 err = drmModeAtomicAddProperty(req, *unused,
2172 info->prop_id,
2173 DRM_MODE_DPMS_OFF);
2174 if (err <= 0)
2175 ret = -1;
2176
2177 drm_property_info_free(infos, WDRM_CONNECTOR__COUNT);
2178 }
2179
2180 wl_array_for_each(unused, &b->unused_crtcs) {
2181 struct drm_property_info infos[WDRM_CRTC__COUNT];
2182 struct drm_property_info *info;
2183 drmModeObjectProperties *props;
2184 uint64_t active;
2185
2186 memset(infos, 0, sizeof(infos));
2187
2188 /* We can't emit a disable on a CRTC that's already
2189 * off, as the kernel will refuse to generate an event
2190 * for an off->off state and fail the commit.
2191 */
2192 props = drmModeObjectGetProperties(b->drm.fd,
2193 *unused,
2194 DRM_MODE_OBJECT_CRTC);
2195 if (!props) {
2196 ret = -1;
2197 continue;
2198 }
2199
2200 drm_property_info_populate(b, crtc_props, infos,
2201 WDRM_CRTC__COUNT,
2202 props);
2203
2204 info = &infos[WDRM_CRTC_ACTIVE];
2205 active = drm_property_get_value(info, props, 0);
2206 drmModeFreeObjectProperties(props);
2207 if (active == 0) {
2208 drm_property_info_free(infos, WDRM_CRTC__COUNT);
2209 continue;
2210 }
2211
2212 err = drmModeAtomicAddProperty(req, *unused,
2213 info->prop_id, 0);
2214 if (err <= 0)
2215 ret = -1;
2216
2217 info = &infos[WDRM_CRTC_MODE_ID];
2218 err = drmModeAtomicAddProperty(req, *unused,
2219 info->prop_id, 0);
2220 if (err <= 0)
2221 ret = -1;
2222
2223 drm_property_info_free(infos, WDRM_CRTC__COUNT);
2224 }
2225
2226 /* Disable all the planes; planes which are being used will
2227 * override this state in the output-state application. */
2228 wl_list_for_each(plane, &b->plane_list, link) {
2229 plane_add_prop(req, plane, WDRM_PLANE_CRTC_ID, 0);
2230 plane_add_prop(req, plane, WDRM_PLANE_FB_ID, 0);
2231 }
2232
2233 flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
2234 }
2235
2236 wl_list_for_each(output_state, &pending_state->output_list, link) {
2237 if (mode == DRM_STATE_APPLY_SYNC)
2238 assert(output_state->dpms == WESTON_DPMS_OFF);
2239 ret |= drm_output_apply_state_atomic(output_state, req, &flags);
2240 }
2241
2242 if (ret != 0) {
2243 weston_log("atomic: couldn't compile atomic state\n");
2244 goto out;
2245 }
2246
2247 switch (mode) {
2248 case DRM_STATE_APPLY_SYNC:
2249 break;
2250 case DRM_STATE_APPLY_ASYNC:
2251 flags |= DRM_MODE_PAGE_FLIP_EVENT | DRM_MODE_ATOMIC_NONBLOCK;
2252 break;
2253 }
2254
2255 ret = drmModeAtomicCommit(b->drm.fd, req, flags, b);
2256 if (ret != 0) {
2257 weston_log("atomic: couldn't commit new state: %m\n");
2258 goto out;
2259 }
2260
2261 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
2262 link)
2263 drm_output_assign_state(output_state, mode);
2264
2265 b->state_invalid = false;
2266
2267 assert(wl_list_empty(&pending_state->output_list));
2268
2269out:
2270 drmModeAtomicFree(req);
2271 drm_pending_state_free(pending_state);
2272 return ret;
2273}
2274#endif
2275
Daniel Stonea08512f2016-11-08 17:46:10 +00002276/**
2277 * Applies all of a pending_state asynchronously: the primary entry point for
2278 * applying KMS state to a device. Updates the state for all outputs in the
2279 * pending_state, as well as disabling any unclaimed outputs.
2280 *
2281 * Unconditionally takes ownership of pending_state, and clears state_invalid.
2282 */
2283static int
2284drm_pending_state_apply(struct drm_pending_state *pending_state)
2285{
2286 struct drm_backend *b = pending_state->backend;
2287 struct drm_output_state *output_state, *tmp;
2288 uint32_t *unused;
2289
Daniel Stone598ee9d2016-11-16 11:55:20 +00002290#ifdef HAVE_DRM_ATOMIC
2291 if (b->atomic_modeset)
2292 return drm_pending_state_apply_atomic(pending_state,
2293 DRM_STATE_APPLY_ASYNC);
2294#endif
2295
Daniel Stonea08512f2016-11-08 17:46:10 +00002296 if (b->state_invalid) {
2297 /* If we need to reset all our state (e.g. because we've
2298 * just started, or just been VT-switched in), explicitly
2299 * disable all the CRTCs we aren't using. This also disables
2300 * all connectors on these CRTCs, so we don't need to do that
2301 * separately with the pre-atomic API. */
2302 wl_array_for_each(unused, &b->unused_crtcs)
2303 drmModeSetCrtc(b->drm.fd, *unused, 0, 0, 0, NULL, 0,
2304 NULL);
2305 }
2306
2307 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
2308 link) {
2309 struct drm_output *output = output_state->output;
2310 int ret;
2311
Daniel Stone598ee9d2016-11-16 11:55:20 +00002312 ret = drm_output_apply_state_legacy(output_state);
Daniel Stonea08512f2016-11-08 17:46:10 +00002313 if (ret != 0) {
2314 weston_log("Couldn't apply state for output %s\n",
2315 output->base.name);
2316 }
2317 }
2318
2319 b->state_invalid = false;
2320
2321 assert(wl_list_empty(&pending_state->output_list));
2322
2323 drm_pending_state_free(pending_state);
2324
2325 return 0;
2326}
2327
2328/**
2329 * The synchronous version of drm_pending_state_apply. May only be used to
2330 * disable outputs. Does so synchronously: the request is guaranteed to have
2331 * completed on return, and the output will not be touched afterwards.
2332 *
2333 * Unconditionally takes ownership of pending_state, and clears state_invalid.
2334 */
2335static int
2336drm_pending_state_apply_sync(struct drm_pending_state *pending_state)
2337{
2338 struct drm_backend *b = pending_state->backend;
2339 struct drm_output_state *output_state, *tmp;
2340 uint32_t *unused;
2341
Daniel Stone598ee9d2016-11-16 11:55:20 +00002342#ifdef HAVE_DRM_ATOMIC
2343 if (b->atomic_modeset)
2344 return drm_pending_state_apply_atomic(pending_state,
2345 DRM_STATE_APPLY_SYNC);
2346#endif
2347
Daniel Stonea08512f2016-11-08 17:46:10 +00002348 if (b->state_invalid) {
2349 /* If we need to reset all our state (e.g. because we've
2350 * just started, or just been VT-switched in), explicitly
2351 * disable all the CRTCs we aren't using. This also disables
2352 * all connectors on these CRTCs, so we don't need to do that
2353 * separately with the pre-atomic API. */
2354 wl_array_for_each(unused, &b->unused_crtcs)
2355 drmModeSetCrtc(b->drm.fd, *unused, 0, 0, 0, NULL, 0,
2356 NULL);
2357 }
2358
2359 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
2360 link) {
2361 int ret;
2362
2363 assert(output_state->dpms == WESTON_DPMS_OFF);
Daniel Stone598ee9d2016-11-16 11:55:20 +00002364 ret = drm_output_apply_state_legacy(output_state);
Daniel Stonea08512f2016-11-08 17:46:10 +00002365 if (ret != 0) {
2366 weston_log("Couldn't apply state for output %s\n",
2367 output_state->output->base.name);
2368 }
2369 }
2370
2371 b->state_invalid = false;
2372
2373 assert(wl_list_empty(&pending_state->output_list));
2374
2375 drm_pending_state_free(pending_state);
2376
2377 return 0;
2378}
2379
2380static int
2381drm_output_repaint(struct weston_output *output_base,
2382 pixman_region32_t *damage,
2383 void *repaint_data)
2384{
2385 struct drm_pending_state *pending_state = repaint_data;
2386 struct drm_output *output = to_drm_output(output_base);
Daniel Stonea08512f2016-11-08 17:46:10 +00002387 struct drm_output_state *state = NULL;
2388 struct drm_plane_state *scanout_state;
2389
2390 if (output->disable_pending || output->destroy_pending)
2391 goto err;
2392
2393 assert(!output->state_last);
2394
2395 /* If planes have been disabled in the core, we might not have
2396 * hit assign_planes at all, so might not have valid output state
2397 * here. */
2398 state = drm_pending_state_get_output(pending_state, output);
2399 if (!state)
2400 state = drm_output_state_duplicate(output->state_cur,
2401 pending_state,
2402 DRM_OUTPUT_STATE_CLEAR_PLANES);
2403 state->dpms = WESTON_DPMS_ON;
2404
2405 drm_output_render(state, damage);
2406 scanout_state = drm_output_state_get_plane(state,
2407 output->scanout_plane);
2408 if (!scanout_state || !scanout_state->fb)
2409 goto err;
2410
Daniel Stonea08512f2016-11-08 17:46:10 +00002411 return 0;
2412
2413err:
2414 drm_output_state_free(state);
David Herrmann1edf44c2013-10-22 17:11:26 +02002415 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002416}
2417
2418static void
Jonas Ådahle5a12252013-04-05 23:07:11 +02002419drm_output_start_repaint_loop(struct weston_output *output_base)
2420{
Armin Krezović545dba62016-08-05 15:54:18 +02002421 struct drm_output *output = to_drm_output(output_base);
Daniel Stone8747f952016-11-29 20:17:32 +00002422 struct drm_pending_state *pending_state;
Daniel Stonee2e80132018-01-16 15:37:33 +00002423 struct drm_plane *scanout_plane = output->scanout_plane;
Armin Krezović545dba62016-08-05 15:54:18 +02002424 struct drm_backend *backend =
2425 to_drm_backend(output_base->compositor);
Mario Kleinerf507ec32015-06-21 21:25:14 +02002426 struct timespec ts, tnow;
2427 struct timespec vbl2now;
2428 int64_t refresh_nsec;
2429 int ret;
2430 drmVBlank vbl = {
2431 .request.type = DRM_VBLANK_RELATIVE,
2432 .request.sequence = 0,
2433 .request.signal = 0,
2434 };
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03002435
Armin Krezović08368132016-09-30 14:11:05 +02002436 if (output->disable_pending || output->destroy_pending)
Xiong Zhangabd5d472013-10-11 14:43:07 +08002437 return;
2438
Daniel Stonee2e80132018-01-16 15:37:33 +00002439 if (!output->scanout_plane->state_cur->fb) {
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03002440 /* We can't page flip if there's no mode set */
David Herrmann3c688c52013-10-22 17:11:25 +02002441 goto finish_frame;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03002442 }
2443
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03002444 /* Need to smash all state in from scratch; current timings might not
2445 * be what we want, page flip might not work, etc.
2446 */
Daniel Stone6020f472018-02-05 15:46:20 +00002447 if (backend->state_invalid)
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03002448 goto finish_frame;
2449
Daniel Stonee2e80132018-01-16 15:37:33 +00002450 assert(scanout_plane->state_cur->output == output);
2451
Mario Kleinerf507ec32015-06-21 21:25:14 +02002452 /* Try to get current msc and timestamp via instant query */
2453 vbl.request.type |= drm_waitvblank_pipe(output);
2454 ret = drmWaitVBlank(backend->drm.fd, &vbl);
2455
2456 /* Error ret or zero timestamp means failure to get valid timestamp */
2457 if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
2458 ts.tv_sec = vbl.reply.tval_sec;
2459 ts.tv_nsec = vbl.reply.tval_usec * 1000;
2460
2461 /* Valid timestamp for most recent vblank - not stale?
2462 * Stale ts could happen on Linux 3.17+, so make sure it
2463 * is not older than 1 refresh duration since now.
2464 */
2465 weston_compositor_read_presentation_clock(backend->compositor,
2466 &tnow);
2467 timespec_sub(&vbl2now, &tnow, &ts);
2468 refresh_nsec =
2469 millihz_to_nsec(output->base.current_mode->refresh);
2470 if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
2471 drm_output_update_msc(output, vbl.reply.sequence);
2472 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002473 WP_PRESENTATION_FEEDBACK_INVALID);
Mario Kleinerf507ec32015-06-21 21:25:14 +02002474 return;
2475 }
2476 }
2477
2478 /* Immediate query didn't provide valid timestamp.
2479 * Use pageflip fallback.
2480 */
Jonas Ådahle5a12252013-04-05 23:07:11 +02002481
Daniel Stone205c0a02017-04-04 17:54:33 +01002482 assert(!output->page_flip_pending);
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002483 assert(!output->state_last);
2484
2485 pending_state = drm_pending_state_alloc(backend);
Daniel Stone8747f952016-11-29 20:17:32 +00002486 drm_output_state_duplicate(output->state_cur, pending_state,
2487 DRM_OUTPUT_STATE_PRESERVE_PLANES);
Daniel Stone205c0a02017-04-04 17:54:33 +01002488
Daniel Stone8747f952016-11-29 20:17:32 +00002489 ret = drm_pending_state_apply(pending_state);
2490 if (ret != 0) {
2491 weston_log("applying repaint-start state failed: %m\n");
David Herrmann3c688c52013-10-22 17:11:25 +02002492 goto finish_frame;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002493 }
David Herrmann3c688c52013-10-22 17:11:25 +02002494
2495 return;
2496
2497finish_frame:
2498 /* if we cannot page-flip, immediately finish frame */
Daniel Stone3615ce12017-03-01 11:34:05 +00002499 weston_output_finish_frame(output_base, NULL,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002500 WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002501}
2502
2503static void
Pekka Paalanen641307c2014-09-23 22:08:47 -04002504drm_output_update_msc(struct drm_output *output, unsigned int seq)
2505{
2506 uint64_t msc_hi = output->base.msc >> 32;
2507
2508 if (seq < (output->base.msc & 0xffffffff))
2509 msc_hi++;
2510
2511 output->base.msc = (msc_hi << 32) + seq;
2512}
2513
2514static void
Jesse Barnes58ef3792012-02-23 09:45:49 -05002515vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
2516 void *data)
2517{
Daniel Stonebc15f682016-11-14 16:57:01 +00002518 struct drm_plane_state *ps = (struct drm_plane_state *) data;
2519 struct drm_output_state *os = ps->output_state;
2520 struct drm_output *output = os->output;
Daniel Stone598ee9d2016-11-16 11:55:20 +00002521 struct drm_backend *b = to_drm_backend(output->base.compositor);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002522 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
2523 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03002524
Daniel Stone598ee9d2016-11-16 11:55:20 +00002525 assert(!b->atomic_modeset);
2526
Pekka Paalanen641307c2014-09-23 22:08:47 -04002527 drm_output_update_msc(output, frame);
Daniel Stone65d87d02017-04-04 17:54:32 +01002528 output->vblank_pending--;
2529 assert(output->vblank_pending >= 0);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002530
Daniel Stonebc15f682016-11-14 16:57:01 +00002531 assert(ps->fb);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03002532
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002533 if (output->page_flip_pending || output->vblank_pending)
2534 return;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00002535
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002536 drm_output_update_complete(output, flags, sec, usec);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002537}
2538
2539static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002540page_flip_handler(int fd, unsigned int frame,
2541 unsigned int sec, unsigned int usec, void *data)
2542{
Armin Krezović545dba62016-08-05 15:54:18 +02002543 struct drm_output *output = data;
Daniel Stone598ee9d2016-11-16 11:55:20 +00002544 struct drm_backend *b = to_drm_backend(output->base.compositor);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002545 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
2546 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
2547 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002548
Pekka Paalanen641307c2014-09-23 22:08:47 -04002549 drm_output_update_msc(output, frame);
2550
Daniel Stone598ee9d2016-11-16 11:55:20 +00002551 assert(!b->atomic_modeset);
Daniel Stone205c0a02017-04-04 17:54:33 +01002552 assert(output->page_flip_pending);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002553 output->page_flip_pending = 0;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002554
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002555 if (output->vblank_pending)
2556 return;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00002557
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002558 drm_output_update_complete(output, flags, sec, usec);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02002559}
2560
Daniel Stoneeedf84c2017-02-10 18:06:04 +00002561/**
2562 * Begin a new repaint cycle
2563 *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002564 * Called by the core compositor at the beginning of a repaint cycle. Creates
2565 * a new pending_state structure to own any output state created by individual
2566 * output repaint functions until the repaint is flushed or cancelled.
Daniel Stoneeedf84c2017-02-10 18:06:04 +00002567 */
2568static void *
2569drm_repaint_begin(struct weston_compositor *compositor)
2570{
2571 struct drm_backend *b = to_drm_backend(compositor);
2572 struct drm_pending_state *ret;
2573
2574 ret = drm_pending_state_alloc(b);
2575 b->repaint_data = ret;
2576
2577 return ret;
2578}
2579
2580/**
2581 * Flush a repaint set
2582 *
2583 * Called by the core compositor when a repaint cycle has been completed
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002584 * and should be flushed. Frees the pending state, transitioning ownership
2585 * of the output state from the pending state, to the update itself. When
2586 * the update completes (see drm_output_update_complete), the output
2587 * state will be freed.
Daniel Stoneeedf84c2017-02-10 18:06:04 +00002588 */
2589static void
2590drm_repaint_flush(struct weston_compositor *compositor, void *repaint_data)
2591{
2592 struct drm_backend *b = to_drm_backend(compositor);
2593 struct drm_pending_state *pending_state = repaint_data;
Daniel Stone6020f472018-02-05 15:46:20 +00002594
Daniel Stonea08512f2016-11-08 17:46:10 +00002595 drm_pending_state_apply(pending_state);
Daniel Stoneeedf84c2017-02-10 18:06:04 +00002596 b->repaint_data = NULL;
2597}
2598
2599/**
2600 * Cancel a repaint set
2601 *
2602 * Called by the core compositor when a repaint has finished, so the data
2603 * held across the repaint cycle should be discarded.
2604 */
2605static void
2606drm_repaint_cancel(struct weston_compositor *compositor, void *repaint_data)
2607{
2608 struct drm_backend *b = to_drm_backend(compositor);
2609 struct drm_pending_state *pending_state = repaint_data;
2610
2611 drm_pending_state_free(pending_state);
2612 b->repaint_data = NULL;
2613}
2614
Daniel Stone598ee9d2016-11-16 11:55:20 +00002615#ifdef HAVE_DRM_ATOMIC
2616static void
2617atomic_flip_handler(int fd, unsigned int frame, unsigned int sec,
2618 unsigned int usec, unsigned int crtc_id, void *data)
2619{
2620 struct drm_backend *b = data;
2621 struct drm_output *output = drm_output_find_by_crtc(b, crtc_id);
2622 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
2623 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
2624 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
2625
2626 /* During the initial modeset, we can disable CRTCs which we don't
2627 * actually handle during normal operation; this will give us events
2628 * for unknown outputs. Ignore them. */
2629 if (!output || !output->base.enabled)
2630 return;
2631
2632 drm_output_update_msc(output, frame);
2633
2634 assert(b->atomic_modeset);
2635 assert(output->atomic_complete_pending);
2636 output->atomic_complete_pending = 0;
2637
2638 drm_output_update_complete(output, flags, sec, usec);
2639}
2640#endif
2641
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05002642static uint32_t
Daniel Stone08d4edf2017-04-04 17:54:34 +01002643drm_output_check_plane_format(struct drm_plane *p,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002644 struct weston_view *ev, struct gbm_bo *bo)
Jesse Barnes58ef3792012-02-23 09:45:49 -05002645{
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05002646 uint32_t i, format;
2647
2648 format = gbm_bo_get_format(bo);
2649
2650 if (format == GBM_FORMAT_ARGB8888) {
2651 pixman_region32_t r;
2652
Kristian Høgsberg63093a32013-03-01 14:29:16 -05002653 pixman_region32_init_rect(&r, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002654 ev->surface->width,
2655 ev->surface->height);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002656 pixman_region32_subtract(&r, &r, &ev->surface->opaque);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05002657
2658 if (!pixman_region32_not_empty(&r))
2659 format = GBM_FORMAT_XRGB8888;
2660
2661 pixman_region32_fini(&r);
2662 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05002663
Daniel Stone08d4edf2017-04-04 17:54:34 +01002664 for (i = 0; i < p->count_formats; i++)
2665 if (p->formats[i] == format)
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05002666 return format;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002667
2668 return 0;
2669}
2670
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002671static struct weston_plane *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002672drm_output_prepare_overlay_view(struct drm_output_state *output_state,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002673 struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -05002674{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002675 struct drm_output *output = output_state->output;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02002676 struct weston_compositor *ec = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02002677 struct drm_backend *b = to_drm_backend(ec);
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002678 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002679 struct wl_resource *buffer_resource;
Daniel Stone08d4edf2017-04-04 17:54:34 +01002680 struct drm_plane *p;
Daniel Stonebc15f682016-11-14 16:57:01 +00002681 struct drm_plane_state *state = NULL;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002682 struct linux_dmabuf_buffer *dmabuf;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002683 struct gbm_bo *bo;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002684 pixman_region32_t dest_rect, src_rect;
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02002685 pixman_box32_t *box, tbox;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002686 uint32_t format;
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04002687 wl_fixed_t sx1, sy1, sx2, sy2;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002688
Giulio Camuffo954f1832014-10-11 18:27:30 +03002689 if (b->sprites_are_broken)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002690 return NULL;
Kristian Høgsberg65bec242012-03-05 19:57:35 -05002691
Daniel Stone296d7a92016-10-21 18:05:37 +01002692 /* Don't import buffers which span multiple outputs. */
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02002693 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002694 return NULL;
Ander Conselvan de Oliveirad450b192012-06-26 17:09:12 +03002695
Daniel Stone296d7a92016-10-21 18:05:37 +01002696 /* We can only import GBM buffers. */
2697 if (b->gbm == NULL)
2698 return NULL;
2699
Jason Ekstranda7af7042013-10-12 22:38:11 -05002700 if (ev->surface->buffer_ref.buffer == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002701 return NULL;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002702 buffer_resource = ev->surface->buffer_ref.buffer->resource;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002703 if (wl_shm_buffer_get(buffer_resource))
Rob Clark702ffae2012-08-09 14:18:27 -05002704 return NULL;
2705
Daniel Stone296d7a92016-10-21 18:05:37 +01002706 if (viewport->buffer.transform != output->base.transform)
2707 return NULL;
2708 if (viewport->buffer.scale != output->base.current_scale)
2709 return NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002710 if (!drm_view_transform_supported(ev))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002711 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002712
Daniel Stone296d7a92016-10-21 18:05:37 +01002713 if (ev->alpha != 1.0f)
2714 return NULL;
2715
Daniel Stone085d2b92015-05-21 00:00:57 +01002716 wl_list_for_each(p, &b->plane_list, link) {
2717 if (p->type != WDRM_PLANE_TYPE_OVERLAY)
2718 continue;
2719
Daniel Stone5ff289a2017-10-07 12:59:02 +01002720 if (!drm_plane_is_available(p, output))
Daniel Stonebc15f682016-11-14 16:57:01 +00002721 continue;
2722
2723 state = drm_output_state_get_plane(output_state, p);
2724 if (state->fb) {
2725 state = NULL;
2726 continue;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002727 }
Daniel Stonebc15f682016-11-14 16:57:01 +00002728
2729 break;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002730 }
2731
2732 /* No sprites available */
Daniel Stonebc15f682016-11-14 16:57:01 +00002733 if (!state)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002734 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002735
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002736 if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) {
Bryce Harringtona3582072015-08-14 12:23:13 -07002737#ifdef HAVE_GBM_FD_IMPORT
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002738 /* XXX: TODO:
2739 *
2740 * Use AddFB2 directly, do not go via GBM.
2741 * Add support for multiplanar formats.
2742 * Both require refactoring in the DRM-backend to
2743 * support a mix of gbm_bos and drmfbs.
2744 */
2745 struct gbm_import_fd_data gbm_dmabuf = {
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00002746 .fd = dmabuf->attributes.fd[0],
2747 .width = dmabuf->attributes.width,
2748 .height = dmabuf->attributes.height,
2749 .stride = dmabuf->attributes.stride[0],
2750 .format = dmabuf->attributes.format
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002751 };
2752
Micah Fedkec8890122017-02-01 15:28:23 -05002753 /* XXX: TODO:
2754 *
2755 * Currently the buffer is rejected if any dmabuf attribute
2756 * flag is set. This keeps us from passing an inverted /
2757 * interlaced / bottom-first buffer (or any other type that may
2758 * be added in the future) through to an overlay. Ultimately,
2759 * these types of buffers should be handled through buffer
2760 * transforms and not as spot-checks requiring specific
2761 * knowledge. */
2762 if (dmabuf->attributes.n_planes != 1 ||
2763 dmabuf->attributes.offset[0] != 0 ||
2764 dmabuf->attributes.flags)
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002765 return NULL;
2766
2767 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf,
2768 GBM_BO_USE_SCANOUT);
Bryce Harringtona3582072015-08-14 12:23:13 -07002769#else
Daniel Stonebc15f682016-11-14 16:57:01 +00002770 goto err;
Bryce Harringtona3582072015-08-14 12:23:13 -07002771#endif
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002772 } else {
2773 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
2774 buffer_resource, GBM_BO_USE_SCANOUT);
2775 }
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04002776 if (!bo)
Daniel Stonebc15f682016-11-14 16:57:01 +00002777 goto err;
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04002778
Daniel Stone08d4edf2017-04-04 17:54:34 +01002779 format = drm_output_check_plane_format(p, ev, bo);
Daniel Stonebc15f682016-11-14 16:57:01 +00002780 if (format == 0)
2781 goto err;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002782
Daniel Stonebc15f682016-11-14 16:57:01 +00002783 state->fb = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT);
2784 if (!state->fb)
2785 goto err;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02002786
Daniel Stonebc15f682016-11-14 16:57:01 +00002787 drm_fb_set_buffer(state->fb, ev->surface->buffer_ref.buffer);
2788
2789 state->output = output;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002790
Jason Ekstranda7af7042013-10-12 22:38:11 -05002791 box = pixman_region32_extents(&ev->transform.boundingbox);
Daniel Stone08d4edf2017-04-04 17:54:34 +01002792 p->base.x = box->x1;
2793 p->base.y = box->y1;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002794
Jesse Barnes58ef3792012-02-23 09:45:49 -05002795 /*
2796 * Calculate the source & dest rects properly based on actual
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002797 * position (note the caller has called weston_view_update_transform()
Jesse Barnes58ef3792012-02-23 09:45:49 -05002798 * for us already).
2799 */
2800 pixman_region32_init(&dest_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002801 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02002802 &output->base.region);
2803 pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002804 box = pixman_region32_extents(&dest_rect);
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02002805 tbox = weston_transformed_rect(output->base.width,
2806 output->base.height,
2807 output->base.transform,
2808 output->base.current_scale,
Alexander Larssond9a7bb72013-05-22 14:41:39 +02002809 *box);
Daniel Stonebc15f682016-11-14 16:57:01 +00002810 state->dest_x = tbox.x1;
2811 state->dest_y = tbox.y1;
2812 state->dest_w = tbox.x2 - tbox.x1;
2813 state->dest_h = tbox.y2 - tbox.y1;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002814 pixman_region32_fini(&dest_rect);
2815
2816 pixman_region32_init(&src_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002817 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02002818 &output->base.region);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002819 box = pixman_region32_extents(&src_rect);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04002820
Jason Ekstranda7af7042013-10-12 22:38:11 -05002821 weston_view_from_global_fixed(ev,
2822 wl_fixed_from_int(box->x1),
2823 wl_fixed_from_int(box->y1),
2824 &sx1, &sy1);
2825 weston_view_from_global_fixed(ev,
2826 wl_fixed_from_int(box->x2),
2827 wl_fixed_from_int(box->y2),
2828 &sx2, &sy2);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04002829
2830 if (sx1 < 0)
2831 sx1 = 0;
2832 if (sy1 < 0)
2833 sy1 = 0;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002834 if (sx2 > wl_fixed_from_int(ev->surface->width))
2835 sx2 = wl_fixed_from_int(ev->surface->width);
2836 if (sy2 > wl_fixed_from_int(ev->surface->height))
2837 sy2 = wl_fixed_from_int(ev->surface->height);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04002838
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02002839 tbox.x1 = sx1;
2840 tbox.y1 = sy1;
2841 tbox.x2 = sx2;
2842 tbox.y2 = sy2;
2843
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002844 tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width),
2845 wl_fixed_from_int(ev->surface->height),
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002846 viewport->buffer.transform,
2847 viewport->buffer.scale,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002848 tbox);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02002849
Daniel Stonebc15f682016-11-14 16:57:01 +00002850 state->src_x = tbox.x1 << 8;
2851 state->src_y = tbox.y1 << 8;
2852 state->src_w = (tbox.x2 - tbox.x1) << 8;
2853 state->src_h = (tbox.y2 - tbox.y1) << 8;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002854 pixman_region32_fini(&src_rect);
2855
Daniel Stone08d4edf2017-04-04 17:54:34 +01002856 return &p->base;
Daniel Stonebc15f682016-11-14 16:57:01 +00002857
2858err:
2859 drm_plane_state_put_back(state);
2860 if (bo)
2861 gbm_bo_destroy(bo);
2862 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002863}
2864
Pekka Paalanend0ead482014-06-16 12:05:40 +03002865/**
2866 * Update the image for the current cursor surface
2867 *
2868 * @param b DRM backend structure
2869 * @param bo GBM buffer object to write into
2870 * @param ev View to use for cursor image
2871 */
2872static void
2873cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo,
2874 struct weston_view *ev)
2875{
2876 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
2877 uint32_t buf[b->cursor_width * b->cursor_height];
2878 int32_t stride;
2879 uint8_t *s;
2880 int i;
2881
2882 assert(buffer && buffer->shm_buffer);
2883 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
2884 assert(ev->surface->width <= b->cursor_width);
2885 assert(ev->surface->height <= b->cursor_height);
2886
2887 memset(buf, 0, sizeof buf);
2888 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
2889 s = wl_shm_buffer_get_data(buffer->shm_buffer);
2890
2891 wl_shm_buffer_begin_access(buffer->shm_buffer);
2892 for (i = 0; i < ev->surface->height; i++)
2893 memcpy(buf + i * b->cursor_width,
2894 s + i * stride,
2895 ev->surface->width * 4);
2896 wl_shm_buffer_end_access(buffer->shm_buffer);
2897
2898 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
2899 weston_log("failed update cursor: %m\n");
2900}
2901
Daniel Stone2ba17f42015-05-19 20:02:41 +01002902static struct weston_plane *
2903drm_output_prepare_cursor_view(struct drm_output_state *output_state,
2904 struct weston_view *ev)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04002905{
Daniel Stone2ba17f42015-05-19 20:02:41 +01002906 struct drm_output *output = output_state->output;
Armin Krezović545dba62016-08-05 15:54:18 +02002907 struct drm_backend *b = to_drm_backend(output->base.compositor);
Daniel Stone2ba17f42015-05-19 20:02:41 +01002908 struct drm_plane *plane = output->cursor_plane;
2909 struct drm_plane_state *plane_state;
2910 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
2911 struct wl_shm_buffer *shmbuf;
2912 bool needs_update = false;
Derek Foremanbe428b32015-11-24 11:39:38 -06002913 float x, y;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05002914
Daniel Stone2ba17f42015-05-19 20:02:41 +01002915 if (!plane)
2916 return NULL;
2917
2918 if (b->cursors_are_broken)
2919 return NULL;
2920
2921 if (!plane->state_cur->complete)
2922 return NULL;
2923
2924 if (plane->state_cur->output && plane->state_cur->output != output)
2925 return NULL;
2926
2927 /* Don't import buffers which span multiple outputs. */
2928 if (ev->output_mask != (1u << output->base.id))
2929 return NULL;
2930
2931 /* We use GBM to import SHM buffers. */
2932 if (b->gbm == NULL)
2933 return NULL;
2934
2935 if (ev->surface->buffer_ref.buffer == NULL)
2936 return NULL;
2937 shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource);
2938 if (!shmbuf)
2939 return NULL;
2940 if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888)
2941 return NULL;
2942
2943 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
2944 return NULL;
2945 if (ev->transform.enabled &&
2946 (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE))
2947 return NULL;
2948 if (viewport->buffer.scale != output->base.current_scale)
2949 return NULL;
2950 if (ev->geometry.scissor_enabled)
2951 return NULL;
2952
2953 if (ev->surface->width > b->cursor_width ||
2954 ev->surface->height > b->cursor_height)
2955 return NULL;
2956
2957 plane_state =
2958 drm_output_state_get_plane(output_state, output->cursor_plane);
2959
2960 if (plane_state && plane_state->fb)
2961 return NULL;
2962
2963 /* Since we're setting plane state up front, we need to work out
2964 * whether or not we need to upload a new cursor. We can't use the
2965 * plane damage, since the planes haven't actually been calculated
2966 * yet: instead try to figure it out directly. KMS cursor planes are
2967 * pretty unique here, in that they lie partway between a Weston plane
2968 * (direct scanout) and a renderer. */
2969 if (ev != output->cursor_view ||
2970 pixman_region32_not_empty(&ev->surface->damage)) {
2971 output->current_cursor++;
2972 output->current_cursor =
2973 output->current_cursor %
2974 ARRAY_LENGTH(output->gbm_cursor_fb);
2975 needs_update = true;
2976 }
2977
2978 output->cursor_view = ev;
2979 weston_view_to_global_float(ev, 0, 0, &x, &y);
2980 plane->base.x = x;
2981 plane->base.y = y;
2982
2983 plane_state->fb =
2984 drm_fb_ref(output->gbm_cursor_fb[output->current_cursor]);
2985 plane_state->output = output;
2986 plane_state->src_x = 0;
2987 plane_state->src_y = 0;
2988 plane_state->src_w = b->cursor_width << 16;
2989 plane_state->src_h = b->cursor_height << 16;
2990 plane_state->dest_x = (x - output->base.x) * output->base.current_scale;
2991 plane_state->dest_y = (y - output->base.y) * output->base.current_scale;
2992 plane_state->dest_w = b->cursor_width;
2993 plane_state->dest_h = b->cursor_height;
2994
2995 if (needs_update)
2996 cursor_bo_update(b, plane_state->fb->bo, ev);
2997
2998 return &plane->base;
2999}
3000
3001static void
3002drm_output_set_cursor(struct drm_output_state *output_state)
3003{
3004 struct drm_output *output = output_state->output;
3005 struct drm_backend *b = to_drm_backend(output->base.compositor);
3006 struct drm_plane *plane = output->cursor_plane;
3007 struct drm_plane_state *state;
3008 EGLint handle;
3009 struct gbm_bo *bo;
3010
3011 if (!plane)
3012 return;
3013
3014 state = drm_output_state_get_existing_plane(output_state, plane);
3015 if (!state)
3016 return;
3017
3018 if (!state->fb) {
3019 pixman_region32_fini(&plane->base.damage);
3020 pixman_region32_init(&plane->base.damage);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003021 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg5626d342012-08-03 11:50:05 -04003022 return;
3023 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05003024
Daniel Stone2ba17f42015-05-19 20:02:41 +01003025 assert(state->fb == output->gbm_cursor_fb[output->current_cursor]);
3026 assert(!plane->state_cur->output || plane->state_cur->output == output);
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05003027
Daniel Stone2ba17f42015-05-19 20:02:41 +01003028 if (plane->state_cur->fb != state->fb) {
3029 bo = state->fb->bo;
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04003030 handle = gbm_bo_get_handle(bo).s32;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003031 if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle,
Daniel Stone2ba17f42015-05-19 20:02:41 +01003032 b->cursor_width, b->cursor_height)) {
Pekka Paalanenae29da22012-08-06 14:57:05 +03003033 weston_log("failed to set cursor: %m\n");
Daniel Stone2ba17f42015-05-19 20:02:41 +01003034 goto err;
Rob Clarkab5b1e32012-08-09 13:24:45 -05003035 }
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003036 }
3037
Daniel Stone2ba17f42015-05-19 20:02:41 +01003038 pixman_region32_fini(&plane->base.damage);
3039 pixman_region32_init(&plane->base.damage);
Pekka Paalanen7eaed402015-11-27 14:20:58 +02003040
Daniel Stone2ba17f42015-05-19 20:02:41 +01003041 if (drmModeMoveCursor(b->drm.fd, output->crtc_id,
3042 state->dest_x, state->dest_y)) {
Daniel Stonea7cba1d2017-04-04 17:54:21 +01003043 weston_log("failed to move cursor: %m\n");
Daniel Stone2ba17f42015-05-19 20:02:41 +01003044 goto err;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003045 }
Daniel Stone2ba17f42015-05-19 20:02:41 +01003046
3047 return;
3048
3049err:
3050 b->cursors_are_broken = 1;
3051 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05003052}
3053
Jesse Barnes58ef3792012-02-23 09:45:49 -05003054static void
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003055drm_assign_planes(struct weston_output *output_base, void *repaint_data)
Jesse Barnes58ef3792012-02-23 09:45:49 -05003056{
Armin Krezović545dba62016-08-05 15:54:18 +02003057 struct drm_backend *b = to_drm_backend(output_base->compositor);
Daniel Stone7b2ddac2016-11-11 19:11:49 +00003058 struct drm_pending_state *pending_state = repaint_data;
Armin Krezović545dba62016-08-05 15:54:18 +02003059 struct drm_output *output = to_drm_output(output_base);
Daniel Stone7b2ddac2016-11-11 19:11:49 +00003060 struct drm_output_state *state;
Daniel Stone2ba17f42015-05-19 20:02:41 +01003061 struct drm_plane_state *plane_state;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003062 struct weston_view *ev, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -05003063 pixman_region32_t overlap, surface_overlap;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003064 struct weston_plane *primary, *next_plane;
Matt Hoosierdf573032017-08-24 09:24:20 -05003065 bool picked_scanout = false;
Jesse Barnes58ef3792012-02-23 09:45:49 -05003066
Daniel Stone7b2ddac2016-11-11 19:11:49 +00003067 assert(!output->state_last);
3068 state = drm_output_state_duplicate(output->state_cur,
3069 pending_state,
3070 DRM_OUTPUT_STATE_CLEAR_PLANES);
3071
Jesse Barnes58ef3792012-02-23 09:45:49 -05003072 /*
3073 * Find a surface for each sprite in the output using some heuristics:
3074 * 1) size
3075 * 2) frequency of update
3076 * 3) opacity (though some hw might support alpha blending)
3077 * 4) clipping (this can be fixed with color keys)
3078 *
3079 * The idea is to save on blitting since this should save power.
3080 * If we can get a large video surface on the sprite for example,
3081 * the main display surface may not need to update at all, and
3082 * the client buffer can be used directly for the sprite surface
3083 * as we do for flipping full screen surfaces.
3084 */
3085 pixman_region32_init(&overlap);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003086 primary = &output_base->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003087
Giulio Camuffo954f1832014-10-11 18:27:30 +03003088 wl_list_for_each_safe(ev, next, &output_base->compositor->view_list, link) {
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02003089 struct weston_surface *es = ev->surface;
3090
3091 /* Test whether this buffer can ever go into a plane:
3092 * non-shm, or small enough to be a cursor.
3093 *
3094 * Also, keep a reference when using the pixman renderer.
3095 * That makes it possible to do a seamless switch to the GL
3096 * renderer and since the pixman renderer keeps a reference
3097 * to the buffer anyway, there is no side effects.
Pekka Paalanenccfeae22012-12-04 15:58:14 +02003098 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003099 if (b->use_pixman ||
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02003100 (es->buffer_ref.buffer &&
3101 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
Derek Foreman87430472015-10-15 10:24:48 -05003102 (ev->surface->width <= b->cursor_width &&
3103 ev->surface->height <= b->cursor_height))))
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05003104 es->keep_buffer = true;
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02003105 else
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05003106 es->keep_buffer = false;
Pekka Paalanenccfeae22012-12-04 15:58:14 +02003107
Jesse Barnes58ef3792012-02-23 09:45:49 -05003108 pixman_region32_init(&surface_overlap);
3109 pixman_region32_intersect(&surface_overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003110 &ev->transform.boundingbox);
Jesse Barnes58ef3792012-02-23 09:45:49 -05003111
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003112 next_plane = NULL;
Matt Hoosierdf573032017-08-24 09:24:20 -05003113 if (pixman_region32_not_empty(&surface_overlap) || picked_scanout)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003114 next_plane = primary;
3115 if (next_plane == NULL)
Daniel Stone7b2ddac2016-11-11 19:11:49 +00003116 next_plane = drm_output_prepare_cursor_view(state, ev);
Matt Hoosierdf573032017-08-24 09:24:20 -05003117
3118 /* If a higher-stacked view already got assigned to scanout, it's incorrect to
3119 * assign a subsequent (lower-stacked) view to scanout.
3120 */
3121 if (next_plane == NULL) {
Daniel Stone7b2ddac2016-11-11 19:11:49 +00003122 next_plane = drm_output_prepare_scanout_view(state, ev);
Matt Hoosierdf573032017-08-24 09:24:20 -05003123 if (next_plane)
3124 picked_scanout = true;
3125 }
3126
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003127 if (next_plane == NULL)
Daniel Stone7b2ddac2016-11-11 19:11:49 +00003128 next_plane = drm_output_prepare_overlay_view(state, ev);
Matt Hoosierdf573032017-08-24 09:24:20 -05003129
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003130 if (next_plane == NULL)
3131 next_plane = primary;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02003132
Jason Ekstranda7af7042013-10-12 22:38:11 -05003133 weston_view_move_to_plane(ev, next_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02003134
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003135 if (next_plane == primary)
Jesse Barnes58ef3792012-02-23 09:45:49 -05003136 pixman_region32_union(&overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003137 &ev->transform.boundingbox);
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04003138
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02003139 if (next_plane == primary ||
Daniel Stone2ba17f42015-05-19 20:02:41 +01003140 (output->cursor_plane &&
3141 next_plane == &output->cursor_plane->base)) {
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02003142 /* cursor plane involves a copy */
3143 ev->psf_flags = 0;
3144 } else {
3145 /* All other planes are a direct scanout of a
3146 * single client buffer.
3147 */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02003148 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02003149 }
3150
Jesse Barnes58ef3792012-02-23 09:45:49 -05003151 pixman_region32_fini(&surface_overlap);
3152 }
3153 pixman_region32_fini(&overlap);
Daniel Stone2ba17f42015-05-19 20:02:41 +01003154
3155 /* We rely on output->cursor_view being both an accurate reflection of
3156 * the cursor plane's state, but also being maintained across repaints
3157 * to avoid unnecessary damage uploads, per the comment in
3158 * drm_output_prepare_cursor_view. In the event that we go from having
3159 * a cursor view to not having a cursor view, we need to clear it. */
3160 if (output->cursor_view) {
3161 plane_state =
3162 drm_output_state_get_existing_plane(state,
3163 output->cursor_plane);
3164 if (!plane_state || !plane_state->fb)
3165 output->cursor_view = NULL;
3166 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05003167}
3168
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003169/**
3170 * Find the closest-matching mode for a given target
3171 *
3172 * Given a target mode, find the most suitable mode amongst the output's
3173 * current mode list to use, preferring the current mode if possible, to
3174 * avoid an expensive mode switch.
3175 *
3176 * @param output DRM output
3177 * @param target_mode Mode to attempt to match
3178 * @returns Pointer to a mode from the output's mode list
3179 */
Alex Wub7b8bda2012-04-17 17:20:48 +08003180static struct drm_mode *
3181choose_mode (struct drm_output *output, struct weston_mode *target_mode)
3182{
3183 struct drm_mode *tmp_mode = NULL, *mode;
3184
Hardeningff39efa2013-09-18 23:56:35 +02003185 if (output->base.current_mode->width == target_mode->width &&
3186 output->base.current_mode->height == target_mode->height &&
3187 (output->base.current_mode->refresh == target_mode->refresh ||
Alex Wub7b8bda2012-04-17 17:20:48 +08003188 target_mode->refresh == 0))
Daniel Stonecb04cc42016-11-16 11:51:27 +00003189 return to_drm_mode(output->base.current_mode);
Alex Wub7b8bda2012-04-17 17:20:48 +08003190
3191 wl_list_for_each(mode, &output->base.mode_list, base.link) {
3192 if (mode->mode_info.hdisplay == target_mode->width &&
3193 mode->mode_info.vdisplay == target_mode->height) {
Mario Kleiner872797c2015-06-21 21:25:09 +02003194 if (mode->base.refresh == target_mode->refresh ||
3195 target_mode->refresh == 0) {
Alex Wub7b8bda2012-04-17 17:20:48 +08003196 return mode;
Daniel Stonef556ebe2015-05-21 08:28:58 +01003197 } else if (!tmp_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08003198 tmp_mode = mode;
3199 }
3200 }
3201
3202 return tmp_mode;
3203}
3204
3205static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003206drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00003207static void
3208drm_output_fini_egl(struct drm_output *output);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003209static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003210drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00003211static void
3212drm_output_fini_pixman(struct drm_output *output);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02003213
3214static int
Alex Wub7b8bda2012-04-17 17:20:48 +08003215drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
3216{
Daniel Stone02d487a2017-10-07 14:01:45 +01003217 struct drm_output *output = to_drm_output(output_base);
3218 struct drm_backend *b = to_drm_backend(output_base->compositor);
3219 struct drm_mode *drm_mode = choose_mode(output, mode);
Alex Wub7b8bda2012-04-17 17:20:48 +08003220
3221 if (!drm_mode) {
Daniel Stone02d487a2017-10-07 14:01:45 +01003222 weston_log("%s: invalid resolution %dx%d\n",
3223 output_base->name, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +08003224 return -1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02003225 }
3226
Hardeningff39efa2013-09-18 23:56:35 +02003227 if (&drm_mode->base == output->base.current_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08003228 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08003229
Hardeningff39efa2013-09-18 23:56:35 +02003230 output->base.current_mode->flags = 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08003231
Hardeningff39efa2013-09-18 23:56:35 +02003232 output->base.current_mode = &drm_mode->base;
3233 output->base.current_mode->flags =
Alex Wub7b8bda2012-04-17 17:20:48 +08003234 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
3235
Daniel Stonef30a18c2017-04-04 17:54:31 +01003236 /* XXX: This drops our current buffer too early, before we've started
3237 * displaying it. Ideally this should be much more atomic and
3238 * integrated with a full repaint cycle, rather than doing a
3239 * sledgehammer modeswitch first, and only later showing new
3240 * content.
3241 */
Daniel Stone6020f472018-02-05 15:46:20 +00003242 b->state_invalid = true;
Alex Wub7b8bda2012-04-17 17:20:48 +08003243
Giulio Camuffo954f1832014-10-11 18:27:30 +03003244 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003245 drm_output_fini_pixman(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003246 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003247 weston_log("failed to init output pixman state with "
3248 "new mode\n");
3249 return -1;
3250 }
3251 } else {
Daniel Stone3e661f72016-11-04 17:24:06 +00003252 drm_output_fini_egl(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003253 if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003254 weston_log("failed to init output egl state with "
3255 "new mode");
3256 return -1;
3257 }
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02003258 }
3259
Alex Wub7b8bda2012-04-17 17:20:48 +08003260 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08003261}
3262
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003263static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003264on_drm_input(int fd, uint32_t mask, void *data)
3265{
Daniel Stone598ee9d2016-11-16 11:55:20 +00003266#ifdef HAVE_DRM_ATOMIC
3267 struct drm_backend *b = data;
3268#endif
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003269 drmEventContext evctx;
3270
3271 memset(&evctx, 0, sizeof evctx);
Daniel Stone598ee9d2016-11-16 11:55:20 +00003272#ifndef HAVE_DRM_ATOMIC
Emil Velikov863e66b2017-04-04 18:07:34 +01003273 evctx.version = 2;
Daniel Stone598ee9d2016-11-16 11:55:20 +00003274#else
3275 evctx.version = 3;
3276 if (b->atomic_modeset)
3277 evctx.page_flip_handler2 = atomic_flip_handler;
3278 else
3279#endif
3280 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -05003281 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003282 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003283
3284 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003285}
3286
3287static int
Daniel Stoneefa504f2016-12-19 16:48:20 +00003288init_kms_caps(struct drm_backend *b)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003289{
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03003290 uint64_t cap;
Daniel Stoneefa504f2016-12-19 16:48:20 +00003291 int ret;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003292 clockid_t clk_id;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04003293
Daniel Stoneefa504f2016-12-19 16:48:20 +00003294 weston_log("using %s\n", b->drm.filename);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003295
Daniel Stoneefa504f2016-12-19 16:48:20 +00003296 ret = drmGetCap(b->drm.fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03003297 if (ret == 0 && cap == 1)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003298 clk_id = CLOCK_MONOTONIC;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03003299 else
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003300 clk_id = CLOCK_REALTIME;
3301
Giulio Camuffo954f1832014-10-11 18:27:30 +03003302 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003303 weston_log("Error: failed to set presentation clock %d.\n",
3304 clk_id);
3305 return -1;
3306 }
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +02003307
Daniel Stoneefa504f2016-12-19 16:48:20 +00003308 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_WIDTH, &cap);
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03003309 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03003310 b->cursor_width = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03003311 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03003312 b->cursor_width = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03003313
Daniel Stoneefa504f2016-12-19 16:48:20 +00003314 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_HEIGHT, &cap);
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03003315 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03003316 b->cursor_height = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03003317 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03003318 b->cursor_height = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03003319
Daniel Stonebe1090b2017-09-06 17:29:57 +01003320 if (!getenv("WESTON_DISABLE_UNIVERSAL_PLANES")) {
3321 ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
3322 b->universal_planes = (ret == 0);
3323 }
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01003324 weston_log("DRM: %s universal planes\n",
3325 b->universal_planes ? "supports" : "does not support");
3326
Pekka Paalanencd011a62016-11-15 22:07:49 +00003327#ifdef HAVE_DRM_ATOMIC
3328 if (b->universal_planes && !getenv("WESTON_DISABLE_ATOMIC")) {
Daniel Stone598ee9d2016-11-16 11:55:20 +00003329 ret = drmGetCap(b->drm.fd, DRM_CAP_CRTC_IN_VBLANK_EVENT, &cap);
3330 if (ret != 0)
3331 cap = 0;
Pekka Paalanencd011a62016-11-15 22:07:49 +00003332 ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_ATOMIC, 1);
Daniel Stone598ee9d2016-11-16 11:55:20 +00003333 b->atomic_modeset = ((ret == 0) && (cap == 1));
Pekka Paalanencd011a62016-11-15 22:07:49 +00003334 }
3335#endif
3336 weston_log("DRM: %s atomic modesetting\n",
3337 b->atomic_modeset ? "supports" : "does not support");
3338
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003339 return 0;
3340}
3341
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003342static struct gbm_device *
3343create_gbm_device(int fd)
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003344{
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003345 struct gbm_device *gbm;
Alexandru DAMIANbe0ac5b2013-10-02 17:51:05 +01003346
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003347 gl_renderer = weston_load_module("gl-renderer.so",
3348 "gl_renderer_interface");
3349 if (!gl_renderer)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003350 return NULL;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003351
3352 /* GBM will load a dri driver, but even though they need symbols from
3353 * libglapi, in some version of Mesa they are not linked to it. Since
3354 * only the gl-renderer module links to it, the call above won't make
3355 * these symbols globally available, and loading the DRI driver fails.
3356 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
3357 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
3358
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003359 gbm = gbm_create_device(fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003360
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003361 return gbm;
3362}
3363
Bryce Harringtonc056a982015-05-19 15:25:18 -07003364/* When initializing EGL, if the preferred buffer format isn't available
Bryce Harringtonb9939982016-04-15 20:28:26 -07003365 * we may be able to substitute an ARGB format for an XRGB one.
Derek Foremanc4cfe852015-05-15 12:12:40 -05003366 *
3367 * This returns 0 if substitution isn't possible, but 0 might be a
3368 * legitimate format for other EGL platforms, so the caller is
3369 * responsible for checking for 0 before calling gl_renderer->create().
3370 *
3371 * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689
3372 * but it's entirely possible we'll see this again on other implementations.
3373 */
3374static int
3375fallback_format_for(uint32_t format)
3376{
3377 switch (format) {
3378 case GBM_FORMAT_XRGB8888:
3379 return GBM_FORMAT_ARGB8888;
3380 case GBM_FORMAT_XRGB2101010:
3381 return GBM_FORMAT_ARGB2101010;
3382 default:
3383 return 0;
3384 }
3385}
3386
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003387static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003388drm_backend_create_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003389{
Derek Foreman6d556372015-11-04 14:47:33 -06003390 EGLint format[3] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01003391 b->gbm_format,
3392 fallback_format_for(b->gbm_format),
Derek Foreman6d556372015-11-04 14:47:33 -06003393 0,
Derek Foremanc4cfe852015-05-15 12:12:40 -05003394 };
Derek Foreman6d556372015-11-04 14:47:33 -06003395 int n_formats = 2;
John Kåre Alsakeref591aa2013-03-02 12:27:39 +01003396
Derek Foremanc4cfe852015-05-15 12:12:40 -05003397 if (format[1])
Derek Foreman6d556372015-11-04 14:47:33 -06003398 n_formats = 3;
Miguel A. Vicodddc6702016-05-18 17:41:07 +02003399 if (gl_renderer->display_create(b->compositor,
3400 EGL_PLATFORM_GBM_KHR,
3401 (void *)b->gbm,
Miguel A. Vico41700e32016-05-18 17:47:59 +02003402 NULL,
Miguel A. Vicodddc6702016-05-18 17:41:07 +02003403 gl_renderer->opaque_attribs,
3404 format,
3405 n_formats) < 0) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003406 return -1;
3407 }
3408
3409 return 0;
3410}
3411
3412static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003413init_egl(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003414{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003415 b->gbm = create_gbm_device(b->drm.fd);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003416
Giulio Camuffo954f1832014-10-11 18:27:30 +03003417 if (!b->gbm)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003418 return -1;
3419
Giulio Camuffo954f1832014-10-11 18:27:30 +03003420 if (drm_backend_create_gl_renderer(b) < 0) {
3421 gbm_device_destroy(b->gbm);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04003422 return -1;
3423 }
3424
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003425 return 0;
3426}
3427
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003428static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003429init_pixman(struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003430{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003431 return pixman_renderer_init(b->compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003432}
3433
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003434/**
Pekka Paalanenec272712014-06-05 11:22:25 +03003435 * Create a drm_plane for a hardware plane
3436 *
3437 * Creates one drm_plane structure for a hardware plane, and initialises its
3438 * properties and formats.
3439 *
Daniel Stone2ba17f42015-05-19 20:02:41 +01003440 * In the absence of universal plane support, where KMS does not explicitly
3441 * expose the primary and cursor planes to userspace, this may also create
3442 * an 'internal' plane for internal management.
3443 *
Pekka Paalanenec272712014-06-05 11:22:25 +03003444 * This function does not add the plane to the list of usable planes in Weston
3445 * itself; the caller is responsible for this.
3446 *
3447 * Call drm_plane_destroy to clean up the plane.
3448 *
Daniel Stone2ba17f42015-05-19 20:02:41 +01003449 * @sa drm_output_find_special_plane
Pekka Paalanenec272712014-06-05 11:22:25 +03003450 * @param b DRM compositor backend
Daniel Stone2ba17f42015-05-19 20:02:41 +01003451 * @param kplane DRM plane to create, or NULL if creating internal plane
3452 * @param output Output to create internal plane for, or NULL
3453 * @param type Type to use when creating internal plane, or invalid
3454 * @param format Format to use for internal planes, or 0
Pekka Paalanenec272712014-06-05 11:22:25 +03003455 */
3456static struct drm_plane *
Daniel Stone2ba17f42015-05-19 20:02:41 +01003457drm_plane_create(struct drm_backend *b, const drmModePlane *kplane,
3458 struct drm_output *output, enum wdrm_plane_type type,
3459 uint32_t format)
Pekka Paalanenec272712014-06-05 11:22:25 +03003460{
3461 struct drm_plane *plane;
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01003462 drmModeObjectProperties *props;
Daniel Stone2ba17f42015-05-19 20:02:41 +01003463 int num_formats = (kplane) ? kplane->count_formats : 1;
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01003464
Daniel Stone2ba17f42015-05-19 20:02:41 +01003465 plane = zalloc(sizeof(*plane) +
3466 (sizeof(uint32_t) * num_formats));
Pekka Paalanenec272712014-06-05 11:22:25 +03003467 if (!plane) {
3468 weston_log("%s: out of memory\n", __func__);
3469 return NULL;
3470 }
3471
3472 plane->backend = b;
Daniel Stonebc15f682016-11-14 16:57:01 +00003473 plane->state_cur = drm_plane_state_alloc(NULL, plane);
3474 plane->state_cur->complete = true;
Pekka Paalanenec272712014-06-05 11:22:25 +03003475
Daniel Stone2ba17f42015-05-19 20:02:41 +01003476 if (kplane) {
3477 plane->possible_crtcs = kplane->possible_crtcs;
3478 plane->plane_id = kplane->plane_id;
3479 plane->count_formats = kplane->count_formats;
3480 memcpy(plane->formats, kplane->formats,
3481 kplane->count_formats * sizeof(kplane->formats[0]));
3482
3483 props = drmModeObjectGetProperties(b->drm.fd, kplane->plane_id,
3484 DRM_MODE_OBJECT_PLANE);
3485 if (!props) {
3486 weston_log("couldn't get plane properties\n");
3487 goto err;
3488 }
3489 drm_property_info_populate(b, plane_props, plane->props,
3490 WDRM_PLANE__COUNT, props);
3491 plane->type =
3492 drm_property_get_value(&plane->props[WDRM_PLANE_TYPE],
3493 props,
3494 WDRM_PLANE_TYPE__COUNT);
3495 drmModeFreeObjectProperties(props);
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01003496 }
Daniel Stone2ba17f42015-05-19 20:02:41 +01003497 else {
3498 plane->possible_crtcs = (1 << output->pipe);
3499 plane->plane_id = 0;
3500 plane->count_formats = 1;
3501 plane->formats[0] = format;
3502 plane->type = type;
3503 }
3504
3505 if (plane->type == WDRM_PLANE_TYPE__COUNT)
3506 goto err_props;
3507
3508 /* With universal planes, everything is a DRM plane; without
3509 * universal planes, the only DRM planes are overlay planes.
3510 * Everything else is a fake plane. */
3511 if (b->universal_planes) {
3512 assert(kplane);
3513 } else {
3514 if (kplane)
3515 assert(plane->type == WDRM_PLANE_TYPE_OVERLAY);
3516 else
3517 assert(plane->type != WDRM_PLANE_TYPE_OVERLAY &&
3518 output);
3519 }
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01003520
Pekka Paalanenec272712014-06-05 11:22:25 +03003521 weston_plane_init(&plane->base, b->compositor, 0, 0);
Daniel Stone085d2b92015-05-21 00:00:57 +01003522 wl_list_insert(&b->plane_list, &plane->link);
Pekka Paalanenec272712014-06-05 11:22:25 +03003523
3524 return plane;
Daniel Stone2ba17f42015-05-19 20:02:41 +01003525
3526err_props:
3527 drm_property_info_free(plane->props, WDRM_PLANE__COUNT);
3528err:
3529 drm_plane_state_free(plane->state_cur, true);
3530 free(plane);
3531 return NULL;
3532}
3533
3534/**
3535 * Find, or create, a special-purpose plane
3536 *
3537 * Primary and cursor planes are a special case, in that before universal
3538 * planes, they are driven by non-plane API calls. Without universal plane
3539 * support, the only way to configure a primary plane is via drmModeSetCrtc,
3540 * and the only way to configure a cursor plane is drmModeSetCursor2.
3541 *
3542 * Although they may actually be regular planes in the hardware, without
3543 * universal plane support, these planes are not actually exposed to
3544 * userspace in the regular plane list.
3545 *
3546 * However, for ease of internal tracking, we want to manage all planes
3547 * through the same drm_plane structures. Therefore, when we are running
3548 * without universal plane support, we create fake drm_plane structures
3549 * to track these planes.
3550 *
3551 * @param b DRM backend
3552 * @param output Output to use for plane
3553 * @param type Type of plane
3554 */
3555static struct drm_plane *
3556drm_output_find_special_plane(struct drm_backend *b, struct drm_output *output,
3557 enum wdrm_plane_type type)
3558{
3559 struct drm_plane *plane;
3560
3561 if (!b->universal_planes) {
3562 uint32_t format;
3563
3564 switch (type) {
3565 case WDRM_PLANE_TYPE_CURSOR:
3566 format = GBM_FORMAT_ARGB8888;
3567 break;
Daniel Stonee2e80132018-01-16 15:37:33 +00003568 case WDRM_PLANE_TYPE_PRIMARY:
3569 /* We don't know what formats the primary plane supports
3570 * before universal planes, so we just assume that the
3571 * GBM format works; however, this isn't set until after
3572 * the output is created. */
3573 format = 0;
3574 break;
Daniel Stone2ba17f42015-05-19 20:02:41 +01003575 default:
3576 assert(!"invalid type in drm_output_find_special_plane");
3577 break;
3578 }
3579
3580 return drm_plane_create(b, NULL, output, type, format);
3581 }
3582
3583 wl_list_for_each(plane, &b->plane_list, link) {
3584 struct drm_output *tmp;
3585 bool found_elsewhere = false;
3586
3587 if (plane->type != type)
3588 continue;
3589 if (!drm_plane_is_available(plane, output))
3590 continue;
3591
3592 /* On some platforms, primary/cursor planes can roam
3593 * between different CRTCs, so make sure we don't claim the
3594 * same plane for two outputs. */
3595 wl_list_for_each(tmp, &b->compositor->pending_output_list,
3596 base.link) {
Daniel Stonee2e80132018-01-16 15:37:33 +00003597 if (tmp->cursor_plane == plane ||
3598 tmp->scanout_plane == plane) {
Daniel Stone2ba17f42015-05-19 20:02:41 +01003599 found_elsewhere = true;
3600 break;
3601 }
3602 }
3603 wl_list_for_each(tmp, &b->compositor->output_list,
3604 base.link) {
Daniel Stonee2e80132018-01-16 15:37:33 +00003605 if (tmp->cursor_plane == plane ||
3606 tmp->scanout_plane == plane) {
Daniel Stone2ba17f42015-05-19 20:02:41 +01003607 found_elsewhere = true;
3608 break;
3609 }
3610 }
3611
3612 if (found_elsewhere)
3613 continue;
3614
3615 plane->possible_crtcs = (1 << output->pipe);
3616 return plane;
3617 }
3618
3619 return NULL;
Pekka Paalanenec272712014-06-05 11:22:25 +03003620}
3621
3622/**
3623 * Destroy one DRM plane
3624 *
3625 * Destroy a DRM plane, removing it from screen and releasing its retained
3626 * buffers in the process. The counterpart to drm_plane_create.
3627 *
3628 * @param plane Plane to deallocate (will be freed)
3629 */
3630static void
3631drm_plane_destroy(struct drm_plane *plane)
3632{
Daniel Stone2ba17f42015-05-19 20:02:41 +01003633 if (plane->type == WDRM_PLANE_TYPE_OVERLAY)
3634 drmModeSetPlane(plane->backend->drm.fd, plane->plane_id,
3635 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
Daniel Stonebc15f682016-11-14 16:57:01 +00003636 drm_plane_state_free(plane->state_cur, true);
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01003637 drm_property_info_free(plane->props, WDRM_PLANE__COUNT);
Pekka Paalanenec272712014-06-05 11:22:25 +03003638 weston_plane_release(&plane->base);
3639 wl_list_remove(&plane->link);
3640 free(plane);
3641}
3642
3643/**
3644 * Initialise sprites (overlay planes)
3645 *
3646 * Walk the list of provided DRM planes, and add overlay planes.
3647 *
3648 * Call destroy_sprites to free these planes.
3649 *
3650 * @param b DRM compositor backend
3651 */
3652static void
3653create_sprites(struct drm_backend *b)
3654{
3655 drmModePlaneRes *kplane_res;
3656 drmModePlane *kplane;
3657 struct drm_plane *drm_plane;
3658 uint32_t i;
Pekka Paalanenec272712014-06-05 11:22:25 +03003659 kplane_res = drmModeGetPlaneResources(b->drm.fd);
3660 if (!kplane_res) {
3661 weston_log("failed to get plane resources: %s\n",
3662 strerror(errno));
3663 return;
3664 }
3665
3666 for (i = 0; i < kplane_res->count_planes; i++) {
3667 kplane = drmModeGetPlane(b->drm.fd, kplane_res->planes[i]);
3668 if (!kplane)
3669 continue;
3670
Daniel Stone2ba17f42015-05-19 20:02:41 +01003671 drm_plane = drm_plane_create(b, kplane, NULL,
3672 WDRM_PLANE_TYPE__COUNT, 0);
Pekka Paalanenec272712014-06-05 11:22:25 +03003673 drmModeFreePlane(kplane);
3674 if (!drm_plane)
3675 continue;
3676
Daniel Stone085d2b92015-05-21 00:00:57 +01003677 if (drm_plane->type == WDRM_PLANE_TYPE_OVERLAY)
3678 weston_compositor_stack_plane(b->compositor,
3679 &drm_plane->base,
3680 &b->compositor->primary_plane);
Pekka Paalanenec272712014-06-05 11:22:25 +03003681 }
3682
3683 drmModeFreePlaneResources(kplane_res);
3684}
3685
3686/**
3687 * Clean up sprites (overlay planes)
3688 *
3689 * The counterpart to create_sprites.
3690 *
3691 * @param b DRM compositor backend
3692 */
3693static void
3694destroy_sprites(struct drm_backend *b)
3695{
3696 struct drm_plane *plane, *next;
3697
Daniel Stone085d2b92015-05-21 00:00:57 +01003698 wl_list_for_each_safe(plane, next, &b->plane_list, link)
Pekka Paalanenec272712014-06-05 11:22:25 +03003699 drm_plane_destroy(plane);
3700}
3701
Pekka Paalanendc14fd42017-11-10 15:31:39 +02003702static uint32_t
3703drm_refresh_rate_mHz(const drmModeModeInfo *info)
3704{
3705 uint64_t refresh;
3706
3707 /* Calculate higher precision (mHz) refresh rate */
3708 refresh = (info->clock * 1000000LL / info->htotal +
3709 info->vtotal / 2) / info->vtotal;
3710
3711 if (info->flags & DRM_MODE_FLAG_INTERLACE)
3712 refresh *= 2;
3713 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
3714 refresh /= 2;
3715 if (info->vscan > 1)
3716 refresh /= info->vscan;
3717
3718 return refresh;
3719}
3720
Pekka Paalanenec272712014-06-05 11:22:25 +03003721/**
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003722 * Add a mode to output's mode list
3723 *
3724 * Copy the supplied DRM mode into a Weston mode structure, and add it to the
3725 * output's mode list.
3726 *
3727 * @param output DRM output to add mode to
3728 * @param info DRM mode structure to add
3729 * @returns Newly-allocated Weston/DRM mode structure
3730 */
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03003731static struct drm_mode *
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003732drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003733{
3734 struct drm_mode *mode;
3735
3736 mode = malloc(sizeof *mode);
3737 if (mode == NULL)
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03003738 return NULL;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003739
3740 mode->base.flags = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02003741 mode->base.width = info->hdisplay;
3742 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04003743
Pekka Paalanendc14fd42017-11-10 15:31:39 +02003744 mode->base.refresh = drm_refresh_rate_mHz(info);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003745 mode->mode_info = *info;
Daniel Stoned5526cb2016-11-16 10:54:10 +00003746 mode->blob_id = 0;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04003747
3748 if (info->type & DRM_MODE_TYPE_PREFERRED)
3749 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
3750
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003751 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
3752
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03003753 return mode;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003754}
3755
Daniel Stoned5526cb2016-11-16 10:54:10 +00003756/**
3757 * Destroys a mode, and removes it from the list.
3758 */
3759static void
3760drm_output_destroy_mode(struct drm_backend *backend, struct drm_mode *mode)
3761{
3762 if (mode->blob_id)
3763 drmModeDestroyPropertyBlob(backend->drm.fd, mode->blob_id);
3764 wl_list_remove(&mode->base.link);
3765 free(mode);
3766}
3767
Pekka Paalanen383b3af2017-09-11 14:40:48 +03003768/** Destroy a list of drm_modes
3769 *
3770 * @param backend The backend for releasing mode property blobs.
3771 * @param mode_list The list linked by drm_mode::base.link.
3772 */
3773static void
3774drm_mode_list_destroy(struct drm_backend *backend, struct wl_list *mode_list)
3775{
3776 struct drm_mode *mode, *next;
3777
3778 wl_list_for_each_safe(mode, next, mode_list, base.link)
3779 drm_output_destroy_mode(backend, mode);
3780}
3781
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003782static int
3783drm_subpixel_to_wayland(int drm_value)
3784{
3785 switch (drm_value) {
3786 default:
3787 case DRM_MODE_SUBPIXEL_UNKNOWN:
3788 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
3789 case DRM_MODE_SUBPIXEL_NONE:
3790 return WL_OUTPUT_SUBPIXEL_NONE;
3791 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
3792 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
3793 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
3794 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
3795 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
3796 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
3797 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
3798 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
3799 }
3800}
3801
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003802/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003803static uint32_t
3804drm_get_backlight(struct drm_output *output)
3805{
3806 long brightness, max_brightness, norm;
3807
3808 brightness = backlight_get_brightness(output->backlight);
3809 max_brightness = backlight_get_max_brightness(output->backlight);
3810
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003811 /* convert it on a scale of 0 to 255 */
3812 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003813
3814 return (uint32_t) norm;
3815}
3816
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003817/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003818static void
3819drm_set_backlight(struct weston_output *output_base, uint32_t value)
3820{
Armin Krezović545dba62016-08-05 15:54:18 +02003821 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003822 long max_brightness, new_brightness;
3823
3824 if (!output->backlight)
3825 return;
3826
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003827 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003828 return;
3829
3830 max_brightness = backlight_get_max_brightness(output->backlight);
3831
3832 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003833 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003834
3835 backlight_set_brightness(output->backlight, new_brightness);
3836}
3837
Daniel Stonea08512f2016-11-08 17:46:10 +00003838/**
3839 * Power output on or off
3840 *
3841 * The DPMS/power level of an output is used to switch it on or off. This
3842 * is DRM's hook for doing so, which can called either as part of repaint,
3843 * or independently of the repaint loop.
3844 *
3845 * If we are called as part of repaint, we simply set the relevant bit in
3846 * state and return.
3847 */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003848static void
3849drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
3850{
Armin Krezović545dba62016-08-05 15:54:18 +02003851 struct drm_output *output = to_drm_output(output_base);
Daniel Stonea08512f2016-11-08 17:46:10 +00003852 struct drm_backend *b = to_drm_backend(output_base->compositor);
3853 struct drm_pending_state *pending_state = b->repaint_data;
3854 struct drm_output_state *state;
Daniel Stone36609c72015-06-18 07:49:02 +01003855 int ret;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003856
Daniel Stonea08512f2016-11-08 17:46:10 +00003857 if (output->state_cur->dpms == level)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003858 return;
3859
Daniel Stonea08512f2016-11-08 17:46:10 +00003860 /* If we're being called during the repaint loop, then this is
3861 * simple: discard any previously-generated state, and create a new
3862 * state where we disable everything. When we come to flush, this
3863 * will be applied.
3864 *
3865 * However, we need to be careful: we can be called whilst another
3866 * output is in its repaint cycle (pending_state exists), but our
3867 * output still has an incomplete state application outstanding.
3868 * In that case, we need to wait until that completes. */
3869 if (pending_state && !output->state_last) {
3870 /* The repaint loop already sets DPMS on; we don't need to
3871 * explicitly set it on here, as it will already happen
3872 * whilst applying the repaint state. */
3873 if (level == WESTON_DPMS_ON)
3874 return;
3875
3876 state = drm_pending_state_get_output(pending_state, output);
3877 if (state)
3878 drm_output_state_free(state);
3879 state = drm_output_get_disable_state(pending_state, output);
Daniel Stone36609c72015-06-18 07:49:02 +01003880 return;
3881 }
3882
Daniel Stonea08512f2016-11-08 17:46:10 +00003883 /* As we throw everything away when disabling, just send us back through
3884 * a repaint cycle. */
3885 if (level == WESTON_DPMS_ON) {
3886 if (output->dpms_off_pending)
3887 output->dpms_off_pending = 0;
3888 weston_output_schedule_repaint(output_base);
3889 return;
3890 }
3891
3892 /* If we've already got a request in the pipeline, then we need to
3893 * park our DPMS request until that request has quiesced. */
3894 if (output->state_last) {
3895 output->dpms_off_pending = 1;
3896 return;
3897 }
3898
3899 pending_state = drm_pending_state_alloc(b);
3900 drm_output_get_disable_state(pending_state, output);
3901 ret = drm_pending_state_apply_sync(pending_state);
3902 if (ret != 0)
3903 weston_log("drm_set_dpms: couldn't disable output?\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003904}
3905
Pekka Paalanen3ce63622014-06-04 16:29:49 +03003906static const char * const connector_type_names[] = {
Pekka Paalanen89c49b32015-08-19 15:25:57 +03003907 [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
3908 [DRM_MODE_CONNECTOR_VGA] = "VGA",
3909 [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
3910 [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
3911 [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
3912 [DRM_MODE_CONNECTOR_Composite] = "Composite",
3913 [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
3914 [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
3915 [DRM_MODE_CONNECTOR_Component] = "Component",
3916 [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
3917 [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
3918 [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
3919 [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
3920 [DRM_MODE_CONNECTOR_TV] = "TV",
3921 [DRM_MODE_CONNECTOR_eDP] = "eDP",
Pekka Paalanenab81f152015-08-24 14:27:07 +03003922#ifdef DRM_MODE_CONNECTOR_DSI
Pekka Paalanen89c49b32015-08-19 15:25:57 +03003923 [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
3924 [DRM_MODE_CONNECTOR_DSI] = "DSI",
Pekka Paalanenab81f152015-08-24 14:27:07 +03003925#endif
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04003926};
3927
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03003928/** Create a name given a DRM connector
3929 *
3930 * \param con The DRM connector whose type and id form the name.
3931 * \return A newly allocate string, or NULL on error. Must be free()'d
3932 * after use.
3933 *
3934 * The name does not identify the DRM display device.
3935 */
Pekka Paalanen3ce63622014-06-04 16:29:49 +03003936static char *
3937make_connector_name(const drmModeConnector *con)
3938{
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03003939 char *name;
Pekka Paalanen89c49b32015-08-19 15:25:57 +03003940 const char *type_name = NULL;
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03003941 int ret;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03003942
3943 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
3944 type_name = connector_type_names[con->connector_type];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03003945
3946 if (!type_name)
3947 type_name = "UNNAMED";
3948
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03003949 ret = asprintf(&name, "%s-%d", type_name, con->connector_type_id);
3950 if (ret < 0)
3951 return NULL;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03003952
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03003953 return name;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03003954}
3955
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003956static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003957find_crtc_for_connector(struct drm_backend *b,
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003958 drmModeRes *resources, drmModeConnector *connector)
3959{
3960 drmModeEncoder *encoder;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003961 int i, j;
Daniel Stone75487c22017-01-16 14:33:38 +00003962 int ret = -1;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003963
3964 for (j = 0; j < connector->count_encoders; j++) {
Daniel Stone75487c22017-01-16 14:33:38 +00003965 uint32_t possible_crtcs, encoder_id, crtc_id;
3966
Giulio Camuffo954f1832014-10-11 18:27:30 +03003967 encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003968 if (encoder == NULL) {
3969 weston_log("Failed to get encoder.\n");
Daniel Stone75487c22017-01-16 14:33:38 +00003970 continue;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003971 }
Daniel Stone75487c22017-01-16 14:33:38 +00003972 encoder_id = encoder->encoder_id;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003973 possible_crtcs = encoder->possible_crtcs;
Daniel Stone75487c22017-01-16 14:33:38 +00003974 crtc_id = encoder->crtc_id;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003975 drmModeFreeEncoder(encoder);
3976
3977 for (i = 0; i < resources->count_crtcs; i++) {
Daniel Stone72c0e1b2017-02-09 13:49:15 +00003978 if (!(possible_crtcs & (1 << i)))
3979 continue;
3980
3981 if (drm_output_find_by_crtc(b, resources->crtcs[i]))
3982 continue;
3983
Daniel Stone75487c22017-01-16 14:33:38 +00003984 /* Try to preserve the existing
3985 * CRTC -> encoder -> connector routing; it makes
3986 * initialisation faster, and also since we have a
3987 * very dumb picking algorithm, may preserve a better
3988 * choice. */
3989 if (!connector->encoder_id ||
3990 (encoder_id == connector->encoder_id &&
3991 crtc_id == resources->crtcs[i]))
3992 return i;
3993
3994 ret = i;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003995 }
3996 }
3997
Daniel Stone75487c22017-01-16 14:33:38 +00003998 return ret;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003999}
4000
Daniel Stonee4256832017-04-04 17:54:27 +01004001static void drm_output_fini_cursor_egl(struct drm_output *output)
4002{
4003 unsigned int i;
4004
4005 for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) {
4006 drm_fb_unref(output->gbm_cursor_fb[i]);
4007 output->gbm_cursor_fb[i] = NULL;
4008 }
4009}
4010
4011static int
4012drm_output_init_cursor_egl(struct drm_output *output, struct drm_backend *b)
4013{
4014 unsigned int i;
4015
Daniel Stone2ba17f42015-05-19 20:02:41 +01004016 /* No point creating cursors if we don't have a plane for them. */
4017 if (!output->cursor_plane)
4018 return 0;
4019
Daniel Stonee4256832017-04-04 17:54:27 +01004020 for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) {
4021 struct gbm_bo *bo;
4022
4023 bo = gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
4024 GBM_FORMAT_ARGB8888,
4025 GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE);
4026 if (!bo)
4027 goto err;
4028
4029 output->gbm_cursor_fb[i] =
4030 drm_fb_get_from_bo(bo, b, GBM_FORMAT_ARGB8888,
4031 BUFFER_CURSOR);
4032 if (!output->gbm_cursor_fb[i]) {
4033 gbm_bo_destroy(bo);
4034 goto err;
4035 }
4036 }
4037
4038 return 0;
4039
4040err:
4041 weston_log("cursor buffers unavailable, using gl cursors\n");
4042 b->cursors_are_broken = 1;
4043 drm_output_fini_cursor_egl(output);
4044 return -1;
4045}
4046
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02004047/* Init output state that depends on gl or gbm */
4048static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03004049drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02004050{
Derek Foremanc4cfe852015-05-15 12:12:40 -05004051 EGLint format[2] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01004052 output->gbm_format,
4053 fallback_format_for(output->gbm_format),
Derek Foremanc4cfe852015-05-15 12:12:40 -05004054 };
Daniel Stonee4256832017-04-04 17:54:27 +01004055 int n_formats = 1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02004056
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01004057 output->gbm_surface = gbm_surface_create(b->gbm,
Hardeningff39efa2013-09-18 23:56:35 +02004058 output->base.current_mode->width,
4059 output->base.current_mode->height,
Derek Foremanc4cfe852015-05-15 12:12:40 -05004060 format[0],
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02004061 GBM_BO_USE_SCANOUT |
4062 GBM_BO_USE_RENDERING);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01004063 if (!output->gbm_surface) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02004064 weston_log("failed to create gbm surface\n");
4065 return -1;
4066 }
4067
Derek Foremanc4cfe852015-05-15 12:12:40 -05004068 if (format[1])
4069 n_formats = 2;
Miguel A. Vicoc095cde2016-05-18 17:43:00 +02004070 if (gl_renderer->output_window_create(&output->base,
4071 (EGLNativeWindowType)output->gbm_surface,
4072 output->gbm_surface,
4073 gl_renderer->opaque_attribs,
4074 format,
4075 n_formats) < 0) {
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02004076 weston_log("failed to create gl renderer output state\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01004077 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02004078 return -1;
4079 }
4080
Daniel Stonee4256832017-04-04 17:54:27 +01004081 drm_output_init_cursor_egl(output, b);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02004082
4083 return 0;
4084}
4085
Daniel Stone3e661f72016-11-04 17:24:06 +00004086static void
4087drm_output_fini_egl(struct drm_output *output)
4088{
Daniel Stonee2e80132018-01-16 15:37:33 +00004089 struct drm_backend *b = to_drm_backend(output->base.compositor);
4090
4091 /* Destroying the GBM surface will destroy all our GBM buffers,
4092 * regardless of refcount. Ensure we destroy them here. */
4093 if (!b->shutting_down &&
4094 output->scanout_plane->state_cur->fb &&
4095 output->scanout_plane->state_cur->fb->type == BUFFER_GBM_SURFACE) {
4096 drm_plane_state_free(output->scanout_plane->state_cur, true);
4097 output->scanout_plane->state_cur =
4098 drm_plane_state_alloc(NULL, output->scanout_plane);
4099 output->scanout_plane->state_cur->complete = true;
4100 }
4101
Daniel Stone3e661f72016-11-04 17:24:06 +00004102 gl_renderer->output_destroy(&output->base);
4103 gbm_surface_destroy(output->gbm_surface);
Daniel Stonee4256832017-04-04 17:54:27 +01004104 drm_output_fini_cursor_egl(output);
Daniel Stone3e661f72016-11-04 17:24:06 +00004105}
4106
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04004107static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03004108drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004109{
Hardeningff39efa2013-09-18 23:56:35 +02004110 int w = output->base.current_mode->width;
4111 int h = output->base.current_mode->height;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03004112 uint32_t format = output->gbm_format;
4113 uint32_t pixman_format;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004114 unsigned int i;
4115
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03004116 switch (format) {
4117 case GBM_FORMAT_XRGB8888:
4118 pixman_format = PIXMAN_x8r8g8b8;
4119 break;
4120 case GBM_FORMAT_RGB565:
4121 pixman_format = PIXMAN_r5g6b5;
4122 break;
4123 default:
4124 weston_log("Unsupported pixman format 0x%x\n", format);
4125 return -1;
4126 }
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004127
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03004128 /* FIXME error checking */
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004129 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03004130 output->dumb[i] = drm_fb_create_dumb(b, w, h, format);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004131 if (!output->dumb[i])
4132 goto err;
4133
4134 output->image[i] =
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03004135 pixman_image_create_bits(pixman_format, w, h,
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004136 output->dumb[i]->map,
4137 output->dumb[i]->stride);
4138 if (!output->image[i])
4139 goto err;
4140 }
4141
4142 if (pixman_renderer_output_create(&output->base) < 0)
4143 goto err;
4144
4145 pixman_region32_init_rect(&output->previous_damage,
Alexander Larsson0b135062013-05-28 16:23:36 +02004146 output->base.x, output->base.y, output->base.width, output->base.height);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004147
4148 return 0;
4149
4150err:
4151 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
4152 if (output->dumb[i])
Daniel Stone6e7a9612017-04-04 17:54:26 +01004153 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004154 if (output->image[i])
4155 pixman_image_unref(output->image[i]);
4156
4157 output->dumb[i] = NULL;
4158 output->image[i] = NULL;
4159 }
4160
4161 return -1;
4162}
4163
4164static void
4165drm_output_fini_pixman(struct drm_output *output)
4166{
Daniel Stonee2e80132018-01-16 15:37:33 +00004167 struct drm_backend *b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004168 unsigned int i;
4169
Daniel Stonee2e80132018-01-16 15:37:33 +00004170 /* Destroying the Pixman surface will destroy all our buffers,
4171 * regardless of refcount. Ensure we destroy them here. */
4172 if (!b->shutting_down &&
4173 output->scanout_plane->state_cur->fb &&
4174 output->scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB) {
4175 drm_plane_state_free(output->scanout_plane->state_cur, true);
4176 output->scanout_plane->state_cur =
4177 drm_plane_state_alloc(NULL, output->scanout_plane);
4178 output->scanout_plane->state_cur->complete = true;
4179 }
4180
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004181 pixman_renderer_output_destroy(&output->base);
4182 pixman_region32_fini(&output->previous_damage);
4183
4184 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004185 pixman_image_unref(output->image[i]);
Daniel Stone6e7a9612017-04-04 17:54:26 +01004186 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004187 output->dumb[i] = NULL;
4188 output->image[i] = NULL;
4189 }
4190}
4191
Richard Hughes2b2092a2013-04-24 14:58:02 +01004192static void
4193edid_parse_string(const uint8_t *data, char text[])
4194{
4195 int i;
4196 int replaced = 0;
4197
4198 /* this is always 12 bytes, but we can't guarantee it's null
4199 * terminated or not junk. */
4200 strncpy(text, (const char *) data, 12);
4201
Bryce Harrington9c7de162015-08-28 13:04:26 -07004202 /* guarantee our new string is null-terminated */
4203 text[12] = '\0';
4204
Richard Hughes2b2092a2013-04-24 14:58:02 +01004205 /* remove insane chars */
4206 for (i = 0; text[i] != '\0'; i++) {
4207 if (text[i] == '\n' ||
4208 text[i] == '\r') {
4209 text[i] = '\0';
4210 break;
4211 }
4212 }
4213
4214 /* ensure string is printable */
4215 for (i = 0; text[i] != '\0'; i++) {
4216 if (!isprint(text[i])) {
4217 text[i] = '-';
4218 replaced++;
4219 }
4220 }
4221
4222 /* if the string is random junk, ignore the string */
4223 if (replaced > 4)
4224 text[0] = '\0';
4225}
4226
4227#define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
4228#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
4229#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
4230#define EDID_OFFSET_DATA_BLOCKS 0x36
4231#define EDID_OFFSET_LAST_BLOCK 0x6c
4232#define EDID_OFFSET_PNPID 0x08
4233#define EDID_OFFSET_SERIAL 0x0c
4234
4235static int
4236edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
4237{
4238 int i;
4239 uint32_t serial_number;
4240
4241 /* check header */
4242 if (length < 128)
4243 return -1;
4244 if (data[0] != 0x00 || data[1] != 0xff)
4245 return -1;
4246
4247 /* decode the PNP ID from three 5 bit words packed into 2 bytes
4248 * /--08--\/--09--\
4249 * 7654321076543210
4250 * |\---/\---/\---/
4251 * R C1 C2 C3 */
4252 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
4253 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
4254 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
4255 edid->pnp_id[3] = '\0';
4256
4257 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
4258 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
4259 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
4260 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
4261 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
4262 if (serial_number > 0)
4263 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
4264
4265 /* parse EDID data */
4266 for (i = EDID_OFFSET_DATA_BLOCKS;
4267 i <= EDID_OFFSET_LAST_BLOCK;
4268 i += 18) {
4269 /* ignore pixel clock data */
4270 if (data[i] != 0)
4271 continue;
4272 if (data[i+2] != 0)
4273 continue;
4274
4275 /* any useful blocks? */
4276 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
4277 edid_parse_string(&data[i+5],
4278 edid->monitor_name);
4279 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
4280 edid_parse_string(&data[i+5],
4281 edid->serial_number);
4282 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
4283 edid_parse_string(&data[i+5],
4284 edid->eisa_id);
4285 }
4286 }
4287 return 0;
4288}
4289
Pekka Paalanen6f1866b2017-04-03 14:22:51 +03004290/** Parse monitor make, model and serial from EDID
4291 *
4292 * \param b The backend instance.
4293 * \param output The output whose \c drm_edid to fill in.
4294 * \param props The DRM connector properties to get the EDID from.
4295 * \param make[out] The monitor make (PNP ID).
4296 * \param model[out] The monitor model (name).
4297 * \param serial_number[out] The monitor serial number.
4298 *
4299 * Each of \c *make, \c *model and \c *serial_number are set only if the
4300 * information is found in the EDID. The pointers they are set to must not
4301 * be free()'d explicitly, instead they get implicitly freed when the
4302 * \c drm_output is destroyed.
4303 */
Richard Hughes2b2092a2013-04-24 14:58:02 +01004304static void
Daniel Stone02cf4662017-03-03 16:19:39 +00004305find_and_parse_output_edid(struct drm_backend *b, struct drm_output *output,
Pekka Paalanen6f1866b2017-04-03 14:22:51 +03004306 drmModeObjectPropertiesPtr props,
4307 const char **make,
4308 const char **model,
4309 const char **serial_number)
Richard Hughes2b2092a2013-04-24 14:58:02 +01004310{
4311 drmModePropertyBlobPtr edid_blob = NULL;
Daniel Stone02cf4662017-03-03 16:19:39 +00004312 uint32_t blob_id;
Richard Hughes2b2092a2013-04-24 14:58:02 +01004313 int rc;
4314
Daniel Stone02cf4662017-03-03 16:19:39 +00004315 blob_id =
4316 drm_property_get_value(&output->props_conn[WDRM_CONNECTOR_EDID],
4317 props, 0);
4318 if (!blob_id)
4319 return;
4320
4321 edid_blob = drmModeGetPropertyBlob(b->drm.fd, blob_id);
Richard Hughes2b2092a2013-04-24 14:58:02 +01004322 if (!edid_blob)
4323 return;
4324
4325 rc = edid_parse(&output->edid,
4326 edid_blob->data,
4327 edid_blob->length);
4328 if (!rc) {
4329 weston_log("EDID data '%s', '%s', '%s'\n",
4330 output->edid.pnp_id,
4331 output->edid.monitor_name,
4332 output->edid.serial_number);
4333 if (output->edid.pnp_id[0] != '\0')
Pekka Paalanen6f1866b2017-04-03 14:22:51 +03004334 *make = output->edid.pnp_id;
Richard Hughes2b2092a2013-04-24 14:58:02 +01004335 if (output->edid.monitor_name[0] != '\0')
Pekka Paalanen6f1866b2017-04-03 14:22:51 +03004336 *model = output->edid.monitor_name;
Richard Hughes2b2092a2013-04-24 14:58:02 +01004337 if (output->edid.serial_number[0] != '\0')
Pekka Paalanen6f1866b2017-04-03 14:22:51 +03004338 *serial_number = output->edid.serial_number;
Richard Hughes2b2092a2013-04-24 14:58:02 +01004339 }
4340 drmModeFreePropertyBlob(edid_blob);
4341}
4342
Kristian Høgsberga30989a2013-05-23 17:23:15 -04004343static int
4344parse_modeline(const char *s, drmModeModeInfo *mode)
4345{
4346 char hsync[16];
4347 char vsync[16];
4348 float fclock;
4349
Pekka Paalanendc4e3c62017-12-05 15:37:41 +02004350 memset(mode, 0, sizeof *mode);
4351
Kristian Høgsberga30989a2013-05-23 17:23:15 -04004352 mode->type = DRM_MODE_TYPE_USERDEF;
4353 mode->hskew = 0;
4354 mode->vscan = 0;
4355 mode->vrefresh = 0;
4356 mode->flags = 0;
4357
Rob Bradford307e09e2013-07-26 16:29:40 +01004358 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
Kristian Høgsberga30989a2013-05-23 17:23:15 -04004359 &fclock,
4360 &mode->hdisplay,
4361 &mode->hsync_start,
4362 &mode->hsync_end,
4363 &mode->htotal,
4364 &mode->vdisplay,
4365 &mode->vsync_start,
4366 &mode->vsync_end,
4367 &mode->vtotal, hsync, vsync) != 11)
4368 return -1;
4369
4370 mode->clock = fclock * 1000;
4371 if (strcmp(hsync, "+hsync") == 0)
4372 mode->flags |= DRM_MODE_FLAG_PHSYNC;
4373 else if (strcmp(hsync, "-hsync") == 0)
4374 mode->flags |= DRM_MODE_FLAG_NHSYNC;
4375 else
4376 return -1;
4377
4378 if (strcmp(vsync, "+vsync") == 0)
4379 mode->flags |= DRM_MODE_FLAG_PVSYNC;
4380 else if (strcmp(vsync, "-vsync") == 0)
4381 mode->flags |= DRM_MODE_FLAG_NVSYNC;
4382 else
4383 return -1;
4384
Emmanuel Gil Peyrota62138b2016-05-02 22:40:11 +01004385 snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f",
4386 mode->hdisplay, mode->vdisplay, fclock);
4387
Kristian Høgsberga30989a2013-05-23 17:23:15 -04004388 return 0;
4389}
4390
Rob Bradford66bd9f52013-06-25 18:56:42 +01004391static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03004392setup_output_seat_constraint(struct drm_backend *b,
Rob Bradford66bd9f52013-06-25 18:56:42 +01004393 struct weston_output *output,
4394 const char *s)
4395{
4396 if (strcmp(s, "") != 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05004397 struct weston_pointer *pointer;
Rob Bradford66bd9f52013-06-25 18:56:42 +01004398 struct udev_seat *seat;
4399
Giulio Camuffo954f1832014-10-11 18:27:30 +03004400 seat = udev_seat_get_named(&b->input, s);
Derek Foreman0720ea32015-07-15 13:00:35 -05004401 if (!seat)
4402 return;
Rob Bradford66bd9f52013-06-25 18:56:42 +01004403
Derek Foreman0720ea32015-07-15 13:00:35 -05004404 seat->base.output = output;
4405
Derek Foreman1281a362015-07-31 16:55:32 -05004406 pointer = weston_seat_get_pointer(&seat->base);
4407 if (pointer)
4408 weston_pointer_clamp(pointer,
4409 &pointer->x,
4410 &pointer->y);
Rob Bradford66bd9f52013-06-25 18:56:42 +01004411 }
4412}
4413
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004414static int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004415parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format)
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004416{
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004417 int ret = 0;
4418
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004419 if (s == NULL)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004420 *gbm_format = default_value;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004421 else if (strcmp(s, "xrgb8888") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004422 *gbm_format = GBM_FORMAT_XRGB8888;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004423 else if (strcmp(s, "rgb565") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004424 *gbm_format = GBM_FORMAT_RGB565;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004425 else if (strcmp(s, "xrgb2101010") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004426 *gbm_format = GBM_FORMAT_XRGB2101010;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004427 else {
4428 weston_log("fatal: unrecognized pixel format: %s\n", s);
4429 ret = -1;
4430 }
4431
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004432 return ret;
4433}
4434
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004435/**
4436 * Choose suitable mode for an output
4437 *
4438 * Find the most suitable mode to use for initial setup (or reconfiguration on
4439 * hotplug etc) for a DRM output.
4440 *
4441 * @param output DRM output to choose mode for
4442 * @param kind Strategy and preference to use when choosing mode
4443 * @param width Desired width for this output
4444 * @param height Desired height for this output
4445 * @param current_mode Mode currently being displayed on this output
4446 * @param modeline Manually-entered mode (may be NULL)
4447 * @returns A mode from the output's mode list, or NULL if none available
4448 */
4449static struct drm_mode *
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004450drm_output_choose_initial_mode(struct drm_backend *backend,
4451 struct drm_output *output,
4452 enum weston_drm_backend_output_mode mode,
Armin Krezović08368132016-09-30 14:11:05 +02004453 const char *modeline,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004454 const drmModeModeInfo *current_mode)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004455{
4456 struct drm_mode *preferred = NULL;
4457 struct drm_mode *current = NULL;
4458 struct drm_mode *configured = NULL;
4459 struct drm_mode *best = NULL;
4460 struct drm_mode *drm_mode;
Armin Krezović08368132016-09-30 14:11:05 +02004461 drmModeModeInfo drm_modeline;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004462 int32_t width = 0;
4463 int32_t height = 0;
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01004464 uint32_t refresh = 0;
4465 int n;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004466
Armin Krezović08368132016-09-30 14:11:05 +02004467 if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) {
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01004468 n = sscanf(modeline, "%dx%d@%d", &width, &height, &refresh);
4469 if (n != 2 && n != 3) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004470 width = -1;
4471
Armin Krezović08368132016-09-30 14:11:05 +02004472 if (parse_modeline(modeline, &drm_modeline) == 0) {
4473 configured = drm_output_add_mode(output, &drm_modeline);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004474 if (!configured)
4475 return NULL;
4476 } else {
4477 weston_log("Invalid modeline \"%s\" for output %s\n",
Armin Krezović08368132016-09-30 14:11:05 +02004478 modeline, output->base.name);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004479 }
4480 }
4481 }
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004482
4483 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004484 if (width == drm_mode->base.width &&
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01004485 height == drm_mode->base.height &&
4486 (refresh == 0 || refresh == drm_mode->mode_info.vrefresh))
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004487 configured = drm_mode;
4488
comic fans7a5c5622016-03-17 14:29:27 +02004489 if (memcmp(current_mode, &drm_mode->mode_info,
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004490 sizeof *current_mode) == 0)
4491 current = drm_mode;
4492
4493 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
4494 preferred = drm_mode;
4495
4496 best = drm_mode;
4497 }
4498
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004499 if (current == NULL && current_mode->clock != 0) {
4500 current = drm_output_add_mode(output, current_mode);
4501 if (!current)
4502 return NULL;
4503 }
4504
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004505 if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004506 configured = current;
4507
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004508 if (configured)
4509 return configured;
4510
4511 if (preferred)
4512 return preferred;
4513
4514 if (current)
4515 return current;
4516
4517 if (best)
4518 return best;
4519
4520 weston_log("no available modes for %s\n", output->base.name);
4521 return NULL;
4522}
4523
Pekka Paalaneneee580b2014-06-04 16:43:06 +03004524static int
4525connector_get_current_mode(drmModeConnector *connector, int drm_fd,
4526 drmModeModeInfo *mode)
4527{
4528 drmModeEncoder *encoder;
4529 drmModeCrtc *crtc;
4530
4531 /* Get the current mode on the crtc that's currently driving
4532 * this connector. */
4533 encoder = drmModeGetEncoder(drm_fd, connector->encoder_id);
4534 memset(mode, 0, sizeof *mode);
4535 if (encoder != NULL) {
4536 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
4537 drmModeFreeEncoder(encoder);
4538 if (crtc == NULL)
4539 return -1;
4540 if (crtc->mode_valid)
4541 *mode = crtc->mode;
4542 drmModeFreeCrtc(crtc);
4543 }
4544
4545 return 0;
4546}
4547
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004548static int
Armin Krezović08368132016-09-30 14:11:05 +02004549drm_output_set_mode(struct weston_output *base,
4550 enum weston_drm_backend_output_mode mode,
4551 const char *modeline)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004552{
Armin Krezović08368132016-09-30 14:11:05 +02004553 struct drm_output *output = to_drm_output(base);
4554 struct drm_backend *b = to_drm_backend(base->compositor);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004555
Armin Krezović445b41b2016-10-09 23:48:16 +02004556 struct drm_mode *current;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004557 drmModeModeInfo crtc_mode;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004558
Armin Krezović08368132016-09-30 14:11:05 +02004559 if (connector_get_current_mode(output->connector, b->drm.fd, &crtc_mode) < 0)
Armin Krezović445b41b2016-10-09 23:48:16 +02004560 return -1;
David Herrmann0f0d54e2011-12-08 17:05:45 +01004561
Armin Krezović08368132016-09-30 14:11:05 +02004562 current = drm_output_choose_initial_mode(b, output, mode, modeline, &crtc_mode);
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004563 if (!current)
Armin Krezović445b41b2016-10-09 23:48:16 +02004564 return -1;
Armin Krezović08368132016-09-30 14:11:05 +02004565
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004566 output->base.current_mode = &current->base;
Hardeningff39efa2013-09-18 23:56:35 +02004567 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
Wang Quanxianacb805a2012-07-30 18:09:46 -04004568
Armin Krezović08368132016-09-30 14:11:05 +02004569 /* Set native_ fields, so weston_output_mode_switch_to_native() works */
4570 output->base.native_mode = output->base.current_mode;
4571 output->base.native_scale = output->base.current_scale;
4572
Armin Krezović08368132016-09-30 14:11:05 +02004573 return 0;
Armin Krezović08368132016-09-30 14:11:05 +02004574}
4575
4576static void
4577drm_output_set_gbm_format(struct weston_output *base,
4578 const char *gbm_format)
4579{
4580 struct drm_output *output = to_drm_output(base);
4581 struct drm_backend *b = to_drm_backend(base->compositor);
4582
4583 if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1)
4584 output->gbm_format = b->gbm_format;
Daniel Stonee2e80132018-01-16 15:37:33 +00004585
4586 /* Without universal planes, we can't discover which formats are
4587 * supported by the primary plane; we just hope that the GBM format
4588 * works. */
4589 if (!b->universal_planes)
4590 output->scanout_plane->formats[0] = output->gbm_format;
Armin Krezović08368132016-09-30 14:11:05 +02004591}
4592
4593static void
4594drm_output_set_seat(struct weston_output *base,
4595 const char *seat)
4596{
4597 struct drm_output *output = to_drm_output(base);
4598 struct drm_backend *b = to_drm_backend(base->compositor);
4599
4600 setup_output_seat_constraint(b, &output->base,
4601 seat ? seat : "");
4602}
4603
4604static int
Pekka Paalanenc4db6f72017-09-05 16:37:03 +03004605drm_output_init_gamma_size(struct drm_output *output)
4606{
4607 struct drm_backend *backend = to_drm_backend(output->base.compositor);
4608 drmModeCrtc *crtc;
4609
4610 assert(output->base.compositor);
4611 assert(output->crtc_id != 0);
4612 crtc = drmModeGetCrtc(backend->drm.fd, output->crtc_id);
4613 if (!crtc)
4614 return -1;
4615
4616 output->base.gamma_size = crtc->gamma_size;
4617
4618 drmModeFreeCrtc(crtc);
4619
4620 return 0;
4621}
4622
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03004623/** Allocate a CRTC for the output
4624 *
4625 * @param output The output with no allocated CRTC.
4626 * @param resources DRM KMS resources.
4627 * @param connector The DRM KMS connector data.
4628 * @return 0 on success, -1 on failure.
4629 *
4630 * Finds a free CRTC that can drive the given connector, reserves the CRTC
4631 * for the output, and loads the CRTC properties.
4632 *
4633 * Populates the cursor and scanout planes.
4634 *
4635 * On failure, the output remains without a CRTC.
4636 */
4637static int
4638drm_output_init_crtc(struct drm_output *output,
4639 drmModeRes *resources, drmModeConnector *connector)
4640{
4641 struct drm_backend *b = to_drm_backend(output->base.compositor);
4642 drmModeObjectPropertiesPtr props;
4643 int i;
4644
4645 assert(output->crtc_id == 0);
4646
4647 i = find_crtc_for_connector(b, resources, connector);
4648 if (i < 0) {
4649 weston_log("No usable crtc/encoder pair for connector.\n");
4650 return -1;
4651 }
4652
4653 output->crtc_id = resources->crtcs[i];
4654 output->pipe = i;
4655
4656 props = drmModeObjectGetProperties(b->drm.fd, output->crtc_id,
4657 DRM_MODE_OBJECT_CRTC);
4658 if (!props) {
4659 weston_log("failed to get CRTC properties\n");
4660 goto err_crtc;
4661 }
4662 drm_property_info_populate(b, crtc_props, output->props_crtc,
4663 WDRM_CRTC__COUNT, props);
4664 drmModeFreeObjectProperties(props);
4665
4666 output->scanout_plane =
4667 drm_output_find_special_plane(b, output,
4668 WDRM_PLANE_TYPE_PRIMARY);
4669 if (!output->scanout_plane) {
4670 weston_log("Failed to find primary plane for output %s\n",
4671 output->base.name);
4672 goto err_crtc;
4673 }
4674
4675 /* Failing to find a cursor plane is not fatal, as we'll fall back
4676 * to software cursor. */
4677 output->cursor_plane =
4678 drm_output_find_special_plane(b, output,
4679 WDRM_PLANE_TYPE_CURSOR);
4680
4681 return 0;
4682
4683err_crtc:
4684 output->crtc_id = 0;
4685 output->pipe = 0;
4686
4687 return -1;
4688}
4689
4690/** Free the CRTC from the output
4691 *
4692 * @param output The output whose CRTC to deallocate.
4693 *
4694 * The CRTC reserved for the given output becomes free to use again.
4695 */
4696static void
4697drm_output_fini_crtc(struct drm_output *output)
4698{
4699 struct drm_backend *b = to_drm_backend(output->base.compositor);
4700
4701 if (!b->universal_planes && !b->shutting_down) {
4702 /* With universal planes, the 'special' planes are allocated at
4703 * startup, freed at shutdown, and live on the plane list in
4704 * between. We want the planes to continue to exist and be freed
4705 * up for other outputs.
4706 *
4707 * Without universal planes, our special planes are
4708 * pseudo-planes allocated at output creation, freed at output
4709 * destruction, and not usable by other outputs.
4710 *
4711 * On the other hand, if the compositor is already shutting down,
4712 * the plane has already been destroyed.
4713 */
4714 if (output->cursor_plane)
4715 drm_plane_destroy(output->cursor_plane);
4716 if (output->scanout_plane)
4717 drm_plane_destroy(output->scanout_plane);
4718 }
4719
4720 drm_property_info_free(output->props_crtc, WDRM_CRTC__COUNT);
4721 output->crtc_id = 0;
4722 output->cursor_plane = NULL;
4723 output->scanout_plane = NULL;
4724}
4725
Pekka Paalanenc4db6f72017-09-05 16:37:03 +03004726static int
Armin Krezović08368132016-09-30 14:11:05 +02004727drm_output_enable(struct weston_output *base)
4728{
4729 struct drm_output *output = to_drm_output(base);
4730 struct drm_backend *b = to_drm_backend(base->compositor);
4731 struct weston_mode *m;
4732
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00004733 if (b->pageflip_timeout)
4734 drm_output_pageflip_timer_create(output);
4735
Giulio Camuffo954f1832014-10-11 18:27:30 +03004736 if (b->use_pixman) {
4737 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004738 weston_log("Failed to init output pixman state\n");
Daniel Stone02cf4662017-03-03 16:19:39 +00004739 goto err;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004740 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03004741 } else if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02004742 weston_log("Failed to init output gl state\n");
Daniel Stone02cf4662017-03-03 16:19:39 +00004743 goto err;
Kristian Høgsberg1d1e0a52012-10-21 13:29:26 -04004744 }
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04004745
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004746 if (output->backlight) {
Kristian Høgsberg220819f2013-05-23 20:29:40 -04004747 weston_log("Initialized backlight, device %s\n",
4748 output->backlight->path);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004749 output->base.set_backlight = drm_set_backlight;
4750 output->base.backlight_current = drm_get_backlight(output);
Kristian Høgsberg220819f2013-05-23 20:29:40 -04004751 } else {
4752 weston_log("Failed to initialize backlight\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004753 }
4754
Jonas Ådahle5a12252013-04-05 23:07:11 +02004755 output->base.start_repaint_loop = drm_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05004756 output->base.repaint = drm_output_repaint;
Jesse Barnes58ef3792012-02-23 09:45:49 -05004757 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004758 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08004759 output->base.switch_mode = drm_output_switch_mode;
Richard Hughese7299962013-05-01 21:52:12 +01004760 output->base.set_gamma = drm_output_set_gamma;
4761
Daniel Stone2ba17f42015-05-19 20:02:41 +01004762 if (output->cursor_plane)
4763 weston_compositor_stack_plane(b->compositor,
4764 &output->cursor_plane->base,
4765 NULL);
4766 else
4767 b->cursors_are_broken = 1;
4768
Daniel Stonee2e80132018-01-16 15:37:33 +00004769 weston_compositor_stack_plane(b->compositor,
4770 &output->scanout_plane->base,
Giulio Camuffo954f1832014-10-11 18:27:30 +03004771 &b->compositor->primary_plane);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004772
Pekka Paalanen9068e012018-02-09 13:31:50 +02004773 wl_array_remove_uint32(&b->unused_connectors, output->connector_id);
4774 wl_array_remove_uint32(&b->unused_crtcs, output->crtc_id);
4775
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04004776 weston_log("Output %s, (connector %d, crtc %d)\n",
Richard Hughesafe690c2013-05-02 10:10:04 +01004777 output->base.name, output->connector_id, output->crtc_id);
Kristian Høgsberg061c4252012-06-28 11:28:15 -04004778 wl_list_for_each(m, &output->base.mode_list, link)
U. Artie Eoffd3ed6cb2014-01-10 10:15:17 -08004779 weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n",
Kristian Høgsberg061c4252012-06-28 11:28:15 -04004780 m->width, m->height, m->refresh / 1000.0,
4781 m->flags & WL_OUTPUT_MODE_PREFERRED ?
4782 ", preferred" : "",
4783 m->flags & WL_OUTPUT_MODE_CURRENT ?
4784 ", current" : "",
Armin Krezović08368132016-09-30 14:11:05 +02004785 output->connector->count_modes == 0 ?
Kristian Høgsberg061c4252012-06-28 11:28:15 -04004786 ", built-in" : "");
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04004787
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004788 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01004789
Daniel Stone02cf4662017-03-03 16:19:39 +00004790err:
David Herrmann0f0d54e2011-12-08 17:05:45 +01004791 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004792}
4793
Jesse Barnes58ef3792012-02-23 09:45:49 -05004794static void
Armin Krezović08368132016-09-30 14:11:05 +02004795drm_output_deinit(struct weston_output *base)
4796{
4797 struct drm_output *output = to_drm_output(base);
4798 struct drm_backend *b = to_drm_backend(base->compositor);
Daniel Stone087ddf02017-02-14 17:51:30 +00004799 uint32_t *unused;
Armin Krezović08368132016-09-30 14:11:05 +02004800
Daniel Stone3e661f72016-11-04 17:24:06 +00004801 if (b->use_pixman)
Armin Krezović08368132016-09-30 14:11:05 +02004802 drm_output_fini_pixman(output);
Daniel Stone3e661f72016-11-04 17:24:06 +00004803 else
4804 drm_output_fini_egl(output);
Armin Krezović08368132016-09-30 14:11:05 +02004805
Daniel Stone2ba17f42015-05-19 20:02:41 +01004806 /* Since our planes are no longer in use anywhere, remove their base
4807 * weston_plane's link from the plane stacking list, unless we're
4808 * shutting down, in which case the plane has already been
4809 * destroyed. */
Daniel Stonee2e80132018-01-16 15:37:33 +00004810 if (!b->shutting_down) {
4811 wl_list_remove(&output->scanout_plane->base.link);
4812 wl_list_init(&output->scanout_plane->base.link);
4813
4814 if (output->cursor_plane) {
4815 wl_list_remove(&output->cursor_plane->base.link);
4816 wl_list_init(&output->cursor_plane->base.link);
4817 /* Turn off hardware cursor */
4818 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
4819 }
Daniel Stone2ba17f42015-05-19 20:02:41 +01004820 }
Daniel Stone087ddf02017-02-14 17:51:30 +00004821
4822 unused = wl_array_add(&b->unused_connectors, sizeof(*unused));
4823 *unused = output->connector_id;
4824 unused = wl_array_add(&b->unused_crtcs, sizeof(*unused));
4825 *unused = output->crtc_id;
Pekka Paalanen5e12b552018-02-09 12:29:10 +02004826
4827 /* Force programming unused connectors and crtcs. */
4828 b->state_invalid = true;
Armin Krezović08368132016-09-30 14:11:05 +02004829}
4830
4831static void
4832drm_output_destroy(struct weston_output *base)
4833{
4834 struct drm_output *output = to_drm_output(base);
4835 struct drm_backend *b = to_drm_backend(base->compositor);
Armin Krezović08368132016-09-30 14:11:05 +02004836
Daniel Stone598ee9d2016-11-16 11:55:20 +00004837 if (output->page_flip_pending || output->vblank_pending ||
4838 output->atomic_complete_pending) {
Armin Krezović08368132016-09-30 14:11:05 +02004839 output->destroy_pending = 1;
4840 weston_log("destroy output while page flip pending\n");
4841 return;
4842 }
4843
4844 if (output->base.enabled)
4845 drm_output_deinit(&output->base);
4846
Pekka Paalanen383b3af2017-09-11 14:40:48 +03004847 drm_mode_list_destroy(b, &output->base.mode_list);
Armin Krezović445b41b2016-10-09 23:48:16 +02004848
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00004849 if (output->pageflip_timer)
4850 wl_event_source_remove(output->pageflip_timer);
4851
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03004852 weston_output_release(&output->base);
Armin Krezović08368132016-09-30 14:11:05 +02004853
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03004854 drm_output_fini_crtc(output);
Daniel Stone02cf4662017-03-03 16:19:39 +00004855
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03004856 drm_property_info_free(output->props_conn, WDRM_CONNECTOR__COUNT);
Armin Krezović08368132016-09-30 14:11:05 +02004857 drmModeFreeConnector(output->connector);
4858
4859 if (output->backlight)
4860 backlight_destroy(output->backlight);
4861
Daniel Stone7b2ddac2016-11-11 19:11:49 +00004862 assert(!output->state_last);
4863 drm_output_state_free(output->state_cur);
4864
Armin Krezović08368132016-09-30 14:11:05 +02004865 free(output);
4866}
4867
4868static int
4869drm_output_disable(struct weston_output *base)
4870{
4871 struct drm_output *output = to_drm_output(base);
Armin Krezović08368132016-09-30 14:11:05 +02004872
Daniel Stone598ee9d2016-11-16 11:55:20 +00004873 if (output->page_flip_pending || output->vblank_pending ||
4874 output->atomic_complete_pending) {
Armin Krezović08368132016-09-30 14:11:05 +02004875 output->disable_pending = 1;
4876 return -1;
4877 }
4878
Daniel Stonea08512f2016-11-08 17:46:10 +00004879 weston_log("Disabling output %s\n", output->base.name);
Daniel Stonea08512f2016-11-08 17:46:10 +00004880
Armin Krezović08368132016-09-30 14:11:05 +02004881 if (output->base.enabled)
4882 drm_output_deinit(&output->base);
4883
4884 output->disable_pending = 0;
4885
Armin Krezović08368132016-09-30 14:11:05 +02004886 return 0;
4887}
4888
4889/**
Daniel Stone087ddf02017-02-14 17:51:30 +00004890 * Update the list of unused connectors and CRTCs
4891 *
4892 * This keeps the unused_connectors and unused_crtcs arrays up to date.
4893 *
4894 * @param b Weston backend structure
4895 * @param resources DRM resources for this device
4896 */
4897static void
4898drm_backend_update_unused_outputs(struct drm_backend *b, drmModeRes *resources)
4899{
4900 int i;
4901
4902 wl_array_release(&b->unused_connectors);
4903 wl_array_init(&b->unused_connectors);
4904
4905 for (i = 0; i < resources->count_connectors; i++) {
4906 struct drm_output *output;
4907 uint32_t *connector_id;
4908
4909 output = drm_output_find_by_connector(b, resources->connectors[i]);
4910 if (output && output->base.enabled)
4911 continue;
4912
4913 connector_id = wl_array_add(&b->unused_connectors,
4914 sizeof(*connector_id));
4915 *connector_id = resources->connectors[i];
4916 }
4917
4918 wl_array_release(&b->unused_crtcs);
4919 wl_array_init(&b->unused_crtcs);
4920
4921 for (i = 0; i < resources->count_crtcs; i++) {
4922 struct drm_output *output;
4923 uint32_t *crtc_id;
4924
4925 output = drm_output_find_by_crtc(b, resources->crtcs[i]);
4926 if (output && output->base.enabled)
4927 continue;
4928
4929 crtc_id = wl_array_add(&b->unused_crtcs, sizeof(*crtc_id));
4930 *crtc_id = resources->crtcs[i];
4931 }
4932}
4933
4934/**
Armin Krezović08368132016-09-30 14:11:05 +02004935 * Create a Weston output structure
4936 *
4937 * Given a DRM connector, create a matching drm_output structure and add it
4938 * to Weston's output list. It also takes ownership of the connector, which
4939 * is released when output is destroyed.
4940 *
4941 * @param b Weston backend structure
4942 * @param resources DRM resources for this device
4943 * @param connector DRM connector to use for this new output
4944 * @param drm_device udev device pointer
4945 * @returns 0 on success, or -1 on failure
4946 */
4947static int
4948create_output_for_connector(struct drm_backend *b,
4949 drmModeRes *resources,
4950 drmModeConnector *connector,
4951 struct udev_device *drm_device)
4952{
4953 struct drm_output *output;
Daniel Stone02cf4662017-03-03 16:19:39 +00004954 drmModeObjectPropertiesPtr props;
Armin Krezović445b41b2016-10-09 23:48:16 +02004955 struct drm_mode *drm_mode;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03004956 char *name;
Pekka Paalanen6f1866b2017-04-03 14:22:51 +03004957 const char *make = "unknown";
4958 const char *model = "unknown";
4959 const char *serial_number = "unknown";
Armin Krezović08368132016-09-30 14:11:05 +02004960 int i;
4961
Armin Krezović08368132016-09-30 14:11:05 +02004962 output = zalloc(sizeof *output);
4963 if (output == NULL)
Daniel Stone62c0d632018-02-05 13:01:02 +00004964 goto err_init;
Armin Krezović08368132016-09-30 14:11:05 +02004965
4966 output->connector = connector;
Armin Krezović08368132016-09-30 14:11:05 +02004967 output->connector_id = connector->connector_id;
4968
4969 output->backlight = backlight_init(drm_device,
4970 connector->connector_type);
4971
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03004972 name = make_connector_name(connector);
4973 weston_output_init(&output->base, b->compositor, name);
4974 free(name);
4975
Armin Krezović08368132016-09-30 14:11:05 +02004976 output->base.enable = drm_output_enable;
4977 output->base.destroy = drm_output_destroy;
4978 output->base.disable = drm_output_disable;
Armin Krezović08368132016-09-30 14:11:05 +02004979
4980 output->destroy_pending = 0;
4981 output->disable_pending = 0;
Armin Krezović08368132016-09-30 14:11:05 +02004982
Pekka Paalanenfc5f5d72017-09-05 16:11:15 +03004983 if (drm_output_init_crtc(output, resources, connector) < 0)
Pekka Paalanencd011a62016-11-15 22:07:49 +00004984 goto err_output;
Pekka Paalanencd011a62016-11-15 22:07:49 +00004985
Daniel Stone02cf4662017-03-03 16:19:39 +00004986 props = drmModeObjectGetProperties(b->drm.fd, connector->connector_id,
4987 DRM_MODE_OBJECT_CONNECTOR);
4988 if (!props) {
4989 weston_log("failed to get connector properties\n");
Daniel Stone62c0d632018-02-05 13:01:02 +00004990 goto err_output;
Daniel Stone02cf4662017-03-03 16:19:39 +00004991 }
4992 drm_property_info_populate(b, connector_props, output->props_conn,
4993 WDRM_CONNECTOR__COUNT, props);
Pekka Paalanen6f1866b2017-04-03 14:22:51 +03004994 find_and_parse_output_edid(b, output, props,
4995 &make, &model, &serial_number);
4996 output->base.make = (char *)make;
4997 output->base.model = (char *)model;
4998 output->base.serial_number = (char *)serial_number;
Pekka Paalanena0bfedc2017-04-03 14:42:51 +03004999 output->base.subpixel = drm_subpixel_to_wayland(output->connector->subpixel);
5000
Daniel Stonec1d0f472017-01-27 15:11:33 +00005001 drmModeFreeObjectProperties(props);
5002
Pekka Paalanena0bfedc2017-04-03 14:42:51 +03005003 if (output->connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
5004 output->connector->connector_type == DRM_MODE_CONNECTOR_eDP)
5005 output->base.connection_internal = true;
5006
Pekka Paalanenc4db6f72017-09-05 16:37:03 +03005007 if (drm_output_init_gamma_size(output) < 0)
5008 goto err_output;
5009
Daniel Stone7b2ddac2016-11-11 19:11:49 +00005010 output->state_cur = drm_output_state_alloc(output, NULL);
5011
Pekka Paalanena0bfedc2017-04-03 14:42:51 +03005012 output->base.mm_width = output->connector->mmWidth;
5013 output->base.mm_height = output->connector->mmHeight;
5014
Armin Krezović445b41b2016-10-09 23:48:16 +02005015 for (i = 0; i < output->connector->count_modes; i++) {
5016 drm_mode = drm_output_add_mode(output, &output->connector->modes[i]);
5017 if (!drm_mode) {
Daniel Stone62c0d632018-02-05 13:01:02 +00005018 weston_log("failed to add mode\n");
5019 goto err_output;
Armin Krezović445b41b2016-10-09 23:48:16 +02005020 }
5021 }
5022
Armin Krezović08368132016-09-30 14:11:05 +02005023 weston_compositor_add_pending_output(&output->base, b->compositor);
5024
5025 return 0;
Armin Krezović445b41b2016-10-09 23:48:16 +02005026
Daniel Stone62c0d632018-02-05 13:01:02 +00005027err_output:
5028 drm_output_destroy(&output->base);
5029 return -1;
5030 /* no fallthrough! */
Armin Krezović445b41b2016-10-09 23:48:16 +02005031
Daniel Stone62c0d632018-02-05 13:01:02 +00005032err_init:
5033 drmModeFreeConnector(connector);
Armin Krezović445b41b2016-10-09 23:48:16 +02005034 return -1;
Armin Krezović08368132016-09-30 14:11:05 +02005035}
5036
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005037static int
Ucan, Emre (ADITG/SW1)9a200d72017-02-02 14:06:56 +00005038create_outputs(struct drm_backend *b, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005039{
5040 drmModeConnector *connector;
5041 drmModeRes *resources;
5042 int i;
5043
Giulio Camuffo954f1832014-10-11 18:27:30 +03005044 resources = drmModeGetResources(b->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005045 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02005046 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005047 return -1;
5048 }
5049
Giulio Camuffo954f1832014-10-11 18:27:30 +03005050 b->min_width = resources->min_width;
5051 b->max_width = resources->max_width;
5052 b->min_height = resources->min_height;
5053 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05005054
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005055 for (i = 0; i < resources->count_connectors; i++) {
Daniel Stone02cf4662017-03-03 16:19:39 +00005056 int ret;
5057
Giulio Camuffo954f1832014-10-11 18:27:30 +03005058 connector = drmModeGetConnector(b->drm.fd,
Benjamin Franzke117483d2011-08-30 11:38:26 +02005059 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005060 if (connector == NULL)
5061 continue;
5062
Ucan, Emre (ADITG/SW1)38ea2d22017-03-08 15:43:18 +00005063 if (connector->connection == DRM_MODE_CONNECTED) {
Daniel Stone02cf4662017-03-03 16:19:39 +00005064 ret = create_output_for_connector(b, resources,
5065 connector, drm_device);
5066 if (ret < 0)
5067 weston_log("failed to create new connector\n");
Armin Krezović08368132016-09-30 14:11:05 +02005068 } else {
5069 drmModeFreeConnector(connector);
Benjamin Franzke9eaee352011-08-02 13:03:54 +02005070 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005071 }
5072
Daniel Stone087ddf02017-02-14 17:51:30 +00005073 drm_backend_update_unused_outputs(b, resources);
5074
Armin Krezović08368132016-09-30 14:11:05 +02005075 if (wl_list_empty(&b->compositor->output_list) &&
5076 wl_list_empty(&b->compositor->pending_output_list))
Martin Minarik6d118362012-06-07 18:01:59 +02005077 weston_log("No currently active connector found.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005078
5079 drmModeFreeResources(resources);
5080
5081 return 0;
5082}
5083
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005084static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03005085update_outputs(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005086{
5087 drmModeConnector *connector;
5088 drmModeRes *resources;
5089 struct drm_output *output, *next;
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00005090 uint32_t *connected;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005091 int i;
5092
Giulio Camuffo954f1832014-10-11 18:27:30 +03005093 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005094 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02005095 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005096 return;
5097 }
5098
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00005099 connected = calloc(resources->count_connectors, sizeof(uint32_t));
5100 if (!connected) {
5101 drmModeFreeResources(resources);
5102 return;
5103 }
5104
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005105 /* collect new connects */
5106 for (i = 0; i < resources->count_connectors; i++) {
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +00005107 uint32_t connector_id = resources->connectors[i];
Benjamin Franzke117483d2011-08-30 11:38:26 +02005108
Giulio Camuffo954f1832014-10-11 18:27:30 +03005109 connector = drmModeGetConnector(b->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01005110 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005111 continue;
5112
David Herrmann7551cff2011-12-08 17:05:43 +01005113 if (connector->connection != DRM_MODE_CONNECTED) {
5114 drmModeFreeConnector(connector);
5115 continue;
5116 }
5117
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00005118 connected[i] = connector_id;
Benjamin Franzke117483d2011-08-30 11:38:26 +02005119
Daniel Stonec0ec7592017-02-09 13:58:35 +00005120 if (drm_output_find_by_connector(b, connector_id)) {
Armin Krezović08368132016-09-30 14:11:05 +02005121 drmModeFreeConnector(connector);
Daniel Stonec0ec7592017-02-09 13:58:35 +00005122 continue;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005123 }
Daniel Stonec0ec7592017-02-09 13:58:35 +00005124
5125 create_output_for_connector(b, resources,
5126 connector, drm_device);
5127 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005128 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005129
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00005130 wl_list_for_each_safe(output, next, &b->compositor->output_list,
5131 base.link) {
5132 bool disconnected = true;
5133
5134 for (i = 0; i < resources->count_connectors; i++) {
5135 if (connected[i] == output->connector_id) {
5136 disconnected = false;
5137 break;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005138 }
5139 }
Armin Krezović08368132016-09-30 14:11:05 +02005140
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00005141 if (!disconnected)
5142 continue;
5143
5144 weston_log("connector %d disconnected\n", output->connector_id);
5145 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005146 }
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00005147
5148 wl_list_for_each_safe(output, next, &b->compositor->pending_output_list,
5149 base.link) {
5150 bool disconnected = true;
5151
5152 for (i = 0; i < resources->count_connectors; i++) {
5153 if (connected[i] == output->connector_id) {
5154 disconnected = false;
5155 break;
5156 }
5157 }
5158
5159 if (!disconnected)
5160 continue;
5161
5162 weston_log("connector %d disconnected\n", output->connector_id);
5163 drm_output_destroy(&output->base);
5164 }
5165
Daniel Stone087ddf02017-02-14 17:51:30 +00005166 drm_backend_update_unused_outputs(b, resources);
5167
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00005168 free(connected);
5169 drmModeFreeResources(resources);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005170}
5171
5172static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03005173udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005174{
David Herrmannd7488c22012-03-11 20:05:21 +01005175 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01005176 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01005177
5178 sysnum = udev_device_get_sysnum(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005179 if (!sysnum || atoi(sysnum) != b->drm.id)
David Herrmannd7488c22012-03-11 20:05:21 +01005180 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02005181
David Herrmann6ac52db2012-03-11 20:05:22 +01005182 val = udev_device_get_property_value(device, "HOTPLUG");
5183 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005184 return 0;
5185
David Herrmann6ac52db2012-03-11 20:05:22 +01005186 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005187}
5188
Kristian Høgsbergb1868472011-04-22 12:27:57 -04005189static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005190udev_drm_event(int fd, uint32_t mask, void *data)
5191{
Giulio Camuffo954f1832014-10-11 18:27:30 +03005192 struct drm_backend *b = data;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005193 struct udev_device *event;
5194
Giulio Camuffo954f1832014-10-11 18:27:30 +03005195 event = udev_monitor_receive_device(b->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02005196
Giulio Camuffo954f1832014-10-11 18:27:30 +03005197 if (udev_event_is_hotplug(b, event))
5198 update_outputs(b, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005199
5200 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04005201
5202 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005203}
5204
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005205static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005206drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005207{
Armin Krezović545dba62016-08-05 15:54:18 +02005208 struct drm_backend *b = to_drm_backend(ec);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005209
Giulio Camuffo954f1832014-10-11 18:27:30 +03005210 udev_input_destroy(&b->input);
Jonas Ådahlc97af922012-03-28 22:36:09 +02005211
Giulio Camuffo954f1832014-10-11 18:27:30 +03005212 wl_event_source_remove(b->udev_drm_source);
5213 wl_event_source_remove(b->drm_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02005214
Daniel Stoneb57c6a02017-10-05 16:27:21 +01005215 b->shutting_down = true;
5216
Giulio Camuffo954f1832014-10-11 18:27:30 +03005217 destroy_sprites(b);
Kristian Høgsberg3d64a3e2013-05-10 12:36:04 -04005218
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03005219 weston_compositor_shutdown(ec);
5220
Giulio Camuffo954f1832014-10-11 18:27:30 +03005221 if (b->gbm)
5222 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02005223
Pekka Paalanen5b0aa552017-12-07 16:06:05 +02005224 udev_monitor_unref(b->udev_monitor);
Pekka Paalanen2a0c6c32017-09-13 16:48:01 +03005225 udev_unref(b->udev);
5226
Giulio Camuffo954f1832014-10-11 18:27:30 +03005227 weston_launcher_destroy(ec->launcher);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005228
Daniel Stone087ddf02017-02-14 17:51:30 +00005229 wl_array_release(&b->unused_crtcs);
5230 wl_array_release(&b->unused_connectors);
5231
Giulio Camuffo954f1832014-10-11 18:27:30 +03005232 close(b->drm.fd);
Pekka Paalanen9bf4f372017-12-07 16:05:29 +02005233 free(b->drm.filename);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005234 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005235}
5236
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04005237static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -07005238session_notify(struct wl_listener *listener, void *data)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04005239{
Kristian Høgsberg61741a22013-09-17 16:02:57 -07005240 struct weston_compositor *compositor = data;
Armin Krezović545dba62016-08-05 15:54:18 +02005241 struct drm_backend *b = to_drm_backend(compositor);
Daniel Stone085d2b92015-05-21 00:00:57 +01005242 struct drm_plane *plane;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04005243 struct drm_output *output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04005244
Giulio Camuffo954f1832014-10-11 18:27:30 +03005245 if (compositor->session_active) {
Kristian Høgsberg61741a22013-09-17 16:02:57 -07005246 weston_log("activating session\n");
Daniel Stonef33e1042016-11-05 08:10:13 +00005247 weston_compositor_wake(compositor);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005248 weston_compositor_damage_all(compositor);
Daniel Stone6020f472018-02-05 15:46:20 +00005249 b->state_invalid = true;
Giulio Camuffo954f1832014-10-11 18:27:30 +03005250 udev_input_enable(&b->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07005251 } else {
5252 weston_log("deactivating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03005253 udev_input_disable(&b->input);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04005254
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005255 weston_compositor_offscreen(compositor);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04005256
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05005257 /* If we have a repaint scheduled (either from a
5258 * pending pageflip or the idle handler), make sure we
5259 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005260 * vt switched away. The OFFSCREEN state will prevent
Abdur Rehman4dca0e12017-01-01 19:46:35 +05005261 * further attempts at repainting. When we switch
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05005262 * back, we schedule a repaint, which will process
5263 * pending frame callbacks. */
5264
Giulio Camuffo954f1832014-10-11 18:27:30 +03005265 wl_list_for_each(output, &compositor->output_list, base.link) {
Daniel Stone09a97e22017-03-01 11:34:06 +00005266 output->base.repaint_needed = false;
Daniel Stone2ba17f42015-05-19 20:02:41 +01005267 if (output->cursor_plane)
5268 drmModeSetCursor(b->drm.fd, output->crtc_id,
5269 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05005270 }
5271
Giulio Camuffo954f1832014-10-11 18:27:30 +03005272 output = container_of(compositor->output_list.next,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04005273 struct drm_output, base.link);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05005274
Daniel Stone085d2b92015-05-21 00:00:57 +01005275 wl_list_for_each(plane, &b->plane_list, link) {
5276 if (plane->type != WDRM_PLANE_TYPE_OVERLAY)
5277 continue;
5278
Giulio Camuffo954f1832014-10-11 18:27:30 +03005279 drmModeSetPlane(b->drm.fd,
Daniel Stone085d2b92015-05-21 00:00:57 +01005280 plane->plane_id,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04005281 output->crtc_id, 0, 0,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05005282 0, 0, 0, 0, 0, 0, 0, 0);
Daniel Stone085d2b92015-05-21 00:00:57 +01005283 }
5284 }
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04005285}
5286
Daniel Stoneefa504f2016-12-19 16:48:20 +00005287/**
5288 * Determines whether or not a device is capable of modesetting. If successful,
5289 * sets b->drm.fd and b->drm.filename to the opened device.
5290 */
5291static bool
5292drm_device_is_kms(struct drm_backend *b, struct udev_device *device)
5293{
5294 const char *filename = udev_device_get_devnode(device);
5295 const char *sysnum = udev_device_get_sysnum(device);
5296 drmModeRes *res;
5297 int id, fd;
5298
5299 if (!filename)
5300 return false;
5301
5302 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
5303 if (fd < 0)
5304 return false;
5305
5306 res = drmModeGetResources(fd);
5307 if (!res)
5308 goto out_fd;
5309
5310 if (res->count_crtcs <= 0 || res->count_connectors <= 0 ||
5311 res->count_encoders <= 0)
5312 goto out_res;
5313
5314 if (sysnum)
5315 id = atoi(sysnum);
5316 if (!sysnum || id < 0) {
5317 weston_log("couldn't get sysnum for device %s\n", filename);
5318 goto out_res;
5319 }
5320
5321 /* We can be called successfully on multiple devices; if we have,
5322 * clean up old entries. */
5323 if (b->drm.fd >= 0)
5324 weston_launcher_close(b->compositor->launcher, b->drm.fd);
5325 free(b->drm.filename);
5326
5327 b->drm.fd = fd;
5328 b->drm.id = id;
5329 b->drm.filename = strdup(filename);
5330
Sergi Granellceb59812017-03-28 12:44:04 +02005331 drmModeFreeResources(res);
5332
Daniel Stoneefa504f2016-12-19 16:48:20 +00005333 return true;
5334
5335out_res:
5336 drmModeFreeResources(res);
5337out_fd:
5338 weston_launcher_close(b->compositor->launcher, fd);
5339 return false;
5340}
5341
David Herrmann0af066f2012-10-29 19:21:16 +01005342/*
5343 * Find primary GPU
5344 * Some systems may have multiple DRM devices attached to a single seat. This
5345 * function loops over all devices and tries to find a PCI device with the
5346 * boot_vga sysfs attribute set to 1.
5347 * If no such device is found, the first DRM device reported by udev is used.
Daniel Stoneefa504f2016-12-19 16:48:20 +00005348 * Devices are also vetted to make sure they are are capable of modesetting,
5349 * rather than pure render nodes (GPU with no display), or pure
5350 * memory-allocation devices (VGEM).
David Herrmann0af066f2012-10-29 19:21:16 +01005351 */
5352static struct udev_device*
Giulio Camuffo954f1832014-10-11 18:27:30 +03005353find_primary_gpu(struct drm_backend *b, const char *seat)
David Herrmann0af066f2012-10-29 19:21:16 +01005354{
5355 struct udev_enumerate *e;
5356 struct udev_list_entry *entry;
5357 const char *path, *device_seat, *id;
5358 struct udev_device *device, *drm_device, *pci;
5359
Giulio Camuffo954f1832014-10-11 18:27:30 +03005360 e = udev_enumerate_new(b->udev);
David Herrmann0af066f2012-10-29 19:21:16 +01005361 udev_enumerate_add_match_subsystem(e, "drm");
5362 udev_enumerate_add_match_sysname(e, "card[0-9]*");
5363
5364 udev_enumerate_scan_devices(e);
5365 drm_device = NULL;
5366 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Daniel Stoneefa504f2016-12-19 16:48:20 +00005367 bool is_boot_vga = false;
5368
David Herrmann0af066f2012-10-29 19:21:16 +01005369 path = udev_list_entry_get_name(entry);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005370 device = udev_device_new_from_syspath(b->udev, path);
David Herrmann0af066f2012-10-29 19:21:16 +01005371 if (!device)
5372 continue;
5373 device_seat = udev_device_get_property_value(device, "ID_SEAT");
5374 if (!device_seat)
5375 device_seat = default_seat;
5376 if (strcmp(device_seat, seat)) {
5377 udev_device_unref(device);
5378 continue;
5379 }
5380
5381 pci = udev_device_get_parent_with_subsystem_devtype(device,
5382 "pci", NULL);
5383 if (pci) {
5384 id = udev_device_get_sysattr_value(pci, "boot_vga");
Daniel Stoneefa504f2016-12-19 16:48:20 +00005385 if (id && !strcmp(id, "1"))
5386 is_boot_vga = true;
David Herrmann0af066f2012-10-29 19:21:16 +01005387 }
5388
Daniel Stoneefa504f2016-12-19 16:48:20 +00005389 /* If we already have a modesetting-capable device, and this
5390 * device isn't our boot-VGA device, we aren't going to use
5391 * it. */
5392 if (!is_boot_vga && drm_device) {
David Herrmann0af066f2012-10-29 19:21:16 +01005393 udev_device_unref(device);
Daniel Stoneefa504f2016-12-19 16:48:20 +00005394 continue;
5395 }
5396
5397 /* Make sure this device is actually capable of modesetting;
5398 * if this call succeeds, b->drm.{fd,filename} will be set,
5399 * and any old values freed. */
5400 if (!drm_device_is_kms(b, device)) {
5401 udev_device_unref(device);
5402 continue;
5403 }
5404
5405 /* There can only be one boot_vga device, and we try to use it
5406 * at all costs. */
5407 if (is_boot_vga) {
5408 if (drm_device)
5409 udev_device_unref(drm_device);
5410 drm_device = device;
5411 break;
5412 }
5413
5414 /* Per the (!is_boot_vga && drm_device) test above, we only
5415 * trump existing saved devices with boot-VGA devices, so if
5416 * we end up here, this must be the first device we've seen. */
5417 assert(!drm_device);
5418 drm_device = device;
David Herrmann0af066f2012-10-29 19:21:16 +01005419 }
5420
Daniel Stoneefa504f2016-12-19 16:48:20 +00005421 /* If we're returning a device to use, we must have an open FD for
5422 * it. */
5423 assert(!!drm_device == (b->drm.fd >= 0));
5424
David Herrmann0af066f2012-10-29 19:21:16 +01005425 udev_enumerate_unref(e);
5426 return drm_device;
5427}
5428
Pekka Paalanenb45ed8b2017-03-28 18:04:27 +03005429static struct udev_device *
5430open_specific_drm_device(struct drm_backend *b, const char *name)
5431{
5432 struct udev_device *device;
5433
5434 device = udev_device_new_from_subsystem_sysname(b->udev, "drm", name);
5435 if (!device) {
5436 weston_log("ERROR: could not open DRM device '%s'\n", name);
5437 return NULL;
5438 }
5439
5440 if (!drm_device_is_kms(b, device)) {
5441 udev_device_unref(device);
5442 weston_log("ERROR: DRM device '%s' is not a KMS device.\n", name);
5443 return NULL;
5444 }
5445
5446 /* If we're returning a device to use, we must have an open FD for
5447 * it. */
5448 assert(b->drm.fd >= 0);
5449
5450 return device;
5451}
5452
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02005453static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02005454planes_binding(struct weston_keyboard *keyboard, const struct timespec *time,
5455 uint32_t key, void *data)
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02005456{
Giulio Camuffo954f1832014-10-11 18:27:30 +03005457 struct drm_backend *b = data;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02005458
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02005459 switch (key) {
5460 case KEY_C:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005461 b->cursors_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02005462 break;
5463 case KEY_V:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005464 b->sprites_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02005465 break;
5466 case KEY_O:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005467 b->sprites_hidden ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02005468 break;
5469 default:
5470 break;
5471 }
Ander Conselvan de Oliveira180f42a2012-11-21 15:11:37 +02005472}
5473
Kristian Høgsberg0eac34a2013-08-30 14:28:22 -07005474#ifdef BUILD_VAAPI_RECORDER
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005475static void
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03005476recorder_destroy(struct drm_output *output)
5477{
5478 vaapi_recorder_destroy(output->recorder);
5479 output->recorder = NULL;
5480
5481 output->base.disable_planes--;
5482
5483 wl_list_remove(&output->recorder_frame_listener.link);
5484 weston_log("[libva recorder] done\n");
5485}
5486
5487static void
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005488recorder_frame_notify(struct wl_listener *listener, void *data)
5489{
5490 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03005491 struct drm_backend *b;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005492 int fd, ret;
5493
5494 output = container_of(listener, struct drm_output,
5495 recorder_frame_listener);
Armin Krezović545dba62016-08-05 15:54:18 +02005496 b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005497
5498 if (!output->recorder)
5499 return;
5500
Daniel Stonee2e80132018-01-16 15:37:33 +00005501 ret = drmPrimeHandleToFD(b->drm.fd,
5502 output->scanout_plane->state_cur->fb->handle,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005503 DRM_CLOEXEC, &fd);
5504 if (ret) {
5505 weston_log("[libva recorder] "
5506 "failed to create prime fd for front buffer\n");
5507 return;
5508 }
5509
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03005510 ret = vaapi_recorder_frame(output->recorder, fd,
Daniel Stonee2e80132018-01-16 15:37:33 +00005511 output->scanout_plane->state_cur->fb->stride);
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03005512 if (ret < 0) {
5513 weston_log("[libva recorder] aborted: %m\n");
5514 recorder_destroy(output);
5515 }
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005516}
5517
5518static void *
Giulio Camuffo954f1832014-10-11 18:27:30 +03005519create_recorder(struct drm_backend *b, int width, int height,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005520 const char *filename)
5521{
5522 int fd;
5523 drm_magic_t magic;
5524
Giulio Camuffo954f1832014-10-11 18:27:30 +03005525 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005526 if (fd < 0)
5527 return NULL;
5528
5529 drmGetMagic(fd, &magic);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005530 drmAuthMagic(b->drm.fd, magic);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005531
5532 return vaapi_recorder_create(fd, width, height, filename);
5533}
5534
5535static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02005536recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time,
5537 uint32_t key, void *data)
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005538{
Giulio Camuffo954f1832014-10-11 18:27:30 +03005539 struct drm_backend *b = data;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005540 struct drm_output *output;
5541 int width, height;
5542
Giulio Camuffo954f1832014-10-11 18:27:30 +03005543 output = container_of(b->compositor->output_list.next,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005544 struct drm_output, base.link);
5545
5546 if (!output->recorder) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01005547 if (output->gbm_format != GBM_FORMAT_XRGB8888) {
Ander Conselvan de Oliveira2ef1cd12014-05-06 16:49:06 +03005548 weston_log("failed to start vaapi recorder: "
5549 "output format not supported\n");
5550 return;
5551 }
5552
Hardeningff39efa2013-09-18 23:56:35 +02005553 width = output->base.current_mode->width;
5554 height = output->base.current_mode->height;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005555
5556 output->recorder =
Giulio Camuffo954f1832014-10-11 18:27:30 +03005557 create_recorder(b, width, height, "capture.h264");
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005558 if (!output->recorder) {
5559 weston_log("failed to create vaapi recorder\n");
5560 return;
5561 }
5562
5563 output->base.disable_planes++;
5564
5565 output->recorder_frame_listener.notify = recorder_frame_notify;
5566 wl_signal_add(&output->base.frame_signal,
5567 &output->recorder_frame_listener);
5568
5569 weston_output_schedule_repaint(&output->base);
5570
5571 weston_log("[libva recorder] initialized\n");
5572 } else {
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03005573 recorder_destroy(output);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005574 }
5575}
5576#else
5577static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02005578recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time,
5579 uint32_t key, void *data)
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005580{
5581 weston_log("Compiled without libva support\n");
5582}
5583#endif
5584
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005585static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03005586switch_to_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005587{
5588 struct drm_output *output;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03005589 bool dmabuf_support_inited;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005590
Giulio Camuffo954f1832014-10-11 18:27:30 +03005591 if (!b->use_pixman)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005592 return;
5593
Pekka Paalanene4d231e2014-06-12 15:12:48 +03005594 dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf;
5595
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005596 weston_log("Switching to GL renderer\n");
5597
Giulio Camuffo954f1832014-10-11 18:27:30 +03005598 b->gbm = create_gbm_device(b->drm.fd);
5599 if (!b->gbm) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005600 weston_log("Failed to create gbm device. "
5601 "Aborting renderer switch\n");
5602 return;
5603 }
5604
Giulio Camuffo954f1832014-10-11 18:27:30 +03005605 wl_list_for_each(output, &b->compositor->output_list, base.link)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005606 pixman_renderer_output_destroy(&output->base);
5607
Giulio Camuffo954f1832014-10-11 18:27:30 +03005608 b->compositor->renderer->destroy(b->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005609
Giulio Camuffo954f1832014-10-11 18:27:30 +03005610 if (drm_backend_create_gl_renderer(b) < 0) {
5611 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005612 weston_log("Failed to create GL renderer. Quitting.\n");
5613 /* FIXME: we need a function to shutdown cleanly */
5614 assert(0);
5615 }
5616
Giulio Camuffo954f1832014-10-11 18:27:30 +03005617 wl_list_for_each(output, &b->compositor->output_list, base.link)
5618 drm_output_init_egl(output, b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005619
Giulio Camuffo954f1832014-10-11 18:27:30 +03005620 b->use_pixman = 0;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03005621
5622 if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) {
5623 if (linux_dmabuf_setup(b->compositor) < 0)
5624 weston_log("Error: initializing dmabuf "
5625 "support failed.\n");
5626 }
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005627}
5628
5629static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02005630renderer_switch_binding(struct weston_keyboard *keyboard,
5631 const struct timespec *time, uint32_t key, void *data)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005632{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005633 struct drm_backend *b =
Armin Krezović545dba62016-08-05 15:54:18 +02005634 to_drm_backend(keyboard->seat->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005635
Giulio Camuffo954f1832014-10-11 18:27:30 +03005636 switch_to_gl_renderer(b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005637}
5638
Armin Krezović08368132016-09-30 14:11:05 +02005639static const struct weston_drm_output_api api = {
5640 drm_output_set_mode,
5641 drm_output_set_gbm_format,
5642 drm_output_set_seat,
5643};
5644
Giulio Camuffo954f1832014-10-11 18:27:30 +03005645static struct drm_backend *
5646drm_backend_create(struct weston_compositor *compositor,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005647 struct weston_drm_backend_config *config)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005648{
Giulio Camuffo954f1832014-10-11 18:27:30 +03005649 struct drm_backend *b;
David Herrmann0af066f2012-10-29 19:21:16 +01005650 struct udev_device *drm_device;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005651 struct wl_event_loop *loop;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005652 const char *seat_id = default_seat;
Armin Krezović08368132016-09-30 14:11:05 +02005653 int ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005654
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04005655 weston_log("initializing drm backend\n");
5656
Giulio Camuffo954f1832014-10-11 18:27:30 +03005657 b = zalloc(sizeof *b);
5658 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005659 return NULL;
Daniel Stone725c2c32012-06-22 14:04:36 +01005660
Daniel Stone6020f472018-02-05 15:46:20 +00005661 b->state_invalid = true;
Daniel Stoneefa504f2016-12-19 16:48:20 +00005662 b->drm.fd = -1;
Daniel Stone087ddf02017-02-14 17:51:30 +00005663 wl_array_init(&b->unused_crtcs);
5664 wl_array_init(&b->unused_connectors);
Daniel Stoneefa504f2016-12-19 16:48:20 +00005665
Pekka Paalanen68583832015-05-19 09:53:16 +03005666 /*
5667 * KMS support for hardware planes cannot properly synchronize
5668 * without nuclear page flip. Without nuclear/atomic, hw plane
5669 * and cursor plane updates would either tear or cause extra
5670 * waits for vblanks which means dropping the compositor framerate
Pekka Paalanen3f32a132015-09-07 15:38:43 +03005671 * to a fraction. For cursors, it's not so bad, so they are
5672 * enabled.
Pekka Paalanen68583832015-05-19 09:53:16 +03005673 *
5674 * These can be enabled again when nuclear/atomic support lands.
5675 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03005676 b->sprites_are_broken = 1;
Giulio Camuffo954f1832014-10-11 18:27:30 +03005677 b->compositor = compositor;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005678 b->use_pixman = config->use_pixman;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00005679 b->pageflip_timeout = config->pageflip_timeout;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07005680
Pekka Paalanen7da9a382017-08-30 11:29:49 +03005681 compositor->backend = &b->base;
5682
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005683 if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0)
5684 goto err_compositor;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07005685
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005686 if (config->seat_id)
5687 seat_id = config->seat_id;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02005688
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01005689 /* Check if we run drm-backend using weston-launch */
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005690 compositor->launcher = weston_launcher_connect(compositor, config->tty,
5691 seat_id, true);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005692 if (compositor->launcher == NULL) {
Pekka Paalanena453f4d2017-10-31 10:19:48 +02005693 weston_log("fatal: drm backend should be run using "
5694 "weston-launch binary, or your system should "
5695 "provide the logind D-Bus API.\n");
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01005696 goto err_compositor;
5697 }
5698
Giulio Camuffo954f1832014-10-11 18:27:30 +03005699 b->udev = udev_new();
5700 if (b->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02005701 weston_log("failed to initialize udev context\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07005702 goto err_launcher;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005703 }
5704
Giulio Camuffo954f1832014-10-11 18:27:30 +03005705 b->session_listener.notify = session_notify;
5706 wl_signal_add(&compositor->session_signal, &b->session_listener);
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05005707
Pekka Paalanenb45ed8b2017-03-28 18:04:27 +03005708 if (config->specific_device)
5709 drm_device = open_specific_drm_device(b, config->specific_device);
5710 else
5711 drm_device = find_primary_gpu(b, seat_id);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04005712 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02005713 weston_log("no drm device found\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07005714 goto err_udev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005715 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005716
Daniel Stoneefa504f2016-12-19 16:48:20 +00005717 if (init_kms_caps(b) < 0) {
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02005718 weston_log("failed to initialize kms\n");
5719 goto err_udev_dev;
5720 }
5721
Giulio Camuffo954f1832014-10-11 18:27:30 +03005722 if (b->use_pixman) {
5723 if (init_pixman(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02005724 weston_log("failed to initialize pixman renderer\n");
5725 goto err_udev_dev;
5726 }
5727 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +03005728 if (init_egl(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02005729 weston_log("failed to initialize egl\n");
5730 goto err_udev_dev;
5731 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005732 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05005733
Giulio Camuffo954f1832014-10-11 18:27:30 +03005734 b->base.destroy = drm_destroy;
Daniel Stoneeedf84c2017-02-10 18:06:04 +00005735 b->base.repaint_begin = drm_repaint_begin;
5736 b->base.repaint_flush = drm_repaint_flush;
5737 b->base.repaint_cancel = drm_repaint_cancel;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02005738
Bob Ham91880f12016-01-12 10:21:47 +00005739 weston_setup_vt_switch_bindings(compositor);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04005740
Daniel Stone085d2b92015-05-21 00:00:57 +01005741 wl_list_init(&b->plane_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005742 create_sprites(b);
Jesse Barnes58ef3792012-02-23 09:45:49 -05005743
Giulio Camuffo954f1832014-10-11 18:27:30 +03005744 if (udev_input_init(&b->input,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03005745 compositor, b->udev, seat_id,
5746 config->configure_device) < 0) {
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03005747 weston_log("failed to create input devices\n");
5748 goto err_sprite;
5749 }
5750
Ucan, Emre (ADITG/SW1)9a200d72017-02-02 14:06:56 +00005751 if (create_outputs(b, drm_device) < 0) {
Daniel Stoneefa504f2016-12-19 16:48:20 +00005752 weston_log("failed to create output for %s\n", b->drm.filename);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03005753 goto err_udev_input;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005754 }
5755
Jason Ekstrand9fc71512014-04-02 19:53:46 -05005756 /* A this point we have some idea of whether or not we have a working
5757 * cursor plane. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03005758 if (!b->cursors_are_broken)
5759 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
Jason Ekstrand9fc71512014-04-02 19:53:46 -05005760
Giulio Camuffo954f1832014-10-11 18:27:30 +03005761 loop = wl_display_get_event_loop(compositor->wl_display);
5762 b->drm_source =
5763 wl_event_loop_add_fd(loop, b->drm.fd,
5764 WL_EVENT_READABLE, on_drm_input, b);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005765
Giulio Camuffo954f1832014-10-11 18:27:30 +03005766 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
5767 if (b->udev_monitor == NULL) {
Abdur Rehman4dca0e12017-01-01 19:46:35 +05005768 weston_log("failed to initialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01005769 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005770 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03005771 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005772 "drm", NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005773 b->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02005774 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03005775 udev_monitor_get_fd(b->udev_monitor),
5776 WL_EVENT_READABLE, udev_drm_event, b);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005777
Giulio Camuffo954f1832014-10-11 18:27:30 +03005778 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02005779 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01005780 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005781 }
5782
Daniel Stonea96b93c2012-06-22 14:04:37 +01005783 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01005784
Giulio Camuffo954f1832014-10-11 18:27:30 +03005785 weston_compositor_add_debug_binding(compositor, KEY_O,
5786 planes_binding, b);
5787 weston_compositor_add_debug_binding(compositor, KEY_C,
5788 planes_binding, b);
5789 weston_compositor_add_debug_binding(compositor, KEY_V,
5790 planes_binding, b);
5791 weston_compositor_add_debug_binding(compositor, KEY_Q,
5792 recorder_binding, b);
5793 weston_compositor_add_debug_binding(compositor, KEY_W,
5794 renderer_switch_binding, b);
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02005795
Pekka Paalanene4d231e2014-06-12 15:12:48 +03005796 if (compositor->renderer->import_dmabuf) {
5797 if (linux_dmabuf_setup(compositor) < 0)
5798 weston_log("Error: initializing dmabuf "
5799 "support failed.\n");
5800 }
5801
Armin Krezović08368132016-09-30 14:11:05 +02005802 ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME,
5803 &api, sizeof(api));
5804
5805 if (ret < 0) {
5806 weston_log("Failed to register output API.\n");
5807 goto err_udev_monitor;
5808 }
5809
Giulio Camuffo954f1832014-10-11 18:27:30 +03005810 return b;
Daniel Stonea96b93c2012-06-22 14:04:37 +01005811
5812err_udev_monitor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005813 wl_event_source_remove(b->udev_drm_source);
5814 udev_monitor_unref(b->udev_monitor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01005815err_drm_source:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005816 wl_event_source_remove(b->drm_source);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03005817err_udev_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005818 udev_input_destroy(&b->input);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04005819err_sprite:
Emmanuel Gil Peyrotb8347e32016-05-02 22:40:13 +01005820 if (b->gbm)
5821 gbm_device_destroy(b->gbm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005822 destroy_sprites(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01005823err_udev_dev:
5824 udev_device_unref(drm_device);
Kristian Høgsberg3f495872013-09-18 23:00:17 -07005825err_launcher:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005826 weston_launcher_destroy(compositor->launcher);
Daniel Stonea96b93c2012-06-22 14:04:37 +01005827err_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005828 udev_unref(b->udev);
Daniel Stonea96b93c2012-06-22 14:04:37 +01005829err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005830 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005831 free(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01005832 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005833}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005834
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005835static void
5836config_init_to_defaults(struct weston_drm_backend_config *config)
5837{
5838}
5839
Giulio Camuffo954f1832014-10-11 18:27:30 +03005840WL_EXPORT int
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01005841weston_backend_init(struct weston_compositor *compositor,
5842 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005843{
Giulio Camuffo954f1832014-10-11 18:27:30 +03005844 struct drm_backend *b;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005845 struct weston_drm_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005846
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005847 if (config_base == NULL ||
5848 config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION ||
5849 config_base->struct_size > sizeof(struct weston_drm_backend_config)) {
5850 weston_log("drm backend config structure is invalid\n");
5851 return -1;
5852 }
Benjamin Franzke117483d2011-08-30 11:38:26 +02005853
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005854 config_init_to_defaults(&config);
5855 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07005856
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005857 b = drm_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005858 if (b == NULL)
5859 return -1;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005860
Giulio Camuffo954f1832014-10-11 18:27:30 +03005861 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005862}