blob: b9014c8ac131857e207e217b49ebf23ed9bf6775 [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
Giulio Camuffo954f1832014-10-11 18:27:30 +0300142struct drm_backend {
143 struct weston_backend base;
144 struct weston_compositor *compositor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400145
146 struct udev *udev;
147 struct wl_event_source *drm_source;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400148
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100149 struct udev_monitor *udev_monitor;
150 struct wl_event_source *udev_drm_source;
151
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100152 struct {
David Herrmannd7488c22012-03-11 20:05:21 +0100153 int id;
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100154 int fd;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300155 char *filename;
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100156 } drm;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200157 struct gbm_device *gbm;
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700158 struct wl_listener session_listener;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100159 uint32_t gbm_format;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200160
Rob Clark4339add2012-08-09 14:18:28 -0500161 /* we need these parameters in order to not fail drmModeAddFB2()
162 * due to out of bounds dimensions, and then mistakenly set
163 * sprites_are_broken:
164 */
Daniel Stonef214fdc2016-11-14 17:43:57 +0000165 int min_width, max_width;
166 int min_height, max_height;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200167 int no_addfb2;
Rob Clark4339add2012-08-09 14:18:28 -0500168
Daniel Stone085d2b92015-05-21 00:00:57 +0100169 struct wl_list plane_list;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500170 int sprites_are_broken;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +0200171 int sprites_hidden;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500172
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000173 void *repaint_data;
174
Rob Clarkab5b1e32012-08-09 13:24:45 -0500175 int cursors_are_broken;
176
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100177 bool universal_planes;
178
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200179 int use_pixman;
180
Rob Bradfordd355b802013-05-31 18:09:55 +0100181 struct udev_input input;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -0300182
Daniel Stone70d337d2015-06-16 18:42:23 +0100183 int32_t cursor_width;
184 int32_t cursor_height;
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +0000185
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000186 uint32_t pageflip_timeout;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400187};
188
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400189struct drm_mode {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500190 struct weston_mode base;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400191 drmModeModeInfo mode_info;
192};
193
Daniel Stonefc175a72017-04-04 17:54:22 +0100194enum drm_fb_type {
195 BUFFER_INVALID = 0, /**< never used */
196 BUFFER_CLIENT, /**< directly sourced from client */
197 BUFFER_PIXMAN_DUMB, /**< internal Pixman rendering */
198 BUFFER_GBM_SURFACE, /**< internal EGL rendering */
Daniel Stonee4256832017-04-04 17:54:27 +0100199 BUFFER_CURSOR, /**< internal cursor buffer */
Daniel Stonefc175a72017-04-04 17:54:22 +0100200};
201
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300202struct drm_fb {
Daniel Stonefc175a72017-04-04 17:54:22 +0100203 enum drm_fb_type type;
204
Daniel Stone6e7a9612017-04-04 17:54:26 +0100205 int refcnt;
206
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200207 uint32_t fb_id, stride, handle, size;
Daniel Stone0b70fa42017-04-04 17:54:23 +0100208 const struct pixel_format_info *format;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000209 int width, height;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200210 int fd;
Pekka Paalanende685b82012-12-04 15:58:12 +0200211 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200212
213 /* Used by gbm fbs */
214 struct gbm_bo *bo;
Daniel Stone05a5ac22017-04-04 17:54:25 +0100215 struct gbm_surface *gbm_surface;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200216
217 /* Used by dumb fbs */
218 void *map;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300219};
220
Richard Hughes2b2092a2013-04-24 14:58:02 +0100221struct drm_edid {
222 char eisa_id[13];
223 char monitor_name[13];
224 char pnp_id[5];
225 char serial_number[13];
226};
227
Daniel Stone08d4edf2017-04-04 17:54:34 +0100228/**
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000229 * Pending state holds one or more drm_output_state structures, collected from
230 * performing repaint. This pending state is transient, and only lives between
231 * beginning a repaint group and flushing the results: after flush, each
232 * output state will complete and be retired separately.
233 */
234struct drm_pending_state {
235 struct drm_backend *backend;
236};
237
238/**
Daniel Stone08d4edf2017-04-04 17:54:34 +0100239 * A plane represents one buffer, positioned within a CRTC, and stacked
240 * relative to other planes on the same CRTC.
241 *
242 * Each CRTC has a 'primary plane', which use used to display the classic
243 * framebuffer contents, as accessed through the legacy drmModeSetCrtc
244 * call (which combines setting the CRTC's actual physical mode, and the
245 * properties of the primary plane).
246 *
247 * The cursor plane also has its own alternate legacy API.
248 *
249 * Other planes are used opportunistically to display content we do not
250 * wish to blit into the primary plane. These non-primary/cursor planes
251 * are referred to as 'sprites'.
252 */
253struct drm_plane {
254 struct wl_list link;
255
256 struct weston_plane base;
257
258 struct drm_output *output;
259 struct drm_backend *backend;
260
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100261 enum wdrm_plane_type type;
262
Daniel Stone08d4edf2017-04-04 17:54:34 +0100263 uint32_t possible_crtcs;
264 uint32_t plane_id;
265 uint32_t count_formats;
266
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100267 struct drm_property_info props[WDRM_PLANE__COUNT];
268
Daniel Stone08d4edf2017-04-04 17:54:34 +0100269 /* The last framebuffer submitted to the kernel for this plane. */
270 struct drm_fb *fb_current;
271 /* The previously-submitted framebuffer, where the hardware has not
272 * yet acknowledged display of fb_current. */
273 struct drm_fb *fb_last;
274 /* Framebuffer we are going to submit to the kernel when the current
275 * repaint is flushed. */
276 struct drm_fb *fb_pending;
277
278 int32_t src_x, src_y;
279 uint32_t src_w, src_h;
280 uint32_t dest_x, dest_y;
281 uint32_t dest_w, dest_h;
282
283 uint32_t formats[];
284};
285
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400286struct drm_output {
Armin Krezović08368132016-09-30 14:11:05 +0200287 struct weston_output base;
288 drmModeConnector *connector;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400289
Daniel Stone17339232015-11-28 12:09:47 +0000290 uint32_t crtc_id; /* object ID to pass to DRM functions */
291 int pipe; /* index of CRTC in resource array / bitmasks */
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400292 uint32_t connector_id;
Matt Roper361d2ad2011-08-29 13:52:23 -0700293 drmModeCrtcPtr original_crtc;
Richard Hughes2b2092a2013-04-24 14:58:02 +0100294 struct drm_edid edid;
Daniel Stone02cf4662017-03-03 16:19:39 +0000295
296 /* Holds the properties for the connector */
297 struct drm_property_info props_conn[WDRM_CONNECTOR__COUNT];
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200298
Daniel Stone36609c72015-06-18 07:49:02 +0100299 enum dpms_enum dpms;
Daniel Stone5bb8f582017-04-04 17:54:28 +0100300 struct backlight *backlight;
Daniel Stone36609c72015-06-18 07:49:02 +0100301
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +0300302 bool state_invalid;
303
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300304 int vblank_pending;
305 int page_flip_pending;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800306 int destroy_pending;
Armin Krezović08368132016-09-30 14:11:05 +0200307 int disable_pending;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300308
Daniel Stonee4256832017-04-04 17:54:27 +0100309 struct drm_fb *gbm_cursor_fb[2];
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400310 struct weston_plane cursor_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500311 struct weston_view *cursor_view;
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400312 int current_cursor;
Daniel Stone5bb8f582017-04-04 17:54:28 +0100313
314 struct gbm_surface *gbm_surface;
315 uint32_t gbm_format;
316
Daniel Stone4202e502016-10-24 13:26:42 +0100317 /* Plane for a fullscreen direct scanout view */
318 struct weston_plane scanout_plane;
Daniel Stonef30a18c2017-04-04 17:54:31 +0100319
320 /* The last framebuffer submitted to the kernel for this CRTC. */
321 struct drm_fb *fb_current;
322 /* The previously-submitted framebuffer, where the hardware has not
323 * yet acknowledged display of fb_current. */
324 struct drm_fb *fb_last;
325 /* Framebuffer we are going to submit to the kernel when the current
326 * repaint is flushed. */
327 struct drm_fb *fb_pending;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200328
329 struct drm_fb *dumb[2];
330 pixman_image_t *image[2];
331 int current_image;
332 pixman_region32_t previous_damage;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300333
334 struct vaapi_recorder *recorder;
335 struct wl_listener recorder_frame_listener;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000336
337 struct wl_event_source *pageflip_timer;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400338};
339
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300340static struct gl_renderer_interface *gl_renderer;
341
Kristian Høgsberg98cfea62013-02-18 16:15:53 -0500342static const char default_seat[] = "seat0";
Pekka Paalanen33156972012-08-03 13:30:30 -0400343
Armin Krezović545dba62016-08-05 15:54:18 +0200344static inline struct drm_output *
345to_drm_output(struct weston_output *base)
346{
347 return container_of(base, struct drm_output, base);
348}
349
350static inline struct drm_backend *
351to_drm_backend(struct weston_compositor *base)
352{
353 return container_of(base->backend, struct drm_backend, base);
354}
355
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000356static int
357pageflip_timeout(void *data) {
358 /*
359 * Our timer just went off, that means we're not receiving drm
360 * page flip events anymore for that output. Let's gracefully exit
361 * weston with a return value so devs can debug what's going on.
362 */
363 struct drm_output *output = data;
364 struct weston_compositor *compositor = output->base.compositor;
365
366 weston_log("Pageflip timeout reached on output %s, your "
367 "driver is probably buggy! Exiting.\n",
368 output->base.name);
369 weston_compositor_exit_with_code(compositor, EXIT_FAILURE);
370
371 return 0;
372}
373
374/* Creates the pageflip timer. Note that it isn't armed by default */
375static int
376drm_output_pageflip_timer_create(struct drm_output *output)
377{
378 struct wl_event_loop *loop = NULL;
379 struct weston_compositor *ec = output->base.compositor;
380
381 loop = wl_display_get_event_loop(ec->wl_display);
382 assert(loop);
383 output->pageflip_timer = wl_event_loop_add_timer(loop,
384 pageflip_timeout,
385 output);
386
387 if (output->pageflip_timer == NULL) {
388 weston_log("creating drm pageflip timer failed: %m\n");
389 return -1;
390 }
391
392 return 0;
393}
394
Daniel Stone02cf4662017-03-03 16:19:39 +0000395/**
396 * Get the current value of a KMS property
397 *
398 * Given a drmModeObjectGetProperties return, as well as the drm_property_info
399 * for the target property, return the current value of that property,
400 * with an optional default. If the property is a KMS enum type, the return
401 * value will be translated into the appropriate internal enum.
402 *
403 * If the property is not present, the default value will be returned.
404 *
405 * @param info Internal structure for property to look up
406 * @param props Raw KMS properties for the target object
407 * @param def Value to return if property is not found
408 */
409static uint64_t
410drm_property_get_value(struct drm_property_info *info,
411 drmModeObjectPropertiesPtr props,
412 uint64_t def)
413{
414 unsigned int i;
415
416 if (info->prop_id == 0)
417 return def;
418
419 for (i = 0; i < props->count_props; i++) {
420 unsigned int j;
421
422 if (props->props[i] != info->prop_id)
423 continue;
424
425 /* Simple (non-enum) types can return the value directly */
426 if (info->num_enum_values == 0)
427 return props->prop_values[i];
428
429 /* Map from raw value to enum value */
430 for (j = 0; j < info->num_enum_values; j++) {
431 if (!info->enum_values[j].valid)
432 continue;
433 if (info->enum_values[j].value != props->prop_values[i])
434 continue;
435
436 return j;
437 }
438
439 /* We don't have a mapping for this enum; return default. */
440 break;
441 }
442
443 return def;
444}
445
446/**
447 * Cache DRM property values
448 *
449 * Update a per-object array of drm_property_info structures, given the
450 * DRM properties of the object.
451 *
452 * Call this every time an object newly appears (note that only connectors
453 * can be hotplugged), the first time it is seen, or when its status changes
454 * in a way which invalidates the potential property values (currently, the
455 * only case for this is connector hotplug).
456 *
457 * This updates the property IDs and enum values within the drm_property_info
458 * array.
459 *
460 * DRM property enum values are dynamic at runtime; the user must query the
461 * property to find out the desired runtime value for a requested string
462 * name. Using the 'type' field on planes as an example, there is no single
463 * hardcoded constant for primary plane types; instead, the property must be
464 * queried at runtime to find the value associated with the string "Primary".
465 *
466 * This helper queries and caches the enum values, to allow us to use a set
467 * of compile-time-constant enums portably across various implementations.
468 * The values given in enum_names are searched for, and stored in the
469 * same-indexed field of the map array.
470 *
471 * @param b DRM backend object
472 * @param src DRM property info array to source from
473 * @param info DRM property info array to copy into
474 * @param num_infos Number of entries in the source array
475 * @param props DRM object properties for the object
476 */
477static void
478drm_property_info_populate(struct drm_backend *b,
479 const struct drm_property_info *src,
480 struct drm_property_info *info,
481 unsigned int num_infos,
482 drmModeObjectProperties *props)
483{
484 drmModePropertyRes *prop;
485 unsigned i, j;
486
487 for (i = 0; i < num_infos; i++) {
488 unsigned int j;
489
490 info[i].name = src[i].name;
491 info[i].prop_id = 0;
492 info[i].num_enum_values = src[i].num_enum_values;
493
494 if (src[i].num_enum_values == 0)
495 continue;
496
497 info[i].enum_values =
498 malloc(src[i].num_enum_values *
499 sizeof(*info[i].enum_values));
500 assert(info[i].enum_values);
501 for (j = 0; j < info[i].num_enum_values; j++) {
502 info[i].enum_values[j].name = src[i].enum_values[j].name;
503 info[i].enum_values[j].valid = false;
504 }
505 }
506
507 for (i = 0; i < props->count_props; i++) {
508 unsigned int k;
509
510 prop = drmModeGetProperty(b->drm.fd, props->props[i]);
511 if (!prop)
512 continue;
513
514 for (j = 0; j < num_infos; j++) {
515 if (!strcmp(prop->name, info[j].name))
516 break;
517 }
518
519 /* We don't know/care about this property. */
520 if (j == num_infos) {
521#ifdef DEBUG
522 weston_log("DRM debug: unrecognized property %u '%s'\n",
523 prop->prop_id, prop->name);
524#endif
525 drmModeFreeProperty(prop);
526 continue;
527 }
528
529 if (info[j].num_enum_values == 0 &&
530 (prop->flags & DRM_MODE_PROP_ENUM)) {
531 weston_log("DRM: expected property %s to not be an"
532 " enum, but it is; ignoring\n", prop->name);
533 drmModeFreeProperty(prop);
534 continue;
535 }
536
537 info[j].prop_id = props->props[i];
538
539 if (info[j].num_enum_values == 0) {
540 drmModeFreeProperty(prop);
541 continue;
542 }
543
544 if (!(prop->flags & DRM_MODE_PROP_ENUM)) {
545 weston_log("DRM: expected property %s to be an enum,"
546 " but it is not; ignoring\n", prop->name);
547 drmModeFreeProperty(prop);
548 info[j].prop_id = 0;
549 continue;
550 }
551
552 for (k = 0; k < info[j].num_enum_values; k++) {
553 int l;
554
555 for (l = 0; l < prop->count_enums; l++) {
556 if (!strcmp(prop->enums[l].name,
557 info[j].enum_values[k].name))
558 break;
559 }
560
561 if (l == prop->count_enums)
562 continue;
563
564 info[j].enum_values[k].valid = true;
565 info[j].enum_values[k].value = prop->enums[l].value;
566 }
567
568 drmModeFreeProperty(prop);
569 }
570
571#ifdef DEBUG
572 for (i = 0; i < num_infos; i++) {
573 if (info[i].prop_id == 0)
574 weston_log("DRM warning: property '%s' missing\n",
575 info[i].name);
576 }
577#endif
578}
579
580/**
581 * Free DRM property information
582 *
583 * Frees all memory associated with a DRM property info array.
584 *
585 * @param info DRM property info array
586 * @param num_props Number of entries in array to free
587 */
588static void
589drm_property_info_free(struct drm_property_info *info, int num_props)
590{
591 int i;
592
593 for (i = 0; i < num_props; i++)
594 free(info[i].enum_values);
595}
596
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400597static void
598drm_output_set_cursor(struct drm_output *output);
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400599
Mario Kleinerf507ec32015-06-21 21:25:14 +0200600static void
601drm_output_update_msc(struct drm_output *output, unsigned int seq);
602
Jesse Barnes58ef3792012-02-23 09:45:49 -0500603static int
Daniel Stone08d4edf2017-04-04 17:54:34 +0100604drm_plane_crtc_supported(struct drm_output *output, struct drm_plane *plane)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500605{
Daniel Stone08d4edf2017-04-04 17:54:34 +0100606 return !!(plane->possible_crtcs & (1 << output->pipe));
Jesse Barnes58ef3792012-02-23 09:45:49 -0500607}
608
Daniel Stone72c0e1b2017-02-09 13:49:15 +0000609static struct drm_output *
610drm_output_find_by_crtc(struct drm_backend *b, uint32_t crtc_id)
611{
612 struct drm_output *output;
613
614 wl_list_for_each(output, &b->compositor->output_list, base.link) {
615 if (output->crtc_id == crtc_id)
616 return output;
617 }
618
619 wl_list_for_each(output, &b->compositor->pending_output_list,
620 base.link) {
621 if (output->crtc_id == crtc_id)
622 return output;
623 }
624
625 return NULL;
626}
627
Daniel Stonec0ec7592017-02-09 13:58:35 +0000628static struct drm_output *
629drm_output_find_by_connector(struct drm_backend *b, uint32_t connector_id)
630{
631 struct drm_output *output;
632
633 wl_list_for_each(output, &b->compositor->output_list, base.link) {
634 if (output->connector_id == connector_id)
635 return output;
636 }
637
638 wl_list_for_each(output, &b->compositor->pending_output_list,
639 base.link) {
640 if (output->connector_id == connector_id)
641 return output;
642 }
643
644 return NULL;
645}
646
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300647static void
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100648drm_fb_destroy(struct drm_fb *fb)
649{
650 if (fb->fb_id != 0)
651 drmModeRmFB(fb->fd, fb->fb_id);
652 weston_buffer_reference(&fb->buffer_ref, NULL);
653 free(fb);
654}
655
656static void
657drm_fb_destroy_dumb(struct drm_fb *fb)
658{
659 struct drm_mode_destroy_dumb destroy_arg;
660
661 assert(fb->type == BUFFER_PIXMAN_DUMB);
662
663 if (fb->map && fb->size > 0)
664 munmap(fb->map, fb->size);
665
666 memset(&destroy_arg, 0, sizeof(destroy_arg));
667 destroy_arg.handle = fb->handle;
668 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
669
670 drm_fb_destroy(fb);
671}
672
673static void
674drm_fb_destroy_gbm(struct gbm_bo *bo, void *data)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300675{
676 struct drm_fb *fb = data;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300677
Daniel Stonee4256832017-04-04 17:54:27 +0100678 assert(fb->type == BUFFER_GBM_SURFACE || fb->type == BUFFER_CLIENT ||
679 fb->type == BUFFER_CURSOR);
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100680 drm_fb_destroy(fb);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300681}
682
683static struct drm_fb *
Daniel Stonef214fdc2016-11-14 17:43:57 +0000684drm_fb_create_dumb(struct drm_backend *b, int width, int height,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300685 uint32_t format)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200686{
687 struct drm_fb *fb;
688 int ret;
689
690 struct drm_mode_create_dumb create_arg;
691 struct drm_mode_destroy_dumb destroy_arg;
692 struct drm_mode_map_dumb map_arg;
693
Peter Huttererf3d62272013-08-08 11:57:05 +1000694 fb = zalloc(sizeof *fb);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200695 if (!fb)
696 return NULL;
697
Daniel Stone6e7a9612017-04-04 17:54:26 +0100698 fb->refcnt = 1;
699
Daniel Stone0b70fa42017-04-04 17:54:23 +0100700 fb->format = pixel_format_get_info(format);
701 if (!fb->format) {
702 weston_log("failed to look up format 0x%lx\n",
703 (unsigned long) format);
704 goto err_fb;
705 }
706
707 if (!fb->format->depth || !fb->format->bpp) {
708 weston_log("format 0x%lx is not compatible with dumb buffers\n",
709 (unsigned long) format);
710 goto err_fb;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300711 }
712
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700713 memset(&create_arg, 0, sizeof create_arg);
Daniel Stone0b70fa42017-04-04 17:54:23 +0100714 create_arg.bpp = fb->format->bpp;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200715 create_arg.width = width;
716 create_arg.height = height;
717
Giulio Camuffo954f1832014-10-11 18:27:30 +0300718 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200719 if (ret)
720 goto err_fb;
721
Daniel Stonefc175a72017-04-04 17:54:22 +0100722 fb->type = BUFFER_PIXMAN_DUMB;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200723 fb->handle = create_arg.handle;
724 fb->stride = create_arg.pitch;
725 fb->size = create_arg.size;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000726 fb->width = width;
727 fb->height = height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300728 fb->fd = b->drm.fd;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200729
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300730 ret = -1;
731
732 if (!b->no_addfb2) {
Yong Bakos4b6321f2016-08-17 17:37:55 -0700733 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300734
735 handles[0] = fb->handle;
736 pitches[0] = fb->stride;
737 offsets[0] = 0;
738
739 ret = drmModeAddFB2(b->drm.fd, width, height,
Daniel Stone0b70fa42017-04-04 17:54:23 +0100740 fb->format->format,
741 handles, pitches, offsets,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300742 &fb->fb_id, 0);
743 if (ret) {
744 weston_log("addfb2 failed: %m\n");
745 b->no_addfb2 = 1;
746 }
747 }
748
749 if (ret) {
Daniel Stone0b70fa42017-04-04 17:54:23 +0100750 ret = drmModeAddFB(b->drm.fd, width, height,
751 fb->format->depth, fb->format->bpp,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300752 fb->stride, fb->handle, &fb->fb_id);
753 }
754
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200755 if (ret)
756 goto err_bo;
757
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700758 memset(&map_arg, 0, sizeof map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200759 map_arg.handle = fb->handle;
Chris Michaeleb2074a2013-05-01 21:26:02 -0400760 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200761 if (ret)
762 goto err_add_fb;
763
Chris Michael4a7ce1f2015-11-10 10:40:37 -0500764 fb->map = mmap(NULL, fb->size, PROT_WRITE,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300765 MAP_SHARED, b->drm.fd, map_arg.offset);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200766 if (fb->map == MAP_FAILED)
767 goto err_add_fb;
768
769 return fb;
770
771err_add_fb:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300772 drmModeRmFB(b->drm.fd, fb->fb_id);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200773err_bo:
774 memset(&destroy_arg, 0, sizeof(destroy_arg));
775 destroy_arg.handle = create_arg.handle;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300776 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200777err_fb:
778 free(fb);
779 return NULL;
780}
781
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200782static struct drm_fb *
Daniel Stone6e7a9612017-04-04 17:54:26 +0100783drm_fb_ref(struct drm_fb *fb)
784{
785 fb->refcnt++;
786 return fb;
787}
788
789static struct drm_fb *
Daniel Stonefc175a72017-04-04 17:54:22 +0100790drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend,
791 uint32_t format, enum drm_fb_type type)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300792{
793 struct drm_fb *fb = gbm_bo_get_user_data(bo);
Derek Foreman482ffdf2016-07-08 12:50:57 -0500794 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300795 int ret;
796
Daniel Stonefc175a72017-04-04 17:54:22 +0100797 if (fb) {
798 assert(fb->type == type);
Daniel Stone6e7a9612017-04-04 17:54:26 +0100799 return drm_fb_ref(fb);
Daniel Stonefc175a72017-04-04 17:54:22 +0100800 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300801
Bryce Harringtonde16d892014-11-20 22:21:57 -0800802 fb = zalloc(sizeof *fb);
803 if (fb == NULL)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200804 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300805
Daniel Stonefc175a72017-04-04 17:54:22 +0100806 fb->type = type;
Daniel Stone6e7a9612017-04-04 17:54:26 +0100807 fb->refcnt = 1;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300808 fb->bo = bo;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300809
Daniel Stonec8c917c2016-11-14 17:45:58 +0000810 fb->width = gbm_bo_get_width(bo);
811 fb->height = gbm_bo_get_height(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200812 fb->stride = gbm_bo_get_stride(bo);
813 fb->handle = gbm_bo_get_handle(bo).u32;
Daniel Stone0b70fa42017-04-04 17:54:23 +0100814 fb->format = pixel_format_get_info(format);
Daniel Stonec8c917c2016-11-14 17:45:58 +0000815 fb->size = fb->stride * fb->height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300816 fb->fd = backend->drm.fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300817
Daniel Stone0b70fa42017-04-04 17:54:23 +0100818 if (!fb->format) {
819 weston_log("couldn't look up format 0x%lx\n",
820 (unsigned long) format);
821 goto err_free;
822 }
823
Daniel Stonec8c917c2016-11-14 17:45:58 +0000824 if (backend->min_width > fb->width ||
825 fb->width > backend->max_width ||
826 backend->min_height > fb->height ||
827 fb->height > backend->max_height) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200828 weston_log("bo geometry out of bounds\n");
829 goto err_free;
830 }
831
832 ret = -1;
833
Giulio Camuffo954f1832014-10-11 18:27:30 +0300834 if (format && !backend->no_addfb2) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200835 handles[0] = fb->handle;
836 pitches[0] = fb->stride;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200837 offsets[0] = 0;
838
Daniel Stonec8c917c2016-11-14 17:45:58 +0000839 ret = drmModeAddFB2(backend->drm.fd, fb->width, fb->height,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200840 format, handles, pitches, offsets,
841 &fb->fb_id, 0);
842 if (ret) {
843 weston_log("addfb2 failed: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300844 backend->no_addfb2 = 1;
845 backend->sprites_are_broken = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200846 }
847 }
848
Daniel Stone0b70fa42017-04-04 17:54:23 +0100849 if (ret && fb->format->depth && fb->format->bpp)
Daniel Stonec8c917c2016-11-14 17:45:58 +0000850 ret = drmModeAddFB(backend->drm.fd, fb->width, fb->height,
Daniel Stone0b70fa42017-04-04 17:54:23 +0100851 fb->format->depth, fb->format->bpp,
852 fb->stride, fb->handle, &fb->fb_id);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200853
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300854 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200855 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200856 goto err_free;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300857 }
858
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100859 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_gbm);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300860
861 return fb;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200862
863err_free:
864 free(fb);
865 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300866}
867
868static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500869drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200870{
Pekka Paalanende685b82012-12-04 15:58:12 +0200871 assert(fb->buffer_ref.buffer == NULL);
Daniel Stonefc175a72017-04-04 17:54:22 +0100872 assert(fb->type == BUFFER_CLIENT);
Pekka Paalanende685b82012-12-04 15:58:12 +0200873 weston_buffer_reference(&fb->buffer_ref, buffer);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200874}
875
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200876static void
Daniel Stone05a5ac22017-04-04 17:54:25 +0100877drm_fb_unref(struct drm_fb *fb)
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200878{
879 if (!fb)
880 return;
881
Daniel Stone6e7a9612017-04-04 17:54:26 +0100882 assert(fb->refcnt > 0);
883 if (--fb->refcnt > 0)
884 return;
885
Daniel Stonefc175a72017-04-04 17:54:22 +0100886 switch (fb->type) {
887 case BUFFER_PIXMAN_DUMB:
Daniel Stone6e7a9612017-04-04 17:54:26 +0100888 drm_fb_destroy_dumb(fb);
Daniel Stonefc175a72017-04-04 17:54:22 +0100889 break;
Daniel Stonee4256832017-04-04 17:54:27 +0100890 case BUFFER_CURSOR:
Daniel Stonefc175a72017-04-04 17:54:22 +0100891 case BUFFER_CLIENT:
892 gbm_bo_destroy(fb->bo);
893 break;
894 case BUFFER_GBM_SURFACE:
Daniel Stone05a5ac22017-04-04 17:54:25 +0100895 gbm_surface_release_buffer(fb->gbm_surface, fb->bo);
Daniel Stonefc175a72017-04-04 17:54:22 +0100896 break;
897 default:
898 assert(NULL);
899 break;
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200900 }
901}
902
Daniel Stone90648872016-10-21 18:08:37 +0100903static int
904drm_view_transform_supported(struct weston_view *ev)
905{
906 return !ev->transform.enabled ||
907 (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
908}
909
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000910/**
911 * Allocate a new drm_pending_state
912 *
913 * Allocate a new, empty, 'pending state' structure to be used across a
914 * repaint cycle or similar.
915 *
916 * @param backend DRM backend
917 * @returns Newly-allocated pending state structure
918 */
919static struct drm_pending_state *
920drm_pending_state_alloc(struct drm_backend *backend)
921{
922 struct drm_pending_state *ret;
923
924 ret = calloc(1, sizeof(*ret));
925 if (!ret)
926 return NULL;
927
928 ret->backend = backend;
929
930 return ret;
931}
932
933/**
934 * Free a drm_pending_state structure
935 *
936 * Frees a pending_state structure.
937 *
938 * @param pending_state Pending state structure to free
939 */
940static void
941drm_pending_state_free(struct drm_pending_state *pending_state)
942{
943 if (!pending_state)
944 return;
945
946 free(pending_state);
947}
948
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500949static uint32_t
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200950drm_output_check_scanout_format(struct drm_output *output,
951 struct weston_surface *es, struct gbm_bo *bo)
952{
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200953 uint32_t format;
954 pixman_region32_t r;
955
956 format = gbm_bo_get_format(bo);
957
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700958 if (format == GBM_FORMAT_ARGB8888) {
959 /* We can scanout an ARGB buffer if the surface's
960 * opaque region covers the whole output, but we have
961 * to use XRGB as the KMS format code. */
Kristian Høgsberg1be87e32014-01-17 14:22:41 -0800962 pixman_region32_init_rect(&r, 0, 0,
963 output->base.width,
964 output->base.height);
965 pixman_region32_subtract(&r, &r, &es->opaque);
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200966
967 if (!pixman_region32_not_empty(&r))
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500968 format = GBM_FORMAT_XRGB8888;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200969
970 pixman_region32_fini(&r);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500971 }
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700972
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100973 if (output->gbm_format == format)
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700974 return format;
975
976 return 0;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200977}
978
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400979static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200980drm_output_prepare_scanout_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500981 struct weston_view *ev)
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500982{
Armin Krezović545dba62016-08-05 15:54:18 +0200983 struct drm_backend *b = to_drm_backend(output->base.compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500984 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200985 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300986 struct gbm_bo *bo;
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500987 uint32_t format;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500988
Daniel Stone90648872016-10-21 18:08:37 +0100989 /* Don't import buffers which span multiple outputs. */
990 if (ev->output_mask != (1u << output->base.id))
991 return NULL;
992
Daniel Stone296d7a92016-10-21 18:05:37 +0100993 /* We use GBM to import buffers. */
994 if (b->gbm == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400995 return NULL;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500996
Daniel Stone296d7a92016-10-21 18:05:37 +0100997 if (buffer == NULL)
998 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +0100999 if (wl_shm_buffer_get(buffer->resource))
1000 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001001
1002 /* Make sure our view is exactly compatible with the output. */
1003 if (ev->geometry.x != output->base.x ||
1004 ev->geometry.y != output->base.y)
1005 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +01001006 if (buffer->width != output->base.current_mode->width ||
1007 buffer->height != output->base.current_mode->height)
1008 return NULL;
1009
Daniel Stone296d7a92016-10-21 18:05:37 +01001010 if (ev->transform.enabled)
1011 return NULL;
Pekka Paalanen5580f222015-02-17 16:33:18 +02001012 if (ev->geometry.scissor_enabled)
1013 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001014 if (viewport->buffer.transform != output->base.transform)
1015 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +01001016 if (viewport->buffer.scale != output->base.current_scale)
1017 return NULL;
1018 if (!drm_view_transform_supported(ev))
1019 return NULL;
1020
1021 if (ev->alpha != 1.0f)
1022 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001023
Giulio Camuffo954f1832014-10-11 18:27:30 +03001024 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
Kristian Høgsberg63996462013-09-03 22:27:08 -07001025 buffer->resource, GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001026
Rob Bradford9b101872012-09-14 23:25:41 +01001027 /* Unable to use the buffer for scanout */
1028 if (!bo)
1029 return NULL;
1030
Jason Ekstranda7af7042013-10-12 22:38:11 -05001031 format = drm_output_check_scanout_format(output, ev->surface, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001032 if (format == 0) {
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +03001033 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001034 return NULL;
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +03001035 }
1036
Daniel Stone5bb8f582017-04-04 17:54:28 +01001037 output->fb_pending = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT);
1038 if (!output->fb_pending) {
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001039 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001040 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001041 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001042
Daniel Stone5bb8f582017-04-04 17:54:28 +01001043 drm_fb_set_buffer(output->fb_pending, buffer);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001044
Daniel Stone4202e502016-10-24 13:26:42 +01001045 return &output->scanout_plane;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001046}
1047
Daniel Stone95d48a22017-04-04 17:54:30 +01001048static struct drm_fb *
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001049drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001050{
Armin Krezović545dba62016-08-05 15:54:18 +02001051 struct drm_backend *b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001052 struct gbm_bo *bo;
Daniel Stone95d48a22017-04-04 17:54:30 +01001053 struct drm_fb *ret;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001054
Giulio Camuffo954f1832014-10-11 18:27:30 +03001055 output->base.compositor->renderer->repaint_output(&output->base,
1056 damage);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001057
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001058 bo = gbm_surface_lock_front_buffer(output->gbm_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001059 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +02001060 weston_log("failed to lock front buffer: %m\n");
Daniel Stone95d48a22017-04-04 17:54:30 +01001061 return NULL;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001062 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001063
Daniel Stone95d48a22017-04-04 17:54:30 +01001064 ret = drm_fb_get_from_bo(bo, b, output->gbm_format, BUFFER_GBM_SURFACE);
1065 if (!ret) {
Martin Minarik6d118362012-06-07 18:01:59 +02001066 weston_log("failed to get drm_fb for bo\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001067 gbm_surface_release_buffer(output->gbm_surface, bo);
Daniel Stone95d48a22017-04-04 17:54:30 +01001068 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001069 }
Daniel Stone95d48a22017-04-04 17:54:30 +01001070 ret->gbm_surface = output->gbm_surface;
1071
1072 return ret;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001073}
1074
Daniel Stone95d48a22017-04-04 17:54:30 +01001075static struct drm_fb *
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001076drm_output_render_pixman(struct drm_output *output, pixman_region32_t *damage)
1077{
1078 struct weston_compositor *ec = output->base.compositor;
1079 pixman_region32_t total_damage, previous_damage;
1080
1081 pixman_region32_init(&total_damage);
1082 pixman_region32_init(&previous_damage);
1083
1084 pixman_region32_copy(&previous_damage, damage);
1085
1086 pixman_region32_union(&total_damage, damage, &output->previous_damage);
1087 pixman_region32_copy(&output->previous_damage, &previous_damage);
1088
1089 output->current_image ^= 1;
1090
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001091 pixman_renderer_output_set_buffer(&output->base,
1092 output->image[output->current_image]);
1093
1094 ec->renderer->repaint_output(&output->base, &total_damage);
1095
1096 pixman_region32_fini(&total_damage);
1097 pixman_region32_fini(&previous_damage);
Daniel Stone95d48a22017-04-04 17:54:30 +01001098
1099 return drm_fb_ref(output->dumb[output->current_image]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001100}
1101
1102static void
1103drm_output_render(struct drm_output *output, pixman_region32_t *damage)
1104{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001105 struct weston_compositor *c = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02001106 struct drm_backend *b = to_drm_backend(c);
Daniel Stone95d48a22017-04-04 17:54:30 +01001107 struct drm_fb *fb;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001108
Daniel Stone4e84f7d2017-04-04 17:54:29 +01001109 /* If we already have a client buffer promoted to scanout, then we don't
1110 * want to render. */
1111 if (output->fb_pending)
1112 return;
1113
Giulio Camuffo954f1832014-10-11 18:27:30 +03001114 if (b->use_pixman)
Daniel Stone95d48a22017-04-04 17:54:30 +01001115 fb = drm_output_render_pixman(output, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001116 else
Daniel Stone95d48a22017-04-04 17:54:30 +01001117 fb = drm_output_render_gl(output, damage);
1118
1119 if (!fb)
1120 return;
1121 output->fb_pending = fb;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001122
Giulio Camuffo954f1832014-10-11 18:27:30 +03001123 pixman_region32_subtract(&c->primary_plane.damage,
1124 &c->primary_plane.damage, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001125}
1126
1127static void
Richard Hughese7299962013-05-01 21:52:12 +01001128drm_output_set_gamma(struct weston_output *output_base,
1129 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
1130{
1131 int rc;
Armin Krezović545dba62016-08-05 15:54:18 +02001132 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001133 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +02001134 to_drm_backend(output->base.compositor);
Richard Hughese7299962013-05-01 21:52:12 +01001135
1136 /* check */
1137 if (output_base->gamma_size != size)
1138 return;
1139 if (!output->original_crtc)
1140 return;
1141
Giulio Camuffo954f1832014-10-11 18:27:30 +03001142 rc = drmModeCrtcSetGamma(backend->drm.fd,
Richard Hughese7299962013-05-01 21:52:12 +01001143 output->crtc_id,
1144 size, r, g, b);
1145 if (rc)
1146 weston_log("set gamma failed: %m\n");
1147}
1148
Bryce Harringtonada4f072015-06-30 13:25:46 -07001149/* Determine the type of vblank synchronization to use for the output.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001150 *
Bryce Harringtonada4f072015-06-30 13:25:46 -07001151 * The pipe parameter indicates which CRTC is in use. Knowing this, we
1152 * can determine which vblank sequence type to use for it. Traditional
1153 * cards had only two CRTCs, with CRTC 0 using no special flags, and
1154 * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
1155 * parameter indicates this.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001156 *
Bryce Harringtonada4f072015-06-30 13:25:46 -07001157 * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
1158 * 0-31. If this is non-zero it indicates we're dealing with a
1159 * multi-gpu situation and we need to calculate the vblank sync
1160 * using DRM_BLANK_HIGH_CRTC_MASK.
1161 */
Pekka Paalanenc8a1ff02015-07-02 15:06:08 +03001162static unsigned int
1163drm_waitvblank_pipe(struct drm_output *output)
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +02001164{
1165 if (output->pipe > 1)
1166 return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
1167 DRM_VBLANK_HIGH_CRTC_MASK;
1168 else if (output->pipe > 0)
1169 return DRM_VBLANK_SECONDARY;
1170 else
1171 return 0;
1172}
1173
David Herrmann1edf44c2013-10-22 17:11:26 +02001174static int
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001175drm_output_repaint(struct weston_output *output_base,
Daniel Stoneb1f166d2017-03-01 11:34:10 +00001176 pixman_region32_t *damage,
1177 void *repaint_data)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001178{
Armin Krezović545dba62016-08-05 15:54:18 +02001179 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001180 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +02001181 to_drm_backend(output->base.compositor);
Daniel Stone085d2b92015-05-21 00:00:57 +01001182 struct drm_plane *p;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001183 struct drm_mode *mode;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001184 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001185
Armin Krezović08368132016-09-30 14:11:05 +02001186 if (output->disable_pending || output->destroy_pending)
David Herrmann1edf44c2013-10-22 17:11:26 +02001187 return -1;
Xiong Zhangabd5d472013-10-11 14:43:07 +08001188
Daniel Stonef30a18c2017-04-04 17:54:31 +01001189 assert(!output->fb_last);
1190
Derek Foreman2cd87fe2017-04-13 13:48:48 -05001191 /* If disable_planes is set then assign_planes() wasn't
1192 * called for this render, so we could still have a stale
1193 * cursor plane set up.
1194 */
1195 if (output->base.disable_planes) {
1196 output->cursor_view = NULL;
1197 output->cursor_plane.x = INT32_MIN;
1198 output->cursor_plane.y = INT32_MIN;
1199 }
1200
Daniel Stone4e84f7d2017-04-04 17:54:29 +01001201 drm_output_render(output, damage);
Daniel Stone5bb8f582017-04-04 17:54:28 +01001202 if (!output->fb_pending)
David Herrmann1edf44c2013-10-22 17:11:26 +02001203 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001204
Hardeningff39efa2013-09-18 23:56:35 +02001205 mode = container_of(output->base.current_mode, struct drm_mode, base);
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03001206 if (output->state_invalid || !output->fb_current ||
Daniel Stone5bb8f582017-04-04 17:54:28 +01001207 output->fb_current->stride != output->fb_pending->stride) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001208 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Daniel Stone5bb8f582017-04-04 17:54:28 +01001209 output->fb_pending->fb_id, 0, 0,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001210 &output->connector_id, 1,
1211 &mode->mode_info);
1212 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +02001213 weston_log("set mode failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +02001214 goto err_pageflip;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001215 }
Ander Conselvan de Oliveira722a2d52013-06-04 16:24:05 +03001216 output_base->set_dpms(output_base, WESTON_DPMS_ON);
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03001217
1218 output->state_invalid = false;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001219 }
1220
Giulio Camuffo954f1832014-10-11 18:27:30 +03001221 if (drmModePageFlip(backend->drm.fd, output->crtc_id,
Daniel Stone5bb8f582017-04-04 17:54:28 +01001222 output->fb_pending->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -05001223 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001224 weston_log("queueing pageflip failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +02001225 goto err_pageflip;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -05001226 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001227
Daniel Stonef30a18c2017-04-04 17:54:31 +01001228 output->fb_last = output->fb_current;
1229 output->fb_current = output->fb_pending;
1230 output->fb_pending = NULL;
1231
Daniel Stone205c0a02017-04-04 17:54:33 +01001232 assert(!output->page_flip_pending);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001233 output->page_flip_pending = 1;
1234
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001235 if (output->pageflip_timer)
1236 wl_event_source_timer_update(output->pageflip_timer,
1237 backend->pageflip_timeout);
1238
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001239 drm_output_set_cursor(output);
1240
Jesse Barnes58ef3792012-02-23 09:45:49 -05001241 /*
1242 * Now, update all the sprite surfaces
1243 */
Daniel Stone085d2b92015-05-21 00:00:57 +01001244 wl_list_for_each(p, &backend->plane_list, link) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001245 uint32_t flags = 0, fb_id = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001246 drmVBlank vbl = {
1247 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
1248 .request.sequence = 1,
1249 };
1250
Daniel Stone085d2b92015-05-21 00:00:57 +01001251 if (p->type != WDRM_PLANE_TYPE_OVERLAY)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001252 continue;
1253
Daniel Stone085d2b92015-05-21 00:00:57 +01001254 if ((!p->fb_current && !p->fb_pending) ||
1255 !drm_plane_crtc_supported(output, p))
1256 continue;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001257
Daniel Stone085d2b92015-05-21 00:00:57 +01001258 if (p->fb_pending && !backend->sprites_hidden)
1259 fb_id = p->fb_pending->fb_id;
1260
1261 ret = drmModeSetPlane(backend->drm.fd, p->plane_id,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001262 output->crtc_id, fb_id, flags,
Daniel Stone085d2b92015-05-21 00:00:57 +01001263 p->dest_x, p->dest_y,
1264 p->dest_w, p->dest_h,
1265 p->src_x, p->src_y,
1266 p->src_w, p->src_h);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001267 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +02001268 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05001269 ret, strerror(errno));
1270
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +02001271 vbl.request.type |= drm_waitvblank_pipe(output);
Rob Clark5ca1a472012-08-08 20:27:37 -05001272
Jesse Barnes58ef3792012-02-23 09:45:49 -05001273 /*
1274 * Queue a vblank signal so we know when the surface
1275 * becomes active on the display or has been replaced.
1276 */
Daniel Stone085d2b92015-05-21 00:00:57 +01001277 vbl.request.signal = (unsigned long) p;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001278 ret = drmWaitVBlank(backend->drm.fd, &vbl);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001279 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +02001280 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05001281 ret, strerror(errno));
1282 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001283
Daniel Stone085d2b92015-05-21 00:00:57 +01001284 p->output = output;
1285 p->fb_last = p->fb_current;
1286 p->fb_current = p->fb_pending;
1287 p->fb_pending = NULL;
Daniel Stone65d87d02017-04-04 17:54:32 +01001288 output->vblank_pending++;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001289 }
1290
David Herrmann1edf44c2013-10-22 17:11:26 +02001291 return 0;
1292
1293err_pageflip:
Kristian Høgsbergb3955b02014-01-23 16:25:06 -08001294 output->cursor_view = NULL;
Daniel Stone5bb8f582017-04-04 17:54:28 +01001295 if (output->fb_pending) {
1296 drm_fb_unref(output->fb_pending);
1297 output->fb_pending = NULL;
David Herrmann1edf44c2013-10-22 17:11:26 +02001298 }
1299
1300 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001301}
1302
1303static void
Jonas Ådahle5a12252013-04-05 23:07:11 +02001304drm_output_start_repaint_loop(struct weston_output *output_base)
1305{
Armin Krezović545dba62016-08-05 15:54:18 +02001306 struct drm_output *output = to_drm_output(output_base);
1307 struct drm_backend *backend =
1308 to_drm_backend(output_base->compositor);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001309 uint32_t fb_id;
Mario Kleinerf507ec32015-06-21 21:25:14 +02001310 struct timespec ts, tnow;
1311 struct timespec vbl2now;
1312 int64_t refresh_nsec;
1313 int ret;
1314 drmVBlank vbl = {
1315 .request.type = DRM_VBLANK_RELATIVE,
1316 .request.sequence = 0,
1317 .request.signal = 0,
1318 };
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001319
Armin Krezović08368132016-09-30 14:11:05 +02001320 if (output->disable_pending || output->destroy_pending)
Xiong Zhangabd5d472013-10-11 14:43:07 +08001321 return;
1322
Daniel Stone5bb8f582017-04-04 17:54:28 +01001323 if (!output->fb_current) {
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001324 /* We can't page flip if there's no mode set */
David Herrmann3c688c52013-10-22 17:11:25 +02001325 goto finish_frame;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001326 }
1327
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03001328 /* Need to smash all state in from scratch; current timings might not
1329 * be what we want, page flip might not work, etc.
1330 */
1331 if (output->state_invalid)
1332 goto finish_frame;
1333
Mario Kleinerf507ec32015-06-21 21:25:14 +02001334 /* Try to get current msc and timestamp via instant query */
1335 vbl.request.type |= drm_waitvblank_pipe(output);
1336 ret = drmWaitVBlank(backend->drm.fd, &vbl);
1337
1338 /* Error ret or zero timestamp means failure to get valid timestamp */
1339 if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
1340 ts.tv_sec = vbl.reply.tval_sec;
1341 ts.tv_nsec = vbl.reply.tval_usec * 1000;
1342
1343 /* Valid timestamp for most recent vblank - not stale?
1344 * Stale ts could happen on Linux 3.17+, so make sure it
1345 * is not older than 1 refresh duration since now.
1346 */
1347 weston_compositor_read_presentation_clock(backend->compositor,
1348 &tnow);
1349 timespec_sub(&vbl2now, &tnow, &ts);
1350 refresh_nsec =
1351 millihz_to_nsec(output->base.current_mode->refresh);
1352 if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
1353 drm_output_update_msc(output, vbl.reply.sequence);
1354 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001355 WP_PRESENTATION_FEEDBACK_INVALID);
Mario Kleinerf507ec32015-06-21 21:25:14 +02001356 return;
1357 }
1358 }
1359
1360 /* Immediate query didn't provide valid timestamp.
1361 * Use pageflip fallback.
1362 */
Daniel Stone5bb8f582017-04-04 17:54:28 +01001363 fb_id = output->fb_current->fb_id;
Jonas Ådahle5a12252013-04-05 23:07:11 +02001364
Daniel Stone205c0a02017-04-04 17:54:33 +01001365 assert(!output->page_flip_pending);
1366 assert(!output->fb_last);
1367
Giulio Camuffo954f1832014-10-11 18:27:30 +03001368 if (drmModePageFlip(backend->drm.fd, output->crtc_id, fb_id,
Jonas Ådahle5a12252013-04-05 23:07:11 +02001369 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
1370 weston_log("queueing pageflip failed: %m\n");
David Herrmann3c688c52013-10-22 17:11:25 +02001371 goto finish_frame;
Jonas Ådahle5a12252013-04-05 23:07:11 +02001372 }
David Herrmann3c688c52013-10-22 17:11:25 +02001373
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001374 if (output->pageflip_timer)
1375 wl_event_source_timer_update(output->pageflip_timer,
1376 backend->pageflip_timeout);
1377
Daniel Stone205c0a02017-04-04 17:54:33 +01001378 output->fb_last = drm_fb_ref(output->fb_current);
1379 output->page_flip_pending = 1;
1380
David Herrmann3c688c52013-10-22 17:11:25 +02001381 return;
1382
1383finish_frame:
1384 /* if we cannot page-flip, immediately finish frame */
Daniel Stone3615ce12017-03-01 11:34:05 +00001385 weston_output_finish_frame(output_base, NULL,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001386 WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001387}
1388
1389static void
Pekka Paalanen641307c2014-09-23 22:08:47 -04001390drm_output_update_msc(struct drm_output *output, unsigned int seq)
1391{
1392 uint64_t msc_hi = output->base.msc >> 32;
1393
1394 if (seq < (output->base.msc & 0xffffffff))
1395 msc_hi++;
1396
1397 output->base.msc = (msc_hi << 32) + seq;
1398}
1399
1400static void
Jesse Barnes58ef3792012-02-23 09:45:49 -05001401vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
1402 void *data)
1403{
Daniel Stone08d4edf2017-04-04 17:54:34 +01001404 struct drm_plane *s = (struct drm_plane *)data;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001405 struct drm_output *output = s->output;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001406 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001407 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
1408 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001409
Pekka Paalanen641307c2014-09-23 22:08:47 -04001410 drm_output_update_msc(output, frame);
Daniel Stone65d87d02017-04-04 17:54:32 +01001411 output->vblank_pending--;
1412 assert(output->vblank_pending >= 0);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001413
Daniel Stonef30a18c2017-04-04 17:54:31 +01001414 assert(s->fb_last || s->fb_current);
1415 drm_fb_unref(s->fb_last);
1416 s->fb_last = NULL;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001417
Daniel Stone65d87d02017-04-04 17:54:32 +01001418 if (!output->page_flip_pending && !output->vblank_pending) {
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001419 /* Stop the pageflip timer instead of rearming it here */
1420 if (output->pageflip_timer)
1421 wl_event_source_timer_update(output->pageflip_timer, 0);
1422
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001423 ts.tv_sec = sec;
1424 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02001425 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001426 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05001427}
1428
1429static void
Armin Krezović08368132016-09-30 14:11:05 +02001430drm_output_destroy(struct weston_output *base);
Xiong Zhangabd5d472013-10-11 14:43:07 +08001431
1432static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001433page_flip_handler(int fd, unsigned int frame,
1434 unsigned int sec, unsigned int usec, void *data)
1435{
Armin Krezović545dba62016-08-05 15:54:18 +02001436 struct drm_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001437 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001438 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
1439 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
1440 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001441
Pekka Paalanen641307c2014-09-23 22:08:47 -04001442 drm_output_update_msc(output, frame);
1443
Daniel Stone205c0a02017-04-04 17:54:33 +01001444 assert(output->page_flip_pending);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001445 output->page_flip_pending = 0;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001446
Daniel Stone205c0a02017-04-04 17:54:33 +01001447 drm_fb_unref(output->fb_last);
1448 output->fb_last = NULL;
1449
Xiong Zhangabd5d472013-10-11 14:43:07 +08001450 if (output->destroy_pending)
1451 drm_output_destroy(&output->base);
Armin Krezović08368132016-09-30 14:11:05 +02001452 else if (output->disable_pending)
1453 weston_output_disable(&output->base);
Xiong Zhangabd5d472013-10-11 14:43:07 +08001454 else if (!output->vblank_pending) {
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001455 /* Stop the pageflip timer instead of rearming it here */
1456 if (output->pageflip_timer)
1457 wl_event_source_timer_update(output->pageflip_timer, 0);
1458
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001459 ts.tv_sec = sec;
1460 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02001461 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03001462
1463 /* We can't call this from frame_notify, because the output's
1464 * repaint needed flag is cleared just after that */
1465 if (output->recorder)
1466 weston_output_schedule_repaint(&output->base);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001467 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001468}
1469
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001470/**
1471 * Begin a new repaint cycle
1472 *
1473 * Called by the core compositor at the beginning of a repaint cycle.
1474 */
1475static void *
1476drm_repaint_begin(struct weston_compositor *compositor)
1477{
1478 struct drm_backend *b = to_drm_backend(compositor);
1479 struct drm_pending_state *ret;
1480
1481 ret = drm_pending_state_alloc(b);
1482 b->repaint_data = ret;
1483
1484 return ret;
1485}
1486
1487/**
1488 * Flush a repaint set
1489 *
1490 * Called by the core compositor when a repaint cycle has been completed
1491 * and should be flushed.
1492 */
1493static void
1494drm_repaint_flush(struct weston_compositor *compositor, void *repaint_data)
1495{
1496 struct drm_backend *b = to_drm_backend(compositor);
1497 struct drm_pending_state *pending_state = repaint_data;
1498
1499 drm_pending_state_free(pending_state);
1500 b->repaint_data = NULL;
1501}
1502
1503/**
1504 * Cancel a repaint set
1505 *
1506 * Called by the core compositor when a repaint has finished, so the data
1507 * held across the repaint cycle should be discarded.
1508 */
1509static void
1510drm_repaint_cancel(struct weston_compositor *compositor, void *repaint_data)
1511{
1512 struct drm_backend *b = to_drm_backend(compositor);
1513 struct drm_pending_state *pending_state = repaint_data;
1514
1515 drm_pending_state_free(pending_state);
1516 b->repaint_data = NULL;
1517}
1518
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001519static uint32_t
Daniel Stone08d4edf2017-04-04 17:54:34 +01001520drm_output_check_plane_format(struct drm_plane *p,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001521 struct weston_view *ev, struct gbm_bo *bo)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001522{
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001523 uint32_t i, format;
1524
1525 format = gbm_bo_get_format(bo);
1526
1527 if (format == GBM_FORMAT_ARGB8888) {
1528 pixman_region32_t r;
1529
Kristian Høgsberg63093a32013-03-01 14:29:16 -05001530 pixman_region32_init_rect(&r, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001531 ev->surface->width,
1532 ev->surface->height);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001533 pixman_region32_subtract(&r, &r, &ev->surface->opaque);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001534
1535 if (!pixman_region32_not_empty(&r))
1536 format = GBM_FORMAT_XRGB8888;
1537
1538 pixman_region32_fini(&r);
1539 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05001540
Daniel Stone08d4edf2017-04-04 17:54:34 +01001541 for (i = 0; i < p->count_formats; i++)
1542 if (p->formats[i] == format)
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001543 return format;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001544
1545 return 0;
1546}
1547
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001548static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001549drm_output_prepare_overlay_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001550 struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001551{
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001552 struct weston_compositor *ec = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02001553 struct drm_backend *b = to_drm_backend(ec);
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001554 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001555 struct wl_resource *buffer_resource;
Daniel Stone08d4edf2017-04-04 17:54:34 +01001556 struct drm_plane *p;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001557 struct linux_dmabuf_buffer *dmabuf;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001558 int found = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001559 struct gbm_bo *bo;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001560 pixman_region32_t dest_rect, src_rect;
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001561 pixman_box32_t *box, tbox;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001562 uint32_t format;
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001563 wl_fixed_t sx1, sy1, sx2, sy2;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001564
Giulio Camuffo954f1832014-10-11 18:27:30 +03001565 if (b->sprites_are_broken)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001566 return NULL;
Kristian Høgsberg65bec242012-03-05 19:57:35 -05001567
Daniel Stone296d7a92016-10-21 18:05:37 +01001568 /* Don't import buffers which span multiple outputs. */
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001569 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001570 return NULL;
Ander Conselvan de Oliveirad450b192012-06-26 17:09:12 +03001571
Daniel Stone296d7a92016-10-21 18:05:37 +01001572 /* We can only import GBM buffers. */
1573 if (b->gbm == NULL)
1574 return NULL;
1575
Jason Ekstranda7af7042013-10-12 22:38:11 -05001576 if (ev->surface->buffer_ref.buffer == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001577 return NULL;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001578 buffer_resource = ev->surface->buffer_ref.buffer->resource;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001579 if (wl_shm_buffer_get(buffer_resource))
Rob Clark702ffae2012-08-09 14:18:27 -05001580 return NULL;
1581
Daniel Stone296d7a92016-10-21 18:05:37 +01001582 if (viewport->buffer.transform != output->base.transform)
1583 return NULL;
1584 if (viewport->buffer.scale != output->base.current_scale)
1585 return NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001586 if (!drm_view_transform_supported(ev))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001587 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001588
Daniel Stone296d7a92016-10-21 18:05:37 +01001589 if (ev->alpha != 1.0f)
1590 return NULL;
1591
Daniel Stone085d2b92015-05-21 00:00:57 +01001592 wl_list_for_each(p, &b->plane_list, link) {
1593 if (p->type != WDRM_PLANE_TYPE_OVERLAY)
1594 continue;
1595
Daniel Stone08d4edf2017-04-04 17:54:34 +01001596 if (!drm_plane_crtc_supported(output, p))
Jesse Barnes58ef3792012-02-23 09:45:49 -05001597 continue;
1598
Daniel Stone08d4edf2017-04-04 17:54:34 +01001599 if (!p->fb_pending) {
Jesse Barnes58ef3792012-02-23 09:45:49 -05001600 found = 1;
1601 break;
1602 }
1603 }
1604
1605 /* No sprites available */
1606 if (!found)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001607 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001608
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001609 if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) {
Bryce Harringtona3582072015-08-14 12:23:13 -07001610#ifdef HAVE_GBM_FD_IMPORT
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001611 /* XXX: TODO:
1612 *
1613 * Use AddFB2 directly, do not go via GBM.
1614 * Add support for multiplanar formats.
1615 * Both require refactoring in the DRM-backend to
1616 * support a mix of gbm_bos and drmfbs.
1617 */
1618 struct gbm_import_fd_data gbm_dmabuf = {
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001619 .fd = dmabuf->attributes.fd[0],
1620 .width = dmabuf->attributes.width,
1621 .height = dmabuf->attributes.height,
1622 .stride = dmabuf->attributes.stride[0],
1623 .format = dmabuf->attributes.format
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001624 };
1625
Micah Fedkec8890122017-02-01 15:28:23 -05001626 /* XXX: TODO:
1627 *
1628 * Currently the buffer is rejected if any dmabuf attribute
1629 * flag is set. This keeps us from passing an inverted /
1630 * interlaced / bottom-first buffer (or any other type that may
1631 * be added in the future) through to an overlay. Ultimately,
1632 * these types of buffers should be handled through buffer
1633 * transforms and not as spot-checks requiring specific
1634 * knowledge. */
1635 if (dmabuf->attributes.n_planes != 1 ||
1636 dmabuf->attributes.offset[0] != 0 ||
1637 dmabuf->attributes.flags)
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001638 return NULL;
1639
1640 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf,
1641 GBM_BO_USE_SCANOUT);
Bryce Harringtona3582072015-08-14 12:23:13 -07001642#else
1643 return NULL;
1644#endif
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001645 } else {
1646 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
1647 buffer_resource, GBM_BO_USE_SCANOUT);
1648 }
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001649 if (!bo)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001650 return NULL;
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001651
Daniel Stone08d4edf2017-04-04 17:54:34 +01001652 format = drm_output_check_plane_format(p, ev, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001653 if (format == 0) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001654 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001655 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001656 }
1657
Daniel Stone08d4edf2017-04-04 17:54:34 +01001658 p->fb_pending = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT);
1659 if (!p->fb_pending) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001660 gbm_bo_destroy(bo);
1661 return NULL;
1662 }
1663
Daniel Stone08d4edf2017-04-04 17:54:34 +01001664 drm_fb_set_buffer(p->fb_pending, ev->surface->buffer_ref.buffer);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001665
Jason Ekstranda7af7042013-10-12 22:38:11 -05001666 box = pixman_region32_extents(&ev->transform.boundingbox);
Daniel Stone08d4edf2017-04-04 17:54:34 +01001667 p->base.x = box->x1;
1668 p->base.y = box->y1;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001669
Jesse Barnes58ef3792012-02-23 09:45:49 -05001670 /*
1671 * Calculate the source & dest rects properly based on actual
Derek Foreman4b1a0a12014-09-10 15:37:33 -05001672 * position (note the caller has called weston_view_update_transform()
Jesse Barnes58ef3792012-02-23 09:45:49 -05001673 * for us already).
1674 */
1675 pixman_region32_init(&dest_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001676 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001677 &output->base.region);
1678 pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001679 box = pixman_region32_extents(&dest_rect);
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001680 tbox = weston_transformed_rect(output->base.width,
1681 output->base.height,
1682 output->base.transform,
1683 output->base.current_scale,
Alexander Larssond9a7bb72013-05-22 14:41:39 +02001684 *box);
Daniel Stone08d4edf2017-04-04 17:54:34 +01001685 p->dest_x = tbox.x1;
1686 p->dest_y = tbox.y1;
1687 p->dest_w = tbox.x2 - tbox.x1;
1688 p->dest_h = tbox.y2 - tbox.y1;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001689 pixman_region32_fini(&dest_rect);
1690
1691 pixman_region32_init(&src_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001692 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001693 &output->base.region);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001694 box = pixman_region32_extents(&src_rect);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001695
Jason Ekstranda7af7042013-10-12 22:38:11 -05001696 weston_view_from_global_fixed(ev,
1697 wl_fixed_from_int(box->x1),
1698 wl_fixed_from_int(box->y1),
1699 &sx1, &sy1);
1700 weston_view_from_global_fixed(ev,
1701 wl_fixed_from_int(box->x2),
1702 wl_fixed_from_int(box->y2),
1703 &sx2, &sy2);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001704
1705 if (sx1 < 0)
1706 sx1 = 0;
1707 if (sy1 < 0)
1708 sy1 = 0;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001709 if (sx2 > wl_fixed_from_int(ev->surface->width))
1710 sx2 = wl_fixed_from_int(ev->surface->width);
1711 if (sy2 > wl_fixed_from_int(ev->surface->height))
1712 sy2 = wl_fixed_from_int(ev->surface->height);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001713
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001714 tbox.x1 = sx1;
1715 tbox.y1 = sy1;
1716 tbox.x2 = sx2;
1717 tbox.y2 = sy2;
1718
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001719 tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width),
1720 wl_fixed_from_int(ev->surface->height),
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001721 viewport->buffer.transform,
1722 viewport->buffer.scale,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001723 tbox);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001724
Daniel Stone08d4edf2017-04-04 17:54:34 +01001725 p->src_x = tbox.x1 << 8;
1726 p->src_y = tbox.y1 << 8;
1727 p->src_w = (tbox.x2 - tbox.x1) << 8;
1728 p->src_h = (tbox.y2 - tbox.y1) << 8;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001729 pixman_region32_fini(&src_rect);
1730
Daniel Stone08d4edf2017-04-04 17:54:34 +01001731 return &p->base;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001732}
1733
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001734static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001735drm_output_prepare_cursor_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001736 struct weston_view *ev)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001737{
Armin Krezović545dba62016-08-05 15:54:18 +02001738 struct drm_backend *b = to_drm_backend(output->base.compositor);
Neil Robertsf37f82c2014-05-01 18:00:41 +01001739 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Derek Foreman6c19b692015-12-03 14:07:12 -06001740 struct wl_shm_buffer *shmbuf;
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001741 float x, y;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001742
Giulio Camuffo954f1832014-10-11 18:27:30 +03001743 if (b->cursors_are_broken)
Kristian Høgsberg8a015802012-08-09 17:19:23 -04001744 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001745
1746 if (output->cursor_view)
Pekka Paalanen5580f222015-02-17 16:33:18 +02001747 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001748
1749 /* Don't import buffers which span multiple outputs. */
1750 if (ev->output_mask != (1u << output->base.id))
1751 return NULL;
1752
1753 /* We use GBM to import SHM buffers. */
1754 if (b->gbm == NULL)
1755 return NULL;
1756
Derek Foreman6c19b692015-12-03 14:07:12 -06001757 if (ev->surface->buffer_ref.buffer == NULL)
1758 return NULL;
1759 shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource);
1760 if (!shmbuf)
1761 return NULL;
1762 if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888)
1763 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001764
1765 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
1766 return NULL;
1767 if (ev->transform.enabled &&
1768 (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE))
1769 return NULL;
1770 if (viewport->buffer.scale != output->base.current_scale)
1771 return NULL;
1772 if (ev->geometry.scissor_enabled)
1773 return NULL;
1774
Derek Foreman6c19b692015-12-03 14:07:12 -06001775 if (ev->surface->width > b->cursor_width ||
Daniel Stone70d337d2015-06-16 18:42:23 +01001776 ev->surface->height > b->cursor_height)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001777 return NULL;
1778
Jason Ekstranda7af7042013-10-12 22:38:11 -05001779 output->cursor_view = ev;
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001780 weston_view_to_global_float(ev, 0, 0, &x, &y);
1781 output->cursor_plane.x = x;
1782 output->cursor_plane.y = y;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001783
1784 return &output->cursor_plane;
1785}
1786
Pekka Paalanend0ead482014-06-16 12:05:40 +03001787/**
1788 * Update the image for the current cursor surface
1789 *
1790 * @param b DRM backend structure
1791 * @param bo GBM buffer object to write into
1792 * @param ev View to use for cursor image
1793 */
1794static void
1795cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo,
1796 struct weston_view *ev)
1797{
1798 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
1799 uint32_t buf[b->cursor_width * b->cursor_height];
1800 int32_t stride;
1801 uint8_t *s;
1802 int i;
1803
1804 assert(buffer && buffer->shm_buffer);
1805 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
1806 assert(ev->surface->width <= b->cursor_width);
1807 assert(ev->surface->height <= b->cursor_height);
1808
1809 memset(buf, 0, sizeof buf);
1810 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
1811 s = wl_shm_buffer_get_data(buffer->shm_buffer);
1812
1813 wl_shm_buffer_begin_access(buffer->shm_buffer);
1814 for (i = 0; i < ev->surface->height; i++)
1815 memcpy(buf + i * b->cursor_width,
1816 s + i * stride,
1817 ev->surface->width * 4);
1818 wl_shm_buffer_end_access(buffer->shm_buffer);
1819
1820 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
1821 weston_log("failed update cursor: %m\n");
1822}
1823
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001824static void
1825drm_output_set_cursor(struct drm_output *output)
1826{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001827 struct weston_view *ev = output->cursor_view;
Armin Krezović545dba62016-08-05 15:54:18 +02001828 struct drm_backend *b = to_drm_backend(output->base.compositor);
Pekka Paalanend0ead482014-06-16 12:05:40 +03001829 EGLint handle;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001830 struct gbm_bo *bo;
Derek Foremanbe428b32015-11-24 11:39:38 -06001831 float x, y;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001832
Jason Ekstranda7af7042013-10-12 22:38:11 -05001833 if (ev == NULL) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001834 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001835 return;
1836 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001837
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001838 if (pixman_region32_not_empty(&output->cursor_plane.damage)) {
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001839 pixman_region32_fini(&output->cursor_plane.damage);
1840 pixman_region32_init(&output->cursor_plane.damage);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001841 output->current_cursor ^= 1;
Daniel Stonee4256832017-04-04 17:54:27 +01001842 bo = output->gbm_cursor_fb[output->current_cursor]->bo;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001843
Pekka Paalanend0ead482014-06-16 12:05:40 +03001844 cursor_bo_update(b, bo, ev);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001845 handle = gbm_bo_get_handle(bo).s32;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001846 if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle,
1847 b->cursor_width, b->cursor_height)) {
Pekka Paalanenae29da22012-08-06 14:57:05 +03001848 weston_log("failed to set cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001849 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001850 }
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001851 }
1852
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001853 x = (output->cursor_plane.x - output->base.x) *
1854 output->base.current_scale;
1855 y = (output->cursor_plane.y - output->base.y) *
1856 output->base.current_scale;
Pekka Paalanen7eaed402015-11-27 14:20:58 +02001857
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001858 if (drmModeMoveCursor(b->drm.fd, output->crtc_id, x, y)) {
1859 weston_log("failed to move cursor: %m\n");
1860 b->cursors_are_broken = 1;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001861 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001862}
1863
Jesse Barnes58ef3792012-02-23 09:45:49 -05001864static void
Daniel Stoneb1f166d2017-03-01 11:34:10 +00001865drm_assign_planes(struct weston_output *output_base, void *repaint_data)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001866{
Armin Krezović545dba62016-08-05 15:54:18 +02001867 struct drm_backend *b = to_drm_backend(output_base->compositor);
1868 struct drm_output *output = to_drm_output(output_base);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001869 struct weston_view *ev, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001870 pixman_region32_t overlap, surface_overlap;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001871 struct weston_plane *primary, *next_plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001872
1873 /*
1874 * Find a surface for each sprite in the output using some heuristics:
1875 * 1) size
1876 * 2) frequency of update
1877 * 3) opacity (though some hw might support alpha blending)
1878 * 4) clipping (this can be fixed with color keys)
1879 *
1880 * The idea is to save on blitting since this should save power.
1881 * If we can get a large video surface on the sprite for example,
1882 * the main display surface may not need to update at all, and
1883 * the client buffer can be used directly for the sprite surface
1884 * as we do for flipping full screen surfaces.
1885 */
1886 pixman_region32_init(&overlap);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001887 primary = &output_base->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001888
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001889 output->cursor_view = NULL;
1890 output->cursor_plane.x = INT32_MIN;
1891 output->cursor_plane.y = INT32_MIN;
1892
Giulio Camuffo954f1832014-10-11 18:27:30 +03001893 wl_list_for_each_safe(ev, next, &output_base->compositor->view_list, link) {
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001894 struct weston_surface *es = ev->surface;
1895
1896 /* Test whether this buffer can ever go into a plane:
1897 * non-shm, or small enough to be a cursor.
1898 *
1899 * Also, keep a reference when using the pixman renderer.
1900 * That makes it possible to do a seamless switch to the GL
1901 * renderer and since the pixman renderer keeps a reference
1902 * to the buffer anyway, there is no side effects.
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001903 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001904 if (b->use_pixman ||
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001905 (es->buffer_ref.buffer &&
1906 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
Derek Foreman87430472015-10-15 10:24:48 -05001907 (ev->surface->width <= b->cursor_width &&
1908 ev->surface->height <= b->cursor_height))))
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001909 es->keep_buffer = true;
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001910 else
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001911 es->keep_buffer = false;
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001912
Jesse Barnes58ef3792012-02-23 09:45:49 -05001913 pixman_region32_init(&surface_overlap);
1914 pixman_region32_intersect(&surface_overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001915 &ev->transform.boundingbox);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001916
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001917 next_plane = NULL;
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001918 if (pixman_region32_not_empty(&surface_overlap))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001919 next_plane = primary;
1920 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001921 next_plane = drm_output_prepare_cursor_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001922 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001923 next_plane = drm_output_prepare_scanout_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001924 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001925 next_plane = drm_output_prepare_overlay_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001926 if (next_plane == NULL)
1927 next_plane = primary;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001928
Jason Ekstranda7af7042013-10-12 22:38:11 -05001929 weston_view_move_to_plane(ev, next_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001930
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001931 if (next_plane == primary)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001932 pixman_region32_union(&overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001933 &ev->transform.boundingbox);
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001934
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001935 if (next_plane == primary ||
1936 next_plane == &output->cursor_plane) {
1937 /* cursor plane involves a copy */
1938 ev->psf_flags = 0;
1939 } else {
1940 /* All other planes are a direct scanout of a
1941 * single client buffer.
1942 */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001943 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001944 }
1945
Jesse Barnes58ef3792012-02-23 09:45:49 -05001946 pixman_region32_fini(&surface_overlap);
1947 }
1948 pixman_region32_fini(&overlap);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001949}
1950
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001951/**
1952 * Find the closest-matching mode for a given target
1953 *
1954 * Given a target mode, find the most suitable mode amongst the output's
1955 * current mode list to use, preferring the current mode if possible, to
1956 * avoid an expensive mode switch.
1957 *
1958 * @param output DRM output
1959 * @param target_mode Mode to attempt to match
1960 * @returns Pointer to a mode from the output's mode list
1961 */
Alex Wub7b8bda2012-04-17 17:20:48 +08001962static struct drm_mode *
1963choose_mode (struct drm_output *output, struct weston_mode *target_mode)
1964{
1965 struct drm_mode *tmp_mode = NULL, *mode;
1966
Hardeningff39efa2013-09-18 23:56:35 +02001967 if (output->base.current_mode->width == target_mode->width &&
1968 output->base.current_mode->height == target_mode->height &&
1969 (output->base.current_mode->refresh == target_mode->refresh ||
Alex Wub7b8bda2012-04-17 17:20:48 +08001970 target_mode->refresh == 0))
Hardeningff39efa2013-09-18 23:56:35 +02001971 return (struct drm_mode *)output->base.current_mode;
Alex Wub7b8bda2012-04-17 17:20:48 +08001972
1973 wl_list_for_each(mode, &output->base.mode_list, base.link) {
1974 if (mode->mode_info.hdisplay == target_mode->width &&
1975 mode->mode_info.vdisplay == target_mode->height) {
Mario Kleiner872797c2015-06-21 21:25:09 +02001976 if (mode->base.refresh == target_mode->refresh ||
1977 target_mode->refresh == 0) {
Alex Wub7b8bda2012-04-17 17:20:48 +08001978 return mode;
Daniel Stonef556ebe2015-05-21 08:28:58 +01001979 } else if (!tmp_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001980 tmp_mode = mode;
1981 }
1982 }
1983
1984 return tmp_mode;
1985}
1986
1987static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001988drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00001989static void
1990drm_output_fini_egl(struct drm_output *output);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001991static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001992drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00001993static void
1994drm_output_fini_pixman(struct drm_output *output);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001995
1996static int
Alex Wub7b8bda2012-04-17 17:20:48 +08001997drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
1998{
1999 struct drm_output *output;
2000 struct drm_mode *drm_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002001 struct drm_backend *b;
Alex Wub7b8bda2012-04-17 17:20:48 +08002002
2003 if (output_base == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002004 weston_log("output is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08002005 return -1;
2006 }
2007
2008 if (mode == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002009 weston_log("mode is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08002010 return -1;
2011 }
2012
Armin Krezović545dba62016-08-05 15:54:18 +02002013 b = to_drm_backend(output_base->compositor);
2014 output = to_drm_output(output_base);
Alex Wub7b8bda2012-04-17 17:20:48 +08002015 drm_mode = choose_mode (output, mode);
2016
2017 if (!drm_mode) {
Martin Minarik6d118362012-06-07 18:01:59 +02002018 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +08002019 return -1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02002020 }
2021
Hardeningff39efa2013-09-18 23:56:35 +02002022 if (&drm_mode->base == output->base.current_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08002023 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08002024
Hardeningff39efa2013-09-18 23:56:35 +02002025 output->base.current_mode->flags = 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08002026
Hardeningff39efa2013-09-18 23:56:35 +02002027 output->base.current_mode = &drm_mode->base;
2028 output->base.current_mode->flags =
Alex Wub7b8bda2012-04-17 17:20:48 +08002029 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
2030
Daniel Stonef30a18c2017-04-04 17:54:31 +01002031 /* XXX: This drops our current buffer too early, before we've started
2032 * displaying it. Ideally this should be much more atomic and
2033 * integrated with a full repaint cycle, rather than doing a
2034 * sledgehammer modeswitch first, and only later showing new
2035 * content.
2036 */
Daniel Stone5bb8f582017-04-04 17:54:28 +01002037 drm_fb_unref(output->fb_current);
Daniel Stonef30a18c2017-04-04 17:54:31 +01002038 assert(!output->fb_last);
2039 assert(!output->fb_pending);
2040 output->fb_last = output->fb_current = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +08002041
Giulio Camuffo954f1832014-10-11 18:27:30 +03002042 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002043 drm_output_fini_pixman(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002044 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002045 weston_log("failed to init output pixman state with "
2046 "new mode\n");
2047 return -1;
2048 }
2049 } else {
Daniel Stone3e661f72016-11-04 17:24:06 +00002050 drm_output_fini_egl(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002051 if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002052 weston_log("failed to init output egl state with "
2053 "new mode");
2054 return -1;
2055 }
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02002056 }
2057
Alex Wub7b8bda2012-04-17 17:20:48 +08002058 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08002059}
2060
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002061static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002062on_drm_input(int fd, uint32_t mask, void *data)
2063{
2064 drmEventContext evctx;
2065
2066 memset(&evctx, 0, sizeof evctx);
Emil Velikov863e66b2017-04-04 18:07:34 +01002067 evctx.version = 2;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002068 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002069 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002070 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002071
2072 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002073}
2074
2075static int
Daniel Stoneefa504f2016-12-19 16:48:20 +00002076init_kms_caps(struct drm_backend *b)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002077{
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03002078 uint64_t cap;
Daniel Stoneefa504f2016-12-19 16:48:20 +00002079 int ret;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002080 clockid_t clk_id;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002081
Daniel Stoneefa504f2016-12-19 16:48:20 +00002082 weston_log("using %s\n", b->drm.filename);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002083
Daniel Stoneefa504f2016-12-19 16:48:20 +00002084 ret = drmGetCap(b->drm.fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03002085 if (ret == 0 && cap == 1)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002086 clk_id = CLOCK_MONOTONIC;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03002087 else
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002088 clk_id = CLOCK_REALTIME;
2089
Giulio Camuffo954f1832014-10-11 18:27:30 +03002090 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002091 weston_log("Error: failed to set presentation clock %d.\n",
2092 clk_id);
2093 return -1;
2094 }
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +02002095
Daniel Stoneefa504f2016-12-19 16:48:20 +00002096 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_WIDTH, &cap);
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03002097 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002098 b->cursor_width = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03002099 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03002100 b->cursor_width = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03002101
Daniel Stoneefa504f2016-12-19 16:48:20 +00002102 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_HEIGHT, &cap);
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03002103 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002104 b->cursor_height = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03002105 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03002106 b->cursor_height = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03002107
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01002108 ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
2109 b->universal_planes = (ret == 0);
2110 weston_log("DRM: %s universal planes\n",
2111 b->universal_planes ? "supports" : "does not support");
2112
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02002113 return 0;
2114}
2115
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002116static struct gbm_device *
2117create_gbm_device(int fd)
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02002118{
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002119 struct gbm_device *gbm;
Alexandru DAMIANbe0ac5b2013-10-02 17:51:05 +01002120
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002121 gl_renderer = weston_load_module("gl-renderer.so",
2122 "gl_renderer_interface");
2123 if (!gl_renderer)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002124 return NULL;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002125
2126 /* GBM will load a dri driver, but even though they need symbols from
2127 * libglapi, in some version of Mesa they are not linked to it. Since
2128 * only the gl-renderer module links to it, the call above won't make
2129 * these symbols globally available, and loading the DRI driver fails.
2130 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
2131 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
2132
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002133 gbm = gbm_create_device(fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002134
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002135 return gbm;
2136}
2137
Bryce Harringtonc056a982015-05-19 15:25:18 -07002138/* When initializing EGL, if the preferred buffer format isn't available
Bryce Harringtonb9939982016-04-15 20:28:26 -07002139 * we may be able to substitute an ARGB format for an XRGB one.
Derek Foremanc4cfe852015-05-15 12:12:40 -05002140 *
2141 * This returns 0 if substitution isn't possible, but 0 might be a
2142 * legitimate format for other EGL platforms, so the caller is
2143 * responsible for checking for 0 before calling gl_renderer->create().
2144 *
2145 * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689
2146 * but it's entirely possible we'll see this again on other implementations.
2147 */
2148static int
2149fallback_format_for(uint32_t format)
2150{
2151 switch (format) {
2152 case GBM_FORMAT_XRGB8888:
2153 return GBM_FORMAT_ARGB8888;
2154 case GBM_FORMAT_XRGB2101010:
2155 return GBM_FORMAT_ARGB2101010;
2156 default:
2157 return 0;
2158 }
2159}
2160
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002161static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002162drm_backend_create_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002163{
Derek Foreman6d556372015-11-04 14:47:33 -06002164 EGLint format[3] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002165 b->gbm_format,
2166 fallback_format_for(b->gbm_format),
Derek Foreman6d556372015-11-04 14:47:33 -06002167 0,
Derek Foremanc4cfe852015-05-15 12:12:40 -05002168 };
Derek Foreman6d556372015-11-04 14:47:33 -06002169 int n_formats = 2;
John Kåre Alsakeref591aa2013-03-02 12:27:39 +01002170
Derek Foremanc4cfe852015-05-15 12:12:40 -05002171 if (format[1])
Derek Foreman6d556372015-11-04 14:47:33 -06002172 n_formats = 3;
Miguel A. Vicodddc6702016-05-18 17:41:07 +02002173 if (gl_renderer->display_create(b->compositor,
2174 EGL_PLATFORM_GBM_KHR,
2175 (void *)b->gbm,
Miguel A. Vico41700e32016-05-18 17:47:59 +02002176 NULL,
Miguel A. Vicodddc6702016-05-18 17:41:07 +02002177 gl_renderer->opaque_attribs,
2178 format,
2179 n_formats) < 0) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002180 return -1;
2181 }
2182
2183 return 0;
2184}
2185
2186static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002187init_egl(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002188{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002189 b->gbm = create_gbm_device(b->drm.fd);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002190
Giulio Camuffo954f1832014-10-11 18:27:30 +03002191 if (!b->gbm)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002192 return -1;
2193
Giulio Camuffo954f1832014-10-11 18:27:30 +03002194 if (drm_backend_create_gl_renderer(b) < 0) {
2195 gbm_device_destroy(b->gbm);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002196 return -1;
2197 }
2198
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002199 return 0;
2200}
2201
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002202static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002203init_pixman(struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002204{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002205 return pixman_renderer_init(b->compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002206}
2207
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002208/**
Pekka Paalanenec272712014-06-05 11:22:25 +03002209 * Create a drm_plane for a hardware plane
2210 *
2211 * Creates one drm_plane structure for a hardware plane, and initialises its
2212 * properties and formats.
2213 *
2214 * This function does not add the plane to the list of usable planes in Weston
2215 * itself; the caller is responsible for this.
2216 *
2217 * Call drm_plane_destroy to clean up the plane.
2218 *
2219 * @param b DRM compositor backend
2220 * @param kplane DRM plane to create
2221 */
2222static struct drm_plane *
2223drm_plane_create(struct drm_backend *b, const drmModePlane *kplane)
2224{
2225 struct drm_plane *plane;
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01002226 drmModeObjectProperties *props;
2227
2228 static struct drm_property_enum_info plane_type_enums[] = {
2229 [WDRM_PLANE_TYPE_PRIMARY] = {
2230 .name = "Primary",
2231 },
2232 [WDRM_PLANE_TYPE_OVERLAY] = {
2233 .name = "Overlay",
2234 },
2235 [WDRM_PLANE_TYPE_CURSOR] = {
2236 .name = "Cursor",
2237 },
2238 };
2239 static const struct drm_property_info plane_props[] = {
2240 [WDRM_PLANE_TYPE] = {
2241 .name = "type",
2242 .enum_values = plane_type_enums,
2243 .num_enum_values = WDRM_PLANE_TYPE__COUNT,
2244 },
2245 };
Pekka Paalanenec272712014-06-05 11:22:25 +03002246
2247 plane = zalloc(sizeof(*plane) + ((sizeof(uint32_t)) *
2248 kplane->count_formats));
2249 if (!plane) {
2250 weston_log("%s: out of memory\n", __func__);
2251 return NULL;
2252 }
2253
2254 plane->backend = b;
2255 plane->possible_crtcs = kplane->possible_crtcs;
2256 plane->plane_id = kplane->plane_id;
2257 plane->count_formats = kplane->count_formats;
2258 memcpy(plane->formats, kplane->formats,
2259 kplane->count_formats * sizeof(kplane->formats[0]));
2260
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01002261 props = drmModeObjectGetProperties(b->drm.fd, kplane->plane_id,
2262 DRM_MODE_OBJECT_PLANE);
2263 if (!props) {
2264 weston_log("couldn't get plane properties\n");
2265 free(plane);
2266 return NULL;
2267 }
2268 drm_property_info_populate(b, plane_props, plane->props,
2269 WDRM_PLANE__COUNT, props);
2270 plane->type =
2271 drm_property_get_value(&plane->props[WDRM_PLANE_TYPE],
2272 props,
2273 WDRM_PLANE_TYPE_OVERLAY);
2274 drmModeFreeObjectProperties(props);
2275
Pekka Paalanenec272712014-06-05 11:22:25 +03002276 weston_plane_init(&plane->base, b->compositor, 0, 0);
Daniel Stone085d2b92015-05-21 00:00:57 +01002277 wl_list_insert(&b->plane_list, &plane->link);
Pekka Paalanenec272712014-06-05 11:22:25 +03002278
2279 return plane;
2280}
2281
2282/**
2283 * Destroy one DRM plane
2284 *
2285 * Destroy a DRM plane, removing it from screen and releasing its retained
2286 * buffers in the process. The counterpart to drm_plane_create.
2287 *
2288 * @param plane Plane to deallocate (will be freed)
2289 */
2290static void
2291drm_plane_destroy(struct drm_plane *plane)
2292{
2293 drmModeSetPlane(plane->backend->drm.fd, plane->plane_id, 0, 0, 0,
2294 0, 0, 0, 0, 0, 0, 0, 0);
2295 assert(!plane->fb_last);
2296 assert(!plane->fb_pending);
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01002297 drm_property_info_free(plane->props, WDRM_PLANE__COUNT);
Pekka Paalanenec272712014-06-05 11:22:25 +03002298 drm_fb_unref(plane->fb_current);
2299 weston_plane_release(&plane->base);
2300 wl_list_remove(&plane->link);
2301 free(plane);
2302}
2303
2304/**
2305 * Initialise sprites (overlay planes)
2306 *
2307 * Walk the list of provided DRM planes, and add overlay planes.
2308 *
2309 * Call destroy_sprites to free these planes.
2310 *
2311 * @param b DRM compositor backend
2312 */
2313static void
2314create_sprites(struct drm_backend *b)
2315{
2316 drmModePlaneRes *kplane_res;
2317 drmModePlane *kplane;
2318 struct drm_plane *drm_plane;
2319 uint32_t i;
2320
2321 kplane_res = drmModeGetPlaneResources(b->drm.fd);
2322 if (!kplane_res) {
2323 weston_log("failed to get plane resources: %s\n",
2324 strerror(errno));
2325 return;
2326 }
2327
2328 for (i = 0; i < kplane_res->count_planes; i++) {
2329 kplane = drmModeGetPlane(b->drm.fd, kplane_res->planes[i]);
2330 if (!kplane)
2331 continue;
2332
2333 drm_plane = drm_plane_create(b, kplane);
2334 drmModeFreePlane(kplane);
2335 if (!drm_plane)
2336 continue;
2337
Daniel Stone085d2b92015-05-21 00:00:57 +01002338 if (drm_plane->type == WDRM_PLANE_TYPE_OVERLAY)
2339 weston_compositor_stack_plane(b->compositor,
2340 &drm_plane->base,
2341 &b->compositor->primary_plane);
Pekka Paalanenec272712014-06-05 11:22:25 +03002342 }
2343
2344 drmModeFreePlaneResources(kplane_res);
2345}
2346
2347/**
2348 * Clean up sprites (overlay planes)
2349 *
2350 * The counterpart to create_sprites.
2351 *
2352 * @param b DRM compositor backend
2353 */
2354static void
2355destroy_sprites(struct drm_backend *b)
2356{
2357 struct drm_plane *plane, *next;
2358
Daniel Stone085d2b92015-05-21 00:00:57 +01002359 wl_list_for_each_safe(plane, next, &b->plane_list, link)
Pekka Paalanenec272712014-06-05 11:22:25 +03002360 drm_plane_destroy(plane);
2361}
2362
2363/**
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002364 * Add a mode to output's mode list
2365 *
2366 * Copy the supplied DRM mode into a Weston mode structure, and add it to the
2367 * output's mode list.
2368 *
2369 * @param output DRM output to add mode to
2370 * @param info DRM mode structure to add
2371 * @returns Newly-allocated Weston/DRM mode structure
2372 */
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002373static struct drm_mode *
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002374drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002375{
2376 struct drm_mode *mode;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04002377 uint64_t refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002378
2379 mode = malloc(sizeof *mode);
2380 if (mode == NULL)
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002381 return NULL;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002382
2383 mode->base.flags = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02002384 mode->base.width = info->hdisplay;
2385 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04002386
2387 /* Calculate higher precision (mHz) refresh rate */
2388 refresh = (info->clock * 1000000LL / info->htotal +
2389 info->vtotal / 2) / info->vtotal;
2390
2391 if (info->flags & DRM_MODE_FLAG_INTERLACE)
2392 refresh *= 2;
2393 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
2394 refresh /= 2;
2395 if (info->vscan > 1)
2396 refresh /= info->vscan;
2397
2398 mode->base.refresh = refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002399 mode->mode_info = *info;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002400
2401 if (info->type & DRM_MODE_TYPE_PREFERRED)
2402 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
2403
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002404 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
2405
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002406 return mode;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002407}
2408
2409static int
2410drm_subpixel_to_wayland(int drm_value)
2411{
2412 switch (drm_value) {
2413 default:
2414 case DRM_MODE_SUBPIXEL_UNKNOWN:
2415 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
2416 case DRM_MODE_SUBPIXEL_NONE:
2417 return WL_OUTPUT_SUBPIXEL_NONE;
2418 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
2419 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
2420 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
2421 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
2422 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
2423 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
2424 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
2425 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
2426 }
2427}
2428
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002429/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002430static uint32_t
2431drm_get_backlight(struct drm_output *output)
2432{
2433 long brightness, max_brightness, norm;
2434
2435 brightness = backlight_get_brightness(output->backlight);
2436 max_brightness = backlight_get_max_brightness(output->backlight);
2437
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002438 /* convert it on a scale of 0 to 255 */
2439 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002440
2441 return (uint32_t) norm;
2442}
2443
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002444/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002445static void
2446drm_set_backlight(struct weston_output *output_base, uint32_t value)
2447{
Armin Krezović545dba62016-08-05 15:54:18 +02002448 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002449 long max_brightness, new_brightness;
2450
2451 if (!output->backlight)
2452 return;
2453
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002454 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002455 return;
2456
2457 max_brightness = backlight_get_max_brightness(output->backlight);
2458
2459 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002460 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002461
2462 backlight_set_brightness(output->backlight, new_brightness);
2463}
2464
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002465static void
2466drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
2467{
Armin Krezović545dba62016-08-05 15:54:18 +02002468 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002469 struct weston_compositor *ec = output_base->compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02002470 struct drm_backend *b = to_drm_backend(ec);
Daniel Stone02cf4662017-03-03 16:19:39 +00002471 struct drm_property_info *prop =
2472 &output->props_conn[WDRM_CONNECTOR_DPMS];
Daniel Stone36609c72015-06-18 07:49:02 +01002473 int ret;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002474
Daniel Stone02cf4662017-03-03 16:19:39 +00002475 if (!prop->prop_id)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002476 return;
2477
Daniel Stone36609c72015-06-18 07:49:02 +01002478 ret = drmModeConnectorSetProperty(b->drm.fd, output->connector_id,
Daniel Stone02cf4662017-03-03 16:19:39 +00002479 prop->prop_id, level);
Daniel Stone36609c72015-06-18 07:49:02 +01002480 if (ret) {
2481 weston_log("DRM: DPMS: failed property set for %s\n",
2482 output->base.name);
2483 return;
2484 }
2485
2486 output->dpms = level;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002487}
2488
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002489static const char * const connector_type_names[] = {
Pekka Paalanen89c49b32015-08-19 15:25:57 +03002490 [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
2491 [DRM_MODE_CONNECTOR_VGA] = "VGA",
2492 [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
2493 [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
2494 [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
2495 [DRM_MODE_CONNECTOR_Composite] = "Composite",
2496 [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
2497 [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
2498 [DRM_MODE_CONNECTOR_Component] = "Component",
2499 [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
2500 [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
2501 [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
2502 [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
2503 [DRM_MODE_CONNECTOR_TV] = "TV",
2504 [DRM_MODE_CONNECTOR_eDP] = "eDP",
Pekka Paalanenab81f152015-08-24 14:27:07 +03002505#ifdef DRM_MODE_CONNECTOR_DSI
Pekka Paalanen89c49b32015-08-19 15:25:57 +03002506 [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
2507 [DRM_MODE_CONNECTOR_DSI] = "DSI",
Pekka Paalanenab81f152015-08-24 14:27:07 +03002508#endif
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04002509};
2510
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03002511/** Create a name given a DRM connector
2512 *
2513 * \param con The DRM connector whose type and id form the name.
2514 * \return A newly allocate string, or NULL on error. Must be free()'d
2515 * after use.
2516 *
2517 * The name does not identify the DRM display device.
2518 */
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002519static char *
2520make_connector_name(const drmModeConnector *con)
2521{
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03002522 char *name;
Pekka Paalanen89c49b32015-08-19 15:25:57 +03002523 const char *type_name = NULL;
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03002524 int ret;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002525
2526 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
2527 type_name = connector_type_names[con->connector_type];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03002528
2529 if (!type_name)
2530 type_name = "UNNAMED";
2531
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03002532 ret = asprintf(&name, "%s-%d", type_name, con->connector_type_id);
2533 if (ret < 0)
2534 return NULL;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002535
Pekka Paalanen1f21ef12017-04-03 13:33:26 +03002536 return name;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002537}
2538
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002539static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002540find_crtc_for_connector(struct drm_backend *b,
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002541 drmModeRes *resources, drmModeConnector *connector)
2542{
2543 drmModeEncoder *encoder;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002544 int i, j;
Daniel Stone75487c22017-01-16 14:33:38 +00002545 int ret = -1;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002546
2547 for (j = 0; j < connector->count_encoders; j++) {
Daniel Stone75487c22017-01-16 14:33:38 +00002548 uint32_t possible_crtcs, encoder_id, crtc_id;
2549
Giulio Camuffo954f1832014-10-11 18:27:30 +03002550 encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002551 if (encoder == NULL) {
2552 weston_log("Failed to get encoder.\n");
Daniel Stone75487c22017-01-16 14:33:38 +00002553 continue;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002554 }
Daniel Stone75487c22017-01-16 14:33:38 +00002555 encoder_id = encoder->encoder_id;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002556 possible_crtcs = encoder->possible_crtcs;
Daniel Stone75487c22017-01-16 14:33:38 +00002557 crtc_id = encoder->crtc_id;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002558 drmModeFreeEncoder(encoder);
2559
2560 for (i = 0; i < resources->count_crtcs; i++) {
Daniel Stone72c0e1b2017-02-09 13:49:15 +00002561 if (!(possible_crtcs & (1 << i)))
2562 continue;
2563
2564 if (drm_output_find_by_crtc(b, resources->crtcs[i]))
2565 continue;
2566
Daniel Stone75487c22017-01-16 14:33:38 +00002567 /* Try to preserve the existing
2568 * CRTC -> encoder -> connector routing; it makes
2569 * initialisation faster, and also since we have a
2570 * very dumb picking algorithm, may preserve a better
2571 * choice. */
2572 if (!connector->encoder_id ||
2573 (encoder_id == connector->encoder_id &&
2574 crtc_id == resources->crtcs[i]))
2575 return i;
2576
2577 ret = i;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002578 }
2579 }
2580
Daniel Stone75487c22017-01-16 14:33:38 +00002581 return ret;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002582}
2583
Daniel Stonee4256832017-04-04 17:54:27 +01002584static void drm_output_fini_cursor_egl(struct drm_output *output)
2585{
2586 unsigned int i;
2587
2588 for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) {
2589 drm_fb_unref(output->gbm_cursor_fb[i]);
2590 output->gbm_cursor_fb[i] = NULL;
2591 }
2592}
2593
2594static int
2595drm_output_init_cursor_egl(struct drm_output *output, struct drm_backend *b)
2596{
2597 unsigned int i;
2598
2599 for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) {
2600 struct gbm_bo *bo;
2601
2602 bo = gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
2603 GBM_FORMAT_ARGB8888,
2604 GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE);
2605 if (!bo)
2606 goto err;
2607
2608 output->gbm_cursor_fb[i] =
2609 drm_fb_get_from_bo(bo, b, GBM_FORMAT_ARGB8888,
2610 BUFFER_CURSOR);
2611 if (!output->gbm_cursor_fb[i]) {
2612 gbm_bo_destroy(bo);
2613 goto err;
2614 }
2615 }
2616
2617 return 0;
2618
2619err:
2620 weston_log("cursor buffers unavailable, using gl cursors\n");
2621 b->cursors_are_broken = 1;
2622 drm_output_fini_cursor_egl(output);
2623 return -1;
2624}
2625
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002626/* Init output state that depends on gl or gbm */
2627static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002628drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002629{
Derek Foremanc4cfe852015-05-15 12:12:40 -05002630 EGLint format[2] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002631 output->gbm_format,
2632 fallback_format_for(output->gbm_format),
Derek Foremanc4cfe852015-05-15 12:12:40 -05002633 };
Daniel Stonee4256832017-04-04 17:54:27 +01002634 int n_formats = 1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02002635
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002636 output->gbm_surface = gbm_surface_create(b->gbm,
Hardeningff39efa2013-09-18 23:56:35 +02002637 output->base.current_mode->width,
2638 output->base.current_mode->height,
Derek Foremanc4cfe852015-05-15 12:12:40 -05002639 format[0],
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002640 GBM_BO_USE_SCANOUT |
2641 GBM_BO_USE_RENDERING);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002642 if (!output->gbm_surface) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002643 weston_log("failed to create gbm surface\n");
2644 return -1;
2645 }
2646
Derek Foremanc4cfe852015-05-15 12:12:40 -05002647 if (format[1])
2648 n_formats = 2;
Miguel A. Vicoc095cde2016-05-18 17:43:00 +02002649 if (gl_renderer->output_window_create(&output->base,
2650 (EGLNativeWindowType)output->gbm_surface,
2651 output->gbm_surface,
2652 gl_renderer->opaque_attribs,
2653 format,
2654 n_formats) < 0) {
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02002655 weston_log("failed to create gl renderer output state\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002656 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002657 return -1;
2658 }
2659
Daniel Stonee4256832017-04-04 17:54:27 +01002660 drm_output_init_cursor_egl(output, b);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002661
2662 return 0;
2663}
2664
Daniel Stone3e661f72016-11-04 17:24:06 +00002665static void
2666drm_output_fini_egl(struct drm_output *output)
2667{
2668 gl_renderer->output_destroy(&output->base);
2669 gbm_surface_destroy(output->gbm_surface);
Daniel Stonee4256832017-04-04 17:54:27 +01002670 drm_output_fini_cursor_egl(output);
Daniel Stone3e661f72016-11-04 17:24:06 +00002671}
2672
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002673static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002674drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002675{
Hardeningff39efa2013-09-18 23:56:35 +02002676 int w = output->base.current_mode->width;
2677 int h = output->base.current_mode->height;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002678 uint32_t format = output->gbm_format;
2679 uint32_t pixman_format;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002680 unsigned int i;
2681
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002682 switch (format) {
2683 case GBM_FORMAT_XRGB8888:
2684 pixman_format = PIXMAN_x8r8g8b8;
2685 break;
2686 case GBM_FORMAT_RGB565:
2687 pixman_format = PIXMAN_r5g6b5;
2688 break;
2689 default:
2690 weston_log("Unsupported pixman format 0x%x\n", format);
2691 return -1;
2692 }
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002693
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002694 /* FIXME error checking */
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002695 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002696 output->dumb[i] = drm_fb_create_dumb(b, w, h, format);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002697 if (!output->dumb[i])
2698 goto err;
2699
2700 output->image[i] =
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002701 pixman_image_create_bits(pixman_format, w, h,
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002702 output->dumb[i]->map,
2703 output->dumb[i]->stride);
2704 if (!output->image[i])
2705 goto err;
2706 }
2707
2708 if (pixman_renderer_output_create(&output->base) < 0)
2709 goto err;
2710
2711 pixman_region32_init_rect(&output->previous_damage,
Alexander Larsson0b135062013-05-28 16:23:36 +02002712 output->base.x, output->base.y, output->base.width, output->base.height);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002713
2714 return 0;
2715
2716err:
2717 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
2718 if (output->dumb[i])
Daniel Stone6e7a9612017-04-04 17:54:26 +01002719 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002720 if (output->image[i])
2721 pixman_image_unref(output->image[i]);
2722
2723 output->dumb[i] = NULL;
2724 output->image[i] = NULL;
2725 }
2726
2727 return -1;
2728}
2729
2730static void
2731drm_output_fini_pixman(struct drm_output *output)
2732{
2733 unsigned int i;
2734
2735 pixman_renderer_output_destroy(&output->base);
2736 pixman_region32_fini(&output->previous_damage);
2737
2738 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002739 pixman_image_unref(output->image[i]);
Daniel Stone6e7a9612017-04-04 17:54:26 +01002740 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002741 output->dumb[i] = NULL;
2742 output->image[i] = NULL;
2743 }
2744}
2745
Richard Hughes2b2092a2013-04-24 14:58:02 +01002746static void
2747edid_parse_string(const uint8_t *data, char text[])
2748{
2749 int i;
2750 int replaced = 0;
2751
2752 /* this is always 12 bytes, but we can't guarantee it's null
2753 * terminated or not junk. */
2754 strncpy(text, (const char *) data, 12);
2755
Bryce Harrington9c7de162015-08-28 13:04:26 -07002756 /* guarantee our new string is null-terminated */
2757 text[12] = '\0';
2758
Richard Hughes2b2092a2013-04-24 14:58:02 +01002759 /* remove insane chars */
2760 for (i = 0; text[i] != '\0'; i++) {
2761 if (text[i] == '\n' ||
2762 text[i] == '\r') {
2763 text[i] = '\0';
2764 break;
2765 }
2766 }
2767
2768 /* ensure string is printable */
2769 for (i = 0; text[i] != '\0'; i++) {
2770 if (!isprint(text[i])) {
2771 text[i] = '-';
2772 replaced++;
2773 }
2774 }
2775
2776 /* if the string is random junk, ignore the string */
2777 if (replaced > 4)
2778 text[0] = '\0';
2779}
2780
2781#define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
2782#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
2783#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
2784#define EDID_OFFSET_DATA_BLOCKS 0x36
2785#define EDID_OFFSET_LAST_BLOCK 0x6c
2786#define EDID_OFFSET_PNPID 0x08
2787#define EDID_OFFSET_SERIAL 0x0c
2788
2789static int
2790edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
2791{
2792 int i;
2793 uint32_t serial_number;
2794
2795 /* check header */
2796 if (length < 128)
2797 return -1;
2798 if (data[0] != 0x00 || data[1] != 0xff)
2799 return -1;
2800
2801 /* decode the PNP ID from three 5 bit words packed into 2 bytes
2802 * /--08--\/--09--\
2803 * 7654321076543210
2804 * |\---/\---/\---/
2805 * R C1 C2 C3 */
2806 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
2807 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
2808 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
2809 edid->pnp_id[3] = '\0';
2810
2811 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
2812 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
2813 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
2814 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
2815 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
2816 if (serial_number > 0)
2817 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
2818
2819 /* parse EDID data */
2820 for (i = EDID_OFFSET_DATA_BLOCKS;
2821 i <= EDID_OFFSET_LAST_BLOCK;
2822 i += 18) {
2823 /* ignore pixel clock data */
2824 if (data[i] != 0)
2825 continue;
2826 if (data[i+2] != 0)
2827 continue;
2828
2829 /* any useful blocks? */
2830 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
2831 edid_parse_string(&data[i+5],
2832 edid->monitor_name);
2833 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
2834 edid_parse_string(&data[i+5],
2835 edid->serial_number);
2836 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
2837 edid_parse_string(&data[i+5],
2838 edid->eisa_id);
2839 }
2840 }
2841 return 0;
2842}
2843
2844static void
Daniel Stone02cf4662017-03-03 16:19:39 +00002845find_and_parse_output_edid(struct drm_backend *b, struct drm_output *output,
2846 drmModeObjectPropertiesPtr props)
Richard Hughes2b2092a2013-04-24 14:58:02 +01002847{
2848 drmModePropertyBlobPtr edid_blob = NULL;
Daniel Stone02cf4662017-03-03 16:19:39 +00002849 uint32_t blob_id;
Richard Hughes2b2092a2013-04-24 14:58:02 +01002850 int rc;
2851
Daniel Stone02cf4662017-03-03 16:19:39 +00002852 blob_id =
2853 drm_property_get_value(&output->props_conn[WDRM_CONNECTOR_EDID],
2854 props, 0);
2855 if (!blob_id)
2856 return;
2857
2858 edid_blob = drmModeGetPropertyBlob(b->drm.fd, blob_id);
Richard Hughes2b2092a2013-04-24 14:58:02 +01002859 if (!edid_blob)
2860 return;
2861
2862 rc = edid_parse(&output->edid,
2863 edid_blob->data,
2864 edid_blob->length);
2865 if (!rc) {
2866 weston_log("EDID data '%s', '%s', '%s'\n",
2867 output->edid.pnp_id,
2868 output->edid.monitor_name,
2869 output->edid.serial_number);
2870 if (output->edid.pnp_id[0] != '\0')
2871 output->base.make = output->edid.pnp_id;
2872 if (output->edid.monitor_name[0] != '\0')
2873 output->base.model = output->edid.monitor_name;
2874 if (output->edid.serial_number[0] != '\0')
2875 output->base.serial_number = output->edid.serial_number;
2876 }
2877 drmModeFreePropertyBlob(edid_blob);
2878}
2879
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002880
2881
2882static int
2883parse_modeline(const char *s, drmModeModeInfo *mode)
2884{
2885 char hsync[16];
2886 char vsync[16];
2887 float fclock;
2888
2889 mode->type = DRM_MODE_TYPE_USERDEF;
2890 mode->hskew = 0;
2891 mode->vscan = 0;
2892 mode->vrefresh = 0;
2893 mode->flags = 0;
2894
Rob Bradford307e09e2013-07-26 16:29:40 +01002895 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002896 &fclock,
2897 &mode->hdisplay,
2898 &mode->hsync_start,
2899 &mode->hsync_end,
2900 &mode->htotal,
2901 &mode->vdisplay,
2902 &mode->vsync_start,
2903 &mode->vsync_end,
2904 &mode->vtotal, hsync, vsync) != 11)
2905 return -1;
2906
2907 mode->clock = fclock * 1000;
2908 if (strcmp(hsync, "+hsync") == 0)
2909 mode->flags |= DRM_MODE_FLAG_PHSYNC;
2910 else if (strcmp(hsync, "-hsync") == 0)
2911 mode->flags |= DRM_MODE_FLAG_NHSYNC;
2912 else
2913 return -1;
2914
2915 if (strcmp(vsync, "+vsync") == 0)
2916 mode->flags |= DRM_MODE_FLAG_PVSYNC;
2917 else if (strcmp(vsync, "-vsync") == 0)
2918 mode->flags |= DRM_MODE_FLAG_NVSYNC;
2919 else
2920 return -1;
2921
Emmanuel Gil Peyrota62138b2016-05-02 22:40:11 +01002922 snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f",
2923 mode->hdisplay, mode->vdisplay, fclock);
2924
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002925 return 0;
2926}
2927
Rob Bradford66bd9f52013-06-25 18:56:42 +01002928static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002929setup_output_seat_constraint(struct drm_backend *b,
Rob Bradford66bd9f52013-06-25 18:56:42 +01002930 struct weston_output *output,
2931 const char *s)
2932{
2933 if (strcmp(s, "") != 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05002934 struct weston_pointer *pointer;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002935 struct udev_seat *seat;
2936
Giulio Camuffo954f1832014-10-11 18:27:30 +03002937 seat = udev_seat_get_named(&b->input, s);
Derek Foreman0720ea32015-07-15 13:00:35 -05002938 if (!seat)
2939 return;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002940
Derek Foreman0720ea32015-07-15 13:00:35 -05002941 seat->base.output = output;
2942
Derek Foreman1281a362015-07-31 16:55:32 -05002943 pointer = weston_seat_get_pointer(&seat->base);
2944 if (pointer)
2945 weston_pointer_clamp(pointer,
2946 &pointer->x,
2947 &pointer->y);
Rob Bradford66bd9f52013-06-25 18:56:42 +01002948 }
2949}
2950
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002951static int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002952parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format)
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002953{
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002954 int ret = 0;
2955
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002956 if (s == NULL)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002957 *gbm_format = default_value;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002958 else if (strcmp(s, "xrgb8888") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002959 *gbm_format = GBM_FORMAT_XRGB8888;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002960 else if (strcmp(s, "rgb565") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002961 *gbm_format = GBM_FORMAT_RGB565;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002962 else if (strcmp(s, "xrgb2101010") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002963 *gbm_format = GBM_FORMAT_XRGB2101010;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002964 else {
2965 weston_log("fatal: unrecognized pixel format: %s\n", s);
2966 ret = -1;
2967 }
2968
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002969 return ret;
2970}
2971
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002972/**
2973 * Choose suitable mode for an output
2974 *
2975 * Find the most suitable mode to use for initial setup (or reconfiguration on
2976 * hotplug etc) for a DRM output.
2977 *
2978 * @param output DRM output to choose mode for
2979 * @param kind Strategy and preference to use when choosing mode
2980 * @param width Desired width for this output
2981 * @param height Desired height for this output
2982 * @param current_mode Mode currently being displayed on this output
2983 * @param modeline Manually-entered mode (may be NULL)
2984 * @returns A mode from the output's mode list, or NULL if none available
2985 */
2986static struct drm_mode *
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002987drm_output_choose_initial_mode(struct drm_backend *backend,
2988 struct drm_output *output,
2989 enum weston_drm_backend_output_mode mode,
Armin Krezović08368132016-09-30 14:11:05 +02002990 const char *modeline,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002991 const drmModeModeInfo *current_mode)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002992{
2993 struct drm_mode *preferred = NULL;
2994 struct drm_mode *current = NULL;
2995 struct drm_mode *configured = NULL;
2996 struct drm_mode *best = NULL;
2997 struct drm_mode *drm_mode;
Armin Krezović08368132016-09-30 14:11:05 +02002998 drmModeModeInfo drm_modeline;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002999 int32_t width = 0;
3000 int32_t height = 0;
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01003001 uint32_t refresh = 0;
3002 int n;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003003
Armin Krezović08368132016-09-30 14:11:05 +02003004 if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) {
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01003005 n = sscanf(modeline, "%dx%d@%d", &width, &height, &refresh);
3006 if (n != 2 && n != 3) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003007 width = -1;
3008
Armin Krezović08368132016-09-30 14:11:05 +02003009 if (parse_modeline(modeline, &drm_modeline) == 0) {
3010 configured = drm_output_add_mode(output, &drm_modeline);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003011 if (!configured)
3012 return NULL;
3013 } else {
3014 weston_log("Invalid modeline \"%s\" for output %s\n",
Armin Krezović08368132016-09-30 14:11:05 +02003015 modeline, output->base.name);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003016 }
3017 }
3018 }
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003019
3020 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003021 if (width == drm_mode->base.width &&
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01003022 height == drm_mode->base.height &&
3023 (refresh == 0 || refresh == drm_mode->mode_info.vrefresh))
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003024 configured = drm_mode;
3025
comic fans7a5c5622016-03-17 14:29:27 +02003026 if (memcmp(current_mode, &drm_mode->mode_info,
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003027 sizeof *current_mode) == 0)
3028 current = drm_mode;
3029
3030 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
3031 preferred = drm_mode;
3032
3033 best = drm_mode;
3034 }
3035
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003036 if (current == NULL && current_mode->clock != 0) {
3037 current = drm_output_add_mode(output, current_mode);
3038 if (!current)
3039 return NULL;
3040 }
3041
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003042 if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003043 configured = current;
3044
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003045 if (configured)
3046 return configured;
3047
3048 if (preferred)
3049 return preferred;
3050
3051 if (current)
3052 return current;
3053
3054 if (best)
3055 return best;
3056
3057 weston_log("no available modes for %s\n", output->base.name);
3058 return NULL;
3059}
3060
Pekka Paalaneneee580b2014-06-04 16:43:06 +03003061static int
3062connector_get_current_mode(drmModeConnector *connector, int drm_fd,
3063 drmModeModeInfo *mode)
3064{
3065 drmModeEncoder *encoder;
3066 drmModeCrtc *crtc;
3067
3068 /* Get the current mode on the crtc that's currently driving
3069 * this connector. */
3070 encoder = drmModeGetEncoder(drm_fd, connector->encoder_id);
3071 memset(mode, 0, sizeof *mode);
3072 if (encoder != NULL) {
3073 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
3074 drmModeFreeEncoder(encoder);
3075 if (crtc == NULL)
3076 return -1;
3077 if (crtc->mode_valid)
3078 *mode = crtc->mode;
3079 drmModeFreeCrtc(crtc);
3080 }
3081
3082 return 0;
3083}
3084
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003085static int
Armin Krezović08368132016-09-30 14:11:05 +02003086drm_output_set_mode(struct weston_output *base,
3087 enum weston_drm_backend_output_mode mode,
3088 const char *modeline)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003089{
Armin Krezović08368132016-09-30 14:11:05 +02003090 struct drm_output *output = to_drm_output(base);
3091 struct drm_backend *b = to_drm_backend(base->compositor);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003092
Armin Krezović445b41b2016-10-09 23:48:16 +02003093 struct drm_mode *current;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003094 drmModeModeInfo crtc_mode;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003095
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003096 output->base.make = "unknown";
3097 output->base.model = "unknown";
Richard Hughes2b2092a2013-04-24 14:58:02 +01003098 output->base.serial_number = "unknown";
Matt Roper361d2ad2011-08-29 13:52:23 -07003099
Armin Krezović08368132016-09-30 14:11:05 +02003100 if (connector_get_current_mode(output->connector, b->drm.fd, &crtc_mode) < 0)
Armin Krezović445b41b2016-10-09 23:48:16 +02003101 return -1;
David Herrmann0f0d54e2011-12-08 17:05:45 +01003102
Armin Krezović08368132016-09-30 14:11:05 +02003103 current = drm_output_choose_initial_mode(b, output, mode, modeline, &crtc_mode);
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003104 if (!current)
Armin Krezović445b41b2016-10-09 23:48:16 +02003105 return -1;
Armin Krezović08368132016-09-30 14:11:05 +02003106
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003107 output->base.current_mode = &current->base;
Hardeningff39efa2013-09-18 23:56:35 +02003108 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
Wang Quanxianacb805a2012-07-30 18:09:46 -04003109
Armin Krezović08368132016-09-30 14:11:05 +02003110 /* Set native_ fields, so weston_output_mode_switch_to_native() works */
3111 output->base.native_mode = output->base.current_mode;
3112 output->base.native_scale = output->base.current_scale;
3113
3114 output->base.mm_width = output->connector->mmWidth;
3115 output->base.mm_height = output->connector->mmHeight;
3116
3117 return 0;
Armin Krezović08368132016-09-30 14:11:05 +02003118}
3119
3120static void
3121drm_output_set_gbm_format(struct weston_output *base,
3122 const char *gbm_format)
3123{
3124 struct drm_output *output = to_drm_output(base);
3125 struct drm_backend *b = to_drm_backend(base->compositor);
3126
3127 if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1)
3128 output->gbm_format = b->gbm_format;
3129}
3130
3131static void
3132drm_output_set_seat(struct weston_output *base,
3133 const char *seat)
3134{
3135 struct drm_output *output = to_drm_output(base);
3136 struct drm_backend *b = to_drm_backend(base->compositor);
3137
3138 setup_output_seat_constraint(b, &output->base,
3139 seat ? seat : "");
3140}
3141
3142static int
3143drm_output_enable(struct weston_output *base)
3144{
3145 struct drm_output *output = to_drm_output(base);
3146 struct drm_backend *b = to_drm_backend(base->compositor);
3147 struct weston_mode *m;
3148
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00003149 if (b->pageflip_timeout)
3150 drm_output_pageflip_timer_create(output);
3151
Giulio Camuffo954f1832014-10-11 18:27:30 +03003152 if (b->use_pixman) {
3153 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003154 weston_log("Failed to init output pixman state\n");
Daniel Stone02cf4662017-03-03 16:19:39 +00003155 goto err;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003156 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03003157 } else if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02003158 weston_log("Failed to init output gl state\n");
Daniel Stone02cf4662017-03-03 16:19:39 +00003159 goto err;
Kristian Høgsberg1d1e0a52012-10-21 13:29:26 -04003160 }
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04003161
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003162 if (output->backlight) {
Kristian Høgsberg220819f2013-05-23 20:29:40 -04003163 weston_log("Initialized backlight, device %s\n",
3164 output->backlight->path);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003165 output->base.set_backlight = drm_set_backlight;
3166 output->base.backlight_current = drm_get_backlight(output);
Kristian Høgsberg220819f2013-05-23 20:29:40 -04003167 } else {
3168 weston_log("Failed to initialize backlight\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003169 }
3170
Jonas Ådahle5a12252013-04-05 23:07:11 +02003171 output->base.start_repaint_loop = drm_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05003172 output->base.repaint = drm_output_repaint;
Jesse Barnes58ef3792012-02-23 09:45:49 -05003173 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003174 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08003175 output->base.switch_mode = drm_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01003176
Richard Hughese7299962013-05-01 21:52:12 +01003177 output->base.gamma_size = output->original_crtc->gamma_size;
3178 output->base.set_gamma = drm_output_set_gamma;
3179
Armin Krezović08368132016-09-30 14:11:05 +02003180 output->base.subpixel = drm_subpixel_to_wayland(output->connector->subpixel);
3181
Armin Krezović9bcf4c52017-02-08 13:55:26 +01003182 if (output->connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
3183 output->connector->connector_type == DRM_MODE_CONNECTOR_eDP)
Pekka Paalanen0079a942017-03-24 15:46:23 +02003184 output->base.connection_internal = true;
Armin Krezović08368132016-09-30 14:11:05 +02003185
Derek Foremanb0427562016-02-05 15:55:20 -06003186 weston_plane_init(&output->cursor_plane, b->compositor,
3187 INT32_MIN, INT32_MIN);
Daniel Stone4202e502016-10-24 13:26:42 +01003188 weston_plane_init(&output->scanout_plane, b->compositor, 0, 0);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003189
Giulio Camuffo954f1832014-10-11 18:27:30 +03003190 weston_compositor_stack_plane(b->compositor, &output->cursor_plane, NULL);
Daniel Stone4202e502016-10-24 13:26:42 +01003191 weston_compositor_stack_plane(b->compositor, &output->scanout_plane,
Giulio Camuffo954f1832014-10-11 18:27:30 +03003192 &b->compositor->primary_plane);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003193
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04003194 weston_log("Output %s, (connector %d, crtc %d)\n",
Richard Hughesafe690c2013-05-02 10:10:04 +01003195 output->base.name, output->connector_id, output->crtc_id);
Kristian Høgsberg061c4252012-06-28 11:28:15 -04003196 wl_list_for_each(m, &output->base.mode_list, link)
U. Artie Eoffd3ed6cb2014-01-10 10:15:17 -08003197 weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n",
Kristian Høgsberg061c4252012-06-28 11:28:15 -04003198 m->width, m->height, m->refresh / 1000.0,
3199 m->flags & WL_OUTPUT_MODE_PREFERRED ?
3200 ", preferred" : "",
3201 m->flags & WL_OUTPUT_MODE_CURRENT ?
3202 ", current" : "",
Armin Krezović08368132016-09-30 14:11:05 +02003203 output->connector->count_modes == 0 ?
Kristian Høgsberg061c4252012-06-28 11:28:15 -04003204 ", built-in" : "");
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003205
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03003206 output->state_invalid = true;
3207
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003208 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01003209
Daniel Stone02cf4662017-03-03 16:19:39 +00003210err:
David Herrmann0f0d54e2011-12-08 17:05:45 +01003211 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003212}
3213
Jesse Barnes58ef3792012-02-23 09:45:49 -05003214static void
Armin Krezović08368132016-09-30 14:11:05 +02003215drm_output_deinit(struct weston_output *base)
3216{
3217 struct drm_output *output = to_drm_output(base);
3218 struct drm_backend *b = to_drm_backend(base->compositor);
3219
Daniel Stonef30a18c2017-04-04 17:54:31 +01003220 /* output->fb_last and output->fb_pending must not be set here;
Daniel Stone6e7a9612017-04-04 17:54:26 +01003221 * destroy_pending/disable_pending exist to guarantee exactly this. */
Daniel Stonef30a18c2017-04-04 17:54:31 +01003222 assert(!output->fb_last);
Daniel Stone5bb8f582017-04-04 17:54:28 +01003223 assert(!output->fb_pending);
3224 drm_fb_unref(output->fb_current);
3225 output->fb_current = NULL;
Daniel Stone6e7a9612017-04-04 17:54:26 +01003226
Daniel Stone3e661f72016-11-04 17:24:06 +00003227 if (b->use_pixman)
Armin Krezović08368132016-09-30 14:11:05 +02003228 drm_output_fini_pixman(output);
Daniel Stone3e661f72016-11-04 17:24:06 +00003229 else
3230 drm_output_fini_egl(output);
Armin Krezović08368132016-09-30 14:11:05 +02003231
Daniel Stone4202e502016-10-24 13:26:42 +01003232 weston_plane_release(&output->scanout_plane);
Armin Krezović08368132016-09-30 14:11:05 +02003233 weston_plane_release(&output->cursor_plane);
3234
Armin Krezović08368132016-09-30 14:11:05 +02003235 /* Turn off hardware cursor */
3236 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
3237}
3238
3239static void
3240drm_output_destroy(struct weston_output *base)
3241{
3242 struct drm_output *output = to_drm_output(base);
3243 struct drm_backend *b = to_drm_backend(base->compositor);
Armin Krezović445b41b2016-10-09 23:48:16 +02003244 struct drm_mode *drm_mode, *next;
Armin Krezović08368132016-09-30 14:11:05 +02003245 drmModeCrtcPtr origcrtc = output->original_crtc;
3246
3247 if (output->page_flip_pending) {
3248 output->destroy_pending = 1;
3249 weston_log("destroy output while page flip pending\n");
3250 return;
3251 }
3252
3253 if (output->base.enabled)
3254 drm_output_deinit(&output->base);
3255
Armin Krezović445b41b2016-10-09 23:48:16 +02003256 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
3257 base.link) {
3258 wl_list_remove(&drm_mode->base.link);
3259 free(drm_mode);
3260 }
3261
Armin Krezović08368132016-09-30 14:11:05 +02003262 if (origcrtc) {
3263 /* Restore original CRTC state */
3264 drmModeSetCrtc(b->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
3265 origcrtc->x, origcrtc->y,
3266 &output->connector_id, 1, &origcrtc->mode);
3267 drmModeFreeCrtc(origcrtc);
3268 }
3269
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00003270 if (output->pageflip_timer)
3271 wl_event_source_remove(output->pageflip_timer);
3272
Armin Krezović08368132016-09-30 14:11:05 +02003273 weston_output_destroy(&output->base);
3274
Daniel Stone02cf4662017-03-03 16:19:39 +00003275 drm_property_info_free(output->props_conn, WDRM_CONNECTOR__COUNT);
3276
Armin Krezović08368132016-09-30 14:11:05 +02003277 drmModeFreeConnector(output->connector);
3278
3279 if (output->backlight)
3280 backlight_destroy(output->backlight);
3281
Armin Krezović08368132016-09-30 14:11:05 +02003282 free(output);
3283}
3284
3285static int
3286drm_output_disable(struct weston_output *base)
3287{
3288 struct drm_output *output = to_drm_output(base);
3289 struct drm_backend *b = to_drm_backend(base->compositor);
3290
3291 if (output->page_flip_pending) {
3292 output->disable_pending = 1;
3293 return -1;
3294 }
3295
3296 if (output->base.enabled)
3297 drm_output_deinit(&output->base);
3298
3299 output->disable_pending = 0;
3300
3301 weston_log("Disabling output %s\n", output->base.name);
3302 drmModeSetCrtc(b->drm.fd, output->crtc_id,
3303 0, 0, 0, 0, 0, NULL);
3304
3305 return 0;
3306}
3307
3308/**
3309 * Create a Weston output structure
3310 *
3311 * Given a DRM connector, create a matching drm_output structure and add it
3312 * to Weston's output list. It also takes ownership of the connector, which
3313 * is released when output is destroyed.
3314 *
3315 * @param b Weston backend structure
3316 * @param resources DRM resources for this device
3317 * @param connector DRM connector to use for this new output
3318 * @param drm_device udev device pointer
3319 * @returns 0 on success, or -1 on failure
3320 */
3321static int
3322create_output_for_connector(struct drm_backend *b,
3323 drmModeRes *resources,
3324 drmModeConnector *connector,
3325 struct udev_device *drm_device)
3326{
3327 struct drm_output *output;
Daniel Stone02cf4662017-03-03 16:19:39 +00003328 drmModeObjectPropertiesPtr props;
Armin Krezović445b41b2016-10-09 23:48:16 +02003329 struct drm_mode *drm_mode;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03003330 char *name;
Armin Krezović08368132016-09-30 14:11:05 +02003331 int i;
3332
Daniel Stone02cf4662017-03-03 16:19:39 +00003333 static const struct drm_property_info connector_props[] = {
3334 [WDRM_CONNECTOR_EDID] = { .name = "EDID" },
3335 [WDRM_CONNECTOR_DPMS] = { .name = "DPMS" },
3336 };
3337
Armin Krezović08368132016-09-30 14:11:05 +02003338 i = find_crtc_for_connector(b, resources, connector);
3339 if (i < 0) {
3340 weston_log("No usable crtc/encoder pair for connector.\n");
Armin Krezović445b41b2016-10-09 23:48:16 +02003341 goto err;
Armin Krezović08368132016-09-30 14:11:05 +02003342 }
3343
3344 output = zalloc(sizeof *output);
3345 if (output == NULL)
Armin Krezović445b41b2016-10-09 23:48:16 +02003346 goto err;
Armin Krezović08368132016-09-30 14:11:05 +02003347
3348 output->connector = connector;
3349 output->crtc_id = resources->crtcs[i];
3350 output->pipe = i;
3351 output->connector_id = connector->connector_id;
3352
3353 output->backlight = backlight_init(drm_device,
3354 connector->connector_type);
3355
Armin Krezović445b41b2016-10-09 23:48:16 +02003356 output->original_crtc = drmModeGetCrtc(b->drm.fd, output->crtc_id);
3357
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03003358 name = make_connector_name(connector);
3359 weston_output_init(&output->base, b->compositor, name);
3360 free(name);
3361
Armin Krezović08368132016-09-30 14:11:05 +02003362 output->base.enable = drm_output_enable;
3363 output->base.destroy = drm_output_destroy;
3364 output->base.disable = drm_output_disable;
Armin Krezović08368132016-09-30 14:11:05 +02003365
3366 output->destroy_pending = 0;
3367 output->disable_pending = 0;
Armin Krezović08368132016-09-30 14:11:05 +02003368
Daniel Stone02cf4662017-03-03 16:19:39 +00003369 props = drmModeObjectGetProperties(b->drm.fd, connector->connector_id,
3370 DRM_MODE_OBJECT_CONNECTOR);
3371 if (!props) {
3372 weston_log("failed to get connector properties\n");
3373 goto err;
3374 }
3375 drm_property_info_populate(b, connector_props, output->props_conn,
3376 WDRM_CONNECTOR__COUNT, props);
3377 find_and_parse_output_edid(b, output, props);
3378 drmModeFreeObjectProperties(props);
3379
Armin Krezović445b41b2016-10-09 23:48:16 +02003380 for (i = 0; i < output->connector->count_modes; i++) {
3381 drm_mode = drm_output_add_mode(output, &output->connector->modes[i]);
3382 if (!drm_mode) {
3383 drm_output_destroy(&output->base);
3384 return -1;
3385 }
3386 }
3387
Armin Krezović08368132016-09-30 14:11:05 +02003388 weston_compositor_add_pending_output(&output->base, b->compositor);
3389
3390 return 0;
Armin Krezović445b41b2016-10-09 23:48:16 +02003391
3392err:
3393 drmModeFreeConnector(connector);
3394
3395 return -1;
Armin Krezović08368132016-09-30 14:11:05 +02003396}
3397
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003398static int
Ucan, Emre (ADITG/SW1)9a200d72017-02-02 14:06:56 +00003399create_outputs(struct drm_backend *b, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003400{
3401 drmModeConnector *connector;
3402 drmModeRes *resources;
3403 int i;
3404
Giulio Camuffo954f1832014-10-11 18:27:30 +03003405 resources = drmModeGetResources(b->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003406 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02003407 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003408 return -1;
3409 }
3410
Giulio Camuffo954f1832014-10-11 18:27:30 +03003411 b->min_width = resources->min_width;
3412 b->max_width = resources->max_width;
3413 b->min_height = resources->min_height;
3414 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05003415
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003416 for (i = 0; i < resources->count_connectors; i++) {
Daniel Stone02cf4662017-03-03 16:19:39 +00003417 int ret;
3418
Giulio Camuffo954f1832014-10-11 18:27:30 +03003419 connector = drmModeGetConnector(b->drm.fd,
Benjamin Franzke117483d2011-08-30 11:38:26 +02003420 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003421 if (connector == NULL)
3422 continue;
3423
Ucan, Emre (ADITG/SW1)38ea2d22017-03-08 15:43:18 +00003424 if (connector->connection == DRM_MODE_CONNECTED) {
Daniel Stone02cf4662017-03-03 16:19:39 +00003425 ret = create_output_for_connector(b, resources,
3426 connector, drm_device);
3427 if (ret < 0)
3428 weston_log("failed to create new connector\n");
Armin Krezović08368132016-09-30 14:11:05 +02003429 } else {
3430 drmModeFreeConnector(connector);
Benjamin Franzke9eaee352011-08-02 13:03:54 +02003431 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003432 }
3433
Armin Krezović08368132016-09-30 14:11:05 +02003434 if (wl_list_empty(&b->compositor->output_list) &&
3435 wl_list_empty(&b->compositor->pending_output_list))
Martin Minarik6d118362012-06-07 18:01:59 +02003436 weston_log("No currently active connector found.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003437
3438 drmModeFreeResources(resources);
3439
3440 return 0;
3441}
3442
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003443static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03003444update_outputs(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003445{
3446 drmModeConnector *connector;
3447 drmModeRes *resources;
3448 struct drm_output *output, *next;
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003449 uint32_t *connected;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003450 int i;
3451
Giulio Camuffo954f1832014-10-11 18:27:30 +03003452 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003453 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02003454 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003455 return;
3456 }
3457
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003458 connected = calloc(resources->count_connectors, sizeof(uint32_t));
3459 if (!connected) {
3460 drmModeFreeResources(resources);
3461 return;
3462 }
3463
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003464 /* collect new connects */
3465 for (i = 0; i < resources->count_connectors; i++) {
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +00003466 uint32_t connector_id = resources->connectors[i];
Benjamin Franzke117483d2011-08-30 11:38:26 +02003467
Giulio Camuffo954f1832014-10-11 18:27:30 +03003468 connector = drmModeGetConnector(b->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01003469 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003470 continue;
3471
David Herrmann7551cff2011-12-08 17:05:43 +01003472 if (connector->connection != DRM_MODE_CONNECTED) {
3473 drmModeFreeConnector(connector);
3474 continue;
3475 }
3476
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003477 connected[i] = connector_id;
Benjamin Franzke117483d2011-08-30 11:38:26 +02003478
Daniel Stonec0ec7592017-02-09 13:58:35 +00003479 if (drm_output_find_by_connector(b, connector_id)) {
Armin Krezović08368132016-09-30 14:11:05 +02003480 drmModeFreeConnector(connector);
Daniel Stonec0ec7592017-02-09 13:58:35 +00003481 continue;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003482 }
Daniel Stonec0ec7592017-02-09 13:58:35 +00003483
3484 create_output_for_connector(b, resources,
3485 connector, drm_device);
3486 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003487 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003488
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003489 wl_list_for_each_safe(output, next, &b->compositor->output_list,
3490 base.link) {
3491 bool disconnected = true;
3492
3493 for (i = 0; i < resources->count_connectors; i++) {
3494 if (connected[i] == output->connector_id) {
3495 disconnected = false;
3496 break;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003497 }
3498 }
Armin Krezović08368132016-09-30 14:11:05 +02003499
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003500 if (!disconnected)
3501 continue;
3502
3503 weston_log("connector %d disconnected\n", output->connector_id);
3504 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003505 }
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003506
3507 wl_list_for_each_safe(output, next, &b->compositor->pending_output_list,
3508 base.link) {
3509 bool disconnected = true;
3510
3511 for (i = 0; i < resources->count_connectors; i++) {
3512 if (connected[i] == output->connector_id) {
3513 disconnected = false;
3514 break;
3515 }
3516 }
3517
3518 if (!disconnected)
3519 continue;
3520
3521 weston_log("connector %d disconnected\n", output->connector_id);
3522 drm_output_destroy(&output->base);
3523 }
3524
3525 free(connected);
3526 drmModeFreeResources(resources);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003527}
3528
3529static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003530udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003531{
David Herrmannd7488c22012-03-11 20:05:21 +01003532 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01003533 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01003534
3535 sysnum = udev_device_get_sysnum(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003536 if (!sysnum || atoi(sysnum) != b->drm.id)
David Herrmannd7488c22012-03-11 20:05:21 +01003537 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02003538
David Herrmann6ac52db2012-03-11 20:05:22 +01003539 val = udev_device_get_property_value(device, "HOTPLUG");
3540 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003541 return 0;
3542
David Herrmann6ac52db2012-03-11 20:05:22 +01003543 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003544}
3545
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003546static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003547udev_drm_event(int fd, uint32_t mask, void *data)
3548{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003549 struct drm_backend *b = data;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003550 struct udev_device *event;
3551
Giulio Camuffo954f1832014-10-11 18:27:30 +03003552 event = udev_monitor_receive_device(b->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02003553
Giulio Camuffo954f1832014-10-11 18:27:30 +03003554 if (udev_event_is_hotplug(b, event))
3555 update_outputs(b, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003556
3557 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003558
3559 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003560}
3561
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003562static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003563drm_restore(struct weston_compositor *ec)
3564{
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003565 weston_launcher_restore(ec->launcher);
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003566}
3567
3568static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003569drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003570{
Armin Krezović545dba62016-08-05 15:54:18 +02003571 struct drm_backend *b = to_drm_backend(ec);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003572
Giulio Camuffo954f1832014-10-11 18:27:30 +03003573 udev_input_destroy(&b->input);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003574
Giulio Camuffo954f1832014-10-11 18:27:30 +03003575 wl_event_source_remove(b->udev_drm_source);
3576 wl_event_source_remove(b->drm_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003577
Giulio Camuffo954f1832014-10-11 18:27:30 +03003578 destroy_sprites(b);
Kristian Høgsberg3d64a3e2013-05-10 12:36:04 -04003579
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03003580 weston_compositor_shutdown(ec);
3581
Giulio Camuffo954f1832014-10-11 18:27:30 +03003582 if (b->gbm)
3583 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003584
Pekka Paalanen2a0c6c32017-09-13 16:48:01 +03003585 udev_unref(b->udev);
3586
Giulio Camuffo954f1832014-10-11 18:27:30 +03003587 weston_launcher_destroy(ec->launcher);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003588
Giulio Camuffo954f1832014-10-11 18:27:30 +03003589 close(b->drm.fd);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003590 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003591}
3592
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003593static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003594session_notify(struct wl_listener *listener, void *data)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003595{
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003596 struct weston_compositor *compositor = data;
Armin Krezović545dba62016-08-05 15:54:18 +02003597 struct drm_backend *b = to_drm_backend(compositor);
Daniel Stone085d2b92015-05-21 00:00:57 +01003598 struct drm_plane *plane;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003599 struct drm_output *output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003600
Giulio Camuffo954f1832014-10-11 18:27:30 +03003601 if (compositor->session_active) {
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003602 weston_log("activating session\n");
Daniel Stonef33e1042016-11-05 08:10:13 +00003603 weston_compositor_wake(compositor);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003604 weston_compositor_damage_all(compositor);
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03003605
3606 wl_list_for_each(output, &compositor->output_list, base.link)
3607 output->state_invalid = true;
3608
Giulio Camuffo954f1832014-10-11 18:27:30 +03003609 udev_input_enable(&b->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003610 } else {
3611 weston_log("deactivating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03003612 udev_input_disable(&b->input);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04003613
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003614 weston_compositor_offscreen(compositor);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04003615
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05003616 /* If we have a repaint scheduled (either from a
3617 * pending pageflip or the idle handler), make sure we
3618 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003619 * vt switched away. The OFFSCREEN state will prevent
Abdur Rehman4dca0e12017-01-01 19:46:35 +05003620 * further attempts at repainting. When we switch
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05003621 * back, we schedule a repaint, which will process
3622 * pending frame callbacks. */
3623
Giulio Camuffo954f1832014-10-11 18:27:30 +03003624 wl_list_for_each(output, &compositor->output_list, base.link) {
Daniel Stone09a97e22017-03-01 11:34:06 +00003625 output->base.repaint_needed = false;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003626 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05003627 }
3628
Giulio Camuffo954f1832014-10-11 18:27:30 +03003629 output = container_of(compositor->output_list.next,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003630 struct drm_output, base.link);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05003631
Daniel Stone085d2b92015-05-21 00:00:57 +01003632 wl_list_for_each(plane, &b->plane_list, link) {
3633 if (plane->type != WDRM_PLANE_TYPE_OVERLAY)
3634 continue;
3635
Giulio Camuffo954f1832014-10-11 18:27:30 +03003636 drmModeSetPlane(b->drm.fd,
Daniel Stone085d2b92015-05-21 00:00:57 +01003637 plane->plane_id,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003638 output->crtc_id, 0, 0,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05003639 0, 0, 0, 0, 0, 0, 0, 0);
Daniel Stone085d2b92015-05-21 00:00:57 +01003640 }
3641 }
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003642}
3643
Daniel Stoneefa504f2016-12-19 16:48:20 +00003644/**
3645 * Determines whether or not a device is capable of modesetting. If successful,
3646 * sets b->drm.fd and b->drm.filename to the opened device.
3647 */
3648static bool
3649drm_device_is_kms(struct drm_backend *b, struct udev_device *device)
3650{
3651 const char *filename = udev_device_get_devnode(device);
3652 const char *sysnum = udev_device_get_sysnum(device);
3653 drmModeRes *res;
3654 int id, fd;
3655
3656 if (!filename)
3657 return false;
3658
3659 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
3660 if (fd < 0)
3661 return false;
3662
3663 res = drmModeGetResources(fd);
3664 if (!res)
3665 goto out_fd;
3666
3667 if (res->count_crtcs <= 0 || res->count_connectors <= 0 ||
3668 res->count_encoders <= 0)
3669 goto out_res;
3670
3671 if (sysnum)
3672 id = atoi(sysnum);
3673 if (!sysnum || id < 0) {
3674 weston_log("couldn't get sysnum for device %s\n", filename);
3675 goto out_res;
3676 }
3677
3678 /* We can be called successfully on multiple devices; if we have,
3679 * clean up old entries. */
3680 if (b->drm.fd >= 0)
3681 weston_launcher_close(b->compositor->launcher, b->drm.fd);
3682 free(b->drm.filename);
3683
3684 b->drm.fd = fd;
3685 b->drm.id = id;
3686 b->drm.filename = strdup(filename);
3687
Sergi Granellceb59812017-03-28 12:44:04 +02003688 drmModeFreeResources(res);
3689
Daniel Stoneefa504f2016-12-19 16:48:20 +00003690 return true;
3691
3692out_res:
3693 drmModeFreeResources(res);
3694out_fd:
3695 weston_launcher_close(b->compositor->launcher, fd);
3696 return false;
3697}
3698
David Herrmann0af066f2012-10-29 19:21:16 +01003699/*
3700 * Find primary GPU
3701 * Some systems may have multiple DRM devices attached to a single seat. This
3702 * function loops over all devices and tries to find a PCI device with the
3703 * boot_vga sysfs attribute set to 1.
3704 * If no such device is found, the first DRM device reported by udev is used.
Daniel Stoneefa504f2016-12-19 16:48:20 +00003705 * Devices are also vetted to make sure they are are capable of modesetting,
3706 * rather than pure render nodes (GPU with no display), or pure
3707 * memory-allocation devices (VGEM).
David Herrmann0af066f2012-10-29 19:21:16 +01003708 */
3709static struct udev_device*
Giulio Camuffo954f1832014-10-11 18:27:30 +03003710find_primary_gpu(struct drm_backend *b, const char *seat)
David Herrmann0af066f2012-10-29 19:21:16 +01003711{
3712 struct udev_enumerate *e;
3713 struct udev_list_entry *entry;
3714 const char *path, *device_seat, *id;
3715 struct udev_device *device, *drm_device, *pci;
3716
Giulio Camuffo954f1832014-10-11 18:27:30 +03003717 e = udev_enumerate_new(b->udev);
David Herrmann0af066f2012-10-29 19:21:16 +01003718 udev_enumerate_add_match_subsystem(e, "drm");
3719 udev_enumerate_add_match_sysname(e, "card[0-9]*");
3720
3721 udev_enumerate_scan_devices(e);
3722 drm_device = NULL;
3723 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Daniel Stoneefa504f2016-12-19 16:48:20 +00003724 bool is_boot_vga = false;
3725
David Herrmann0af066f2012-10-29 19:21:16 +01003726 path = udev_list_entry_get_name(entry);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003727 device = udev_device_new_from_syspath(b->udev, path);
David Herrmann0af066f2012-10-29 19:21:16 +01003728 if (!device)
3729 continue;
3730 device_seat = udev_device_get_property_value(device, "ID_SEAT");
3731 if (!device_seat)
3732 device_seat = default_seat;
3733 if (strcmp(device_seat, seat)) {
3734 udev_device_unref(device);
3735 continue;
3736 }
3737
3738 pci = udev_device_get_parent_with_subsystem_devtype(device,
3739 "pci", NULL);
3740 if (pci) {
3741 id = udev_device_get_sysattr_value(pci, "boot_vga");
Daniel Stoneefa504f2016-12-19 16:48:20 +00003742 if (id && !strcmp(id, "1"))
3743 is_boot_vga = true;
David Herrmann0af066f2012-10-29 19:21:16 +01003744 }
3745
Daniel Stoneefa504f2016-12-19 16:48:20 +00003746 /* If we already have a modesetting-capable device, and this
3747 * device isn't our boot-VGA device, we aren't going to use
3748 * it. */
3749 if (!is_boot_vga && drm_device) {
David Herrmann0af066f2012-10-29 19:21:16 +01003750 udev_device_unref(device);
Daniel Stoneefa504f2016-12-19 16:48:20 +00003751 continue;
3752 }
3753
3754 /* Make sure this device is actually capable of modesetting;
3755 * if this call succeeds, b->drm.{fd,filename} will be set,
3756 * and any old values freed. */
3757 if (!drm_device_is_kms(b, device)) {
3758 udev_device_unref(device);
3759 continue;
3760 }
3761
3762 /* There can only be one boot_vga device, and we try to use it
3763 * at all costs. */
3764 if (is_boot_vga) {
3765 if (drm_device)
3766 udev_device_unref(drm_device);
3767 drm_device = device;
3768 break;
3769 }
3770
3771 /* Per the (!is_boot_vga && drm_device) test above, we only
3772 * trump existing saved devices with boot-VGA devices, so if
3773 * we end up here, this must be the first device we've seen. */
3774 assert(!drm_device);
3775 drm_device = device;
David Herrmann0af066f2012-10-29 19:21:16 +01003776 }
3777
Daniel Stoneefa504f2016-12-19 16:48:20 +00003778 /* If we're returning a device to use, we must have an open FD for
3779 * it. */
3780 assert(!!drm_device == (b->drm.fd >= 0));
3781
David Herrmann0af066f2012-10-29 19:21:16 +01003782 udev_enumerate_unref(e);
3783 return drm_device;
3784}
3785
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003786static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003787planes_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
3788 void *data)
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003789{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003790 struct drm_backend *b = data;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003791
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003792 switch (key) {
3793 case KEY_C:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003794 b->cursors_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003795 break;
3796 case KEY_V:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003797 b->sprites_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003798 break;
3799 case KEY_O:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003800 b->sprites_hidden ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003801 break;
3802 default:
3803 break;
3804 }
Ander Conselvan de Oliveira180f42a2012-11-21 15:11:37 +02003805}
3806
Kristian Høgsberg0eac34a2013-08-30 14:28:22 -07003807#ifdef BUILD_VAAPI_RECORDER
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003808static void
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003809recorder_destroy(struct drm_output *output)
3810{
3811 vaapi_recorder_destroy(output->recorder);
3812 output->recorder = NULL;
3813
3814 output->base.disable_planes--;
3815
3816 wl_list_remove(&output->recorder_frame_listener.link);
3817 weston_log("[libva recorder] done\n");
3818}
3819
3820static void
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003821recorder_frame_notify(struct wl_listener *listener, void *data)
3822{
3823 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003824 struct drm_backend *b;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003825 int fd, ret;
3826
3827 output = container_of(listener, struct drm_output,
3828 recorder_frame_listener);
Armin Krezović545dba62016-08-05 15:54:18 +02003829 b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003830
3831 if (!output->recorder)
3832 return;
3833
Daniel Stone5bb8f582017-04-04 17:54:28 +01003834 ret = drmPrimeHandleToFD(b->drm.fd, output->fb_current->handle,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003835 DRM_CLOEXEC, &fd);
3836 if (ret) {
3837 weston_log("[libva recorder] "
3838 "failed to create prime fd for front buffer\n");
3839 return;
3840 }
3841
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003842 ret = vaapi_recorder_frame(output->recorder, fd,
Daniel Stone5bb8f582017-04-04 17:54:28 +01003843 output->fb_current->stride);
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003844 if (ret < 0) {
3845 weston_log("[libva recorder] aborted: %m\n");
3846 recorder_destroy(output);
3847 }
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003848}
3849
3850static void *
Giulio Camuffo954f1832014-10-11 18:27:30 +03003851create_recorder(struct drm_backend *b, int width, int height,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003852 const char *filename)
3853{
3854 int fd;
3855 drm_magic_t magic;
3856
Giulio Camuffo954f1832014-10-11 18:27:30 +03003857 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003858 if (fd < 0)
3859 return NULL;
3860
3861 drmGetMagic(fd, &magic);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003862 drmAuthMagic(b->drm.fd, magic);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003863
3864 return vaapi_recorder_create(fd, width, height, filename);
3865}
3866
3867static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003868recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003869 void *data)
3870{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003871 struct drm_backend *b = data;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003872 struct drm_output *output;
3873 int width, height;
3874
Giulio Camuffo954f1832014-10-11 18:27:30 +03003875 output = container_of(b->compositor->output_list.next,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003876 struct drm_output, base.link);
3877
3878 if (!output->recorder) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01003879 if (output->gbm_format != GBM_FORMAT_XRGB8888) {
Ander Conselvan de Oliveira2ef1cd12014-05-06 16:49:06 +03003880 weston_log("failed to start vaapi recorder: "
3881 "output format not supported\n");
3882 return;
3883 }
3884
Hardeningff39efa2013-09-18 23:56:35 +02003885 width = output->base.current_mode->width;
3886 height = output->base.current_mode->height;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003887
3888 output->recorder =
Giulio Camuffo954f1832014-10-11 18:27:30 +03003889 create_recorder(b, width, height, "capture.h264");
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003890 if (!output->recorder) {
3891 weston_log("failed to create vaapi recorder\n");
3892 return;
3893 }
3894
3895 output->base.disable_planes++;
3896
3897 output->recorder_frame_listener.notify = recorder_frame_notify;
3898 wl_signal_add(&output->base.frame_signal,
3899 &output->recorder_frame_listener);
3900
3901 weston_output_schedule_repaint(&output->base);
3902
3903 weston_log("[libva recorder] initialized\n");
3904 } else {
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003905 recorder_destroy(output);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003906 }
3907}
3908#else
3909static void
Bryce Harrington4a8a3a12015-07-16 19:12:26 -07003910recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003911 void *data)
3912{
3913 weston_log("Compiled without libva support\n");
3914}
3915#endif
3916
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003917static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03003918switch_to_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003919{
3920 struct drm_output *output;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003921 bool dmabuf_support_inited;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003922
Giulio Camuffo954f1832014-10-11 18:27:30 +03003923 if (!b->use_pixman)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003924 return;
3925
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003926 dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf;
3927
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003928 weston_log("Switching to GL renderer\n");
3929
Giulio Camuffo954f1832014-10-11 18:27:30 +03003930 b->gbm = create_gbm_device(b->drm.fd);
3931 if (!b->gbm) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003932 weston_log("Failed to create gbm device. "
3933 "Aborting renderer switch\n");
3934 return;
3935 }
3936
Giulio Camuffo954f1832014-10-11 18:27:30 +03003937 wl_list_for_each(output, &b->compositor->output_list, base.link)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003938 pixman_renderer_output_destroy(&output->base);
3939
Giulio Camuffo954f1832014-10-11 18:27:30 +03003940 b->compositor->renderer->destroy(b->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003941
Giulio Camuffo954f1832014-10-11 18:27:30 +03003942 if (drm_backend_create_gl_renderer(b) < 0) {
3943 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003944 weston_log("Failed to create GL renderer. Quitting.\n");
3945 /* FIXME: we need a function to shutdown cleanly */
3946 assert(0);
3947 }
3948
Giulio Camuffo954f1832014-10-11 18:27:30 +03003949 wl_list_for_each(output, &b->compositor->output_list, base.link)
3950 drm_output_init_egl(output, b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003951
Giulio Camuffo954f1832014-10-11 18:27:30 +03003952 b->use_pixman = 0;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003953
3954 if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) {
3955 if (linux_dmabuf_setup(b->compositor) < 0)
3956 weston_log("Error: initializing dmabuf "
3957 "support failed.\n");
3958 }
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003959}
3960
3961static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003962renderer_switch_binding(struct weston_keyboard *keyboard, uint32_t time,
3963 uint32_t key, void *data)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003964{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003965 struct drm_backend *b =
Armin Krezović545dba62016-08-05 15:54:18 +02003966 to_drm_backend(keyboard->seat->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003967
Giulio Camuffo954f1832014-10-11 18:27:30 +03003968 switch_to_gl_renderer(b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003969}
3970
Armin Krezović08368132016-09-30 14:11:05 +02003971static const struct weston_drm_output_api api = {
3972 drm_output_set_mode,
3973 drm_output_set_gbm_format,
3974 drm_output_set_seat,
3975};
3976
Giulio Camuffo954f1832014-10-11 18:27:30 +03003977static struct drm_backend *
3978drm_backend_create(struct weston_compositor *compositor,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003979 struct weston_drm_backend_config *config)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003980{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003981 struct drm_backend *b;
David Herrmann0af066f2012-10-29 19:21:16 +01003982 struct udev_device *drm_device;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003983 struct wl_event_loop *loop;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003984 const char *seat_id = default_seat;
Armin Krezović08368132016-09-30 14:11:05 +02003985 int ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003986
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003987 weston_log("initializing drm backend\n");
3988
Giulio Camuffo954f1832014-10-11 18:27:30 +03003989 b = zalloc(sizeof *b);
3990 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003991 return NULL;
Daniel Stone725c2c32012-06-22 14:04:36 +01003992
Daniel Stoneefa504f2016-12-19 16:48:20 +00003993 b->drm.fd = -1;
3994
Pekka Paalanen68583832015-05-19 09:53:16 +03003995 /*
3996 * KMS support for hardware planes cannot properly synchronize
3997 * without nuclear page flip. Without nuclear/atomic, hw plane
3998 * and cursor plane updates would either tear or cause extra
3999 * waits for vblanks which means dropping the compositor framerate
Pekka Paalanen3f32a132015-09-07 15:38:43 +03004000 * to a fraction. For cursors, it's not so bad, so they are
4001 * enabled.
Pekka Paalanen68583832015-05-19 09:53:16 +03004002 *
4003 * These can be enabled again when nuclear/atomic support lands.
4004 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03004005 b->sprites_are_broken = 1;
Giulio Camuffo954f1832014-10-11 18:27:30 +03004006 b->compositor = compositor;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004007 b->use_pixman = config->use_pixman;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00004008 b->pageflip_timeout = config->pageflip_timeout;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07004009
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004010 if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0)
4011 goto err_compositor;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07004012
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004013 if (config->seat_id)
4014 seat_id = config->seat_id;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004015
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01004016 /* Check if we run drm-backend using weston-launch */
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004017 compositor->launcher = weston_launcher_connect(compositor, config->tty,
4018 seat_id, true);
Giulio Camuffo954f1832014-10-11 18:27:30 +03004019 if (compositor->launcher == NULL) {
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01004020 weston_log("fatal: drm backend should be run "
Kristian Høgsbergb76237e2013-04-04 21:36:20 -04004021 "using weston-launch binary or as root\n");
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01004022 goto err_compositor;
4023 }
4024
Giulio Camuffo954f1832014-10-11 18:27:30 +03004025 b->udev = udev_new();
4026 if (b->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02004027 weston_log("failed to initialize udev context\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07004028 goto err_launcher;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004029 }
4030
Giulio Camuffo954f1832014-10-11 18:27:30 +03004031 b->session_listener.notify = session_notify;
4032 wl_signal_add(&compositor->session_signal, &b->session_listener);
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05004033
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004034 drm_device = find_primary_gpu(b, seat_id);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04004035 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02004036 weston_log("no drm device found\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07004037 goto err_udev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004038 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004039
Daniel Stoneefa504f2016-12-19 16:48:20 +00004040 if (init_kms_caps(b) < 0) {
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02004041 weston_log("failed to initialize kms\n");
4042 goto err_udev_dev;
4043 }
4044
Giulio Camuffo954f1832014-10-11 18:27:30 +03004045 if (b->use_pixman) {
4046 if (init_pixman(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004047 weston_log("failed to initialize pixman renderer\n");
4048 goto err_udev_dev;
4049 }
4050 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +03004051 if (init_egl(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004052 weston_log("failed to initialize egl\n");
4053 goto err_udev_dev;
4054 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004055 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05004056
Giulio Camuffo954f1832014-10-11 18:27:30 +03004057 b->base.destroy = drm_destroy;
4058 b->base.restore = drm_restore;
Daniel Stoneeedf84c2017-02-10 18:06:04 +00004059 b->base.repaint_begin = drm_repaint_begin;
4060 b->base.repaint_flush = drm_repaint_flush;
4061 b->base.repaint_cancel = drm_repaint_cancel;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02004062
Bob Ham91880f12016-01-12 10:21:47 +00004063 weston_setup_vt_switch_bindings(compositor);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04004064
Daniel Stone085d2b92015-05-21 00:00:57 +01004065 wl_list_init(&b->plane_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03004066 create_sprites(b);
Jesse Barnes58ef3792012-02-23 09:45:49 -05004067
Giulio Camuffo954f1832014-10-11 18:27:30 +03004068 if (udev_input_init(&b->input,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03004069 compositor, b->udev, seat_id,
4070 config->configure_device) < 0) {
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03004071 weston_log("failed to create input devices\n");
4072 goto err_sprite;
4073 }
4074
Ucan, Emre (ADITG/SW1)9a200d72017-02-02 14:06:56 +00004075 if (create_outputs(b, drm_device) < 0) {
Daniel Stoneefa504f2016-12-19 16:48:20 +00004076 weston_log("failed to create output for %s\n", b->drm.filename);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03004077 goto err_udev_input;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004078 }
4079
Jason Ekstrand9fc71512014-04-02 19:53:46 -05004080 /* A this point we have some idea of whether or not we have a working
4081 * cursor plane. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03004082 if (!b->cursors_are_broken)
4083 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
Jason Ekstrand9fc71512014-04-02 19:53:46 -05004084
Giulio Camuffo954f1832014-10-11 18:27:30 +03004085 loop = wl_display_get_event_loop(compositor->wl_display);
4086 b->drm_source =
4087 wl_event_loop_add_fd(loop, b->drm.fd,
4088 WL_EVENT_READABLE, on_drm_input, b);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004089
Giulio Camuffo954f1832014-10-11 18:27:30 +03004090 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
4091 if (b->udev_monitor == NULL) {
Abdur Rehman4dca0e12017-01-01 19:46:35 +05004092 weston_log("failed to initialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01004093 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004094 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03004095 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004096 "drm", NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03004097 b->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02004098 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03004099 udev_monitor_get_fd(b->udev_monitor),
4100 WL_EVENT_READABLE, udev_drm_event, b);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004101
Giulio Camuffo954f1832014-10-11 18:27:30 +03004102 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02004103 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01004104 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004105 }
4106
Daniel Stonea96b93c2012-06-22 14:04:37 +01004107 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01004108
Giulio Camuffo954f1832014-10-11 18:27:30 +03004109 weston_compositor_add_debug_binding(compositor, KEY_O,
4110 planes_binding, b);
4111 weston_compositor_add_debug_binding(compositor, KEY_C,
4112 planes_binding, b);
4113 weston_compositor_add_debug_binding(compositor, KEY_V,
4114 planes_binding, b);
4115 weston_compositor_add_debug_binding(compositor, KEY_Q,
4116 recorder_binding, b);
4117 weston_compositor_add_debug_binding(compositor, KEY_W,
4118 renderer_switch_binding, b);
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02004119
Pekka Paalanene4d231e2014-06-12 15:12:48 +03004120 if (compositor->renderer->import_dmabuf) {
4121 if (linux_dmabuf_setup(compositor) < 0)
4122 weston_log("Error: initializing dmabuf "
4123 "support failed.\n");
4124 }
4125
Giulio Camuffo954f1832014-10-11 18:27:30 +03004126 compositor->backend = &b->base;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03004127
Armin Krezović08368132016-09-30 14:11:05 +02004128 ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME,
4129 &api, sizeof(api));
4130
4131 if (ret < 0) {
4132 weston_log("Failed to register output API.\n");
4133 goto err_udev_monitor;
4134 }
4135
Giulio Camuffo954f1832014-10-11 18:27:30 +03004136 return b;
Daniel Stonea96b93c2012-06-22 14:04:37 +01004137
4138err_udev_monitor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03004139 wl_event_source_remove(b->udev_drm_source);
4140 udev_monitor_unref(b->udev_monitor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01004141err_drm_source:
Giulio Camuffo954f1832014-10-11 18:27:30 +03004142 wl_event_source_remove(b->drm_source);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03004143err_udev_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03004144 udev_input_destroy(&b->input);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04004145err_sprite:
Emmanuel Gil Peyrotb8347e32016-05-02 22:40:13 +01004146 if (b->gbm)
4147 gbm_device_destroy(b->gbm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03004148 destroy_sprites(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01004149err_udev_dev:
4150 udev_device_unref(drm_device);
Kristian Høgsberg3f495872013-09-18 23:00:17 -07004151err_launcher:
Giulio Camuffo954f1832014-10-11 18:27:30 +03004152 weston_launcher_destroy(compositor->launcher);
Daniel Stonea96b93c2012-06-22 14:04:37 +01004153err_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +03004154 udev_unref(b->udev);
Daniel Stonea96b93c2012-06-22 14:04:37 +01004155err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03004156 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03004157 free(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01004158 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004159}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004160
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004161static void
4162config_init_to_defaults(struct weston_drm_backend_config *config)
4163{
4164}
4165
Giulio Camuffo954f1832014-10-11 18:27:30 +03004166WL_EXPORT int
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01004167weston_backend_init(struct weston_compositor *compositor,
4168 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004169{
Giulio Camuffo954f1832014-10-11 18:27:30 +03004170 struct drm_backend *b;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004171 struct weston_drm_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004172
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004173 if (config_base == NULL ||
4174 config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION ||
4175 config_base->struct_size > sizeof(struct weston_drm_backend_config)) {
4176 weston_log("drm backend config structure is invalid\n");
4177 return -1;
4178 }
Benjamin Franzke117483d2011-08-30 11:38:26 +02004179
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004180 config_init_to_defaults(&config);
4181 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07004182
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004183 b = drm_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03004184 if (b == NULL)
4185 return -1;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004186
Giulio Camuffo954f1832014-10-11 18:27:30 +03004187 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004188}