blob: 85fef2af8e5a14f41dd7707a7137a9c2c44fd364 [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/**
Pekka Paalanenc5de57f2015-05-20 23:01:44 +010087 * List of properties attached to DRM planes
88 */
89enum wdrm_plane_property {
90 WDRM_PLANE_TYPE = 0,
91 WDRM_PLANE__COUNT
92};
93
94/**
95 * Possible values for the WDRM_PLANE_TYPE property.
96 */
97enum wdrm_plane_type {
98 WDRM_PLANE_TYPE_PRIMARY = 0,
99 WDRM_PLANE_TYPE_CURSOR,
100 WDRM_PLANE_TYPE_OVERLAY,
101 WDRM_PLANE_TYPE__COUNT
102};
103
104/**
Daniel Stone02cf4662017-03-03 16:19:39 +0000105 * List of properties attached to a DRM connector
106 */
107enum wdrm_connector_property {
108 WDRM_CONNECTOR_EDID = 0,
109 WDRM_CONNECTOR_DPMS,
110 WDRM_CONNECTOR__COUNT
111};
112
113/**
114 * Represents the values of an enum-type KMS property
115 */
116struct drm_property_enum_info {
117 const char *name; /**< name as string (static, not freed) */
118 bool valid; /**< true if value is supported; ignore if false */
119 uint64_t value; /**< raw value */
120};
121
122/**
123 * Holds information on a DRM property, including its ID and the enum
124 * values it holds.
125 *
126 * DRM properties are allocated dynamically, and maintained as DRM objects
127 * within the normal object ID space; they thus do not have a stable ID
128 * to refer to. This includes enum values, which must be referred to by
129 * integer values, but these are not stable.
130 *
131 * drm_property_info allows a cache to be maintained where Weston can use
132 * enum values internally to refer to properties, with the mapping to DRM
133 * ID values being maintained internally.
134 */
135struct drm_property_info {
136 const char *name; /**< name as string (static, not freed) */
137 uint32_t prop_id; /**< KMS property object ID */
138 unsigned int num_enum_values; /**< number of enum values */
139 struct drm_property_enum_info *enum_values; /**< array of enum values */
140};
141
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000142/**
143 * Mode for drm_output_state_duplicate.
144 */
145enum drm_output_state_duplicate_mode {
146 DRM_OUTPUT_STATE_CLEAR_PLANES, /**< reset all planes to off */
147 DRM_OUTPUT_STATE_PRESERVE_PLANES, /**< preserve plane state */
148};
149
150/**
151 * Mode for drm_pending_state_apply and co.
152 */
153enum drm_state_apply_mode {
154 DRM_STATE_APPLY_SYNC, /**< state fully processed */
155 DRM_STATE_APPLY_ASYNC, /**< state pending event delivery */
156};
157
Giulio Camuffo954f1832014-10-11 18:27:30 +0300158struct drm_backend {
159 struct weston_backend base;
160 struct weston_compositor *compositor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400161
162 struct udev *udev;
163 struct wl_event_source *drm_source;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400164
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100165 struct udev_monitor *udev_monitor;
166 struct wl_event_source *udev_drm_source;
167
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100168 struct {
David Herrmannd7488c22012-03-11 20:05:21 +0100169 int id;
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100170 int fd;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300171 char *filename;
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100172 } drm;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200173 struct gbm_device *gbm;
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700174 struct wl_listener session_listener;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100175 uint32_t gbm_format;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200176
Rob Clark4339add2012-08-09 14:18:28 -0500177 /* we need these parameters in order to not fail drmModeAddFB2()
178 * due to out of bounds dimensions, and then mistakenly set
179 * sprites_are_broken:
180 */
Daniel Stonef214fdc2016-11-14 17:43:57 +0000181 int min_width, max_width;
182 int min_height, max_height;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200183 int no_addfb2;
Rob Clark4339add2012-08-09 14:18:28 -0500184
Daniel Stone085d2b92015-05-21 00:00:57 +0100185 struct wl_list plane_list;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500186 int sprites_are_broken;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +0200187 int sprites_hidden;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500188
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000189 void *repaint_data;
190
Daniel Stone6020f472018-02-05 15:46:20 +0000191 bool state_invalid;
192
Daniel Stone087ddf02017-02-14 17:51:30 +0000193 /* Connector and CRTC IDs not used by any enabled output. */
194 struct wl_array unused_connectors;
195 struct wl_array unused_crtcs;
196
Rob Clarkab5b1e32012-08-09 13:24:45 -0500197 int cursors_are_broken;
198
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100199 bool universal_planes;
200
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200201 int use_pixman;
202
Rob Bradfordd355b802013-05-31 18:09:55 +0100203 struct udev_input input;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -0300204
Daniel Stone70d337d2015-06-16 18:42:23 +0100205 int32_t cursor_width;
206 int32_t cursor_height;
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +0000207
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000208 uint32_t pageflip_timeout;
Daniel Stoneb57c6a02017-10-05 16:27:21 +0100209
210 bool shutting_down;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400211};
212
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400213struct drm_mode {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500214 struct weston_mode base;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400215 drmModeModeInfo mode_info;
216};
217
Daniel Stonefc175a72017-04-04 17:54:22 +0100218enum drm_fb_type {
219 BUFFER_INVALID = 0, /**< never used */
220 BUFFER_CLIENT, /**< directly sourced from client */
221 BUFFER_PIXMAN_DUMB, /**< internal Pixman rendering */
222 BUFFER_GBM_SURFACE, /**< internal EGL rendering */
Daniel Stonee4256832017-04-04 17:54:27 +0100223 BUFFER_CURSOR, /**< internal cursor buffer */
Daniel Stonefc175a72017-04-04 17:54:22 +0100224};
225
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300226struct drm_fb {
Daniel Stonefc175a72017-04-04 17:54:22 +0100227 enum drm_fb_type type;
228
Daniel Stone6e7a9612017-04-04 17:54:26 +0100229 int refcnt;
230
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200231 uint32_t fb_id, stride, handle, size;
Daniel Stone0b70fa42017-04-04 17:54:23 +0100232 const struct pixel_format_info *format;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000233 int width, height;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200234 int fd;
Pekka Paalanende685b82012-12-04 15:58:12 +0200235 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200236
237 /* Used by gbm fbs */
238 struct gbm_bo *bo;
Daniel Stone05a5ac22017-04-04 17:54:25 +0100239 struct gbm_surface *gbm_surface;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200240
241 /* Used by dumb fbs */
242 void *map;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300243};
244
Richard Hughes2b2092a2013-04-24 14:58:02 +0100245struct drm_edid {
246 char eisa_id[13];
247 char monitor_name[13];
248 char pnp_id[5];
249 char serial_number[13];
250};
251
Daniel Stone08d4edf2017-04-04 17:54:34 +0100252/**
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000253 * Pending state holds one or more drm_output_state structures, collected from
254 * performing repaint. This pending state is transient, and only lives between
255 * beginning a repaint group and flushing the results: after flush, each
256 * output state will complete and be retired separately.
257 */
258struct drm_pending_state {
259 struct drm_backend *backend;
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000260 struct wl_list output_list;
261};
262
263/*
264 * Output state holds the dynamic state for one Weston output, i.e. a KMS CRTC,
265 * plus >= 1 each of encoder/connector/plane. Since everything but the planes
266 * is currently statically assigned per-output, we mainly use this to track
267 * plane state.
268 *
269 * pending_state is set when the output state is owned by a pending_state,
270 * i.e. when it is being constructed and has not yet been applied. When the
271 * output state has been applied, the owning pending_state is freed.
272 */
273struct drm_output_state {
274 struct drm_pending_state *pending_state;
275 struct drm_output *output;
276 struct wl_list link;
Daniel Stonea08512f2016-11-08 17:46:10 +0000277 enum dpms_enum dpms;
Daniel Stonebc15f682016-11-14 16:57:01 +0000278 struct wl_list plane_list;
279};
280
281/**
282 * Plane state holds the dynamic state for a plane: where it is positioned,
283 * and which buffer it is currently displaying.
284 *
285 * The plane state is owned by an output state, except when setting an initial
286 * state. See drm_output_state for notes on state object lifetime.
287 */
288struct drm_plane_state {
289 struct drm_plane *plane;
290 struct drm_output *output;
291 struct drm_output_state *output_state;
292
293 struct drm_fb *fb;
294
295 int32_t src_x, src_y;
296 uint32_t src_w, src_h;
297 int32_t dest_x, dest_y;
298 uint32_t dest_w, dest_h;
299
300 bool complete;
301
302 struct wl_list link; /* drm_output_state::plane_list */
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000303};
304
305/**
Daniel Stone08d4edf2017-04-04 17:54:34 +0100306 * A plane represents one buffer, positioned within a CRTC, and stacked
307 * relative to other planes on the same CRTC.
308 *
309 * Each CRTC has a 'primary plane', which use used to display the classic
310 * framebuffer contents, as accessed through the legacy drmModeSetCrtc
311 * call (which combines setting the CRTC's actual physical mode, and the
312 * properties of the primary plane).
313 *
314 * The cursor plane also has its own alternate legacy API.
315 *
316 * Other planes are used opportunistically to display content we do not
317 * wish to blit into the primary plane. These non-primary/cursor planes
318 * are referred to as 'sprites'.
319 */
320struct drm_plane {
Daniel Stone08d4edf2017-04-04 17:54:34 +0100321 struct weston_plane base;
322
Daniel Stone08d4edf2017-04-04 17:54:34 +0100323 struct drm_backend *backend;
324
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100325 enum wdrm_plane_type type;
326
Daniel Stone08d4edf2017-04-04 17:54:34 +0100327 uint32_t possible_crtcs;
328 uint32_t plane_id;
329 uint32_t count_formats;
330
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100331 struct drm_property_info props[WDRM_PLANE__COUNT];
332
Daniel Stonebc15f682016-11-14 16:57:01 +0000333 /* The last state submitted to the kernel for this plane. */
334 struct drm_plane_state *state_cur;
Daniel Stone08d4edf2017-04-04 17:54:34 +0100335
Daniel Stonebc15f682016-11-14 16:57:01 +0000336 struct wl_list link;
Daniel Stone08d4edf2017-04-04 17:54:34 +0100337
338 uint32_t formats[];
339};
340
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400341struct drm_output {
Armin Krezović08368132016-09-30 14:11:05 +0200342 struct weston_output base;
343 drmModeConnector *connector;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400344
Daniel Stone17339232015-11-28 12:09:47 +0000345 uint32_t crtc_id; /* object ID to pass to DRM functions */
346 int pipe; /* index of CRTC in resource array / bitmasks */
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400347 uint32_t connector_id;
Matt Roper361d2ad2011-08-29 13:52:23 -0700348 drmModeCrtcPtr original_crtc;
Richard Hughes2b2092a2013-04-24 14:58:02 +0100349 struct drm_edid edid;
Daniel Stone02cf4662017-03-03 16:19:39 +0000350
351 /* Holds the properties for the connector */
352 struct drm_property_info props_conn[WDRM_CONNECTOR__COUNT];
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200353
Daniel Stone5bb8f582017-04-04 17:54:28 +0100354 struct backlight *backlight;
Daniel Stone36609c72015-06-18 07:49:02 +0100355
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300356 int vblank_pending;
357 int page_flip_pending;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800358 int destroy_pending;
Armin Krezović08368132016-09-30 14:11:05 +0200359 int disable_pending;
Daniel Stonea08512f2016-11-08 17:46:10 +0000360 int dpms_off_pending;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300361
Daniel Stonee4256832017-04-04 17:54:27 +0100362 struct drm_fb *gbm_cursor_fb[2];
Daniel Stone2ba17f42015-05-19 20:02:41 +0100363 struct drm_plane *cursor_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500364 struct weston_view *cursor_view;
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400365 int current_cursor;
Daniel Stone5bb8f582017-04-04 17:54:28 +0100366
367 struct gbm_surface *gbm_surface;
368 uint32_t gbm_format;
369
Daniel Stonee2e80132018-01-16 15:37:33 +0000370 /* Plane being displayed directly on the CRTC */
371 struct drm_plane *scanout_plane;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200372
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000373 /* The last state submitted to the kernel for this CRTC. */
374 struct drm_output_state *state_cur;
375 /* The previously-submitted state, where the hardware has not
376 * yet acknowledged completion of state_cur. */
377 struct drm_output_state *state_last;
378
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200379 struct drm_fb *dumb[2];
380 pixman_image_t *image[2];
381 int current_image;
382 pixman_region32_t previous_damage;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300383
384 struct vaapi_recorder *recorder;
385 struct wl_listener recorder_frame_listener;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000386
387 struct wl_event_source *pageflip_timer;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400388};
389
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300390static struct gl_renderer_interface *gl_renderer;
391
Kristian Høgsberg98cfea62013-02-18 16:15:53 -0500392static const char default_seat[] = "seat0";
Pekka Paalanen33156972012-08-03 13:30:30 -0400393
Daniel Stone087ddf02017-02-14 17:51:30 +0000394static void
395wl_array_remove_uint32(struct wl_array *array, uint32_t elm)
396{
397 uint32_t *pos, *end;
398
399 end = (uint32_t *) ((char *) array->data + array->size);
400
401 wl_array_for_each(pos, array) {
402 if (*pos != elm)
403 continue;
404
405 array->size -= sizeof(*pos);
406 if (pos + 1 == end)
407 break;
408
409 memmove(pos, pos + 1, (char *) end - (char *) (pos + 1));
410 break;
411 }
412}
413
Armin Krezović545dba62016-08-05 15:54:18 +0200414static inline struct drm_output *
415to_drm_output(struct weston_output *base)
416{
417 return container_of(base, struct drm_output, base);
418}
419
420static inline struct drm_backend *
421to_drm_backend(struct weston_compositor *base)
422{
423 return container_of(base->backend, struct drm_backend, base);
424}
425
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000426static int
427pageflip_timeout(void *data) {
428 /*
429 * Our timer just went off, that means we're not receiving drm
430 * page flip events anymore for that output. Let's gracefully exit
431 * weston with a return value so devs can debug what's going on.
432 */
433 struct drm_output *output = data;
434 struct weston_compositor *compositor = output->base.compositor;
435
436 weston_log("Pageflip timeout reached on output %s, your "
437 "driver is probably buggy! Exiting.\n",
438 output->base.name);
439 weston_compositor_exit_with_code(compositor, EXIT_FAILURE);
440
441 return 0;
442}
443
444/* Creates the pageflip timer. Note that it isn't armed by default */
445static int
446drm_output_pageflip_timer_create(struct drm_output *output)
447{
448 struct wl_event_loop *loop = NULL;
449 struct weston_compositor *ec = output->base.compositor;
450
451 loop = wl_display_get_event_loop(ec->wl_display);
452 assert(loop);
453 output->pageflip_timer = wl_event_loop_add_timer(loop,
454 pageflip_timeout,
455 output);
456
457 if (output->pageflip_timer == NULL) {
458 weston_log("creating drm pageflip timer failed: %m\n");
459 return -1;
460 }
461
462 return 0;
463}
464
Daniel Stonecb04cc42016-11-16 11:51:27 +0000465static inline struct drm_mode *
466to_drm_mode(struct weston_mode *base)
467{
468 return container_of(base, struct drm_mode, base);
469}
470
Daniel Stone02cf4662017-03-03 16:19:39 +0000471/**
472 * Get the current value of a KMS property
473 *
474 * Given a drmModeObjectGetProperties return, as well as the drm_property_info
475 * for the target property, return the current value of that property,
476 * with an optional default. If the property is a KMS enum type, the return
477 * value will be translated into the appropriate internal enum.
478 *
479 * If the property is not present, the default value will be returned.
480 *
481 * @param info Internal structure for property to look up
482 * @param props Raw KMS properties for the target object
483 * @param def Value to return if property is not found
484 */
485static uint64_t
486drm_property_get_value(struct drm_property_info *info,
487 drmModeObjectPropertiesPtr props,
488 uint64_t def)
489{
490 unsigned int i;
491
492 if (info->prop_id == 0)
493 return def;
494
495 for (i = 0; i < props->count_props; i++) {
496 unsigned int j;
497
498 if (props->props[i] != info->prop_id)
499 continue;
500
501 /* Simple (non-enum) types can return the value directly */
502 if (info->num_enum_values == 0)
503 return props->prop_values[i];
504
505 /* Map from raw value to enum value */
506 for (j = 0; j < info->num_enum_values; j++) {
507 if (!info->enum_values[j].valid)
508 continue;
509 if (info->enum_values[j].value != props->prop_values[i])
510 continue;
511
512 return j;
513 }
514
515 /* We don't have a mapping for this enum; return default. */
516 break;
517 }
518
519 return def;
520}
521
522/**
523 * Cache DRM property values
524 *
525 * Update a per-object array of drm_property_info structures, given the
526 * DRM properties of the object.
527 *
528 * Call this every time an object newly appears (note that only connectors
529 * can be hotplugged), the first time it is seen, or when its status changes
530 * in a way which invalidates the potential property values (currently, the
531 * only case for this is connector hotplug).
532 *
533 * This updates the property IDs and enum values within the drm_property_info
534 * array.
535 *
536 * DRM property enum values are dynamic at runtime; the user must query the
537 * property to find out the desired runtime value for a requested string
538 * name. Using the 'type' field on planes as an example, there is no single
539 * hardcoded constant for primary plane types; instead, the property must be
540 * queried at runtime to find the value associated with the string "Primary".
541 *
542 * This helper queries and caches the enum values, to allow us to use a set
543 * of compile-time-constant enums portably across various implementations.
544 * The values given in enum_names are searched for, and stored in the
545 * same-indexed field of the map array.
546 *
547 * @param b DRM backend object
548 * @param src DRM property info array to source from
549 * @param info DRM property info array to copy into
550 * @param num_infos Number of entries in the source array
551 * @param props DRM object properties for the object
552 */
553static void
554drm_property_info_populate(struct drm_backend *b,
555 const struct drm_property_info *src,
556 struct drm_property_info *info,
557 unsigned int num_infos,
558 drmModeObjectProperties *props)
559{
560 drmModePropertyRes *prop;
561 unsigned i, j;
562
563 for (i = 0; i < num_infos; i++) {
564 unsigned int j;
565
566 info[i].name = src[i].name;
567 info[i].prop_id = 0;
568 info[i].num_enum_values = src[i].num_enum_values;
569
570 if (src[i].num_enum_values == 0)
571 continue;
572
573 info[i].enum_values =
574 malloc(src[i].num_enum_values *
575 sizeof(*info[i].enum_values));
576 assert(info[i].enum_values);
577 for (j = 0; j < info[i].num_enum_values; j++) {
578 info[i].enum_values[j].name = src[i].enum_values[j].name;
579 info[i].enum_values[j].valid = false;
580 }
581 }
582
583 for (i = 0; i < props->count_props; i++) {
584 unsigned int k;
585
586 prop = drmModeGetProperty(b->drm.fd, props->props[i]);
587 if (!prop)
588 continue;
589
590 for (j = 0; j < num_infos; j++) {
591 if (!strcmp(prop->name, info[j].name))
592 break;
593 }
594
595 /* We don't know/care about this property. */
596 if (j == num_infos) {
597#ifdef DEBUG
598 weston_log("DRM debug: unrecognized property %u '%s'\n",
599 prop->prop_id, prop->name);
600#endif
601 drmModeFreeProperty(prop);
602 continue;
603 }
604
605 if (info[j].num_enum_values == 0 &&
606 (prop->flags & DRM_MODE_PROP_ENUM)) {
607 weston_log("DRM: expected property %s to not be an"
608 " enum, but it is; ignoring\n", prop->name);
609 drmModeFreeProperty(prop);
610 continue;
611 }
612
613 info[j].prop_id = props->props[i];
614
615 if (info[j].num_enum_values == 0) {
616 drmModeFreeProperty(prop);
617 continue;
618 }
619
620 if (!(prop->flags & DRM_MODE_PROP_ENUM)) {
621 weston_log("DRM: expected property %s to be an enum,"
622 " but it is not; ignoring\n", prop->name);
623 drmModeFreeProperty(prop);
624 info[j].prop_id = 0;
625 continue;
626 }
627
628 for (k = 0; k < info[j].num_enum_values; k++) {
629 int l;
630
631 for (l = 0; l < prop->count_enums; l++) {
632 if (!strcmp(prop->enums[l].name,
633 info[j].enum_values[k].name))
634 break;
635 }
636
637 if (l == prop->count_enums)
638 continue;
639
640 info[j].enum_values[k].valid = true;
641 info[j].enum_values[k].value = prop->enums[l].value;
642 }
643
644 drmModeFreeProperty(prop);
645 }
646
647#ifdef DEBUG
648 for (i = 0; i < num_infos; i++) {
649 if (info[i].prop_id == 0)
650 weston_log("DRM warning: property '%s' missing\n",
651 info[i].name);
652 }
653#endif
654}
655
656/**
657 * Free DRM property information
658 *
659 * Frees all memory associated with a DRM property info array.
660 *
661 * @param info DRM property info array
662 * @param num_props Number of entries in array to free
663 */
664static void
665drm_property_info_free(struct drm_property_info *info, int num_props)
666{
667 int i;
668
669 for (i = 0; i < num_props; i++)
670 free(info[i].enum_values);
671}
672
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400673static void
Daniel Stone2ba17f42015-05-19 20:02:41 +0100674drm_output_set_cursor(struct drm_output_state *output_state);
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400675
Mario Kleinerf507ec32015-06-21 21:25:14 +0200676static void
677drm_output_update_msc(struct drm_output *output, unsigned int seq);
678
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000679static void
680drm_output_destroy(struct weston_output *output_base);
681
Daniel Stone5ff289a2017-10-07 12:59:02 +0100682/**
683 * Returns true if the plane can be used on the given output for its current
684 * repaint cycle.
685 */
686static bool
687drm_plane_is_available(struct drm_plane *plane, struct drm_output *output)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500688{
Daniel Stone5ff289a2017-10-07 12:59:02 +0100689 assert(plane->state_cur);
690
691 /* The plane still has a request not yet completed by the kernel. */
692 if (!plane->state_cur->complete)
693 return false;
694
695 /* The plane is still active on another output. */
696 if (plane->state_cur->output && plane->state_cur->output != output)
697 return false;
698
699 /* Check whether the plane can be used with this CRTC; possible_crtcs
700 * is a bitmask of CRTC indices (pipe), rather than CRTC object ID. */
Daniel Stone08d4edf2017-04-04 17:54:34 +0100701 return !!(plane->possible_crtcs & (1 << output->pipe));
Jesse Barnes58ef3792012-02-23 09:45:49 -0500702}
703
Daniel Stone72c0e1b2017-02-09 13:49:15 +0000704static struct drm_output *
705drm_output_find_by_crtc(struct drm_backend *b, uint32_t crtc_id)
706{
707 struct drm_output *output;
708
709 wl_list_for_each(output, &b->compositor->output_list, base.link) {
710 if (output->crtc_id == crtc_id)
711 return output;
712 }
713
714 wl_list_for_each(output, &b->compositor->pending_output_list,
715 base.link) {
716 if (output->crtc_id == crtc_id)
717 return output;
718 }
719
720 return NULL;
721}
722
Daniel Stonec0ec7592017-02-09 13:58:35 +0000723static struct drm_output *
724drm_output_find_by_connector(struct drm_backend *b, uint32_t connector_id)
725{
726 struct drm_output *output;
727
728 wl_list_for_each(output, &b->compositor->output_list, base.link) {
729 if (output->connector_id == connector_id)
730 return output;
731 }
732
733 wl_list_for_each(output, &b->compositor->pending_output_list,
734 base.link) {
735 if (output->connector_id == connector_id)
736 return output;
737 }
738
739 return NULL;
740}
741
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300742static void
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100743drm_fb_destroy(struct drm_fb *fb)
744{
745 if (fb->fb_id != 0)
746 drmModeRmFB(fb->fd, fb->fb_id);
747 weston_buffer_reference(&fb->buffer_ref, NULL);
748 free(fb);
749}
750
751static void
752drm_fb_destroy_dumb(struct drm_fb *fb)
753{
754 struct drm_mode_destroy_dumb destroy_arg;
755
756 assert(fb->type == BUFFER_PIXMAN_DUMB);
757
758 if (fb->map && fb->size > 0)
759 munmap(fb->map, fb->size);
760
761 memset(&destroy_arg, 0, sizeof(destroy_arg));
762 destroy_arg.handle = fb->handle;
763 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
764
765 drm_fb_destroy(fb);
766}
767
768static void
769drm_fb_destroy_gbm(struct gbm_bo *bo, void *data)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300770{
771 struct drm_fb *fb = data;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300772
Daniel Stonee4256832017-04-04 17:54:27 +0100773 assert(fb->type == BUFFER_GBM_SURFACE || fb->type == BUFFER_CLIENT ||
774 fb->type == BUFFER_CURSOR);
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100775 drm_fb_destroy(fb);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300776}
777
778static struct drm_fb *
Daniel Stonef214fdc2016-11-14 17:43:57 +0000779drm_fb_create_dumb(struct drm_backend *b, int width, int height,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300780 uint32_t format)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200781{
782 struct drm_fb *fb;
783 int ret;
784
785 struct drm_mode_create_dumb create_arg;
786 struct drm_mode_destroy_dumb destroy_arg;
787 struct drm_mode_map_dumb map_arg;
788
Peter Huttererf3d62272013-08-08 11:57:05 +1000789 fb = zalloc(sizeof *fb);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200790 if (!fb)
791 return NULL;
792
Daniel Stone6e7a9612017-04-04 17:54:26 +0100793 fb->refcnt = 1;
794
Daniel Stone0b70fa42017-04-04 17:54:23 +0100795 fb->format = pixel_format_get_info(format);
796 if (!fb->format) {
797 weston_log("failed to look up format 0x%lx\n",
798 (unsigned long) format);
799 goto err_fb;
800 }
801
802 if (!fb->format->depth || !fb->format->bpp) {
803 weston_log("format 0x%lx is not compatible with dumb buffers\n",
804 (unsigned long) format);
805 goto err_fb;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300806 }
807
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700808 memset(&create_arg, 0, sizeof create_arg);
Daniel Stone0b70fa42017-04-04 17:54:23 +0100809 create_arg.bpp = fb->format->bpp;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200810 create_arg.width = width;
811 create_arg.height = height;
812
Giulio Camuffo954f1832014-10-11 18:27:30 +0300813 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200814 if (ret)
815 goto err_fb;
816
Daniel Stonefc175a72017-04-04 17:54:22 +0100817 fb->type = BUFFER_PIXMAN_DUMB;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200818 fb->handle = create_arg.handle;
819 fb->stride = create_arg.pitch;
820 fb->size = create_arg.size;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000821 fb->width = width;
822 fb->height = height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300823 fb->fd = b->drm.fd;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200824
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300825 ret = -1;
826
827 if (!b->no_addfb2) {
Yong Bakos4b6321f2016-08-17 17:37:55 -0700828 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300829
830 handles[0] = fb->handle;
831 pitches[0] = fb->stride;
832 offsets[0] = 0;
833
834 ret = drmModeAddFB2(b->drm.fd, width, height,
Daniel Stone0b70fa42017-04-04 17:54:23 +0100835 fb->format->format,
836 handles, pitches, offsets,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300837 &fb->fb_id, 0);
838 if (ret) {
839 weston_log("addfb2 failed: %m\n");
840 b->no_addfb2 = 1;
841 }
842 }
843
844 if (ret) {
Daniel Stone0b70fa42017-04-04 17:54:23 +0100845 ret = drmModeAddFB(b->drm.fd, width, height,
846 fb->format->depth, fb->format->bpp,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300847 fb->stride, fb->handle, &fb->fb_id);
848 }
849
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200850 if (ret)
851 goto err_bo;
852
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700853 memset(&map_arg, 0, sizeof map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200854 map_arg.handle = fb->handle;
Chris Michaeleb2074a2013-05-01 21:26:02 -0400855 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200856 if (ret)
857 goto err_add_fb;
858
Chris Michael4a7ce1f2015-11-10 10:40:37 -0500859 fb->map = mmap(NULL, fb->size, PROT_WRITE,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300860 MAP_SHARED, b->drm.fd, map_arg.offset);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200861 if (fb->map == MAP_FAILED)
862 goto err_add_fb;
863
864 return fb;
865
866err_add_fb:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300867 drmModeRmFB(b->drm.fd, fb->fb_id);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200868err_bo:
869 memset(&destroy_arg, 0, sizeof(destroy_arg));
870 destroy_arg.handle = create_arg.handle;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300871 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200872err_fb:
873 free(fb);
874 return NULL;
875}
876
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200877static struct drm_fb *
Daniel Stone6e7a9612017-04-04 17:54:26 +0100878drm_fb_ref(struct drm_fb *fb)
879{
880 fb->refcnt++;
881 return fb;
882}
883
884static struct drm_fb *
Daniel Stonefc175a72017-04-04 17:54:22 +0100885drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend,
886 uint32_t format, enum drm_fb_type type)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300887{
888 struct drm_fb *fb = gbm_bo_get_user_data(bo);
Derek Foreman482ffdf2016-07-08 12:50:57 -0500889 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300890 int ret;
891
Daniel Stonefc175a72017-04-04 17:54:22 +0100892 if (fb) {
893 assert(fb->type == type);
Daniel Stone6e7a9612017-04-04 17:54:26 +0100894 return drm_fb_ref(fb);
Daniel Stonefc175a72017-04-04 17:54:22 +0100895 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300896
Bryce Harringtonde16d892014-11-20 22:21:57 -0800897 fb = zalloc(sizeof *fb);
898 if (fb == NULL)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200899 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300900
Daniel Stonefc175a72017-04-04 17:54:22 +0100901 fb->type = type;
Daniel Stone6e7a9612017-04-04 17:54:26 +0100902 fb->refcnt = 1;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300903 fb->bo = bo;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300904
Daniel Stonec8c917c2016-11-14 17:45:58 +0000905 fb->width = gbm_bo_get_width(bo);
906 fb->height = gbm_bo_get_height(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200907 fb->stride = gbm_bo_get_stride(bo);
908 fb->handle = gbm_bo_get_handle(bo).u32;
Daniel Stone0b70fa42017-04-04 17:54:23 +0100909 fb->format = pixel_format_get_info(format);
Daniel Stonec8c917c2016-11-14 17:45:58 +0000910 fb->size = fb->stride * fb->height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300911 fb->fd = backend->drm.fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300912
Daniel Stone0b70fa42017-04-04 17:54:23 +0100913 if (!fb->format) {
914 weston_log("couldn't look up format 0x%lx\n",
915 (unsigned long) format);
916 goto err_free;
917 }
918
Daniel Stonec8c917c2016-11-14 17:45:58 +0000919 if (backend->min_width > fb->width ||
920 fb->width > backend->max_width ||
921 backend->min_height > fb->height ||
922 fb->height > backend->max_height) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200923 weston_log("bo geometry out of bounds\n");
924 goto err_free;
925 }
926
927 ret = -1;
928
Giulio Camuffo954f1832014-10-11 18:27:30 +0300929 if (format && !backend->no_addfb2) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200930 handles[0] = fb->handle;
931 pitches[0] = fb->stride;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200932 offsets[0] = 0;
933
Daniel Stonec8c917c2016-11-14 17:45:58 +0000934 ret = drmModeAddFB2(backend->drm.fd, fb->width, fb->height,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200935 format, handles, pitches, offsets,
936 &fb->fb_id, 0);
937 if (ret) {
938 weston_log("addfb2 failed: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300939 backend->no_addfb2 = 1;
940 backend->sprites_are_broken = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200941 }
942 }
943
Daniel Stone0b70fa42017-04-04 17:54:23 +0100944 if (ret && fb->format->depth && fb->format->bpp)
Daniel Stonec8c917c2016-11-14 17:45:58 +0000945 ret = drmModeAddFB(backend->drm.fd, fb->width, fb->height,
Daniel Stone0b70fa42017-04-04 17:54:23 +0100946 fb->format->depth, fb->format->bpp,
947 fb->stride, fb->handle, &fb->fb_id);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200948
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300949 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200950 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200951 goto err_free;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300952 }
953
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100954 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_gbm);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300955
956 return fb;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200957
958err_free:
959 free(fb);
960 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300961}
962
963static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500964drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200965{
Pekka Paalanende685b82012-12-04 15:58:12 +0200966 assert(fb->buffer_ref.buffer == NULL);
Daniel Stonefc175a72017-04-04 17:54:22 +0100967 assert(fb->type == BUFFER_CLIENT);
Pekka Paalanende685b82012-12-04 15:58:12 +0200968 weston_buffer_reference(&fb->buffer_ref, buffer);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200969}
970
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200971static void
Daniel Stone05a5ac22017-04-04 17:54:25 +0100972drm_fb_unref(struct drm_fb *fb)
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200973{
974 if (!fb)
975 return;
976
Daniel Stone6e7a9612017-04-04 17:54:26 +0100977 assert(fb->refcnt > 0);
978 if (--fb->refcnt > 0)
979 return;
980
Daniel Stonefc175a72017-04-04 17:54:22 +0100981 switch (fb->type) {
982 case BUFFER_PIXMAN_DUMB:
Daniel Stone6e7a9612017-04-04 17:54:26 +0100983 drm_fb_destroy_dumb(fb);
Daniel Stonefc175a72017-04-04 17:54:22 +0100984 break;
Daniel Stonee4256832017-04-04 17:54:27 +0100985 case BUFFER_CURSOR:
Daniel Stonefc175a72017-04-04 17:54:22 +0100986 case BUFFER_CLIENT:
987 gbm_bo_destroy(fb->bo);
988 break;
989 case BUFFER_GBM_SURFACE:
Daniel Stone05a5ac22017-04-04 17:54:25 +0100990 gbm_surface_release_buffer(fb->gbm_surface, fb->bo);
Daniel Stonefc175a72017-04-04 17:54:22 +0100991 break;
992 default:
993 assert(NULL);
994 break;
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200995 }
996}
997
Daniel Stone7b2ddac2016-11-11 19:11:49 +0000998/**
Daniel Stonebc15f682016-11-14 16:57:01 +0000999 * Allocate a new, empty, plane state.
1000 */
1001static struct drm_plane_state *
1002drm_plane_state_alloc(struct drm_output_state *state_output,
1003 struct drm_plane *plane)
1004{
1005 struct drm_plane_state *state = zalloc(sizeof(*state));
1006
1007 assert(state);
1008 state->output_state = state_output;
1009 state->plane = plane;
1010
1011 /* Here we only add the plane state to the desired link, and not
1012 * set the member. Having an output pointer set means that the
1013 * plane will be displayed on the output; this won't be the case
1014 * when we go to disable a plane. In this case, it must be part of
1015 * the commit (and thus the output state), but the member must be
1016 * NULL, as it will not be on any output when the state takes
1017 * effect.
1018 */
1019 if (state_output)
1020 wl_list_insert(&state_output->plane_list, &state->link);
1021 else
1022 wl_list_init(&state->link);
1023
1024 return state;
1025}
1026
1027/**
1028 * Free an existing plane state. As a special case, the state will not
1029 * normally be freed if it is the current state; see drm_plane_set_state.
1030 */
1031static void
1032drm_plane_state_free(struct drm_plane_state *state, bool force)
1033{
1034 if (!state)
1035 return;
1036
1037 wl_list_remove(&state->link);
1038 wl_list_init(&state->link);
1039 state->output_state = NULL;
1040
1041 if (force || state != state->plane->state_cur) {
1042 drm_fb_unref(state->fb);
1043 free(state);
1044 }
1045}
1046
1047/**
1048 * Duplicate an existing plane state into a new plane state, storing it within
1049 * the given output state. If the output state already contains a plane state
1050 * for the drm_plane referenced by 'src', that plane state is freed first.
1051 */
1052static struct drm_plane_state *
1053drm_plane_state_duplicate(struct drm_output_state *state_output,
1054 struct drm_plane_state *src)
1055{
1056 struct drm_plane_state *dst = malloc(sizeof(*dst));
1057 struct drm_plane_state *old, *tmp;
1058
1059 assert(src);
1060 assert(dst);
1061 *dst = *src;
1062 wl_list_init(&dst->link);
1063
1064 wl_list_for_each_safe(old, tmp, &state_output->plane_list, link) {
1065 /* Duplicating a plane state into the same output state, so
1066 * it can replace itself with an identical copy of itself,
1067 * makes no sense. */
1068 assert(old != src);
1069 if (old->plane == dst->plane)
1070 drm_plane_state_free(old, false);
1071 }
1072
1073 wl_list_insert(&state_output->plane_list, &dst->link);
1074 if (src->fb)
1075 dst->fb = drm_fb_ref(src->fb);
1076 dst->output_state = state_output;
1077 dst->complete = false;
1078
1079 return dst;
1080}
1081
1082/**
1083 * Remove a plane state from an output state; if the plane was previously
1084 * enabled, then replace it with a disabling state. This ensures that the
1085 * output state was untouched from it was before the plane state was
1086 * modified by the caller of this function.
1087 *
1088 * This is required as drm_output_state_get_plane may either allocate a
1089 * new plane state, in which case this function will just perform a matching
1090 * drm_plane_state_free, or it may instead repurpose an existing disabling
1091 * state (if the plane was previously active), in which case this function
1092 * will reset it.
1093 */
1094static void
1095drm_plane_state_put_back(struct drm_plane_state *state)
1096{
1097 struct drm_output_state *state_output;
1098 struct drm_plane *plane;
1099
1100 if (!state)
1101 return;
1102
1103 state_output = state->output_state;
1104 plane = state->plane;
1105 drm_plane_state_free(state, false);
1106
1107 /* Plane was previously disabled; no need to keep this temporary
1108 * state around. */
1109 if (!plane->state_cur->fb)
1110 return;
1111
1112 (void) drm_plane_state_alloc(state_output, plane);
1113}
1114
1115/**
Daniel Stone2ba17f42015-05-19 20:02:41 +01001116 * Return a plane state from a drm_output_state.
1117 */
1118static struct drm_plane_state *
1119drm_output_state_get_existing_plane(struct drm_output_state *state_output,
1120 struct drm_plane *plane)
1121{
1122 struct drm_plane_state *ps;
1123
1124 wl_list_for_each(ps, &state_output->plane_list, link) {
1125 if (ps->plane == plane)
1126 return ps;
1127 }
1128
1129 return NULL;
1130}
1131
1132/**
Daniel Stonebc15f682016-11-14 16:57:01 +00001133 * Return a plane state from a drm_output_state, either existing or
1134 * freshly allocated.
1135 */
1136static struct drm_plane_state *
1137drm_output_state_get_plane(struct drm_output_state *state_output,
1138 struct drm_plane *plane)
1139{
1140 struct drm_plane_state *ps;
1141
Daniel Stone2ba17f42015-05-19 20:02:41 +01001142 ps = drm_output_state_get_existing_plane(state_output, plane);
1143 if (ps)
1144 return ps;
Daniel Stonebc15f682016-11-14 16:57:01 +00001145
1146 return drm_plane_state_alloc(state_output, plane);
1147}
1148
1149/**
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001150 * Allocate a new, empty drm_output_state. This should not generally be used
1151 * in the repaint cycle; see drm_output_state_duplicate.
1152 */
1153static struct drm_output_state *
1154drm_output_state_alloc(struct drm_output *output,
1155 struct drm_pending_state *pending_state)
Daniel Stone90648872016-10-21 18:08:37 +01001156{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001157 struct drm_output_state *state = zalloc(sizeof(*state));
1158
1159 assert(state);
1160 state->output = output;
Daniel Stonea08512f2016-11-08 17:46:10 +00001161 state->dpms = WESTON_DPMS_OFF;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001162 state->pending_state = pending_state;
1163 if (pending_state)
1164 wl_list_insert(&pending_state->output_list, &state->link);
1165 else
1166 wl_list_init(&state->link);
1167
Daniel Stonebc15f682016-11-14 16:57:01 +00001168 wl_list_init(&state->plane_list);
1169
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001170 return state;
1171}
1172
1173/**
1174 * Duplicate an existing drm_output_state into a new one. This is generally
1175 * used during the repaint cycle, to capture the existing state of an output
1176 * and modify it to create a new state to be used.
1177 *
1178 * The mode determines whether the output will be reset to an a blank state,
1179 * or an exact mirror of the current state.
1180 */
1181static struct drm_output_state *
1182drm_output_state_duplicate(struct drm_output_state *src,
1183 struct drm_pending_state *pending_state,
1184 enum drm_output_state_duplicate_mode plane_mode)
1185{
1186 struct drm_output_state *dst = malloc(sizeof(*dst));
Daniel Stonebc15f682016-11-14 16:57:01 +00001187 struct drm_plane_state *ps;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001188
1189 assert(dst);
1190
1191 /* Copy the whole structure, then individually modify the
1192 * pending_state, as well as the list link into our pending
1193 * state. */
1194 *dst = *src;
1195
1196 dst->pending_state = pending_state;
1197 if (pending_state)
1198 wl_list_insert(&pending_state->output_list, &dst->link);
1199 else
1200 wl_list_init(&dst->link);
1201
Daniel Stonebc15f682016-11-14 16:57:01 +00001202 wl_list_init(&dst->plane_list);
1203
1204 wl_list_for_each(ps, &src->plane_list, link) {
1205 /* Don't carry planes which are now disabled; these should be
1206 * free for other outputs to reuse. */
1207 if (!ps->output)
1208 continue;
1209
1210 if (plane_mode == DRM_OUTPUT_STATE_CLEAR_PLANES)
1211 (void) drm_plane_state_alloc(dst, ps->plane);
1212 else
1213 (void) drm_plane_state_duplicate(dst, ps);
1214 }
1215
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001216 return dst;
1217}
1218
1219/**
1220 * Free an unused drm_output_state.
1221 */
1222static void
1223drm_output_state_free(struct drm_output_state *state)
1224{
Daniel Stonebc15f682016-11-14 16:57:01 +00001225 struct drm_plane_state *ps, *next;
1226
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001227 if (!state)
1228 return;
1229
Daniel Stonebc15f682016-11-14 16:57:01 +00001230 wl_list_for_each_safe(ps, next, &state->plane_list, link)
1231 drm_plane_state_free(ps, false);
1232
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001233 wl_list_remove(&state->link);
Daniel Stonebc15f682016-11-14 16:57:01 +00001234
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001235 free(state);
Daniel Stone90648872016-10-21 18:08:37 +01001236}
1237
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001238/**
Daniel Stonea08512f2016-11-08 17:46:10 +00001239 * Get output state to disable output
1240 *
1241 * Returns a pointer to an output_state object which can be used to disable
1242 * an output (e.g. DPMS off).
1243 *
1244 * @param pending_state The pending state object owning this update
1245 * @param output The output to disable
1246 * @returns A drm_output_state to disable the output
1247 */
1248static struct drm_output_state *
1249drm_output_get_disable_state(struct drm_pending_state *pending_state,
1250 struct drm_output *output)
1251{
1252 struct drm_output_state *output_state;
1253
1254 output_state = drm_output_state_duplicate(output->state_cur,
1255 pending_state,
1256 DRM_OUTPUT_STATE_CLEAR_PLANES);
1257 output_state->dpms = WESTON_DPMS_OFF;
1258
1259 return output_state;
1260}
1261
1262/**
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001263 * Allocate a new drm_pending_state
1264 *
1265 * Allocate a new, empty, 'pending state' structure to be used across a
1266 * repaint cycle or similar.
1267 *
1268 * @param backend DRM backend
1269 * @returns Newly-allocated pending state structure
1270 */
1271static struct drm_pending_state *
1272drm_pending_state_alloc(struct drm_backend *backend)
1273{
1274 struct drm_pending_state *ret;
1275
1276 ret = calloc(1, sizeof(*ret));
1277 if (!ret)
1278 return NULL;
1279
1280 ret->backend = backend;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001281 wl_list_init(&ret->output_list);
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001282
1283 return ret;
1284}
1285
1286/**
1287 * Free a drm_pending_state structure
1288 *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001289 * Frees a pending_state structure, as well as any output_states connected
1290 * to this pending state.
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001291 *
1292 * @param pending_state Pending state structure to free
1293 */
1294static void
1295drm_pending_state_free(struct drm_pending_state *pending_state)
1296{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001297 struct drm_output_state *output_state, *tmp;
1298
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001299 if (!pending_state)
1300 return;
1301
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001302 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
1303 link) {
1304 drm_output_state_free(output_state);
1305 }
1306
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001307 free(pending_state);
1308}
1309
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001310/**
1311 * Find an output state in a pending state
1312 *
1313 * Given a pending_state structure, find the output_state for a particular
1314 * output.
1315 *
1316 * @param pending_state Pending state structure to search
1317 * @param output Output to find state for
1318 * @returns Output state if present, or NULL if not
1319 */
1320static struct drm_output_state *
1321drm_pending_state_get_output(struct drm_pending_state *pending_state,
1322 struct drm_output *output)
1323{
1324 struct drm_output_state *output_state;
1325
1326 wl_list_for_each(output_state, &pending_state->output_list, link) {
1327 if (output_state->output == output)
1328 return output_state;
1329 }
1330
1331 return NULL;
1332}
1333
Daniel Stonea08512f2016-11-08 17:46:10 +00001334static int drm_pending_state_apply_sync(struct drm_pending_state *state);
1335
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001336/**
1337 * Mark a drm_output_state (the output's last state) as complete. This handles
1338 * any post-completion actions such as updating the repaint timer, disabling the
1339 * output, and finally freeing the state.
1340 */
1341static void
1342drm_output_update_complete(struct drm_output *output, uint32_t flags,
1343 unsigned int sec, unsigned int usec)
1344{
Daniel Stonea08512f2016-11-08 17:46:10 +00001345 struct drm_backend *b = to_drm_backend(output->base.compositor);
Daniel Stonebc15f682016-11-14 16:57:01 +00001346 struct drm_plane_state *ps;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001347 struct timespec ts;
1348
1349 /* Stop the pageflip timer instead of rearming it here */
1350 if (output->pageflip_timer)
1351 wl_event_source_timer_update(output->pageflip_timer, 0);
1352
Daniel Stonebc15f682016-11-14 16:57:01 +00001353 wl_list_for_each(ps, &output->state_cur->plane_list, link)
1354 ps->complete = true;
1355
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001356 drm_output_state_free(output->state_last);
1357 output->state_last = NULL;
1358
1359 if (output->destroy_pending) {
Daniel Stonea08512f2016-11-08 17:46:10 +00001360 output->destroy_pending = 0;
1361 output->disable_pending = 0;
1362 output->dpms_off_pending = 0;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001363 drm_output_destroy(&output->base);
1364 return;
1365 } else if (output->disable_pending) {
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001366 output->disable_pending = 0;
Daniel Stonea08512f2016-11-08 17:46:10 +00001367 output->dpms_off_pending = 0;
1368 weston_output_disable(&output->base);
1369 return;
1370 } else if (output->dpms_off_pending) {
1371 struct drm_pending_state *pending = drm_pending_state_alloc(b);
1372 output->dpms_off_pending = 0;
1373 drm_output_get_disable_state(pending, output);
1374 drm_pending_state_apply_sync(pending);
1375 return;
1376 } else if (output->state_cur->dpms == WESTON_DPMS_OFF &&
1377 output->base.repaint_status != REPAINT_AWAITING_COMPLETION) {
1378 /* DPMS can happen to us either in the middle of a repaint
1379 * cycle (when we have painted fresh content, only to throw it
1380 * away for DPMS off), or at any other random point. If the
1381 * latter is true, then we cannot go through finish_frame,
1382 * because the repaint machinery does not expect this. */
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001383 return;
1384 }
1385
1386 ts.tv_sec = sec;
1387 ts.tv_nsec = usec * 1000;
1388 weston_output_finish_frame(&output->base, &ts, flags);
1389
1390 /* We can't call this from frame_notify, because the output's
1391 * repaint needed flag is cleared just after that */
1392 if (output->recorder)
1393 weston_output_schedule_repaint(&output->base);
1394}
1395
1396/**
1397 * Mark an output state as current on the output, i.e. it has been
1398 * submitted to the kernel. The mode argument determines whether this
1399 * update will be applied synchronously (e.g. when calling drmModeSetCrtc),
1400 * or asynchronously (in which case we wait for events to complete).
1401 */
1402static void
1403drm_output_assign_state(struct drm_output_state *state,
1404 enum drm_state_apply_mode mode)
1405{
1406 struct drm_output *output = state->output;
Daniel Stonebc15f682016-11-14 16:57:01 +00001407 struct drm_plane_state *plane_state;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001408
1409 assert(!output->state_last);
1410
1411 if (mode == DRM_STATE_APPLY_ASYNC)
1412 output->state_last = output->state_cur;
1413 else
1414 drm_output_state_free(output->state_cur);
1415
1416 wl_list_remove(&state->link);
1417 wl_list_init(&state->link);
1418 state->pending_state = NULL;
1419
1420 output->state_cur = state;
Daniel Stonebc15f682016-11-14 16:57:01 +00001421
1422 /* Replace state_cur on each affected plane with the new state, being
1423 * careful to dispose of orphaned (but only orphaned) previous state.
1424 * If the previous state is not orphaned (still has an output_state
1425 * attached), it will be disposed of by freeing the output_state. */
1426 wl_list_for_each(plane_state, &state->plane_list, link) {
1427 struct drm_plane *plane = plane_state->plane;
1428
1429 if (plane->state_cur && !plane->state_cur->output_state)
1430 drm_plane_state_free(plane->state_cur, true);
1431 plane->state_cur = plane_state;
1432
1433 if (mode != DRM_STATE_APPLY_ASYNC) {
1434 plane_state->complete = true;
1435 continue;
1436 }
1437
1438 if (plane->type == WDRM_PLANE_TYPE_OVERLAY)
1439 output->vblank_pending++;
Daniel Stonee2e80132018-01-16 15:37:33 +00001440 else if (plane->type == WDRM_PLANE_TYPE_PRIMARY)
1441 output->page_flip_pending = 1;
Daniel Stonebc15f682016-11-14 16:57:01 +00001442 }
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001443}
1444
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001445static int
1446drm_view_transform_supported(struct weston_view *ev)
1447{
1448 return !ev->transform.enabled ||
1449 (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
1450}
1451
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001452static uint32_t
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +02001453drm_output_check_scanout_format(struct drm_output *output,
1454 struct weston_surface *es, struct gbm_bo *bo)
1455{
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +02001456 uint32_t format;
1457 pixman_region32_t r;
1458
1459 format = gbm_bo_get_format(bo);
1460
Kristian Høgsberg3f97b342013-10-16 16:08:57 -07001461 if (format == GBM_FORMAT_ARGB8888) {
1462 /* We can scanout an ARGB buffer if the surface's
1463 * opaque region covers the whole output, but we have
1464 * to use XRGB as the KMS format code. */
Kristian Høgsberg1be87e32014-01-17 14:22:41 -08001465 pixman_region32_init_rect(&r, 0, 0,
1466 output->base.width,
1467 output->base.height);
1468 pixman_region32_subtract(&r, &r, &es->opaque);
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +02001469
1470 if (!pixman_region32_not_empty(&r))
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001471 format = GBM_FORMAT_XRGB8888;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +02001472
1473 pixman_region32_fini(&r);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001474 }
Kristian Høgsberg3f97b342013-10-16 16:08:57 -07001475
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001476 if (output->gbm_format == format)
Kristian Høgsberg3f97b342013-10-16 16:08:57 -07001477 return format;
1478
1479 return 0;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +02001480}
1481
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001482static struct weston_plane *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001483drm_output_prepare_scanout_view(struct drm_output_state *output_state,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001484 struct weston_view *ev)
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001485{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001486 struct drm_output *output = output_state->output;
Armin Krezović545dba62016-08-05 15:54:18 +02001487 struct drm_backend *b = to_drm_backend(output->base.compositor);
Daniel Stonee2e80132018-01-16 15:37:33 +00001488 struct drm_plane *scanout_plane = output->scanout_plane;
1489 struct drm_plane_state *state;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001490 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001491 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001492 struct gbm_bo *bo;
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001493 uint32_t format;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001494
Daniel Stone90648872016-10-21 18:08:37 +01001495 /* Don't import buffers which span multiple outputs. */
1496 if (ev->output_mask != (1u << output->base.id))
1497 return NULL;
1498
Daniel Stone296d7a92016-10-21 18:05:37 +01001499 /* We use GBM to import buffers. */
1500 if (b->gbm == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001501 return NULL;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001502
Daniel Stone296d7a92016-10-21 18:05:37 +01001503 if (buffer == NULL)
1504 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +01001505 if (wl_shm_buffer_get(buffer->resource))
1506 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001507
1508 /* Make sure our view is exactly compatible with the output. */
1509 if (ev->geometry.x != output->base.x ||
1510 ev->geometry.y != output->base.y)
1511 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +01001512 if (buffer->width != output->base.current_mode->width ||
1513 buffer->height != output->base.current_mode->height)
1514 return NULL;
1515
Daniel Stone296d7a92016-10-21 18:05:37 +01001516 if (ev->transform.enabled)
1517 return NULL;
Pekka Paalanen5580f222015-02-17 16:33:18 +02001518 if (ev->geometry.scissor_enabled)
1519 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001520 if (viewport->buffer.transform != output->base.transform)
1521 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +01001522 if (viewport->buffer.scale != output->base.current_scale)
1523 return NULL;
1524 if (!drm_view_transform_supported(ev))
1525 return NULL;
1526
1527 if (ev->alpha != 1.0f)
1528 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001529
Daniel Stonee2e80132018-01-16 15:37:33 +00001530 state = drm_output_state_get_plane(output_state, scanout_plane);
1531 if (state->fb) {
1532 /* If there is already a framebuffer on the scanout plane,
1533 * a client view has already been placed on the scanout
1534 * view. In that case, do not free or put back the state,
1535 * but just leave it in place and quietly exit. */
1536 return NULL;
1537 }
1538
Giulio Camuffo954f1832014-10-11 18:27:30 +03001539 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
Kristian Høgsberg63996462013-09-03 22:27:08 -07001540 buffer->resource, GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001541
Rob Bradford9b101872012-09-14 23:25:41 +01001542 /* Unable to use the buffer for scanout */
1543 if (!bo)
1544 return NULL;
1545
Jason Ekstranda7af7042013-10-12 22:38:11 -05001546 format = drm_output_check_scanout_format(output, ev->surface, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001547 if (format == 0) {
Daniel Stonee2e80132018-01-16 15:37:33 +00001548 drm_plane_state_put_back(state);
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +03001549 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001550 return NULL;
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +03001551 }
1552
Daniel Stonee2e80132018-01-16 15:37:33 +00001553 state->fb = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT);
1554 if (!state->fb) {
1555 drm_plane_state_put_back(state);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001556 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001557 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001558 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001559
Daniel Stonee2e80132018-01-16 15:37:33 +00001560 drm_fb_set_buffer(state->fb, buffer);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001561
Daniel Stonee2e80132018-01-16 15:37:33 +00001562 state->output = output;
1563
1564 state->src_x = 0;
1565 state->src_y = 0;
1566 state->src_w = state->fb->width << 16;
1567 state->src_h = state->fb->height << 16;
1568
1569 state->dest_x = 0;
1570 state->dest_y = 0;
1571 state->dest_w = output->base.current_mode->width;
1572 state->dest_h = output->base.current_mode->height;
1573
1574 return &scanout_plane->base;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001575}
1576
Daniel Stone95d48a22017-04-04 17:54:30 +01001577static struct drm_fb *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001578drm_output_render_gl(struct drm_output_state *state, pixman_region32_t *damage)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001579{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001580 struct drm_output *output = state->output;
Armin Krezović545dba62016-08-05 15:54:18 +02001581 struct drm_backend *b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001582 struct gbm_bo *bo;
Daniel Stone95d48a22017-04-04 17:54:30 +01001583 struct drm_fb *ret;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001584
Giulio Camuffo954f1832014-10-11 18:27:30 +03001585 output->base.compositor->renderer->repaint_output(&output->base,
1586 damage);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001587
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001588 bo = gbm_surface_lock_front_buffer(output->gbm_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001589 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +02001590 weston_log("failed to lock front buffer: %m\n");
Daniel Stone95d48a22017-04-04 17:54:30 +01001591 return NULL;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001592 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001593
Daniel Stone95d48a22017-04-04 17:54:30 +01001594 ret = drm_fb_get_from_bo(bo, b, output->gbm_format, BUFFER_GBM_SURFACE);
1595 if (!ret) {
Martin Minarik6d118362012-06-07 18:01:59 +02001596 weston_log("failed to get drm_fb for bo\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001597 gbm_surface_release_buffer(output->gbm_surface, bo);
Daniel Stone95d48a22017-04-04 17:54:30 +01001598 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001599 }
Daniel Stone95d48a22017-04-04 17:54:30 +01001600 ret->gbm_surface = output->gbm_surface;
1601
1602 return ret;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001603}
1604
Daniel Stone95d48a22017-04-04 17:54:30 +01001605static struct drm_fb *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001606drm_output_render_pixman(struct drm_output_state *state,
1607 pixman_region32_t *damage)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001608{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001609 struct drm_output *output = state->output;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001610 struct weston_compositor *ec = output->base.compositor;
1611 pixman_region32_t total_damage, previous_damage;
1612
1613 pixman_region32_init(&total_damage);
1614 pixman_region32_init(&previous_damage);
1615
1616 pixman_region32_copy(&previous_damage, damage);
1617
1618 pixman_region32_union(&total_damage, damage, &output->previous_damage);
1619 pixman_region32_copy(&output->previous_damage, &previous_damage);
1620
1621 output->current_image ^= 1;
1622
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001623 pixman_renderer_output_set_buffer(&output->base,
1624 output->image[output->current_image]);
1625
1626 ec->renderer->repaint_output(&output->base, &total_damage);
1627
1628 pixman_region32_fini(&total_damage);
1629 pixman_region32_fini(&previous_damage);
Daniel Stone95d48a22017-04-04 17:54:30 +01001630
1631 return drm_fb_ref(output->dumb[output->current_image]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001632}
1633
1634static void
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001635drm_output_render(struct drm_output_state *state, pixman_region32_t *damage)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001636{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001637 struct drm_output *output = state->output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001638 struct weston_compositor *c = output->base.compositor;
Daniel Stonee2e80132018-01-16 15:37:33 +00001639 struct drm_plane_state *scanout_state;
Daniel Stonee95169b2016-11-14 17:46:59 +00001640 struct drm_plane *scanout_plane = output->scanout_plane;
Armin Krezović545dba62016-08-05 15:54:18 +02001641 struct drm_backend *b = to_drm_backend(c);
Daniel Stone95d48a22017-04-04 17:54:30 +01001642 struct drm_fb *fb;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001643
Daniel Stone4e84f7d2017-04-04 17:54:29 +01001644 /* If we already have a client buffer promoted to scanout, then we don't
1645 * want to render. */
Daniel Stonee2e80132018-01-16 15:37:33 +00001646 scanout_state = drm_output_state_get_plane(state,
1647 output->scanout_plane);
1648 if (scanout_state->fb)
Daniel Stone4e84f7d2017-04-04 17:54:29 +01001649 return;
1650
Daniel Stonee95169b2016-11-14 17:46:59 +00001651 if (!pixman_region32_not_empty(damage) &&
1652 scanout_plane->state_cur->fb &&
1653 (scanout_plane->state_cur->fb->type == BUFFER_GBM_SURFACE ||
1654 scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB) &&
1655 scanout_plane->state_cur->fb->width ==
1656 output->base.current_mode->width &&
1657 scanout_plane->state_cur->fb->height ==
1658 output->base.current_mode->height) {
1659 fb = drm_fb_ref(scanout_plane->state_cur->fb);
1660 } else if (b->use_pixman) {
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001661 fb = drm_output_render_pixman(state, damage);
Daniel Stonee95169b2016-11-14 17:46:59 +00001662 } else {
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001663 fb = drm_output_render_gl(state, damage);
Daniel Stonee95169b2016-11-14 17:46:59 +00001664 }
Daniel Stone95d48a22017-04-04 17:54:30 +01001665
Daniel Stonee2e80132018-01-16 15:37:33 +00001666 if (!fb) {
1667 drm_plane_state_put_back(scanout_state);
Daniel Stone95d48a22017-04-04 17:54:30 +01001668 return;
Daniel Stonee2e80132018-01-16 15:37:33 +00001669 }
1670
1671 scanout_state->fb = fb;
1672 scanout_state->output = output;
1673
1674 scanout_state->src_x = 0;
1675 scanout_state->src_y = 0;
1676 scanout_state->src_w = output->base.current_mode->width << 16;
1677 scanout_state->src_h = output->base.current_mode->height << 16;
1678
1679 scanout_state->dest_x = 0;
1680 scanout_state->dest_y = 0;
1681 scanout_state->dest_w = scanout_state->src_w >> 16;
1682 scanout_state->dest_h = scanout_state->src_h >> 16;
1683
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001684
Giulio Camuffo954f1832014-10-11 18:27:30 +03001685 pixman_region32_subtract(&c->primary_plane.damage,
1686 &c->primary_plane.damage, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001687}
1688
1689static void
Richard Hughese7299962013-05-01 21:52:12 +01001690drm_output_set_gamma(struct weston_output *output_base,
1691 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
1692{
1693 int rc;
Armin Krezović545dba62016-08-05 15:54:18 +02001694 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001695 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +02001696 to_drm_backend(output->base.compositor);
Richard Hughese7299962013-05-01 21:52:12 +01001697
1698 /* check */
1699 if (output_base->gamma_size != size)
1700 return;
1701 if (!output->original_crtc)
1702 return;
1703
Giulio Camuffo954f1832014-10-11 18:27:30 +03001704 rc = drmModeCrtcSetGamma(backend->drm.fd,
Richard Hughese7299962013-05-01 21:52:12 +01001705 output->crtc_id,
1706 size, r, g, b);
1707 if (rc)
1708 weston_log("set gamma failed: %m\n");
1709}
1710
Bryce Harringtonada4f072015-06-30 13:25:46 -07001711/* Determine the type of vblank synchronization to use for the output.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001712 *
Bryce Harringtonada4f072015-06-30 13:25:46 -07001713 * The pipe parameter indicates which CRTC is in use. Knowing this, we
1714 * can determine which vblank sequence type to use for it. Traditional
1715 * cards had only two CRTCs, with CRTC 0 using no special flags, and
1716 * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
1717 * parameter indicates this.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001718 *
Bryce Harringtonada4f072015-06-30 13:25:46 -07001719 * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
1720 * 0-31. If this is non-zero it indicates we're dealing with a
1721 * multi-gpu situation and we need to calculate the vblank sync
1722 * using DRM_BLANK_HIGH_CRTC_MASK.
1723 */
Pekka Paalanenc8a1ff02015-07-02 15:06:08 +03001724static unsigned int
1725drm_waitvblank_pipe(struct drm_output *output)
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +02001726{
1727 if (output->pipe > 1)
1728 return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
1729 DRM_VBLANK_HIGH_CRTC_MASK;
1730 else if (output->pipe > 0)
1731 return DRM_VBLANK_SECONDARY;
1732 else
1733 return 0;
1734}
1735
David Herrmann1edf44c2013-10-22 17:11:26 +02001736static int
Daniel Stonea08512f2016-11-08 17:46:10 +00001737drm_output_apply_state(struct drm_output_state *state)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001738{
Daniel Stonea08512f2016-11-08 17:46:10 +00001739 struct drm_output *output = state->output;
1740 struct drm_backend *backend = to_drm_backend(output->base.compositor);
Daniel Stonee2e80132018-01-16 15:37:33 +00001741 struct drm_plane *scanout_plane = output->scanout_plane;
Daniel Stonea08512f2016-11-08 17:46:10 +00001742 struct drm_property_info *dpms_prop =
1743 &output->props_conn[WDRM_CONNECTOR_DPMS];
Daniel Stonee2e80132018-01-16 15:37:33 +00001744 struct drm_plane_state *scanout_state;
Daniel Stonebc15f682016-11-14 16:57:01 +00001745 struct drm_plane_state *ps;
Daniel Stone085d2b92015-05-21 00:00:57 +01001746 struct drm_plane *p;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001747 struct drm_mode *mode;
Daniel Stonea08512f2016-11-08 17:46:10 +00001748 struct timespec now;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001749 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001750
Derek Foreman2cd87fe2017-04-13 13:48:48 -05001751 /* If disable_planes is set then assign_planes() wasn't
1752 * called for this render, so we could still have a stale
1753 * cursor plane set up.
1754 */
1755 if (output->base.disable_planes) {
1756 output->cursor_view = NULL;
Daniel Stone2ba17f42015-05-19 20:02:41 +01001757 output->cursor_plane->base.x = INT32_MIN;
1758 output->cursor_plane->base.y = INT32_MIN;
Derek Foreman2cd87fe2017-04-13 13:48:48 -05001759 }
1760
Daniel Stonea08512f2016-11-08 17:46:10 +00001761 if (state->dpms != WESTON_DPMS_ON) {
1762 wl_list_for_each(ps, &state->plane_list, link) {
1763 p = ps->plane;
1764 assert(ps->fb == NULL);
1765 assert(ps->output == NULL);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001766
Daniel Stonea08512f2016-11-08 17:46:10 +00001767 if (p->type != WDRM_PLANE_TYPE_OVERLAY)
1768 continue;
1769
1770 ret = drmModeSetPlane(backend->drm.fd, p->plane_id,
1771 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1772 if (ret)
1773 weston_log("drmModeSetPlane failed disable: %m\n");
1774 }
1775
1776 if (output->cursor_plane) {
1777 ret = drmModeSetCursor(backend->drm.fd, output->crtc_id,
1778 0, 0, 0);
1779 if (ret)
1780 weston_log("drmModeSetCursor failed disable: %m\n");
1781 }
1782
1783 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id, 0, 0, 0,
1784 &output->connector_id, 0, NULL);
1785 if (ret)
1786 weston_log("drmModeSetCrtc failed disabling: %m\n");
1787
1788 drm_output_assign_state(state, DRM_STATE_APPLY_SYNC);
1789 weston_compositor_read_presentation_clock(output->base.compositor, &now);
1790 drm_output_update_complete(output,
1791 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION,
1792 now.tv_sec, now.tv_nsec / 1000);
1793
1794 return 0;
1795 }
1796
1797 scanout_state =
1798 drm_output_state_get_existing_plane(state, scanout_plane);
Daniel Stone087ddf02017-02-14 17:51:30 +00001799
Daniel Stonee2e80132018-01-16 15:37:33 +00001800 /* The legacy SetCrtc API doesn't allow us to do scaling, and the
1801 * legacy PageFlip API doesn't allow us to do clipping either. */
1802 assert(scanout_state->src_x == 0);
1803 assert(scanout_state->src_y == 0);
1804 assert(scanout_state->src_w ==
1805 (unsigned) (output->base.current_mode->width << 16));
1806 assert(scanout_state->src_h ==
1807 (unsigned) (output->base.current_mode->height << 16));
1808 assert(scanout_state->dest_x == 0);
1809 assert(scanout_state->dest_y == 0);
1810 assert(scanout_state->dest_w == scanout_state->src_w >> 16);
1811 assert(scanout_state->dest_h == scanout_state->src_h >> 16);
1812
Daniel Stonecb04cc42016-11-16 11:51:27 +00001813 mode = to_drm_mode(output->base.current_mode);
Daniel Stone6020f472018-02-05 15:46:20 +00001814 if (backend->state_invalid || !scanout_plane->state_cur->fb ||
Daniel Stonee2e80132018-01-16 15:37:33 +00001815 scanout_plane->state_cur->fb->stride != scanout_state->fb->stride) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001816 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Daniel Stonee2e80132018-01-16 15:37:33 +00001817 scanout_state->fb->fb_id,
1818 0, 0,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001819 &output->connector_id, 1,
1820 &mode->mode_info);
1821 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +02001822 weston_log("set mode failed: %m\n");
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001823 goto err;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001824 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001825 }
1826
Giulio Camuffo954f1832014-10-11 18:27:30 +03001827 if (drmModePageFlip(backend->drm.fd, output->crtc_id,
Daniel Stonee2e80132018-01-16 15:37:33 +00001828 scanout_state->fb->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -05001829 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001830 weston_log("queueing pageflip failed: %m\n");
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001831 goto err;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -05001832 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001833
Daniel Stone205c0a02017-04-04 17:54:33 +01001834 assert(!output->page_flip_pending);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001835
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001836 if (output->pageflip_timer)
1837 wl_event_source_timer_update(output->pageflip_timer,
1838 backend->pageflip_timeout);
1839
Daniel Stone2ba17f42015-05-19 20:02:41 +01001840 drm_output_set_cursor(state);
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001841
Jesse Barnes58ef3792012-02-23 09:45:49 -05001842 /*
1843 * Now, update all the sprite surfaces
1844 */
Daniel Stonebc15f682016-11-14 16:57:01 +00001845 wl_list_for_each(ps, &state->plane_list, link) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001846 uint32_t flags = 0, fb_id = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001847 drmVBlank vbl = {
1848 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
1849 .request.sequence = 1,
1850 };
1851
Daniel Stonebc15f682016-11-14 16:57:01 +00001852 p = ps->plane;
Daniel Stone085d2b92015-05-21 00:00:57 +01001853 if (p->type != WDRM_PLANE_TYPE_OVERLAY)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001854 continue;
1855
Daniel Stonebc15f682016-11-14 16:57:01 +00001856 assert(p->state_cur->complete);
1857 assert(!!p->state_cur->output == !!p->state_cur->fb);
1858 assert(!p->state_cur->output || p->state_cur->output == output);
1859 assert(!ps->complete);
1860 assert(!ps->output || ps->output == output);
1861 assert(!!ps->output == !!ps->fb);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001862
Daniel Stonebc15f682016-11-14 16:57:01 +00001863 if (ps->fb && !backend->sprites_hidden)
1864 fb_id = ps->fb->fb_id;
Daniel Stone085d2b92015-05-21 00:00:57 +01001865
1866 ret = drmModeSetPlane(backend->drm.fd, p->plane_id,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001867 output->crtc_id, fb_id, flags,
Daniel Stonebc15f682016-11-14 16:57:01 +00001868 ps->dest_x, ps->dest_y,
1869 ps->dest_w, ps->dest_h,
1870 ps->src_x, ps->src_y,
1871 ps->src_w, ps->src_h);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001872 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +02001873 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05001874 ret, strerror(errno));
1875
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +02001876 vbl.request.type |= drm_waitvblank_pipe(output);
Rob Clark5ca1a472012-08-08 20:27:37 -05001877
Jesse Barnes58ef3792012-02-23 09:45:49 -05001878 /*
1879 * Queue a vblank signal so we know when the surface
1880 * becomes active on the display or has been replaced.
1881 */
Daniel Stonebc15f682016-11-14 16:57:01 +00001882 vbl.request.signal = (unsigned long) ps;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001883 ret = drmWaitVBlank(backend->drm.fd, &vbl);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001884 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +02001885 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05001886 ret, strerror(errno));
1887 }
1888 }
1889
Daniel Stonea08512f2016-11-08 17:46:10 +00001890 if (dpms_prop->prop_id && state->dpms != output->state_cur->dpms) {
1891 ret = drmModeConnectorSetProperty(backend->drm.fd,
1892 output->connector_id,
1893 dpms_prop->prop_id,
1894 state->dpms);
1895 if (ret) {
1896 weston_log("DRM: DPMS: failed property set for %s\n",
1897 output->base.name);
1898 }
1899 }
1900
1901 drm_output_assign_state(state, DRM_STATE_APPLY_ASYNC);
1902
David Herrmann1edf44c2013-10-22 17:11:26 +02001903 return 0;
1904
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001905err:
Kristian Høgsbergb3955b02014-01-23 16:25:06 -08001906 output->cursor_view = NULL;
Daniel Stone7b2ddac2016-11-11 19:11:49 +00001907 drm_output_state_free(state);
Daniel Stonea08512f2016-11-08 17:46:10 +00001908 return -1;
1909}
David Herrmann1edf44c2013-10-22 17:11:26 +02001910
Daniel Stonea08512f2016-11-08 17:46:10 +00001911/**
1912 * Applies all of a pending_state asynchronously: the primary entry point for
1913 * applying KMS state to a device. Updates the state for all outputs in the
1914 * pending_state, as well as disabling any unclaimed outputs.
1915 *
1916 * Unconditionally takes ownership of pending_state, and clears state_invalid.
1917 */
1918static int
1919drm_pending_state_apply(struct drm_pending_state *pending_state)
1920{
1921 struct drm_backend *b = pending_state->backend;
1922 struct drm_output_state *output_state, *tmp;
1923 uint32_t *unused;
1924
1925 if (b->state_invalid) {
1926 /* If we need to reset all our state (e.g. because we've
1927 * just started, or just been VT-switched in), explicitly
1928 * disable all the CRTCs we aren't using. This also disables
1929 * all connectors on these CRTCs, so we don't need to do that
1930 * separately with the pre-atomic API. */
1931 wl_array_for_each(unused, &b->unused_crtcs)
1932 drmModeSetCrtc(b->drm.fd, *unused, 0, 0, 0, NULL, 0,
1933 NULL);
1934 }
1935
1936 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
1937 link) {
1938 struct drm_output *output = output_state->output;
1939 int ret;
1940
1941 ret = drm_output_apply_state(output_state);
1942 if (ret != 0) {
1943 weston_log("Couldn't apply state for output %s\n",
1944 output->base.name);
1945 }
1946 }
1947
1948 b->state_invalid = false;
1949
1950 assert(wl_list_empty(&pending_state->output_list));
1951
1952 drm_pending_state_free(pending_state);
1953
1954 return 0;
1955}
1956
1957/**
1958 * The synchronous version of drm_pending_state_apply. May only be used to
1959 * disable outputs. Does so synchronously: the request is guaranteed to have
1960 * completed on return, and the output will not be touched afterwards.
1961 *
1962 * Unconditionally takes ownership of pending_state, and clears state_invalid.
1963 */
1964static int
1965drm_pending_state_apply_sync(struct drm_pending_state *pending_state)
1966{
1967 struct drm_backend *b = pending_state->backend;
1968 struct drm_output_state *output_state, *tmp;
1969 uint32_t *unused;
1970
1971 if (b->state_invalid) {
1972 /* If we need to reset all our state (e.g. because we've
1973 * just started, or just been VT-switched in), explicitly
1974 * disable all the CRTCs we aren't using. This also disables
1975 * all connectors on these CRTCs, so we don't need to do that
1976 * separately with the pre-atomic API. */
1977 wl_array_for_each(unused, &b->unused_crtcs)
1978 drmModeSetCrtc(b->drm.fd, *unused, 0, 0, 0, NULL, 0,
1979 NULL);
1980 }
1981
1982 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
1983 link) {
1984 int ret;
1985
1986 assert(output_state->dpms == WESTON_DPMS_OFF);
1987 ret = drm_output_apply_state(output_state);
1988 if (ret != 0) {
1989 weston_log("Couldn't apply state for output %s\n",
1990 output_state->output->base.name);
1991 }
1992 }
1993
1994 b->state_invalid = false;
1995
1996 assert(wl_list_empty(&pending_state->output_list));
1997
1998 drm_pending_state_free(pending_state);
1999
2000 return 0;
2001}
2002
2003static int
2004drm_output_repaint(struct weston_output *output_base,
2005 pixman_region32_t *damage,
2006 void *repaint_data)
2007{
2008 struct drm_pending_state *pending_state = repaint_data;
2009 struct drm_output *output = to_drm_output(output_base);
2010 struct drm_backend *backend = to_drm_backend(output_base->compositor);
2011 struct drm_output_state *state = NULL;
2012 struct drm_plane_state *scanout_state;
2013
2014 if (output->disable_pending || output->destroy_pending)
2015 goto err;
2016
2017 assert(!output->state_last);
2018
2019 /* If planes have been disabled in the core, we might not have
2020 * hit assign_planes at all, so might not have valid output state
2021 * here. */
2022 state = drm_pending_state_get_output(pending_state, output);
2023 if (!state)
2024 state = drm_output_state_duplicate(output->state_cur,
2025 pending_state,
2026 DRM_OUTPUT_STATE_CLEAR_PLANES);
2027 state->dpms = WESTON_DPMS_ON;
2028
2029 drm_output_render(state, damage);
2030 scanout_state = drm_output_state_get_plane(state,
2031 output->scanout_plane);
2032 if (!scanout_state || !scanout_state->fb)
2033 goto err;
2034
2035 wl_array_remove_uint32(&backend->unused_connectors,
2036 output->connector_id);
2037 wl_array_remove_uint32(&backend->unused_crtcs, output->crtc_id);
2038
2039 return 0;
2040
2041err:
2042 drm_output_state_free(state);
David Herrmann1edf44c2013-10-22 17:11:26 +02002043 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002044}
2045
2046static void
Jonas Ådahle5a12252013-04-05 23:07:11 +02002047drm_output_start_repaint_loop(struct weston_output *output_base)
2048{
Armin Krezović545dba62016-08-05 15:54:18 +02002049 struct drm_output *output = to_drm_output(output_base);
Daniel Stone8747f952016-11-29 20:17:32 +00002050 struct drm_pending_state *pending_state;
Daniel Stonee2e80132018-01-16 15:37:33 +00002051 struct drm_plane *scanout_plane = output->scanout_plane;
Armin Krezović545dba62016-08-05 15:54:18 +02002052 struct drm_backend *backend =
2053 to_drm_backend(output_base->compositor);
Mario Kleinerf507ec32015-06-21 21:25:14 +02002054 struct timespec ts, tnow;
2055 struct timespec vbl2now;
2056 int64_t refresh_nsec;
2057 int ret;
2058 drmVBlank vbl = {
2059 .request.type = DRM_VBLANK_RELATIVE,
2060 .request.sequence = 0,
2061 .request.signal = 0,
2062 };
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03002063
Armin Krezović08368132016-09-30 14:11:05 +02002064 if (output->disable_pending || output->destroy_pending)
Xiong Zhangabd5d472013-10-11 14:43:07 +08002065 return;
2066
Daniel Stonee2e80132018-01-16 15:37:33 +00002067 if (!output->scanout_plane->state_cur->fb) {
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03002068 /* We can't page flip if there's no mode set */
David Herrmann3c688c52013-10-22 17:11:25 +02002069 goto finish_frame;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03002070 }
2071
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03002072 /* Need to smash all state in from scratch; current timings might not
2073 * be what we want, page flip might not work, etc.
2074 */
Daniel Stone6020f472018-02-05 15:46:20 +00002075 if (backend->state_invalid)
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03002076 goto finish_frame;
2077
Daniel Stonee2e80132018-01-16 15:37:33 +00002078 assert(scanout_plane->state_cur->output == output);
2079
Mario Kleinerf507ec32015-06-21 21:25:14 +02002080 /* Try to get current msc and timestamp via instant query */
2081 vbl.request.type |= drm_waitvblank_pipe(output);
2082 ret = drmWaitVBlank(backend->drm.fd, &vbl);
2083
2084 /* Error ret or zero timestamp means failure to get valid timestamp */
2085 if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
2086 ts.tv_sec = vbl.reply.tval_sec;
2087 ts.tv_nsec = vbl.reply.tval_usec * 1000;
2088
2089 /* Valid timestamp for most recent vblank - not stale?
2090 * Stale ts could happen on Linux 3.17+, so make sure it
2091 * is not older than 1 refresh duration since now.
2092 */
2093 weston_compositor_read_presentation_clock(backend->compositor,
2094 &tnow);
2095 timespec_sub(&vbl2now, &tnow, &ts);
2096 refresh_nsec =
2097 millihz_to_nsec(output->base.current_mode->refresh);
2098 if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
2099 drm_output_update_msc(output, vbl.reply.sequence);
2100 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002101 WP_PRESENTATION_FEEDBACK_INVALID);
Mario Kleinerf507ec32015-06-21 21:25:14 +02002102 return;
2103 }
2104 }
2105
2106 /* Immediate query didn't provide valid timestamp.
2107 * Use pageflip fallback.
2108 */
Jonas Ådahle5a12252013-04-05 23:07:11 +02002109
Daniel Stone205c0a02017-04-04 17:54:33 +01002110 assert(!output->page_flip_pending);
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002111 assert(!output->state_last);
2112
2113 pending_state = drm_pending_state_alloc(backend);
Daniel Stone8747f952016-11-29 20:17:32 +00002114 drm_output_state_duplicate(output->state_cur, pending_state,
2115 DRM_OUTPUT_STATE_PRESERVE_PLANES);
Daniel Stone205c0a02017-04-04 17:54:33 +01002116
Daniel Stone8747f952016-11-29 20:17:32 +00002117 ret = drm_pending_state_apply(pending_state);
2118 if (ret != 0) {
2119 weston_log("applying repaint-start state failed: %m\n");
David Herrmann3c688c52013-10-22 17:11:25 +02002120 goto finish_frame;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002121 }
David Herrmann3c688c52013-10-22 17:11:25 +02002122
2123 return;
2124
2125finish_frame:
2126 /* if we cannot page-flip, immediately finish frame */
Daniel Stone3615ce12017-03-01 11:34:05 +00002127 weston_output_finish_frame(output_base, NULL,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002128 WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002129}
2130
2131static void
Pekka Paalanen641307c2014-09-23 22:08:47 -04002132drm_output_update_msc(struct drm_output *output, unsigned int seq)
2133{
2134 uint64_t msc_hi = output->base.msc >> 32;
2135
2136 if (seq < (output->base.msc & 0xffffffff))
2137 msc_hi++;
2138
2139 output->base.msc = (msc_hi << 32) + seq;
2140}
2141
2142static void
Jesse Barnes58ef3792012-02-23 09:45:49 -05002143vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
2144 void *data)
2145{
Daniel Stonebc15f682016-11-14 16:57:01 +00002146 struct drm_plane_state *ps = (struct drm_plane_state *) data;
2147 struct drm_output_state *os = ps->output_state;
2148 struct drm_output *output = os->output;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002149 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
2150 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03002151
Pekka Paalanen641307c2014-09-23 22:08:47 -04002152 drm_output_update_msc(output, frame);
Daniel Stone65d87d02017-04-04 17:54:32 +01002153 output->vblank_pending--;
2154 assert(output->vblank_pending >= 0);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002155
Daniel Stonebc15f682016-11-14 16:57:01 +00002156 assert(ps->fb);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03002157
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002158 if (output->page_flip_pending || output->vblank_pending)
2159 return;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00002160
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002161 drm_output_update_complete(output, flags, sec, usec);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002162}
2163
2164static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002165page_flip_handler(int fd, unsigned int frame,
2166 unsigned int sec, unsigned int usec, void *data)
2167{
Armin Krezović545dba62016-08-05 15:54:18 +02002168 struct drm_output *output = data;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002169 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
2170 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
2171 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002172
Pekka Paalanen641307c2014-09-23 22:08:47 -04002173 drm_output_update_msc(output, frame);
2174
Daniel Stone205c0a02017-04-04 17:54:33 +01002175 assert(output->page_flip_pending);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002176 output->page_flip_pending = 0;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002177
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002178 if (output->vblank_pending)
2179 return;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00002180
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002181 drm_output_update_complete(output, flags, sec, usec);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02002182}
2183
Daniel Stoneeedf84c2017-02-10 18:06:04 +00002184/**
2185 * Begin a new repaint cycle
2186 *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002187 * Called by the core compositor at the beginning of a repaint cycle. Creates
2188 * a new pending_state structure to own any output state created by individual
2189 * output repaint functions until the repaint is flushed or cancelled.
Daniel Stoneeedf84c2017-02-10 18:06:04 +00002190 */
2191static void *
2192drm_repaint_begin(struct weston_compositor *compositor)
2193{
2194 struct drm_backend *b = to_drm_backend(compositor);
2195 struct drm_pending_state *ret;
2196
2197 ret = drm_pending_state_alloc(b);
2198 b->repaint_data = ret;
2199
2200 return ret;
2201}
2202
2203/**
2204 * Flush a repaint set
2205 *
2206 * Called by the core compositor when a repaint cycle has been completed
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002207 * and should be flushed. Frees the pending state, transitioning ownership
2208 * of the output state from the pending state, to the update itself. When
2209 * the update completes (see drm_output_update_complete), the output
2210 * state will be freed.
Daniel Stoneeedf84c2017-02-10 18:06:04 +00002211 */
2212static void
2213drm_repaint_flush(struct weston_compositor *compositor, void *repaint_data)
2214{
2215 struct drm_backend *b = to_drm_backend(compositor);
2216 struct drm_pending_state *pending_state = repaint_data;
Daniel Stone6020f472018-02-05 15:46:20 +00002217
Daniel Stonea08512f2016-11-08 17:46:10 +00002218 drm_pending_state_apply(pending_state);
Daniel Stoneeedf84c2017-02-10 18:06:04 +00002219 b->repaint_data = NULL;
2220}
2221
2222/**
2223 * Cancel a repaint set
2224 *
2225 * Called by the core compositor when a repaint has finished, so the data
2226 * held across the repaint cycle should be discarded.
2227 */
2228static void
2229drm_repaint_cancel(struct weston_compositor *compositor, void *repaint_data)
2230{
2231 struct drm_backend *b = to_drm_backend(compositor);
2232 struct drm_pending_state *pending_state = repaint_data;
2233
2234 drm_pending_state_free(pending_state);
2235 b->repaint_data = NULL;
2236}
2237
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05002238static uint32_t
Daniel Stone08d4edf2017-04-04 17:54:34 +01002239drm_output_check_plane_format(struct drm_plane *p,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002240 struct weston_view *ev, struct gbm_bo *bo)
Jesse Barnes58ef3792012-02-23 09:45:49 -05002241{
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05002242 uint32_t i, format;
2243
2244 format = gbm_bo_get_format(bo);
2245
2246 if (format == GBM_FORMAT_ARGB8888) {
2247 pixman_region32_t r;
2248
Kristian Høgsberg63093a32013-03-01 14:29:16 -05002249 pixman_region32_init_rect(&r, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002250 ev->surface->width,
2251 ev->surface->height);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002252 pixman_region32_subtract(&r, &r, &ev->surface->opaque);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05002253
2254 if (!pixman_region32_not_empty(&r))
2255 format = GBM_FORMAT_XRGB8888;
2256
2257 pixman_region32_fini(&r);
2258 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05002259
Daniel Stone08d4edf2017-04-04 17:54:34 +01002260 for (i = 0; i < p->count_formats; i++)
2261 if (p->formats[i] == format)
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05002262 return format;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002263
2264 return 0;
2265}
2266
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002267static struct weston_plane *
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002268drm_output_prepare_overlay_view(struct drm_output_state *output_state,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002269 struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -05002270{
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002271 struct drm_output *output = output_state->output;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02002272 struct weston_compositor *ec = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02002273 struct drm_backend *b = to_drm_backend(ec);
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002274 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002275 struct wl_resource *buffer_resource;
Daniel Stone08d4edf2017-04-04 17:54:34 +01002276 struct drm_plane *p;
Daniel Stonebc15f682016-11-14 16:57:01 +00002277 struct drm_plane_state *state = NULL;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002278 struct linux_dmabuf_buffer *dmabuf;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002279 struct gbm_bo *bo;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002280 pixman_region32_t dest_rect, src_rect;
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02002281 pixman_box32_t *box, tbox;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002282 uint32_t format;
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04002283 wl_fixed_t sx1, sy1, sx2, sy2;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002284
Giulio Camuffo954f1832014-10-11 18:27:30 +03002285 if (b->sprites_are_broken)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002286 return NULL;
Kristian Høgsberg65bec242012-03-05 19:57:35 -05002287
Daniel Stone296d7a92016-10-21 18:05:37 +01002288 /* Don't import buffers which span multiple outputs. */
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02002289 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002290 return NULL;
Ander Conselvan de Oliveirad450b192012-06-26 17:09:12 +03002291
Daniel Stone296d7a92016-10-21 18:05:37 +01002292 /* We can only import GBM buffers. */
2293 if (b->gbm == NULL)
2294 return NULL;
2295
Jason Ekstranda7af7042013-10-12 22:38:11 -05002296 if (ev->surface->buffer_ref.buffer == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002297 return NULL;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002298 buffer_resource = ev->surface->buffer_ref.buffer->resource;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002299 if (wl_shm_buffer_get(buffer_resource))
Rob Clark702ffae2012-08-09 14:18:27 -05002300 return NULL;
2301
Daniel Stone296d7a92016-10-21 18:05:37 +01002302 if (viewport->buffer.transform != output->base.transform)
2303 return NULL;
2304 if (viewport->buffer.scale != output->base.current_scale)
2305 return NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002306 if (!drm_view_transform_supported(ev))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002307 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002308
Daniel Stone296d7a92016-10-21 18:05:37 +01002309 if (ev->alpha != 1.0f)
2310 return NULL;
2311
Daniel Stone085d2b92015-05-21 00:00:57 +01002312 wl_list_for_each(p, &b->plane_list, link) {
2313 if (p->type != WDRM_PLANE_TYPE_OVERLAY)
2314 continue;
2315
Daniel Stone5ff289a2017-10-07 12:59:02 +01002316 if (!drm_plane_is_available(p, output))
Daniel Stonebc15f682016-11-14 16:57:01 +00002317 continue;
2318
2319 state = drm_output_state_get_plane(output_state, p);
2320 if (state->fb) {
2321 state = NULL;
2322 continue;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002323 }
Daniel Stonebc15f682016-11-14 16:57:01 +00002324
2325 break;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002326 }
2327
2328 /* No sprites available */
Daniel Stonebc15f682016-11-14 16:57:01 +00002329 if (!state)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002330 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002331
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002332 if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) {
Bryce Harringtona3582072015-08-14 12:23:13 -07002333#ifdef HAVE_GBM_FD_IMPORT
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002334 /* XXX: TODO:
2335 *
2336 * Use AddFB2 directly, do not go via GBM.
2337 * Add support for multiplanar formats.
2338 * Both require refactoring in the DRM-backend to
2339 * support a mix of gbm_bos and drmfbs.
2340 */
2341 struct gbm_import_fd_data gbm_dmabuf = {
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00002342 .fd = dmabuf->attributes.fd[0],
2343 .width = dmabuf->attributes.width,
2344 .height = dmabuf->attributes.height,
2345 .stride = dmabuf->attributes.stride[0],
2346 .format = dmabuf->attributes.format
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002347 };
2348
Micah Fedkec8890122017-02-01 15:28:23 -05002349 /* XXX: TODO:
2350 *
2351 * Currently the buffer is rejected if any dmabuf attribute
2352 * flag is set. This keeps us from passing an inverted /
2353 * interlaced / bottom-first buffer (or any other type that may
2354 * be added in the future) through to an overlay. Ultimately,
2355 * these types of buffers should be handled through buffer
2356 * transforms and not as spot-checks requiring specific
2357 * knowledge. */
2358 if (dmabuf->attributes.n_planes != 1 ||
2359 dmabuf->attributes.offset[0] != 0 ||
2360 dmabuf->attributes.flags)
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002361 return NULL;
2362
2363 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf,
2364 GBM_BO_USE_SCANOUT);
Bryce Harringtona3582072015-08-14 12:23:13 -07002365#else
Daniel Stonebc15f682016-11-14 16:57:01 +00002366 goto err;
Bryce Harringtona3582072015-08-14 12:23:13 -07002367#endif
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03002368 } else {
2369 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
2370 buffer_resource, GBM_BO_USE_SCANOUT);
2371 }
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04002372 if (!bo)
Daniel Stonebc15f682016-11-14 16:57:01 +00002373 goto err;
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04002374
Daniel Stone08d4edf2017-04-04 17:54:34 +01002375 format = drm_output_check_plane_format(p, ev, bo);
Daniel Stonebc15f682016-11-14 16:57:01 +00002376 if (format == 0)
2377 goto err;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002378
Daniel Stonebc15f682016-11-14 16:57:01 +00002379 state->fb = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT);
2380 if (!state->fb)
2381 goto err;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02002382
Daniel Stonebc15f682016-11-14 16:57:01 +00002383 drm_fb_set_buffer(state->fb, ev->surface->buffer_ref.buffer);
2384
2385 state->output = output;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002386
Jason Ekstranda7af7042013-10-12 22:38:11 -05002387 box = pixman_region32_extents(&ev->transform.boundingbox);
Daniel Stone08d4edf2017-04-04 17:54:34 +01002388 p->base.x = box->x1;
2389 p->base.y = box->y1;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002390
Jesse Barnes58ef3792012-02-23 09:45:49 -05002391 /*
2392 * Calculate the source & dest rects properly based on actual
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002393 * position (note the caller has called weston_view_update_transform()
Jesse Barnes58ef3792012-02-23 09:45:49 -05002394 * for us already).
2395 */
2396 pixman_region32_init(&dest_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002397 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02002398 &output->base.region);
2399 pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002400 box = pixman_region32_extents(&dest_rect);
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02002401 tbox = weston_transformed_rect(output->base.width,
2402 output->base.height,
2403 output->base.transform,
2404 output->base.current_scale,
Alexander Larssond9a7bb72013-05-22 14:41:39 +02002405 *box);
Daniel Stonebc15f682016-11-14 16:57:01 +00002406 state->dest_x = tbox.x1;
2407 state->dest_y = tbox.y1;
2408 state->dest_w = tbox.x2 - tbox.x1;
2409 state->dest_h = tbox.y2 - tbox.y1;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002410 pixman_region32_fini(&dest_rect);
2411
2412 pixman_region32_init(&src_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002413 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02002414 &output->base.region);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002415 box = pixman_region32_extents(&src_rect);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04002416
Jason Ekstranda7af7042013-10-12 22:38:11 -05002417 weston_view_from_global_fixed(ev,
2418 wl_fixed_from_int(box->x1),
2419 wl_fixed_from_int(box->y1),
2420 &sx1, &sy1);
2421 weston_view_from_global_fixed(ev,
2422 wl_fixed_from_int(box->x2),
2423 wl_fixed_from_int(box->y2),
2424 &sx2, &sy2);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04002425
2426 if (sx1 < 0)
2427 sx1 = 0;
2428 if (sy1 < 0)
2429 sy1 = 0;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002430 if (sx2 > wl_fixed_from_int(ev->surface->width))
2431 sx2 = wl_fixed_from_int(ev->surface->width);
2432 if (sy2 > wl_fixed_from_int(ev->surface->height))
2433 sy2 = wl_fixed_from_int(ev->surface->height);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04002434
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02002435 tbox.x1 = sx1;
2436 tbox.y1 = sy1;
2437 tbox.x2 = sx2;
2438 tbox.y2 = sy2;
2439
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002440 tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width),
2441 wl_fixed_from_int(ev->surface->height),
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002442 viewport->buffer.transform,
2443 viewport->buffer.scale,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002444 tbox);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02002445
Daniel Stonebc15f682016-11-14 16:57:01 +00002446 state->src_x = tbox.x1 << 8;
2447 state->src_y = tbox.y1 << 8;
2448 state->src_w = (tbox.x2 - tbox.x1) << 8;
2449 state->src_h = (tbox.y2 - tbox.y1) << 8;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002450 pixman_region32_fini(&src_rect);
2451
Daniel Stone08d4edf2017-04-04 17:54:34 +01002452 return &p->base;
Daniel Stonebc15f682016-11-14 16:57:01 +00002453
2454err:
2455 drm_plane_state_put_back(state);
2456 if (bo)
2457 gbm_bo_destroy(bo);
2458 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002459}
2460
Pekka Paalanend0ead482014-06-16 12:05:40 +03002461/**
2462 * Update the image for the current cursor surface
2463 *
2464 * @param b DRM backend structure
2465 * @param bo GBM buffer object to write into
2466 * @param ev View to use for cursor image
2467 */
2468static void
2469cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo,
2470 struct weston_view *ev)
2471{
2472 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
2473 uint32_t buf[b->cursor_width * b->cursor_height];
2474 int32_t stride;
2475 uint8_t *s;
2476 int i;
2477
2478 assert(buffer && buffer->shm_buffer);
2479 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
2480 assert(ev->surface->width <= b->cursor_width);
2481 assert(ev->surface->height <= b->cursor_height);
2482
2483 memset(buf, 0, sizeof buf);
2484 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
2485 s = wl_shm_buffer_get_data(buffer->shm_buffer);
2486
2487 wl_shm_buffer_begin_access(buffer->shm_buffer);
2488 for (i = 0; i < ev->surface->height; i++)
2489 memcpy(buf + i * b->cursor_width,
2490 s + i * stride,
2491 ev->surface->width * 4);
2492 wl_shm_buffer_end_access(buffer->shm_buffer);
2493
2494 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
2495 weston_log("failed update cursor: %m\n");
2496}
2497
Daniel Stone2ba17f42015-05-19 20:02:41 +01002498static struct weston_plane *
2499drm_output_prepare_cursor_view(struct drm_output_state *output_state,
2500 struct weston_view *ev)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04002501{
Daniel Stone2ba17f42015-05-19 20:02:41 +01002502 struct drm_output *output = output_state->output;
Armin Krezović545dba62016-08-05 15:54:18 +02002503 struct drm_backend *b = to_drm_backend(output->base.compositor);
Daniel Stone2ba17f42015-05-19 20:02:41 +01002504 struct drm_plane *plane = output->cursor_plane;
2505 struct drm_plane_state *plane_state;
2506 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
2507 struct wl_shm_buffer *shmbuf;
2508 bool needs_update = false;
Derek Foremanbe428b32015-11-24 11:39:38 -06002509 float x, y;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05002510
Daniel Stone2ba17f42015-05-19 20:02:41 +01002511 if (!plane)
2512 return NULL;
2513
2514 if (b->cursors_are_broken)
2515 return NULL;
2516
2517 if (!plane->state_cur->complete)
2518 return NULL;
2519
2520 if (plane->state_cur->output && plane->state_cur->output != output)
2521 return NULL;
2522
2523 /* Don't import buffers which span multiple outputs. */
2524 if (ev->output_mask != (1u << output->base.id))
2525 return NULL;
2526
2527 /* We use GBM to import SHM buffers. */
2528 if (b->gbm == NULL)
2529 return NULL;
2530
2531 if (ev->surface->buffer_ref.buffer == NULL)
2532 return NULL;
2533 shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource);
2534 if (!shmbuf)
2535 return NULL;
2536 if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888)
2537 return NULL;
2538
2539 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
2540 return NULL;
2541 if (ev->transform.enabled &&
2542 (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE))
2543 return NULL;
2544 if (viewport->buffer.scale != output->base.current_scale)
2545 return NULL;
2546 if (ev->geometry.scissor_enabled)
2547 return NULL;
2548
2549 if (ev->surface->width > b->cursor_width ||
2550 ev->surface->height > b->cursor_height)
2551 return NULL;
2552
2553 plane_state =
2554 drm_output_state_get_plane(output_state, output->cursor_plane);
2555
2556 if (plane_state && plane_state->fb)
2557 return NULL;
2558
2559 /* Since we're setting plane state up front, we need to work out
2560 * whether or not we need to upload a new cursor. We can't use the
2561 * plane damage, since the planes haven't actually been calculated
2562 * yet: instead try to figure it out directly. KMS cursor planes are
2563 * pretty unique here, in that they lie partway between a Weston plane
2564 * (direct scanout) and a renderer. */
2565 if (ev != output->cursor_view ||
2566 pixman_region32_not_empty(&ev->surface->damage)) {
2567 output->current_cursor++;
2568 output->current_cursor =
2569 output->current_cursor %
2570 ARRAY_LENGTH(output->gbm_cursor_fb);
2571 needs_update = true;
2572 }
2573
2574 output->cursor_view = ev;
2575 weston_view_to_global_float(ev, 0, 0, &x, &y);
2576 plane->base.x = x;
2577 plane->base.y = y;
2578
2579 plane_state->fb =
2580 drm_fb_ref(output->gbm_cursor_fb[output->current_cursor]);
2581 plane_state->output = output;
2582 plane_state->src_x = 0;
2583 plane_state->src_y = 0;
2584 plane_state->src_w = b->cursor_width << 16;
2585 plane_state->src_h = b->cursor_height << 16;
2586 plane_state->dest_x = (x - output->base.x) * output->base.current_scale;
2587 plane_state->dest_y = (y - output->base.y) * output->base.current_scale;
2588 plane_state->dest_w = b->cursor_width;
2589 plane_state->dest_h = b->cursor_height;
2590
2591 if (needs_update)
2592 cursor_bo_update(b, plane_state->fb->bo, ev);
2593
2594 return &plane->base;
2595}
2596
2597static void
2598drm_output_set_cursor(struct drm_output_state *output_state)
2599{
2600 struct drm_output *output = output_state->output;
2601 struct drm_backend *b = to_drm_backend(output->base.compositor);
2602 struct drm_plane *plane = output->cursor_plane;
2603 struct drm_plane_state *state;
2604 EGLint handle;
2605 struct gbm_bo *bo;
2606
2607 if (!plane)
2608 return;
2609
2610 state = drm_output_state_get_existing_plane(output_state, plane);
2611 if (!state)
2612 return;
2613
2614 if (!state->fb) {
2615 pixman_region32_fini(&plane->base.damage);
2616 pixman_region32_init(&plane->base.damage);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002617 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg5626d342012-08-03 11:50:05 -04002618 return;
2619 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05002620
Daniel Stone2ba17f42015-05-19 20:02:41 +01002621 assert(state->fb == output->gbm_cursor_fb[output->current_cursor]);
2622 assert(!plane->state_cur->output || plane->state_cur->output == output);
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05002623
Daniel Stone2ba17f42015-05-19 20:02:41 +01002624 if (plane->state_cur->fb != state->fb) {
2625 bo = state->fb->bo;
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04002626 handle = gbm_bo_get_handle(bo).s32;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002627 if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle,
Daniel Stone2ba17f42015-05-19 20:02:41 +01002628 b->cursor_width, b->cursor_height)) {
Pekka Paalanenae29da22012-08-06 14:57:05 +03002629 weston_log("failed to set cursor: %m\n");
Daniel Stone2ba17f42015-05-19 20:02:41 +01002630 goto err;
Rob Clarkab5b1e32012-08-09 13:24:45 -05002631 }
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002632 }
2633
Daniel Stone2ba17f42015-05-19 20:02:41 +01002634 pixman_region32_fini(&plane->base.damage);
2635 pixman_region32_init(&plane->base.damage);
Pekka Paalanen7eaed402015-11-27 14:20:58 +02002636
Daniel Stone2ba17f42015-05-19 20:02:41 +01002637 if (drmModeMoveCursor(b->drm.fd, output->crtc_id,
2638 state->dest_x, state->dest_y)) {
Daniel Stonea7cba1d2017-04-04 17:54:21 +01002639 weston_log("failed to move cursor: %m\n");
Daniel Stone2ba17f42015-05-19 20:02:41 +01002640 goto err;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002641 }
Daniel Stone2ba17f42015-05-19 20:02:41 +01002642
2643 return;
2644
2645err:
2646 b->cursors_are_broken = 1;
2647 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05002648}
2649
Jesse Barnes58ef3792012-02-23 09:45:49 -05002650static void
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002651drm_assign_planes(struct weston_output *output_base, void *repaint_data)
Jesse Barnes58ef3792012-02-23 09:45:49 -05002652{
Armin Krezović545dba62016-08-05 15:54:18 +02002653 struct drm_backend *b = to_drm_backend(output_base->compositor);
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002654 struct drm_pending_state *pending_state = repaint_data;
Armin Krezović545dba62016-08-05 15:54:18 +02002655 struct drm_output *output = to_drm_output(output_base);
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002656 struct drm_output_state *state;
Daniel Stone2ba17f42015-05-19 20:02:41 +01002657 struct drm_plane_state *plane_state;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002658 struct weston_view *ev, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002659 pixman_region32_t overlap, surface_overlap;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002660 struct weston_plane *primary, *next_plane;
Matt Hoosierdf573032017-08-24 09:24:20 -05002661 bool picked_scanout = false;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002662
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002663 assert(!output->state_last);
2664 state = drm_output_state_duplicate(output->state_cur,
2665 pending_state,
2666 DRM_OUTPUT_STATE_CLEAR_PLANES);
2667
Jesse Barnes58ef3792012-02-23 09:45:49 -05002668 /*
2669 * Find a surface for each sprite in the output using some heuristics:
2670 * 1) size
2671 * 2) frequency of update
2672 * 3) opacity (though some hw might support alpha blending)
2673 * 4) clipping (this can be fixed with color keys)
2674 *
2675 * The idea is to save on blitting since this should save power.
2676 * If we can get a large video surface on the sprite for example,
2677 * the main display surface may not need to update at all, and
2678 * the client buffer can be used directly for the sprite surface
2679 * as we do for flipping full screen surfaces.
2680 */
2681 pixman_region32_init(&overlap);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002682 primary = &output_base->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002683
Giulio Camuffo954f1832014-10-11 18:27:30 +03002684 wl_list_for_each_safe(ev, next, &output_base->compositor->view_list, link) {
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02002685 struct weston_surface *es = ev->surface;
2686
2687 /* Test whether this buffer can ever go into a plane:
2688 * non-shm, or small enough to be a cursor.
2689 *
2690 * Also, keep a reference when using the pixman renderer.
2691 * That makes it possible to do a seamless switch to the GL
2692 * renderer and since the pixman renderer keeps a reference
2693 * to the buffer anyway, there is no side effects.
Pekka Paalanenccfeae22012-12-04 15:58:14 +02002694 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03002695 if (b->use_pixman ||
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02002696 (es->buffer_ref.buffer &&
2697 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
Derek Foreman87430472015-10-15 10:24:48 -05002698 (ev->surface->width <= b->cursor_width &&
2699 ev->surface->height <= b->cursor_height))))
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05002700 es->keep_buffer = true;
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02002701 else
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05002702 es->keep_buffer = false;
Pekka Paalanenccfeae22012-12-04 15:58:14 +02002703
Jesse Barnes58ef3792012-02-23 09:45:49 -05002704 pixman_region32_init(&surface_overlap);
2705 pixman_region32_intersect(&surface_overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002706 &ev->transform.boundingbox);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002707
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002708 next_plane = NULL;
Matt Hoosierdf573032017-08-24 09:24:20 -05002709 if (pixman_region32_not_empty(&surface_overlap) || picked_scanout)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002710 next_plane = primary;
2711 if (next_plane == NULL)
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002712 next_plane = drm_output_prepare_cursor_view(state, ev);
Matt Hoosierdf573032017-08-24 09:24:20 -05002713
2714 /* If a higher-stacked view already got assigned to scanout, it's incorrect to
2715 * assign a subsequent (lower-stacked) view to scanout.
2716 */
2717 if (next_plane == NULL) {
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002718 next_plane = drm_output_prepare_scanout_view(state, ev);
Matt Hoosierdf573032017-08-24 09:24:20 -05002719 if (next_plane)
2720 picked_scanout = true;
2721 }
2722
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002723 if (next_plane == NULL)
Daniel Stone7b2ddac2016-11-11 19:11:49 +00002724 next_plane = drm_output_prepare_overlay_view(state, ev);
Matt Hoosierdf573032017-08-24 09:24:20 -05002725
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002726 if (next_plane == NULL)
2727 next_plane = primary;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002728
Jason Ekstranda7af7042013-10-12 22:38:11 -05002729 weston_view_move_to_plane(ev, next_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002730
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002731 if (next_plane == primary)
Jesse Barnes58ef3792012-02-23 09:45:49 -05002732 pixman_region32_union(&overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002733 &ev->transform.boundingbox);
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04002734
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002735 if (next_plane == primary ||
Daniel Stone2ba17f42015-05-19 20:02:41 +01002736 (output->cursor_plane &&
2737 next_plane == &output->cursor_plane->base)) {
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002738 /* cursor plane involves a copy */
2739 ev->psf_flags = 0;
2740 } else {
2741 /* All other planes are a direct scanout of a
2742 * single client buffer.
2743 */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002744 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002745 }
2746
Jesse Barnes58ef3792012-02-23 09:45:49 -05002747 pixman_region32_fini(&surface_overlap);
2748 }
2749 pixman_region32_fini(&overlap);
Daniel Stone2ba17f42015-05-19 20:02:41 +01002750
2751 /* We rely on output->cursor_view being both an accurate reflection of
2752 * the cursor plane's state, but also being maintained across repaints
2753 * to avoid unnecessary damage uploads, per the comment in
2754 * drm_output_prepare_cursor_view. In the event that we go from having
2755 * a cursor view to not having a cursor view, we need to clear it. */
2756 if (output->cursor_view) {
2757 plane_state =
2758 drm_output_state_get_existing_plane(state,
2759 output->cursor_plane);
2760 if (!plane_state || !plane_state->fb)
2761 output->cursor_view = NULL;
2762 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05002763}
2764
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002765/**
2766 * Find the closest-matching mode for a given target
2767 *
2768 * Given a target mode, find the most suitable mode amongst the output's
2769 * current mode list to use, preferring the current mode if possible, to
2770 * avoid an expensive mode switch.
2771 *
2772 * @param output DRM output
2773 * @param target_mode Mode to attempt to match
2774 * @returns Pointer to a mode from the output's mode list
2775 */
Alex Wub7b8bda2012-04-17 17:20:48 +08002776static struct drm_mode *
2777choose_mode (struct drm_output *output, struct weston_mode *target_mode)
2778{
2779 struct drm_mode *tmp_mode = NULL, *mode;
2780
Hardeningff39efa2013-09-18 23:56:35 +02002781 if (output->base.current_mode->width == target_mode->width &&
2782 output->base.current_mode->height == target_mode->height &&
2783 (output->base.current_mode->refresh == target_mode->refresh ||
Alex Wub7b8bda2012-04-17 17:20:48 +08002784 target_mode->refresh == 0))
Daniel Stonecb04cc42016-11-16 11:51:27 +00002785 return to_drm_mode(output->base.current_mode);
Alex Wub7b8bda2012-04-17 17:20:48 +08002786
2787 wl_list_for_each(mode, &output->base.mode_list, base.link) {
2788 if (mode->mode_info.hdisplay == target_mode->width &&
2789 mode->mode_info.vdisplay == target_mode->height) {
Mario Kleiner872797c2015-06-21 21:25:09 +02002790 if (mode->base.refresh == target_mode->refresh ||
2791 target_mode->refresh == 0) {
Alex Wub7b8bda2012-04-17 17:20:48 +08002792 return mode;
Daniel Stonef556ebe2015-05-21 08:28:58 +01002793 } else if (!tmp_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08002794 tmp_mode = mode;
2795 }
2796 }
2797
2798 return tmp_mode;
2799}
2800
2801static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002802drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00002803static void
2804drm_output_fini_egl(struct drm_output *output);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002805static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002806drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00002807static void
2808drm_output_fini_pixman(struct drm_output *output);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02002809
2810static int
Alex Wub7b8bda2012-04-17 17:20:48 +08002811drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
2812{
Daniel Stone02d487a2017-10-07 14:01:45 +01002813 struct drm_output *output = to_drm_output(output_base);
2814 struct drm_backend *b = to_drm_backend(output_base->compositor);
2815 struct drm_mode *drm_mode = choose_mode(output, mode);
Alex Wub7b8bda2012-04-17 17:20:48 +08002816
2817 if (!drm_mode) {
Daniel Stone02d487a2017-10-07 14:01:45 +01002818 weston_log("%s: invalid resolution %dx%d\n",
2819 output_base->name, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +08002820 return -1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02002821 }
2822
Hardeningff39efa2013-09-18 23:56:35 +02002823 if (&drm_mode->base == output->base.current_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08002824 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08002825
Hardeningff39efa2013-09-18 23:56:35 +02002826 output->base.current_mode->flags = 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08002827
Hardeningff39efa2013-09-18 23:56:35 +02002828 output->base.current_mode = &drm_mode->base;
2829 output->base.current_mode->flags =
Alex Wub7b8bda2012-04-17 17:20:48 +08002830 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
2831
Daniel Stonef30a18c2017-04-04 17:54:31 +01002832 /* XXX: This drops our current buffer too early, before we've started
2833 * displaying it. Ideally this should be much more atomic and
2834 * integrated with a full repaint cycle, rather than doing a
2835 * sledgehammer modeswitch first, and only later showing new
2836 * content.
2837 */
Daniel Stone6020f472018-02-05 15:46:20 +00002838 b->state_invalid = true;
Alex Wub7b8bda2012-04-17 17:20:48 +08002839
Giulio Camuffo954f1832014-10-11 18:27:30 +03002840 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002841 drm_output_fini_pixman(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002842 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002843 weston_log("failed to init output pixman state with "
2844 "new mode\n");
2845 return -1;
2846 }
2847 } else {
Daniel Stone3e661f72016-11-04 17:24:06 +00002848 drm_output_fini_egl(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002849 if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002850 weston_log("failed to init output egl state with "
2851 "new mode");
2852 return -1;
2853 }
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02002854 }
2855
Alex Wub7b8bda2012-04-17 17:20:48 +08002856 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08002857}
2858
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002859static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002860on_drm_input(int fd, uint32_t mask, void *data)
2861{
2862 drmEventContext evctx;
2863
2864 memset(&evctx, 0, sizeof evctx);
Emil Velikov863e66b2017-04-04 18:07:34 +01002865 evctx.version = 2;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002866 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002867 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002868 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002869
2870 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002871}
2872
2873static int
Daniel Stoneefa504f2016-12-19 16:48:20 +00002874init_kms_caps(struct drm_backend *b)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002875{
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03002876 uint64_t cap;
Daniel Stoneefa504f2016-12-19 16:48:20 +00002877 int ret;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002878 clockid_t clk_id;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002879
Daniel Stoneefa504f2016-12-19 16:48:20 +00002880 weston_log("using %s\n", b->drm.filename);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002881
Daniel Stoneefa504f2016-12-19 16:48:20 +00002882 ret = drmGetCap(b->drm.fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03002883 if (ret == 0 && cap == 1)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002884 clk_id = CLOCK_MONOTONIC;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03002885 else
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002886 clk_id = CLOCK_REALTIME;
2887
Giulio Camuffo954f1832014-10-11 18:27:30 +03002888 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002889 weston_log("Error: failed to set presentation clock %d.\n",
2890 clk_id);
2891 return -1;
2892 }
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +02002893
Daniel Stoneefa504f2016-12-19 16:48:20 +00002894 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_WIDTH, &cap);
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03002895 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002896 b->cursor_width = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03002897 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03002898 b->cursor_width = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03002899
Daniel Stoneefa504f2016-12-19 16:48:20 +00002900 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_HEIGHT, &cap);
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03002901 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002902 b->cursor_height = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03002903 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03002904 b->cursor_height = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03002905
Daniel Stonebe1090b2017-09-06 17:29:57 +01002906 if (!getenv("WESTON_DISABLE_UNIVERSAL_PLANES")) {
2907 ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
2908 b->universal_planes = (ret == 0);
2909 }
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01002910 weston_log("DRM: %s universal planes\n",
2911 b->universal_planes ? "supports" : "does not support");
2912
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02002913 return 0;
2914}
2915
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002916static struct gbm_device *
2917create_gbm_device(int fd)
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02002918{
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002919 struct gbm_device *gbm;
Alexandru DAMIANbe0ac5b2013-10-02 17:51:05 +01002920
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002921 gl_renderer = weston_load_module("gl-renderer.so",
2922 "gl_renderer_interface");
2923 if (!gl_renderer)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002924 return NULL;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002925
2926 /* GBM will load a dri driver, but even though they need symbols from
2927 * libglapi, in some version of Mesa they are not linked to it. Since
2928 * only the gl-renderer module links to it, the call above won't make
2929 * these symbols globally available, and loading the DRI driver fails.
2930 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
2931 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
2932
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002933 gbm = gbm_create_device(fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002934
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002935 return gbm;
2936}
2937
Bryce Harringtonc056a982015-05-19 15:25:18 -07002938/* When initializing EGL, if the preferred buffer format isn't available
Bryce Harringtonb9939982016-04-15 20:28:26 -07002939 * we may be able to substitute an ARGB format for an XRGB one.
Derek Foremanc4cfe852015-05-15 12:12:40 -05002940 *
2941 * This returns 0 if substitution isn't possible, but 0 might be a
2942 * legitimate format for other EGL platforms, so the caller is
2943 * responsible for checking for 0 before calling gl_renderer->create().
2944 *
2945 * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689
2946 * but it's entirely possible we'll see this again on other implementations.
2947 */
2948static int
2949fallback_format_for(uint32_t format)
2950{
2951 switch (format) {
2952 case GBM_FORMAT_XRGB8888:
2953 return GBM_FORMAT_ARGB8888;
2954 case GBM_FORMAT_XRGB2101010:
2955 return GBM_FORMAT_ARGB2101010;
2956 default:
2957 return 0;
2958 }
2959}
2960
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002961static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002962drm_backend_create_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002963{
Derek Foreman6d556372015-11-04 14:47:33 -06002964 EGLint format[3] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002965 b->gbm_format,
2966 fallback_format_for(b->gbm_format),
Derek Foreman6d556372015-11-04 14:47:33 -06002967 0,
Derek Foremanc4cfe852015-05-15 12:12:40 -05002968 };
Derek Foreman6d556372015-11-04 14:47:33 -06002969 int n_formats = 2;
John Kåre Alsakeref591aa2013-03-02 12:27:39 +01002970
Derek Foremanc4cfe852015-05-15 12:12:40 -05002971 if (format[1])
Derek Foreman6d556372015-11-04 14:47:33 -06002972 n_formats = 3;
Miguel A. Vicodddc6702016-05-18 17:41:07 +02002973 if (gl_renderer->display_create(b->compositor,
2974 EGL_PLATFORM_GBM_KHR,
2975 (void *)b->gbm,
Miguel A. Vico41700e32016-05-18 17:47:59 +02002976 NULL,
Miguel A. Vicodddc6702016-05-18 17:41:07 +02002977 gl_renderer->opaque_attribs,
2978 format,
2979 n_formats) < 0) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002980 return -1;
2981 }
2982
2983 return 0;
2984}
2985
2986static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002987init_egl(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002988{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002989 b->gbm = create_gbm_device(b->drm.fd);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002990
Giulio Camuffo954f1832014-10-11 18:27:30 +03002991 if (!b->gbm)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002992 return -1;
2993
Giulio Camuffo954f1832014-10-11 18:27:30 +03002994 if (drm_backend_create_gl_renderer(b) < 0) {
2995 gbm_device_destroy(b->gbm);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002996 return -1;
2997 }
2998
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002999 return 0;
3000}
3001
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003002static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003003init_pixman(struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003004{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003005 return pixman_renderer_init(b->compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003006}
3007
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003008/**
Pekka Paalanenec272712014-06-05 11:22:25 +03003009 * Create a drm_plane for a hardware plane
3010 *
3011 * Creates one drm_plane structure for a hardware plane, and initialises its
3012 * properties and formats.
3013 *
Daniel Stone2ba17f42015-05-19 20:02:41 +01003014 * In the absence of universal plane support, where KMS does not explicitly
3015 * expose the primary and cursor planes to userspace, this may also create
3016 * an 'internal' plane for internal management.
3017 *
Pekka Paalanenec272712014-06-05 11:22:25 +03003018 * This function does not add the plane to the list of usable planes in Weston
3019 * itself; the caller is responsible for this.
3020 *
3021 * Call drm_plane_destroy to clean up the plane.
3022 *
Daniel Stone2ba17f42015-05-19 20:02:41 +01003023 * @sa drm_output_find_special_plane
Pekka Paalanenec272712014-06-05 11:22:25 +03003024 * @param b DRM compositor backend
Daniel Stone2ba17f42015-05-19 20:02:41 +01003025 * @param kplane DRM plane to create, or NULL if creating internal plane
3026 * @param output Output to create internal plane for, or NULL
3027 * @param type Type to use when creating internal plane, or invalid
3028 * @param format Format to use for internal planes, or 0
Pekka Paalanenec272712014-06-05 11:22:25 +03003029 */
3030static struct drm_plane *
Daniel Stone2ba17f42015-05-19 20:02:41 +01003031drm_plane_create(struct drm_backend *b, const drmModePlane *kplane,
3032 struct drm_output *output, enum wdrm_plane_type type,
3033 uint32_t format)
Pekka Paalanenec272712014-06-05 11:22:25 +03003034{
3035 struct drm_plane *plane;
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01003036 drmModeObjectProperties *props;
Daniel Stone2ba17f42015-05-19 20:02:41 +01003037 int num_formats = (kplane) ? kplane->count_formats : 1;
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01003038
3039 static struct drm_property_enum_info plane_type_enums[] = {
3040 [WDRM_PLANE_TYPE_PRIMARY] = {
3041 .name = "Primary",
3042 },
3043 [WDRM_PLANE_TYPE_OVERLAY] = {
3044 .name = "Overlay",
3045 },
3046 [WDRM_PLANE_TYPE_CURSOR] = {
3047 .name = "Cursor",
3048 },
3049 };
3050 static const struct drm_property_info plane_props[] = {
3051 [WDRM_PLANE_TYPE] = {
3052 .name = "type",
3053 .enum_values = plane_type_enums,
3054 .num_enum_values = WDRM_PLANE_TYPE__COUNT,
3055 },
3056 };
Pekka Paalanenec272712014-06-05 11:22:25 +03003057
Daniel Stone2ba17f42015-05-19 20:02:41 +01003058 plane = zalloc(sizeof(*plane) +
3059 (sizeof(uint32_t) * num_formats));
Pekka Paalanenec272712014-06-05 11:22:25 +03003060 if (!plane) {
3061 weston_log("%s: out of memory\n", __func__);
3062 return NULL;
3063 }
3064
3065 plane->backend = b;
Daniel Stonebc15f682016-11-14 16:57:01 +00003066 plane->state_cur = drm_plane_state_alloc(NULL, plane);
3067 plane->state_cur->complete = true;
Pekka Paalanenec272712014-06-05 11:22:25 +03003068
Daniel Stone2ba17f42015-05-19 20:02:41 +01003069 if (kplane) {
3070 plane->possible_crtcs = kplane->possible_crtcs;
3071 plane->plane_id = kplane->plane_id;
3072 plane->count_formats = kplane->count_formats;
3073 memcpy(plane->formats, kplane->formats,
3074 kplane->count_formats * sizeof(kplane->formats[0]));
3075
3076 props = drmModeObjectGetProperties(b->drm.fd, kplane->plane_id,
3077 DRM_MODE_OBJECT_PLANE);
3078 if (!props) {
3079 weston_log("couldn't get plane properties\n");
3080 goto err;
3081 }
3082 drm_property_info_populate(b, plane_props, plane->props,
3083 WDRM_PLANE__COUNT, props);
3084 plane->type =
3085 drm_property_get_value(&plane->props[WDRM_PLANE_TYPE],
3086 props,
3087 WDRM_PLANE_TYPE__COUNT);
3088 drmModeFreeObjectProperties(props);
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01003089 }
Daniel Stone2ba17f42015-05-19 20:02:41 +01003090 else {
3091 plane->possible_crtcs = (1 << output->pipe);
3092 plane->plane_id = 0;
3093 plane->count_formats = 1;
3094 plane->formats[0] = format;
3095 plane->type = type;
3096 }
3097
3098 if (plane->type == WDRM_PLANE_TYPE__COUNT)
3099 goto err_props;
3100
3101 /* With universal planes, everything is a DRM plane; without
3102 * universal planes, the only DRM planes are overlay planes.
3103 * Everything else is a fake plane. */
3104 if (b->universal_planes) {
3105 assert(kplane);
3106 } else {
3107 if (kplane)
3108 assert(plane->type == WDRM_PLANE_TYPE_OVERLAY);
3109 else
3110 assert(plane->type != WDRM_PLANE_TYPE_OVERLAY &&
3111 output);
3112 }
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01003113
Pekka Paalanenec272712014-06-05 11:22:25 +03003114 weston_plane_init(&plane->base, b->compositor, 0, 0);
Daniel Stone085d2b92015-05-21 00:00:57 +01003115 wl_list_insert(&b->plane_list, &plane->link);
Pekka Paalanenec272712014-06-05 11:22:25 +03003116
3117 return plane;
Daniel Stone2ba17f42015-05-19 20:02:41 +01003118
3119err_props:
3120 drm_property_info_free(plane->props, WDRM_PLANE__COUNT);
3121err:
3122 drm_plane_state_free(plane->state_cur, true);
3123 free(plane);
3124 return NULL;
3125}
3126
3127/**
3128 * Find, or create, a special-purpose plane
3129 *
3130 * Primary and cursor planes are a special case, in that before universal
3131 * planes, they are driven by non-plane API calls. Without universal plane
3132 * support, the only way to configure a primary plane is via drmModeSetCrtc,
3133 * and the only way to configure a cursor plane is drmModeSetCursor2.
3134 *
3135 * Although they may actually be regular planes in the hardware, without
3136 * universal plane support, these planes are not actually exposed to
3137 * userspace in the regular plane list.
3138 *
3139 * However, for ease of internal tracking, we want to manage all planes
3140 * through the same drm_plane structures. Therefore, when we are running
3141 * without universal plane support, we create fake drm_plane structures
3142 * to track these planes.
3143 *
3144 * @param b DRM backend
3145 * @param output Output to use for plane
3146 * @param type Type of plane
3147 */
3148static struct drm_plane *
3149drm_output_find_special_plane(struct drm_backend *b, struct drm_output *output,
3150 enum wdrm_plane_type type)
3151{
3152 struct drm_plane *plane;
3153
3154 if (!b->universal_planes) {
3155 uint32_t format;
3156
3157 switch (type) {
3158 case WDRM_PLANE_TYPE_CURSOR:
3159 format = GBM_FORMAT_ARGB8888;
3160 break;
Daniel Stonee2e80132018-01-16 15:37:33 +00003161 case WDRM_PLANE_TYPE_PRIMARY:
3162 /* We don't know what formats the primary plane supports
3163 * before universal planes, so we just assume that the
3164 * GBM format works; however, this isn't set until after
3165 * the output is created. */
3166 format = 0;
3167 break;
Daniel Stone2ba17f42015-05-19 20:02:41 +01003168 default:
3169 assert(!"invalid type in drm_output_find_special_plane");
3170 break;
3171 }
3172
3173 return drm_plane_create(b, NULL, output, type, format);
3174 }
3175
3176 wl_list_for_each(plane, &b->plane_list, link) {
3177 struct drm_output *tmp;
3178 bool found_elsewhere = false;
3179
3180 if (plane->type != type)
3181 continue;
3182 if (!drm_plane_is_available(plane, output))
3183 continue;
3184
3185 /* On some platforms, primary/cursor planes can roam
3186 * between different CRTCs, so make sure we don't claim the
3187 * same plane for two outputs. */
3188 wl_list_for_each(tmp, &b->compositor->pending_output_list,
3189 base.link) {
Daniel Stonee2e80132018-01-16 15:37:33 +00003190 if (tmp->cursor_plane == plane ||
3191 tmp->scanout_plane == plane) {
Daniel Stone2ba17f42015-05-19 20:02:41 +01003192 found_elsewhere = true;
3193 break;
3194 }
3195 }
3196 wl_list_for_each(tmp, &b->compositor->output_list,
3197 base.link) {
Daniel Stonee2e80132018-01-16 15:37:33 +00003198 if (tmp->cursor_plane == plane ||
3199 tmp->scanout_plane == plane) {
Daniel Stone2ba17f42015-05-19 20:02:41 +01003200 found_elsewhere = true;
3201 break;
3202 }
3203 }
3204
3205 if (found_elsewhere)
3206 continue;
3207
3208 plane->possible_crtcs = (1 << output->pipe);
3209 return plane;
3210 }
3211
3212 return NULL;
Pekka Paalanenec272712014-06-05 11:22:25 +03003213}
3214
3215/**
3216 * Destroy one DRM plane
3217 *
3218 * Destroy a DRM plane, removing it from screen and releasing its retained
3219 * buffers in the process. The counterpart to drm_plane_create.
3220 *
3221 * @param plane Plane to deallocate (will be freed)
3222 */
3223static void
3224drm_plane_destroy(struct drm_plane *plane)
3225{
Daniel Stone2ba17f42015-05-19 20:02:41 +01003226 if (plane->type == WDRM_PLANE_TYPE_OVERLAY)
3227 drmModeSetPlane(plane->backend->drm.fd, plane->plane_id,
3228 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
Daniel Stonebc15f682016-11-14 16:57:01 +00003229 drm_plane_state_free(plane->state_cur, true);
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01003230 drm_property_info_free(plane->props, WDRM_PLANE__COUNT);
Pekka Paalanenec272712014-06-05 11:22:25 +03003231 weston_plane_release(&plane->base);
3232 wl_list_remove(&plane->link);
3233 free(plane);
3234}
3235
3236/**
3237 * Initialise sprites (overlay planes)
3238 *
3239 * Walk the list of provided DRM planes, and add overlay planes.
3240 *
3241 * Call destroy_sprites to free these planes.
3242 *
3243 * @param b DRM compositor backend
3244 */
3245static void
3246create_sprites(struct drm_backend *b)
3247{
3248 drmModePlaneRes *kplane_res;
3249 drmModePlane *kplane;
3250 struct drm_plane *drm_plane;
3251 uint32_t i;
3252
3253 kplane_res = drmModeGetPlaneResources(b->drm.fd);
3254 if (!kplane_res) {
3255 weston_log("failed to get plane resources: %s\n",
3256 strerror(errno));
3257 return;
3258 }
3259
3260 for (i = 0; i < kplane_res->count_planes; i++) {
3261 kplane = drmModeGetPlane(b->drm.fd, kplane_res->planes[i]);
3262 if (!kplane)
3263 continue;
3264
Daniel Stone2ba17f42015-05-19 20:02:41 +01003265 drm_plane = drm_plane_create(b, kplane, NULL,
3266 WDRM_PLANE_TYPE__COUNT, 0);
Pekka Paalanenec272712014-06-05 11:22:25 +03003267 drmModeFreePlane(kplane);
3268 if (!drm_plane)
3269 continue;
3270
Daniel Stone085d2b92015-05-21 00:00:57 +01003271 if (drm_plane->type == WDRM_PLANE_TYPE_OVERLAY)
3272 weston_compositor_stack_plane(b->compositor,
3273 &drm_plane->base,
3274 &b->compositor->primary_plane);
Pekka Paalanenec272712014-06-05 11:22:25 +03003275 }
3276
3277 drmModeFreePlaneResources(kplane_res);
3278}
3279
3280/**
3281 * Clean up sprites (overlay planes)
3282 *
3283 * The counterpart to create_sprites.
3284 *
3285 * @param b DRM compositor backend
3286 */
3287static void
3288destroy_sprites(struct drm_backend *b)
3289{
3290 struct drm_plane *plane, *next;
3291
Daniel Stone085d2b92015-05-21 00:00:57 +01003292 wl_list_for_each_safe(plane, next, &b->plane_list, link)
Pekka Paalanenec272712014-06-05 11:22:25 +03003293 drm_plane_destroy(plane);
3294}
3295
3296/**
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003297 * Add a mode to output's mode list
3298 *
3299 * Copy the supplied DRM mode into a Weston mode structure, and add it to the
3300 * output's mode list.
3301 *
3302 * @param output DRM output to add mode to
3303 * @param info DRM mode structure to add
3304 * @returns Newly-allocated Weston/DRM mode structure
3305 */
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03003306static struct drm_mode *
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003307drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003308{
3309 struct drm_mode *mode;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04003310 uint64_t refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003311
3312 mode = malloc(sizeof *mode);
3313 if (mode == NULL)
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03003314 return NULL;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003315
3316 mode->base.flags = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02003317 mode->base.width = info->hdisplay;
3318 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04003319
3320 /* Calculate higher precision (mHz) refresh rate */
3321 refresh = (info->clock * 1000000LL / info->htotal +
3322 info->vtotal / 2) / info->vtotal;
3323
3324 if (info->flags & DRM_MODE_FLAG_INTERLACE)
3325 refresh *= 2;
3326 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
3327 refresh /= 2;
3328 if (info->vscan > 1)
3329 refresh /= info->vscan;
3330
3331 mode->base.refresh = refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003332 mode->mode_info = *info;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04003333
3334 if (info->type & DRM_MODE_TYPE_PREFERRED)
3335 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
3336
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003337 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
3338
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03003339 return mode;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003340}
3341
3342static int
3343drm_subpixel_to_wayland(int drm_value)
3344{
3345 switch (drm_value) {
3346 default:
3347 case DRM_MODE_SUBPIXEL_UNKNOWN:
3348 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
3349 case DRM_MODE_SUBPIXEL_NONE:
3350 return WL_OUTPUT_SUBPIXEL_NONE;
3351 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
3352 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
3353 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
3354 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
3355 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
3356 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
3357 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
3358 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
3359 }
3360}
3361
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003362/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003363static uint32_t
3364drm_get_backlight(struct drm_output *output)
3365{
3366 long brightness, max_brightness, norm;
3367
3368 brightness = backlight_get_brightness(output->backlight);
3369 max_brightness = backlight_get_max_brightness(output->backlight);
3370
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003371 /* convert it on a scale of 0 to 255 */
3372 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003373
3374 return (uint32_t) norm;
3375}
3376
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003377/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003378static void
3379drm_set_backlight(struct weston_output *output_base, uint32_t value)
3380{
Armin Krezović545dba62016-08-05 15:54:18 +02003381 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003382 long max_brightness, new_brightness;
3383
3384 if (!output->backlight)
3385 return;
3386
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003387 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003388 return;
3389
3390 max_brightness = backlight_get_max_brightness(output->backlight);
3391
3392 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003393 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003394
3395 backlight_set_brightness(output->backlight, new_brightness);
3396}
3397
Daniel Stonea08512f2016-11-08 17:46:10 +00003398/**
3399 * Power output on or off
3400 *
3401 * The DPMS/power level of an output is used to switch it on or off. This
3402 * is DRM's hook for doing so, which can called either as part of repaint,
3403 * or independently of the repaint loop.
3404 *
3405 * If we are called as part of repaint, we simply set the relevant bit in
3406 * state and return.
3407 */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003408static void
3409drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
3410{
Armin Krezović545dba62016-08-05 15:54:18 +02003411 struct drm_output *output = to_drm_output(output_base);
Daniel Stonea08512f2016-11-08 17:46:10 +00003412 struct drm_backend *b = to_drm_backend(output_base->compositor);
3413 struct drm_pending_state *pending_state = b->repaint_data;
3414 struct drm_output_state *state;
Daniel Stone36609c72015-06-18 07:49:02 +01003415 int ret;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003416
Daniel Stonea08512f2016-11-08 17:46:10 +00003417 if (output->state_cur->dpms == level)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003418 return;
3419
Daniel Stonea08512f2016-11-08 17:46:10 +00003420 /* If we're being called during the repaint loop, then this is
3421 * simple: discard any previously-generated state, and create a new
3422 * state where we disable everything. When we come to flush, this
3423 * will be applied.
3424 *
3425 * However, we need to be careful: we can be called whilst another
3426 * output is in its repaint cycle (pending_state exists), but our
3427 * output still has an incomplete state application outstanding.
3428 * In that case, we need to wait until that completes. */
3429 if (pending_state && !output->state_last) {
3430 /* The repaint loop already sets DPMS on; we don't need to
3431 * explicitly set it on here, as it will already happen
3432 * whilst applying the repaint state. */
3433 if (level == WESTON_DPMS_ON)
3434 return;
3435
3436 state = drm_pending_state_get_output(pending_state, output);
3437 if (state)
3438 drm_output_state_free(state);
3439 state = drm_output_get_disable_state(pending_state, output);
Daniel Stone36609c72015-06-18 07:49:02 +01003440 return;
3441 }
3442
Daniel Stonea08512f2016-11-08 17:46:10 +00003443 /* As we throw everything away when disabling, just send us back through
3444 * a repaint cycle. */
3445 if (level == WESTON_DPMS_ON) {
3446 if (output->dpms_off_pending)
3447 output->dpms_off_pending = 0;
3448 weston_output_schedule_repaint(output_base);
3449 return;
3450 }
3451
3452 /* If we've already got a request in the pipeline, then we need to
3453 * park our DPMS request until that request has quiesced. */
3454 if (output->state_last) {
3455 output->dpms_off_pending = 1;
3456 return;
3457 }
3458
3459 pending_state = drm_pending_state_alloc(b);
3460 drm_output_get_disable_state(pending_state, output);
3461 ret = drm_pending_state_apply_sync(pending_state);
3462 if (ret != 0)
3463 weston_log("drm_set_dpms: couldn't disable output?\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003464}
3465
Pekka Paalanen3ce63622014-06-04 16:29:49 +03003466static const char * const connector_type_names[] = {
Pekka Paalanen89c49b32015-08-19 15:25:57 +03003467 [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
3468 [DRM_MODE_CONNECTOR_VGA] = "VGA",
3469 [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
3470 [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
3471 [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
3472 [DRM_MODE_CONNECTOR_Composite] = "Composite",
3473 [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
3474 [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
3475 [DRM_MODE_CONNECTOR_Component] = "Component",
3476 [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
3477 [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
3478 [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
3479 [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
3480 [DRM_MODE_CONNECTOR_TV] = "TV",
3481 [DRM_MODE_CONNECTOR_eDP] = "eDP",
Pekka Paalanenab81f152015-08-24 14:27:07 +03003482#ifdef DRM_MODE_CONNECTOR_DSI
Pekka Paalanen89c49b32015-08-19 15:25:57 +03003483 [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
3484 [DRM_MODE_CONNECTOR_DSI] = "DSI",
Pekka Paalanenab81f152015-08-24 14:27:07 +03003485#endif
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04003486};
3487
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03003488/** Create a name given a DRM connector
3489 *
3490 * \param con The DRM connector whose type and id form the name.
3491 * \return A newly allocate string, or NULL on error. Must be free()'d
3492 * after use.
3493 *
3494 * The name does not identify the DRM display device.
3495 */
Pekka Paalanen3ce63622014-06-04 16:29:49 +03003496static char *
3497make_connector_name(const drmModeConnector *con)
3498{
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03003499 char *name;
Pekka Paalanen89c49b32015-08-19 15:25:57 +03003500 const char *type_name = NULL;
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03003501 int ret;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03003502
3503 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
3504 type_name = connector_type_names[con->connector_type];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03003505
3506 if (!type_name)
3507 type_name = "UNNAMED";
3508
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03003509 ret = asprintf(&name, "%s-%d", type_name, con->connector_type_id);
3510 if (ret < 0)
3511 return NULL;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03003512
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03003513 return name;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03003514}
3515
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003516static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003517find_crtc_for_connector(struct drm_backend *b,
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003518 drmModeRes *resources, drmModeConnector *connector)
3519{
3520 drmModeEncoder *encoder;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003521 int i, j;
Daniel Stone75487c22017-01-16 14:33:38 +00003522 int ret = -1;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003523
3524 for (j = 0; j < connector->count_encoders; j++) {
Daniel Stone75487c22017-01-16 14:33:38 +00003525 uint32_t possible_crtcs, encoder_id, crtc_id;
3526
Giulio Camuffo954f1832014-10-11 18:27:30 +03003527 encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003528 if (encoder == NULL) {
3529 weston_log("Failed to get encoder.\n");
Daniel Stone75487c22017-01-16 14:33:38 +00003530 continue;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003531 }
Daniel Stone75487c22017-01-16 14:33:38 +00003532 encoder_id = encoder->encoder_id;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003533 possible_crtcs = encoder->possible_crtcs;
Daniel Stone75487c22017-01-16 14:33:38 +00003534 crtc_id = encoder->crtc_id;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003535 drmModeFreeEncoder(encoder);
3536
3537 for (i = 0; i < resources->count_crtcs; i++) {
Daniel Stone72c0e1b2017-02-09 13:49:15 +00003538 if (!(possible_crtcs & (1 << i)))
3539 continue;
3540
3541 if (drm_output_find_by_crtc(b, resources->crtcs[i]))
3542 continue;
3543
Daniel Stone75487c22017-01-16 14:33:38 +00003544 /* Try to preserve the existing
3545 * CRTC -> encoder -> connector routing; it makes
3546 * initialisation faster, and also since we have a
3547 * very dumb picking algorithm, may preserve a better
3548 * choice. */
3549 if (!connector->encoder_id ||
3550 (encoder_id == connector->encoder_id &&
3551 crtc_id == resources->crtcs[i]))
3552 return i;
3553
3554 ret = i;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003555 }
3556 }
3557
Daniel Stone75487c22017-01-16 14:33:38 +00003558 return ret;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003559}
3560
Daniel Stonee4256832017-04-04 17:54:27 +01003561static void drm_output_fini_cursor_egl(struct drm_output *output)
3562{
3563 unsigned int i;
3564
3565 for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) {
3566 drm_fb_unref(output->gbm_cursor_fb[i]);
3567 output->gbm_cursor_fb[i] = NULL;
3568 }
3569}
3570
3571static int
3572drm_output_init_cursor_egl(struct drm_output *output, struct drm_backend *b)
3573{
3574 unsigned int i;
3575
Daniel Stone2ba17f42015-05-19 20:02:41 +01003576 /* No point creating cursors if we don't have a plane for them. */
3577 if (!output->cursor_plane)
3578 return 0;
3579
Daniel Stonee4256832017-04-04 17:54:27 +01003580 for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) {
3581 struct gbm_bo *bo;
3582
3583 bo = gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
3584 GBM_FORMAT_ARGB8888,
3585 GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE);
3586 if (!bo)
3587 goto err;
3588
3589 output->gbm_cursor_fb[i] =
3590 drm_fb_get_from_bo(bo, b, GBM_FORMAT_ARGB8888,
3591 BUFFER_CURSOR);
3592 if (!output->gbm_cursor_fb[i]) {
3593 gbm_bo_destroy(bo);
3594 goto err;
3595 }
3596 }
3597
3598 return 0;
3599
3600err:
3601 weston_log("cursor buffers unavailable, using gl cursors\n");
3602 b->cursors_are_broken = 1;
3603 drm_output_fini_cursor_egl(output);
3604 return -1;
3605}
3606
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02003607/* Init output state that depends on gl or gbm */
3608static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003609drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02003610{
Derek Foremanc4cfe852015-05-15 12:12:40 -05003611 EGLint format[2] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01003612 output->gbm_format,
3613 fallback_format_for(output->gbm_format),
Derek Foremanc4cfe852015-05-15 12:12:40 -05003614 };
Daniel Stonee4256832017-04-04 17:54:27 +01003615 int n_formats = 1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02003616
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01003617 output->gbm_surface = gbm_surface_create(b->gbm,
Hardeningff39efa2013-09-18 23:56:35 +02003618 output->base.current_mode->width,
3619 output->base.current_mode->height,
Derek Foremanc4cfe852015-05-15 12:12:40 -05003620 format[0],
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02003621 GBM_BO_USE_SCANOUT |
3622 GBM_BO_USE_RENDERING);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01003623 if (!output->gbm_surface) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02003624 weston_log("failed to create gbm surface\n");
3625 return -1;
3626 }
3627
Derek Foremanc4cfe852015-05-15 12:12:40 -05003628 if (format[1])
3629 n_formats = 2;
Miguel A. Vicoc095cde2016-05-18 17:43:00 +02003630 if (gl_renderer->output_window_create(&output->base,
3631 (EGLNativeWindowType)output->gbm_surface,
3632 output->gbm_surface,
3633 gl_renderer->opaque_attribs,
3634 format,
3635 n_formats) < 0) {
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02003636 weston_log("failed to create gl renderer output state\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01003637 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02003638 return -1;
3639 }
3640
Daniel Stonee4256832017-04-04 17:54:27 +01003641 drm_output_init_cursor_egl(output, b);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02003642
3643 return 0;
3644}
3645
Daniel Stone3e661f72016-11-04 17:24:06 +00003646static void
3647drm_output_fini_egl(struct drm_output *output)
3648{
Daniel Stonee2e80132018-01-16 15:37:33 +00003649 struct drm_backend *b = to_drm_backend(output->base.compositor);
3650
3651 /* Destroying the GBM surface will destroy all our GBM buffers,
3652 * regardless of refcount. Ensure we destroy them here. */
3653 if (!b->shutting_down &&
3654 output->scanout_plane->state_cur->fb &&
3655 output->scanout_plane->state_cur->fb->type == BUFFER_GBM_SURFACE) {
3656 drm_plane_state_free(output->scanout_plane->state_cur, true);
3657 output->scanout_plane->state_cur =
3658 drm_plane_state_alloc(NULL, output->scanout_plane);
3659 output->scanout_plane->state_cur->complete = true;
3660 }
3661
Daniel Stone3e661f72016-11-04 17:24:06 +00003662 gl_renderer->output_destroy(&output->base);
3663 gbm_surface_destroy(output->gbm_surface);
Daniel Stonee4256832017-04-04 17:54:27 +01003664 drm_output_fini_cursor_egl(output);
Daniel Stone3e661f72016-11-04 17:24:06 +00003665}
3666
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04003667static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003668drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003669{
Hardeningff39efa2013-09-18 23:56:35 +02003670 int w = output->base.current_mode->width;
3671 int h = output->base.current_mode->height;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03003672 uint32_t format = output->gbm_format;
3673 uint32_t pixman_format;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003674 unsigned int i;
3675
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03003676 switch (format) {
3677 case GBM_FORMAT_XRGB8888:
3678 pixman_format = PIXMAN_x8r8g8b8;
3679 break;
3680 case GBM_FORMAT_RGB565:
3681 pixman_format = PIXMAN_r5g6b5;
3682 break;
3683 default:
3684 weston_log("Unsupported pixman format 0x%x\n", format);
3685 return -1;
3686 }
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003687
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03003688 /* FIXME error checking */
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003689 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03003690 output->dumb[i] = drm_fb_create_dumb(b, w, h, format);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003691 if (!output->dumb[i])
3692 goto err;
3693
3694 output->image[i] =
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03003695 pixman_image_create_bits(pixman_format, w, h,
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003696 output->dumb[i]->map,
3697 output->dumb[i]->stride);
3698 if (!output->image[i])
3699 goto err;
3700 }
3701
3702 if (pixman_renderer_output_create(&output->base) < 0)
3703 goto err;
3704
3705 pixman_region32_init_rect(&output->previous_damage,
Alexander Larsson0b135062013-05-28 16:23:36 +02003706 output->base.x, output->base.y, output->base.width, output->base.height);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003707
3708 return 0;
3709
3710err:
3711 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
3712 if (output->dumb[i])
Daniel Stone6e7a9612017-04-04 17:54:26 +01003713 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003714 if (output->image[i])
3715 pixman_image_unref(output->image[i]);
3716
3717 output->dumb[i] = NULL;
3718 output->image[i] = NULL;
3719 }
3720
3721 return -1;
3722}
3723
3724static void
3725drm_output_fini_pixman(struct drm_output *output)
3726{
Daniel Stonee2e80132018-01-16 15:37:33 +00003727 struct drm_backend *b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003728 unsigned int i;
3729
Daniel Stonee2e80132018-01-16 15:37:33 +00003730 /* Destroying the Pixman surface will destroy all our buffers,
3731 * regardless of refcount. Ensure we destroy them here. */
3732 if (!b->shutting_down &&
3733 output->scanout_plane->state_cur->fb &&
3734 output->scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB) {
3735 drm_plane_state_free(output->scanout_plane->state_cur, true);
3736 output->scanout_plane->state_cur =
3737 drm_plane_state_alloc(NULL, output->scanout_plane);
3738 output->scanout_plane->state_cur->complete = true;
3739 }
3740
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003741 pixman_renderer_output_destroy(&output->base);
3742 pixman_region32_fini(&output->previous_damage);
3743
3744 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003745 pixman_image_unref(output->image[i]);
Daniel Stone6e7a9612017-04-04 17:54:26 +01003746 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003747 output->dumb[i] = NULL;
3748 output->image[i] = NULL;
3749 }
3750}
3751
Richard Hughes2b2092a2013-04-24 14:58:02 +01003752static void
3753edid_parse_string(const uint8_t *data, char text[])
3754{
3755 int i;
3756 int replaced = 0;
3757
3758 /* this is always 12 bytes, but we can't guarantee it's null
3759 * terminated or not junk. */
3760 strncpy(text, (const char *) data, 12);
3761
Bryce Harrington9c7de162015-08-28 13:04:26 -07003762 /* guarantee our new string is null-terminated */
3763 text[12] = '\0';
3764
Richard Hughes2b2092a2013-04-24 14:58:02 +01003765 /* remove insane chars */
3766 for (i = 0; text[i] != '\0'; i++) {
3767 if (text[i] == '\n' ||
3768 text[i] == '\r') {
3769 text[i] = '\0';
3770 break;
3771 }
3772 }
3773
3774 /* ensure string is printable */
3775 for (i = 0; text[i] != '\0'; i++) {
3776 if (!isprint(text[i])) {
3777 text[i] = '-';
3778 replaced++;
3779 }
3780 }
3781
3782 /* if the string is random junk, ignore the string */
3783 if (replaced > 4)
3784 text[0] = '\0';
3785}
3786
3787#define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
3788#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
3789#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
3790#define EDID_OFFSET_DATA_BLOCKS 0x36
3791#define EDID_OFFSET_LAST_BLOCK 0x6c
3792#define EDID_OFFSET_PNPID 0x08
3793#define EDID_OFFSET_SERIAL 0x0c
3794
3795static int
3796edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
3797{
3798 int i;
3799 uint32_t serial_number;
3800
3801 /* check header */
3802 if (length < 128)
3803 return -1;
3804 if (data[0] != 0x00 || data[1] != 0xff)
3805 return -1;
3806
3807 /* decode the PNP ID from three 5 bit words packed into 2 bytes
3808 * /--08--\/--09--\
3809 * 7654321076543210
3810 * |\---/\---/\---/
3811 * R C1 C2 C3 */
3812 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
3813 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
3814 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
3815 edid->pnp_id[3] = '\0';
3816
3817 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
3818 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
3819 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
3820 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
3821 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
3822 if (serial_number > 0)
3823 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
3824
3825 /* parse EDID data */
3826 for (i = EDID_OFFSET_DATA_BLOCKS;
3827 i <= EDID_OFFSET_LAST_BLOCK;
3828 i += 18) {
3829 /* ignore pixel clock data */
3830 if (data[i] != 0)
3831 continue;
3832 if (data[i+2] != 0)
3833 continue;
3834
3835 /* any useful blocks? */
3836 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
3837 edid_parse_string(&data[i+5],
3838 edid->monitor_name);
3839 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
3840 edid_parse_string(&data[i+5],
3841 edid->serial_number);
3842 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
3843 edid_parse_string(&data[i+5],
3844 edid->eisa_id);
3845 }
3846 }
3847 return 0;
3848}
3849
Pekka Paalanen6f1866b2017-04-03 14:22:51 +03003850/** Parse monitor make, model and serial from EDID
3851 *
3852 * \param b The backend instance.
3853 * \param output The output whose \c drm_edid to fill in.
3854 * \param props The DRM connector properties to get the EDID from.
3855 * \param make[out] The monitor make (PNP ID).
3856 * \param model[out] The monitor model (name).
3857 * \param serial_number[out] The monitor serial number.
3858 *
3859 * Each of \c *make, \c *model and \c *serial_number are set only if the
3860 * information is found in the EDID. The pointers they are set to must not
3861 * be free()'d explicitly, instead they get implicitly freed when the
3862 * \c drm_output is destroyed.
3863 */
Richard Hughes2b2092a2013-04-24 14:58:02 +01003864static void
Daniel Stone02cf4662017-03-03 16:19:39 +00003865find_and_parse_output_edid(struct drm_backend *b, struct drm_output *output,
Pekka Paalanen6f1866b2017-04-03 14:22:51 +03003866 drmModeObjectPropertiesPtr props,
3867 const char **make,
3868 const char **model,
3869 const char **serial_number)
Richard Hughes2b2092a2013-04-24 14:58:02 +01003870{
3871 drmModePropertyBlobPtr edid_blob = NULL;
Daniel Stone02cf4662017-03-03 16:19:39 +00003872 uint32_t blob_id;
Richard Hughes2b2092a2013-04-24 14:58:02 +01003873 int rc;
3874
Daniel Stone02cf4662017-03-03 16:19:39 +00003875 blob_id =
3876 drm_property_get_value(&output->props_conn[WDRM_CONNECTOR_EDID],
3877 props, 0);
3878 if (!blob_id)
3879 return;
3880
3881 edid_blob = drmModeGetPropertyBlob(b->drm.fd, blob_id);
Richard Hughes2b2092a2013-04-24 14:58:02 +01003882 if (!edid_blob)
3883 return;
3884
3885 rc = edid_parse(&output->edid,
3886 edid_blob->data,
3887 edid_blob->length);
3888 if (!rc) {
3889 weston_log("EDID data '%s', '%s', '%s'\n",
3890 output->edid.pnp_id,
3891 output->edid.monitor_name,
3892 output->edid.serial_number);
3893 if (output->edid.pnp_id[0] != '\0')
Pekka Paalanen6f1866b2017-04-03 14:22:51 +03003894 *make = output->edid.pnp_id;
Richard Hughes2b2092a2013-04-24 14:58:02 +01003895 if (output->edid.monitor_name[0] != '\0')
Pekka Paalanen6f1866b2017-04-03 14:22:51 +03003896 *model = output->edid.monitor_name;
Richard Hughes2b2092a2013-04-24 14:58:02 +01003897 if (output->edid.serial_number[0] != '\0')
Pekka Paalanen6f1866b2017-04-03 14:22:51 +03003898 *serial_number = output->edid.serial_number;
Richard Hughes2b2092a2013-04-24 14:58:02 +01003899 }
3900 drmModeFreePropertyBlob(edid_blob);
3901}
3902
Kristian Høgsberga30989a2013-05-23 17:23:15 -04003903static int
3904parse_modeline(const char *s, drmModeModeInfo *mode)
3905{
3906 char hsync[16];
3907 char vsync[16];
3908 float fclock;
3909
3910 mode->type = DRM_MODE_TYPE_USERDEF;
3911 mode->hskew = 0;
3912 mode->vscan = 0;
3913 mode->vrefresh = 0;
3914 mode->flags = 0;
3915
Rob Bradford307e09e2013-07-26 16:29:40 +01003916 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
Kristian Høgsberga30989a2013-05-23 17:23:15 -04003917 &fclock,
3918 &mode->hdisplay,
3919 &mode->hsync_start,
3920 &mode->hsync_end,
3921 &mode->htotal,
3922 &mode->vdisplay,
3923 &mode->vsync_start,
3924 &mode->vsync_end,
3925 &mode->vtotal, hsync, vsync) != 11)
3926 return -1;
3927
3928 mode->clock = fclock * 1000;
3929 if (strcmp(hsync, "+hsync") == 0)
3930 mode->flags |= DRM_MODE_FLAG_PHSYNC;
3931 else if (strcmp(hsync, "-hsync") == 0)
3932 mode->flags |= DRM_MODE_FLAG_NHSYNC;
3933 else
3934 return -1;
3935
3936 if (strcmp(vsync, "+vsync") == 0)
3937 mode->flags |= DRM_MODE_FLAG_PVSYNC;
3938 else if (strcmp(vsync, "-vsync") == 0)
3939 mode->flags |= DRM_MODE_FLAG_NVSYNC;
3940 else
3941 return -1;
3942
Emmanuel Gil Peyrota62138b2016-05-02 22:40:11 +01003943 snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f",
3944 mode->hdisplay, mode->vdisplay, fclock);
3945
Kristian Høgsberga30989a2013-05-23 17:23:15 -04003946 return 0;
3947}
3948
Rob Bradford66bd9f52013-06-25 18:56:42 +01003949static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03003950setup_output_seat_constraint(struct drm_backend *b,
Rob Bradford66bd9f52013-06-25 18:56:42 +01003951 struct weston_output *output,
3952 const char *s)
3953{
3954 if (strcmp(s, "") != 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003955 struct weston_pointer *pointer;
Rob Bradford66bd9f52013-06-25 18:56:42 +01003956 struct udev_seat *seat;
3957
Giulio Camuffo954f1832014-10-11 18:27:30 +03003958 seat = udev_seat_get_named(&b->input, s);
Derek Foreman0720ea32015-07-15 13:00:35 -05003959 if (!seat)
3960 return;
Rob Bradford66bd9f52013-06-25 18:56:42 +01003961
Derek Foreman0720ea32015-07-15 13:00:35 -05003962 seat->base.output = output;
3963
Derek Foreman1281a362015-07-31 16:55:32 -05003964 pointer = weston_seat_get_pointer(&seat->base);
3965 if (pointer)
3966 weston_pointer_clamp(pointer,
3967 &pointer->x,
3968 &pointer->y);
Rob Bradford66bd9f52013-06-25 18:56:42 +01003969 }
3970}
3971
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003972static int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003973parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format)
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003974{
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003975 int ret = 0;
3976
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003977 if (s == NULL)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003978 *gbm_format = default_value;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003979 else if (strcmp(s, "xrgb8888") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003980 *gbm_format = GBM_FORMAT_XRGB8888;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003981 else if (strcmp(s, "rgb565") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003982 *gbm_format = GBM_FORMAT_RGB565;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003983 else if (strcmp(s, "xrgb2101010") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003984 *gbm_format = GBM_FORMAT_XRGB2101010;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003985 else {
3986 weston_log("fatal: unrecognized pixel format: %s\n", s);
3987 ret = -1;
3988 }
3989
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003990 return ret;
3991}
3992
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003993/**
3994 * Choose suitable mode for an output
3995 *
3996 * Find the most suitable mode to use for initial setup (or reconfiguration on
3997 * hotplug etc) for a DRM output.
3998 *
3999 * @param output DRM output to choose mode for
4000 * @param kind Strategy and preference to use when choosing mode
4001 * @param width Desired width for this output
4002 * @param height Desired height for this output
4003 * @param current_mode Mode currently being displayed on this output
4004 * @param modeline Manually-entered mode (may be NULL)
4005 * @returns A mode from the output's mode list, or NULL if none available
4006 */
4007static struct drm_mode *
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004008drm_output_choose_initial_mode(struct drm_backend *backend,
4009 struct drm_output *output,
4010 enum weston_drm_backend_output_mode mode,
Armin Krezović08368132016-09-30 14:11:05 +02004011 const char *modeline,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004012 const drmModeModeInfo *current_mode)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004013{
4014 struct drm_mode *preferred = NULL;
4015 struct drm_mode *current = NULL;
4016 struct drm_mode *configured = NULL;
4017 struct drm_mode *best = NULL;
4018 struct drm_mode *drm_mode;
Armin Krezović08368132016-09-30 14:11:05 +02004019 drmModeModeInfo drm_modeline;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004020 int32_t width = 0;
4021 int32_t height = 0;
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01004022 uint32_t refresh = 0;
4023 int n;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004024
Armin Krezović08368132016-09-30 14:11:05 +02004025 if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) {
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01004026 n = sscanf(modeline, "%dx%d@%d", &width, &height, &refresh);
4027 if (n != 2 && n != 3) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004028 width = -1;
4029
Armin Krezović08368132016-09-30 14:11:05 +02004030 if (parse_modeline(modeline, &drm_modeline) == 0) {
4031 configured = drm_output_add_mode(output, &drm_modeline);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004032 if (!configured)
4033 return NULL;
4034 } else {
4035 weston_log("Invalid modeline \"%s\" for output %s\n",
Armin Krezović08368132016-09-30 14:11:05 +02004036 modeline, output->base.name);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004037 }
4038 }
4039 }
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004040
4041 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004042 if (width == drm_mode->base.width &&
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01004043 height == drm_mode->base.height &&
4044 (refresh == 0 || refresh == drm_mode->mode_info.vrefresh))
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004045 configured = drm_mode;
4046
comic fans7a5c5622016-03-17 14:29:27 +02004047 if (memcmp(current_mode, &drm_mode->mode_info,
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004048 sizeof *current_mode) == 0)
4049 current = drm_mode;
4050
4051 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
4052 preferred = drm_mode;
4053
4054 best = drm_mode;
4055 }
4056
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004057 if (current == NULL && current_mode->clock != 0) {
4058 current = drm_output_add_mode(output, current_mode);
4059 if (!current)
4060 return NULL;
4061 }
4062
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004063 if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004064 configured = current;
4065
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004066 if (configured)
4067 return configured;
4068
4069 if (preferred)
4070 return preferred;
4071
4072 if (current)
4073 return current;
4074
4075 if (best)
4076 return best;
4077
4078 weston_log("no available modes for %s\n", output->base.name);
4079 return NULL;
4080}
4081
Pekka Paalaneneee580b2014-06-04 16:43:06 +03004082static int
4083connector_get_current_mode(drmModeConnector *connector, int drm_fd,
4084 drmModeModeInfo *mode)
4085{
4086 drmModeEncoder *encoder;
4087 drmModeCrtc *crtc;
4088
4089 /* Get the current mode on the crtc that's currently driving
4090 * this connector. */
4091 encoder = drmModeGetEncoder(drm_fd, connector->encoder_id);
4092 memset(mode, 0, sizeof *mode);
4093 if (encoder != NULL) {
4094 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
4095 drmModeFreeEncoder(encoder);
4096 if (crtc == NULL)
4097 return -1;
4098 if (crtc->mode_valid)
4099 *mode = crtc->mode;
4100 drmModeFreeCrtc(crtc);
4101 }
4102
4103 return 0;
4104}
4105
Neil Roberts77c1a5b2014-03-07 18:05:50 +00004106static int
Armin Krezović08368132016-09-30 14:11:05 +02004107drm_output_set_mode(struct weston_output *base,
4108 enum weston_drm_backend_output_mode mode,
4109 const char *modeline)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004110{
Armin Krezović08368132016-09-30 14:11:05 +02004111 struct drm_output *output = to_drm_output(base);
4112 struct drm_backend *b = to_drm_backend(base->compositor);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004113
Armin Krezović445b41b2016-10-09 23:48:16 +02004114 struct drm_mode *current;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004115 drmModeModeInfo crtc_mode;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004116
Armin Krezović08368132016-09-30 14:11:05 +02004117 if (connector_get_current_mode(output->connector, b->drm.fd, &crtc_mode) < 0)
Armin Krezović445b41b2016-10-09 23:48:16 +02004118 return -1;
David Herrmann0f0d54e2011-12-08 17:05:45 +01004119
Armin Krezović08368132016-09-30 14:11:05 +02004120 current = drm_output_choose_initial_mode(b, output, mode, modeline, &crtc_mode);
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004121 if (!current)
Armin Krezović445b41b2016-10-09 23:48:16 +02004122 return -1;
Armin Krezović08368132016-09-30 14:11:05 +02004123
Pekka Paalanen7b36b422014-06-04 14:00:53 +03004124 output->base.current_mode = &current->base;
Hardeningff39efa2013-09-18 23:56:35 +02004125 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
Wang Quanxianacb805a2012-07-30 18:09:46 -04004126
Armin Krezović08368132016-09-30 14:11:05 +02004127 /* Set native_ fields, so weston_output_mode_switch_to_native() works */
4128 output->base.native_mode = output->base.current_mode;
4129 output->base.native_scale = output->base.current_scale;
4130
Armin Krezović08368132016-09-30 14:11:05 +02004131 return 0;
Armin Krezović08368132016-09-30 14:11:05 +02004132}
4133
4134static void
4135drm_output_set_gbm_format(struct weston_output *base,
4136 const char *gbm_format)
4137{
4138 struct drm_output *output = to_drm_output(base);
4139 struct drm_backend *b = to_drm_backend(base->compositor);
4140
4141 if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1)
4142 output->gbm_format = b->gbm_format;
Daniel Stonee2e80132018-01-16 15:37:33 +00004143
4144 /* Without universal planes, we can't discover which formats are
4145 * supported by the primary plane; we just hope that the GBM format
4146 * works. */
4147 if (!b->universal_planes)
4148 output->scanout_plane->formats[0] = output->gbm_format;
Armin Krezović08368132016-09-30 14:11:05 +02004149}
4150
4151static void
4152drm_output_set_seat(struct weston_output *base,
4153 const char *seat)
4154{
4155 struct drm_output *output = to_drm_output(base);
4156 struct drm_backend *b = to_drm_backend(base->compositor);
4157
4158 setup_output_seat_constraint(b, &output->base,
4159 seat ? seat : "");
4160}
4161
4162static int
4163drm_output_enable(struct weston_output *base)
4164{
4165 struct drm_output *output = to_drm_output(base);
4166 struct drm_backend *b = to_drm_backend(base->compositor);
4167 struct weston_mode *m;
4168
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00004169 if (b->pageflip_timeout)
4170 drm_output_pageflip_timer_create(output);
4171
Giulio Camuffo954f1832014-10-11 18:27:30 +03004172 if (b->use_pixman) {
4173 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004174 weston_log("Failed to init output pixman state\n");
Daniel Stone02cf4662017-03-03 16:19:39 +00004175 goto err;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004176 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03004177 } else if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02004178 weston_log("Failed to init output gl state\n");
Daniel Stone02cf4662017-03-03 16:19:39 +00004179 goto err;
Kristian Høgsberg1d1e0a52012-10-21 13:29:26 -04004180 }
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04004181
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004182 if (output->backlight) {
Kristian Høgsberg220819f2013-05-23 20:29:40 -04004183 weston_log("Initialized backlight, device %s\n",
4184 output->backlight->path);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004185 output->base.set_backlight = drm_set_backlight;
4186 output->base.backlight_current = drm_get_backlight(output);
Kristian Høgsberg220819f2013-05-23 20:29:40 -04004187 } else {
4188 weston_log("Failed to initialize backlight\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004189 }
4190
Jonas Ådahle5a12252013-04-05 23:07:11 +02004191 output->base.start_repaint_loop = drm_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05004192 output->base.repaint = drm_output_repaint;
Jesse Barnes58ef3792012-02-23 09:45:49 -05004193 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004194 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08004195 output->base.switch_mode = drm_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01004196
Richard Hughese7299962013-05-01 21:52:12 +01004197 output->base.gamma_size = output->original_crtc->gamma_size;
4198 output->base.set_gamma = drm_output_set_gamma;
4199
Daniel Stone2ba17f42015-05-19 20:02:41 +01004200 if (output->cursor_plane)
4201 weston_compositor_stack_plane(b->compositor,
4202 &output->cursor_plane->base,
4203 NULL);
4204 else
4205 b->cursors_are_broken = 1;
4206
Daniel Stonee2e80132018-01-16 15:37:33 +00004207 weston_compositor_stack_plane(b->compositor,
4208 &output->scanout_plane->base,
Giulio Camuffo954f1832014-10-11 18:27:30 +03004209 &b->compositor->primary_plane);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004210
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04004211 weston_log("Output %s, (connector %d, crtc %d)\n",
Richard Hughesafe690c2013-05-02 10:10:04 +01004212 output->base.name, output->connector_id, output->crtc_id);
Kristian Høgsberg061c4252012-06-28 11:28:15 -04004213 wl_list_for_each(m, &output->base.mode_list, link)
U. Artie Eoffd3ed6cb2014-01-10 10:15:17 -08004214 weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n",
Kristian Høgsberg061c4252012-06-28 11:28:15 -04004215 m->width, m->height, m->refresh / 1000.0,
4216 m->flags & WL_OUTPUT_MODE_PREFERRED ?
4217 ", preferred" : "",
4218 m->flags & WL_OUTPUT_MODE_CURRENT ?
4219 ", current" : "",
Armin Krezović08368132016-09-30 14:11:05 +02004220 output->connector->count_modes == 0 ?
Kristian Høgsberg061c4252012-06-28 11:28:15 -04004221 ", built-in" : "");
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04004222
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004223 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01004224
Daniel Stone02cf4662017-03-03 16:19:39 +00004225err:
David Herrmann0f0d54e2011-12-08 17:05:45 +01004226 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004227}
4228
Jesse Barnes58ef3792012-02-23 09:45:49 -05004229static void
Armin Krezović08368132016-09-30 14:11:05 +02004230drm_output_deinit(struct weston_output *base)
4231{
4232 struct drm_output *output = to_drm_output(base);
4233 struct drm_backend *b = to_drm_backend(base->compositor);
Daniel Stone087ddf02017-02-14 17:51:30 +00004234 uint32_t *unused;
Armin Krezović08368132016-09-30 14:11:05 +02004235
Daniel Stone3e661f72016-11-04 17:24:06 +00004236 if (b->use_pixman)
Armin Krezović08368132016-09-30 14:11:05 +02004237 drm_output_fini_pixman(output);
Daniel Stone3e661f72016-11-04 17:24:06 +00004238 else
4239 drm_output_fini_egl(output);
Armin Krezović08368132016-09-30 14:11:05 +02004240
Daniel Stone2ba17f42015-05-19 20:02:41 +01004241 /* Since our planes are no longer in use anywhere, remove their base
4242 * weston_plane's link from the plane stacking list, unless we're
4243 * shutting down, in which case the plane has already been
4244 * destroyed. */
Daniel Stonee2e80132018-01-16 15:37:33 +00004245 if (!b->shutting_down) {
4246 wl_list_remove(&output->scanout_plane->base.link);
4247 wl_list_init(&output->scanout_plane->base.link);
4248
4249 if (output->cursor_plane) {
4250 wl_list_remove(&output->cursor_plane->base.link);
4251 wl_list_init(&output->cursor_plane->base.link);
4252 /* Turn off hardware cursor */
4253 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
4254 }
Daniel Stone2ba17f42015-05-19 20:02:41 +01004255 }
Daniel Stone087ddf02017-02-14 17:51:30 +00004256
4257 unused = wl_array_add(&b->unused_connectors, sizeof(*unused));
4258 *unused = output->connector_id;
4259 unused = wl_array_add(&b->unused_crtcs, sizeof(*unused));
4260 *unused = output->crtc_id;
Armin Krezović08368132016-09-30 14:11:05 +02004261}
4262
4263static void
4264drm_output_destroy(struct weston_output *base)
4265{
4266 struct drm_output *output = to_drm_output(base);
4267 struct drm_backend *b = to_drm_backend(base->compositor);
Armin Krezović445b41b2016-10-09 23:48:16 +02004268 struct drm_mode *drm_mode, *next;
Armin Krezović08368132016-09-30 14:11:05 +02004269 drmModeCrtcPtr origcrtc = output->original_crtc;
4270
Daniel Stone7b2ddac2016-11-11 19:11:49 +00004271 if (output->page_flip_pending || output->vblank_pending) {
Armin Krezović08368132016-09-30 14:11:05 +02004272 output->destroy_pending = 1;
4273 weston_log("destroy output while page flip pending\n");
4274 return;
4275 }
4276
4277 if (output->base.enabled)
4278 drm_output_deinit(&output->base);
4279
Daniel Stone2ba17f42015-05-19 20:02:41 +01004280 if (!b->universal_planes && !b->shutting_down) {
4281 /* With universal planes, the 'special' planes are allocated at
4282 * startup, freed at shutdown, and live on the plane list in
4283 * between. We want the planes to continue to exist and be freed
4284 * up for other outputs.
4285 *
4286 * Without universal planes, our special planes are
4287 * pseudo-planes allocated at output creation, freed at output
4288 * destruction, and not usable by other outputs.
4289 *
4290 * On the other hand, if the compositor is already shutting down,
4291 * the plane has already been destroyed.
4292 */
4293 if (output->cursor_plane)
4294 drm_plane_destroy(output->cursor_plane);
Daniel Stonee2e80132018-01-16 15:37:33 +00004295 drm_plane_destroy(output->scanout_plane);
Daniel Stone2ba17f42015-05-19 20:02:41 +01004296 }
4297
Armin Krezović445b41b2016-10-09 23:48:16 +02004298 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
4299 base.link) {
4300 wl_list_remove(&drm_mode->base.link);
4301 free(drm_mode);
4302 }
4303
Armin Krezović08368132016-09-30 14:11:05 +02004304 if (origcrtc) {
4305 /* Restore original CRTC state */
4306 drmModeSetCrtc(b->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
4307 origcrtc->x, origcrtc->y,
4308 &output->connector_id, 1, &origcrtc->mode);
4309 drmModeFreeCrtc(origcrtc);
4310 }
4311
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00004312 if (output->pageflip_timer)
4313 wl_event_source_remove(output->pageflip_timer);
4314
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03004315 weston_output_release(&output->base);
Armin Krezović08368132016-09-30 14:11:05 +02004316
Daniel Stone02cf4662017-03-03 16:19:39 +00004317 drm_property_info_free(output->props_conn, WDRM_CONNECTOR__COUNT);
4318
Armin Krezović08368132016-09-30 14:11:05 +02004319 drmModeFreeConnector(output->connector);
4320
4321 if (output->backlight)
4322 backlight_destroy(output->backlight);
4323
Daniel Stone7b2ddac2016-11-11 19:11:49 +00004324 assert(!output->state_last);
4325 drm_output_state_free(output->state_cur);
4326
Armin Krezović08368132016-09-30 14:11:05 +02004327 free(output);
4328}
4329
4330static int
4331drm_output_disable(struct weston_output *base)
4332{
4333 struct drm_output *output = to_drm_output(base);
4334 struct drm_backend *b = to_drm_backend(base->compositor);
Daniel Stonea08512f2016-11-08 17:46:10 +00004335 struct drm_pending_state *pending_state;
4336 int ret;
Armin Krezović08368132016-09-30 14:11:05 +02004337
Daniel Stone7b2ddac2016-11-11 19:11:49 +00004338 if (output->page_flip_pending || output->vblank_pending) {
Armin Krezović08368132016-09-30 14:11:05 +02004339 output->disable_pending = 1;
4340 return -1;
4341 }
4342
Daniel Stonea08512f2016-11-08 17:46:10 +00004343 weston_log("Disabling output %s\n", output->base.name);
4344 pending_state = drm_pending_state_alloc(b);
4345 drm_output_get_disable_state(pending_state, output);
4346 ret = drm_pending_state_apply_sync(pending_state);
4347 if (ret)
4348 weston_log("Couldn't disable output %s\n", output->base.name);
4349
Armin Krezović08368132016-09-30 14:11:05 +02004350 if (output->base.enabled)
4351 drm_output_deinit(&output->base);
4352
4353 output->disable_pending = 0;
4354
Armin Krezović08368132016-09-30 14:11:05 +02004355 return 0;
4356}
4357
4358/**
Daniel Stone087ddf02017-02-14 17:51:30 +00004359 * Update the list of unused connectors and CRTCs
4360 *
4361 * This keeps the unused_connectors and unused_crtcs arrays up to date.
4362 *
4363 * @param b Weston backend structure
4364 * @param resources DRM resources for this device
4365 */
4366static void
4367drm_backend_update_unused_outputs(struct drm_backend *b, drmModeRes *resources)
4368{
4369 int i;
4370
4371 wl_array_release(&b->unused_connectors);
4372 wl_array_init(&b->unused_connectors);
4373
4374 for (i = 0; i < resources->count_connectors; i++) {
4375 struct drm_output *output;
4376 uint32_t *connector_id;
4377
4378 output = drm_output_find_by_connector(b, resources->connectors[i]);
4379 if (output && output->base.enabled)
4380 continue;
4381
4382 connector_id = wl_array_add(&b->unused_connectors,
4383 sizeof(*connector_id));
4384 *connector_id = resources->connectors[i];
4385 }
4386
4387 wl_array_release(&b->unused_crtcs);
4388 wl_array_init(&b->unused_crtcs);
4389
4390 for (i = 0; i < resources->count_crtcs; i++) {
4391 struct drm_output *output;
4392 uint32_t *crtc_id;
4393
4394 output = drm_output_find_by_crtc(b, resources->crtcs[i]);
4395 if (output && output->base.enabled)
4396 continue;
4397
4398 crtc_id = wl_array_add(&b->unused_crtcs, sizeof(*crtc_id));
4399 *crtc_id = resources->crtcs[i];
4400 }
4401}
4402
4403/**
Armin Krezović08368132016-09-30 14:11:05 +02004404 * Create a Weston output structure
4405 *
4406 * Given a DRM connector, create a matching drm_output structure and add it
4407 * to Weston's output list. It also takes ownership of the connector, which
4408 * is released when output is destroyed.
4409 *
4410 * @param b Weston backend structure
4411 * @param resources DRM resources for this device
4412 * @param connector DRM connector to use for this new output
4413 * @param drm_device udev device pointer
4414 * @returns 0 on success, or -1 on failure
4415 */
4416static int
4417create_output_for_connector(struct drm_backend *b,
4418 drmModeRes *resources,
4419 drmModeConnector *connector,
4420 struct udev_device *drm_device)
4421{
4422 struct drm_output *output;
Daniel Stone02cf4662017-03-03 16:19:39 +00004423 drmModeObjectPropertiesPtr props;
Armin Krezović445b41b2016-10-09 23:48:16 +02004424 struct drm_mode *drm_mode;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03004425 char *name;
Pekka Paalanen6f1866b2017-04-03 14:22:51 +03004426 const char *make = "unknown";
4427 const char *model = "unknown";
4428 const char *serial_number = "unknown";
Armin Krezović08368132016-09-30 14:11:05 +02004429 int i;
4430
Daniel Stone02cf4662017-03-03 16:19:39 +00004431 static const struct drm_property_info connector_props[] = {
4432 [WDRM_CONNECTOR_EDID] = { .name = "EDID" },
4433 [WDRM_CONNECTOR_DPMS] = { .name = "DPMS" },
4434 };
4435
Armin Krezović08368132016-09-30 14:11:05 +02004436 i = find_crtc_for_connector(b, resources, connector);
4437 if (i < 0) {
4438 weston_log("No usable crtc/encoder pair for connector.\n");
Armin Krezović445b41b2016-10-09 23:48:16 +02004439 goto err;
Armin Krezović08368132016-09-30 14:11:05 +02004440 }
4441
4442 output = zalloc(sizeof *output);
4443 if (output == NULL)
Armin Krezović445b41b2016-10-09 23:48:16 +02004444 goto err;
Armin Krezović08368132016-09-30 14:11:05 +02004445
4446 output->connector = connector;
4447 output->crtc_id = resources->crtcs[i];
4448 output->pipe = i;
4449 output->connector_id = connector->connector_id;
4450
4451 output->backlight = backlight_init(drm_device,
4452 connector->connector_type);
4453
Armin Krezović445b41b2016-10-09 23:48:16 +02004454 output->original_crtc = drmModeGetCrtc(b->drm.fd, output->crtc_id);
4455
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03004456 name = make_connector_name(connector);
4457 weston_output_init(&output->base, b->compositor, name);
4458 free(name);
4459
Armin Krezović08368132016-09-30 14:11:05 +02004460 output->base.enable = drm_output_enable;
4461 output->base.destroy = drm_output_destroy;
4462 output->base.disable = drm_output_disable;
Armin Krezović08368132016-09-30 14:11:05 +02004463
4464 output->destroy_pending = 0;
4465 output->disable_pending = 0;
Armin Krezović08368132016-09-30 14:11:05 +02004466
Daniel Stone02cf4662017-03-03 16:19:39 +00004467 props = drmModeObjectGetProperties(b->drm.fd, connector->connector_id,
4468 DRM_MODE_OBJECT_CONNECTOR);
4469 if (!props) {
4470 weston_log("failed to get connector properties\n");
4471 goto err;
4472 }
4473 drm_property_info_populate(b, connector_props, output->props_conn,
4474 WDRM_CONNECTOR__COUNT, props);
Pekka Paalanen6f1866b2017-04-03 14:22:51 +03004475 find_and_parse_output_edid(b, output, props,
4476 &make, &model, &serial_number);
4477 output->base.make = (char *)make;
4478 output->base.model = (char *)model;
4479 output->base.serial_number = (char *)serial_number;
Pekka Paalanena0bfedc2017-04-03 14:42:51 +03004480 output->base.subpixel = drm_subpixel_to_wayland(output->connector->subpixel);
4481
4482 if (output->connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
4483 output->connector->connector_type == DRM_MODE_CONNECTOR_eDP)
4484 output->base.connection_internal = true;
4485
Daniel Stone7b2ddac2016-11-11 19:11:49 +00004486 output->state_cur = drm_output_state_alloc(output, NULL);
4487
Pekka Paalanena0bfedc2017-04-03 14:42:51 +03004488 output->base.mm_width = output->connector->mmWidth;
4489 output->base.mm_height = output->connector->mmHeight;
4490
Daniel Stone02cf4662017-03-03 16:19:39 +00004491 drmModeFreeObjectProperties(props);
4492
Armin Krezović445b41b2016-10-09 23:48:16 +02004493 for (i = 0; i < output->connector->count_modes; i++) {
4494 drm_mode = drm_output_add_mode(output, &output->connector->modes[i]);
4495 if (!drm_mode) {
4496 drm_output_destroy(&output->base);
4497 return -1;
4498 }
4499 }
4500
Daniel Stonee2e80132018-01-16 15:37:33 +00004501 output->scanout_plane =
4502 drm_output_find_special_plane(b, output,
4503 WDRM_PLANE_TYPE_PRIMARY);
4504 if (!output->scanout_plane) {
4505 weston_log("Failed to find primary plane for output %s\n",
4506 output->base.name);
4507 drm_output_destroy(&output->base);
4508 return -1;
4509 }
4510
Daniel Stone2ba17f42015-05-19 20:02:41 +01004511 /* Failing to find a cursor plane is not fatal, as we'll fall back
4512 * to software cursor. */
4513 output->cursor_plane =
4514 drm_output_find_special_plane(b, output,
4515 WDRM_PLANE_TYPE_CURSOR);
4516
Armin Krezović08368132016-09-30 14:11:05 +02004517 weston_compositor_add_pending_output(&output->base, b->compositor);
4518
4519 return 0;
Armin Krezović445b41b2016-10-09 23:48:16 +02004520
4521err:
4522 drmModeFreeConnector(connector);
4523
4524 return -1;
Armin Krezović08368132016-09-30 14:11:05 +02004525}
4526
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004527static int
Ucan, Emre (ADITG/SW1)9a200d72017-02-02 14:06:56 +00004528create_outputs(struct drm_backend *b, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004529{
4530 drmModeConnector *connector;
4531 drmModeRes *resources;
4532 int i;
4533
Giulio Camuffo954f1832014-10-11 18:27:30 +03004534 resources = drmModeGetResources(b->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004535 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02004536 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004537 return -1;
4538 }
4539
Giulio Camuffo954f1832014-10-11 18:27:30 +03004540 b->min_width = resources->min_width;
4541 b->max_width = resources->max_width;
4542 b->min_height = resources->min_height;
4543 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05004544
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004545 for (i = 0; i < resources->count_connectors; i++) {
Daniel Stone02cf4662017-03-03 16:19:39 +00004546 int ret;
4547
Giulio Camuffo954f1832014-10-11 18:27:30 +03004548 connector = drmModeGetConnector(b->drm.fd,
Benjamin Franzke117483d2011-08-30 11:38:26 +02004549 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004550 if (connector == NULL)
4551 continue;
4552
Ucan, Emre (ADITG/SW1)38ea2d22017-03-08 15:43:18 +00004553 if (connector->connection == DRM_MODE_CONNECTED) {
Daniel Stone02cf4662017-03-03 16:19:39 +00004554 ret = create_output_for_connector(b, resources,
4555 connector, drm_device);
4556 if (ret < 0)
4557 weston_log("failed to create new connector\n");
Armin Krezović08368132016-09-30 14:11:05 +02004558 } else {
4559 drmModeFreeConnector(connector);
Benjamin Franzke9eaee352011-08-02 13:03:54 +02004560 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004561 }
4562
Daniel Stone087ddf02017-02-14 17:51:30 +00004563 drm_backend_update_unused_outputs(b, resources);
4564
Armin Krezović08368132016-09-30 14:11:05 +02004565 if (wl_list_empty(&b->compositor->output_list) &&
4566 wl_list_empty(&b->compositor->pending_output_list))
Martin Minarik6d118362012-06-07 18:01:59 +02004567 weston_log("No currently active connector found.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004568
4569 drmModeFreeResources(resources);
4570
4571 return 0;
4572}
4573
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004574static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03004575update_outputs(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004576{
4577 drmModeConnector *connector;
4578 drmModeRes *resources;
4579 struct drm_output *output, *next;
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00004580 uint32_t *connected;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004581 int i;
4582
Giulio Camuffo954f1832014-10-11 18:27:30 +03004583 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004584 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02004585 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004586 return;
4587 }
4588
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00004589 connected = calloc(resources->count_connectors, sizeof(uint32_t));
4590 if (!connected) {
4591 drmModeFreeResources(resources);
4592 return;
4593 }
4594
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004595 /* collect new connects */
4596 for (i = 0; i < resources->count_connectors; i++) {
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +00004597 uint32_t connector_id = resources->connectors[i];
Benjamin Franzke117483d2011-08-30 11:38:26 +02004598
Giulio Camuffo954f1832014-10-11 18:27:30 +03004599 connector = drmModeGetConnector(b->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01004600 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004601 continue;
4602
David Herrmann7551cff2011-12-08 17:05:43 +01004603 if (connector->connection != DRM_MODE_CONNECTED) {
4604 drmModeFreeConnector(connector);
4605 continue;
4606 }
4607
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00004608 connected[i] = connector_id;
Benjamin Franzke117483d2011-08-30 11:38:26 +02004609
Daniel Stonec0ec7592017-02-09 13:58:35 +00004610 if (drm_output_find_by_connector(b, connector_id)) {
Armin Krezović08368132016-09-30 14:11:05 +02004611 drmModeFreeConnector(connector);
Daniel Stonec0ec7592017-02-09 13:58:35 +00004612 continue;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004613 }
Daniel Stonec0ec7592017-02-09 13:58:35 +00004614
4615 create_output_for_connector(b, resources,
4616 connector, drm_device);
4617 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004618 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004619
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00004620 wl_list_for_each_safe(output, next, &b->compositor->output_list,
4621 base.link) {
4622 bool disconnected = true;
4623
4624 for (i = 0; i < resources->count_connectors; i++) {
4625 if (connected[i] == output->connector_id) {
4626 disconnected = false;
4627 break;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004628 }
4629 }
Armin Krezović08368132016-09-30 14:11:05 +02004630
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00004631 if (!disconnected)
4632 continue;
4633
4634 weston_log("connector %d disconnected\n", output->connector_id);
4635 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004636 }
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00004637
4638 wl_list_for_each_safe(output, next, &b->compositor->pending_output_list,
4639 base.link) {
4640 bool disconnected = true;
4641
4642 for (i = 0; i < resources->count_connectors; i++) {
4643 if (connected[i] == output->connector_id) {
4644 disconnected = false;
4645 break;
4646 }
4647 }
4648
4649 if (!disconnected)
4650 continue;
4651
4652 weston_log("connector %d disconnected\n", output->connector_id);
4653 drm_output_destroy(&output->base);
4654 }
4655
Daniel Stone087ddf02017-02-14 17:51:30 +00004656 drm_backend_update_unused_outputs(b, resources);
4657
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00004658 free(connected);
4659 drmModeFreeResources(resources);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004660}
4661
4662static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03004663udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004664{
David Herrmannd7488c22012-03-11 20:05:21 +01004665 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01004666 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01004667
4668 sysnum = udev_device_get_sysnum(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03004669 if (!sysnum || atoi(sysnum) != b->drm.id)
David Herrmannd7488c22012-03-11 20:05:21 +01004670 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02004671
David Herrmann6ac52db2012-03-11 20:05:22 +01004672 val = udev_device_get_property_value(device, "HOTPLUG");
4673 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004674 return 0;
4675
David Herrmann6ac52db2012-03-11 20:05:22 +01004676 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004677}
4678
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004679static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004680udev_drm_event(int fd, uint32_t mask, void *data)
4681{
Giulio Camuffo954f1832014-10-11 18:27:30 +03004682 struct drm_backend *b = data;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004683 struct udev_device *event;
4684
Giulio Camuffo954f1832014-10-11 18:27:30 +03004685 event = udev_monitor_receive_device(b->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02004686
Giulio Camuffo954f1832014-10-11 18:27:30 +03004687 if (udev_event_is_hotplug(b, event))
4688 update_outputs(b, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004689
4690 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004691
4692 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004693}
4694
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004695static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004696drm_restore(struct weston_compositor *ec)
4697{
Kristian Høgsberg3f495872013-09-18 23:00:17 -07004698 weston_launcher_restore(ec->launcher);
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004699}
4700
4701static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004702drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004703{
Armin Krezović545dba62016-08-05 15:54:18 +02004704 struct drm_backend *b = to_drm_backend(ec);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004705
Giulio Camuffo954f1832014-10-11 18:27:30 +03004706 udev_input_destroy(&b->input);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004707
Giulio Camuffo954f1832014-10-11 18:27:30 +03004708 wl_event_source_remove(b->udev_drm_source);
4709 wl_event_source_remove(b->drm_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004710
Daniel Stoneb57c6a02017-10-05 16:27:21 +01004711 b->shutting_down = true;
4712
Giulio Camuffo954f1832014-10-11 18:27:30 +03004713 destroy_sprites(b);
Kristian Høgsberg3d64a3e2013-05-10 12:36:04 -04004714
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03004715 weston_compositor_shutdown(ec);
4716
Giulio Camuffo954f1832014-10-11 18:27:30 +03004717 if (b->gbm)
4718 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004719
Pekka Paalanen2a0c6c32017-09-13 16:48:01 +03004720 udev_unref(b->udev);
4721
Giulio Camuffo954f1832014-10-11 18:27:30 +03004722 weston_launcher_destroy(ec->launcher);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004723
Daniel Stone087ddf02017-02-14 17:51:30 +00004724 wl_array_release(&b->unused_crtcs);
4725 wl_array_release(&b->unused_connectors);
4726
Giulio Camuffo954f1832014-10-11 18:27:30 +03004727 close(b->drm.fd);
Giulio Camuffo954f1832014-10-11 18:27:30 +03004728 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004729}
4730
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04004731static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004732session_notify(struct wl_listener *listener, void *data)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04004733{
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004734 struct weston_compositor *compositor = data;
Armin Krezović545dba62016-08-05 15:54:18 +02004735 struct drm_backend *b = to_drm_backend(compositor);
Daniel Stone085d2b92015-05-21 00:00:57 +01004736 struct drm_plane *plane;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04004737 struct drm_output *output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04004738
Giulio Camuffo954f1832014-10-11 18:27:30 +03004739 if (compositor->session_active) {
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004740 weston_log("activating session\n");
Daniel Stonef33e1042016-11-05 08:10:13 +00004741 weston_compositor_wake(compositor);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004742 weston_compositor_damage_all(compositor);
Daniel Stone6020f472018-02-05 15:46:20 +00004743 b->state_invalid = true;
Giulio Camuffo954f1832014-10-11 18:27:30 +03004744 udev_input_enable(&b->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004745 } else {
4746 weston_log("deactivating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03004747 udev_input_disable(&b->input);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04004748
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004749 weston_compositor_offscreen(compositor);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04004750
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05004751 /* If we have a repaint scheduled (either from a
4752 * pending pageflip or the idle handler), make sure we
4753 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004754 * vt switched away. The OFFSCREEN state will prevent
Abdur Rehman4dca0e12017-01-01 19:46:35 +05004755 * further attempts at repainting. When we switch
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05004756 * back, we schedule a repaint, which will process
4757 * pending frame callbacks. */
4758
Giulio Camuffo954f1832014-10-11 18:27:30 +03004759 wl_list_for_each(output, &compositor->output_list, base.link) {
Daniel Stone09a97e22017-03-01 11:34:06 +00004760 output->base.repaint_needed = false;
Daniel Stone2ba17f42015-05-19 20:02:41 +01004761 if (output->cursor_plane)
4762 drmModeSetCursor(b->drm.fd, output->crtc_id,
4763 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05004764 }
4765
Giulio Camuffo954f1832014-10-11 18:27:30 +03004766 output = container_of(compositor->output_list.next,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04004767 struct drm_output, base.link);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05004768
Daniel Stone085d2b92015-05-21 00:00:57 +01004769 wl_list_for_each(plane, &b->plane_list, link) {
4770 if (plane->type != WDRM_PLANE_TYPE_OVERLAY)
4771 continue;
4772
Giulio Camuffo954f1832014-10-11 18:27:30 +03004773 drmModeSetPlane(b->drm.fd,
Daniel Stone085d2b92015-05-21 00:00:57 +01004774 plane->plane_id,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04004775 output->crtc_id, 0, 0,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05004776 0, 0, 0, 0, 0, 0, 0, 0);
Daniel Stone085d2b92015-05-21 00:00:57 +01004777 }
4778 }
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04004779}
4780
Daniel Stoneefa504f2016-12-19 16:48:20 +00004781/**
4782 * Determines whether or not a device is capable of modesetting. If successful,
4783 * sets b->drm.fd and b->drm.filename to the opened device.
4784 */
4785static bool
4786drm_device_is_kms(struct drm_backend *b, struct udev_device *device)
4787{
4788 const char *filename = udev_device_get_devnode(device);
4789 const char *sysnum = udev_device_get_sysnum(device);
4790 drmModeRes *res;
4791 int id, fd;
4792
4793 if (!filename)
4794 return false;
4795
4796 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
4797 if (fd < 0)
4798 return false;
4799
4800 res = drmModeGetResources(fd);
4801 if (!res)
4802 goto out_fd;
4803
4804 if (res->count_crtcs <= 0 || res->count_connectors <= 0 ||
4805 res->count_encoders <= 0)
4806 goto out_res;
4807
4808 if (sysnum)
4809 id = atoi(sysnum);
4810 if (!sysnum || id < 0) {
4811 weston_log("couldn't get sysnum for device %s\n", filename);
4812 goto out_res;
4813 }
4814
4815 /* We can be called successfully on multiple devices; if we have,
4816 * clean up old entries. */
4817 if (b->drm.fd >= 0)
4818 weston_launcher_close(b->compositor->launcher, b->drm.fd);
4819 free(b->drm.filename);
4820
4821 b->drm.fd = fd;
4822 b->drm.id = id;
4823 b->drm.filename = strdup(filename);
4824
Sergi Granellceb59812017-03-28 12:44:04 +02004825 drmModeFreeResources(res);
4826
Daniel Stoneefa504f2016-12-19 16:48:20 +00004827 return true;
4828
4829out_res:
4830 drmModeFreeResources(res);
4831out_fd:
4832 weston_launcher_close(b->compositor->launcher, fd);
4833 return false;
4834}
4835
David Herrmann0af066f2012-10-29 19:21:16 +01004836/*
4837 * Find primary GPU
4838 * Some systems may have multiple DRM devices attached to a single seat. This
4839 * function loops over all devices and tries to find a PCI device with the
4840 * boot_vga sysfs attribute set to 1.
4841 * If no such device is found, the first DRM device reported by udev is used.
Daniel Stoneefa504f2016-12-19 16:48:20 +00004842 * Devices are also vetted to make sure they are are capable of modesetting,
4843 * rather than pure render nodes (GPU with no display), or pure
4844 * memory-allocation devices (VGEM).
David Herrmann0af066f2012-10-29 19:21:16 +01004845 */
4846static struct udev_device*
Giulio Camuffo954f1832014-10-11 18:27:30 +03004847find_primary_gpu(struct drm_backend *b, const char *seat)
David Herrmann0af066f2012-10-29 19:21:16 +01004848{
4849 struct udev_enumerate *e;
4850 struct udev_list_entry *entry;
4851 const char *path, *device_seat, *id;
4852 struct udev_device *device, *drm_device, *pci;
4853
Giulio Camuffo954f1832014-10-11 18:27:30 +03004854 e = udev_enumerate_new(b->udev);
David Herrmann0af066f2012-10-29 19:21:16 +01004855 udev_enumerate_add_match_subsystem(e, "drm");
4856 udev_enumerate_add_match_sysname(e, "card[0-9]*");
4857
4858 udev_enumerate_scan_devices(e);
4859 drm_device = NULL;
4860 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Daniel Stoneefa504f2016-12-19 16:48:20 +00004861 bool is_boot_vga = false;
4862
David Herrmann0af066f2012-10-29 19:21:16 +01004863 path = udev_list_entry_get_name(entry);
Giulio Camuffo954f1832014-10-11 18:27:30 +03004864 device = udev_device_new_from_syspath(b->udev, path);
David Herrmann0af066f2012-10-29 19:21:16 +01004865 if (!device)
4866 continue;
4867 device_seat = udev_device_get_property_value(device, "ID_SEAT");
4868 if (!device_seat)
4869 device_seat = default_seat;
4870 if (strcmp(device_seat, seat)) {
4871 udev_device_unref(device);
4872 continue;
4873 }
4874
4875 pci = udev_device_get_parent_with_subsystem_devtype(device,
4876 "pci", NULL);
4877 if (pci) {
4878 id = udev_device_get_sysattr_value(pci, "boot_vga");
Daniel Stoneefa504f2016-12-19 16:48:20 +00004879 if (id && !strcmp(id, "1"))
4880 is_boot_vga = true;
David Herrmann0af066f2012-10-29 19:21:16 +01004881 }
4882
Daniel Stoneefa504f2016-12-19 16:48:20 +00004883 /* If we already have a modesetting-capable device, and this
4884 * device isn't our boot-VGA device, we aren't going to use
4885 * it. */
4886 if (!is_boot_vga && drm_device) {
David Herrmann0af066f2012-10-29 19:21:16 +01004887 udev_device_unref(device);
Daniel Stoneefa504f2016-12-19 16:48:20 +00004888 continue;
4889 }
4890
4891 /* Make sure this device is actually capable of modesetting;
4892 * if this call succeeds, b->drm.{fd,filename} will be set,
4893 * and any old values freed. */
4894 if (!drm_device_is_kms(b, device)) {
4895 udev_device_unref(device);
4896 continue;
4897 }
4898
4899 /* There can only be one boot_vga device, and we try to use it
4900 * at all costs. */
4901 if (is_boot_vga) {
4902 if (drm_device)
4903 udev_device_unref(drm_device);
4904 drm_device = device;
4905 break;
4906 }
4907
4908 /* Per the (!is_boot_vga && drm_device) test above, we only
4909 * trump existing saved devices with boot-VGA devices, so if
4910 * we end up here, this must be the first device we've seen. */
4911 assert(!drm_device);
4912 drm_device = device;
David Herrmann0af066f2012-10-29 19:21:16 +01004913 }
4914
Daniel Stoneefa504f2016-12-19 16:48:20 +00004915 /* If we're returning a device to use, we must have an open FD for
4916 * it. */
4917 assert(!!drm_device == (b->drm.fd >= 0));
4918
David Herrmann0af066f2012-10-29 19:21:16 +01004919 udev_enumerate_unref(e);
4920 return drm_device;
4921}
4922
Pekka Paalanenb45ed8b2017-03-28 18:04:27 +03004923static struct udev_device *
4924open_specific_drm_device(struct drm_backend *b, const char *name)
4925{
4926 struct udev_device *device;
4927
4928 device = udev_device_new_from_subsystem_sysname(b->udev, "drm", name);
4929 if (!device) {
4930 weston_log("ERROR: could not open DRM device '%s'\n", name);
4931 return NULL;
4932 }
4933
4934 if (!drm_device_is_kms(b, device)) {
4935 udev_device_unref(device);
4936 weston_log("ERROR: DRM device '%s' is not a KMS device.\n", name);
4937 return NULL;
4938 }
4939
4940 /* If we're returning a device to use, we must have an open FD for
4941 * it. */
4942 assert(b->drm.fd >= 0);
4943
4944 return device;
4945}
4946
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02004947static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004948planes_binding(struct weston_keyboard *keyboard, const struct timespec *time,
4949 uint32_t key, void *data)
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02004950{
Giulio Camuffo954f1832014-10-11 18:27:30 +03004951 struct drm_backend *b = data;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02004952
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02004953 switch (key) {
4954 case KEY_C:
Giulio Camuffo954f1832014-10-11 18:27:30 +03004955 b->cursors_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02004956 break;
4957 case KEY_V:
Giulio Camuffo954f1832014-10-11 18:27:30 +03004958 b->sprites_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02004959 break;
4960 case KEY_O:
Giulio Camuffo954f1832014-10-11 18:27:30 +03004961 b->sprites_hidden ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02004962 break;
4963 default:
4964 break;
4965 }
Ander Conselvan de Oliveira180f42a2012-11-21 15:11:37 +02004966}
4967
Kristian Høgsberg0eac34a2013-08-30 14:28:22 -07004968#ifdef BUILD_VAAPI_RECORDER
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03004969static void
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03004970recorder_destroy(struct drm_output *output)
4971{
4972 vaapi_recorder_destroy(output->recorder);
4973 output->recorder = NULL;
4974
4975 output->base.disable_planes--;
4976
4977 wl_list_remove(&output->recorder_frame_listener.link);
4978 weston_log("[libva recorder] done\n");
4979}
4980
4981static void
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03004982recorder_frame_notify(struct wl_listener *listener, void *data)
4983{
4984 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03004985 struct drm_backend *b;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03004986 int fd, ret;
4987
4988 output = container_of(listener, struct drm_output,
4989 recorder_frame_listener);
Armin Krezović545dba62016-08-05 15:54:18 +02004990 b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03004991
4992 if (!output->recorder)
4993 return;
4994
Daniel Stonee2e80132018-01-16 15:37:33 +00004995 ret = drmPrimeHandleToFD(b->drm.fd,
4996 output->scanout_plane->state_cur->fb->handle,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03004997 DRM_CLOEXEC, &fd);
4998 if (ret) {
4999 weston_log("[libva recorder] "
5000 "failed to create prime fd for front buffer\n");
5001 return;
5002 }
5003
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03005004 ret = vaapi_recorder_frame(output->recorder, fd,
Daniel Stonee2e80132018-01-16 15:37:33 +00005005 output->scanout_plane->state_cur->fb->stride);
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03005006 if (ret < 0) {
5007 weston_log("[libva recorder] aborted: %m\n");
5008 recorder_destroy(output);
5009 }
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005010}
5011
5012static void *
Giulio Camuffo954f1832014-10-11 18:27:30 +03005013create_recorder(struct drm_backend *b, int width, int height,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005014 const char *filename)
5015{
5016 int fd;
5017 drm_magic_t magic;
5018
Giulio Camuffo954f1832014-10-11 18:27:30 +03005019 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005020 if (fd < 0)
5021 return NULL;
5022
5023 drmGetMagic(fd, &magic);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005024 drmAuthMagic(b->drm.fd, magic);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005025
5026 return vaapi_recorder_create(fd, width, height, filename);
5027}
5028
5029static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02005030recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time,
5031 uint32_t key, void *data)
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005032{
Giulio Camuffo954f1832014-10-11 18:27:30 +03005033 struct drm_backend *b = data;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005034 struct drm_output *output;
5035 int width, height;
5036
Giulio Camuffo954f1832014-10-11 18:27:30 +03005037 output = container_of(b->compositor->output_list.next,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005038 struct drm_output, base.link);
5039
5040 if (!output->recorder) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01005041 if (output->gbm_format != GBM_FORMAT_XRGB8888) {
Ander Conselvan de Oliveira2ef1cd12014-05-06 16:49:06 +03005042 weston_log("failed to start vaapi recorder: "
5043 "output format not supported\n");
5044 return;
5045 }
5046
Hardeningff39efa2013-09-18 23:56:35 +02005047 width = output->base.current_mode->width;
5048 height = output->base.current_mode->height;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005049
5050 output->recorder =
Giulio Camuffo954f1832014-10-11 18:27:30 +03005051 create_recorder(b, width, height, "capture.h264");
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005052 if (!output->recorder) {
5053 weston_log("failed to create vaapi recorder\n");
5054 return;
5055 }
5056
5057 output->base.disable_planes++;
5058
5059 output->recorder_frame_listener.notify = recorder_frame_notify;
5060 wl_signal_add(&output->base.frame_signal,
5061 &output->recorder_frame_listener);
5062
5063 weston_output_schedule_repaint(&output->base);
5064
5065 weston_log("[libva recorder] initialized\n");
5066 } else {
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03005067 recorder_destroy(output);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005068 }
5069}
5070#else
5071static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02005072recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time,
5073 uint32_t key, void *data)
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03005074{
5075 weston_log("Compiled without libva support\n");
5076}
5077#endif
5078
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005079static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03005080switch_to_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005081{
5082 struct drm_output *output;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03005083 bool dmabuf_support_inited;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005084
Giulio Camuffo954f1832014-10-11 18:27:30 +03005085 if (!b->use_pixman)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005086 return;
5087
Pekka Paalanene4d231e2014-06-12 15:12:48 +03005088 dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf;
5089
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005090 weston_log("Switching to GL renderer\n");
5091
Giulio Camuffo954f1832014-10-11 18:27:30 +03005092 b->gbm = create_gbm_device(b->drm.fd);
5093 if (!b->gbm) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005094 weston_log("Failed to create gbm device. "
5095 "Aborting renderer switch\n");
5096 return;
5097 }
5098
Giulio Camuffo954f1832014-10-11 18:27:30 +03005099 wl_list_for_each(output, &b->compositor->output_list, base.link)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005100 pixman_renderer_output_destroy(&output->base);
5101
Giulio Camuffo954f1832014-10-11 18:27:30 +03005102 b->compositor->renderer->destroy(b->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005103
Giulio Camuffo954f1832014-10-11 18:27:30 +03005104 if (drm_backend_create_gl_renderer(b) < 0) {
5105 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005106 weston_log("Failed to create GL renderer. Quitting.\n");
5107 /* FIXME: we need a function to shutdown cleanly */
5108 assert(0);
5109 }
5110
Giulio Camuffo954f1832014-10-11 18:27:30 +03005111 wl_list_for_each(output, &b->compositor->output_list, base.link)
5112 drm_output_init_egl(output, b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005113
Giulio Camuffo954f1832014-10-11 18:27:30 +03005114 b->use_pixman = 0;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03005115
5116 if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) {
5117 if (linux_dmabuf_setup(b->compositor) < 0)
5118 weston_log("Error: initializing dmabuf "
5119 "support failed.\n");
5120 }
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005121}
5122
5123static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02005124renderer_switch_binding(struct weston_keyboard *keyboard,
5125 const struct timespec *time, uint32_t key, void *data)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005126{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005127 struct drm_backend *b =
Armin Krezović545dba62016-08-05 15:54:18 +02005128 to_drm_backend(keyboard->seat->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005129
Giulio Camuffo954f1832014-10-11 18:27:30 +03005130 switch_to_gl_renderer(b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02005131}
5132
Armin Krezović08368132016-09-30 14:11:05 +02005133static const struct weston_drm_output_api api = {
5134 drm_output_set_mode,
5135 drm_output_set_gbm_format,
5136 drm_output_set_seat,
5137};
5138
Giulio Camuffo954f1832014-10-11 18:27:30 +03005139static struct drm_backend *
5140drm_backend_create(struct weston_compositor *compositor,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005141 struct weston_drm_backend_config *config)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005142{
Giulio Camuffo954f1832014-10-11 18:27:30 +03005143 struct drm_backend *b;
David Herrmann0af066f2012-10-29 19:21:16 +01005144 struct udev_device *drm_device;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005145 struct wl_event_loop *loop;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005146 const char *seat_id = default_seat;
Armin Krezović08368132016-09-30 14:11:05 +02005147 int ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005148
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04005149 weston_log("initializing drm backend\n");
5150
Giulio Camuffo954f1832014-10-11 18:27:30 +03005151 b = zalloc(sizeof *b);
5152 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005153 return NULL;
Daniel Stone725c2c32012-06-22 14:04:36 +01005154
Daniel Stone6020f472018-02-05 15:46:20 +00005155 b->state_invalid = true;
Daniel Stoneefa504f2016-12-19 16:48:20 +00005156 b->drm.fd = -1;
Daniel Stone087ddf02017-02-14 17:51:30 +00005157 wl_array_init(&b->unused_crtcs);
5158 wl_array_init(&b->unused_connectors);
Daniel Stoneefa504f2016-12-19 16:48:20 +00005159
Pekka Paalanen68583832015-05-19 09:53:16 +03005160 /*
5161 * KMS support for hardware planes cannot properly synchronize
5162 * without nuclear page flip. Without nuclear/atomic, hw plane
5163 * and cursor plane updates would either tear or cause extra
5164 * waits for vblanks which means dropping the compositor framerate
Pekka Paalanen3f32a132015-09-07 15:38:43 +03005165 * to a fraction. For cursors, it's not so bad, so they are
5166 * enabled.
Pekka Paalanen68583832015-05-19 09:53:16 +03005167 *
5168 * These can be enabled again when nuclear/atomic support lands.
5169 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03005170 b->sprites_are_broken = 1;
Giulio Camuffo954f1832014-10-11 18:27:30 +03005171 b->compositor = compositor;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005172 b->use_pixman = config->use_pixman;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00005173 b->pageflip_timeout = config->pageflip_timeout;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07005174
Pekka Paalanen7da9a382017-08-30 11:29:49 +03005175 compositor->backend = &b->base;
5176
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005177 if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0)
5178 goto err_compositor;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07005179
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005180 if (config->seat_id)
5181 seat_id = config->seat_id;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02005182
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01005183 /* Check if we run drm-backend using weston-launch */
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005184 compositor->launcher = weston_launcher_connect(compositor, config->tty,
5185 seat_id, true);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005186 if (compositor->launcher == NULL) {
Pekka Paalanena453f4d2017-10-31 10:19:48 +02005187 weston_log("fatal: drm backend should be run using "
5188 "weston-launch binary, or your system should "
5189 "provide the logind D-Bus API.\n");
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01005190 goto err_compositor;
5191 }
5192
Giulio Camuffo954f1832014-10-11 18:27:30 +03005193 b->udev = udev_new();
5194 if (b->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02005195 weston_log("failed to initialize udev context\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07005196 goto err_launcher;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005197 }
5198
Giulio Camuffo954f1832014-10-11 18:27:30 +03005199 b->session_listener.notify = session_notify;
5200 wl_signal_add(&compositor->session_signal, &b->session_listener);
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05005201
Pekka Paalanenb45ed8b2017-03-28 18:04:27 +03005202 if (config->specific_device)
5203 drm_device = open_specific_drm_device(b, config->specific_device);
5204 else
5205 drm_device = find_primary_gpu(b, seat_id);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04005206 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02005207 weston_log("no drm device found\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07005208 goto err_udev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005209 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005210
Daniel Stoneefa504f2016-12-19 16:48:20 +00005211 if (init_kms_caps(b) < 0) {
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02005212 weston_log("failed to initialize kms\n");
5213 goto err_udev_dev;
5214 }
5215
Giulio Camuffo954f1832014-10-11 18:27:30 +03005216 if (b->use_pixman) {
5217 if (init_pixman(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02005218 weston_log("failed to initialize pixman renderer\n");
5219 goto err_udev_dev;
5220 }
5221 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +03005222 if (init_egl(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02005223 weston_log("failed to initialize egl\n");
5224 goto err_udev_dev;
5225 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005226 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05005227
Giulio Camuffo954f1832014-10-11 18:27:30 +03005228 b->base.destroy = drm_destroy;
5229 b->base.restore = drm_restore;
Daniel Stoneeedf84c2017-02-10 18:06:04 +00005230 b->base.repaint_begin = drm_repaint_begin;
5231 b->base.repaint_flush = drm_repaint_flush;
5232 b->base.repaint_cancel = drm_repaint_cancel;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02005233
Bob Ham91880f12016-01-12 10:21:47 +00005234 weston_setup_vt_switch_bindings(compositor);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04005235
Daniel Stone085d2b92015-05-21 00:00:57 +01005236 wl_list_init(&b->plane_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005237 create_sprites(b);
Jesse Barnes58ef3792012-02-23 09:45:49 -05005238
Giulio Camuffo954f1832014-10-11 18:27:30 +03005239 if (udev_input_init(&b->input,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03005240 compositor, b->udev, seat_id,
5241 config->configure_device) < 0) {
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03005242 weston_log("failed to create input devices\n");
5243 goto err_sprite;
5244 }
5245
Ucan, Emre (ADITG/SW1)9a200d72017-02-02 14:06:56 +00005246 if (create_outputs(b, drm_device) < 0) {
Daniel Stoneefa504f2016-12-19 16:48:20 +00005247 weston_log("failed to create output for %s\n", b->drm.filename);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03005248 goto err_udev_input;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005249 }
5250
Jason Ekstrand9fc71512014-04-02 19:53:46 -05005251 /* A this point we have some idea of whether or not we have a working
5252 * cursor plane. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03005253 if (!b->cursors_are_broken)
5254 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
Jason Ekstrand9fc71512014-04-02 19:53:46 -05005255
Giulio Camuffo954f1832014-10-11 18:27:30 +03005256 loop = wl_display_get_event_loop(compositor->wl_display);
5257 b->drm_source =
5258 wl_event_loop_add_fd(loop, b->drm.fd,
5259 WL_EVENT_READABLE, on_drm_input, b);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005260
Giulio Camuffo954f1832014-10-11 18:27:30 +03005261 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
5262 if (b->udev_monitor == NULL) {
Abdur Rehman4dca0e12017-01-01 19:46:35 +05005263 weston_log("failed to initialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01005264 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005265 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03005266 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005267 "drm", NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005268 b->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02005269 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03005270 udev_monitor_get_fd(b->udev_monitor),
5271 WL_EVENT_READABLE, udev_drm_event, b);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005272
Giulio Camuffo954f1832014-10-11 18:27:30 +03005273 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02005274 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01005275 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005276 }
5277
Daniel Stonea96b93c2012-06-22 14:04:37 +01005278 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01005279
Giulio Camuffo954f1832014-10-11 18:27:30 +03005280 weston_compositor_add_debug_binding(compositor, KEY_O,
5281 planes_binding, b);
5282 weston_compositor_add_debug_binding(compositor, KEY_C,
5283 planes_binding, b);
5284 weston_compositor_add_debug_binding(compositor, KEY_V,
5285 planes_binding, b);
5286 weston_compositor_add_debug_binding(compositor, KEY_Q,
5287 recorder_binding, b);
5288 weston_compositor_add_debug_binding(compositor, KEY_W,
5289 renderer_switch_binding, b);
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02005290
Pekka Paalanene4d231e2014-06-12 15:12:48 +03005291 if (compositor->renderer->import_dmabuf) {
5292 if (linux_dmabuf_setup(compositor) < 0)
5293 weston_log("Error: initializing dmabuf "
5294 "support failed.\n");
5295 }
5296
Armin Krezović08368132016-09-30 14:11:05 +02005297 ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME,
5298 &api, sizeof(api));
5299
5300 if (ret < 0) {
5301 weston_log("Failed to register output API.\n");
5302 goto err_udev_monitor;
5303 }
5304
Giulio Camuffo954f1832014-10-11 18:27:30 +03005305 return b;
Daniel Stonea96b93c2012-06-22 14:04:37 +01005306
5307err_udev_monitor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005308 wl_event_source_remove(b->udev_drm_source);
5309 udev_monitor_unref(b->udev_monitor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01005310err_drm_source:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005311 wl_event_source_remove(b->drm_source);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03005312err_udev_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005313 udev_input_destroy(&b->input);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04005314err_sprite:
Emmanuel Gil Peyrotb8347e32016-05-02 22:40:13 +01005315 if (b->gbm)
5316 gbm_device_destroy(b->gbm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005317 destroy_sprites(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01005318err_udev_dev:
5319 udev_device_unref(drm_device);
Kristian Høgsberg3f495872013-09-18 23:00:17 -07005320err_launcher:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005321 weston_launcher_destroy(compositor->launcher);
Daniel Stonea96b93c2012-06-22 14:04:37 +01005322err_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005323 udev_unref(b->udev);
Daniel Stonea96b93c2012-06-22 14:04:37 +01005324err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03005325 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005326 free(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01005327 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04005328}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005329
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005330static void
5331config_init_to_defaults(struct weston_drm_backend_config *config)
5332{
5333}
5334
Giulio Camuffo954f1832014-10-11 18:27:30 +03005335WL_EXPORT int
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01005336weston_backend_init(struct weston_compositor *compositor,
5337 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005338{
Giulio Camuffo954f1832014-10-11 18:27:30 +03005339 struct drm_backend *b;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005340 struct weston_drm_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005341
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005342 if (config_base == NULL ||
5343 config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION ||
5344 config_base->struct_size > sizeof(struct weston_drm_backend_config)) {
5345 weston_log("drm backend config structure is invalid\n");
5346 return -1;
5347 }
Benjamin Franzke117483d2011-08-30 11:38:26 +02005348
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005349 config_init_to_defaults(&config);
5350 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07005351
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005352 b = drm_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03005353 if (b == NULL)
5354 return -1;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07005355
Giulio Camuffo954f1832014-10-11 18:27:30 +03005356 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005357}