blob: 11d818a80129d488c9b2e5ec13b9bf63012c86b4 [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
Jesse Barnes58ef3792012-02-23 09:45:49 -0500169 struct wl_list sprite_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
Rob Clarkab5b1e32012-08-09 13:24:45 -0500173 int cursors_are_broken;
174
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100175 bool universal_planes;
176
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200177 int use_pixman;
178
Rob Bradfordd355b802013-05-31 18:09:55 +0100179 struct udev_input input;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -0300180
Daniel Stone70d337d2015-06-16 18:42:23 +0100181 int32_t cursor_width;
182 int32_t cursor_height;
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +0000183
184 uint32_t connector;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000185 uint32_t pageflip_timeout;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400186};
187
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400188struct drm_mode {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500189 struct weston_mode base;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400190 drmModeModeInfo mode_info;
191};
192
Daniel Stonefc175a72017-04-04 17:54:22 +0100193enum drm_fb_type {
194 BUFFER_INVALID = 0, /**< never used */
195 BUFFER_CLIENT, /**< directly sourced from client */
196 BUFFER_PIXMAN_DUMB, /**< internal Pixman rendering */
197 BUFFER_GBM_SURFACE, /**< internal EGL rendering */
Daniel Stonee4256832017-04-04 17:54:27 +0100198 BUFFER_CURSOR, /**< internal cursor buffer */
Daniel Stonefc175a72017-04-04 17:54:22 +0100199};
200
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300201struct drm_fb {
Daniel Stonefc175a72017-04-04 17:54:22 +0100202 enum drm_fb_type type;
203
Daniel Stone6e7a9612017-04-04 17:54:26 +0100204 int refcnt;
205
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200206 uint32_t fb_id, stride, handle, size;
Daniel Stone0b70fa42017-04-04 17:54:23 +0100207 const struct pixel_format_info *format;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000208 int width, height;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200209 int fd;
Pekka Paalanende685b82012-12-04 15:58:12 +0200210 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200211
212 /* Used by gbm fbs */
213 struct gbm_bo *bo;
Daniel Stone05a5ac22017-04-04 17:54:25 +0100214 struct gbm_surface *gbm_surface;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200215
216 /* Used by dumb fbs */
217 void *map;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300218};
219
Richard Hughes2b2092a2013-04-24 14:58:02 +0100220struct drm_edid {
221 char eisa_id[13];
222 char monitor_name[13];
223 char pnp_id[5];
224 char serial_number[13];
225};
226
Daniel Stone08d4edf2017-04-04 17:54:34 +0100227/**
228 * A plane represents one buffer, positioned within a CRTC, and stacked
229 * relative to other planes on the same CRTC.
230 *
231 * Each CRTC has a 'primary plane', which use used to display the classic
232 * framebuffer contents, as accessed through the legacy drmModeSetCrtc
233 * call (which combines setting the CRTC's actual physical mode, and the
234 * properties of the primary plane).
235 *
236 * The cursor plane also has its own alternate legacy API.
237 *
238 * Other planes are used opportunistically to display content we do not
239 * wish to blit into the primary plane. These non-primary/cursor planes
240 * are referred to as 'sprites'.
241 */
242struct drm_plane {
243 struct wl_list link;
244
245 struct weston_plane base;
246
247 struct drm_output *output;
248 struct drm_backend *backend;
249
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100250 enum wdrm_plane_type type;
251
Daniel Stone08d4edf2017-04-04 17:54:34 +0100252 uint32_t possible_crtcs;
253 uint32_t plane_id;
254 uint32_t count_formats;
255
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100256 struct drm_property_info props[WDRM_PLANE__COUNT];
257
Daniel Stone08d4edf2017-04-04 17:54:34 +0100258 /* The last framebuffer submitted to the kernel for this plane. */
259 struct drm_fb *fb_current;
260 /* The previously-submitted framebuffer, where the hardware has not
261 * yet acknowledged display of fb_current. */
262 struct drm_fb *fb_last;
263 /* Framebuffer we are going to submit to the kernel when the current
264 * repaint is flushed. */
265 struct drm_fb *fb_pending;
266
267 int32_t src_x, src_y;
268 uint32_t src_w, src_h;
269 uint32_t dest_x, dest_y;
270 uint32_t dest_w, dest_h;
271
272 uint32_t formats[];
273};
274
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400275struct drm_output {
Armin Krezović08368132016-09-30 14:11:05 +0200276 struct weston_output base;
277 drmModeConnector *connector;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400278
Daniel Stone17339232015-11-28 12:09:47 +0000279 uint32_t crtc_id; /* object ID to pass to DRM functions */
280 int pipe; /* index of CRTC in resource array / bitmasks */
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400281 uint32_t connector_id;
Matt Roper361d2ad2011-08-29 13:52:23 -0700282 drmModeCrtcPtr original_crtc;
Richard Hughes2b2092a2013-04-24 14:58:02 +0100283 struct drm_edid edid;
Daniel Stone02cf4662017-03-03 16:19:39 +0000284
285 /* Holds the properties for the connector */
286 struct drm_property_info props_conn[WDRM_CONNECTOR__COUNT];
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200287
Daniel Stone36609c72015-06-18 07:49:02 +0100288 enum dpms_enum dpms;
Daniel Stone5bb8f582017-04-04 17:54:28 +0100289 struct backlight *backlight;
Daniel Stone36609c72015-06-18 07:49:02 +0100290
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +0300291 bool state_invalid;
292
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300293 int vblank_pending;
294 int page_flip_pending;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800295 int destroy_pending;
Armin Krezović08368132016-09-30 14:11:05 +0200296 int disable_pending;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300297
Daniel Stonee4256832017-04-04 17:54:27 +0100298 struct drm_fb *gbm_cursor_fb[2];
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400299 struct weston_plane cursor_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500300 struct weston_view *cursor_view;
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400301 int current_cursor;
Daniel Stone5bb8f582017-04-04 17:54:28 +0100302
303 struct gbm_surface *gbm_surface;
304 uint32_t gbm_format;
305
Daniel Stone4202e502016-10-24 13:26:42 +0100306 /* Plane for a fullscreen direct scanout view */
307 struct weston_plane scanout_plane;
Daniel Stonef30a18c2017-04-04 17:54:31 +0100308
309 /* The last framebuffer submitted to the kernel for this CRTC. */
310 struct drm_fb *fb_current;
311 /* The previously-submitted framebuffer, where the hardware has not
312 * yet acknowledged display of fb_current. */
313 struct drm_fb *fb_last;
314 /* Framebuffer we are going to submit to the kernel when the current
315 * repaint is flushed. */
316 struct drm_fb *fb_pending;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200317
318 struct drm_fb *dumb[2];
319 pixman_image_t *image[2];
320 int current_image;
321 pixman_region32_t previous_damage;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300322
323 struct vaapi_recorder *recorder;
324 struct wl_listener recorder_frame_listener;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000325
326 struct wl_event_source *pageflip_timer;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400327};
328
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300329static struct gl_renderer_interface *gl_renderer;
330
Kristian Høgsberg98cfea62013-02-18 16:15:53 -0500331static const char default_seat[] = "seat0";
Pekka Paalanen33156972012-08-03 13:30:30 -0400332
Armin Krezović545dba62016-08-05 15:54:18 +0200333static inline struct drm_output *
334to_drm_output(struct weston_output *base)
335{
336 return container_of(base, struct drm_output, base);
337}
338
339static inline struct drm_backend *
340to_drm_backend(struct weston_compositor *base)
341{
342 return container_of(base->backend, struct drm_backend, base);
343}
344
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000345static int
346pageflip_timeout(void *data) {
347 /*
348 * Our timer just went off, that means we're not receiving drm
349 * page flip events anymore for that output. Let's gracefully exit
350 * weston with a return value so devs can debug what's going on.
351 */
352 struct drm_output *output = data;
353 struct weston_compositor *compositor = output->base.compositor;
354
355 weston_log("Pageflip timeout reached on output %s, your "
356 "driver is probably buggy! Exiting.\n",
357 output->base.name);
358 weston_compositor_exit_with_code(compositor, EXIT_FAILURE);
359
360 return 0;
361}
362
363/* Creates the pageflip timer. Note that it isn't armed by default */
364static int
365drm_output_pageflip_timer_create(struct drm_output *output)
366{
367 struct wl_event_loop *loop = NULL;
368 struct weston_compositor *ec = output->base.compositor;
369
370 loop = wl_display_get_event_loop(ec->wl_display);
371 assert(loop);
372 output->pageflip_timer = wl_event_loop_add_timer(loop,
373 pageflip_timeout,
374 output);
375
376 if (output->pageflip_timer == NULL) {
377 weston_log("creating drm pageflip timer failed: %m\n");
378 return -1;
379 }
380
381 return 0;
382}
383
Daniel Stone02cf4662017-03-03 16:19:39 +0000384/**
385 * Get the current value of a KMS property
386 *
387 * Given a drmModeObjectGetProperties return, as well as the drm_property_info
388 * for the target property, return the current value of that property,
389 * with an optional default. If the property is a KMS enum type, the return
390 * value will be translated into the appropriate internal enum.
391 *
392 * If the property is not present, the default value will be returned.
393 *
394 * @param info Internal structure for property to look up
395 * @param props Raw KMS properties for the target object
396 * @param def Value to return if property is not found
397 */
398static uint64_t
399drm_property_get_value(struct drm_property_info *info,
400 drmModeObjectPropertiesPtr props,
401 uint64_t def)
402{
403 unsigned int i;
404
405 if (info->prop_id == 0)
406 return def;
407
408 for (i = 0; i < props->count_props; i++) {
409 unsigned int j;
410
411 if (props->props[i] != info->prop_id)
412 continue;
413
414 /* Simple (non-enum) types can return the value directly */
415 if (info->num_enum_values == 0)
416 return props->prop_values[i];
417
418 /* Map from raw value to enum value */
419 for (j = 0; j < info->num_enum_values; j++) {
420 if (!info->enum_values[j].valid)
421 continue;
422 if (info->enum_values[j].value != props->prop_values[i])
423 continue;
424
425 return j;
426 }
427
428 /* We don't have a mapping for this enum; return default. */
429 break;
430 }
431
432 return def;
433}
434
435/**
436 * Cache DRM property values
437 *
438 * Update a per-object array of drm_property_info structures, given the
439 * DRM properties of the object.
440 *
441 * Call this every time an object newly appears (note that only connectors
442 * can be hotplugged), the first time it is seen, or when its status changes
443 * in a way which invalidates the potential property values (currently, the
444 * only case for this is connector hotplug).
445 *
446 * This updates the property IDs and enum values within the drm_property_info
447 * array.
448 *
449 * DRM property enum values are dynamic at runtime; the user must query the
450 * property to find out the desired runtime value for a requested string
451 * name. Using the 'type' field on planes as an example, there is no single
452 * hardcoded constant for primary plane types; instead, the property must be
453 * queried at runtime to find the value associated with the string "Primary".
454 *
455 * This helper queries and caches the enum values, to allow us to use a set
456 * of compile-time-constant enums portably across various implementations.
457 * The values given in enum_names are searched for, and stored in the
458 * same-indexed field of the map array.
459 *
460 * @param b DRM backend object
461 * @param src DRM property info array to source from
462 * @param info DRM property info array to copy into
463 * @param num_infos Number of entries in the source array
464 * @param props DRM object properties for the object
465 */
466static void
467drm_property_info_populate(struct drm_backend *b,
468 const struct drm_property_info *src,
469 struct drm_property_info *info,
470 unsigned int num_infos,
471 drmModeObjectProperties *props)
472{
473 drmModePropertyRes *prop;
474 unsigned i, j;
475
476 for (i = 0; i < num_infos; i++) {
477 unsigned int j;
478
479 info[i].name = src[i].name;
480 info[i].prop_id = 0;
481 info[i].num_enum_values = src[i].num_enum_values;
482
483 if (src[i].num_enum_values == 0)
484 continue;
485
486 info[i].enum_values =
487 malloc(src[i].num_enum_values *
488 sizeof(*info[i].enum_values));
489 assert(info[i].enum_values);
490 for (j = 0; j < info[i].num_enum_values; j++) {
491 info[i].enum_values[j].name = src[i].enum_values[j].name;
492 info[i].enum_values[j].valid = false;
493 }
494 }
495
496 for (i = 0; i < props->count_props; i++) {
497 unsigned int k;
498
499 prop = drmModeGetProperty(b->drm.fd, props->props[i]);
500 if (!prop)
501 continue;
502
503 for (j = 0; j < num_infos; j++) {
504 if (!strcmp(prop->name, info[j].name))
505 break;
506 }
507
508 /* We don't know/care about this property. */
509 if (j == num_infos) {
510#ifdef DEBUG
511 weston_log("DRM debug: unrecognized property %u '%s'\n",
512 prop->prop_id, prop->name);
513#endif
514 drmModeFreeProperty(prop);
515 continue;
516 }
517
518 if (info[j].num_enum_values == 0 &&
519 (prop->flags & DRM_MODE_PROP_ENUM)) {
520 weston_log("DRM: expected property %s to not be an"
521 " enum, but it is; ignoring\n", prop->name);
522 drmModeFreeProperty(prop);
523 continue;
524 }
525
526 info[j].prop_id = props->props[i];
527
528 if (info[j].num_enum_values == 0) {
529 drmModeFreeProperty(prop);
530 continue;
531 }
532
533 if (!(prop->flags & DRM_MODE_PROP_ENUM)) {
534 weston_log("DRM: expected property %s to be an enum,"
535 " but it is not; ignoring\n", prop->name);
536 drmModeFreeProperty(prop);
537 info[j].prop_id = 0;
538 continue;
539 }
540
541 for (k = 0; k < info[j].num_enum_values; k++) {
542 int l;
543
544 for (l = 0; l < prop->count_enums; l++) {
545 if (!strcmp(prop->enums[l].name,
546 info[j].enum_values[k].name))
547 break;
548 }
549
550 if (l == prop->count_enums)
551 continue;
552
553 info[j].enum_values[k].valid = true;
554 info[j].enum_values[k].value = prop->enums[l].value;
555 }
556
557 drmModeFreeProperty(prop);
558 }
559
560#ifdef DEBUG
561 for (i = 0; i < num_infos; i++) {
562 if (info[i].prop_id == 0)
563 weston_log("DRM warning: property '%s' missing\n",
564 info[i].name);
565 }
566#endif
567}
568
569/**
570 * Free DRM property information
571 *
572 * Frees all memory associated with a DRM property info array.
573 *
574 * @param info DRM property info array
575 * @param num_props Number of entries in array to free
576 */
577static void
578drm_property_info_free(struct drm_property_info *info, int num_props)
579{
580 int i;
581
582 for (i = 0; i < num_props; i++)
583 free(info[i].enum_values);
584}
585
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400586static void
587drm_output_set_cursor(struct drm_output *output);
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400588
Mario Kleinerf507ec32015-06-21 21:25:14 +0200589static void
590drm_output_update_msc(struct drm_output *output, unsigned int seq);
591
Jesse Barnes58ef3792012-02-23 09:45:49 -0500592static int
Daniel Stone08d4edf2017-04-04 17:54:34 +0100593drm_plane_crtc_supported(struct drm_output *output, struct drm_plane *plane)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500594{
Daniel Stone08d4edf2017-04-04 17:54:34 +0100595 return !!(plane->possible_crtcs & (1 << output->pipe));
Jesse Barnes58ef3792012-02-23 09:45:49 -0500596}
597
Daniel Stone72c0e1b2017-02-09 13:49:15 +0000598static struct drm_output *
599drm_output_find_by_crtc(struct drm_backend *b, uint32_t crtc_id)
600{
601 struct drm_output *output;
602
603 wl_list_for_each(output, &b->compositor->output_list, base.link) {
604 if (output->crtc_id == crtc_id)
605 return output;
606 }
607
608 wl_list_for_each(output, &b->compositor->pending_output_list,
609 base.link) {
610 if (output->crtc_id == crtc_id)
611 return output;
612 }
613
614 return NULL;
615}
616
Daniel Stonec0ec7592017-02-09 13:58:35 +0000617static struct drm_output *
618drm_output_find_by_connector(struct drm_backend *b, uint32_t connector_id)
619{
620 struct drm_output *output;
621
622 wl_list_for_each(output, &b->compositor->output_list, base.link) {
623 if (output->connector_id == connector_id)
624 return output;
625 }
626
627 wl_list_for_each(output, &b->compositor->pending_output_list,
628 base.link) {
629 if (output->connector_id == connector_id)
630 return output;
631 }
632
633 return NULL;
634}
635
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300636static void
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100637drm_fb_destroy(struct drm_fb *fb)
638{
639 if (fb->fb_id != 0)
640 drmModeRmFB(fb->fd, fb->fb_id);
641 weston_buffer_reference(&fb->buffer_ref, NULL);
642 free(fb);
643}
644
645static void
646drm_fb_destroy_dumb(struct drm_fb *fb)
647{
648 struct drm_mode_destroy_dumb destroy_arg;
649
650 assert(fb->type == BUFFER_PIXMAN_DUMB);
651
652 if (fb->map && fb->size > 0)
653 munmap(fb->map, fb->size);
654
655 memset(&destroy_arg, 0, sizeof(destroy_arg));
656 destroy_arg.handle = fb->handle;
657 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
658
659 drm_fb_destroy(fb);
660}
661
662static void
663drm_fb_destroy_gbm(struct gbm_bo *bo, void *data)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300664{
665 struct drm_fb *fb = data;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300666
Daniel Stonee4256832017-04-04 17:54:27 +0100667 assert(fb->type == BUFFER_GBM_SURFACE || fb->type == BUFFER_CLIENT ||
668 fb->type == BUFFER_CURSOR);
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100669 drm_fb_destroy(fb);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300670}
671
672static struct drm_fb *
Daniel Stonef214fdc2016-11-14 17:43:57 +0000673drm_fb_create_dumb(struct drm_backend *b, int width, int height,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300674 uint32_t format)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200675{
676 struct drm_fb *fb;
677 int ret;
678
679 struct drm_mode_create_dumb create_arg;
680 struct drm_mode_destroy_dumb destroy_arg;
681 struct drm_mode_map_dumb map_arg;
682
Peter Huttererf3d62272013-08-08 11:57:05 +1000683 fb = zalloc(sizeof *fb);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200684 if (!fb)
685 return NULL;
686
Daniel Stone6e7a9612017-04-04 17:54:26 +0100687 fb->refcnt = 1;
688
Daniel Stone0b70fa42017-04-04 17:54:23 +0100689 fb->format = pixel_format_get_info(format);
690 if (!fb->format) {
691 weston_log("failed to look up format 0x%lx\n",
692 (unsigned long) format);
693 goto err_fb;
694 }
695
696 if (!fb->format->depth || !fb->format->bpp) {
697 weston_log("format 0x%lx is not compatible with dumb buffers\n",
698 (unsigned long) format);
699 goto err_fb;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300700 }
701
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700702 memset(&create_arg, 0, sizeof create_arg);
Daniel Stone0b70fa42017-04-04 17:54:23 +0100703 create_arg.bpp = fb->format->bpp;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200704 create_arg.width = width;
705 create_arg.height = height;
706
Giulio Camuffo954f1832014-10-11 18:27:30 +0300707 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200708 if (ret)
709 goto err_fb;
710
Daniel Stonefc175a72017-04-04 17:54:22 +0100711 fb->type = BUFFER_PIXMAN_DUMB;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200712 fb->handle = create_arg.handle;
713 fb->stride = create_arg.pitch;
714 fb->size = create_arg.size;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000715 fb->width = width;
716 fb->height = height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300717 fb->fd = b->drm.fd;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200718
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300719 ret = -1;
720
721 if (!b->no_addfb2) {
Yong Bakos4b6321f2016-08-17 17:37:55 -0700722 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300723
724 handles[0] = fb->handle;
725 pitches[0] = fb->stride;
726 offsets[0] = 0;
727
728 ret = drmModeAddFB2(b->drm.fd, width, height,
Daniel Stone0b70fa42017-04-04 17:54:23 +0100729 fb->format->format,
730 handles, pitches, offsets,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300731 &fb->fb_id, 0);
732 if (ret) {
733 weston_log("addfb2 failed: %m\n");
734 b->no_addfb2 = 1;
735 }
736 }
737
738 if (ret) {
Daniel Stone0b70fa42017-04-04 17:54:23 +0100739 ret = drmModeAddFB(b->drm.fd, width, height,
740 fb->format->depth, fb->format->bpp,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300741 fb->stride, fb->handle, &fb->fb_id);
742 }
743
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200744 if (ret)
745 goto err_bo;
746
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700747 memset(&map_arg, 0, sizeof map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200748 map_arg.handle = fb->handle;
Chris Michaeleb2074a2013-05-01 21:26:02 -0400749 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200750 if (ret)
751 goto err_add_fb;
752
Chris Michael4a7ce1f2015-11-10 10:40:37 -0500753 fb->map = mmap(NULL, fb->size, PROT_WRITE,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300754 MAP_SHARED, b->drm.fd, map_arg.offset);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200755 if (fb->map == MAP_FAILED)
756 goto err_add_fb;
757
758 return fb;
759
760err_add_fb:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300761 drmModeRmFB(b->drm.fd, fb->fb_id);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200762err_bo:
763 memset(&destroy_arg, 0, sizeof(destroy_arg));
764 destroy_arg.handle = create_arg.handle;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300765 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200766err_fb:
767 free(fb);
768 return NULL;
769}
770
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200771static struct drm_fb *
Daniel Stone6e7a9612017-04-04 17:54:26 +0100772drm_fb_ref(struct drm_fb *fb)
773{
774 fb->refcnt++;
775 return fb;
776}
777
778static struct drm_fb *
Daniel Stonefc175a72017-04-04 17:54:22 +0100779drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend,
780 uint32_t format, enum drm_fb_type type)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300781{
782 struct drm_fb *fb = gbm_bo_get_user_data(bo);
Derek Foreman482ffdf2016-07-08 12:50:57 -0500783 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300784 int ret;
785
Daniel Stonefc175a72017-04-04 17:54:22 +0100786 if (fb) {
787 assert(fb->type == type);
Daniel Stone6e7a9612017-04-04 17:54:26 +0100788 return drm_fb_ref(fb);
Daniel Stonefc175a72017-04-04 17:54:22 +0100789 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300790
Bryce Harringtonde16d892014-11-20 22:21:57 -0800791 fb = zalloc(sizeof *fb);
792 if (fb == NULL)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200793 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300794
Daniel Stonefc175a72017-04-04 17:54:22 +0100795 fb->type = type;
Daniel Stone6e7a9612017-04-04 17:54:26 +0100796 fb->refcnt = 1;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300797 fb->bo = bo;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300798
Daniel Stonec8c917c2016-11-14 17:45:58 +0000799 fb->width = gbm_bo_get_width(bo);
800 fb->height = gbm_bo_get_height(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200801 fb->stride = gbm_bo_get_stride(bo);
802 fb->handle = gbm_bo_get_handle(bo).u32;
Daniel Stone0b70fa42017-04-04 17:54:23 +0100803 fb->format = pixel_format_get_info(format);
Daniel Stonec8c917c2016-11-14 17:45:58 +0000804 fb->size = fb->stride * fb->height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300805 fb->fd = backend->drm.fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300806
Daniel Stone0b70fa42017-04-04 17:54:23 +0100807 if (!fb->format) {
808 weston_log("couldn't look up format 0x%lx\n",
809 (unsigned long) format);
810 goto err_free;
811 }
812
Daniel Stonec8c917c2016-11-14 17:45:58 +0000813 if (backend->min_width > fb->width ||
814 fb->width > backend->max_width ||
815 backend->min_height > fb->height ||
816 fb->height > backend->max_height) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200817 weston_log("bo geometry out of bounds\n");
818 goto err_free;
819 }
820
821 ret = -1;
822
Giulio Camuffo954f1832014-10-11 18:27:30 +0300823 if (format && !backend->no_addfb2) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200824 handles[0] = fb->handle;
825 pitches[0] = fb->stride;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200826 offsets[0] = 0;
827
Daniel Stonec8c917c2016-11-14 17:45:58 +0000828 ret = drmModeAddFB2(backend->drm.fd, fb->width, fb->height,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200829 format, handles, pitches, offsets,
830 &fb->fb_id, 0);
831 if (ret) {
832 weston_log("addfb2 failed: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300833 backend->no_addfb2 = 1;
834 backend->sprites_are_broken = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200835 }
836 }
837
Daniel Stone0b70fa42017-04-04 17:54:23 +0100838 if (ret && fb->format->depth && fb->format->bpp)
Daniel Stonec8c917c2016-11-14 17:45:58 +0000839 ret = drmModeAddFB(backend->drm.fd, fb->width, fb->height,
Daniel Stone0b70fa42017-04-04 17:54:23 +0100840 fb->format->depth, fb->format->bpp,
841 fb->stride, fb->handle, &fb->fb_id);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200842
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300843 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200844 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200845 goto err_free;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300846 }
847
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100848 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_gbm);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300849
850 return fb;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200851
852err_free:
853 free(fb);
854 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300855}
856
857static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500858drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200859{
Pekka Paalanende685b82012-12-04 15:58:12 +0200860 assert(fb->buffer_ref.buffer == NULL);
Daniel Stonefc175a72017-04-04 17:54:22 +0100861 assert(fb->type == BUFFER_CLIENT);
Pekka Paalanende685b82012-12-04 15:58:12 +0200862 weston_buffer_reference(&fb->buffer_ref, buffer);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200863}
864
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200865static void
Daniel Stone05a5ac22017-04-04 17:54:25 +0100866drm_fb_unref(struct drm_fb *fb)
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200867{
868 if (!fb)
869 return;
870
Daniel Stone6e7a9612017-04-04 17:54:26 +0100871 assert(fb->refcnt > 0);
872 if (--fb->refcnt > 0)
873 return;
874
Daniel Stonefc175a72017-04-04 17:54:22 +0100875 switch (fb->type) {
876 case BUFFER_PIXMAN_DUMB:
Daniel Stone6e7a9612017-04-04 17:54:26 +0100877 drm_fb_destroy_dumb(fb);
Daniel Stonefc175a72017-04-04 17:54:22 +0100878 break;
Daniel Stonee4256832017-04-04 17:54:27 +0100879 case BUFFER_CURSOR:
Daniel Stonefc175a72017-04-04 17:54:22 +0100880 case BUFFER_CLIENT:
881 gbm_bo_destroy(fb->bo);
882 break;
883 case BUFFER_GBM_SURFACE:
Daniel Stone05a5ac22017-04-04 17:54:25 +0100884 gbm_surface_release_buffer(fb->gbm_surface, fb->bo);
Daniel Stonefc175a72017-04-04 17:54:22 +0100885 break;
886 default:
887 assert(NULL);
888 break;
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200889 }
890}
891
Daniel Stone90648872016-10-21 18:08:37 +0100892static int
893drm_view_transform_supported(struct weston_view *ev)
894{
895 return !ev->transform.enabled ||
896 (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
897}
898
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500899static uint32_t
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200900drm_output_check_scanout_format(struct drm_output *output,
901 struct weston_surface *es, struct gbm_bo *bo)
902{
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200903 uint32_t format;
904 pixman_region32_t r;
905
906 format = gbm_bo_get_format(bo);
907
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700908 if (format == GBM_FORMAT_ARGB8888) {
909 /* We can scanout an ARGB buffer if the surface's
910 * opaque region covers the whole output, but we have
911 * to use XRGB as the KMS format code. */
Kristian Høgsberg1be87e32014-01-17 14:22:41 -0800912 pixman_region32_init_rect(&r, 0, 0,
913 output->base.width,
914 output->base.height);
915 pixman_region32_subtract(&r, &r, &es->opaque);
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200916
917 if (!pixman_region32_not_empty(&r))
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500918 format = GBM_FORMAT_XRGB8888;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200919
920 pixman_region32_fini(&r);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500921 }
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700922
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100923 if (output->gbm_format == format)
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700924 return format;
925
926 return 0;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200927}
928
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400929static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200930drm_output_prepare_scanout_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500931 struct weston_view *ev)
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500932{
Armin Krezović545dba62016-08-05 15:54:18 +0200933 struct drm_backend *b = to_drm_backend(output->base.compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500934 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200935 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300936 struct gbm_bo *bo;
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500937 uint32_t format;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500938
Daniel Stone90648872016-10-21 18:08:37 +0100939 /* Don't import buffers which span multiple outputs. */
940 if (ev->output_mask != (1u << output->base.id))
941 return NULL;
942
Daniel Stone296d7a92016-10-21 18:05:37 +0100943 /* We use GBM to import buffers. */
944 if (b->gbm == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400945 return NULL;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500946
Daniel Stone296d7a92016-10-21 18:05:37 +0100947 if (buffer == NULL)
948 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +0100949 if (wl_shm_buffer_get(buffer->resource))
950 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +0100951
952 /* Make sure our view is exactly compatible with the output. */
953 if (ev->geometry.x != output->base.x ||
954 ev->geometry.y != output->base.y)
955 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +0100956 if (buffer->width != output->base.current_mode->width ||
957 buffer->height != output->base.current_mode->height)
958 return NULL;
959
Daniel Stone296d7a92016-10-21 18:05:37 +0100960 if (ev->transform.enabled)
961 return NULL;
Pekka Paalanen5580f222015-02-17 16:33:18 +0200962 if (ev->geometry.scissor_enabled)
963 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +0100964 if (viewport->buffer.transform != output->base.transform)
965 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +0100966 if (viewport->buffer.scale != output->base.current_scale)
967 return NULL;
968 if (!drm_view_transform_supported(ev))
969 return NULL;
970
971 if (ev->alpha != 1.0f)
972 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +0100973
Giulio Camuffo954f1832014-10-11 18:27:30 +0300974 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
Kristian Høgsberg63996462013-09-03 22:27:08 -0700975 buffer->resource, GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500976
Rob Bradford9b101872012-09-14 23:25:41 +0100977 /* Unable to use the buffer for scanout */
978 if (!bo)
979 return NULL;
980
Jason Ekstranda7af7042013-10-12 22:38:11 -0500981 format = drm_output_check_scanout_format(output, ev->surface, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500982 if (format == 0) {
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300983 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400984 return NULL;
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300985 }
986
Daniel Stone5bb8f582017-04-04 17:54:28 +0100987 output->fb_pending = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT);
988 if (!output->fb_pending) {
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300989 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400990 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300991 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500992
Daniel Stone5bb8f582017-04-04 17:54:28 +0100993 drm_fb_set_buffer(output->fb_pending, buffer);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500994
Daniel Stone4202e502016-10-24 13:26:42 +0100995 return &output->scanout_plane;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500996}
997
Daniel Stone95d48a22017-04-04 17:54:30 +0100998static struct drm_fb *
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200999drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001000{
Armin Krezović545dba62016-08-05 15:54:18 +02001001 struct drm_backend *b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001002 struct gbm_bo *bo;
Daniel Stone95d48a22017-04-04 17:54:30 +01001003 struct drm_fb *ret;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001004
Giulio Camuffo954f1832014-10-11 18:27:30 +03001005 output->base.compositor->renderer->repaint_output(&output->base,
1006 damage);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001007
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001008 bo = gbm_surface_lock_front_buffer(output->gbm_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001009 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +02001010 weston_log("failed to lock front buffer: %m\n");
Daniel Stone95d48a22017-04-04 17:54:30 +01001011 return NULL;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001012 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001013
Daniel Stone95d48a22017-04-04 17:54:30 +01001014 ret = drm_fb_get_from_bo(bo, b, output->gbm_format, BUFFER_GBM_SURFACE);
1015 if (!ret) {
Martin Minarik6d118362012-06-07 18:01:59 +02001016 weston_log("failed to get drm_fb for bo\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001017 gbm_surface_release_buffer(output->gbm_surface, bo);
Daniel Stone95d48a22017-04-04 17:54:30 +01001018 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001019 }
Daniel Stone95d48a22017-04-04 17:54:30 +01001020 ret->gbm_surface = output->gbm_surface;
1021
1022 return ret;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001023}
1024
Daniel Stone95d48a22017-04-04 17:54:30 +01001025static struct drm_fb *
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001026drm_output_render_pixman(struct drm_output *output, pixman_region32_t *damage)
1027{
1028 struct weston_compositor *ec = output->base.compositor;
1029 pixman_region32_t total_damage, previous_damage;
1030
1031 pixman_region32_init(&total_damage);
1032 pixman_region32_init(&previous_damage);
1033
1034 pixman_region32_copy(&previous_damage, damage);
1035
1036 pixman_region32_union(&total_damage, damage, &output->previous_damage);
1037 pixman_region32_copy(&output->previous_damage, &previous_damage);
1038
1039 output->current_image ^= 1;
1040
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001041 pixman_renderer_output_set_buffer(&output->base,
1042 output->image[output->current_image]);
1043
1044 ec->renderer->repaint_output(&output->base, &total_damage);
1045
1046 pixman_region32_fini(&total_damage);
1047 pixman_region32_fini(&previous_damage);
Daniel Stone95d48a22017-04-04 17:54:30 +01001048
1049 return drm_fb_ref(output->dumb[output->current_image]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001050}
1051
1052static void
1053drm_output_render(struct drm_output *output, pixman_region32_t *damage)
1054{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001055 struct weston_compositor *c = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02001056 struct drm_backend *b = to_drm_backend(c);
Daniel Stone95d48a22017-04-04 17:54:30 +01001057 struct drm_fb *fb;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001058
Daniel Stone4e84f7d2017-04-04 17:54:29 +01001059 /* If we already have a client buffer promoted to scanout, then we don't
1060 * want to render. */
1061 if (output->fb_pending)
1062 return;
1063
Giulio Camuffo954f1832014-10-11 18:27:30 +03001064 if (b->use_pixman)
Daniel Stone95d48a22017-04-04 17:54:30 +01001065 fb = drm_output_render_pixman(output, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001066 else
Daniel Stone95d48a22017-04-04 17:54:30 +01001067 fb = drm_output_render_gl(output, damage);
1068
1069 if (!fb)
1070 return;
1071 output->fb_pending = fb;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001072
Giulio Camuffo954f1832014-10-11 18:27:30 +03001073 pixman_region32_subtract(&c->primary_plane.damage,
1074 &c->primary_plane.damage, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001075}
1076
1077static void
Richard Hughese7299962013-05-01 21:52:12 +01001078drm_output_set_gamma(struct weston_output *output_base,
1079 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
1080{
1081 int rc;
Armin Krezović545dba62016-08-05 15:54:18 +02001082 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001083 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +02001084 to_drm_backend(output->base.compositor);
Richard Hughese7299962013-05-01 21:52:12 +01001085
1086 /* check */
1087 if (output_base->gamma_size != size)
1088 return;
1089 if (!output->original_crtc)
1090 return;
1091
Giulio Camuffo954f1832014-10-11 18:27:30 +03001092 rc = drmModeCrtcSetGamma(backend->drm.fd,
Richard Hughese7299962013-05-01 21:52:12 +01001093 output->crtc_id,
1094 size, r, g, b);
1095 if (rc)
1096 weston_log("set gamma failed: %m\n");
1097}
1098
Bryce Harringtonada4f072015-06-30 13:25:46 -07001099/* Determine the type of vblank synchronization to use for the output.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001100 *
Bryce Harringtonada4f072015-06-30 13:25:46 -07001101 * The pipe parameter indicates which CRTC is in use. Knowing this, we
1102 * can determine which vblank sequence type to use for it. Traditional
1103 * cards had only two CRTCs, with CRTC 0 using no special flags, and
1104 * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
1105 * parameter indicates this.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001106 *
Bryce Harringtonada4f072015-06-30 13:25:46 -07001107 * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
1108 * 0-31. If this is non-zero it indicates we're dealing with a
1109 * multi-gpu situation and we need to calculate the vblank sync
1110 * using DRM_BLANK_HIGH_CRTC_MASK.
1111 */
Pekka Paalanenc8a1ff02015-07-02 15:06:08 +03001112static unsigned int
1113drm_waitvblank_pipe(struct drm_output *output)
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +02001114{
1115 if (output->pipe > 1)
1116 return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
1117 DRM_VBLANK_HIGH_CRTC_MASK;
1118 else if (output->pipe > 0)
1119 return DRM_VBLANK_SECONDARY;
1120 else
1121 return 0;
1122}
1123
David Herrmann1edf44c2013-10-22 17:11:26 +02001124static int
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001125drm_output_repaint(struct weston_output *output_base,
Daniel Stoneb1f166d2017-03-01 11:34:10 +00001126 pixman_region32_t *damage,
1127 void *repaint_data)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001128{
Armin Krezović545dba62016-08-05 15:54:18 +02001129 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001130 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +02001131 to_drm_backend(output->base.compositor);
Daniel Stone08d4edf2017-04-04 17:54:34 +01001132 struct drm_plane *s;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001133 struct drm_mode *mode;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001134 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001135
Armin Krezović08368132016-09-30 14:11:05 +02001136 if (output->disable_pending || output->destroy_pending)
David Herrmann1edf44c2013-10-22 17:11:26 +02001137 return -1;
Xiong Zhangabd5d472013-10-11 14:43:07 +08001138
Daniel Stonef30a18c2017-04-04 17:54:31 +01001139 assert(!output->fb_last);
1140
Derek Foreman2cd87fe2017-04-13 13:48:48 -05001141 /* If disable_planes is set then assign_planes() wasn't
1142 * called for this render, so we could still have a stale
1143 * cursor plane set up.
1144 */
1145 if (output->base.disable_planes) {
1146 output->cursor_view = NULL;
1147 output->cursor_plane.x = INT32_MIN;
1148 output->cursor_plane.y = INT32_MIN;
1149 }
1150
Daniel Stone4e84f7d2017-04-04 17:54:29 +01001151 drm_output_render(output, damage);
Daniel Stone5bb8f582017-04-04 17:54:28 +01001152 if (!output->fb_pending)
David Herrmann1edf44c2013-10-22 17:11:26 +02001153 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001154
Hardeningff39efa2013-09-18 23:56:35 +02001155 mode = container_of(output->base.current_mode, struct drm_mode, base);
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03001156 if (output->state_invalid || !output->fb_current ||
Daniel Stone5bb8f582017-04-04 17:54:28 +01001157 output->fb_current->stride != output->fb_pending->stride) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001158 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Daniel Stone5bb8f582017-04-04 17:54:28 +01001159 output->fb_pending->fb_id, 0, 0,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001160 &output->connector_id, 1,
1161 &mode->mode_info);
1162 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +02001163 weston_log("set mode failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +02001164 goto err_pageflip;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001165 }
Ander Conselvan de Oliveira722a2d52013-06-04 16:24:05 +03001166 output_base->set_dpms(output_base, WESTON_DPMS_ON);
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03001167
1168 output->state_invalid = false;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001169 }
1170
Giulio Camuffo954f1832014-10-11 18:27:30 +03001171 if (drmModePageFlip(backend->drm.fd, output->crtc_id,
Daniel Stone5bb8f582017-04-04 17:54:28 +01001172 output->fb_pending->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -05001173 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001174 weston_log("queueing pageflip failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +02001175 goto err_pageflip;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -05001176 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001177
Daniel Stonef30a18c2017-04-04 17:54:31 +01001178 output->fb_last = output->fb_current;
1179 output->fb_current = output->fb_pending;
1180 output->fb_pending = NULL;
1181
Daniel Stone205c0a02017-04-04 17:54:33 +01001182 assert(!output->page_flip_pending);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001183 output->page_flip_pending = 1;
1184
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001185 if (output->pageflip_timer)
1186 wl_event_source_timer_update(output->pageflip_timer,
1187 backend->pageflip_timeout);
1188
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001189 drm_output_set_cursor(output);
1190
Jesse Barnes58ef3792012-02-23 09:45:49 -05001191 /*
1192 * Now, update all the sprite surfaces
1193 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001194 wl_list_for_each(s, &backend->sprite_list, link) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001195 uint32_t flags = 0, fb_id = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001196 drmVBlank vbl = {
1197 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
1198 .request.sequence = 1,
1199 };
1200
Daniel Stonef30a18c2017-04-04 17:54:31 +01001201 /* XXX: Set output much earlier, so we don't attempt to place
1202 * planes on entirely the wrong output. */
Daniel Stone5bb8f582017-04-04 17:54:28 +01001203 if ((!s->fb_current && !s->fb_pending) ||
Daniel Stone08d4edf2017-04-04 17:54:34 +01001204 !drm_plane_crtc_supported(output, s))
Jesse Barnes58ef3792012-02-23 09:45:49 -05001205 continue;
1206
Daniel Stone5bb8f582017-04-04 17:54:28 +01001207 if (s->fb_pending && !backend->sprites_hidden)
1208 fb_id = s->fb_pending->fb_id;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001209
Giulio Camuffo954f1832014-10-11 18:27:30 +03001210 ret = drmModeSetPlane(backend->drm.fd, s->plane_id,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001211 output->crtc_id, fb_id, flags,
Jesse Barnes58ef3792012-02-23 09:45:49 -05001212 s->dest_x, s->dest_y,
1213 s->dest_w, s->dest_h,
1214 s->src_x, s->src_y,
1215 s->src_w, s->src_h);
1216 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +02001217 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05001218 ret, strerror(errno));
1219
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +02001220 vbl.request.type |= drm_waitvblank_pipe(output);
Rob Clark5ca1a472012-08-08 20:27:37 -05001221
Jesse Barnes58ef3792012-02-23 09:45:49 -05001222 /*
1223 * Queue a vblank signal so we know when the surface
1224 * becomes active on the display or has been replaced.
1225 */
1226 vbl.request.signal = (unsigned long)s;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001227 ret = drmWaitVBlank(backend->drm.fd, &vbl);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001228 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +02001229 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05001230 ret, strerror(errno));
1231 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001232
1233 s->output = output;
Daniel Stonef30a18c2017-04-04 17:54:31 +01001234 s->fb_last = s->fb_current;
1235 s->fb_current = s->fb_pending;
1236 s->fb_pending = NULL;
Daniel Stone65d87d02017-04-04 17:54:32 +01001237 output->vblank_pending++;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001238 }
1239
David Herrmann1edf44c2013-10-22 17:11:26 +02001240 return 0;
1241
1242err_pageflip:
Kristian Høgsbergb3955b02014-01-23 16:25:06 -08001243 output->cursor_view = NULL;
Daniel Stone5bb8f582017-04-04 17:54:28 +01001244 if (output->fb_pending) {
1245 drm_fb_unref(output->fb_pending);
1246 output->fb_pending = NULL;
David Herrmann1edf44c2013-10-22 17:11:26 +02001247 }
1248
1249 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001250}
1251
1252static void
Jonas Ådahle5a12252013-04-05 23:07:11 +02001253drm_output_start_repaint_loop(struct weston_output *output_base)
1254{
Armin Krezović545dba62016-08-05 15:54:18 +02001255 struct drm_output *output = to_drm_output(output_base);
1256 struct drm_backend *backend =
1257 to_drm_backend(output_base->compositor);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001258 uint32_t fb_id;
Mario Kleinerf507ec32015-06-21 21:25:14 +02001259 struct timespec ts, tnow;
1260 struct timespec vbl2now;
1261 int64_t refresh_nsec;
1262 int ret;
1263 drmVBlank vbl = {
1264 .request.type = DRM_VBLANK_RELATIVE,
1265 .request.sequence = 0,
1266 .request.signal = 0,
1267 };
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001268
Armin Krezović08368132016-09-30 14:11:05 +02001269 if (output->disable_pending || output->destroy_pending)
Xiong Zhangabd5d472013-10-11 14:43:07 +08001270 return;
1271
Daniel Stone5bb8f582017-04-04 17:54:28 +01001272 if (!output->fb_current) {
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001273 /* We can't page flip if there's no mode set */
David Herrmann3c688c52013-10-22 17:11:25 +02001274 goto finish_frame;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001275 }
1276
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03001277 /* Need to smash all state in from scratch; current timings might not
1278 * be what we want, page flip might not work, etc.
1279 */
1280 if (output->state_invalid)
1281 goto finish_frame;
1282
Mario Kleinerf507ec32015-06-21 21:25:14 +02001283 /* Try to get current msc and timestamp via instant query */
1284 vbl.request.type |= drm_waitvblank_pipe(output);
1285 ret = drmWaitVBlank(backend->drm.fd, &vbl);
1286
1287 /* Error ret or zero timestamp means failure to get valid timestamp */
1288 if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
1289 ts.tv_sec = vbl.reply.tval_sec;
1290 ts.tv_nsec = vbl.reply.tval_usec * 1000;
1291
1292 /* Valid timestamp for most recent vblank - not stale?
1293 * Stale ts could happen on Linux 3.17+, so make sure it
1294 * is not older than 1 refresh duration since now.
1295 */
1296 weston_compositor_read_presentation_clock(backend->compositor,
1297 &tnow);
1298 timespec_sub(&vbl2now, &tnow, &ts);
1299 refresh_nsec =
1300 millihz_to_nsec(output->base.current_mode->refresh);
1301 if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
1302 drm_output_update_msc(output, vbl.reply.sequence);
1303 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001304 WP_PRESENTATION_FEEDBACK_INVALID);
Mario Kleinerf507ec32015-06-21 21:25:14 +02001305 return;
1306 }
1307 }
1308
1309 /* Immediate query didn't provide valid timestamp.
1310 * Use pageflip fallback.
1311 */
Daniel Stone5bb8f582017-04-04 17:54:28 +01001312 fb_id = output->fb_current->fb_id;
Jonas Ådahle5a12252013-04-05 23:07:11 +02001313
Daniel Stone205c0a02017-04-04 17:54:33 +01001314 assert(!output->page_flip_pending);
1315 assert(!output->fb_last);
1316
Giulio Camuffo954f1832014-10-11 18:27:30 +03001317 if (drmModePageFlip(backend->drm.fd, output->crtc_id, fb_id,
Jonas Ådahle5a12252013-04-05 23:07:11 +02001318 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
1319 weston_log("queueing pageflip failed: %m\n");
David Herrmann3c688c52013-10-22 17:11:25 +02001320 goto finish_frame;
Jonas Ådahle5a12252013-04-05 23:07:11 +02001321 }
David Herrmann3c688c52013-10-22 17:11:25 +02001322
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001323 if (output->pageflip_timer)
1324 wl_event_source_timer_update(output->pageflip_timer,
1325 backend->pageflip_timeout);
1326
Daniel Stone205c0a02017-04-04 17:54:33 +01001327 output->fb_last = drm_fb_ref(output->fb_current);
1328 output->page_flip_pending = 1;
1329
David Herrmann3c688c52013-10-22 17:11:25 +02001330 return;
1331
1332finish_frame:
1333 /* if we cannot page-flip, immediately finish frame */
Daniel Stone3615ce12017-03-01 11:34:05 +00001334 weston_output_finish_frame(output_base, NULL,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001335 WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001336}
1337
1338static void
Pekka Paalanen641307c2014-09-23 22:08:47 -04001339drm_output_update_msc(struct drm_output *output, unsigned int seq)
1340{
1341 uint64_t msc_hi = output->base.msc >> 32;
1342
1343 if (seq < (output->base.msc & 0xffffffff))
1344 msc_hi++;
1345
1346 output->base.msc = (msc_hi << 32) + seq;
1347}
1348
1349static void
Jesse Barnes58ef3792012-02-23 09:45:49 -05001350vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
1351 void *data)
1352{
Daniel Stone08d4edf2017-04-04 17:54:34 +01001353 struct drm_plane *s = (struct drm_plane *)data;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001354 struct drm_output *output = s->output;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001355 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001356 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
1357 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001358
Pekka Paalanen641307c2014-09-23 22:08:47 -04001359 drm_output_update_msc(output, frame);
Daniel Stone65d87d02017-04-04 17:54:32 +01001360 output->vblank_pending--;
1361 assert(output->vblank_pending >= 0);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001362
Daniel Stonef30a18c2017-04-04 17:54:31 +01001363 assert(s->fb_last || s->fb_current);
1364 drm_fb_unref(s->fb_last);
1365 s->fb_last = NULL;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001366
Daniel Stone65d87d02017-04-04 17:54:32 +01001367 if (!output->page_flip_pending && !output->vblank_pending) {
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001368 /* Stop the pageflip timer instead of rearming it here */
1369 if (output->pageflip_timer)
1370 wl_event_source_timer_update(output->pageflip_timer, 0);
1371
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001372 ts.tv_sec = sec;
1373 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02001374 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001375 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05001376}
1377
1378static void
Armin Krezović08368132016-09-30 14:11:05 +02001379drm_output_destroy(struct weston_output *base);
Xiong Zhangabd5d472013-10-11 14:43:07 +08001380
1381static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001382page_flip_handler(int fd, unsigned int frame,
1383 unsigned int sec, unsigned int usec, void *data)
1384{
Armin Krezović545dba62016-08-05 15:54:18 +02001385 struct drm_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001386 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001387 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
1388 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
1389 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001390
Pekka Paalanen641307c2014-09-23 22:08:47 -04001391 drm_output_update_msc(output, frame);
1392
Daniel Stone205c0a02017-04-04 17:54:33 +01001393 assert(output->page_flip_pending);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001394 output->page_flip_pending = 0;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001395
Daniel Stone205c0a02017-04-04 17:54:33 +01001396 drm_fb_unref(output->fb_last);
1397 output->fb_last = NULL;
1398
Xiong Zhangabd5d472013-10-11 14:43:07 +08001399 if (output->destroy_pending)
1400 drm_output_destroy(&output->base);
Armin Krezović08368132016-09-30 14:11:05 +02001401 else if (output->disable_pending)
1402 weston_output_disable(&output->base);
Xiong Zhangabd5d472013-10-11 14:43:07 +08001403 else if (!output->vblank_pending) {
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001404 /* Stop the pageflip timer instead of rearming it here */
1405 if (output->pageflip_timer)
1406 wl_event_source_timer_update(output->pageflip_timer, 0);
1407
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001408 ts.tv_sec = sec;
1409 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02001410 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03001411
1412 /* We can't call this from frame_notify, because the output's
1413 * repaint needed flag is cleared just after that */
1414 if (output->recorder)
1415 weston_output_schedule_repaint(&output->base);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001416 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001417}
1418
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001419static uint32_t
Daniel Stone08d4edf2017-04-04 17:54:34 +01001420drm_output_check_plane_format(struct drm_plane *p,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001421 struct weston_view *ev, struct gbm_bo *bo)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001422{
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001423 uint32_t i, format;
1424
1425 format = gbm_bo_get_format(bo);
1426
1427 if (format == GBM_FORMAT_ARGB8888) {
1428 pixman_region32_t r;
1429
Kristian Høgsberg63093a32013-03-01 14:29:16 -05001430 pixman_region32_init_rect(&r, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001431 ev->surface->width,
1432 ev->surface->height);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001433 pixman_region32_subtract(&r, &r, &ev->surface->opaque);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001434
1435 if (!pixman_region32_not_empty(&r))
1436 format = GBM_FORMAT_XRGB8888;
1437
1438 pixman_region32_fini(&r);
1439 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05001440
Daniel Stone08d4edf2017-04-04 17:54:34 +01001441 for (i = 0; i < p->count_formats; i++)
1442 if (p->formats[i] == format)
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001443 return format;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001444
1445 return 0;
1446}
1447
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001448static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001449drm_output_prepare_overlay_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001450 struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001451{
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001452 struct weston_compositor *ec = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02001453 struct drm_backend *b = to_drm_backend(ec);
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001454 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001455 struct wl_resource *buffer_resource;
Daniel Stone08d4edf2017-04-04 17:54:34 +01001456 struct drm_plane *p;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001457 struct linux_dmabuf_buffer *dmabuf;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001458 int found = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001459 struct gbm_bo *bo;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001460 pixman_region32_t dest_rect, src_rect;
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001461 pixman_box32_t *box, tbox;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001462 uint32_t format;
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001463 wl_fixed_t sx1, sy1, sx2, sy2;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001464
Giulio Camuffo954f1832014-10-11 18:27:30 +03001465 if (b->sprites_are_broken)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001466 return NULL;
Kristian Høgsberg65bec242012-03-05 19:57:35 -05001467
Daniel Stone296d7a92016-10-21 18:05:37 +01001468 /* Don't import buffers which span multiple outputs. */
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001469 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001470 return NULL;
Ander Conselvan de Oliveirad450b192012-06-26 17:09:12 +03001471
Daniel Stone296d7a92016-10-21 18:05:37 +01001472 /* We can only import GBM buffers. */
1473 if (b->gbm == NULL)
1474 return NULL;
1475
Jason Ekstranda7af7042013-10-12 22:38:11 -05001476 if (ev->surface->buffer_ref.buffer == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001477 return NULL;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001478 buffer_resource = ev->surface->buffer_ref.buffer->resource;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001479 if (wl_shm_buffer_get(buffer_resource))
Rob Clark702ffae2012-08-09 14:18:27 -05001480 return NULL;
1481
Daniel Stone296d7a92016-10-21 18:05:37 +01001482 if (viewport->buffer.transform != output->base.transform)
1483 return NULL;
1484 if (viewport->buffer.scale != output->base.current_scale)
1485 return NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001486 if (!drm_view_transform_supported(ev))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001487 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001488
Daniel Stone296d7a92016-10-21 18:05:37 +01001489 if (ev->alpha != 1.0f)
1490 return NULL;
1491
Daniel Stone08d4edf2017-04-04 17:54:34 +01001492 wl_list_for_each(p, &b->sprite_list, link) {
1493 if (!drm_plane_crtc_supported(output, p))
Jesse Barnes58ef3792012-02-23 09:45:49 -05001494 continue;
1495
Daniel Stone08d4edf2017-04-04 17:54:34 +01001496 if (!p->fb_pending) {
Jesse Barnes58ef3792012-02-23 09:45:49 -05001497 found = 1;
1498 break;
1499 }
1500 }
1501
1502 /* No sprites available */
1503 if (!found)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001504 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001505
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001506 if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) {
Bryce Harringtona3582072015-08-14 12:23:13 -07001507#ifdef HAVE_GBM_FD_IMPORT
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001508 /* XXX: TODO:
1509 *
1510 * Use AddFB2 directly, do not go via GBM.
1511 * Add support for multiplanar formats.
1512 * Both require refactoring in the DRM-backend to
1513 * support a mix of gbm_bos and drmfbs.
1514 */
1515 struct gbm_import_fd_data gbm_dmabuf = {
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001516 .fd = dmabuf->attributes.fd[0],
1517 .width = dmabuf->attributes.width,
1518 .height = dmabuf->attributes.height,
1519 .stride = dmabuf->attributes.stride[0],
1520 .format = dmabuf->attributes.format
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001521 };
1522
Micah Fedkec8890122017-02-01 15:28:23 -05001523 /* XXX: TODO:
1524 *
1525 * Currently the buffer is rejected if any dmabuf attribute
1526 * flag is set. This keeps us from passing an inverted /
1527 * interlaced / bottom-first buffer (or any other type that may
1528 * be added in the future) through to an overlay. Ultimately,
1529 * these types of buffers should be handled through buffer
1530 * transforms and not as spot-checks requiring specific
1531 * knowledge. */
1532 if (dmabuf->attributes.n_planes != 1 ||
1533 dmabuf->attributes.offset[0] != 0 ||
1534 dmabuf->attributes.flags)
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001535 return NULL;
1536
1537 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf,
1538 GBM_BO_USE_SCANOUT);
Bryce Harringtona3582072015-08-14 12:23:13 -07001539#else
1540 return NULL;
1541#endif
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001542 } else {
1543 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
1544 buffer_resource, GBM_BO_USE_SCANOUT);
1545 }
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001546 if (!bo)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001547 return NULL;
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001548
Daniel Stone08d4edf2017-04-04 17:54:34 +01001549 format = drm_output_check_plane_format(p, ev, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001550 if (format == 0) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001551 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001552 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001553 }
1554
Daniel Stone08d4edf2017-04-04 17:54:34 +01001555 p->fb_pending = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT);
1556 if (!p->fb_pending) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001557 gbm_bo_destroy(bo);
1558 return NULL;
1559 }
1560
Daniel Stone08d4edf2017-04-04 17:54:34 +01001561 drm_fb_set_buffer(p->fb_pending, ev->surface->buffer_ref.buffer);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001562
Jason Ekstranda7af7042013-10-12 22:38:11 -05001563 box = pixman_region32_extents(&ev->transform.boundingbox);
Daniel Stone08d4edf2017-04-04 17:54:34 +01001564 p->base.x = box->x1;
1565 p->base.y = box->y1;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001566
Jesse Barnes58ef3792012-02-23 09:45:49 -05001567 /*
1568 * Calculate the source & dest rects properly based on actual
Derek Foreman4b1a0a12014-09-10 15:37:33 -05001569 * position (note the caller has called weston_view_update_transform()
Jesse Barnes58ef3792012-02-23 09:45:49 -05001570 * for us already).
1571 */
1572 pixman_region32_init(&dest_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001573 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001574 &output->base.region);
1575 pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001576 box = pixman_region32_extents(&dest_rect);
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001577 tbox = weston_transformed_rect(output->base.width,
1578 output->base.height,
1579 output->base.transform,
1580 output->base.current_scale,
Alexander Larssond9a7bb72013-05-22 14:41:39 +02001581 *box);
Daniel Stone08d4edf2017-04-04 17:54:34 +01001582 p->dest_x = tbox.x1;
1583 p->dest_y = tbox.y1;
1584 p->dest_w = tbox.x2 - tbox.x1;
1585 p->dest_h = tbox.y2 - tbox.y1;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001586 pixman_region32_fini(&dest_rect);
1587
1588 pixman_region32_init(&src_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001589 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001590 &output->base.region);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001591 box = pixman_region32_extents(&src_rect);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001592
Jason Ekstranda7af7042013-10-12 22:38:11 -05001593 weston_view_from_global_fixed(ev,
1594 wl_fixed_from_int(box->x1),
1595 wl_fixed_from_int(box->y1),
1596 &sx1, &sy1);
1597 weston_view_from_global_fixed(ev,
1598 wl_fixed_from_int(box->x2),
1599 wl_fixed_from_int(box->y2),
1600 &sx2, &sy2);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001601
1602 if (sx1 < 0)
1603 sx1 = 0;
1604 if (sy1 < 0)
1605 sy1 = 0;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001606 if (sx2 > wl_fixed_from_int(ev->surface->width))
1607 sx2 = wl_fixed_from_int(ev->surface->width);
1608 if (sy2 > wl_fixed_from_int(ev->surface->height))
1609 sy2 = wl_fixed_from_int(ev->surface->height);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001610
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001611 tbox.x1 = sx1;
1612 tbox.y1 = sy1;
1613 tbox.x2 = sx2;
1614 tbox.y2 = sy2;
1615
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001616 tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width),
1617 wl_fixed_from_int(ev->surface->height),
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001618 viewport->buffer.transform,
1619 viewport->buffer.scale,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001620 tbox);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001621
Daniel Stone08d4edf2017-04-04 17:54:34 +01001622 p->src_x = tbox.x1 << 8;
1623 p->src_y = tbox.y1 << 8;
1624 p->src_w = (tbox.x2 - tbox.x1) << 8;
1625 p->src_h = (tbox.y2 - tbox.y1) << 8;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001626 pixman_region32_fini(&src_rect);
1627
Daniel Stone08d4edf2017-04-04 17:54:34 +01001628 return &p->base;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001629}
1630
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001631static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001632drm_output_prepare_cursor_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001633 struct weston_view *ev)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001634{
Armin Krezović545dba62016-08-05 15:54:18 +02001635 struct drm_backend *b = to_drm_backend(output->base.compositor);
Neil Robertsf37f82c2014-05-01 18:00:41 +01001636 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Derek Foreman6c19b692015-12-03 14:07:12 -06001637 struct wl_shm_buffer *shmbuf;
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001638 float x, y;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001639
Giulio Camuffo954f1832014-10-11 18:27:30 +03001640 if (b->cursors_are_broken)
Kristian Høgsberg8a015802012-08-09 17:19:23 -04001641 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001642
1643 if (output->cursor_view)
Pekka Paalanen5580f222015-02-17 16:33:18 +02001644 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001645
1646 /* Don't import buffers which span multiple outputs. */
1647 if (ev->output_mask != (1u << output->base.id))
1648 return NULL;
1649
1650 /* We use GBM to import SHM buffers. */
1651 if (b->gbm == NULL)
1652 return NULL;
1653
Derek Foreman6c19b692015-12-03 14:07:12 -06001654 if (ev->surface->buffer_ref.buffer == NULL)
1655 return NULL;
1656 shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource);
1657 if (!shmbuf)
1658 return NULL;
1659 if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888)
1660 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001661
1662 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
1663 return NULL;
1664 if (ev->transform.enabled &&
1665 (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE))
1666 return NULL;
1667 if (viewport->buffer.scale != output->base.current_scale)
1668 return NULL;
1669 if (ev->geometry.scissor_enabled)
1670 return NULL;
1671
Derek Foreman6c19b692015-12-03 14:07:12 -06001672 if (ev->surface->width > b->cursor_width ||
Daniel Stone70d337d2015-06-16 18:42:23 +01001673 ev->surface->height > b->cursor_height)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001674 return NULL;
1675
Jason Ekstranda7af7042013-10-12 22:38:11 -05001676 output->cursor_view = ev;
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001677 weston_view_to_global_float(ev, 0, 0, &x, &y);
1678 output->cursor_plane.x = x;
1679 output->cursor_plane.y = y;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001680
1681 return &output->cursor_plane;
1682}
1683
Pekka Paalanend0ead482014-06-16 12:05:40 +03001684/**
1685 * Update the image for the current cursor surface
1686 *
1687 * @param b DRM backend structure
1688 * @param bo GBM buffer object to write into
1689 * @param ev View to use for cursor image
1690 */
1691static void
1692cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo,
1693 struct weston_view *ev)
1694{
1695 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
1696 uint32_t buf[b->cursor_width * b->cursor_height];
1697 int32_t stride;
1698 uint8_t *s;
1699 int i;
1700
1701 assert(buffer && buffer->shm_buffer);
1702 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
1703 assert(ev->surface->width <= b->cursor_width);
1704 assert(ev->surface->height <= b->cursor_height);
1705
1706 memset(buf, 0, sizeof buf);
1707 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
1708 s = wl_shm_buffer_get_data(buffer->shm_buffer);
1709
1710 wl_shm_buffer_begin_access(buffer->shm_buffer);
1711 for (i = 0; i < ev->surface->height; i++)
1712 memcpy(buf + i * b->cursor_width,
1713 s + i * stride,
1714 ev->surface->width * 4);
1715 wl_shm_buffer_end_access(buffer->shm_buffer);
1716
1717 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
1718 weston_log("failed update cursor: %m\n");
1719}
1720
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001721static void
1722drm_output_set_cursor(struct drm_output *output)
1723{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001724 struct weston_view *ev = output->cursor_view;
Armin Krezović545dba62016-08-05 15:54:18 +02001725 struct drm_backend *b = to_drm_backend(output->base.compositor);
Pekka Paalanend0ead482014-06-16 12:05:40 +03001726 EGLint handle;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001727 struct gbm_bo *bo;
Derek Foremanbe428b32015-11-24 11:39:38 -06001728 float x, y;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001729
Jason Ekstranda7af7042013-10-12 22:38:11 -05001730 if (ev == NULL) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001731 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001732 return;
1733 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001734
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001735 if (pixman_region32_not_empty(&output->cursor_plane.damage)) {
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001736 pixman_region32_fini(&output->cursor_plane.damage);
1737 pixman_region32_init(&output->cursor_plane.damage);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001738 output->current_cursor ^= 1;
Daniel Stonee4256832017-04-04 17:54:27 +01001739 bo = output->gbm_cursor_fb[output->current_cursor]->bo;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001740
Pekka Paalanend0ead482014-06-16 12:05:40 +03001741 cursor_bo_update(b, bo, ev);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001742 handle = gbm_bo_get_handle(bo).s32;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001743 if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle,
1744 b->cursor_width, b->cursor_height)) {
Pekka Paalanenae29da22012-08-06 14:57:05 +03001745 weston_log("failed to set cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001746 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001747 }
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001748 }
1749
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001750 x = (output->cursor_plane.x - output->base.x) *
1751 output->base.current_scale;
1752 y = (output->cursor_plane.y - output->base.y) *
1753 output->base.current_scale;
Pekka Paalanen7eaed402015-11-27 14:20:58 +02001754
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001755 if (drmModeMoveCursor(b->drm.fd, output->crtc_id, x, y)) {
1756 weston_log("failed to move cursor: %m\n");
1757 b->cursors_are_broken = 1;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001758 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001759}
1760
Jesse Barnes58ef3792012-02-23 09:45:49 -05001761static void
Daniel Stoneb1f166d2017-03-01 11:34:10 +00001762drm_assign_planes(struct weston_output *output_base, void *repaint_data)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001763{
Armin Krezović545dba62016-08-05 15:54:18 +02001764 struct drm_backend *b = to_drm_backend(output_base->compositor);
1765 struct drm_output *output = to_drm_output(output_base);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001766 struct weston_view *ev, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001767 pixman_region32_t overlap, surface_overlap;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001768 struct weston_plane *primary, *next_plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001769
1770 /*
1771 * Find a surface for each sprite in the output using some heuristics:
1772 * 1) size
1773 * 2) frequency of update
1774 * 3) opacity (though some hw might support alpha blending)
1775 * 4) clipping (this can be fixed with color keys)
1776 *
1777 * The idea is to save on blitting since this should save power.
1778 * If we can get a large video surface on the sprite for example,
1779 * the main display surface may not need to update at all, and
1780 * the client buffer can be used directly for the sprite surface
1781 * as we do for flipping full screen surfaces.
1782 */
1783 pixman_region32_init(&overlap);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001784 primary = &output_base->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001785
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001786 output->cursor_view = NULL;
1787 output->cursor_plane.x = INT32_MIN;
1788 output->cursor_plane.y = INT32_MIN;
1789
Giulio Camuffo954f1832014-10-11 18:27:30 +03001790 wl_list_for_each_safe(ev, next, &output_base->compositor->view_list, link) {
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001791 struct weston_surface *es = ev->surface;
1792
1793 /* Test whether this buffer can ever go into a plane:
1794 * non-shm, or small enough to be a cursor.
1795 *
1796 * Also, keep a reference when using the pixman renderer.
1797 * That makes it possible to do a seamless switch to the GL
1798 * renderer and since the pixman renderer keeps a reference
1799 * to the buffer anyway, there is no side effects.
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001800 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001801 if (b->use_pixman ||
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001802 (es->buffer_ref.buffer &&
1803 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
Derek Foreman87430472015-10-15 10:24:48 -05001804 (ev->surface->width <= b->cursor_width &&
1805 ev->surface->height <= b->cursor_height))))
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001806 es->keep_buffer = true;
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001807 else
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001808 es->keep_buffer = false;
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001809
Jesse Barnes58ef3792012-02-23 09:45:49 -05001810 pixman_region32_init(&surface_overlap);
1811 pixman_region32_intersect(&surface_overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001812 &ev->transform.boundingbox);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001813
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001814 next_plane = NULL;
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001815 if (pixman_region32_not_empty(&surface_overlap))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001816 next_plane = primary;
1817 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001818 next_plane = drm_output_prepare_cursor_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001819 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001820 next_plane = drm_output_prepare_scanout_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001821 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001822 next_plane = drm_output_prepare_overlay_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001823 if (next_plane == NULL)
1824 next_plane = primary;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001825
Jason Ekstranda7af7042013-10-12 22:38:11 -05001826 weston_view_move_to_plane(ev, next_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001827
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001828 if (next_plane == primary)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001829 pixman_region32_union(&overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001830 &ev->transform.boundingbox);
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001831
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001832 if (next_plane == primary ||
1833 next_plane == &output->cursor_plane) {
1834 /* cursor plane involves a copy */
1835 ev->psf_flags = 0;
1836 } else {
1837 /* All other planes are a direct scanout of a
1838 * single client buffer.
1839 */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001840 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001841 }
1842
Jesse Barnes58ef3792012-02-23 09:45:49 -05001843 pixman_region32_fini(&surface_overlap);
1844 }
1845 pixman_region32_fini(&overlap);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001846}
1847
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001848/**
1849 * Find the closest-matching mode for a given target
1850 *
1851 * Given a target mode, find the most suitable mode amongst the output's
1852 * current mode list to use, preferring the current mode if possible, to
1853 * avoid an expensive mode switch.
1854 *
1855 * @param output DRM output
1856 * @param target_mode Mode to attempt to match
1857 * @returns Pointer to a mode from the output's mode list
1858 */
Alex Wub7b8bda2012-04-17 17:20:48 +08001859static struct drm_mode *
1860choose_mode (struct drm_output *output, struct weston_mode *target_mode)
1861{
1862 struct drm_mode *tmp_mode = NULL, *mode;
1863
Hardeningff39efa2013-09-18 23:56:35 +02001864 if (output->base.current_mode->width == target_mode->width &&
1865 output->base.current_mode->height == target_mode->height &&
1866 (output->base.current_mode->refresh == target_mode->refresh ||
Alex Wub7b8bda2012-04-17 17:20:48 +08001867 target_mode->refresh == 0))
Hardeningff39efa2013-09-18 23:56:35 +02001868 return (struct drm_mode *)output->base.current_mode;
Alex Wub7b8bda2012-04-17 17:20:48 +08001869
1870 wl_list_for_each(mode, &output->base.mode_list, base.link) {
1871 if (mode->mode_info.hdisplay == target_mode->width &&
1872 mode->mode_info.vdisplay == target_mode->height) {
Mario Kleiner872797c2015-06-21 21:25:09 +02001873 if (mode->base.refresh == target_mode->refresh ||
1874 target_mode->refresh == 0) {
Alex Wub7b8bda2012-04-17 17:20:48 +08001875 return mode;
Daniel Stonef556ebe2015-05-21 08:28:58 +01001876 } else if (!tmp_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001877 tmp_mode = mode;
1878 }
1879 }
1880
1881 return tmp_mode;
1882}
1883
1884static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001885drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00001886static void
1887drm_output_fini_egl(struct drm_output *output);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001888static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001889drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00001890static void
1891drm_output_fini_pixman(struct drm_output *output);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001892
1893static int
Alex Wub7b8bda2012-04-17 17:20:48 +08001894drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
1895{
1896 struct drm_output *output;
1897 struct drm_mode *drm_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001898 struct drm_backend *b;
Alex Wub7b8bda2012-04-17 17:20:48 +08001899
1900 if (output_base == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001901 weston_log("output is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001902 return -1;
1903 }
1904
1905 if (mode == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001906 weston_log("mode is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001907 return -1;
1908 }
1909
Armin Krezović545dba62016-08-05 15:54:18 +02001910 b = to_drm_backend(output_base->compositor);
1911 output = to_drm_output(output_base);
Alex Wub7b8bda2012-04-17 17:20:48 +08001912 drm_mode = choose_mode (output, mode);
1913
1914 if (!drm_mode) {
Martin Minarik6d118362012-06-07 18:01:59 +02001915 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +08001916 return -1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001917 }
1918
Hardeningff39efa2013-09-18 23:56:35 +02001919 if (&drm_mode->base == output->base.current_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001920 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001921
Hardeningff39efa2013-09-18 23:56:35 +02001922 output->base.current_mode->flags = 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001923
Hardeningff39efa2013-09-18 23:56:35 +02001924 output->base.current_mode = &drm_mode->base;
1925 output->base.current_mode->flags =
Alex Wub7b8bda2012-04-17 17:20:48 +08001926 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1927
Daniel Stonef30a18c2017-04-04 17:54:31 +01001928 /* XXX: This drops our current buffer too early, before we've started
1929 * displaying it. Ideally this should be much more atomic and
1930 * integrated with a full repaint cycle, rather than doing a
1931 * sledgehammer modeswitch first, and only later showing new
1932 * content.
1933 */
Daniel Stone5bb8f582017-04-04 17:54:28 +01001934 drm_fb_unref(output->fb_current);
Daniel Stonef30a18c2017-04-04 17:54:31 +01001935 assert(!output->fb_last);
1936 assert(!output->fb_pending);
1937 output->fb_last = output->fb_current = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +08001938
Giulio Camuffo954f1832014-10-11 18:27:30 +03001939 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001940 drm_output_fini_pixman(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001941 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001942 weston_log("failed to init output pixman state with "
1943 "new mode\n");
1944 return -1;
1945 }
1946 } else {
Daniel Stone3e661f72016-11-04 17:24:06 +00001947 drm_output_fini_egl(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001948 if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001949 weston_log("failed to init output egl state with "
1950 "new mode");
1951 return -1;
1952 }
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001953 }
1954
Alex Wub7b8bda2012-04-17 17:20:48 +08001955 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001956}
1957
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001958static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001959on_drm_input(int fd, uint32_t mask, void *data)
1960{
1961 drmEventContext evctx;
1962
1963 memset(&evctx, 0, sizeof evctx);
Emil Velikov863e66b2017-04-04 18:07:34 +01001964 evctx.version = 2;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001965 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001966 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001967 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001968
1969 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001970}
1971
1972static int
Daniel Stoneefa504f2016-12-19 16:48:20 +00001973init_kms_caps(struct drm_backend *b)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001974{
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001975 uint64_t cap;
Daniel Stoneefa504f2016-12-19 16:48:20 +00001976 int ret;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001977 clockid_t clk_id;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001978
Daniel Stoneefa504f2016-12-19 16:48:20 +00001979 weston_log("using %s\n", b->drm.filename);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001980
Daniel Stoneefa504f2016-12-19 16:48:20 +00001981 ret = drmGetCap(b->drm.fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001982 if (ret == 0 && cap == 1)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001983 clk_id = CLOCK_MONOTONIC;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001984 else
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001985 clk_id = CLOCK_REALTIME;
1986
Giulio Camuffo954f1832014-10-11 18:27:30 +03001987 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001988 weston_log("Error: failed to set presentation clock %d.\n",
1989 clk_id);
1990 return -1;
1991 }
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +02001992
Daniel Stoneefa504f2016-12-19 16:48:20 +00001993 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_WIDTH, &cap);
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001994 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001995 b->cursor_width = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001996 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001997 b->cursor_width = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001998
Daniel Stoneefa504f2016-12-19 16:48:20 +00001999 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_HEIGHT, &cap);
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03002000 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002001 b->cursor_height = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03002002 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03002003 b->cursor_height = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03002004
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01002005 ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
2006 b->universal_planes = (ret == 0);
2007 weston_log("DRM: %s universal planes\n",
2008 b->universal_planes ? "supports" : "does not support");
2009
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02002010 return 0;
2011}
2012
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002013static struct gbm_device *
2014create_gbm_device(int fd)
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02002015{
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002016 struct gbm_device *gbm;
Alexandru DAMIANbe0ac5b2013-10-02 17:51:05 +01002017
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002018 gl_renderer = weston_load_module("gl-renderer.so",
2019 "gl_renderer_interface");
2020 if (!gl_renderer)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002021 return NULL;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002022
2023 /* GBM will load a dri driver, but even though they need symbols from
2024 * libglapi, in some version of Mesa they are not linked to it. Since
2025 * only the gl-renderer module links to it, the call above won't make
2026 * these symbols globally available, and loading the DRI driver fails.
2027 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
2028 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
2029
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002030 gbm = gbm_create_device(fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002031
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002032 return gbm;
2033}
2034
Bryce Harringtonc056a982015-05-19 15:25:18 -07002035/* When initializing EGL, if the preferred buffer format isn't available
Bryce Harringtonb9939982016-04-15 20:28:26 -07002036 * we may be able to substitute an ARGB format for an XRGB one.
Derek Foremanc4cfe852015-05-15 12:12:40 -05002037 *
2038 * This returns 0 if substitution isn't possible, but 0 might be a
2039 * legitimate format for other EGL platforms, so the caller is
2040 * responsible for checking for 0 before calling gl_renderer->create().
2041 *
2042 * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689
2043 * but it's entirely possible we'll see this again on other implementations.
2044 */
2045static int
2046fallback_format_for(uint32_t format)
2047{
2048 switch (format) {
2049 case GBM_FORMAT_XRGB8888:
2050 return GBM_FORMAT_ARGB8888;
2051 case GBM_FORMAT_XRGB2101010:
2052 return GBM_FORMAT_ARGB2101010;
2053 default:
2054 return 0;
2055 }
2056}
2057
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002058static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002059drm_backend_create_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002060{
Derek Foreman6d556372015-11-04 14:47:33 -06002061 EGLint format[3] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002062 b->gbm_format,
2063 fallback_format_for(b->gbm_format),
Derek Foreman6d556372015-11-04 14:47:33 -06002064 0,
Derek Foremanc4cfe852015-05-15 12:12:40 -05002065 };
Derek Foreman6d556372015-11-04 14:47:33 -06002066 int n_formats = 2;
John Kåre Alsakeref591aa2013-03-02 12:27:39 +01002067
Derek Foremanc4cfe852015-05-15 12:12:40 -05002068 if (format[1])
Derek Foreman6d556372015-11-04 14:47:33 -06002069 n_formats = 3;
Miguel A. Vicodddc6702016-05-18 17:41:07 +02002070 if (gl_renderer->display_create(b->compositor,
2071 EGL_PLATFORM_GBM_KHR,
2072 (void *)b->gbm,
Miguel A. Vico41700e32016-05-18 17:47:59 +02002073 NULL,
Miguel A. Vicodddc6702016-05-18 17:41:07 +02002074 gl_renderer->opaque_attribs,
2075 format,
2076 n_formats) < 0) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002077 return -1;
2078 }
2079
2080 return 0;
2081}
2082
2083static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002084init_egl(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002085{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002086 b->gbm = create_gbm_device(b->drm.fd);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002087
Giulio Camuffo954f1832014-10-11 18:27:30 +03002088 if (!b->gbm)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002089 return -1;
2090
Giulio Camuffo954f1832014-10-11 18:27:30 +03002091 if (drm_backend_create_gl_renderer(b) < 0) {
2092 gbm_device_destroy(b->gbm);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002093 return -1;
2094 }
2095
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002096 return 0;
2097}
2098
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002099static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002100init_pixman(struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002101{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002102 return pixman_renderer_init(b->compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002103}
2104
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002105/**
Pekka Paalanenec272712014-06-05 11:22:25 +03002106 * Create a drm_plane for a hardware plane
2107 *
2108 * Creates one drm_plane structure for a hardware plane, and initialises its
2109 * properties and formats.
2110 *
2111 * This function does not add the plane to the list of usable planes in Weston
2112 * itself; the caller is responsible for this.
2113 *
2114 * Call drm_plane_destroy to clean up the plane.
2115 *
2116 * @param b DRM compositor backend
2117 * @param kplane DRM plane to create
2118 */
2119static struct drm_plane *
2120drm_plane_create(struct drm_backend *b, const drmModePlane *kplane)
2121{
2122 struct drm_plane *plane;
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01002123 drmModeObjectProperties *props;
2124
2125 static struct drm_property_enum_info plane_type_enums[] = {
2126 [WDRM_PLANE_TYPE_PRIMARY] = {
2127 .name = "Primary",
2128 },
2129 [WDRM_PLANE_TYPE_OVERLAY] = {
2130 .name = "Overlay",
2131 },
2132 [WDRM_PLANE_TYPE_CURSOR] = {
2133 .name = "Cursor",
2134 },
2135 };
2136 static const struct drm_property_info plane_props[] = {
2137 [WDRM_PLANE_TYPE] = {
2138 .name = "type",
2139 .enum_values = plane_type_enums,
2140 .num_enum_values = WDRM_PLANE_TYPE__COUNT,
2141 },
2142 };
Pekka Paalanenec272712014-06-05 11:22:25 +03002143
2144 plane = zalloc(sizeof(*plane) + ((sizeof(uint32_t)) *
2145 kplane->count_formats));
2146 if (!plane) {
2147 weston_log("%s: out of memory\n", __func__);
2148 return NULL;
2149 }
2150
2151 plane->backend = b;
2152 plane->possible_crtcs = kplane->possible_crtcs;
2153 plane->plane_id = kplane->plane_id;
2154 plane->count_formats = kplane->count_formats;
2155 memcpy(plane->formats, kplane->formats,
2156 kplane->count_formats * sizeof(kplane->formats[0]));
2157
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01002158 props = drmModeObjectGetProperties(b->drm.fd, kplane->plane_id,
2159 DRM_MODE_OBJECT_PLANE);
2160 if (!props) {
2161 weston_log("couldn't get plane properties\n");
2162 free(plane);
2163 return NULL;
2164 }
2165 drm_property_info_populate(b, plane_props, plane->props,
2166 WDRM_PLANE__COUNT, props);
2167 plane->type =
2168 drm_property_get_value(&plane->props[WDRM_PLANE_TYPE],
2169 props,
2170 WDRM_PLANE_TYPE_OVERLAY);
2171 drmModeFreeObjectProperties(props);
2172
Pekka Paalanenec272712014-06-05 11:22:25 +03002173 weston_plane_init(&plane->base, b->compositor, 0, 0);
2174 wl_list_insert(&b->sprite_list, &plane->link);
2175
2176 return plane;
2177}
2178
2179/**
2180 * Destroy one DRM plane
2181 *
2182 * Destroy a DRM plane, removing it from screen and releasing its retained
2183 * buffers in the process. The counterpart to drm_plane_create.
2184 *
2185 * @param plane Plane to deallocate (will be freed)
2186 */
2187static void
2188drm_plane_destroy(struct drm_plane *plane)
2189{
2190 drmModeSetPlane(plane->backend->drm.fd, plane->plane_id, 0, 0, 0,
2191 0, 0, 0, 0, 0, 0, 0, 0);
2192 assert(!plane->fb_last);
2193 assert(!plane->fb_pending);
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01002194 drm_property_info_free(plane->props, WDRM_PLANE__COUNT);
Pekka Paalanenec272712014-06-05 11:22:25 +03002195 drm_fb_unref(plane->fb_current);
2196 weston_plane_release(&plane->base);
2197 wl_list_remove(&plane->link);
2198 free(plane);
2199}
2200
2201/**
2202 * Initialise sprites (overlay planes)
2203 *
2204 * Walk the list of provided DRM planes, and add overlay planes.
2205 *
2206 * Call destroy_sprites to free these planes.
2207 *
2208 * @param b DRM compositor backend
2209 */
2210static void
2211create_sprites(struct drm_backend *b)
2212{
2213 drmModePlaneRes *kplane_res;
2214 drmModePlane *kplane;
2215 struct drm_plane *drm_plane;
2216 uint32_t i;
2217
2218 kplane_res = drmModeGetPlaneResources(b->drm.fd);
2219 if (!kplane_res) {
2220 weston_log("failed to get plane resources: %s\n",
2221 strerror(errno));
2222 return;
2223 }
2224
2225 for (i = 0; i < kplane_res->count_planes; i++) {
2226 kplane = drmModeGetPlane(b->drm.fd, kplane_res->planes[i]);
2227 if (!kplane)
2228 continue;
2229
2230 drm_plane = drm_plane_create(b, kplane);
2231 drmModeFreePlane(kplane);
2232 if (!drm_plane)
2233 continue;
2234
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01002235 /* Ignore non-overlay planes for now. */
2236 if (drm_plane->type != WDRM_PLANE_TYPE_OVERLAY) {
2237 drm_plane_destroy(drm_plane);
2238 continue;
2239 }
2240
Pekka Paalanenec272712014-06-05 11:22:25 +03002241 weston_compositor_stack_plane(b->compositor, &drm_plane->base,
2242 &b->compositor->primary_plane);
2243 }
2244
2245 drmModeFreePlaneResources(kplane_res);
2246}
2247
2248/**
2249 * Clean up sprites (overlay planes)
2250 *
2251 * The counterpart to create_sprites.
2252 *
2253 * @param b DRM compositor backend
2254 */
2255static void
2256destroy_sprites(struct drm_backend *b)
2257{
2258 struct drm_plane *plane, *next;
2259
2260 wl_list_for_each_safe(plane, next, &b->sprite_list, link)
2261 drm_plane_destroy(plane);
2262}
2263
2264/**
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002265 * Add a mode to output's mode list
2266 *
2267 * Copy the supplied DRM mode into a Weston mode structure, and add it to the
2268 * output's mode list.
2269 *
2270 * @param output DRM output to add mode to
2271 * @param info DRM mode structure to add
2272 * @returns Newly-allocated Weston/DRM mode structure
2273 */
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002274static struct drm_mode *
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002275drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002276{
2277 struct drm_mode *mode;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04002278 uint64_t refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002279
2280 mode = malloc(sizeof *mode);
2281 if (mode == NULL)
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002282 return NULL;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002283
2284 mode->base.flags = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02002285 mode->base.width = info->hdisplay;
2286 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04002287
2288 /* Calculate higher precision (mHz) refresh rate */
2289 refresh = (info->clock * 1000000LL / info->htotal +
2290 info->vtotal / 2) / info->vtotal;
2291
2292 if (info->flags & DRM_MODE_FLAG_INTERLACE)
2293 refresh *= 2;
2294 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
2295 refresh /= 2;
2296 if (info->vscan > 1)
2297 refresh /= info->vscan;
2298
2299 mode->base.refresh = refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002300 mode->mode_info = *info;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002301
2302 if (info->type & DRM_MODE_TYPE_PREFERRED)
2303 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
2304
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002305 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
2306
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002307 return mode;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002308}
2309
2310static int
2311drm_subpixel_to_wayland(int drm_value)
2312{
2313 switch (drm_value) {
2314 default:
2315 case DRM_MODE_SUBPIXEL_UNKNOWN:
2316 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
2317 case DRM_MODE_SUBPIXEL_NONE:
2318 return WL_OUTPUT_SUBPIXEL_NONE;
2319 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
2320 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
2321 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
2322 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
2323 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
2324 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
2325 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
2326 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
2327 }
2328}
2329
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002330/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002331static uint32_t
2332drm_get_backlight(struct drm_output *output)
2333{
2334 long brightness, max_brightness, norm;
2335
2336 brightness = backlight_get_brightness(output->backlight);
2337 max_brightness = backlight_get_max_brightness(output->backlight);
2338
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002339 /* convert it on a scale of 0 to 255 */
2340 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002341
2342 return (uint32_t) norm;
2343}
2344
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002345/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002346static void
2347drm_set_backlight(struct weston_output *output_base, uint32_t value)
2348{
Armin Krezović545dba62016-08-05 15:54:18 +02002349 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002350 long max_brightness, new_brightness;
2351
2352 if (!output->backlight)
2353 return;
2354
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002355 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002356 return;
2357
2358 max_brightness = backlight_get_max_brightness(output->backlight);
2359
2360 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002361 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002362
2363 backlight_set_brightness(output->backlight, new_brightness);
2364}
2365
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002366static void
2367drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
2368{
Armin Krezović545dba62016-08-05 15:54:18 +02002369 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002370 struct weston_compositor *ec = output_base->compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02002371 struct drm_backend *b = to_drm_backend(ec);
Daniel Stone02cf4662017-03-03 16:19:39 +00002372 struct drm_property_info *prop =
2373 &output->props_conn[WDRM_CONNECTOR_DPMS];
Daniel Stone36609c72015-06-18 07:49:02 +01002374 int ret;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002375
Daniel Stone02cf4662017-03-03 16:19:39 +00002376 if (!prop->prop_id)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002377 return;
2378
Daniel Stone36609c72015-06-18 07:49:02 +01002379 ret = drmModeConnectorSetProperty(b->drm.fd, output->connector_id,
Daniel Stone02cf4662017-03-03 16:19:39 +00002380 prop->prop_id, level);
Daniel Stone36609c72015-06-18 07:49:02 +01002381 if (ret) {
2382 weston_log("DRM: DPMS: failed property set for %s\n",
2383 output->base.name);
2384 return;
2385 }
2386
2387 output->dpms = level;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002388}
2389
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002390static const char * const connector_type_names[] = {
Pekka Paalanen89c49b32015-08-19 15:25:57 +03002391 [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
2392 [DRM_MODE_CONNECTOR_VGA] = "VGA",
2393 [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
2394 [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
2395 [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
2396 [DRM_MODE_CONNECTOR_Composite] = "Composite",
2397 [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
2398 [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
2399 [DRM_MODE_CONNECTOR_Component] = "Component",
2400 [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
2401 [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
2402 [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
2403 [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
2404 [DRM_MODE_CONNECTOR_TV] = "TV",
2405 [DRM_MODE_CONNECTOR_eDP] = "eDP",
Pekka Paalanenab81f152015-08-24 14:27:07 +03002406#ifdef DRM_MODE_CONNECTOR_DSI
Pekka Paalanen89c49b32015-08-19 15:25:57 +03002407 [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
2408 [DRM_MODE_CONNECTOR_DSI] = "DSI",
Pekka Paalanenab81f152015-08-24 14:27:07 +03002409#endif
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04002410};
2411
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002412static char *
2413make_connector_name(const drmModeConnector *con)
2414{
2415 char name[32];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03002416 const char *type_name = NULL;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002417
2418 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
2419 type_name = connector_type_names[con->connector_type];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03002420
2421 if (!type_name)
2422 type_name = "UNNAMED";
2423
2424 snprintf(name, sizeof name, "%s-%d", type_name, con->connector_type_id);
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002425
2426 return strdup(name);
2427}
2428
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002429static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002430find_crtc_for_connector(struct drm_backend *b,
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002431 drmModeRes *resources, drmModeConnector *connector)
2432{
2433 drmModeEncoder *encoder;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002434 int i, j;
Daniel Stone75487c22017-01-16 14:33:38 +00002435 int ret = -1;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002436
2437 for (j = 0; j < connector->count_encoders; j++) {
Daniel Stone75487c22017-01-16 14:33:38 +00002438 uint32_t possible_crtcs, encoder_id, crtc_id;
2439
Giulio Camuffo954f1832014-10-11 18:27:30 +03002440 encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002441 if (encoder == NULL) {
2442 weston_log("Failed to get encoder.\n");
Daniel Stone75487c22017-01-16 14:33:38 +00002443 continue;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002444 }
Daniel Stone75487c22017-01-16 14:33:38 +00002445 encoder_id = encoder->encoder_id;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002446 possible_crtcs = encoder->possible_crtcs;
Daniel Stone75487c22017-01-16 14:33:38 +00002447 crtc_id = encoder->crtc_id;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002448 drmModeFreeEncoder(encoder);
2449
2450 for (i = 0; i < resources->count_crtcs; i++) {
Daniel Stone72c0e1b2017-02-09 13:49:15 +00002451 if (!(possible_crtcs & (1 << i)))
2452 continue;
2453
2454 if (drm_output_find_by_crtc(b, resources->crtcs[i]))
2455 continue;
2456
Daniel Stone75487c22017-01-16 14:33:38 +00002457 /* Try to preserve the existing
2458 * CRTC -> encoder -> connector routing; it makes
2459 * initialisation faster, and also since we have a
2460 * very dumb picking algorithm, may preserve a better
2461 * choice. */
2462 if (!connector->encoder_id ||
2463 (encoder_id == connector->encoder_id &&
2464 crtc_id == resources->crtcs[i]))
2465 return i;
2466
2467 ret = i;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002468 }
2469 }
2470
Daniel Stone75487c22017-01-16 14:33:38 +00002471 return ret;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002472}
2473
Daniel Stonee4256832017-04-04 17:54:27 +01002474static void drm_output_fini_cursor_egl(struct drm_output *output)
2475{
2476 unsigned int i;
2477
2478 for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) {
2479 drm_fb_unref(output->gbm_cursor_fb[i]);
2480 output->gbm_cursor_fb[i] = NULL;
2481 }
2482}
2483
2484static int
2485drm_output_init_cursor_egl(struct drm_output *output, struct drm_backend *b)
2486{
2487 unsigned int i;
2488
2489 for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) {
2490 struct gbm_bo *bo;
2491
2492 bo = gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
2493 GBM_FORMAT_ARGB8888,
2494 GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE);
2495 if (!bo)
2496 goto err;
2497
2498 output->gbm_cursor_fb[i] =
2499 drm_fb_get_from_bo(bo, b, GBM_FORMAT_ARGB8888,
2500 BUFFER_CURSOR);
2501 if (!output->gbm_cursor_fb[i]) {
2502 gbm_bo_destroy(bo);
2503 goto err;
2504 }
2505 }
2506
2507 return 0;
2508
2509err:
2510 weston_log("cursor buffers unavailable, using gl cursors\n");
2511 b->cursors_are_broken = 1;
2512 drm_output_fini_cursor_egl(output);
2513 return -1;
2514}
2515
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002516/* Init output state that depends on gl or gbm */
2517static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002518drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002519{
Derek Foremanc4cfe852015-05-15 12:12:40 -05002520 EGLint format[2] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002521 output->gbm_format,
2522 fallback_format_for(output->gbm_format),
Derek Foremanc4cfe852015-05-15 12:12:40 -05002523 };
Daniel Stonee4256832017-04-04 17:54:27 +01002524 int n_formats = 1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02002525
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002526 output->gbm_surface = gbm_surface_create(b->gbm,
Hardeningff39efa2013-09-18 23:56:35 +02002527 output->base.current_mode->width,
2528 output->base.current_mode->height,
Derek Foremanc4cfe852015-05-15 12:12:40 -05002529 format[0],
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002530 GBM_BO_USE_SCANOUT |
2531 GBM_BO_USE_RENDERING);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002532 if (!output->gbm_surface) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002533 weston_log("failed to create gbm surface\n");
2534 return -1;
2535 }
2536
Derek Foremanc4cfe852015-05-15 12:12:40 -05002537 if (format[1])
2538 n_formats = 2;
Miguel A. Vicoc095cde2016-05-18 17:43:00 +02002539 if (gl_renderer->output_window_create(&output->base,
2540 (EGLNativeWindowType)output->gbm_surface,
2541 output->gbm_surface,
2542 gl_renderer->opaque_attribs,
2543 format,
2544 n_formats) < 0) {
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02002545 weston_log("failed to create gl renderer output state\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002546 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002547 return -1;
2548 }
2549
Daniel Stonee4256832017-04-04 17:54:27 +01002550 drm_output_init_cursor_egl(output, b);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002551
2552 return 0;
2553}
2554
Daniel Stone3e661f72016-11-04 17:24:06 +00002555static void
2556drm_output_fini_egl(struct drm_output *output)
2557{
2558 gl_renderer->output_destroy(&output->base);
2559 gbm_surface_destroy(output->gbm_surface);
Daniel Stonee4256832017-04-04 17:54:27 +01002560 drm_output_fini_cursor_egl(output);
Daniel Stone3e661f72016-11-04 17:24:06 +00002561}
2562
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002563static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002564drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002565{
Hardeningff39efa2013-09-18 23:56:35 +02002566 int w = output->base.current_mode->width;
2567 int h = output->base.current_mode->height;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002568 uint32_t format = output->gbm_format;
2569 uint32_t pixman_format;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002570 unsigned int i;
2571
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002572 switch (format) {
2573 case GBM_FORMAT_XRGB8888:
2574 pixman_format = PIXMAN_x8r8g8b8;
2575 break;
2576 case GBM_FORMAT_RGB565:
2577 pixman_format = PIXMAN_r5g6b5;
2578 break;
2579 default:
2580 weston_log("Unsupported pixman format 0x%x\n", format);
2581 return -1;
2582 }
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002583
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002584 /* FIXME error checking */
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002585 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002586 output->dumb[i] = drm_fb_create_dumb(b, w, h, format);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002587 if (!output->dumb[i])
2588 goto err;
2589
2590 output->image[i] =
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002591 pixman_image_create_bits(pixman_format, w, h,
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002592 output->dumb[i]->map,
2593 output->dumb[i]->stride);
2594 if (!output->image[i])
2595 goto err;
2596 }
2597
2598 if (pixman_renderer_output_create(&output->base) < 0)
2599 goto err;
2600
2601 pixman_region32_init_rect(&output->previous_damage,
Alexander Larsson0b135062013-05-28 16:23:36 +02002602 output->base.x, output->base.y, output->base.width, output->base.height);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002603
2604 return 0;
2605
2606err:
2607 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
2608 if (output->dumb[i])
Daniel Stone6e7a9612017-04-04 17:54:26 +01002609 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002610 if (output->image[i])
2611 pixman_image_unref(output->image[i]);
2612
2613 output->dumb[i] = NULL;
2614 output->image[i] = NULL;
2615 }
2616
2617 return -1;
2618}
2619
2620static void
2621drm_output_fini_pixman(struct drm_output *output)
2622{
2623 unsigned int i;
2624
2625 pixman_renderer_output_destroy(&output->base);
2626 pixman_region32_fini(&output->previous_damage);
2627
2628 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002629 pixman_image_unref(output->image[i]);
Daniel Stone6e7a9612017-04-04 17:54:26 +01002630 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002631 output->dumb[i] = NULL;
2632 output->image[i] = NULL;
2633 }
2634}
2635
Richard Hughes2b2092a2013-04-24 14:58:02 +01002636static void
2637edid_parse_string(const uint8_t *data, char text[])
2638{
2639 int i;
2640 int replaced = 0;
2641
2642 /* this is always 12 bytes, but we can't guarantee it's null
2643 * terminated or not junk. */
2644 strncpy(text, (const char *) data, 12);
2645
Bryce Harrington9c7de162015-08-28 13:04:26 -07002646 /* guarantee our new string is null-terminated */
2647 text[12] = '\0';
2648
Richard Hughes2b2092a2013-04-24 14:58:02 +01002649 /* remove insane chars */
2650 for (i = 0; text[i] != '\0'; i++) {
2651 if (text[i] == '\n' ||
2652 text[i] == '\r') {
2653 text[i] = '\0';
2654 break;
2655 }
2656 }
2657
2658 /* ensure string is printable */
2659 for (i = 0; text[i] != '\0'; i++) {
2660 if (!isprint(text[i])) {
2661 text[i] = '-';
2662 replaced++;
2663 }
2664 }
2665
2666 /* if the string is random junk, ignore the string */
2667 if (replaced > 4)
2668 text[0] = '\0';
2669}
2670
2671#define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
2672#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
2673#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
2674#define EDID_OFFSET_DATA_BLOCKS 0x36
2675#define EDID_OFFSET_LAST_BLOCK 0x6c
2676#define EDID_OFFSET_PNPID 0x08
2677#define EDID_OFFSET_SERIAL 0x0c
2678
2679static int
2680edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
2681{
2682 int i;
2683 uint32_t serial_number;
2684
2685 /* check header */
2686 if (length < 128)
2687 return -1;
2688 if (data[0] != 0x00 || data[1] != 0xff)
2689 return -1;
2690
2691 /* decode the PNP ID from three 5 bit words packed into 2 bytes
2692 * /--08--\/--09--\
2693 * 7654321076543210
2694 * |\---/\---/\---/
2695 * R C1 C2 C3 */
2696 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
2697 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
2698 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
2699 edid->pnp_id[3] = '\0';
2700
2701 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
2702 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
2703 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
2704 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
2705 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
2706 if (serial_number > 0)
2707 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
2708
2709 /* parse EDID data */
2710 for (i = EDID_OFFSET_DATA_BLOCKS;
2711 i <= EDID_OFFSET_LAST_BLOCK;
2712 i += 18) {
2713 /* ignore pixel clock data */
2714 if (data[i] != 0)
2715 continue;
2716 if (data[i+2] != 0)
2717 continue;
2718
2719 /* any useful blocks? */
2720 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
2721 edid_parse_string(&data[i+5],
2722 edid->monitor_name);
2723 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
2724 edid_parse_string(&data[i+5],
2725 edid->serial_number);
2726 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
2727 edid_parse_string(&data[i+5],
2728 edid->eisa_id);
2729 }
2730 }
2731 return 0;
2732}
2733
2734static void
Daniel Stone02cf4662017-03-03 16:19:39 +00002735find_and_parse_output_edid(struct drm_backend *b, struct drm_output *output,
2736 drmModeObjectPropertiesPtr props)
Richard Hughes2b2092a2013-04-24 14:58:02 +01002737{
2738 drmModePropertyBlobPtr edid_blob = NULL;
Daniel Stone02cf4662017-03-03 16:19:39 +00002739 uint32_t blob_id;
Richard Hughes2b2092a2013-04-24 14:58:02 +01002740 int rc;
2741
Daniel Stone02cf4662017-03-03 16:19:39 +00002742 blob_id =
2743 drm_property_get_value(&output->props_conn[WDRM_CONNECTOR_EDID],
2744 props, 0);
2745 if (!blob_id)
2746 return;
2747
2748 edid_blob = drmModeGetPropertyBlob(b->drm.fd, blob_id);
Richard Hughes2b2092a2013-04-24 14:58:02 +01002749 if (!edid_blob)
2750 return;
2751
2752 rc = edid_parse(&output->edid,
2753 edid_blob->data,
2754 edid_blob->length);
2755 if (!rc) {
2756 weston_log("EDID data '%s', '%s', '%s'\n",
2757 output->edid.pnp_id,
2758 output->edid.monitor_name,
2759 output->edid.serial_number);
2760 if (output->edid.pnp_id[0] != '\0')
2761 output->base.make = output->edid.pnp_id;
2762 if (output->edid.monitor_name[0] != '\0')
2763 output->base.model = output->edid.monitor_name;
2764 if (output->edid.serial_number[0] != '\0')
2765 output->base.serial_number = output->edid.serial_number;
2766 }
2767 drmModeFreePropertyBlob(edid_blob);
2768}
2769
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002770
2771
2772static int
2773parse_modeline(const char *s, drmModeModeInfo *mode)
2774{
2775 char hsync[16];
2776 char vsync[16];
2777 float fclock;
2778
2779 mode->type = DRM_MODE_TYPE_USERDEF;
2780 mode->hskew = 0;
2781 mode->vscan = 0;
2782 mode->vrefresh = 0;
2783 mode->flags = 0;
2784
Rob Bradford307e09e2013-07-26 16:29:40 +01002785 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002786 &fclock,
2787 &mode->hdisplay,
2788 &mode->hsync_start,
2789 &mode->hsync_end,
2790 &mode->htotal,
2791 &mode->vdisplay,
2792 &mode->vsync_start,
2793 &mode->vsync_end,
2794 &mode->vtotal, hsync, vsync) != 11)
2795 return -1;
2796
2797 mode->clock = fclock * 1000;
2798 if (strcmp(hsync, "+hsync") == 0)
2799 mode->flags |= DRM_MODE_FLAG_PHSYNC;
2800 else if (strcmp(hsync, "-hsync") == 0)
2801 mode->flags |= DRM_MODE_FLAG_NHSYNC;
2802 else
2803 return -1;
2804
2805 if (strcmp(vsync, "+vsync") == 0)
2806 mode->flags |= DRM_MODE_FLAG_PVSYNC;
2807 else if (strcmp(vsync, "-vsync") == 0)
2808 mode->flags |= DRM_MODE_FLAG_NVSYNC;
2809 else
2810 return -1;
2811
Emmanuel Gil Peyrota62138b2016-05-02 22:40:11 +01002812 snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f",
2813 mode->hdisplay, mode->vdisplay, fclock);
2814
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002815 return 0;
2816}
2817
Rob Bradford66bd9f52013-06-25 18:56:42 +01002818static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002819setup_output_seat_constraint(struct drm_backend *b,
Rob Bradford66bd9f52013-06-25 18:56:42 +01002820 struct weston_output *output,
2821 const char *s)
2822{
2823 if (strcmp(s, "") != 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05002824 struct weston_pointer *pointer;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002825 struct udev_seat *seat;
2826
Giulio Camuffo954f1832014-10-11 18:27:30 +03002827 seat = udev_seat_get_named(&b->input, s);
Derek Foreman0720ea32015-07-15 13:00:35 -05002828 if (!seat)
2829 return;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002830
Derek Foreman0720ea32015-07-15 13:00:35 -05002831 seat->base.output = output;
2832
Derek Foreman1281a362015-07-31 16:55:32 -05002833 pointer = weston_seat_get_pointer(&seat->base);
2834 if (pointer)
2835 weston_pointer_clamp(pointer,
2836 &pointer->x,
2837 &pointer->y);
Rob Bradford66bd9f52013-06-25 18:56:42 +01002838 }
2839}
2840
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002841static int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002842parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format)
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002843{
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002844 int ret = 0;
2845
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002846 if (s == NULL)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002847 *gbm_format = default_value;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002848 else if (strcmp(s, "xrgb8888") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002849 *gbm_format = GBM_FORMAT_XRGB8888;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002850 else if (strcmp(s, "rgb565") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002851 *gbm_format = GBM_FORMAT_RGB565;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002852 else if (strcmp(s, "xrgb2101010") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002853 *gbm_format = GBM_FORMAT_XRGB2101010;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002854 else {
2855 weston_log("fatal: unrecognized pixel format: %s\n", s);
2856 ret = -1;
2857 }
2858
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002859 return ret;
2860}
2861
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002862/**
2863 * Choose suitable mode for an output
2864 *
2865 * Find the most suitable mode to use for initial setup (or reconfiguration on
2866 * hotplug etc) for a DRM output.
2867 *
2868 * @param output DRM output to choose mode for
2869 * @param kind Strategy and preference to use when choosing mode
2870 * @param width Desired width for this output
2871 * @param height Desired height for this output
2872 * @param current_mode Mode currently being displayed on this output
2873 * @param modeline Manually-entered mode (may be NULL)
2874 * @returns A mode from the output's mode list, or NULL if none available
2875 */
2876static struct drm_mode *
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002877drm_output_choose_initial_mode(struct drm_backend *backend,
2878 struct drm_output *output,
2879 enum weston_drm_backend_output_mode mode,
Armin Krezović08368132016-09-30 14:11:05 +02002880 const char *modeline,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002881 const drmModeModeInfo *current_mode)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002882{
2883 struct drm_mode *preferred = NULL;
2884 struct drm_mode *current = NULL;
2885 struct drm_mode *configured = NULL;
2886 struct drm_mode *best = NULL;
2887 struct drm_mode *drm_mode;
Armin Krezović08368132016-09-30 14:11:05 +02002888 drmModeModeInfo drm_modeline;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002889 int32_t width = 0;
2890 int32_t height = 0;
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01002891 uint32_t refresh = 0;
2892 int n;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002893
Armin Krezović08368132016-09-30 14:11:05 +02002894 if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) {
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01002895 n = sscanf(modeline, "%dx%d@%d", &width, &height, &refresh);
2896 if (n != 2 && n != 3) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002897 width = -1;
2898
Armin Krezović08368132016-09-30 14:11:05 +02002899 if (parse_modeline(modeline, &drm_modeline) == 0) {
2900 configured = drm_output_add_mode(output, &drm_modeline);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002901 if (!configured)
2902 return NULL;
2903 } else {
2904 weston_log("Invalid modeline \"%s\" for output %s\n",
Armin Krezović08368132016-09-30 14:11:05 +02002905 modeline, output->base.name);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002906 }
2907 }
2908 }
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002909
2910 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002911 if (width == drm_mode->base.width &&
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01002912 height == drm_mode->base.height &&
2913 (refresh == 0 || refresh == drm_mode->mode_info.vrefresh))
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002914 configured = drm_mode;
2915
comic fans7a5c5622016-03-17 14:29:27 +02002916 if (memcmp(current_mode, &drm_mode->mode_info,
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002917 sizeof *current_mode) == 0)
2918 current = drm_mode;
2919
2920 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
2921 preferred = drm_mode;
2922
2923 best = drm_mode;
2924 }
2925
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002926 if (current == NULL && current_mode->clock != 0) {
2927 current = drm_output_add_mode(output, current_mode);
2928 if (!current)
2929 return NULL;
2930 }
2931
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002932 if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002933 configured = current;
2934
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002935 if (configured)
2936 return configured;
2937
2938 if (preferred)
2939 return preferred;
2940
2941 if (current)
2942 return current;
2943
2944 if (best)
2945 return best;
2946
2947 weston_log("no available modes for %s\n", output->base.name);
2948 return NULL;
2949}
2950
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002951static int
2952connector_get_current_mode(drmModeConnector *connector, int drm_fd,
2953 drmModeModeInfo *mode)
2954{
2955 drmModeEncoder *encoder;
2956 drmModeCrtc *crtc;
2957
2958 /* Get the current mode on the crtc that's currently driving
2959 * this connector. */
2960 encoder = drmModeGetEncoder(drm_fd, connector->encoder_id);
2961 memset(mode, 0, sizeof *mode);
2962 if (encoder != NULL) {
2963 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
2964 drmModeFreeEncoder(encoder);
2965 if (crtc == NULL)
2966 return -1;
2967 if (crtc->mode_valid)
2968 *mode = crtc->mode;
2969 drmModeFreeCrtc(crtc);
2970 }
2971
2972 return 0;
2973}
2974
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002975static int
Armin Krezović08368132016-09-30 14:11:05 +02002976drm_output_set_mode(struct weston_output *base,
2977 enum weston_drm_backend_output_mode mode,
2978 const char *modeline)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002979{
Armin Krezović08368132016-09-30 14:11:05 +02002980 struct drm_output *output = to_drm_output(base);
2981 struct drm_backend *b = to_drm_backend(base->compositor);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002982
Armin Krezović445b41b2016-10-09 23:48:16 +02002983 struct drm_mode *current;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002984 drmModeModeInfo crtc_mode;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002985
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002986 output->base.make = "unknown";
2987 output->base.model = "unknown";
Richard Hughes2b2092a2013-04-24 14:58:02 +01002988 output->base.serial_number = "unknown";
Matt Roper361d2ad2011-08-29 13:52:23 -07002989
Armin Krezović08368132016-09-30 14:11:05 +02002990 if (connector_get_current_mode(output->connector, b->drm.fd, &crtc_mode) < 0)
Armin Krezović445b41b2016-10-09 23:48:16 +02002991 return -1;
David Herrmann0f0d54e2011-12-08 17:05:45 +01002992
Armin Krezović08368132016-09-30 14:11:05 +02002993 current = drm_output_choose_initial_mode(b, output, mode, modeline, &crtc_mode);
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002994 if (!current)
Armin Krezović445b41b2016-10-09 23:48:16 +02002995 return -1;
Armin Krezović08368132016-09-30 14:11:05 +02002996
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002997 output->base.current_mode = &current->base;
Hardeningff39efa2013-09-18 23:56:35 +02002998 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
Wang Quanxianacb805a2012-07-30 18:09:46 -04002999
Armin Krezović08368132016-09-30 14:11:05 +02003000 /* Set native_ fields, so weston_output_mode_switch_to_native() works */
3001 output->base.native_mode = output->base.current_mode;
3002 output->base.native_scale = output->base.current_scale;
3003
3004 output->base.mm_width = output->connector->mmWidth;
3005 output->base.mm_height = output->connector->mmHeight;
3006
3007 return 0;
Armin Krezović08368132016-09-30 14:11:05 +02003008}
3009
3010static void
3011drm_output_set_gbm_format(struct weston_output *base,
3012 const char *gbm_format)
3013{
3014 struct drm_output *output = to_drm_output(base);
3015 struct drm_backend *b = to_drm_backend(base->compositor);
3016
3017 if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1)
3018 output->gbm_format = b->gbm_format;
3019}
3020
3021static void
3022drm_output_set_seat(struct weston_output *base,
3023 const char *seat)
3024{
3025 struct drm_output *output = to_drm_output(base);
3026 struct drm_backend *b = to_drm_backend(base->compositor);
3027
3028 setup_output_seat_constraint(b, &output->base,
3029 seat ? seat : "");
3030}
3031
3032static int
3033drm_output_enable(struct weston_output *base)
3034{
3035 struct drm_output *output = to_drm_output(base);
3036 struct drm_backend *b = to_drm_backend(base->compositor);
3037 struct weston_mode *m;
3038
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00003039 if (b->pageflip_timeout)
3040 drm_output_pageflip_timer_create(output);
3041
Giulio Camuffo954f1832014-10-11 18:27:30 +03003042 if (b->use_pixman) {
3043 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003044 weston_log("Failed to init output pixman state\n");
Daniel Stone02cf4662017-03-03 16:19:39 +00003045 goto err;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003046 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03003047 } else if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02003048 weston_log("Failed to init output gl state\n");
Daniel Stone02cf4662017-03-03 16:19:39 +00003049 goto err;
Kristian Høgsberg1d1e0a52012-10-21 13:29:26 -04003050 }
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04003051
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003052 if (output->backlight) {
Kristian Høgsberg220819f2013-05-23 20:29:40 -04003053 weston_log("Initialized backlight, device %s\n",
3054 output->backlight->path);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003055 output->base.set_backlight = drm_set_backlight;
3056 output->base.backlight_current = drm_get_backlight(output);
Kristian Høgsberg220819f2013-05-23 20:29:40 -04003057 } else {
3058 weston_log("Failed to initialize backlight\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003059 }
3060
Jonas Ådahle5a12252013-04-05 23:07:11 +02003061 output->base.start_repaint_loop = drm_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05003062 output->base.repaint = drm_output_repaint;
Jesse Barnes58ef3792012-02-23 09:45:49 -05003063 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003064 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08003065 output->base.switch_mode = drm_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01003066
Richard Hughese7299962013-05-01 21:52:12 +01003067 output->base.gamma_size = output->original_crtc->gamma_size;
3068 output->base.set_gamma = drm_output_set_gamma;
3069
Armin Krezović08368132016-09-30 14:11:05 +02003070 output->base.subpixel = drm_subpixel_to_wayland(output->connector->subpixel);
3071
Armin Krezović9bcf4c52017-02-08 13:55:26 +01003072 if (output->connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
3073 output->connector->connector_type == DRM_MODE_CONNECTOR_eDP)
Pekka Paalanen0079a942017-03-24 15:46:23 +02003074 output->base.connection_internal = true;
Armin Krezović08368132016-09-30 14:11:05 +02003075
Derek Foremanb0427562016-02-05 15:55:20 -06003076 weston_plane_init(&output->cursor_plane, b->compositor,
3077 INT32_MIN, INT32_MIN);
Daniel Stone4202e502016-10-24 13:26:42 +01003078 weston_plane_init(&output->scanout_plane, b->compositor, 0, 0);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003079
Giulio Camuffo954f1832014-10-11 18:27:30 +03003080 weston_compositor_stack_plane(b->compositor, &output->cursor_plane, NULL);
Daniel Stone4202e502016-10-24 13:26:42 +01003081 weston_compositor_stack_plane(b->compositor, &output->scanout_plane,
Giulio Camuffo954f1832014-10-11 18:27:30 +03003082 &b->compositor->primary_plane);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003083
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04003084 weston_log("Output %s, (connector %d, crtc %d)\n",
Richard Hughesafe690c2013-05-02 10:10:04 +01003085 output->base.name, output->connector_id, output->crtc_id);
Kristian Høgsberg061c4252012-06-28 11:28:15 -04003086 wl_list_for_each(m, &output->base.mode_list, link)
U. Artie Eoffd3ed6cb2014-01-10 10:15:17 -08003087 weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n",
Kristian Høgsberg061c4252012-06-28 11:28:15 -04003088 m->width, m->height, m->refresh / 1000.0,
3089 m->flags & WL_OUTPUT_MODE_PREFERRED ?
3090 ", preferred" : "",
3091 m->flags & WL_OUTPUT_MODE_CURRENT ?
3092 ", current" : "",
Armin Krezović08368132016-09-30 14:11:05 +02003093 output->connector->count_modes == 0 ?
Kristian Høgsberg061c4252012-06-28 11:28:15 -04003094 ", built-in" : "");
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003095
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03003096 output->state_invalid = true;
3097
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003098 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01003099
Daniel Stone02cf4662017-03-03 16:19:39 +00003100err:
David Herrmann0f0d54e2011-12-08 17:05:45 +01003101 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003102}
3103
Jesse Barnes58ef3792012-02-23 09:45:49 -05003104static void
Armin Krezović08368132016-09-30 14:11:05 +02003105drm_output_deinit(struct weston_output *base)
3106{
3107 struct drm_output *output = to_drm_output(base);
3108 struct drm_backend *b = to_drm_backend(base->compositor);
3109
Daniel Stonef30a18c2017-04-04 17:54:31 +01003110 /* output->fb_last and output->fb_pending must not be set here;
Daniel Stone6e7a9612017-04-04 17:54:26 +01003111 * destroy_pending/disable_pending exist to guarantee exactly this. */
Daniel Stonef30a18c2017-04-04 17:54:31 +01003112 assert(!output->fb_last);
Daniel Stone5bb8f582017-04-04 17:54:28 +01003113 assert(!output->fb_pending);
3114 drm_fb_unref(output->fb_current);
3115 output->fb_current = NULL;
Daniel Stone6e7a9612017-04-04 17:54:26 +01003116
Daniel Stone3e661f72016-11-04 17:24:06 +00003117 if (b->use_pixman)
Armin Krezović08368132016-09-30 14:11:05 +02003118 drm_output_fini_pixman(output);
Daniel Stone3e661f72016-11-04 17:24:06 +00003119 else
3120 drm_output_fini_egl(output);
Armin Krezović08368132016-09-30 14:11:05 +02003121
Daniel Stone4202e502016-10-24 13:26:42 +01003122 weston_plane_release(&output->scanout_plane);
Armin Krezović08368132016-09-30 14:11:05 +02003123 weston_plane_release(&output->cursor_plane);
3124
Armin Krezović08368132016-09-30 14:11:05 +02003125 /* Turn off hardware cursor */
3126 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
3127}
3128
3129static void
3130drm_output_destroy(struct weston_output *base)
3131{
3132 struct drm_output *output = to_drm_output(base);
3133 struct drm_backend *b = to_drm_backend(base->compositor);
Armin Krezović445b41b2016-10-09 23:48:16 +02003134 struct drm_mode *drm_mode, *next;
Armin Krezović08368132016-09-30 14:11:05 +02003135 drmModeCrtcPtr origcrtc = output->original_crtc;
3136
3137 if (output->page_flip_pending) {
3138 output->destroy_pending = 1;
3139 weston_log("destroy output while page flip pending\n");
3140 return;
3141 }
3142
3143 if (output->base.enabled)
3144 drm_output_deinit(&output->base);
3145
Armin Krezović445b41b2016-10-09 23:48:16 +02003146 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
3147 base.link) {
3148 wl_list_remove(&drm_mode->base.link);
3149 free(drm_mode);
3150 }
3151
Armin Krezović08368132016-09-30 14:11:05 +02003152 if (origcrtc) {
3153 /* Restore original CRTC state */
3154 drmModeSetCrtc(b->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
3155 origcrtc->x, origcrtc->y,
3156 &output->connector_id, 1, &origcrtc->mode);
3157 drmModeFreeCrtc(origcrtc);
3158 }
3159
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00003160 if (output->pageflip_timer)
3161 wl_event_source_remove(output->pageflip_timer);
3162
Armin Krezović08368132016-09-30 14:11:05 +02003163 weston_output_destroy(&output->base);
3164
Daniel Stone02cf4662017-03-03 16:19:39 +00003165 drm_property_info_free(output->props_conn, WDRM_CONNECTOR__COUNT);
3166
Armin Krezović08368132016-09-30 14:11:05 +02003167 drmModeFreeConnector(output->connector);
3168
3169 if (output->backlight)
3170 backlight_destroy(output->backlight);
3171
Armin Krezović08368132016-09-30 14:11:05 +02003172 free(output);
3173}
3174
3175static int
3176drm_output_disable(struct weston_output *base)
3177{
3178 struct drm_output *output = to_drm_output(base);
3179 struct drm_backend *b = to_drm_backend(base->compositor);
3180
3181 if (output->page_flip_pending) {
3182 output->disable_pending = 1;
3183 return -1;
3184 }
3185
3186 if (output->base.enabled)
3187 drm_output_deinit(&output->base);
3188
3189 output->disable_pending = 0;
3190
3191 weston_log("Disabling output %s\n", output->base.name);
3192 drmModeSetCrtc(b->drm.fd, output->crtc_id,
3193 0, 0, 0, 0, 0, NULL);
3194
3195 return 0;
3196}
3197
3198/**
3199 * Create a Weston output structure
3200 *
3201 * Given a DRM connector, create a matching drm_output structure and add it
3202 * to Weston's output list. It also takes ownership of the connector, which
3203 * is released when output is destroyed.
3204 *
3205 * @param b Weston backend structure
3206 * @param resources DRM resources for this device
3207 * @param connector DRM connector to use for this new output
3208 * @param drm_device udev device pointer
3209 * @returns 0 on success, or -1 on failure
3210 */
3211static int
3212create_output_for_connector(struct drm_backend *b,
3213 drmModeRes *resources,
3214 drmModeConnector *connector,
3215 struct udev_device *drm_device)
3216{
3217 struct drm_output *output;
Daniel Stone02cf4662017-03-03 16:19:39 +00003218 drmModeObjectPropertiesPtr props;
Armin Krezović445b41b2016-10-09 23:48:16 +02003219 struct drm_mode *drm_mode;
Armin Krezović08368132016-09-30 14:11:05 +02003220 int i;
3221
Daniel Stone02cf4662017-03-03 16:19:39 +00003222 static const struct drm_property_info connector_props[] = {
3223 [WDRM_CONNECTOR_EDID] = { .name = "EDID" },
3224 [WDRM_CONNECTOR_DPMS] = { .name = "DPMS" },
3225 };
3226
Armin Krezović08368132016-09-30 14:11:05 +02003227 i = find_crtc_for_connector(b, resources, connector);
3228 if (i < 0) {
3229 weston_log("No usable crtc/encoder pair for connector.\n");
Armin Krezović445b41b2016-10-09 23:48:16 +02003230 goto err;
Armin Krezović08368132016-09-30 14:11:05 +02003231 }
3232
3233 output = zalloc(sizeof *output);
3234 if (output == NULL)
Armin Krezović445b41b2016-10-09 23:48:16 +02003235 goto err;
Armin Krezović08368132016-09-30 14:11:05 +02003236
3237 output->connector = connector;
3238 output->crtc_id = resources->crtcs[i];
3239 output->pipe = i;
3240 output->connector_id = connector->connector_id;
3241
3242 output->backlight = backlight_init(drm_device,
3243 connector->connector_type);
3244
Armin Krezović445b41b2016-10-09 23:48:16 +02003245 output->original_crtc = drmModeGetCrtc(b->drm.fd, output->crtc_id);
3246
Armin Krezović08368132016-09-30 14:11:05 +02003247 output->base.enable = drm_output_enable;
3248 output->base.destroy = drm_output_destroy;
3249 output->base.disable = drm_output_disable;
3250 output->base.name = make_connector_name(connector);
3251
3252 output->destroy_pending = 0;
3253 output->disable_pending = 0;
Armin Krezović08368132016-09-30 14:11:05 +02003254
Daniel Stone02cf4662017-03-03 16:19:39 +00003255 props = drmModeObjectGetProperties(b->drm.fd, connector->connector_id,
3256 DRM_MODE_OBJECT_CONNECTOR);
3257 if (!props) {
3258 weston_log("failed to get connector properties\n");
3259 goto err;
3260 }
3261 drm_property_info_populate(b, connector_props, output->props_conn,
3262 WDRM_CONNECTOR__COUNT, props);
3263 find_and_parse_output_edid(b, output, props);
3264 drmModeFreeObjectProperties(props);
3265
Armin Krezović40087402016-09-30 14:11:12 +02003266 weston_output_init(&output->base, b->compositor);
Armin Krezović445b41b2016-10-09 23:48:16 +02003267
3268 wl_list_init(&output->base.mode_list);
3269
3270 for (i = 0; i < output->connector->count_modes; i++) {
3271 drm_mode = drm_output_add_mode(output, &output->connector->modes[i]);
3272 if (!drm_mode) {
3273 drm_output_destroy(&output->base);
3274 return -1;
3275 }
3276 }
3277
Armin Krezović08368132016-09-30 14:11:05 +02003278 weston_compositor_add_pending_output(&output->base, b->compositor);
3279
3280 return 0;
Armin Krezović445b41b2016-10-09 23:48:16 +02003281
3282err:
3283 drmModeFreeConnector(connector);
3284
3285 return -1;
Armin Krezović08368132016-09-30 14:11:05 +02003286}
3287
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003288static int
Ucan, Emre (ADITG/SW1)9a200d72017-02-02 14:06:56 +00003289create_outputs(struct drm_backend *b, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003290{
3291 drmModeConnector *connector;
3292 drmModeRes *resources;
3293 int i;
3294
Giulio Camuffo954f1832014-10-11 18:27:30 +03003295 resources = drmModeGetResources(b->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003296 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02003297 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003298 return -1;
3299 }
3300
Giulio Camuffo954f1832014-10-11 18:27:30 +03003301 b->min_width = resources->min_width;
3302 b->max_width = resources->max_width;
3303 b->min_height = resources->min_height;
3304 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05003305
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003306 for (i = 0; i < resources->count_connectors; i++) {
Daniel Stone02cf4662017-03-03 16:19:39 +00003307 int ret;
3308
Giulio Camuffo954f1832014-10-11 18:27:30 +03003309 connector = drmModeGetConnector(b->drm.fd,
Benjamin Franzke117483d2011-08-30 11:38:26 +02003310 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003311 if (connector == NULL)
3312 continue;
3313
3314 if (connector->connection == DRM_MODE_CONNECTED &&
Ucan, Emre (ADITG/SW1)9a200d72017-02-02 14:06:56 +00003315 (b->connector == 0 ||
3316 connector->connector_id == b->connector)) {
Daniel Stone02cf4662017-03-03 16:19:39 +00003317 ret = create_output_for_connector(b, resources,
3318 connector, drm_device);
3319 if (ret < 0)
3320 weston_log("failed to create new connector\n");
Armin Krezović08368132016-09-30 14:11:05 +02003321 } else {
3322 drmModeFreeConnector(connector);
Benjamin Franzke9eaee352011-08-02 13:03:54 +02003323 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003324 }
3325
Armin Krezović08368132016-09-30 14:11:05 +02003326 if (wl_list_empty(&b->compositor->output_list) &&
3327 wl_list_empty(&b->compositor->pending_output_list))
Martin Minarik6d118362012-06-07 18:01:59 +02003328 weston_log("No currently active connector found.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003329
3330 drmModeFreeResources(resources);
3331
3332 return 0;
3333}
3334
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003335static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03003336update_outputs(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003337{
3338 drmModeConnector *connector;
3339 drmModeRes *resources;
3340 struct drm_output *output, *next;
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003341 uint32_t *connected;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003342 int i;
3343
Giulio Camuffo954f1832014-10-11 18:27:30 +03003344 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003345 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02003346 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003347 return;
3348 }
3349
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003350 connected = calloc(resources->count_connectors, sizeof(uint32_t));
3351 if (!connected) {
3352 drmModeFreeResources(resources);
3353 return;
3354 }
3355
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003356 /* collect new connects */
3357 for (i = 0; i < resources->count_connectors; i++) {
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +00003358 uint32_t connector_id = resources->connectors[i];
Benjamin Franzke117483d2011-08-30 11:38:26 +02003359
Giulio Camuffo954f1832014-10-11 18:27:30 +03003360 connector = drmModeGetConnector(b->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01003361 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003362 continue;
3363
David Herrmann7551cff2011-12-08 17:05:43 +01003364 if (connector->connection != DRM_MODE_CONNECTED) {
3365 drmModeFreeConnector(connector);
3366 continue;
3367 }
3368
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +00003369 if (b->connector && (b->connector != connector_id)) {
3370 drmModeFreeConnector(connector);
3371 continue;
3372 }
3373
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003374 connected[i] = connector_id;
Benjamin Franzke117483d2011-08-30 11:38:26 +02003375
Daniel Stonec0ec7592017-02-09 13:58:35 +00003376 if (drm_output_find_by_connector(b, connector_id)) {
Armin Krezović08368132016-09-30 14:11:05 +02003377 drmModeFreeConnector(connector);
Daniel Stonec0ec7592017-02-09 13:58:35 +00003378 continue;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003379 }
Daniel Stonec0ec7592017-02-09 13:58:35 +00003380
3381 create_output_for_connector(b, resources,
3382 connector, drm_device);
3383 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003384 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003385
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003386 wl_list_for_each_safe(output, next, &b->compositor->output_list,
3387 base.link) {
3388 bool disconnected = true;
3389
3390 for (i = 0; i < resources->count_connectors; i++) {
3391 if (connected[i] == output->connector_id) {
3392 disconnected = false;
3393 break;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003394 }
3395 }
Armin Krezović08368132016-09-30 14:11:05 +02003396
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003397 if (!disconnected)
3398 continue;
3399
3400 weston_log("connector %d disconnected\n", output->connector_id);
3401 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003402 }
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003403
3404 wl_list_for_each_safe(output, next, &b->compositor->pending_output_list,
3405 base.link) {
3406 bool disconnected = true;
3407
3408 for (i = 0; i < resources->count_connectors; i++) {
3409 if (connected[i] == output->connector_id) {
3410 disconnected = false;
3411 break;
3412 }
3413 }
3414
3415 if (!disconnected)
3416 continue;
3417
3418 weston_log("connector %d disconnected\n", output->connector_id);
3419 drm_output_destroy(&output->base);
3420 }
3421
3422 free(connected);
3423 drmModeFreeResources(resources);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003424}
3425
3426static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003427udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003428{
David Herrmannd7488c22012-03-11 20:05:21 +01003429 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01003430 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01003431
3432 sysnum = udev_device_get_sysnum(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003433 if (!sysnum || atoi(sysnum) != b->drm.id)
David Herrmannd7488c22012-03-11 20:05:21 +01003434 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02003435
David Herrmann6ac52db2012-03-11 20:05:22 +01003436 val = udev_device_get_property_value(device, "HOTPLUG");
3437 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003438 return 0;
3439
David Herrmann6ac52db2012-03-11 20:05:22 +01003440 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003441}
3442
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003443static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003444udev_drm_event(int fd, uint32_t mask, void *data)
3445{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003446 struct drm_backend *b = data;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003447 struct udev_device *event;
3448
Giulio Camuffo954f1832014-10-11 18:27:30 +03003449 event = udev_monitor_receive_device(b->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02003450
Giulio Camuffo954f1832014-10-11 18:27:30 +03003451 if (udev_event_is_hotplug(b, event))
3452 update_outputs(b, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003453
3454 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003455
3456 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003457}
3458
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003459static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003460drm_restore(struct weston_compositor *ec)
3461{
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003462 weston_launcher_restore(ec->launcher);
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003463}
3464
3465static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003466drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003467{
Armin Krezović545dba62016-08-05 15:54:18 +02003468 struct drm_backend *b = to_drm_backend(ec);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003469
Giulio Camuffo954f1832014-10-11 18:27:30 +03003470 udev_input_destroy(&b->input);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003471
Giulio Camuffo954f1832014-10-11 18:27:30 +03003472 wl_event_source_remove(b->udev_drm_source);
3473 wl_event_source_remove(b->drm_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003474
Giulio Camuffo954f1832014-10-11 18:27:30 +03003475 destroy_sprites(b);
Kristian Høgsberg3d64a3e2013-05-10 12:36:04 -04003476
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03003477 weston_compositor_shutdown(ec);
3478
Giulio Camuffo954f1832014-10-11 18:27:30 +03003479 if (b->gbm)
3480 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003481
Giulio Camuffo954f1832014-10-11 18:27:30 +03003482 weston_launcher_destroy(ec->launcher);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003483
Giulio Camuffo954f1832014-10-11 18:27:30 +03003484 close(b->drm.fd);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003485 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003486}
3487
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003488static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003489session_notify(struct wl_listener *listener, void *data)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003490{
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003491 struct weston_compositor *compositor = data;
Armin Krezović545dba62016-08-05 15:54:18 +02003492 struct drm_backend *b = to_drm_backend(compositor);
Daniel Stone08d4edf2017-04-04 17:54:34 +01003493 struct drm_plane *sprite;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003494 struct drm_output *output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003495
Giulio Camuffo954f1832014-10-11 18:27:30 +03003496 if (compositor->session_active) {
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003497 weston_log("activating session\n");
Daniel Stonef33e1042016-11-05 08:10:13 +00003498 weston_compositor_wake(compositor);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003499 weston_compositor_damage_all(compositor);
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03003500
3501 wl_list_for_each(output, &compositor->output_list, base.link)
3502 output->state_invalid = true;
3503
Giulio Camuffo954f1832014-10-11 18:27:30 +03003504 udev_input_enable(&b->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003505 } else {
3506 weston_log("deactivating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03003507 udev_input_disable(&b->input);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04003508
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003509 weston_compositor_offscreen(compositor);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04003510
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05003511 /* If we have a repaint scheduled (either from a
3512 * pending pageflip or the idle handler), make sure we
3513 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003514 * vt switched away. The OFFSCREEN state will prevent
Abdur Rehman4dca0e12017-01-01 19:46:35 +05003515 * further attempts at repainting. When we switch
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05003516 * back, we schedule a repaint, which will process
3517 * pending frame callbacks. */
3518
Giulio Camuffo954f1832014-10-11 18:27:30 +03003519 wl_list_for_each(output, &compositor->output_list, base.link) {
Daniel Stone09a97e22017-03-01 11:34:06 +00003520 output->base.repaint_needed = false;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003521 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05003522 }
3523
Giulio Camuffo954f1832014-10-11 18:27:30 +03003524 output = container_of(compositor->output_list.next,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003525 struct drm_output, base.link);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05003526
Giulio Camuffo954f1832014-10-11 18:27:30 +03003527 wl_list_for_each(sprite, &b->sprite_list, link)
3528 drmModeSetPlane(b->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05003529 sprite->plane_id,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003530 output->crtc_id, 0, 0,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05003531 0, 0, 0, 0, 0, 0, 0, 0);
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003532 };
3533}
3534
Daniel Stoneefa504f2016-12-19 16:48:20 +00003535/**
3536 * Determines whether or not a device is capable of modesetting. If successful,
3537 * sets b->drm.fd and b->drm.filename to the opened device.
3538 */
3539static bool
3540drm_device_is_kms(struct drm_backend *b, struct udev_device *device)
3541{
3542 const char *filename = udev_device_get_devnode(device);
3543 const char *sysnum = udev_device_get_sysnum(device);
3544 drmModeRes *res;
3545 int id, fd;
3546
3547 if (!filename)
3548 return false;
3549
3550 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
3551 if (fd < 0)
3552 return false;
3553
3554 res = drmModeGetResources(fd);
3555 if (!res)
3556 goto out_fd;
3557
3558 if (res->count_crtcs <= 0 || res->count_connectors <= 0 ||
3559 res->count_encoders <= 0)
3560 goto out_res;
3561
3562 if (sysnum)
3563 id = atoi(sysnum);
3564 if (!sysnum || id < 0) {
3565 weston_log("couldn't get sysnum for device %s\n", filename);
3566 goto out_res;
3567 }
3568
3569 /* We can be called successfully on multiple devices; if we have,
3570 * clean up old entries. */
3571 if (b->drm.fd >= 0)
3572 weston_launcher_close(b->compositor->launcher, b->drm.fd);
3573 free(b->drm.filename);
3574
3575 b->drm.fd = fd;
3576 b->drm.id = id;
3577 b->drm.filename = strdup(filename);
3578
Sergi Granellceb59812017-03-28 12:44:04 +02003579 drmModeFreeResources(res);
3580
Daniel Stoneefa504f2016-12-19 16:48:20 +00003581 return true;
3582
3583out_res:
3584 drmModeFreeResources(res);
3585out_fd:
3586 weston_launcher_close(b->compositor->launcher, fd);
3587 return false;
3588}
3589
David Herrmann0af066f2012-10-29 19:21:16 +01003590/*
3591 * Find primary GPU
3592 * Some systems may have multiple DRM devices attached to a single seat. This
3593 * function loops over all devices and tries to find a PCI device with the
3594 * boot_vga sysfs attribute set to 1.
3595 * If no such device is found, the first DRM device reported by udev is used.
Daniel Stoneefa504f2016-12-19 16:48:20 +00003596 * Devices are also vetted to make sure they are are capable of modesetting,
3597 * rather than pure render nodes (GPU with no display), or pure
3598 * memory-allocation devices (VGEM).
David Herrmann0af066f2012-10-29 19:21:16 +01003599 */
3600static struct udev_device*
Giulio Camuffo954f1832014-10-11 18:27:30 +03003601find_primary_gpu(struct drm_backend *b, const char *seat)
David Herrmann0af066f2012-10-29 19:21:16 +01003602{
3603 struct udev_enumerate *e;
3604 struct udev_list_entry *entry;
3605 const char *path, *device_seat, *id;
3606 struct udev_device *device, *drm_device, *pci;
3607
Giulio Camuffo954f1832014-10-11 18:27:30 +03003608 e = udev_enumerate_new(b->udev);
David Herrmann0af066f2012-10-29 19:21:16 +01003609 udev_enumerate_add_match_subsystem(e, "drm");
3610 udev_enumerate_add_match_sysname(e, "card[0-9]*");
3611
3612 udev_enumerate_scan_devices(e);
3613 drm_device = NULL;
3614 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Daniel Stoneefa504f2016-12-19 16:48:20 +00003615 bool is_boot_vga = false;
3616
David Herrmann0af066f2012-10-29 19:21:16 +01003617 path = udev_list_entry_get_name(entry);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003618 device = udev_device_new_from_syspath(b->udev, path);
David Herrmann0af066f2012-10-29 19:21:16 +01003619 if (!device)
3620 continue;
3621 device_seat = udev_device_get_property_value(device, "ID_SEAT");
3622 if (!device_seat)
3623 device_seat = default_seat;
3624 if (strcmp(device_seat, seat)) {
3625 udev_device_unref(device);
3626 continue;
3627 }
3628
3629 pci = udev_device_get_parent_with_subsystem_devtype(device,
3630 "pci", NULL);
3631 if (pci) {
3632 id = udev_device_get_sysattr_value(pci, "boot_vga");
Daniel Stoneefa504f2016-12-19 16:48:20 +00003633 if (id && !strcmp(id, "1"))
3634 is_boot_vga = true;
David Herrmann0af066f2012-10-29 19:21:16 +01003635 }
3636
Daniel Stoneefa504f2016-12-19 16:48:20 +00003637 /* If we already have a modesetting-capable device, and this
3638 * device isn't our boot-VGA device, we aren't going to use
3639 * it. */
3640 if (!is_boot_vga && drm_device) {
David Herrmann0af066f2012-10-29 19:21:16 +01003641 udev_device_unref(device);
Daniel Stoneefa504f2016-12-19 16:48:20 +00003642 continue;
3643 }
3644
3645 /* Make sure this device is actually capable of modesetting;
3646 * if this call succeeds, b->drm.{fd,filename} will be set,
3647 * and any old values freed. */
3648 if (!drm_device_is_kms(b, device)) {
3649 udev_device_unref(device);
3650 continue;
3651 }
3652
3653 /* There can only be one boot_vga device, and we try to use it
3654 * at all costs. */
3655 if (is_boot_vga) {
3656 if (drm_device)
3657 udev_device_unref(drm_device);
3658 drm_device = device;
3659 break;
3660 }
3661
3662 /* Per the (!is_boot_vga && drm_device) test above, we only
3663 * trump existing saved devices with boot-VGA devices, so if
3664 * we end up here, this must be the first device we've seen. */
3665 assert(!drm_device);
3666 drm_device = device;
David Herrmann0af066f2012-10-29 19:21:16 +01003667 }
3668
Daniel Stoneefa504f2016-12-19 16:48:20 +00003669 /* If we're returning a device to use, we must have an open FD for
3670 * it. */
3671 assert(!!drm_device == (b->drm.fd >= 0));
3672
David Herrmann0af066f2012-10-29 19:21:16 +01003673 udev_enumerate_unref(e);
3674 return drm_device;
3675}
3676
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003677static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003678planes_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
3679 void *data)
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003680{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003681 struct drm_backend *b = data;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003682
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003683 switch (key) {
3684 case KEY_C:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003685 b->cursors_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003686 break;
3687 case KEY_V:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003688 b->sprites_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003689 break;
3690 case KEY_O:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003691 b->sprites_hidden ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003692 break;
3693 default:
3694 break;
3695 }
Ander Conselvan de Oliveira180f42a2012-11-21 15:11:37 +02003696}
3697
Kristian Høgsberg0eac34a2013-08-30 14:28:22 -07003698#ifdef BUILD_VAAPI_RECORDER
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003699static void
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003700recorder_destroy(struct drm_output *output)
3701{
3702 vaapi_recorder_destroy(output->recorder);
3703 output->recorder = NULL;
3704
3705 output->base.disable_planes--;
3706
3707 wl_list_remove(&output->recorder_frame_listener.link);
3708 weston_log("[libva recorder] done\n");
3709}
3710
3711static void
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003712recorder_frame_notify(struct wl_listener *listener, void *data)
3713{
3714 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003715 struct drm_backend *b;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003716 int fd, ret;
3717
3718 output = container_of(listener, struct drm_output,
3719 recorder_frame_listener);
Armin Krezović545dba62016-08-05 15:54:18 +02003720 b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003721
3722 if (!output->recorder)
3723 return;
3724
Daniel Stone5bb8f582017-04-04 17:54:28 +01003725 ret = drmPrimeHandleToFD(b->drm.fd, output->fb_current->handle,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003726 DRM_CLOEXEC, &fd);
3727 if (ret) {
3728 weston_log("[libva recorder] "
3729 "failed to create prime fd for front buffer\n");
3730 return;
3731 }
3732
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003733 ret = vaapi_recorder_frame(output->recorder, fd,
Daniel Stone5bb8f582017-04-04 17:54:28 +01003734 output->fb_current->stride);
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003735 if (ret < 0) {
3736 weston_log("[libva recorder] aborted: %m\n");
3737 recorder_destroy(output);
3738 }
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003739}
3740
3741static void *
Giulio Camuffo954f1832014-10-11 18:27:30 +03003742create_recorder(struct drm_backend *b, int width, int height,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003743 const char *filename)
3744{
3745 int fd;
3746 drm_magic_t magic;
3747
Giulio Camuffo954f1832014-10-11 18:27:30 +03003748 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003749 if (fd < 0)
3750 return NULL;
3751
3752 drmGetMagic(fd, &magic);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003753 drmAuthMagic(b->drm.fd, magic);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003754
3755 return vaapi_recorder_create(fd, width, height, filename);
3756}
3757
3758static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003759recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003760 void *data)
3761{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003762 struct drm_backend *b = data;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003763 struct drm_output *output;
3764 int width, height;
3765
Giulio Camuffo954f1832014-10-11 18:27:30 +03003766 output = container_of(b->compositor->output_list.next,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003767 struct drm_output, base.link);
3768
3769 if (!output->recorder) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01003770 if (output->gbm_format != GBM_FORMAT_XRGB8888) {
Ander Conselvan de Oliveira2ef1cd12014-05-06 16:49:06 +03003771 weston_log("failed to start vaapi recorder: "
3772 "output format not supported\n");
3773 return;
3774 }
3775
Hardeningff39efa2013-09-18 23:56:35 +02003776 width = output->base.current_mode->width;
3777 height = output->base.current_mode->height;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003778
3779 output->recorder =
Giulio Camuffo954f1832014-10-11 18:27:30 +03003780 create_recorder(b, width, height, "capture.h264");
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003781 if (!output->recorder) {
3782 weston_log("failed to create vaapi recorder\n");
3783 return;
3784 }
3785
3786 output->base.disable_planes++;
3787
3788 output->recorder_frame_listener.notify = recorder_frame_notify;
3789 wl_signal_add(&output->base.frame_signal,
3790 &output->recorder_frame_listener);
3791
3792 weston_output_schedule_repaint(&output->base);
3793
3794 weston_log("[libva recorder] initialized\n");
3795 } else {
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003796 recorder_destroy(output);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003797 }
3798}
3799#else
3800static void
Bryce Harrington4a8a3a12015-07-16 19:12:26 -07003801recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003802 void *data)
3803{
3804 weston_log("Compiled without libva support\n");
3805}
3806#endif
3807
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003808static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03003809switch_to_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003810{
3811 struct drm_output *output;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003812 bool dmabuf_support_inited;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003813
Giulio Camuffo954f1832014-10-11 18:27:30 +03003814 if (!b->use_pixman)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003815 return;
3816
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003817 dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf;
3818
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003819 weston_log("Switching to GL renderer\n");
3820
Giulio Camuffo954f1832014-10-11 18:27:30 +03003821 b->gbm = create_gbm_device(b->drm.fd);
3822 if (!b->gbm) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003823 weston_log("Failed to create gbm device. "
3824 "Aborting renderer switch\n");
3825 return;
3826 }
3827
Giulio Camuffo954f1832014-10-11 18:27:30 +03003828 wl_list_for_each(output, &b->compositor->output_list, base.link)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003829 pixman_renderer_output_destroy(&output->base);
3830
Giulio Camuffo954f1832014-10-11 18:27:30 +03003831 b->compositor->renderer->destroy(b->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003832
Giulio Camuffo954f1832014-10-11 18:27:30 +03003833 if (drm_backend_create_gl_renderer(b) < 0) {
3834 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003835 weston_log("Failed to create GL renderer. Quitting.\n");
3836 /* FIXME: we need a function to shutdown cleanly */
3837 assert(0);
3838 }
3839
Giulio Camuffo954f1832014-10-11 18:27:30 +03003840 wl_list_for_each(output, &b->compositor->output_list, base.link)
3841 drm_output_init_egl(output, b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003842
Giulio Camuffo954f1832014-10-11 18:27:30 +03003843 b->use_pixman = 0;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003844
3845 if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) {
3846 if (linux_dmabuf_setup(b->compositor) < 0)
3847 weston_log("Error: initializing dmabuf "
3848 "support failed.\n");
3849 }
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003850}
3851
3852static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003853renderer_switch_binding(struct weston_keyboard *keyboard, uint32_t time,
3854 uint32_t key, void *data)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003855{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003856 struct drm_backend *b =
Armin Krezović545dba62016-08-05 15:54:18 +02003857 to_drm_backend(keyboard->seat->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003858
Giulio Camuffo954f1832014-10-11 18:27:30 +03003859 switch_to_gl_renderer(b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003860}
3861
Armin Krezović08368132016-09-30 14:11:05 +02003862static const struct weston_drm_output_api api = {
3863 drm_output_set_mode,
3864 drm_output_set_gbm_format,
3865 drm_output_set_seat,
3866};
3867
Giulio Camuffo954f1832014-10-11 18:27:30 +03003868static struct drm_backend *
3869drm_backend_create(struct weston_compositor *compositor,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003870 struct weston_drm_backend_config *config)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003871{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003872 struct drm_backend *b;
David Herrmann0af066f2012-10-29 19:21:16 +01003873 struct udev_device *drm_device;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003874 struct wl_event_loop *loop;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003875 const char *seat_id = default_seat;
Armin Krezović08368132016-09-30 14:11:05 +02003876 int ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003877
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003878 weston_log("initializing drm backend\n");
3879
Giulio Camuffo954f1832014-10-11 18:27:30 +03003880 b = zalloc(sizeof *b);
3881 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003882 return NULL;
Daniel Stone725c2c32012-06-22 14:04:36 +01003883
Daniel Stoneefa504f2016-12-19 16:48:20 +00003884 b->drm.fd = -1;
3885
Pekka Paalanen68583832015-05-19 09:53:16 +03003886 /*
3887 * KMS support for hardware planes cannot properly synchronize
3888 * without nuclear page flip. Without nuclear/atomic, hw plane
3889 * and cursor plane updates would either tear or cause extra
3890 * waits for vblanks which means dropping the compositor framerate
Pekka Paalanen3f32a132015-09-07 15:38:43 +03003891 * to a fraction. For cursors, it's not so bad, so they are
3892 * enabled.
Pekka Paalanen68583832015-05-19 09:53:16 +03003893 *
3894 * These can be enabled again when nuclear/atomic support lands.
3895 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003896 b->sprites_are_broken = 1;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003897 b->compositor = compositor;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003898 b->use_pixman = config->use_pixman;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00003899 b->pageflip_timeout = config->pageflip_timeout;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003900
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003901 if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0)
3902 goto err_compositor;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003903
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003904 if (config->seat_id)
3905 seat_id = config->seat_id;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003906
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003907 /* Check if we run drm-backend using weston-launch */
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003908 compositor->launcher = weston_launcher_connect(compositor, config->tty,
3909 seat_id, true);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003910 if (compositor->launcher == NULL) {
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003911 weston_log("fatal: drm backend should be run "
Kristian Høgsbergb76237e2013-04-04 21:36:20 -04003912 "using weston-launch binary or as root\n");
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003913 goto err_compositor;
3914 }
3915
Giulio Camuffo954f1832014-10-11 18:27:30 +03003916 b->udev = udev_new();
3917 if (b->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003918 weston_log("failed to initialize udev context\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003919 goto err_launcher;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003920 }
3921
Giulio Camuffo954f1832014-10-11 18:27:30 +03003922 b->session_listener.notify = session_notify;
3923 wl_signal_add(&compositor->session_signal, &b->session_listener);
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05003924
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003925 drm_device = find_primary_gpu(b, seat_id);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04003926 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003927 weston_log("no drm device found\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003928 goto err_udev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003929 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003930
Daniel Stoneefa504f2016-12-19 16:48:20 +00003931 if (init_kms_caps(b) < 0) {
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003932 weston_log("failed to initialize kms\n");
3933 goto err_udev_dev;
3934 }
3935
Giulio Camuffo954f1832014-10-11 18:27:30 +03003936 if (b->use_pixman) {
3937 if (init_pixman(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003938 weston_log("failed to initialize pixman renderer\n");
3939 goto err_udev_dev;
3940 }
3941 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +03003942 if (init_egl(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003943 weston_log("failed to initialize egl\n");
3944 goto err_udev_dev;
3945 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003946 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05003947
Giulio Camuffo954f1832014-10-11 18:27:30 +03003948 b->base.destroy = drm_destroy;
3949 b->base.restore = drm_restore;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02003950
Bob Ham91880f12016-01-12 10:21:47 +00003951 weston_setup_vt_switch_bindings(compositor);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04003952
Giulio Camuffo954f1832014-10-11 18:27:30 +03003953 wl_list_init(&b->sprite_list);
3954 create_sprites(b);
Jesse Barnes58ef3792012-02-23 09:45:49 -05003955
Giulio Camuffo954f1832014-10-11 18:27:30 +03003956 if (udev_input_init(&b->input,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03003957 compositor, b->udev, seat_id,
3958 config->configure_device) < 0) {
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003959 weston_log("failed to create input devices\n");
3960 goto err_sprite;
3961 }
3962
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +00003963 b->connector = config->connector;
3964
Ucan, Emre (ADITG/SW1)9a200d72017-02-02 14:06:56 +00003965 if (create_outputs(b, drm_device) < 0) {
Daniel Stoneefa504f2016-12-19 16:48:20 +00003966 weston_log("failed to create output for %s\n", b->drm.filename);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003967 goto err_udev_input;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003968 }
3969
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003970 /* A this point we have some idea of whether or not we have a working
3971 * cursor plane. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003972 if (!b->cursors_are_broken)
3973 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003974
Giulio Camuffo954f1832014-10-11 18:27:30 +03003975 loop = wl_display_get_event_loop(compositor->wl_display);
3976 b->drm_source =
3977 wl_event_loop_add_fd(loop, b->drm.fd,
3978 WL_EVENT_READABLE, on_drm_input, b);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003979
Giulio Camuffo954f1832014-10-11 18:27:30 +03003980 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
3981 if (b->udev_monitor == NULL) {
Abdur Rehman4dca0e12017-01-01 19:46:35 +05003982 weston_log("failed to initialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003983 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003984 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03003985 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003986 "drm", NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003987 b->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02003988 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03003989 udev_monitor_get_fd(b->udev_monitor),
3990 WL_EVENT_READABLE, udev_drm_event, b);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003991
Giulio Camuffo954f1832014-10-11 18:27:30 +03003992 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003993 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003994 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003995 }
3996
Daniel Stonea96b93c2012-06-22 14:04:37 +01003997 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003998
Giulio Camuffo954f1832014-10-11 18:27:30 +03003999 weston_compositor_add_debug_binding(compositor, KEY_O,
4000 planes_binding, b);
4001 weston_compositor_add_debug_binding(compositor, KEY_C,
4002 planes_binding, b);
4003 weston_compositor_add_debug_binding(compositor, KEY_V,
4004 planes_binding, b);
4005 weston_compositor_add_debug_binding(compositor, KEY_Q,
4006 recorder_binding, b);
4007 weston_compositor_add_debug_binding(compositor, KEY_W,
4008 renderer_switch_binding, b);
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02004009
Pekka Paalanene4d231e2014-06-12 15:12:48 +03004010 if (compositor->renderer->import_dmabuf) {
4011 if (linux_dmabuf_setup(compositor) < 0)
4012 weston_log("Error: initializing dmabuf "
4013 "support failed.\n");
4014 }
4015
Giulio Camuffo954f1832014-10-11 18:27:30 +03004016 compositor->backend = &b->base;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03004017
Armin Krezović08368132016-09-30 14:11:05 +02004018 ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME,
4019 &api, sizeof(api));
4020
4021 if (ret < 0) {
4022 weston_log("Failed to register output API.\n");
4023 goto err_udev_monitor;
4024 }
4025
Giulio Camuffo954f1832014-10-11 18:27:30 +03004026 return b;
Daniel Stonea96b93c2012-06-22 14:04:37 +01004027
4028err_udev_monitor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03004029 wl_event_source_remove(b->udev_drm_source);
4030 udev_monitor_unref(b->udev_monitor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01004031err_drm_source:
Giulio Camuffo954f1832014-10-11 18:27:30 +03004032 wl_event_source_remove(b->drm_source);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03004033err_udev_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03004034 udev_input_destroy(&b->input);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04004035err_sprite:
Emmanuel Gil Peyrotb8347e32016-05-02 22:40:13 +01004036 if (b->gbm)
4037 gbm_device_destroy(b->gbm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03004038 destroy_sprites(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01004039err_udev_dev:
4040 udev_device_unref(drm_device);
Kristian Høgsberg3f495872013-09-18 23:00:17 -07004041err_launcher:
Giulio Camuffo954f1832014-10-11 18:27:30 +03004042 weston_launcher_destroy(compositor->launcher);
Daniel Stonea96b93c2012-06-22 14:04:37 +01004043err_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +03004044 udev_unref(b->udev);
Daniel Stonea96b93c2012-06-22 14:04:37 +01004045err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03004046 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03004047 free(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01004048 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004049}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004050
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004051static void
4052config_init_to_defaults(struct weston_drm_backend_config *config)
4053{
4054}
4055
Giulio Camuffo954f1832014-10-11 18:27:30 +03004056WL_EXPORT int
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01004057weston_backend_init(struct weston_compositor *compositor,
4058 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004059{
Giulio Camuffo954f1832014-10-11 18:27:30 +03004060 struct drm_backend *b;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004061 struct weston_drm_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004062
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004063 if (config_base == NULL ||
4064 config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION ||
4065 config_base->struct_size > sizeof(struct weston_drm_backend_config)) {
4066 weston_log("drm backend config structure is invalid\n");
4067 return -1;
4068 }
Benjamin Franzke117483d2011-08-30 11:38:26 +02004069
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004070 config_init_to_defaults(&config);
4071 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07004072
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004073 b = drm_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03004074 if (b == NULL)
4075 return -1;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004076
Giulio Camuffo954f1832014-10-11 18:27:30 +03004077 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004078}