blob: 4089afc418401cbfff64331328b786af0c2e738c [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
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -030070#ifndef DRM_CAP_CURSOR_WIDTH
71#define DRM_CAP_CURSOR_WIDTH 0x8
72#endif
73
74#ifndef DRM_CAP_CURSOR_HEIGHT
75#define DRM_CAP_CURSOR_HEIGHT 0x9
76#endif
77
78#ifndef GBM_BO_USE_CURSOR
79#define GBM_BO_USE_CURSOR GBM_BO_USE_CURSOR_64X64
80#endif
81
Giulio Camuffo954f1832014-10-11 18:27:30 +030082struct drm_backend {
83 struct weston_backend base;
84 struct weston_compositor *compositor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040085
86 struct udev *udev;
87 struct wl_event_source *drm_source;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040088
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010089 struct udev_monitor *udev_monitor;
90 struct wl_event_source *udev_drm_source;
91
Benjamin Franzke2af7f102011-03-02 11:14:59 +010092 struct {
David Herrmannd7488c22012-03-11 20:05:21 +010093 int id;
Benjamin Franzke2af7f102011-03-02 11:14:59 +010094 int fd;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +030095 char *filename;
Benjamin Franzke2af7f102011-03-02 11:14:59 +010096 } drm;
Benjamin Franzke060cf802011-04-30 09:32:11 +020097 struct gbm_device *gbm;
Kristian Høgsberg61741a22013-09-17 16:02:57 -070098 struct wl_listener session_listener;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +010099 uint32_t gbm_format;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200100
Rob Clark4339add2012-08-09 14:18:28 -0500101 /* we need these parameters in order to not fail drmModeAddFB2()
102 * due to out of bounds dimensions, and then mistakenly set
103 * sprites_are_broken:
104 */
Daniel Stonef214fdc2016-11-14 17:43:57 +0000105 int min_width, max_width;
106 int min_height, max_height;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200107 int no_addfb2;
Rob Clark4339add2012-08-09 14:18:28 -0500108
Jesse Barnes58ef3792012-02-23 09:45:49 -0500109 struct wl_list sprite_list;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500110 int sprites_are_broken;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +0200111 int sprites_hidden;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500112
Rob Clarkab5b1e32012-08-09 13:24:45 -0500113 int cursors_are_broken;
114
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200115 int use_pixman;
116
Rob Bradfordd355b802013-05-31 18:09:55 +0100117 struct udev_input input;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -0300118
Daniel Stone70d337d2015-06-16 18:42:23 +0100119 int32_t cursor_width;
120 int32_t cursor_height;
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +0000121
122 uint32_t connector;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000123 uint32_t pageflip_timeout;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400124};
125
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400126struct drm_mode {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500127 struct weston_mode base;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400128 drmModeModeInfo mode_info;
129};
130
Daniel Stonefc175a72017-04-04 17:54:22 +0100131enum drm_fb_type {
132 BUFFER_INVALID = 0, /**< never used */
133 BUFFER_CLIENT, /**< directly sourced from client */
134 BUFFER_PIXMAN_DUMB, /**< internal Pixman rendering */
135 BUFFER_GBM_SURFACE, /**< internal EGL rendering */
Daniel Stonee4256832017-04-04 17:54:27 +0100136 BUFFER_CURSOR, /**< internal cursor buffer */
Daniel Stonefc175a72017-04-04 17:54:22 +0100137};
138
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300139struct drm_fb {
Daniel Stonefc175a72017-04-04 17:54:22 +0100140 enum drm_fb_type type;
141
Daniel Stone6e7a9612017-04-04 17:54:26 +0100142 int refcnt;
143
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200144 uint32_t fb_id, stride, handle, size;
Daniel Stone0b70fa42017-04-04 17:54:23 +0100145 const struct pixel_format_info *format;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000146 int width, height;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200147 int fd;
Pekka Paalanende685b82012-12-04 15:58:12 +0200148 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200149
150 /* Used by gbm fbs */
151 struct gbm_bo *bo;
Daniel Stone05a5ac22017-04-04 17:54:25 +0100152 struct gbm_surface *gbm_surface;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200153
154 /* Used by dumb fbs */
155 void *map;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300156};
157
Richard Hughes2b2092a2013-04-24 14:58:02 +0100158struct drm_edid {
159 char eisa_id[13];
160 char monitor_name[13];
161 char pnp_id[5];
162 char serial_number[13];
163};
164
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400165struct drm_output {
Armin Krezović08368132016-09-30 14:11:05 +0200166 struct weston_output base;
167 drmModeConnector *connector;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400168
Daniel Stone17339232015-11-28 12:09:47 +0000169 uint32_t crtc_id; /* object ID to pass to DRM functions */
170 int pipe; /* index of CRTC in resource array / bitmasks */
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400171 uint32_t connector_id;
Matt Roper361d2ad2011-08-29 13:52:23 -0700172 drmModeCrtcPtr original_crtc;
Richard Hughes2b2092a2013-04-24 14:58:02 +0100173 struct drm_edid edid;
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +0300174 drmModePropertyPtr dpms_prop;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100175 uint32_t gbm_format;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200176
Daniel Stone36609c72015-06-18 07:49:02 +0100177 enum dpms_enum dpms;
178
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300179 int vblank_pending;
180 int page_flip_pending;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800181 int destroy_pending;
Armin Krezović08368132016-09-30 14:11:05 +0200182 int disable_pending;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300183
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100184 struct gbm_surface *gbm_surface;
Daniel Stonee4256832017-04-04 17:54:27 +0100185 struct drm_fb *gbm_cursor_fb[2];
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400186 struct weston_plane cursor_plane;
187 struct weston_plane fb_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500188 struct weston_view *cursor_view;
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400189 int current_cursor;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300190 struct drm_fb *current, *next;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200191 struct backlight *backlight;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200192
193 struct drm_fb *dumb[2];
194 pixman_image_t *image[2];
195 int current_image;
196 pixman_region32_t previous_damage;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300197
198 struct vaapi_recorder *recorder;
199 struct wl_listener recorder_frame_listener;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000200
201 struct wl_event_source *pageflip_timer;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400202};
203
Jesse Barnes58ef3792012-02-23 09:45:49 -0500204/*
205 * An output has a primary display plane plus zero or more sprites for
206 * blending display contents.
207 */
208struct drm_sprite {
209 struct wl_list link;
210
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400211 struct weston_plane plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500212
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200213 struct drm_fb *current, *next;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300214 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300215 struct drm_backend *backend;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500216
Jesse Barnes58ef3792012-02-23 09:45:49 -0500217 uint32_t possible_crtcs;
218 uint32_t plane_id;
219 uint32_t count_formats;
220
221 int32_t src_x, src_y;
222 uint32_t src_w, src_h;
223 uint32_t dest_x, dest_y;
224 uint32_t dest_w, dest_h;
225
226 uint32_t formats[];
227};
228
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300229static struct gl_renderer_interface *gl_renderer;
230
Kristian Høgsberg98cfea62013-02-18 16:15:53 -0500231static const char default_seat[] = "seat0";
Pekka Paalanen33156972012-08-03 13:30:30 -0400232
Armin Krezović545dba62016-08-05 15:54:18 +0200233static inline struct drm_output *
234to_drm_output(struct weston_output *base)
235{
236 return container_of(base, struct drm_output, base);
237}
238
239static inline struct drm_backend *
240to_drm_backend(struct weston_compositor *base)
241{
242 return container_of(base->backend, struct drm_backend, base);
243}
244
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000245static int
246pageflip_timeout(void *data) {
247 /*
248 * Our timer just went off, that means we're not receiving drm
249 * page flip events anymore for that output. Let's gracefully exit
250 * weston with a return value so devs can debug what's going on.
251 */
252 struct drm_output *output = data;
253 struct weston_compositor *compositor = output->base.compositor;
254
255 weston_log("Pageflip timeout reached on output %s, your "
256 "driver is probably buggy! Exiting.\n",
257 output->base.name);
258 weston_compositor_exit_with_code(compositor, EXIT_FAILURE);
259
260 return 0;
261}
262
263/* Creates the pageflip timer. Note that it isn't armed by default */
264static int
265drm_output_pageflip_timer_create(struct drm_output *output)
266{
267 struct wl_event_loop *loop = NULL;
268 struct weston_compositor *ec = output->base.compositor;
269
270 loop = wl_display_get_event_loop(ec->wl_display);
271 assert(loop);
272 output->pageflip_timer = wl_event_loop_add_timer(loop,
273 pageflip_timeout,
274 output);
275
276 if (output->pageflip_timer == NULL) {
277 weston_log("creating drm pageflip timer failed: %m\n");
278 return -1;
279 }
280
281 return 0;
282}
283
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400284static void
285drm_output_set_cursor(struct drm_output *output);
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400286
Mario Kleinerf507ec32015-06-21 21:25:14 +0200287static void
288drm_output_update_msc(struct drm_output *output, unsigned int seq);
289
Jesse Barnes58ef3792012-02-23 09:45:49 -0500290static int
Daniel Stonea3ae4762015-11-28 12:07:36 +0000291drm_sprite_crtc_supported(struct drm_output *output, struct drm_sprite *sprite)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500292{
Daniel Stonea3ae4762015-11-28 12:07:36 +0000293 return !!(sprite->possible_crtcs & (1 << output->pipe));
Jesse Barnes58ef3792012-02-23 09:45:49 -0500294}
295
Daniel Stone72c0e1b2017-02-09 13:49:15 +0000296static struct drm_output *
297drm_output_find_by_crtc(struct drm_backend *b, uint32_t crtc_id)
298{
299 struct drm_output *output;
300
301 wl_list_for_each(output, &b->compositor->output_list, base.link) {
302 if (output->crtc_id == crtc_id)
303 return output;
304 }
305
306 wl_list_for_each(output, &b->compositor->pending_output_list,
307 base.link) {
308 if (output->crtc_id == crtc_id)
309 return output;
310 }
311
312 return NULL;
313}
314
Daniel Stonec0ec7592017-02-09 13:58:35 +0000315static struct drm_output *
316drm_output_find_by_connector(struct drm_backend *b, uint32_t connector_id)
317{
318 struct drm_output *output;
319
320 wl_list_for_each(output, &b->compositor->output_list, base.link) {
321 if (output->connector_id == connector_id)
322 return output;
323 }
324
325 wl_list_for_each(output, &b->compositor->pending_output_list,
326 base.link) {
327 if (output->connector_id == connector_id)
328 return output;
329 }
330
331 return NULL;
332}
333
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300334static void
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100335drm_fb_destroy(struct drm_fb *fb)
336{
337 if (fb->fb_id != 0)
338 drmModeRmFB(fb->fd, fb->fb_id);
339 weston_buffer_reference(&fb->buffer_ref, NULL);
340 free(fb);
341}
342
343static void
344drm_fb_destroy_dumb(struct drm_fb *fb)
345{
346 struct drm_mode_destroy_dumb destroy_arg;
347
348 assert(fb->type == BUFFER_PIXMAN_DUMB);
349
350 if (fb->map && fb->size > 0)
351 munmap(fb->map, fb->size);
352
353 memset(&destroy_arg, 0, sizeof(destroy_arg));
354 destroy_arg.handle = fb->handle;
355 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
356
357 drm_fb_destroy(fb);
358}
359
360static void
361drm_fb_destroy_gbm(struct gbm_bo *bo, void *data)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300362{
363 struct drm_fb *fb = data;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300364
Daniel Stonee4256832017-04-04 17:54:27 +0100365 assert(fb->type == BUFFER_GBM_SURFACE || fb->type == BUFFER_CLIENT ||
366 fb->type == BUFFER_CURSOR);
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100367 drm_fb_destroy(fb);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300368}
369
370static struct drm_fb *
Daniel Stonef214fdc2016-11-14 17:43:57 +0000371drm_fb_create_dumb(struct drm_backend *b, int width, int height,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300372 uint32_t format)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200373{
374 struct drm_fb *fb;
375 int ret;
376
377 struct drm_mode_create_dumb create_arg;
378 struct drm_mode_destroy_dumb destroy_arg;
379 struct drm_mode_map_dumb map_arg;
380
Peter Huttererf3d62272013-08-08 11:57:05 +1000381 fb = zalloc(sizeof *fb);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200382 if (!fb)
383 return NULL;
384
Daniel Stone6e7a9612017-04-04 17:54:26 +0100385 fb->refcnt = 1;
386
Daniel Stone0b70fa42017-04-04 17:54:23 +0100387 fb->format = pixel_format_get_info(format);
388 if (!fb->format) {
389 weston_log("failed to look up format 0x%lx\n",
390 (unsigned long) format);
391 goto err_fb;
392 }
393
394 if (!fb->format->depth || !fb->format->bpp) {
395 weston_log("format 0x%lx is not compatible with dumb buffers\n",
396 (unsigned long) format);
397 goto err_fb;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300398 }
399
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700400 memset(&create_arg, 0, sizeof create_arg);
Daniel Stone0b70fa42017-04-04 17:54:23 +0100401 create_arg.bpp = fb->format->bpp;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200402 create_arg.width = width;
403 create_arg.height = height;
404
Giulio Camuffo954f1832014-10-11 18:27:30 +0300405 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200406 if (ret)
407 goto err_fb;
408
Daniel Stonefc175a72017-04-04 17:54:22 +0100409 fb->type = BUFFER_PIXMAN_DUMB;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200410 fb->handle = create_arg.handle;
411 fb->stride = create_arg.pitch;
412 fb->size = create_arg.size;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000413 fb->width = width;
414 fb->height = height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300415 fb->fd = b->drm.fd;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200416
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300417 ret = -1;
418
419 if (!b->no_addfb2) {
Yong Bakos4b6321f2016-08-17 17:37:55 -0700420 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300421
422 handles[0] = fb->handle;
423 pitches[0] = fb->stride;
424 offsets[0] = 0;
425
426 ret = drmModeAddFB2(b->drm.fd, width, height,
Daniel Stone0b70fa42017-04-04 17:54:23 +0100427 fb->format->format,
428 handles, pitches, offsets,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300429 &fb->fb_id, 0);
430 if (ret) {
431 weston_log("addfb2 failed: %m\n");
432 b->no_addfb2 = 1;
433 }
434 }
435
436 if (ret) {
Daniel Stone0b70fa42017-04-04 17:54:23 +0100437 ret = drmModeAddFB(b->drm.fd, width, height,
438 fb->format->depth, fb->format->bpp,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300439 fb->stride, fb->handle, &fb->fb_id);
440 }
441
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200442 if (ret)
443 goto err_bo;
444
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700445 memset(&map_arg, 0, sizeof map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200446 map_arg.handle = fb->handle;
Chris Michaeleb2074a2013-05-01 21:26:02 -0400447 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200448 if (ret)
449 goto err_add_fb;
450
Chris Michael4a7ce1f2015-11-10 10:40:37 -0500451 fb->map = mmap(NULL, fb->size, PROT_WRITE,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300452 MAP_SHARED, b->drm.fd, map_arg.offset);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200453 if (fb->map == MAP_FAILED)
454 goto err_add_fb;
455
456 return fb;
457
458err_add_fb:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300459 drmModeRmFB(b->drm.fd, fb->fb_id);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200460err_bo:
461 memset(&destroy_arg, 0, sizeof(destroy_arg));
462 destroy_arg.handle = create_arg.handle;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300463 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200464err_fb:
465 free(fb);
466 return NULL;
467}
468
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200469static struct drm_fb *
Daniel Stone6e7a9612017-04-04 17:54:26 +0100470drm_fb_ref(struct drm_fb *fb)
471{
472 fb->refcnt++;
473 return fb;
474}
475
476static struct drm_fb *
Daniel Stonefc175a72017-04-04 17:54:22 +0100477drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend,
478 uint32_t format, enum drm_fb_type type)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300479{
480 struct drm_fb *fb = gbm_bo_get_user_data(bo);
Derek Foreman482ffdf2016-07-08 12:50:57 -0500481 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300482 int ret;
483
Daniel Stonefc175a72017-04-04 17:54:22 +0100484 if (fb) {
485 assert(fb->type == type);
Daniel Stone6e7a9612017-04-04 17:54:26 +0100486 return drm_fb_ref(fb);
Daniel Stonefc175a72017-04-04 17:54:22 +0100487 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300488
Bryce Harringtonde16d892014-11-20 22:21:57 -0800489 fb = zalloc(sizeof *fb);
490 if (fb == NULL)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200491 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300492
Daniel Stonefc175a72017-04-04 17:54:22 +0100493 fb->type = type;
Daniel Stone6e7a9612017-04-04 17:54:26 +0100494 fb->refcnt = 1;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300495 fb->bo = bo;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300496
Daniel Stonec8c917c2016-11-14 17:45:58 +0000497 fb->width = gbm_bo_get_width(bo);
498 fb->height = gbm_bo_get_height(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200499 fb->stride = gbm_bo_get_stride(bo);
500 fb->handle = gbm_bo_get_handle(bo).u32;
Daniel Stone0b70fa42017-04-04 17:54:23 +0100501 fb->format = pixel_format_get_info(format);
Daniel Stonec8c917c2016-11-14 17:45:58 +0000502 fb->size = fb->stride * fb->height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300503 fb->fd = backend->drm.fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300504
Daniel Stone0b70fa42017-04-04 17:54:23 +0100505 if (!fb->format) {
506 weston_log("couldn't look up format 0x%lx\n",
507 (unsigned long) format);
508 goto err_free;
509 }
510
Daniel Stonec8c917c2016-11-14 17:45:58 +0000511 if (backend->min_width > fb->width ||
512 fb->width > backend->max_width ||
513 backend->min_height > fb->height ||
514 fb->height > backend->max_height) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200515 weston_log("bo geometry out of bounds\n");
516 goto err_free;
517 }
518
519 ret = -1;
520
Giulio Camuffo954f1832014-10-11 18:27:30 +0300521 if (format && !backend->no_addfb2) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200522 handles[0] = fb->handle;
523 pitches[0] = fb->stride;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200524 offsets[0] = 0;
525
Daniel Stonec8c917c2016-11-14 17:45:58 +0000526 ret = drmModeAddFB2(backend->drm.fd, fb->width, fb->height,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200527 format, handles, pitches, offsets,
528 &fb->fb_id, 0);
529 if (ret) {
530 weston_log("addfb2 failed: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300531 backend->no_addfb2 = 1;
532 backend->sprites_are_broken = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200533 }
534 }
535
Daniel Stone0b70fa42017-04-04 17:54:23 +0100536 if (ret && fb->format->depth && fb->format->bpp)
Daniel Stonec8c917c2016-11-14 17:45:58 +0000537 ret = drmModeAddFB(backend->drm.fd, fb->width, fb->height,
Daniel Stone0b70fa42017-04-04 17:54:23 +0100538 fb->format->depth, fb->format->bpp,
539 fb->stride, fb->handle, &fb->fb_id);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200540
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300541 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200542 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200543 goto err_free;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300544 }
545
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100546 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_gbm);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300547
548 return fb;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200549
550err_free:
551 free(fb);
552 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300553}
554
555static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500556drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200557{
Pekka Paalanende685b82012-12-04 15:58:12 +0200558 assert(fb->buffer_ref.buffer == NULL);
Daniel Stonefc175a72017-04-04 17:54:22 +0100559 assert(fb->type == BUFFER_CLIENT);
Pekka Paalanende685b82012-12-04 15:58:12 +0200560 weston_buffer_reference(&fb->buffer_ref, buffer);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200561}
562
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200563static void
Daniel Stone05a5ac22017-04-04 17:54:25 +0100564drm_fb_unref(struct drm_fb *fb)
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200565{
566 if (!fb)
567 return;
568
Daniel Stone6e7a9612017-04-04 17:54:26 +0100569 assert(fb->refcnt > 0);
570 if (--fb->refcnt > 0)
571 return;
572
Daniel Stonefc175a72017-04-04 17:54:22 +0100573 switch (fb->type) {
574 case BUFFER_PIXMAN_DUMB:
Daniel Stone6e7a9612017-04-04 17:54:26 +0100575 drm_fb_destroy_dumb(fb);
Daniel Stonefc175a72017-04-04 17:54:22 +0100576 break;
Daniel Stonee4256832017-04-04 17:54:27 +0100577 case BUFFER_CURSOR:
Daniel Stonefc175a72017-04-04 17:54:22 +0100578 case BUFFER_CLIENT:
579 gbm_bo_destroy(fb->bo);
580 break;
581 case BUFFER_GBM_SURFACE:
Daniel Stone05a5ac22017-04-04 17:54:25 +0100582 gbm_surface_release_buffer(fb->gbm_surface, fb->bo);
Daniel Stonefc175a72017-04-04 17:54:22 +0100583 break;
584 default:
585 assert(NULL);
586 break;
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200587 }
588}
589
Daniel Stone90648872016-10-21 18:08:37 +0100590static int
591drm_view_transform_supported(struct weston_view *ev)
592{
593 return !ev->transform.enabled ||
594 (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
595}
596
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500597static uint32_t
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200598drm_output_check_scanout_format(struct drm_output *output,
599 struct weston_surface *es, struct gbm_bo *bo)
600{
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200601 uint32_t format;
602 pixman_region32_t r;
603
604 format = gbm_bo_get_format(bo);
605
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700606 if (format == GBM_FORMAT_ARGB8888) {
607 /* We can scanout an ARGB buffer if the surface's
608 * opaque region covers the whole output, but we have
609 * to use XRGB as the KMS format code. */
Kristian Høgsberg1be87e32014-01-17 14:22:41 -0800610 pixman_region32_init_rect(&r, 0, 0,
611 output->base.width,
612 output->base.height);
613 pixman_region32_subtract(&r, &r, &es->opaque);
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200614
615 if (!pixman_region32_not_empty(&r))
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500616 format = GBM_FORMAT_XRGB8888;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200617
618 pixman_region32_fini(&r);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500619 }
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700620
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100621 if (output->gbm_format == format)
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700622 return format;
623
624 return 0;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200625}
626
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400627static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200628drm_output_prepare_scanout_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500629 struct weston_view *ev)
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500630{
Armin Krezović545dba62016-08-05 15:54:18 +0200631 struct drm_backend *b = to_drm_backend(output->base.compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500632 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200633 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300634 struct gbm_bo *bo;
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500635 uint32_t format;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500636
Daniel Stone90648872016-10-21 18:08:37 +0100637 /* Don't import buffers which span multiple outputs. */
638 if (ev->output_mask != (1u << output->base.id))
639 return NULL;
640
Daniel Stone296d7a92016-10-21 18:05:37 +0100641 /* We use GBM to import buffers. */
642 if (b->gbm == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400643 return NULL;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500644
Daniel Stone296d7a92016-10-21 18:05:37 +0100645 if (buffer == NULL)
646 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +0100647 if (wl_shm_buffer_get(buffer->resource))
648 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +0100649
650 /* Make sure our view is exactly compatible with the output. */
651 if (ev->geometry.x != output->base.x ||
652 ev->geometry.y != output->base.y)
653 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +0100654 if (buffer->width != output->base.current_mode->width ||
655 buffer->height != output->base.current_mode->height)
656 return NULL;
657
Daniel Stone296d7a92016-10-21 18:05:37 +0100658 if (ev->transform.enabled)
659 return NULL;
Pekka Paalanen5580f222015-02-17 16:33:18 +0200660 if (ev->geometry.scissor_enabled)
661 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +0100662 if (viewport->buffer.transform != output->base.transform)
663 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +0100664 if (viewport->buffer.scale != output->base.current_scale)
665 return NULL;
666 if (!drm_view_transform_supported(ev))
667 return NULL;
668
669 if (ev->alpha != 1.0f)
670 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +0100671
Giulio Camuffo954f1832014-10-11 18:27:30 +0300672 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
Kristian Høgsberg63996462013-09-03 22:27:08 -0700673 buffer->resource, GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500674
Rob Bradford9b101872012-09-14 23:25:41 +0100675 /* Unable to use the buffer for scanout */
676 if (!bo)
677 return NULL;
678
Jason Ekstranda7af7042013-10-12 22:38:11 -0500679 format = drm_output_check_scanout_format(output, ev->surface, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500680 if (format == 0) {
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300681 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400682 return NULL;
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300683 }
684
Daniel Stonefc175a72017-04-04 17:54:22 +0100685 output->next = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300686 if (!output->next) {
687 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400688 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300689 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500690
Pekka Paalanende685b82012-12-04 15:58:12 +0200691 drm_fb_set_buffer(output->next, buffer);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500692
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400693 return &output->fb_plane;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500694}
695
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500696static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200697drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400698{
Armin Krezović545dba62016-08-05 15:54:18 +0200699 struct drm_backend *b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300700 struct gbm_bo *bo;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400701
Giulio Camuffo954f1832014-10-11 18:27:30 +0300702 output->base.compositor->renderer->repaint_output(&output->base,
703 damage);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400704
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100705 bo = gbm_surface_lock_front_buffer(output->gbm_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300706 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +0200707 weston_log("failed to lock front buffer: %m\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400708 return;
709 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300710
Daniel Stonefc175a72017-04-04 17:54:22 +0100711 output->next = drm_fb_get_from_bo(bo, b, output->gbm_format,
712 BUFFER_GBM_SURFACE);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300713 if (!output->next) {
Martin Minarik6d118362012-06-07 18:01:59 +0200714 weston_log("failed to get drm_fb for bo\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100715 gbm_surface_release_buffer(output->gbm_surface, bo);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300716 return;
717 }
Daniel Stone05a5ac22017-04-04 17:54:25 +0100718 output->next->gbm_surface = output->gbm_surface;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400719}
720
721static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200722drm_output_render_pixman(struct drm_output *output, pixman_region32_t *damage)
723{
724 struct weston_compositor *ec = output->base.compositor;
725 pixman_region32_t total_damage, previous_damage;
726
727 pixman_region32_init(&total_damage);
728 pixman_region32_init(&previous_damage);
729
730 pixman_region32_copy(&previous_damage, damage);
731
732 pixman_region32_union(&total_damage, damage, &output->previous_damage);
733 pixman_region32_copy(&output->previous_damage, &previous_damage);
734
735 output->current_image ^= 1;
736
Daniel Stone6e7a9612017-04-04 17:54:26 +0100737 output->next = drm_fb_ref(output->dumb[output->current_image]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200738 pixman_renderer_output_set_buffer(&output->base,
739 output->image[output->current_image]);
740
741 ec->renderer->repaint_output(&output->base, &total_damage);
742
743 pixman_region32_fini(&total_damage);
744 pixman_region32_fini(&previous_damage);
745}
746
747static void
748drm_output_render(struct drm_output *output, pixman_region32_t *damage)
749{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300750 struct weston_compositor *c = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +0200751 struct drm_backend *b = to_drm_backend(c);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200752
Giulio Camuffo954f1832014-10-11 18:27:30 +0300753 if (b->use_pixman)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200754 drm_output_render_pixman(output, damage);
755 else
756 drm_output_render_gl(output, damage);
757
Giulio Camuffo954f1832014-10-11 18:27:30 +0300758 pixman_region32_subtract(&c->primary_plane.damage,
759 &c->primary_plane.damage, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200760}
761
762static void
Richard Hughese7299962013-05-01 21:52:12 +0100763drm_output_set_gamma(struct weston_output *output_base,
764 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
765{
766 int rc;
Armin Krezović545dba62016-08-05 15:54:18 +0200767 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300768 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +0200769 to_drm_backend(output->base.compositor);
Richard Hughese7299962013-05-01 21:52:12 +0100770
771 /* check */
772 if (output_base->gamma_size != size)
773 return;
774 if (!output->original_crtc)
775 return;
776
Giulio Camuffo954f1832014-10-11 18:27:30 +0300777 rc = drmModeCrtcSetGamma(backend->drm.fd,
Richard Hughese7299962013-05-01 21:52:12 +0100778 output->crtc_id,
779 size, r, g, b);
780 if (rc)
781 weston_log("set gamma failed: %m\n");
782}
783
Bryce Harringtonada4f072015-06-30 13:25:46 -0700784/* Determine the type of vblank synchronization to use for the output.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200785 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700786 * The pipe parameter indicates which CRTC is in use. Knowing this, we
787 * can determine which vblank sequence type to use for it. Traditional
788 * cards had only two CRTCs, with CRTC 0 using no special flags, and
789 * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
790 * parameter indicates this.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200791 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700792 * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
793 * 0-31. If this is non-zero it indicates we're dealing with a
794 * multi-gpu situation and we need to calculate the vblank sync
795 * using DRM_BLANK_HIGH_CRTC_MASK.
796 */
Pekka Paalanenc8a1ff02015-07-02 15:06:08 +0300797static unsigned int
798drm_waitvblank_pipe(struct drm_output *output)
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200799{
800 if (output->pipe > 1)
801 return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
802 DRM_VBLANK_HIGH_CRTC_MASK;
803 else if (output->pipe > 0)
804 return DRM_VBLANK_SECONDARY;
805 else
806 return 0;
807}
808
David Herrmann1edf44c2013-10-22 17:11:26 +0200809static int
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500810drm_output_repaint(struct weston_output *output_base,
Daniel Stoneb1f166d2017-03-01 11:34:10 +0000811 pixman_region32_t *damage,
812 void *repaint_data)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100813{
Armin Krezović545dba62016-08-05 15:54:18 +0200814 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300815 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +0200816 to_drm_backend(output->base.compositor);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500817 struct drm_sprite *s;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400818 struct drm_mode *mode;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500819 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100820
Armin Krezović08368132016-09-30 14:11:05 +0200821 if (output->disable_pending || output->destroy_pending)
David Herrmann1edf44c2013-10-22 17:11:26 +0200822 return -1;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800823
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300824 if (!output->next)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400825 drm_output_render(output, damage);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300826 if (!output->next)
David Herrmann1edf44c2013-10-22 17:11:26 +0200827 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100828
Hardeningff39efa2013-09-18 23:56:35 +0200829 mode = container_of(output->base.current_mode, struct drm_mode, base);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +0200830 if (!output->current ||
831 output->current->stride != output->next->stride) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300832 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300833 output->next->fb_id, 0, 0,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400834 &output->connector_id, 1,
835 &mode->mode_info);
836 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200837 weston_log("set mode failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200838 goto err_pageflip;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400839 }
Ander Conselvan de Oliveira722a2d52013-06-04 16:24:05 +0300840 output_base->set_dpms(output_base, WESTON_DPMS_ON);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200841 }
842
Giulio Camuffo954f1832014-10-11 18:27:30 +0300843 if (drmModePageFlip(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300844 output->next->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500845 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200846 weston_log("queueing pageflip failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200847 goto err_pageflip;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500848 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100849
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300850 output->page_flip_pending = 1;
851
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000852 if (output->pageflip_timer)
853 wl_event_source_timer_update(output->pageflip_timer,
854 backend->pageflip_timeout);
855
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400856 drm_output_set_cursor(output);
857
Jesse Barnes58ef3792012-02-23 09:45:49 -0500858 /*
859 * Now, update all the sprite surfaces
860 */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300861 wl_list_for_each(s, &backend->sprite_list, link) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200862 uint32_t flags = 0, fb_id = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500863 drmVBlank vbl = {
864 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
865 .request.sequence = 1,
866 };
867
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200868 if ((!s->current && !s->next) ||
Daniel Stonea3ae4762015-11-28 12:07:36 +0000869 !drm_sprite_crtc_supported(output, s))
Jesse Barnes58ef3792012-02-23 09:45:49 -0500870 continue;
871
Giulio Camuffo954f1832014-10-11 18:27:30 +0300872 if (s->next && !backend->sprites_hidden)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200873 fb_id = s->next->fb_id;
874
Giulio Camuffo954f1832014-10-11 18:27:30 +0300875 ret = drmModeSetPlane(backend->drm.fd, s->plane_id,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200876 output->crtc_id, fb_id, flags,
Jesse Barnes58ef3792012-02-23 09:45:49 -0500877 s->dest_x, s->dest_y,
878 s->dest_w, s->dest_h,
879 s->src_x, s->src_y,
880 s->src_w, s->src_h);
881 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +0200882 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500883 ret, strerror(errno));
884
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200885 vbl.request.type |= drm_waitvblank_pipe(output);
Rob Clark5ca1a472012-08-08 20:27:37 -0500886
Jesse Barnes58ef3792012-02-23 09:45:49 -0500887 /*
888 * Queue a vblank signal so we know when the surface
889 * becomes active on the display or has been replaced.
890 */
891 vbl.request.signal = (unsigned long)s;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300892 ret = drmWaitVBlank(backend->drm.fd, &vbl);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500893 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200894 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500895 ret, strerror(errno));
896 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300897
898 s->output = output;
899 output->vblank_pending = 1;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500900 }
901
David Herrmann1edf44c2013-10-22 17:11:26 +0200902 return 0;
903
904err_pageflip:
Kristian Høgsbergb3955b02014-01-23 16:25:06 -0800905 output->cursor_view = NULL;
David Herrmann1edf44c2013-10-22 17:11:26 +0200906 if (output->next) {
Daniel Stone05a5ac22017-04-04 17:54:25 +0100907 drm_fb_unref(output->next);
David Herrmann1edf44c2013-10-22 17:11:26 +0200908 output->next = NULL;
909 }
910
911 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400912}
913
914static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200915drm_output_start_repaint_loop(struct weston_output *output_base)
916{
Armin Krezović545dba62016-08-05 15:54:18 +0200917 struct drm_output *output = to_drm_output(output_base);
918 struct drm_backend *backend =
919 to_drm_backend(output_base->compositor);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200920 uint32_t fb_id;
Mario Kleinerf507ec32015-06-21 21:25:14 +0200921 struct timespec ts, tnow;
922 struct timespec vbl2now;
923 int64_t refresh_nsec;
924 int ret;
925 drmVBlank vbl = {
926 .request.type = DRM_VBLANK_RELATIVE,
927 .request.sequence = 0,
928 .request.signal = 0,
929 };
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300930
Armin Krezović08368132016-09-30 14:11:05 +0200931 if (output->disable_pending || output->destroy_pending)
Xiong Zhangabd5d472013-10-11 14:43:07 +0800932 return;
933
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300934 if (!output->current) {
935 /* We can't page flip if there's no mode set */
David Herrmann3c688c52013-10-22 17:11:25 +0200936 goto finish_frame;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300937 }
938
Mario Kleinerf507ec32015-06-21 21:25:14 +0200939 /* Try to get current msc and timestamp via instant query */
940 vbl.request.type |= drm_waitvblank_pipe(output);
941 ret = drmWaitVBlank(backend->drm.fd, &vbl);
942
943 /* Error ret or zero timestamp means failure to get valid timestamp */
944 if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
945 ts.tv_sec = vbl.reply.tval_sec;
946 ts.tv_nsec = vbl.reply.tval_usec * 1000;
947
948 /* Valid timestamp for most recent vblank - not stale?
949 * Stale ts could happen on Linux 3.17+, so make sure it
950 * is not older than 1 refresh duration since now.
951 */
952 weston_compositor_read_presentation_clock(backend->compositor,
953 &tnow);
954 timespec_sub(&vbl2now, &tnow, &ts);
955 refresh_nsec =
956 millihz_to_nsec(output->base.current_mode->refresh);
957 if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
958 drm_output_update_msc(output, vbl.reply.sequence);
959 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200960 WP_PRESENTATION_FEEDBACK_INVALID);
Mario Kleinerf507ec32015-06-21 21:25:14 +0200961 return;
962 }
963 }
964
965 /* Immediate query didn't provide valid timestamp.
966 * Use pageflip fallback.
967 */
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300968 fb_id = output->current->fb_id;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200969
Giulio Camuffo954f1832014-10-11 18:27:30 +0300970 if (drmModePageFlip(backend->drm.fd, output->crtc_id, fb_id,
Jonas Ådahle5a12252013-04-05 23:07:11 +0200971 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
972 weston_log("queueing pageflip failed: %m\n");
David Herrmann3c688c52013-10-22 17:11:25 +0200973 goto finish_frame;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200974 }
David Herrmann3c688c52013-10-22 17:11:25 +0200975
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000976 if (output->pageflip_timer)
977 wl_event_source_timer_update(output->pageflip_timer,
978 backend->pageflip_timeout);
979
David Herrmann3c688c52013-10-22 17:11:25 +0200980 return;
981
982finish_frame:
983 /* if we cannot page-flip, immediately finish frame */
Daniel Stone3615ce12017-03-01 11:34:05 +0000984 weston_output_finish_frame(output_base, NULL,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200985 WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200986}
987
988static void
Pekka Paalanen641307c2014-09-23 22:08:47 -0400989drm_output_update_msc(struct drm_output *output, unsigned int seq)
990{
991 uint64_t msc_hi = output->base.msc >> 32;
992
993 if (seq < (output->base.msc & 0xffffffff))
994 msc_hi++;
995
996 output->base.msc = (msc_hi << 32) + seq;
997}
998
999static void
Jesse Barnes58ef3792012-02-23 09:45:49 -05001000vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
1001 void *data)
1002{
1003 struct drm_sprite *s = (struct drm_sprite *)data;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001004 struct drm_output *output = s->output;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001005 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001006 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
1007 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001008
Pekka Paalanen641307c2014-09-23 22:08:47 -04001009 drm_output_update_msc(output, frame);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001010 output->vblank_pending = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001011
Daniel Stone05a5ac22017-04-04 17:54:25 +01001012 drm_fb_unref(s->current);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001013 s->current = s->next;
1014 s->next = NULL;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001015
1016 if (!output->page_flip_pending) {
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001017 /* Stop the pageflip timer instead of rearming it here */
1018 if (output->pageflip_timer)
1019 wl_event_source_timer_update(output->pageflip_timer, 0);
1020
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001021 ts.tv_sec = sec;
1022 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02001023 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001024 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05001025}
1026
1027static void
Armin Krezović08368132016-09-30 14:11:05 +02001028drm_output_destroy(struct weston_output *base);
Xiong Zhangabd5d472013-10-11 14:43:07 +08001029
1030static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001031page_flip_handler(int fd, unsigned int frame,
1032 unsigned int sec, unsigned int usec, void *data)
1033{
Armin Krezović545dba62016-08-05 15:54:18 +02001034 struct drm_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001035 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001036 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
1037 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
1038 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001039
Pekka Paalanen641307c2014-09-23 22:08:47 -04001040 drm_output_update_msc(output, frame);
1041
Jonas Ådahle5a12252013-04-05 23:07:11 +02001042 /* We don't set page_flip_pending on start_repaint_loop, in that case
1043 * we just want to page flip to the current buffer to get an accurate
1044 * timestamp */
1045 if (output->page_flip_pending) {
Daniel Stone05a5ac22017-04-04 17:54:25 +01001046 drm_fb_unref(output->current);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001047 output->current = output->next;
1048 output->next = NULL;
1049 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001050
Jonas Ådahle5a12252013-04-05 23:07:11 +02001051 output->page_flip_pending = 0;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001052
Xiong Zhangabd5d472013-10-11 14:43:07 +08001053 if (output->destroy_pending)
1054 drm_output_destroy(&output->base);
Armin Krezović08368132016-09-30 14:11:05 +02001055 else if (output->disable_pending)
1056 weston_output_disable(&output->base);
Xiong Zhangabd5d472013-10-11 14:43:07 +08001057 else if (!output->vblank_pending) {
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001058 /* Stop the pageflip timer instead of rearming it here */
1059 if (output->pageflip_timer)
1060 wl_event_source_timer_update(output->pageflip_timer, 0);
1061
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001062 ts.tv_sec = sec;
1063 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02001064 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03001065
1066 /* We can't call this from frame_notify, because the output's
1067 * repaint needed flag is cleared just after that */
1068 if (output->recorder)
1069 weston_output_schedule_repaint(&output->base);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001070 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001071}
1072
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001073static uint32_t
1074drm_output_check_sprite_format(struct drm_sprite *s,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001075 struct weston_view *ev, struct gbm_bo *bo)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001076{
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001077 uint32_t i, format;
1078
1079 format = gbm_bo_get_format(bo);
1080
1081 if (format == GBM_FORMAT_ARGB8888) {
1082 pixman_region32_t r;
1083
Kristian Høgsberg63093a32013-03-01 14:29:16 -05001084 pixman_region32_init_rect(&r, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001085 ev->surface->width,
1086 ev->surface->height);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001087 pixman_region32_subtract(&r, &r, &ev->surface->opaque);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001088
1089 if (!pixman_region32_not_empty(&r))
1090 format = GBM_FORMAT_XRGB8888;
1091
1092 pixman_region32_fini(&r);
1093 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05001094
1095 for (i = 0; i < s->count_formats; i++)
1096 if (s->formats[i] == format)
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001097 return format;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001098
1099 return 0;
1100}
1101
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001102static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001103drm_output_prepare_overlay_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001104 struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001105{
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001106 struct weston_compositor *ec = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02001107 struct drm_backend *b = to_drm_backend(ec);
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001108 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001109 struct wl_resource *buffer_resource;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001110 struct drm_sprite *s;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001111 struct linux_dmabuf_buffer *dmabuf;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001112 int found = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001113 struct gbm_bo *bo;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001114 pixman_region32_t dest_rect, src_rect;
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001115 pixman_box32_t *box, tbox;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001116 uint32_t format;
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001117 wl_fixed_t sx1, sy1, sx2, sy2;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001118
Giulio Camuffo954f1832014-10-11 18:27:30 +03001119 if (b->sprites_are_broken)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001120 return NULL;
Kristian Høgsberg65bec242012-03-05 19:57:35 -05001121
Daniel Stone296d7a92016-10-21 18:05:37 +01001122 /* Don't import buffers which span multiple outputs. */
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001123 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001124 return NULL;
Ander Conselvan de Oliveirad450b192012-06-26 17:09:12 +03001125
Daniel Stone296d7a92016-10-21 18:05:37 +01001126 /* We can only import GBM buffers. */
1127 if (b->gbm == NULL)
1128 return NULL;
1129
Jason Ekstranda7af7042013-10-12 22:38:11 -05001130 if (ev->surface->buffer_ref.buffer == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001131 return NULL;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001132 buffer_resource = ev->surface->buffer_ref.buffer->resource;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001133 if (wl_shm_buffer_get(buffer_resource))
Rob Clark702ffae2012-08-09 14:18:27 -05001134 return NULL;
1135
Daniel Stone296d7a92016-10-21 18:05:37 +01001136 if (viewport->buffer.transform != output->base.transform)
1137 return NULL;
1138 if (viewport->buffer.scale != output->base.current_scale)
1139 return NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001140 if (!drm_view_transform_supported(ev))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001141 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001142
Daniel Stone296d7a92016-10-21 18:05:37 +01001143 if (ev->alpha != 1.0f)
1144 return NULL;
1145
Giulio Camuffo954f1832014-10-11 18:27:30 +03001146 wl_list_for_each(s, &b->sprite_list, link) {
Daniel Stonea3ae4762015-11-28 12:07:36 +00001147 if (!drm_sprite_crtc_supported(output, s))
Jesse Barnes58ef3792012-02-23 09:45:49 -05001148 continue;
1149
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001150 if (!s->next) {
Jesse Barnes58ef3792012-02-23 09:45:49 -05001151 found = 1;
1152 break;
1153 }
1154 }
1155
1156 /* No sprites available */
1157 if (!found)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001158 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001159
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001160 if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) {
Bryce Harringtona3582072015-08-14 12:23:13 -07001161#ifdef HAVE_GBM_FD_IMPORT
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001162 /* XXX: TODO:
1163 *
1164 * Use AddFB2 directly, do not go via GBM.
1165 * Add support for multiplanar formats.
1166 * Both require refactoring in the DRM-backend to
1167 * support a mix of gbm_bos and drmfbs.
1168 */
1169 struct gbm_import_fd_data gbm_dmabuf = {
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001170 .fd = dmabuf->attributes.fd[0],
1171 .width = dmabuf->attributes.width,
1172 .height = dmabuf->attributes.height,
1173 .stride = dmabuf->attributes.stride[0],
1174 .format = dmabuf->attributes.format
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001175 };
1176
Micah Fedkec8890122017-02-01 15:28:23 -05001177 /* XXX: TODO:
1178 *
1179 * Currently the buffer is rejected if any dmabuf attribute
1180 * flag is set. This keeps us from passing an inverted /
1181 * interlaced / bottom-first buffer (or any other type that may
1182 * be added in the future) through to an overlay. Ultimately,
1183 * these types of buffers should be handled through buffer
1184 * transforms and not as spot-checks requiring specific
1185 * knowledge. */
1186 if (dmabuf->attributes.n_planes != 1 ||
1187 dmabuf->attributes.offset[0] != 0 ||
1188 dmabuf->attributes.flags)
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001189 return NULL;
1190
1191 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf,
1192 GBM_BO_USE_SCANOUT);
Bryce Harringtona3582072015-08-14 12:23:13 -07001193#else
1194 return NULL;
1195#endif
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001196 } else {
1197 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
1198 buffer_resource, GBM_BO_USE_SCANOUT);
1199 }
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001200 if (!bo)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001201 return NULL;
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001202
Jason Ekstranda7af7042013-10-12 22:38:11 -05001203 format = drm_output_check_sprite_format(s, ev, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001204 if (format == 0) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001205 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001206 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001207 }
1208
Daniel Stonefc175a72017-04-04 17:54:22 +01001209 s->next = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001210 if (!s->next) {
1211 gbm_bo_destroy(bo);
1212 return NULL;
1213 }
1214
Jason Ekstranda7af7042013-10-12 22:38:11 -05001215 drm_fb_set_buffer(s->next, ev->surface->buffer_ref.buffer);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001216
Jason Ekstranda7af7042013-10-12 22:38:11 -05001217 box = pixman_region32_extents(&ev->transform.boundingbox);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001218 s->plane.x = box->x1;
1219 s->plane.y = box->y1;
1220
Jesse Barnes58ef3792012-02-23 09:45:49 -05001221 /*
1222 * Calculate the source & dest rects properly based on actual
Derek Foreman4b1a0a12014-09-10 15:37:33 -05001223 * position (note the caller has called weston_view_update_transform()
Jesse Barnes58ef3792012-02-23 09:45:49 -05001224 * for us already).
1225 */
1226 pixman_region32_init(&dest_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001227 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001228 &output->base.region);
1229 pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001230 box = pixman_region32_extents(&dest_rect);
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001231 tbox = weston_transformed_rect(output->base.width,
1232 output->base.height,
1233 output->base.transform,
1234 output->base.current_scale,
Alexander Larssond9a7bb72013-05-22 14:41:39 +02001235 *box);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001236 s->dest_x = tbox.x1;
1237 s->dest_y = tbox.y1;
1238 s->dest_w = tbox.x2 - tbox.x1;
1239 s->dest_h = tbox.y2 - tbox.y1;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001240 pixman_region32_fini(&dest_rect);
1241
1242 pixman_region32_init(&src_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001243 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001244 &output->base.region);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001245 box = pixman_region32_extents(&src_rect);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001246
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247 weston_view_from_global_fixed(ev,
1248 wl_fixed_from_int(box->x1),
1249 wl_fixed_from_int(box->y1),
1250 &sx1, &sy1);
1251 weston_view_from_global_fixed(ev,
1252 wl_fixed_from_int(box->x2),
1253 wl_fixed_from_int(box->y2),
1254 &sx2, &sy2);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001255
1256 if (sx1 < 0)
1257 sx1 = 0;
1258 if (sy1 < 0)
1259 sy1 = 0;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001260 if (sx2 > wl_fixed_from_int(ev->surface->width))
1261 sx2 = wl_fixed_from_int(ev->surface->width);
1262 if (sy2 > wl_fixed_from_int(ev->surface->height))
1263 sy2 = wl_fixed_from_int(ev->surface->height);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001264
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001265 tbox.x1 = sx1;
1266 tbox.y1 = sy1;
1267 tbox.x2 = sx2;
1268 tbox.y2 = sy2;
1269
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001270 tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width),
1271 wl_fixed_from_int(ev->surface->height),
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001272 viewport->buffer.transform,
1273 viewport->buffer.scale,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001274 tbox);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001275
1276 s->src_x = tbox.x1 << 8;
1277 s->src_y = tbox.y1 << 8;
1278 s->src_w = (tbox.x2 - tbox.x1) << 8;
1279 s->src_h = (tbox.y2 - tbox.y1) << 8;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001280 pixman_region32_fini(&src_rect);
1281
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001282 return &s->plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001283}
1284
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001285static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001286drm_output_prepare_cursor_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001287 struct weston_view *ev)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001288{
Armin Krezović545dba62016-08-05 15:54:18 +02001289 struct drm_backend *b = to_drm_backend(output->base.compositor);
Neil Robertsf37f82c2014-05-01 18:00:41 +01001290 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Derek Foreman6c19b692015-12-03 14:07:12 -06001291 struct wl_shm_buffer *shmbuf;
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001292 float x, y;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001293
Giulio Camuffo954f1832014-10-11 18:27:30 +03001294 if (b->cursors_are_broken)
Kristian Høgsberg8a015802012-08-09 17:19:23 -04001295 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001296
1297 if (output->cursor_view)
Pekka Paalanen5580f222015-02-17 16:33:18 +02001298 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001299
1300 /* Don't import buffers which span multiple outputs. */
1301 if (ev->output_mask != (1u << output->base.id))
1302 return NULL;
1303
1304 /* We use GBM to import SHM buffers. */
1305 if (b->gbm == NULL)
1306 return NULL;
1307
Derek Foreman6c19b692015-12-03 14:07:12 -06001308 if (ev->surface->buffer_ref.buffer == NULL)
1309 return NULL;
1310 shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource);
1311 if (!shmbuf)
1312 return NULL;
1313 if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888)
1314 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001315
1316 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
1317 return NULL;
1318 if (ev->transform.enabled &&
1319 (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE))
1320 return NULL;
1321 if (viewport->buffer.scale != output->base.current_scale)
1322 return NULL;
1323 if (ev->geometry.scissor_enabled)
1324 return NULL;
1325
Derek Foreman6c19b692015-12-03 14:07:12 -06001326 if (ev->surface->width > b->cursor_width ||
Daniel Stone70d337d2015-06-16 18:42:23 +01001327 ev->surface->height > b->cursor_height)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001328 return NULL;
1329
Jason Ekstranda7af7042013-10-12 22:38:11 -05001330 output->cursor_view = ev;
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001331 weston_view_to_global_float(ev, 0, 0, &x, &y);
1332 output->cursor_plane.x = x;
1333 output->cursor_plane.y = y;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001334
1335 return &output->cursor_plane;
1336}
1337
Pekka Paalanend0ead482014-06-16 12:05:40 +03001338/**
1339 * Update the image for the current cursor surface
1340 *
1341 * @param b DRM backend structure
1342 * @param bo GBM buffer object to write into
1343 * @param ev View to use for cursor image
1344 */
1345static void
1346cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo,
1347 struct weston_view *ev)
1348{
1349 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
1350 uint32_t buf[b->cursor_width * b->cursor_height];
1351 int32_t stride;
1352 uint8_t *s;
1353 int i;
1354
1355 assert(buffer && buffer->shm_buffer);
1356 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
1357 assert(ev->surface->width <= b->cursor_width);
1358 assert(ev->surface->height <= b->cursor_height);
1359
1360 memset(buf, 0, sizeof buf);
1361 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
1362 s = wl_shm_buffer_get_data(buffer->shm_buffer);
1363
1364 wl_shm_buffer_begin_access(buffer->shm_buffer);
1365 for (i = 0; i < ev->surface->height; i++)
1366 memcpy(buf + i * b->cursor_width,
1367 s + i * stride,
1368 ev->surface->width * 4);
1369 wl_shm_buffer_end_access(buffer->shm_buffer);
1370
1371 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
1372 weston_log("failed update cursor: %m\n");
1373}
1374
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001375static void
1376drm_output_set_cursor(struct drm_output *output)
1377{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001378 struct weston_view *ev = output->cursor_view;
Armin Krezović545dba62016-08-05 15:54:18 +02001379 struct drm_backend *b = to_drm_backend(output->base.compositor);
Pekka Paalanend0ead482014-06-16 12:05:40 +03001380 EGLint handle;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001381 struct gbm_bo *bo;
Derek Foremanbe428b32015-11-24 11:39:38 -06001382 float x, y;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001383
Jason Ekstranda7af7042013-10-12 22:38:11 -05001384 if (ev == NULL) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001385 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001386 return;
1387 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001388
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001389 if (pixman_region32_not_empty(&output->cursor_plane.damage)) {
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001390 pixman_region32_fini(&output->cursor_plane.damage);
1391 pixman_region32_init(&output->cursor_plane.damage);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001392 output->current_cursor ^= 1;
Daniel Stonee4256832017-04-04 17:54:27 +01001393 bo = output->gbm_cursor_fb[output->current_cursor]->bo;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001394
Pekka Paalanend0ead482014-06-16 12:05:40 +03001395 cursor_bo_update(b, bo, ev);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001396 handle = gbm_bo_get_handle(bo).s32;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001397 if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle,
1398 b->cursor_width, b->cursor_height)) {
Pekka Paalanenae29da22012-08-06 14:57:05 +03001399 weston_log("failed to set cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001400 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001401 }
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001402 }
1403
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001404 x = (output->cursor_plane.x - output->base.x) *
1405 output->base.current_scale;
1406 y = (output->cursor_plane.y - output->base.y) *
1407 output->base.current_scale;
Pekka Paalanen7eaed402015-11-27 14:20:58 +02001408
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001409 if (drmModeMoveCursor(b->drm.fd, output->crtc_id, x, y)) {
1410 weston_log("failed to move cursor: %m\n");
1411 b->cursors_are_broken = 1;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001412 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001413}
1414
Jesse Barnes58ef3792012-02-23 09:45:49 -05001415static void
Daniel Stoneb1f166d2017-03-01 11:34:10 +00001416drm_assign_planes(struct weston_output *output_base, void *repaint_data)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001417{
Armin Krezović545dba62016-08-05 15:54:18 +02001418 struct drm_backend *b = to_drm_backend(output_base->compositor);
1419 struct drm_output *output = to_drm_output(output_base);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001420 struct weston_view *ev, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001421 pixman_region32_t overlap, surface_overlap;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001422 struct weston_plane *primary, *next_plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001423
1424 /*
1425 * Find a surface for each sprite in the output using some heuristics:
1426 * 1) size
1427 * 2) frequency of update
1428 * 3) opacity (though some hw might support alpha blending)
1429 * 4) clipping (this can be fixed with color keys)
1430 *
1431 * The idea is to save on blitting since this should save power.
1432 * If we can get a large video surface on the sprite for example,
1433 * the main display surface may not need to update at all, and
1434 * the client buffer can be used directly for the sprite surface
1435 * as we do for flipping full screen surfaces.
1436 */
1437 pixman_region32_init(&overlap);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001438 primary = &output_base->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001439
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001440 output->cursor_view = NULL;
1441 output->cursor_plane.x = INT32_MIN;
1442 output->cursor_plane.y = INT32_MIN;
1443
Giulio Camuffo954f1832014-10-11 18:27:30 +03001444 wl_list_for_each_safe(ev, next, &output_base->compositor->view_list, link) {
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001445 struct weston_surface *es = ev->surface;
1446
1447 /* Test whether this buffer can ever go into a plane:
1448 * non-shm, or small enough to be a cursor.
1449 *
1450 * Also, keep a reference when using the pixman renderer.
1451 * That makes it possible to do a seamless switch to the GL
1452 * renderer and since the pixman renderer keeps a reference
1453 * to the buffer anyway, there is no side effects.
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001454 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001455 if (b->use_pixman ||
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001456 (es->buffer_ref.buffer &&
1457 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
Derek Foreman87430472015-10-15 10:24:48 -05001458 (ev->surface->width <= b->cursor_width &&
1459 ev->surface->height <= b->cursor_height))))
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001460 es->keep_buffer = true;
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001461 else
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001462 es->keep_buffer = false;
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001463
Jesse Barnes58ef3792012-02-23 09:45:49 -05001464 pixman_region32_init(&surface_overlap);
1465 pixman_region32_intersect(&surface_overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001466 &ev->transform.boundingbox);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001467
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001468 next_plane = NULL;
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001469 if (pixman_region32_not_empty(&surface_overlap))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001470 next_plane = primary;
1471 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001472 next_plane = drm_output_prepare_cursor_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001473 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001474 next_plane = drm_output_prepare_scanout_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001475 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001476 next_plane = drm_output_prepare_overlay_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001477 if (next_plane == NULL)
1478 next_plane = primary;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001479
Jason Ekstranda7af7042013-10-12 22:38:11 -05001480 weston_view_move_to_plane(ev, next_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001481
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001482 if (next_plane == primary)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001483 pixman_region32_union(&overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001484 &ev->transform.boundingbox);
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001485
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001486 if (next_plane == primary ||
1487 next_plane == &output->cursor_plane) {
1488 /* cursor plane involves a copy */
1489 ev->psf_flags = 0;
1490 } else {
1491 /* All other planes are a direct scanout of a
1492 * single client buffer.
1493 */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001494 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001495 }
1496
Jesse Barnes58ef3792012-02-23 09:45:49 -05001497 pixman_region32_fini(&surface_overlap);
1498 }
1499 pixman_region32_fini(&overlap);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001500}
1501
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001502/**
1503 * Find the closest-matching mode for a given target
1504 *
1505 * Given a target mode, find the most suitable mode amongst the output's
1506 * current mode list to use, preferring the current mode if possible, to
1507 * avoid an expensive mode switch.
1508 *
1509 * @param output DRM output
1510 * @param target_mode Mode to attempt to match
1511 * @returns Pointer to a mode from the output's mode list
1512 */
Alex Wub7b8bda2012-04-17 17:20:48 +08001513static struct drm_mode *
1514choose_mode (struct drm_output *output, struct weston_mode *target_mode)
1515{
1516 struct drm_mode *tmp_mode = NULL, *mode;
1517
Hardeningff39efa2013-09-18 23:56:35 +02001518 if (output->base.current_mode->width == target_mode->width &&
1519 output->base.current_mode->height == target_mode->height &&
1520 (output->base.current_mode->refresh == target_mode->refresh ||
Alex Wub7b8bda2012-04-17 17:20:48 +08001521 target_mode->refresh == 0))
Hardeningff39efa2013-09-18 23:56:35 +02001522 return (struct drm_mode *)output->base.current_mode;
Alex Wub7b8bda2012-04-17 17:20:48 +08001523
1524 wl_list_for_each(mode, &output->base.mode_list, base.link) {
1525 if (mode->mode_info.hdisplay == target_mode->width &&
1526 mode->mode_info.vdisplay == target_mode->height) {
Mario Kleiner872797c2015-06-21 21:25:09 +02001527 if (mode->base.refresh == target_mode->refresh ||
1528 target_mode->refresh == 0) {
Alex Wub7b8bda2012-04-17 17:20:48 +08001529 return mode;
Daniel Stonef556ebe2015-05-21 08:28:58 +01001530 } else if (!tmp_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001531 tmp_mode = mode;
1532 }
1533 }
1534
1535 return tmp_mode;
1536}
1537
1538static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001539drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00001540static void
1541drm_output_fini_egl(struct drm_output *output);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001542static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001543drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00001544static void
1545drm_output_fini_pixman(struct drm_output *output);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001546
1547static int
Alex Wub7b8bda2012-04-17 17:20:48 +08001548drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
1549{
1550 struct drm_output *output;
1551 struct drm_mode *drm_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001552 struct drm_backend *b;
Alex Wub7b8bda2012-04-17 17:20:48 +08001553
1554 if (output_base == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001555 weston_log("output is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001556 return -1;
1557 }
1558
1559 if (mode == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001560 weston_log("mode is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001561 return -1;
1562 }
1563
Armin Krezović545dba62016-08-05 15:54:18 +02001564 b = to_drm_backend(output_base->compositor);
1565 output = to_drm_output(output_base);
Alex Wub7b8bda2012-04-17 17:20:48 +08001566 drm_mode = choose_mode (output, mode);
1567
1568 if (!drm_mode) {
Martin Minarik6d118362012-06-07 18:01:59 +02001569 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +08001570 return -1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001571 }
1572
Hardeningff39efa2013-09-18 23:56:35 +02001573 if (&drm_mode->base == output->base.current_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001574 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001575
Hardeningff39efa2013-09-18 23:56:35 +02001576 output->base.current_mode->flags = 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001577
Hardeningff39efa2013-09-18 23:56:35 +02001578 output->base.current_mode = &drm_mode->base;
1579 output->base.current_mode->flags =
Alex Wub7b8bda2012-04-17 17:20:48 +08001580 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1581
Alex Wub7b8bda2012-04-17 17:20:48 +08001582 /* reset rendering stuff. */
Daniel Stone05a5ac22017-04-04 17:54:25 +01001583 drm_fb_unref(output->current);
1584 drm_fb_unref(output->next);
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02001585 output->current = output->next = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +08001586
Giulio Camuffo954f1832014-10-11 18:27:30 +03001587 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001588 drm_output_fini_pixman(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001589 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001590 weston_log("failed to init output pixman state with "
1591 "new mode\n");
1592 return -1;
1593 }
1594 } else {
Daniel Stone3e661f72016-11-04 17:24:06 +00001595 drm_output_fini_egl(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001596 if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001597 weston_log("failed to init output egl state with "
1598 "new mode");
1599 return -1;
1600 }
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001601 }
1602
Alex Wub7b8bda2012-04-17 17:20:48 +08001603 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001604}
1605
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001606static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001607on_drm_input(int fd, uint32_t mask, void *data)
1608{
1609 drmEventContext evctx;
1610
1611 memset(&evctx, 0, sizeof evctx);
1612 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1613 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001614 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001615 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001616
1617 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001618}
1619
1620static int
Daniel Stoneefa504f2016-12-19 16:48:20 +00001621init_kms_caps(struct drm_backend *b)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001622{
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001623 uint64_t cap;
Daniel Stoneefa504f2016-12-19 16:48:20 +00001624 int ret;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001625 clockid_t clk_id;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001626
Daniel Stoneefa504f2016-12-19 16:48:20 +00001627 weston_log("using %s\n", b->drm.filename);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001628
Daniel Stoneefa504f2016-12-19 16:48:20 +00001629 ret = drmGetCap(b->drm.fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001630 if (ret == 0 && cap == 1)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001631 clk_id = CLOCK_MONOTONIC;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001632 else
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001633 clk_id = CLOCK_REALTIME;
1634
Giulio Camuffo954f1832014-10-11 18:27:30 +03001635 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001636 weston_log("Error: failed to set presentation clock %d.\n",
1637 clk_id);
1638 return -1;
1639 }
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +02001640
Daniel Stoneefa504f2016-12-19 16:48:20 +00001641 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_WIDTH, &cap);
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001642 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001643 b->cursor_width = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001644 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001645 b->cursor_width = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001646
Daniel Stoneefa504f2016-12-19 16:48:20 +00001647 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_HEIGHT, &cap);
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001648 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001649 b->cursor_height = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001650 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001651 b->cursor_height = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001652
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001653 return 0;
1654}
1655
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001656static struct gbm_device *
1657create_gbm_device(int fd)
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001658{
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001659 struct gbm_device *gbm;
Alexandru DAMIANbe0ac5b2013-10-02 17:51:05 +01001660
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001661 gl_renderer = weston_load_module("gl-renderer.so",
1662 "gl_renderer_interface");
1663 if (!gl_renderer)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001664 return NULL;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001665
1666 /* GBM will load a dri driver, but even though they need symbols from
1667 * libglapi, in some version of Mesa they are not linked to it. Since
1668 * only the gl-renderer module links to it, the call above won't make
1669 * these symbols globally available, and loading the DRI driver fails.
1670 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
1671 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
1672
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001673 gbm = gbm_create_device(fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001674
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001675 return gbm;
1676}
1677
Bryce Harringtonc056a982015-05-19 15:25:18 -07001678/* When initializing EGL, if the preferred buffer format isn't available
Bryce Harringtonb9939982016-04-15 20:28:26 -07001679 * we may be able to substitute an ARGB format for an XRGB one.
Derek Foremanc4cfe852015-05-15 12:12:40 -05001680 *
1681 * This returns 0 if substitution isn't possible, but 0 might be a
1682 * legitimate format for other EGL platforms, so the caller is
1683 * responsible for checking for 0 before calling gl_renderer->create().
1684 *
1685 * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689
1686 * but it's entirely possible we'll see this again on other implementations.
1687 */
1688static int
1689fallback_format_for(uint32_t format)
1690{
1691 switch (format) {
1692 case GBM_FORMAT_XRGB8888:
1693 return GBM_FORMAT_ARGB8888;
1694 case GBM_FORMAT_XRGB2101010:
1695 return GBM_FORMAT_ARGB2101010;
1696 default:
1697 return 0;
1698 }
1699}
1700
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001701static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001702drm_backend_create_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001703{
Derek Foreman6d556372015-11-04 14:47:33 -06001704 EGLint format[3] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001705 b->gbm_format,
1706 fallback_format_for(b->gbm_format),
Derek Foreman6d556372015-11-04 14:47:33 -06001707 0,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001708 };
Derek Foreman6d556372015-11-04 14:47:33 -06001709 int n_formats = 2;
John Kåre Alsakeref591aa2013-03-02 12:27:39 +01001710
Derek Foremanc4cfe852015-05-15 12:12:40 -05001711 if (format[1])
Derek Foreman6d556372015-11-04 14:47:33 -06001712 n_formats = 3;
Miguel A. Vicodddc6702016-05-18 17:41:07 +02001713 if (gl_renderer->display_create(b->compositor,
1714 EGL_PLATFORM_GBM_KHR,
1715 (void *)b->gbm,
Miguel A. Vico41700e32016-05-18 17:47:59 +02001716 NULL,
Miguel A. Vicodddc6702016-05-18 17:41:07 +02001717 gl_renderer->opaque_attribs,
1718 format,
1719 n_formats) < 0) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001720 return -1;
1721 }
1722
1723 return 0;
1724}
1725
1726static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001727init_egl(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001728{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001729 b->gbm = create_gbm_device(b->drm.fd);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001730
Giulio Camuffo954f1832014-10-11 18:27:30 +03001731 if (!b->gbm)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001732 return -1;
1733
Giulio Camuffo954f1832014-10-11 18:27:30 +03001734 if (drm_backend_create_gl_renderer(b) < 0) {
1735 gbm_device_destroy(b->gbm);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001736 return -1;
1737 }
1738
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001739 return 0;
1740}
1741
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001742static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001743init_pixman(struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001744{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001745 return pixman_renderer_init(b->compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001746}
1747
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001748/**
1749 * Add a mode to output's mode list
1750 *
1751 * Copy the supplied DRM mode into a Weston mode structure, and add it to the
1752 * output's mode list.
1753 *
1754 * @param output DRM output to add mode to
1755 * @param info DRM mode structure to add
1756 * @returns Newly-allocated Weston/DRM mode structure
1757 */
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001758static struct drm_mode *
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001759drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001760{
1761 struct drm_mode *mode;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001762 uint64_t refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001763
1764 mode = malloc(sizeof *mode);
1765 if (mode == NULL)
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001766 return NULL;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001767
1768 mode->base.flags = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02001769 mode->base.width = info->hdisplay;
1770 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001771
1772 /* Calculate higher precision (mHz) refresh rate */
1773 refresh = (info->clock * 1000000LL / info->htotal +
1774 info->vtotal / 2) / info->vtotal;
1775
1776 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1777 refresh *= 2;
1778 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1779 refresh /= 2;
1780 if (info->vscan > 1)
1781 refresh /= info->vscan;
1782
1783 mode->base.refresh = refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001784 mode->mode_info = *info;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04001785
1786 if (info->type & DRM_MODE_TYPE_PREFERRED)
1787 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1788
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001789 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1790
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001791 return mode;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001792}
1793
1794static int
1795drm_subpixel_to_wayland(int drm_value)
1796{
1797 switch (drm_value) {
1798 default:
1799 case DRM_MODE_SUBPIXEL_UNKNOWN:
1800 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1801 case DRM_MODE_SUBPIXEL_NONE:
1802 return WL_OUTPUT_SUBPIXEL_NONE;
1803 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1804 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1805 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1806 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1807 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1808 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1809 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1810 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1811 }
1812}
1813
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001814/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001815static uint32_t
1816drm_get_backlight(struct drm_output *output)
1817{
1818 long brightness, max_brightness, norm;
1819
1820 brightness = backlight_get_brightness(output->backlight);
1821 max_brightness = backlight_get_max_brightness(output->backlight);
1822
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001823 /* convert it on a scale of 0 to 255 */
1824 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001825
1826 return (uint32_t) norm;
1827}
1828
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001829/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001830static void
1831drm_set_backlight(struct weston_output *output_base, uint32_t value)
1832{
Armin Krezović545dba62016-08-05 15:54:18 +02001833 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001834 long max_brightness, new_brightness;
1835
1836 if (!output->backlight)
1837 return;
1838
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001839 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001840 return;
1841
1842 max_brightness = backlight_get_max_brightness(output->backlight);
1843
1844 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001845 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001846
1847 backlight_set_brightness(output->backlight, new_brightness);
1848}
1849
1850static drmModePropertyPtr
1851drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1852{
1853 drmModePropertyPtr props;
1854 int i;
1855
1856 for (i = 0; i < connector->count_props; i++) {
1857 props = drmModeGetProperty(fd, connector->props[i]);
1858 if (!props)
1859 continue;
1860
1861 if (!strcmp(props->name, name))
1862 return props;
1863
1864 drmModeFreeProperty(props);
1865 }
1866
1867 return NULL;
1868}
1869
1870static void
1871drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1872{
Armin Krezović545dba62016-08-05 15:54:18 +02001873 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001874 struct weston_compositor *ec = output_base->compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02001875 struct drm_backend *b = to_drm_backend(ec);
Daniel Stone36609c72015-06-18 07:49:02 +01001876 int ret;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001877
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +03001878 if (!output->dpms_prop)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001879 return;
1880
Daniel Stone36609c72015-06-18 07:49:02 +01001881 ret = drmModeConnectorSetProperty(b->drm.fd, output->connector_id,
1882 output->dpms_prop->prop_id, level);
1883 if (ret) {
1884 weston_log("DRM: DPMS: failed property set for %s\n",
1885 output->base.name);
1886 return;
1887 }
1888
1889 output->dpms = level;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001890}
1891
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001892static const char * const connector_type_names[] = {
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001893 [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
1894 [DRM_MODE_CONNECTOR_VGA] = "VGA",
1895 [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
1896 [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
1897 [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
1898 [DRM_MODE_CONNECTOR_Composite] = "Composite",
1899 [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
1900 [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
1901 [DRM_MODE_CONNECTOR_Component] = "Component",
1902 [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
1903 [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
1904 [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
1905 [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
1906 [DRM_MODE_CONNECTOR_TV] = "TV",
1907 [DRM_MODE_CONNECTOR_eDP] = "eDP",
Pekka Paalanenab81f152015-08-24 14:27:07 +03001908#ifdef DRM_MODE_CONNECTOR_DSI
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001909 [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
1910 [DRM_MODE_CONNECTOR_DSI] = "DSI",
Pekka Paalanenab81f152015-08-24 14:27:07 +03001911#endif
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04001912};
1913
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001914static char *
1915make_connector_name(const drmModeConnector *con)
1916{
1917 char name[32];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001918 const char *type_name = NULL;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001919
1920 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
1921 type_name = connector_type_names[con->connector_type];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001922
1923 if (!type_name)
1924 type_name = "UNNAMED";
1925
1926 snprintf(name, sizeof name, "%s-%d", type_name, con->connector_type_id);
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001927
1928 return strdup(name);
1929}
1930
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001931static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001932find_crtc_for_connector(struct drm_backend *b,
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001933 drmModeRes *resources, drmModeConnector *connector)
1934{
1935 drmModeEncoder *encoder;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001936 int i, j;
Daniel Stone75487c22017-01-16 14:33:38 +00001937 int ret = -1;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001938
1939 for (j = 0; j < connector->count_encoders; j++) {
Daniel Stone75487c22017-01-16 14:33:38 +00001940 uint32_t possible_crtcs, encoder_id, crtc_id;
1941
Giulio Camuffo954f1832014-10-11 18:27:30 +03001942 encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001943 if (encoder == NULL) {
1944 weston_log("Failed to get encoder.\n");
Daniel Stone75487c22017-01-16 14:33:38 +00001945 continue;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001946 }
Daniel Stone75487c22017-01-16 14:33:38 +00001947 encoder_id = encoder->encoder_id;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001948 possible_crtcs = encoder->possible_crtcs;
Daniel Stone75487c22017-01-16 14:33:38 +00001949 crtc_id = encoder->crtc_id;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001950 drmModeFreeEncoder(encoder);
1951
1952 for (i = 0; i < resources->count_crtcs; i++) {
Daniel Stone72c0e1b2017-02-09 13:49:15 +00001953 if (!(possible_crtcs & (1 << i)))
1954 continue;
1955
1956 if (drm_output_find_by_crtc(b, resources->crtcs[i]))
1957 continue;
1958
Daniel Stone75487c22017-01-16 14:33:38 +00001959 /* Try to preserve the existing
1960 * CRTC -> encoder -> connector routing; it makes
1961 * initialisation faster, and also since we have a
1962 * very dumb picking algorithm, may preserve a better
1963 * choice. */
1964 if (!connector->encoder_id ||
1965 (encoder_id == connector->encoder_id &&
1966 crtc_id == resources->crtcs[i]))
1967 return i;
1968
1969 ret = i;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001970 }
1971 }
1972
Daniel Stone75487c22017-01-16 14:33:38 +00001973 return ret;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001974}
1975
Daniel Stonee4256832017-04-04 17:54:27 +01001976static void drm_output_fini_cursor_egl(struct drm_output *output)
1977{
1978 unsigned int i;
1979
1980 for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) {
1981 drm_fb_unref(output->gbm_cursor_fb[i]);
1982 output->gbm_cursor_fb[i] = NULL;
1983 }
1984}
1985
1986static int
1987drm_output_init_cursor_egl(struct drm_output *output, struct drm_backend *b)
1988{
1989 unsigned int i;
1990
1991 for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) {
1992 struct gbm_bo *bo;
1993
1994 bo = gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
1995 GBM_FORMAT_ARGB8888,
1996 GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE);
1997 if (!bo)
1998 goto err;
1999
2000 output->gbm_cursor_fb[i] =
2001 drm_fb_get_from_bo(bo, b, GBM_FORMAT_ARGB8888,
2002 BUFFER_CURSOR);
2003 if (!output->gbm_cursor_fb[i]) {
2004 gbm_bo_destroy(bo);
2005 goto err;
2006 }
2007 }
2008
2009 return 0;
2010
2011err:
2012 weston_log("cursor buffers unavailable, using gl cursors\n");
2013 b->cursors_are_broken = 1;
2014 drm_output_fini_cursor_egl(output);
2015 return -1;
2016}
2017
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002018/* Init output state that depends on gl or gbm */
2019static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002020drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002021{
Derek Foremanc4cfe852015-05-15 12:12:40 -05002022 EGLint format[2] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002023 output->gbm_format,
2024 fallback_format_for(output->gbm_format),
Derek Foremanc4cfe852015-05-15 12:12:40 -05002025 };
Daniel Stonee4256832017-04-04 17:54:27 +01002026 int n_formats = 1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02002027
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002028 output->gbm_surface = gbm_surface_create(b->gbm,
Hardeningff39efa2013-09-18 23:56:35 +02002029 output->base.current_mode->width,
2030 output->base.current_mode->height,
Derek Foremanc4cfe852015-05-15 12:12:40 -05002031 format[0],
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002032 GBM_BO_USE_SCANOUT |
2033 GBM_BO_USE_RENDERING);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002034 if (!output->gbm_surface) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002035 weston_log("failed to create gbm surface\n");
2036 return -1;
2037 }
2038
Derek Foremanc4cfe852015-05-15 12:12:40 -05002039 if (format[1])
2040 n_formats = 2;
Miguel A. Vicoc095cde2016-05-18 17:43:00 +02002041 if (gl_renderer->output_window_create(&output->base,
2042 (EGLNativeWindowType)output->gbm_surface,
2043 output->gbm_surface,
2044 gl_renderer->opaque_attribs,
2045 format,
2046 n_formats) < 0) {
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02002047 weston_log("failed to create gl renderer output state\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002048 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002049 return -1;
2050 }
2051
Daniel Stonee4256832017-04-04 17:54:27 +01002052 drm_output_init_cursor_egl(output, b);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002053
2054 return 0;
2055}
2056
Daniel Stone3e661f72016-11-04 17:24:06 +00002057static void
2058drm_output_fini_egl(struct drm_output *output)
2059{
2060 gl_renderer->output_destroy(&output->base);
2061 gbm_surface_destroy(output->gbm_surface);
Daniel Stonee4256832017-04-04 17:54:27 +01002062 drm_output_fini_cursor_egl(output);
Daniel Stone3e661f72016-11-04 17:24:06 +00002063}
2064
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002065static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002066drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002067{
Hardeningff39efa2013-09-18 23:56:35 +02002068 int w = output->base.current_mode->width;
2069 int h = output->base.current_mode->height;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002070 uint32_t format = output->gbm_format;
2071 uint32_t pixman_format;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002072 unsigned int i;
2073
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002074 switch (format) {
2075 case GBM_FORMAT_XRGB8888:
2076 pixman_format = PIXMAN_x8r8g8b8;
2077 break;
2078 case GBM_FORMAT_RGB565:
2079 pixman_format = PIXMAN_r5g6b5;
2080 break;
2081 default:
2082 weston_log("Unsupported pixman format 0x%x\n", format);
2083 return -1;
2084 }
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002085
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002086 /* FIXME error checking */
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002087 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002088 output->dumb[i] = drm_fb_create_dumb(b, w, h, format);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002089 if (!output->dumb[i])
2090 goto err;
2091
2092 output->image[i] =
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002093 pixman_image_create_bits(pixman_format, w, h,
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002094 output->dumb[i]->map,
2095 output->dumb[i]->stride);
2096 if (!output->image[i])
2097 goto err;
2098 }
2099
2100 if (pixman_renderer_output_create(&output->base) < 0)
2101 goto err;
2102
2103 pixman_region32_init_rect(&output->previous_damage,
Alexander Larsson0b135062013-05-28 16:23:36 +02002104 output->base.x, output->base.y, output->base.width, output->base.height);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002105
2106 return 0;
2107
2108err:
2109 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
2110 if (output->dumb[i])
Daniel Stone6e7a9612017-04-04 17:54:26 +01002111 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002112 if (output->image[i])
2113 pixman_image_unref(output->image[i]);
2114
2115 output->dumb[i] = NULL;
2116 output->image[i] = NULL;
2117 }
2118
2119 return -1;
2120}
2121
2122static void
2123drm_output_fini_pixman(struct drm_output *output)
2124{
2125 unsigned int i;
2126
2127 pixman_renderer_output_destroy(&output->base);
2128 pixman_region32_fini(&output->previous_damage);
2129
2130 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002131 pixman_image_unref(output->image[i]);
Daniel Stone6e7a9612017-04-04 17:54:26 +01002132 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002133 output->dumb[i] = NULL;
2134 output->image[i] = NULL;
2135 }
2136}
2137
Richard Hughes2b2092a2013-04-24 14:58:02 +01002138static void
2139edid_parse_string(const uint8_t *data, char text[])
2140{
2141 int i;
2142 int replaced = 0;
2143
2144 /* this is always 12 bytes, but we can't guarantee it's null
2145 * terminated or not junk. */
2146 strncpy(text, (const char *) data, 12);
2147
Bryce Harrington9c7de162015-08-28 13:04:26 -07002148 /* guarantee our new string is null-terminated */
2149 text[12] = '\0';
2150
Richard Hughes2b2092a2013-04-24 14:58:02 +01002151 /* remove insane chars */
2152 for (i = 0; text[i] != '\0'; i++) {
2153 if (text[i] == '\n' ||
2154 text[i] == '\r') {
2155 text[i] = '\0';
2156 break;
2157 }
2158 }
2159
2160 /* ensure string is printable */
2161 for (i = 0; text[i] != '\0'; i++) {
2162 if (!isprint(text[i])) {
2163 text[i] = '-';
2164 replaced++;
2165 }
2166 }
2167
2168 /* if the string is random junk, ignore the string */
2169 if (replaced > 4)
2170 text[0] = '\0';
2171}
2172
2173#define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
2174#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
2175#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
2176#define EDID_OFFSET_DATA_BLOCKS 0x36
2177#define EDID_OFFSET_LAST_BLOCK 0x6c
2178#define EDID_OFFSET_PNPID 0x08
2179#define EDID_OFFSET_SERIAL 0x0c
2180
2181static int
2182edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
2183{
2184 int i;
2185 uint32_t serial_number;
2186
2187 /* check header */
2188 if (length < 128)
2189 return -1;
2190 if (data[0] != 0x00 || data[1] != 0xff)
2191 return -1;
2192
2193 /* decode the PNP ID from three 5 bit words packed into 2 bytes
2194 * /--08--\/--09--\
2195 * 7654321076543210
2196 * |\---/\---/\---/
2197 * R C1 C2 C3 */
2198 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
2199 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
2200 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
2201 edid->pnp_id[3] = '\0';
2202
2203 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
2204 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
2205 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
2206 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
2207 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
2208 if (serial_number > 0)
2209 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
2210
2211 /* parse EDID data */
2212 for (i = EDID_OFFSET_DATA_BLOCKS;
2213 i <= EDID_OFFSET_LAST_BLOCK;
2214 i += 18) {
2215 /* ignore pixel clock data */
2216 if (data[i] != 0)
2217 continue;
2218 if (data[i+2] != 0)
2219 continue;
2220
2221 /* any useful blocks? */
2222 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
2223 edid_parse_string(&data[i+5],
2224 edid->monitor_name);
2225 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
2226 edid_parse_string(&data[i+5],
2227 edid->serial_number);
2228 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
2229 edid_parse_string(&data[i+5],
2230 edid->eisa_id);
2231 }
2232 }
2233 return 0;
2234}
2235
2236static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002237find_and_parse_output_edid(struct drm_backend *b,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002238 struct drm_output *output,
2239 drmModeConnector *connector)
2240{
2241 drmModePropertyBlobPtr edid_blob = NULL;
2242 drmModePropertyPtr property;
2243 int i;
2244 int rc;
2245
2246 for (i = 0; i < connector->count_props && !edid_blob; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002247 property = drmModeGetProperty(b->drm.fd, connector->props[i]);
Richard Hughes2b2092a2013-04-24 14:58:02 +01002248 if (!property)
2249 continue;
2250 if ((property->flags & DRM_MODE_PROP_BLOB) &&
2251 !strcmp(property->name, "EDID")) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002252 edid_blob = drmModeGetPropertyBlob(b->drm.fd,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002253 connector->prop_values[i]);
2254 }
2255 drmModeFreeProperty(property);
2256 }
2257 if (!edid_blob)
2258 return;
2259
2260 rc = edid_parse(&output->edid,
2261 edid_blob->data,
2262 edid_blob->length);
2263 if (!rc) {
2264 weston_log("EDID data '%s', '%s', '%s'\n",
2265 output->edid.pnp_id,
2266 output->edid.monitor_name,
2267 output->edid.serial_number);
2268 if (output->edid.pnp_id[0] != '\0')
2269 output->base.make = output->edid.pnp_id;
2270 if (output->edid.monitor_name[0] != '\0')
2271 output->base.model = output->edid.monitor_name;
2272 if (output->edid.serial_number[0] != '\0')
2273 output->base.serial_number = output->edid.serial_number;
2274 }
2275 drmModeFreePropertyBlob(edid_blob);
2276}
2277
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002278
2279
2280static int
2281parse_modeline(const char *s, drmModeModeInfo *mode)
2282{
2283 char hsync[16];
2284 char vsync[16];
2285 float fclock;
2286
2287 mode->type = DRM_MODE_TYPE_USERDEF;
2288 mode->hskew = 0;
2289 mode->vscan = 0;
2290 mode->vrefresh = 0;
2291 mode->flags = 0;
2292
Rob Bradford307e09e2013-07-26 16:29:40 +01002293 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002294 &fclock,
2295 &mode->hdisplay,
2296 &mode->hsync_start,
2297 &mode->hsync_end,
2298 &mode->htotal,
2299 &mode->vdisplay,
2300 &mode->vsync_start,
2301 &mode->vsync_end,
2302 &mode->vtotal, hsync, vsync) != 11)
2303 return -1;
2304
2305 mode->clock = fclock * 1000;
2306 if (strcmp(hsync, "+hsync") == 0)
2307 mode->flags |= DRM_MODE_FLAG_PHSYNC;
2308 else if (strcmp(hsync, "-hsync") == 0)
2309 mode->flags |= DRM_MODE_FLAG_NHSYNC;
2310 else
2311 return -1;
2312
2313 if (strcmp(vsync, "+vsync") == 0)
2314 mode->flags |= DRM_MODE_FLAG_PVSYNC;
2315 else if (strcmp(vsync, "-vsync") == 0)
2316 mode->flags |= DRM_MODE_FLAG_NVSYNC;
2317 else
2318 return -1;
2319
Emmanuel Gil Peyrota62138b2016-05-02 22:40:11 +01002320 snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f",
2321 mode->hdisplay, mode->vdisplay, fclock);
2322
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002323 return 0;
2324}
2325
Rob Bradford66bd9f52013-06-25 18:56:42 +01002326static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002327setup_output_seat_constraint(struct drm_backend *b,
Rob Bradford66bd9f52013-06-25 18:56:42 +01002328 struct weston_output *output,
2329 const char *s)
2330{
2331 if (strcmp(s, "") != 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05002332 struct weston_pointer *pointer;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002333 struct udev_seat *seat;
2334
Giulio Camuffo954f1832014-10-11 18:27:30 +03002335 seat = udev_seat_get_named(&b->input, s);
Derek Foreman0720ea32015-07-15 13:00:35 -05002336 if (!seat)
2337 return;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002338
Derek Foreman0720ea32015-07-15 13:00:35 -05002339 seat->base.output = output;
2340
Derek Foreman1281a362015-07-31 16:55:32 -05002341 pointer = weston_seat_get_pointer(&seat->base);
2342 if (pointer)
2343 weston_pointer_clamp(pointer,
2344 &pointer->x,
2345 &pointer->y);
Rob Bradford66bd9f52013-06-25 18:56:42 +01002346 }
2347}
2348
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002349static int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002350parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format)
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002351{
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002352 int ret = 0;
2353
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002354 if (s == NULL)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002355 *gbm_format = default_value;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002356 else if (strcmp(s, "xrgb8888") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002357 *gbm_format = GBM_FORMAT_XRGB8888;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002358 else if (strcmp(s, "rgb565") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002359 *gbm_format = GBM_FORMAT_RGB565;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002360 else if (strcmp(s, "xrgb2101010") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002361 *gbm_format = GBM_FORMAT_XRGB2101010;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002362 else {
2363 weston_log("fatal: unrecognized pixel format: %s\n", s);
2364 ret = -1;
2365 }
2366
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002367 return ret;
2368}
2369
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002370/**
2371 * Choose suitable mode for an output
2372 *
2373 * Find the most suitable mode to use for initial setup (or reconfiguration on
2374 * hotplug etc) for a DRM output.
2375 *
2376 * @param output DRM output to choose mode for
2377 * @param kind Strategy and preference to use when choosing mode
2378 * @param width Desired width for this output
2379 * @param height Desired height for this output
2380 * @param current_mode Mode currently being displayed on this output
2381 * @param modeline Manually-entered mode (may be NULL)
2382 * @returns A mode from the output's mode list, or NULL if none available
2383 */
2384static struct drm_mode *
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002385drm_output_choose_initial_mode(struct drm_backend *backend,
2386 struct drm_output *output,
2387 enum weston_drm_backend_output_mode mode,
Armin Krezović08368132016-09-30 14:11:05 +02002388 const char *modeline,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002389 const drmModeModeInfo *current_mode)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002390{
2391 struct drm_mode *preferred = NULL;
2392 struct drm_mode *current = NULL;
2393 struct drm_mode *configured = NULL;
2394 struct drm_mode *best = NULL;
2395 struct drm_mode *drm_mode;
Armin Krezović08368132016-09-30 14:11:05 +02002396 drmModeModeInfo drm_modeline;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002397 int32_t width = 0;
2398 int32_t height = 0;
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01002399 uint32_t refresh = 0;
2400 int n;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002401
Armin Krezović08368132016-09-30 14:11:05 +02002402 if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) {
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01002403 n = sscanf(modeline, "%dx%d@%d", &width, &height, &refresh);
2404 if (n != 2 && n != 3) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002405 width = -1;
2406
Armin Krezović08368132016-09-30 14:11:05 +02002407 if (parse_modeline(modeline, &drm_modeline) == 0) {
2408 configured = drm_output_add_mode(output, &drm_modeline);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002409 if (!configured)
2410 return NULL;
2411 } else {
2412 weston_log("Invalid modeline \"%s\" for output %s\n",
Armin Krezović08368132016-09-30 14:11:05 +02002413 modeline, output->base.name);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002414 }
2415 }
2416 }
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002417
2418 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002419 if (width == drm_mode->base.width &&
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01002420 height == drm_mode->base.height &&
2421 (refresh == 0 || refresh == drm_mode->mode_info.vrefresh))
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002422 configured = drm_mode;
2423
comic fans7a5c5622016-03-17 14:29:27 +02002424 if (memcmp(current_mode, &drm_mode->mode_info,
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002425 sizeof *current_mode) == 0)
2426 current = drm_mode;
2427
2428 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
2429 preferred = drm_mode;
2430
2431 best = drm_mode;
2432 }
2433
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002434 if (current == NULL && current_mode->clock != 0) {
2435 current = drm_output_add_mode(output, current_mode);
2436 if (!current)
2437 return NULL;
2438 }
2439
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002440 if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002441 configured = current;
2442
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002443 if (configured)
2444 return configured;
2445
2446 if (preferred)
2447 return preferred;
2448
2449 if (current)
2450 return current;
2451
2452 if (best)
2453 return best;
2454
2455 weston_log("no available modes for %s\n", output->base.name);
2456 return NULL;
2457}
2458
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002459static int
2460connector_get_current_mode(drmModeConnector *connector, int drm_fd,
2461 drmModeModeInfo *mode)
2462{
2463 drmModeEncoder *encoder;
2464 drmModeCrtc *crtc;
2465
2466 /* Get the current mode on the crtc that's currently driving
2467 * this connector. */
2468 encoder = drmModeGetEncoder(drm_fd, connector->encoder_id);
2469 memset(mode, 0, sizeof *mode);
2470 if (encoder != NULL) {
2471 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
2472 drmModeFreeEncoder(encoder);
2473 if (crtc == NULL)
2474 return -1;
2475 if (crtc->mode_valid)
2476 *mode = crtc->mode;
2477 drmModeFreeCrtc(crtc);
2478 }
2479
2480 return 0;
2481}
2482
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002483static int
Armin Krezović08368132016-09-30 14:11:05 +02002484drm_output_set_mode(struct weston_output *base,
2485 enum weston_drm_backend_output_mode mode,
2486 const char *modeline)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002487{
Armin Krezović08368132016-09-30 14:11:05 +02002488 struct drm_output *output = to_drm_output(base);
2489 struct drm_backend *b = to_drm_backend(base->compositor);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002490
Armin Krezović445b41b2016-10-09 23:48:16 +02002491 struct drm_mode *current;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002492 drmModeModeInfo crtc_mode;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002493
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002494 output->base.make = "unknown";
2495 output->base.model = "unknown";
Richard Hughes2b2092a2013-04-24 14:58:02 +01002496 output->base.serial_number = "unknown";
Matt Roper361d2ad2011-08-29 13:52:23 -07002497
Armin Krezović08368132016-09-30 14:11:05 +02002498 if (connector_get_current_mode(output->connector, b->drm.fd, &crtc_mode) < 0)
Armin Krezović445b41b2016-10-09 23:48:16 +02002499 return -1;
David Herrmann0f0d54e2011-12-08 17:05:45 +01002500
Armin Krezović08368132016-09-30 14:11:05 +02002501 current = drm_output_choose_initial_mode(b, output, mode, modeline, &crtc_mode);
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002502 if (!current)
Armin Krezović445b41b2016-10-09 23:48:16 +02002503 return -1;
Armin Krezović08368132016-09-30 14:11:05 +02002504
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002505 output->base.current_mode = &current->base;
Hardeningff39efa2013-09-18 23:56:35 +02002506 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
Wang Quanxianacb805a2012-07-30 18:09:46 -04002507
Armin Krezović08368132016-09-30 14:11:05 +02002508 /* Set native_ fields, so weston_output_mode_switch_to_native() works */
2509 output->base.native_mode = output->base.current_mode;
2510 output->base.native_scale = output->base.current_scale;
2511
2512 output->base.mm_width = output->connector->mmWidth;
2513 output->base.mm_height = output->connector->mmHeight;
2514
2515 return 0;
Armin Krezović08368132016-09-30 14:11:05 +02002516}
2517
2518static void
2519drm_output_set_gbm_format(struct weston_output *base,
2520 const char *gbm_format)
2521{
2522 struct drm_output *output = to_drm_output(base);
2523 struct drm_backend *b = to_drm_backend(base->compositor);
2524
2525 if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1)
2526 output->gbm_format = b->gbm_format;
2527}
2528
2529static void
2530drm_output_set_seat(struct weston_output *base,
2531 const char *seat)
2532{
2533 struct drm_output *output = to_drm_output(base);
2534 struct drm_backend *b = to_drm_backend(base->compositor);
2535
2536 setup_output_seat_constraint(b, &output->base,
2537 seat ? seat : "");
2538}
2539
2540static int
2541drm_output_enable(struct weston_output *base)
2542{
2543 struct drm_output *output = to_drm_output(base);
2544 struct drm_backend *b = to_drm_backend(base->compositor);
2545 struct weston_mode *m;
2546
2547 output->dpms_prop = drm_get_prop(b->drm.fd, output->connector, "DPMS");
John Kåre Alsaker94659272012-11-13 19:10:18 +01002548
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00002549 if (b->pageflip_timeout)
2550 drm_output_pageflip_timer_create(output);
2551
Giulio Camuffo954f1832014-10-11 18:27:30 +03002552 if (b->use_pixman) {
2553 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002554 weston_log("Failed to init output pixman state\n");
Armin Krezović08368132016-09-30 14:11:05 +02002555 goto err_free;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002556 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03002557 } else if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002558 weston_log("Failed to init output gl state\n");
Armin Krezović08368132016-09-30 14:11:05 +02002559 goto err_free;
Kristian Høgsberg1d1e0a52012-10-21 13:29:26 -04002560 }
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04002561
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002562 if (output->backlight) {
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002563 weston_log("Initialized backlight, device %s\n",
2564 output->backlight->path);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002565 output->base.set_backlight = drm_set_backlight;
2566 output->base.backlight_current = drm_get_backlight(output);
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002567 } else {
2568 weston_log("Failed to initialize backlight\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002569 }
2570
Jonas Ådahle5a12252013-04-05 23:07:11 +02002571 output->base.start_repaint_loop = drm_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05002572 output->base.repaint = drm_output_repaint;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002573 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002574 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08002575 output->base.switch_mode = drm_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002576
Richard Hughese7299962013-05-01 21:52:12 +01002577 output->base.gamma_size = output->original_crtc->gamma_size;
2578 output->base.set_gamma = drm_output_set_gamma;
2579
Armin Krezović08368132016-09-30 14:11:05 +02002580 output->base.subpixel = drm_subpixel_to_wayland(output->connector->subpixel);
2581
2582 find_and_parse_output_edid(b, output, output->connector);
Armin Krezović9bcf4c52017-02-08 13:55:26 +01002583 if (output->connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
2584 output->connector->connector_type == DRM_MODE_CONNECTOR_eDP)
Armin Krezović08368132016-09-30 14:11:05 +02002585 output->base.connection_internal = 1;
2586
Derek Foremanb0427562016-02-05 15:55:20 -06002587 weston_plane_init(&output->cursor_plane, b->compositor,
2588 INT32_MIN, INT32_MIN);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002589 weston_plane_init(&output->fb_plane, b->compositor, 0, 0);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002590
Giulio Camuffo954f1832014-10-11 18:27:30 +03002591 weston_compositor_stack_plane(b->compositor, &output->cursor_plane, NULL);
2592 weston_compositor_stack_plane(b->compositor, &output->fb_plane,
2593 &b->compositor->primary_plane);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002594
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04002595 weston_log("Output %s, (connector %d, crtc %d)\n",
Richard Hughesafe690c2013-05-02 10:10:04 +01002596 output->base.name, output->connector_id, output->crtc_id);
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002597 wl_list_for_each(m, &output->base.mode_list, link)
U. Artie Eoffd3ed6cb2014-01-10 10:15:17 -08002598 weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n",
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002599 m->width, m->height, m->refresh / 1000.0,
2600 m->flags & WL_OUTPUT_MODE_PREFERRED ?
2601 ", preferred" : "",
2602 m->flags & WL_OUTPUT_MODE_CURRENT ?
2603 ", current" : "",
Armin Krezović08368132016-09-30 14:11:05 +02002604 output->connector->count_modes == 0 ?
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002605 ", built-in" : "");
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002606
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002607 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01002608
2609err_free:
Armin Krezović08368132016-09-30 14:11:05 +02002610 drmModeFreeProperty(output->dpms_prop);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002611
David Herrmann0f0d54e2011-12-08 17:05:45 +01002612 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002613}
2614
Jesse Barnes58ef3792012-02-23 09:45:49 -05002615static void
Armin Krezović08368132016-09-30 14:11:05 +02002616drm_output_deinit(struct weston_output *base)
2617{
2618 struct drm_output *output = to_drm_output(base);
2619 struct drm_backend *b = to_drm_backend(base->compositor);
2620
Daniel Stone6e7a9612017-04-04 17:54:26 +01002621 /* output->next must not be set here;
2622 * destroy_pending/disable_pending exist to guarantee exactly this. */
2623 assert(!output->next);
2624 drm_fb_unref(output->current);
2625 output->current = NULL;
2626
Daniel Stone3e661f72016-11-04 17:24:06 +00002627 if (b->use_pixman)
Armin Krezović08368132016-09-30 14:11:05 +02002628 drm_output_fini_pixman(output);
Daniel Stone3e661f72016-11-04 17:24:06 +00002629 else
2630 drm_output_fini_egl(output);
Armin Krezović08368132016-09-30 14:11:05 +02002631
2632 weston_plane_release(&output->fb_plane);
2633 weston_plane_release(&output->cursor_plane);
2634
2635 drmModeFreeProperty(output->dpms_prop);
2636
2637 /* Turn off hardware cursor */
2638 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
2639}
2640
2641static void
2642drm_output_destroy(struct weston_output *base)
2643{
2644 struct drm_output *output = to_drm_output(base);
2645 struct drm_backend *b = to_drm_backend(base->compositor);
Armin Krezović445b41b2016-10-09 23:48:16 +02002646 struct drm_mode *drm_mode, *next;
Armin Krezović08368132016-09-30 14:11:05 +02002647 drmModeCrtcPtr origcrtc = output->original_crtc;
2648
2649 if (output->page_flip_pending) {
2650 output->destroy_pending = 1;
2651 weston_log("destroy output while page flip pending\n");
2652 return;
2653 }
2654
2655 if (output->base.enabled)
2656 drm_output_deinit(&output->base);
2657
Armin Krezović445b41b2016-10-09 23:48:16 +02002658 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
2659 base.link) {
2660 wl_list_remove(&drm_mode->base.link);
2661 free(drm_mode);
2662 }
2663
Armin Krezović08368132016-09-30 14:11:05 +02002664 if (origcrtc) {
2665 /* Restore original CRTC state */
2666 drmModeSetCrtc(b->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
2667 origcrtc->x, origcrtc->y,
2668 &output->connector_id, 1, &origcrtc->mode);
2669 drmModeFreeCrtc(origcrtc);
2670 }
2671
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00002672 if (output->pageflip_timer)
2673 wl_event_source_remove(output->pageflip_timer);
2674
Armin Krezović08368132016-09-30 14:11:05 +02002675 weston_output_destroy(&output->base);
2676
2677 drmModeFreeConnector(output->connector);
2678
2679 if (output->backlight)
2680 backlight_destroy(output->backlight);
2681
Armin Krezović08368132016-09-30 14:11:05 +02002682 free(output);
2683}
2684
2685static int
2686drm_output_disable(struct weston_output *base)
2687{
2688 struct drm_output *output = to_drm_output(base);
2689 struct drm_backend *b = to_drm_backend(base->compositor);
2690
2691 if (output->page_flip_pending) {
2692 output->disable_pending = 1;
2693 return -1;
2694 }
2695
2696 if (output->base.enabled)
2697 drm_output_deinit(&output->base);
2698
2699 output->disable_pending = 0;
2700
2701 weston_log("Disabling output %s\n", output->base.name);
2702 drmModeSetCrtc(b->drm.fd, output->crtc_id,
2703 0, 0, 0, 0, 0, NULL);
2704
2705 return 0;
2706}
2707
2708/**
2709 * Create a Weston output structure
2710 *
2711 * Given a DRM connector, create a matching drm_output structure and add it
2712 * to Weston's output list. It also takes ownership of the connector, which
2713 * is released when output is destroyed.
2714 *
2715 * @param b Weston backend structure
2716 * @param resources DRM resources for this device
2717 * @param connector DRM connector to use for this new output
2718 * @param drm_device udev device pointer
2719 * @returns 0 on success, or -1 on failure
2720 */
2721static int
2722create_output_for_connector(struct drm_backend *b,
2723 drmModeRes *resources,
2724 drmModeConnector *connector,
2725 struct udev_device *drm_device)
2726{
2727 struct drm_output *output;
Armin Krezović445b41b2016-10-09 23:48:16 +02002728 struct drm_mode *drm_mode;
Armin Krezović08368132016-09-30 14:11:05 +02002729 int i;
2730
2731 i = find_crtc_for_connector(b, resources, connector);
2732 if (i < 0) {
2733 weston_log("No usable crtc/encoder pair for connector.\n");
Armin Krezović445b41b2016-10-09 23:48:16 +02002734 goto err;
Armin Krezović08368132016-09-30 14:11:05 +02002735 }
2736
2737 output = zalloc(sizeof *output);
2738 if (output == NULL)
Armin Krezović445b41b2016-10-09 23:48:16 +02002739 goto err;
Armin Krezović08368132016-09-30 14:11:05 +02002740
2741 output->connector = connector;
2742 output->crtc_id = resources->crtcs[i];
2743 output->pipe = i;
2744 output->connector_id = connector->connector_id;
2745
2746 output->backlight = backlight_init(drm_device,
2747 connector->connector_type);
2748
Armin Krezović445b41b2016-10-09 23:48:16 +02002749 output->original_crtc = drmModeGetCrtc(b->drm.fd, output->crtc_id);
2750
Armin Krezović08368132016-09-30 14:11:05 +02002751 output->base.enable = drm_output_enable;
2752 output->base.destroy = drm_output_destroy;
2753 output->base.disable = drm_output_disable;
2754 output->base.name = make_connector_name(connector);
2755
2756 output->destroy_pending = 0;
2757 output->disable_pending = 0;
Armin Krezović08368132016-09-30 14:11:05 +02002758
Armin Krezović40087402016-09-30 14:11:12 +02002759 weston_output_init(&output->base, b->compositor);
Armin Krezović445b41b2016-10-09 23:48:16 +02002760
2761 wl_list_init(&output->base.mode_list);
2762
2763 for (i = 0; i < output->connector->count_modes; i++) {
2764 drm_mode = drm_output_add_mode(output, &output->connector->modes[i]);
2765 if (!drm_mode) {
2766 drm_output_destroy(&output->base);
2767 return -1;
2768 }
2769 }
2770
Armin Krezović08368132016-09-30 14:11:05 +02002771 weston_compositor_add_pending_output(&output->base, b->compositor);
2772
2773 return 0;
Armin Krezović445b41b2016-10-09 23:48:16 +02002774
2775err:
2776 drmModeFreeConnector(connector);
2777
2778 return -1;
Armin Krezović08368132016-09-30 14:11:05 +02002779}
2780
2781static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002782create_sprites(struct drm_backend *b)
Jesse Barnes58ef3792012-02-23 09:45:49 -05002783{
2784 struct drm_sprite *sprite;
2785 drmModePlaneRes *plane_res;
2786 drmModePlane *plane;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002787 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002788
Giulio Camuffo954f1832014-10-11 18:27:30 +03002789 plane_res = drmModeGetPlaneResources(b->drm.fd);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002790 if (!plane_res) {
Martin Minarik6d118362012-06-07 18:01:59 +02002791 weston_log("failed to get plane resources: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002792 strerror(errno));
2793 return;
2794 }
2795
2796 for (i = 0; i < plane_res->count_planes; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002797 plane = drmModeGetPlane(b->drm.fd, plane_res->planes[i]);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002798 if (!plane)
2799 continue;
2800
Peter Huttererf3d62272013-08-08 11:57:05 +10002801 sprite = zalloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
Jesse Barnes58ef3792012-02-23 09:45:49 -05002802 plane->count_formats));
2803 if (!sprite) {
Martin Minarik6d118362012-06-07 18:01:59 +02002804 weston_log("%s: out of memory\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002805 __func__);
Chris Michael8b376872014-01-02 11:39:40 +00002806 drmModeFreePlane(plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002807 continue;
2808 }
2809
Jesse Barnes58ef3792012-02-23 09:45:49 -05002810 sprite->possible_crtcs = plane->possible_crtcs;
2811 sprite->plane_id = plane->plane_id;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02002812 sprite->current = NULL;
2813 sprite->next = NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002814 sprite->backend = b;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002815 sprite->count_formats = plane->count_formats;
2816 memcpy(sprite->formats, plane->formats,
Rob Clark8efbc8e2012-03-11 19:48:44 -05002817 plane->count_formats * sizeof(plane->formats[0]));
Jesse Barnes58ef3792012-02-23 09:45:49 -05002818 drmModeFreePlane(plane);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002819 weston_plane_init(&sprite->plane, b->compositor, 0, 0);
2820 weston_compositor_stack_plane(b->compositor, &sprite->plane,
2821 &b->compositor->primary_plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002822
Giulio Camuffo954f1832014-10-11 18:27:30 +03002823 wl_list_insert(&b->sprite_list, &sprite->link);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002824 }
2825
Samuel Iglesias Gonsalvezde466732013-06-13 10:03:33 +02002826 drmModeFreePlaneResources(plane_res);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002827}
2828
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002829static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002830destroy_sprites(struct drm_backend *backend)
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002831{
2832 struct drm_sprite *sprite, *next;
2833 struct drm_output *output;
2834
Giulio Camuffo954f1832014-10-11 18:27:30 +03002835 output = container_of(backend->compositor->output_list.next,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002836 struct drm_output, base.link);
2837
Giulio Camuffo954f1832014-10-11 18:27:30 +03002838 wl_list_for_each_safe(sprite, next, &backend->sprite_list, link) {
2839 drmModeSetPlane(backend->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002840 sprite->plane_id,
2841 output->crtc_id, 0, 0,
2842 0, 0, 0, 0, 0, 0, 0, 0);
Daniel Stone05a5ac22017-04-04 17:54:25 +01002843 drm_fb_unref(sprite->current);
2844 drm_fb_unref(sprite->next);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002845 weston_plane_release(&sprite->plane);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002846 free(sprite);
2847 }
2848}
Jesse Barnes58ef3792012-02-23 09:45:49 -05002849
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002850static int
Ucan, Emre (ADITG/SW1)9a200d72017-02-02 14:06:56 +00002851create_outputs(struct drm_backend *b, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002852{
2853 drmModeConnector *connector;
2854 drmModeRes *resources;
2855 int i;
2856
Giulio Camuffo954f1832014-10-11 18:27:30 +03002857 resources = drmModeGetResources(b->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002858 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002859 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002860 return -1;
2861 }
2862
Giulio Camuffo954f1832014-10-11 18:27:30 +03002863 b->min_width = resources->min_width;
2864 b->max_width = resources->max_width;
2865 b->min_height = resources->min_height;
2866 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05002867
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002868 for (i = 0; i < resources->count_connectors; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002869 connector = drmModeGetConnector(b->drm.fd,
Benjamin Franzke117483d2011-08-30 11:38:26 +02002870 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002871 if (connector == NULL)
2872 continue;
2873
2874 if (connector->connection == DRM_MODE_CONNECTED &&
Ucan, Emre (ADITG/SW1)9a200d72017-02-02 14:06:56 +00002875 (b->connector == 0 ||
2876 connector->connector_id == b->connector)) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002877 if (create_output_for_connector(b, resources,
Armin Krezović445b41b2016-10-09 23:48:16 +02002878 connector, drm_device) < 0)
Benjamin Franzke439d9862011-10-07 08:20:53 +02002879 continue;
Armin Krezović08368132016-09-30 14:11:05 +02002880 } else {
2881 drmModeFreeConnector(connector);
Benjamin Franzke9eaee352011-08-02 13:03:54 +02002882 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002883 }
2884
Armin Krezović08368132016-09-30 14:11:05 +02002885 if (wl_list_empty(&b->compositor->output_list) &&
2886 wl_list_empty(&b->compositor->pending_output_list))
Martin Minarik6d118362012-06-07 18:01:59 +02002887 weston_log("No currently active connector found.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002888
2889 drmModeFreeResources(resources);
2890
2891 return 0;
2892}
2893
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002894static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002895update_outputs(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002896{
2897 drmModeConnector *connector;
2898 drmModeRes *resources;
2899 struct drm_output *output, *next;
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002900 uint32_t *connected;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002901 int i;
2902
Giulio Camuffo954f1832014-10-11 18:27:30 +03002903 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002904 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002905 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002906 return;
2907 }
2908
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002909 connected = calloc(resources->count_connectors, sizeof(uint32_t));
2910 if (!connected) {
2911 drmModeFreeResources(resources);
2912 return;
2913 }
2914
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002915 /* collect new connects */
2916 for (i = 0; i < resources->count_connectors; i++) {
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +00002917 uint32_t connector_id = resources->connectors[i];
Benjamin Franzke117483d2011-08-30 11:38:26 +02002918
Giulio Camuffo954f1832014-10-11 18:27:30 +03002919 connector = drmModeGetConnector(b->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01002920 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002921 continue;
2922
David Herrmann7551cff2011-12-08 17:05:43 +01002923 if (connector->connection != DRM_MODE_CONNECTED) {
2924 drmModeFreeConnector(connector);
2925 continue;
2926 }
2927
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +00002928 if (b->connector && (b->connector != connector_id)) {
2929 drmModeFreeConnector(connector);
2930 continue;
2931 }
2932
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002933 connected[i] = connector_id;
Benjamin Franzke117483d2011-08-30 11:38:26 +02002934
Daniel Stonec0ec7592017-02-09 13:58:35 +00002935 if (drm_output_find_by_connector(b, connector_id)) {
Armin Krezović08368132016-09-30 14:11:05 +02002936 drmModeFreeConnector(connector);
Daniel Stonec0ec7592017-02-09 13:58:35 +00002937 continue;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002938 }
Daniel Stonec0ec7592017-02-09 13:58:35 +00002939
2940 create_output_for_connector(b, resources,
2941 connector, drm_device);
2942 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002943 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002944
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002945 wl_list_for_each_safe(output, next, &b->compositor->output_list,
2946 base.link) {
2947 bool disconnected = true;
2948
2949 for (i = 0; i < resources->count_connectors; i++) {
2950 if (connected[i] == output->connector_id) {
2951 disconnected = false;
2952 break;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002953 }
2954 }
Armin Krezović08368132016-09-30 14:11:05 +02002955
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002956 if (!disconnected)
2957 continue;
2958
2959 weston_log("connector %d disconnected\n", output->connector_id);
2960 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002961 }
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002962
2963 wl_list_for_each_safe(output, next, &b->compositor->pending_output_list,
2964 base.link) {
2965 bool disconnected = true;
2966
2967 for (i = 0; i < resources->count_connectors; i++) {
2968 if (connected[i] == output->connector_id) {
2969 disconnected = false;
2970 break;
2971 }
2972 }
2973
2974 if (!disconnected)
2975 continue;
2976
2977 weston_log("connector %d disconnected\n", output->connector_id);
2978 drm_output_destroy(&output->base);
2979 }
2980
2981 free(connected);
2982 drmModeFreeResources(resources);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002983}
2984
2985static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002986udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002987{
David Herrmannd7488c22012-03-11 20:05:21 +01002988 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01002989 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01002990
2991 sysnum = udev_device_get_sysnum(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002992 if (!sysnum || atoi(sysnum) != b->drm.id)
David Herrmannd7488c22012-03-11 20:05:21 +01002993 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02002994
David Herrmann6ac52db2012-03-11 20:05:22 +01002995 val = udev_device_get_property_value(device, "HOTPLUG");
2996 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002997 return 0;
2998
David Herrmann6ac52db2012-03-11 20:05:22 +01002999 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003000}
3001
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003002static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003003udev_drm_event(int fd, uint32_t mask, void *data)
3004{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003005 struct drm_backend *b = data;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003006 struct udev_device *event;
3007
Giulio Camuffo954f1832014-10-11 18:27:30 +03003008 event = udev_monitor_receive_device(b->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02003009
Giulio Camuffo954f1832014-10-11 18:27:30 +03003010 if (udev_event_is_hotplug(b, event))
3011 update_outputs(b, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003012
3013 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003014
3015 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003016}
3017
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003018static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003019drm_restore(struct weston_compositor *ec)
3020{
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003021 weston_launcher_restore(ec->launcher);
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003022}
3023
3024static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003025drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003026{
Armin Krezović545dba62016-08-05 15:54:18 +02003027 struct drm_backend *b = to_drm_backend(ec);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003028
Giulio Camuffo954f1832014-10-11 18:27:30 +03003029 udev_input_destroy(&b->input);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003030
Giulio Camuffo954f1832014-10-11 18:27:30 +03003031 wl_event_source_remove(b->udev_drm_source);
3032 wl_event_source_remove(b->drm_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003033
Giulio Camuffo954f1832014-10-11 18:27:30 +03003034 destroy_sprites(b);
Kristian Høgsberg3d64a3e2013-05-10 12:36:04 -04003035
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03003036 weston_compositor_shutdown(ec);
3037
Giulio Camuffo954f1832014-10-11 18:27:30 +03003038 if (b->gbm)
3039 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003040
Giulio Camuffo954f1832014-10-11 18:27:30 +03003041 weston_launcher_destroy(ec->launcher);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003042
Giulio Camuffo954f1832014-10-11 18:27:30 +03003043 close(b->drm.fd);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003044 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003045}
3046
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003047static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003048session_notify(struct wl_listener *listener, void *data)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003049{
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003050 struct weston_compositor *compositor = data;
Armin Krezović545dba62016-08-05 15:54:18 +02003051 struct drm_backend *b = to_drm_backend(compositor);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05003052 struct drm_sprite *sprite;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003053 struct drm_output *output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003054
Giulio Camuffo954f1832014-10-11 18:27:30 +03003055 if (compositor->session_active) {
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003056 weston_log("activating session\n");
Daniel Stonef33e1042016-11-05 08:10:13 +00003057 weston_compositor_wake(compositor);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003058 weston_compositor_damage_all(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003059 udev_input_enable(&b->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003060 } else {
3061 weston_log("deactivating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03003062 udev_input_disable(&b->input);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04003063
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003064 weston_compositor_offscreen(compositor);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04003065
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05003066 /* If we have a repaint scheduled (either from a
3067 * pending pageflip or the idle handler), make sure we
3068 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003069 * vt switched away. The OFFSCREEN state will prevent
Abdur Rehman4dca0e12017-01-01 19:46:35 +05003070 * further attempts at repainting. When we switch
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05003071 * back, we schedule a repaint, which will process
3072 * pending frame callbacks. */
3073
Giulio Camuffo954f1832014-10-11 18:27:30 +03003074 wl_list_for_each(output, &compositor->output_list, base.link) {
Daniel Stone09a97e22017-03-01 11:34:06 +00003075 output->base.repaint_needed = false;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003076 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05003077 }
3078
Giulio Camuffo954f1832014-10-11 18:27:30 +03003079 output = container_of(compositor->output_list.next,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003080 struct drm_output, base.link);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05003081
Giulio Camuffo954f1832014-10-11 18:27:30 +03003082 wl_list_for_each(sprite, &b->sprite_list, link)
3083 drmModeSetPlane(b->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05003084 sprite->plane_id,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003085 output->crtc_id, 0, 0,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05003086 0, 0, 0, 0, 0, 0, 0, 0);
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003087 };
3088}
3089
Daniel Stoneefa504f2016-12-19 16:48:20 +00003090/**
3091 * Determines whether or not a device is capable of modesetting. If successful,
3092 * sets b->drm.fd and b->drm.filename to the opened device.
3093 */
3094static bool
3095drm_device_is_kms(struct drm_backend *b, struct udev_device *device)
3096{
3097 const char *filename = udev_device_get_devnode(device);
3098 const char *sysnum = udev_device_get_sysnum(device);
3099 drmModeRes *res;
3100 int id, fd;
3101
3102 if (!filename)
3103 return false;
3104
3105 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
3106 if (fd < 0)
3107 return false;
3108
3109 res = drmModeGetResources(fd);
3110 if (!res)
3111 goto out_fd;
3112
3113 if (res->count_crtcs <= 0 || res->count_connectors <= 0 ||
3114 res->count_encoders <= 0)
3115 goto out_res;
3116
3117 if (sysnum)
3118 id = atoi(sysnum);
3119 if (!sysnum || id < 0) {
3120 weston_log("couldn't get sysnum for device %s\n", filename);
3121 goto out_res;
3122 }
3123
3124 /* We can be called successfully on multiple devices; if we have,
3125 * clean up old entries. */
3126 if (b->drm.fd >= 0)
3127 weston_launcher_close(b->compositor->launcher, b->drm.fd);
3128 free(b->drm.filename);
3129
3130 b->drm.fd = fd;
3131 b->drm.id = id;
3132 b->drm.filename = strdup(filename);
3133
Sergi Granellceb59812017-03-28 12:44:04 +02003134 drmModeFreeResources(res);
3135
Daniel Stoneefa504f2016-12-19 16:48:20 +00003136 return true;
3137
3138out_res:
3139 drmModeFreeResources(res);
3140out_fd:
3141 weston_launcher_close(b->compositor->launcher, fd);
3142 return false;
3143}
3144
David Herrmann0af066f2012-10-29 19:21:16 +01003145/*
3146 * Find primary GPU
3147 * Some systems may have multiple DRM devices attached to a single seat. This
3148 * function loops over all devices and tries to find a PCI device with the
3149 * boot_vga sysfs attribute set to 1.
3150 * If no such device is found, the first DRM device reported by udev is used.
Daniel Stoneefa504f2016-12-19 16:48:20 +00003151 * Devices are also vetted to make sure they are are capable of modesetting,
3152 * rather than pure render nodes (GPU with no display), or pure
3153 * memory-allocation devices (VGEM).
David Herrmann0af066f2012-10-29 19:21:16 +01003154 */
3155static struct udev_device*
Giulio Camuffo954f1832014-10-11 18:27:30 +03003156find_primary_gpu(struct drm_backend *b, const char *seat)
David Herrmann0af066f2012-10-29 19:21:16 +01003157{
3158 struct udev_enumerate *e;
3159 struct udev_list_entry *entry;
3160 const char *path, *device_seat, *id;
3161 struct udev_device *device, *drm_device, *pci;
3162
Giulio Camuffo954f1832014-10-11 18:27:30 +03003163 e = udev_enumerate_new(b->udev);
David Herrmann0af066f2012-10-29 19:21:16 +01003164 udev_enumerate_add_match_subsystem(e, "drm");
3165 udev_enumerate_add_match_sysname(e, "card[0-9]*");
3166
3167 udev_enumerate_scan_devices(e);
3168 drm_device = NULL;
3169 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Daniel Stoneefa504f2016-12-19 16:48:20 +00003170 bool is_boot_vga = false;
3171
David Herrmann0af066f2012-10-29 19:21:16 +01003172 path = udev_list_entry_get_name(entry);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003173 device = udev_device_new_from_syspath(b->udev, path);
David Herrmann0af066f2012-10-29 19:21:16 +01003174 if (!device)
3175 continue;
3176 device_seat = udev_device_get_property_value(device, "ID_SEAT");
3177 if (!device_seat)
3178 device_seat = default_seat;
3179 if (strcmp(device_seat, seat)) {
3180 udev_device_unref(device);
3181 continue;
3182 }
3183
3184 pci = udev_device_get_parent_with_subsystem_devtype(device,
3185 "pci", NULL);
3186 if (pci) {
3187 id = udev_device_get_sysattr_value(pci, "boot_vga");
Daniel Stoneefa504f2016-12-19 16:48:20 +00003188 if (id && !strcmp(id, "1"))
3189 is_boot_vga = true;
David Herrmann0af066f2012-10-29 19:21:16 +01003190 }
3191
Daniel Stoneefa504f2016-12-19 16:48:20 +00003192 /* If we already have a modesetting-capable device, and this
3193 * device isn't our boot-VGA device, we aren't going to use
3194 * it. */
3195 if (!is_boot_vga && drm_device) {
David Herrmann0af066f2012-10-29 19:21:16 +01003196 udev_device_unref(device);
Daniel Stoneefa504f2016-12-19 16:48:20 +00003197 continue;
3198 }
3199
3200 /* Make sure this device is actually capable of modesetting;
3201 * if this call succeeds, b->drm.{fd,filename} will be set,
3202 * and any old values freed. */
3203 if (!drm_device_is_kms(b, device)) {
3204 udev_device_unref(device);
3205 continue;
3206 }
3207
3208 /* There can only be one boot_vga device, and we try to use it
3209 * at all costs. */
3210 if (is_boot_vga) {
3211 if (drm_device)
3212 udev_device_unref(drm_device);
3213 drm_device = device;
3214 break;
3215 }
3216
3217 /* Per the (!is_boot_vga && drm_device) test above, we only
3218 * trump existing saved devices with boot-VGA devices, so if
3219 * we end up here, this must be the first device we've seen. */
3220 assert(!drm_device);
3221 drm_device = device;
David Herrmann0af066f2012-10-29 19:21:16 +01003222 }
3223
Daniel Stoneefa504f2016-12-19 16:48:20 +00003224 /* If we're returning a device to use, we must have an open FD for
3225 * it. */
3226 assert(!!drm_device == (b->drm.fd >= 0));
3227
David Herrmann0af066f2012-10-29 19:21:16 +01003228 udev_enumerate_unref(e);
3229 return drm_device;
3230}
3231
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003232static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003233planes_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
3234 void *data)
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003235{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003236 struct drm_backend *b = data;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003237
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003238 switch (key) {
3239 case KEY_C:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003240 b->cursors_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003241 break;
3242 case KEY_V:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003243 b->sprites_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003244 break;
3245 case KEY_O:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003246 b->sprites_hidden ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003247 break;
3248 default:
3249 break;
3250 }
Ander Conselvan de Oliveira180f42a2012-11-21 15:11:37 +02003251}
3252
Kristian Høgsberg0eac34a2013-08-30 14:28:22 -07003253#ifdef BUILD_VAAPI_RECORDER
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003254static void
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003255recorder_destroy(struct drm_output *output)
3256{
3257 vaapi_recorder_destroy(output->recorder);
3258 output->recorder = NULL;
3259
3260 output->base.disable_planes--;
3261
3262 wl_list_remove(&output->recorder_frame_listener.link);
3263 weston_log("[libva recorder] done\n");
3264}
3265
3266static void
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003267recorder_frame_notify(struct wl_listener *listener, void *data)
3268{
3269 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003270 struct drm_backend *b;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003271 int fd, ret;
3272
3273 output = container_of(listener, struct drm_output,
3274 recorder_frame_listener);
Armin Krezović545dba62016-08-05 15:54:18 +02003275 b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003276
3277 if (!output->recorder)
3278 return;
3279
Giulio Camuffo954f1832014-10-11 18:27:30 +03003280 ret = drmPrimeHandleToFD(b->drm.fd, output->current->handle,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003281 DRM_CLOEXEC, &fd);
3282 if (ret) {
3283 weston_log("[libva recorder] "
3284 "failed to create prime fd for front buffer\n");
3285 return;
3286 }
3287
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003288 ret = vaapi_recorder_frame(output->recorder, fd,
3289 output->current->stride);
3290 if (ret < 0) {
3291 weston_log("[libva recorder] aborted: %m\n");
3292 recorder_destroy(output);
3293 }
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003294}
3295
3296static void *
Giulio Camuffo954f1832014-10-11 18:27:30 +03003297create_recorder(struct drm_backend *b, int width, int height,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003298 const char *filename)
3299{
3300 int fd;
3301 drm_magic_t magic;
3302
Giulio Camuffo954f1832014-10-11 18:27:30 +03003303 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003304 if (fd < 0)
3305 return NULL;
3306
3307 drmGetMagic(fd, &magic);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003308 drmAuthMagic(b->drm.fd, magic);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003309
3310 return vaapi_recorder_create(fd, width, height, filename);
3311}
3312
3313static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003314recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003315 void *data)
3316{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003317 struct drm_backend *b = data;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003318 struct drm_output *output;
3319 int width, height;
3320
Giulio Camuffo954f1832014-10-11 18:27:30 +03003321 output = container_of(b->compositor->output_list.next,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003322 struct drm_output, base.link);
3323
3324 if (!output->recorder) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01003325 if (output->gbm_format != GBM_FORMAT_XRGB8888) {
Ander Conselvan de Oliveira2ef1cd12014-05-06 16:49:06 +03003326 weston_log("failed to start vaapi recorder: "
3327 "output format not supported\n");
3328 return;
3329 }
3330
Hardeningff39efa2013-09-18 23:56:35 +02003331 width = output->base.current_mode->width;
3332 height = output->base.current_mode->height;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003333
3334 output->recorder =
Giulio Camuffo954f1832014-10-11 18:27:30 +03003335 create_recorder(b, width, height, "capture.h264");
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003336 if (!output->recorder) {
3337 weston_log("failed to create vaapi recorder\n");
3338 return;
3339 }
3340
3341 output->base.disable_planes++;
3342
3343 output->recorder_frame_listener.notify = recorder_frame_notify;
3344 wl_signal_add(&output->base.frame_signal,
3345 &output->recorder_frame_listener);
3346
3347 weston_output_schedule_repaint(&output->base);
3348
3349 weston_log("[libva recorder] initialized\n");
3350 } else {
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003351 recorder_destroy(output);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003352 }
3353}
3354#else
3355static void
Bryce Harrington4a8a3a12015-07-16 19:12:26 -07003356recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003357 void *data)
3358{
3359 weston_log("Compiled without libva support\n");
3360}
3361#endif
3362
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003363static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03003364switch_to_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003365{
3366 struct drm_output *output;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003367 bool dmabuf_support_inited;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003368
Giulio Camuffo954f1832014-10-11 18:27:30 +03003369 if (!b->use_pixman)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003370 return;
3371
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003372 dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf;
3373
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003374 weston_log("Switching to GL renderer\n");
3375
Giulio Camuffo954f1832014-10-11 18:27:30 +03003376 b->gbm = create_gbm_device(b->drm.fd);
3377 if (!b->gbm) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003378 weston_log("Failed to create gbm device. "
3379 "Aborting renderer switch\n");
3380 return;
3381 }
3382
Giulio Camuffo954f1832014-10-11 18:27:30 +03003383 wl_list_for_each(output, &b->compositor->output_list, base.link)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003384 pixman_renderer_output_destroy(&output->base);
3385
Giulio Camuffo954f1832014-10-11 18:27:30 +03003386 b->compositor->renderer->destroy(b->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003387
Giulio Camuffo954f1832014-10-11 18:27:30 +03003388 if (drm_backend_create_gl_renderer(b) < 0) {
3389 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003390 weston_log("Failed to create GL renderer. Quitting.\n");
3391 /* FIXME: we need a function to shutdown cleanly */
3392 assert(0);
3393 }
3394
Giulio Camuffo954f1832014-10-11 18:27:30 +03003395 wl_list_for_each(output, &b->compositor->output_list, base.link)
3396 drm_output_init_egl(output, b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003397
Giulio Camuffo954f1832014-10-11 18:27:30 +03003398 b->use_pixman = 0;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003399
3400 if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) {
3401 if (linux_dmabuf_setup(b->compositor) < 0)
3402 weston_log("Error: initializing dmabuf "
3403 "support failed.\n");
3404 }
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003405}
3406
3407static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003408renderer_switch_binding(struct weston_keyboard *keyboard, uint32_t time,
3409 uint32_t key, void *data)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003410{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003411 struct drm_backend *b =
Armin Krezović545dba62016-08-05 15:54:18 +02003412 to_drm_backend(keyboard->seat->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003413
Giulio Camuffo954f1832014-10-11 18:27:30 +03003414 switch_to_gl_renderer(b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003415}
3416
Armin Krezović08368132016-09-30 14:11:05 +02003417static const struct weston_drm_output_api api = {
3418 drm_output_set_mode,
3419 drm_output_set_gbm_format,
3420 drm_output_set_seat,
3421};
3422
Giulio Camuffo954f1832014-10-11 18:27:30 +03003423static struct drm_backend *
3424drm_backend_create(struct weston_compositor *compositor,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003425 struct weston_drm_backend_config *config)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003426{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003427 struct drm_backend *b;
David Herrmann0af066f2012-10-29 19:21:16 +01003428 struct udev_device *drm_device;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003429 struct wl_event_loop *loop;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003430 const char *seat_id = default_seat;
Armin Krezović08368132016-09-30 14:11:05 +02003431 int ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003432
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003433 weston_log("initializing drm backend\n");
3434
Giulio Camuffo954f1832014-10-11 18:27:30 +03003435 b = zalloc(sizeof *b);
3436 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003437 return NULL;
Daniel Stone725c2c32012-06-22 14:04:36 +01003438
Daniel Stoneefa504f2016-12-19 16:48:20 +00003439 b->drm.fd = -1;
3440
Pekka Paalanen68583832015-05-19 09:53:16 +03003441 /*
3442 * KMS support for hardware planes cannot properly synchronize
3443 * without nuclear page flip. Without nuclear/atomic, hw plane
3444 * and cursor plane updates would either tear or cause extra
3445 * waits for vblanks which means dropping the compositor framerate
Pekka Paalanen3f32a132015-09-07 15:38:43 +03003446 * to a fraction. For cursors, it's not so bad, so they are
3447 * enabled.
Pekka Paalanen68583832015-05-19 09:53:16 +03003448 *
3449 * These can be enabled again when nuclear/atomic support lands.
3450 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003451 b->sprites_are_broken = 1;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003452 b->compositor = compositor;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003453 b->use_pixman = config->use_pixman;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00003454 b->pageflip_timeout = config->pageflip_timeout;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003455
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003456 if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0)
3457 goto err_compositor;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003458
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003459 if (config->seat_id)
3460 seat_id = config->seat_id;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003461
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003462 /* Check if we run drm-backend using weston-launch */
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003463 compositor->launcher = weston_launcher_connect(compositor, config->tty,
3464 seat_id, true);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003465 if (compositor->launcher == NULL) {
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003466 weston_log("fatal: drm backend should be run "
Kristian Høgsbergb76237e2013-04-04 21:36:20 -04003467 "using weston-launch binary or as root\n");
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003468 goto err_compositor;
3469 }
3470
Giulio Camuffo954f1832014-10-11 18:27:30 +03003471 b->udev = udev_new();
3472 if (b->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003473 weston_log("failed to initialize udev context\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003474 goto err_launcher;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003475 }
3476
Giulio Camuffo954f1832014-10-11 18:27:30 +03003477 b->session_listener.notify = session_notify;
3478 wl_signal_add(&compositor->session_signal, &b->session_listener);
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05003479
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003480 drm_device = find_primary_gpu(b, seat_id);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04003481 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003482 weston_log("no drm device found\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003483 goto err_udev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003484 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003485
Daniel Stoneefa504f2016-12-19 16:48:20 +00003486 if (init_kms_caps(b) < 0) {
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003487 weston_log("failed to initialize kms\n");
3488 goto err_udev_dev;
3489 }
3490
Giulio Camuffo954f1832014-10-11 18:27:30 +03003491 if (b->use_pixman) {
3492 if (init_pixman(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003493 weston_log("failed to initialize pixman renderer\n");
3494 goto err_udev_dev;
3495 }
3496 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +03003497 if (init_egl(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003498 weston_log("failed to initialize egl\n");
3499 goto err_udev_dev;
3500 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003501 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05003502
Giulio Camuffo954f1832014-10-11 18:27:30 +03003503 b->base.destroy = drm_destroy;
3504 b->base.restore = drm_restore;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02003505
Bob Ham91880f12016-01-12 10:21:47 +00003506 weston_setup_vt_switch_bindings(compositor);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04003507
Giulio Camuffo954f1832014-10-11 18:27:30 +03003508 wl_list_init(&b->sprite_list);
3509 create_sprites(b);
Jesse Barnes58ef3792012-02-23 09:45:49 -05003510
Giulio Camuffo954f1832014-10-11 18:27:30 +03003511 if (udev_input_init(&b->input,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03003512 compositor, b->udev, seat_id,
3513 config->configure_device) < 0) {
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003514 weston_log("failed to create input devices\n");
3515 goto err_sprite;
3516 }
3517
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +00003518 b->connector = config->connector;
3519
Ucan, Emre (ADITG/SW1)9a200d72017-02-02 14:06:56 +00003520 if (create_outputs(b, drm_device) < 0) {
Daniel Stoneefa504f2016-12-19 16:48:20 +00003521 weston_log("failed to create output for %s\n", b->drm.filename);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003522 goto err_udev_input;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003523 }
3524
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003525 /* A this point we have some idea of whether or not we have a working
3526 * cursor plane. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003527 if (!b->cursors_are_broken)
3528 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003529
Giulio Camuffo954f1832014-10-11 18:27:30 +03003530 loop = wl_display_get_event_loop(compositor->wl_display);
3531 b->drm_source =
3532 wl_event_loop_add_fd(loop, b->drm.fd,
3533 WL_EVENT_READABLE, on_drm_input, b);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003534
Giulio Camuffo954f1832014-10-11 18:27:30 +03003535 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
3536 if (b->udev_monitor == NULL) {
Abdur Rehman4dca0e12017-01-01 19:46:35 +05003537 weston_log("failed to initialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003538 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003539 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03003540 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003541 "drm", NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003542 b->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02003543 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03003544 udev_monitor_get_fd(b->udev_monitor),
3545 WL_EVENT_READABLE, udev_drm_event, b);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003546
Giulio Camuffo954f1832014-10-11 18:27:30 +03003547 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003548 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003549 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003550 }
3551
Daniel Stonea96b93c2012-06-22 14:04:37 +01003552 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003553
Giulio Camuffo954f1832014-10-11 18:27:30 +03003554 weston_compositor_add_debug_binding(compositor, KEY_O,
3555 planes_binding, b);
3556 weston_compositor_add_debug_binding(compositor, KEY_C,
3557 planes_binding, b);
3558 weston_compositor_add_debug_binding(compositor, KEY_V,
3559 planes_binding, b);
3560 weston_compositor_add_debug_binding(compositor, KEY_Q,
3561 recorder_binding, b);
3562 weston_compositor_add_debug_binding(compositor, KEY_W,
3563 renderer_switch_binding, b);
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003564
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003565 if (compositor->renderer->import_dmabuf) {
3566 if (linux_dmabuf_setup(compositor) < 0)
3567 weston_log("Error: initializing dmabuf "
3568 "support failed.\n");
3569 }
3570
Giulio Camuffo954f1832014-10-11 18:27:30 +03003571 compositor->backend = &b->base;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003572
Armin Krezović08368132016-09-30 14:11:05 +02003573 ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME,
3574 &api, sizeof(api));
3575
3576 if (ret < 0) {
3577 weston_log("Failed to register output API.\n");
3578 goto err_udev_monitor;
3579 }
3580
Giulio Camuffo954f1832014-10-11 18:27:30 +03003581 return b;
Daniel Stonea96b93c2012-06-22 14:04:37 +01003582
3583err_udev_monitor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003584 wl_event_source_remove(b->udev_drm_source);
3585 udev_monitor_unref(b->udev_monitor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003586err_drm_source:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003587 wl_event_source_remove(b->drm_source);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003588err_udev_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003589 udev_input_destroy(&b->input);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003590err_sprite:
Emmanuel Gil Peyrotb8347e32016-05-02 22:40:13 +01003591 if (b->gbm)
3592 gbm_device_destroy(b->gbm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003593 destroy_sprites(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003594err_udev_dev:
3595 udev_device_unref(drm_device);
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003596err_launcher:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003597 weston_launcher_destroy(compositor->launcher);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003598err_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003599 udev_unref(b->udev);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003600err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003601 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003602 free(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003603 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003604}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003605
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003606static void
3607config_init_to_defaults(struct weston_drm_backend_config *config)
3608{
3609}
3610
Giulio Camuffo954f1832014-10-11 18:27:30 +03003611WL_EXPORT int
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01003612weston_backend_init(struct weston_compositor *compositor,
3613 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003614{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003615 struct drm_backend *b;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003616 struct weston_drm_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003617
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003618 if (config_base == NULL ||
3619 config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION ||
3620 config_base->struct_size > sizeof(struct weston_drm_backend_config)) {
3621 weston_log("drm backend config structure is invalid\n");
3622 return -1;
3623 }
Benjamin Franzke117483d2011-08-30 11:38:26 +02003624
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003625 config_init_to_defaults(&config);
3626 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003627
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003628 b = drm_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003629 if (b == NULL)
3630 return -1;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003631
Giulio Camuffo954f1832014-10-11 18:27:30 +03003632 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003633}