blob: decd586fca2a4b148c1e6e399d77bb5629b6ec26 [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
Daniel Stone08d4edf2017-04-04 17:54:34 +0100165/**
166 * A plane represents one buffer, positioned within a CRTC, and stacked
167 * relative to other planes on the same CRTC.
168 *
169 * Each CRTC has a 'primary plane', which use used to display the classic
170 * framebuffer contents, as accessed through the legacy drmModeSetCrtc
171 * call (which combines setting the CRTC's actual physical mode, and the
172 * properties of the primary plane).
173 *
174 * The cursor plane also has its own alternate legacy API.
175 *
176 * Other planes are used opportunistically to display content we do not
177 * wish to blit into the primary plane. These non-primary/cursor planes
178 * are referred to as 'sprites'.
179 */
180struct drm_plane {
181 struct wl_list link;
182
183 struct weston_plane base;
184
185 struct drm_output *output;
186 struct drm_backend *backend;
187
188 uint32_t possible_crtcs;
189 uint32_t plane_id;
190 uint32_t count_formats;
191
192 /* The last framebuffer submitted to the kernel for this plane. */
193 struct drm_fb *fb_current;
194 /* The previously-submitted framebuffer, where the hardware has not
195 * yet acknowledged display of fb_current. */
196 struct drm_fb *fb_last;
197 /* Framebuffer we are going to submit to the kernel when the current
198 * repaint is flushed. */
199 struct drm_fb *fb_pending;
200
201 int32_t src_x, src_y;
202 uint32_t src_w, src_h;
203 uint32_t dest_x, dest_y;
204 uint32_t dest_w, dest_h;
205
206 uint32_t formats[];
207};
208
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400209struct drm_output {
Armin Krezović08368132016-09-30 14:11:05 +0200210 struct weston_output base;
211 drmModeConnector *connector;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400212
Daniel Stone17339232015-11-28 12:09:47 +0000213 uint32_t crtc_id; /* object ID to pass to DRM functions */
214 int pipe; /* index of CRTC in resource array / bitmasks */
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400215 uint32_t connector_id;
Matt Roper361d2ad2011-08-29 13:52:23 -0700216 drmModeCrtcPtr original_crtc;
Richard Hughes2b2092a2013-04-24 14:58:02 +0100217 struct drm_edid edid;
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +0300218 drmModePropertyPtr dpms_prop;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200219
Daniel Stone36609c72015-06-18 07:49:02 +0100220 enum dpms_enum dpms;
Daniel Stone5bb8f582017-04-04 17:54:28 +0100221 struct backlight *backlight;
Daniel Stone36609c72015-06-18 07:49:02 +0100222
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +0300223 bool state_invalid;
224
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300225 int vblank_pending;
226 int page_flip_pending;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800227 int destroy_pending;
Armin Krezović08368132016-09-30 14:11:05 +0200228 int disable_pending;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300229
Daniel Stonee4256832017-04-04 17:54:27 +0100230 struct drm_fb *gbm_cursor_fb[2];
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400231 struct weston_plane cursor_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500232 struct weston_view *cursor_view;
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400233 int current_cursor;
Daniel Stone5bb8f582017-04-04 17:54:28 +0100234
235 struct gbm_surface *gbm_surface;
236 uint32_t gbm_format;
237
238 struct weston_plane fb_plane;
Daniel Stonef30a18c2017-04-04 17:54:31 +0100239
240 /* The last framebuffer submitted to the kernel for this CRTC. */
241 struct drm_fb *fb_current;
242 /* The previously-submitted framebuffer, where the hardware has not
243 * yet acknowledged display of fb_current. */
244 struct drm_fb *fb_last;
245 /* Framebuffer we are going to submit to the kernel when the current
246 * repaint is flushed. */
247 struct drm_fb *fb_pending;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200248
249 struct drm_fb *dumb[2];
250 pixman_image_t *image[2];
251 int current_image;
252 pixman_region32_t previous_damage;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300253
254 struct vaapi_recorder *recorder;
255 struct wl_listener recorder_frame_listener;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000256
257 struct wl_event_source *pageflip_timer;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400258};
259
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300260static struct gl_renderer_interface *gl_renderer;
261
Kristian Høgsberg98cfea62013-02-18 16:15:53 -0500262static const char default_seat[] = "seat0";
Pekka Paalanen33156972012-08-03 13:30:30 -0400263
Armin Krezović545dba62016-08-05 15:54:18 +0200264static inline struct drm_output *
265to_drm_output(struct weston_output *base)
266{
267 return container_of(base, struct drm_output, base);
268}
269
270static inline struct drm_backend *
271to_drm_backend(struct weston_compositor *base)
272{
273 return container_of(base->backend, struct drm_backend, base);
274}
275
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000276static int
277pageflip_timeout(void *data) {
278 /*
279 * Our timer just went off, that means we're not receiving drm
280 * page flip events anymore for that output. Let's gracefully exit
281 * weston with a return value so devs can debug what's going on.
282 */
283 struct drm_output *output = data;
284 struct weston_compositor *compositor = output->base.compositor;
285
286 weston_log("Pageflip timeout reached on output %s, your "
287 "driver is probably buggy! Exiting.\n",
288 output->base.name);
289 weston_compositor_exit_with_code(compositor, EXIT_FAILURE);
290
291 return 0;
292}
293
294/* Creates the pageflip timer. Note that it isn't armed by default */
295static int
296drm_output_pageflip_timer_create(struct drm_output *output)
297{
298 struct wl_event_loop *loop = NULL;
299 struct weston_compositor *ec = output->base.compositor;
300
301 loop = wl_display_get_event_loop(ec->wl_display);
302 assert(loop);
303 output->pageflip_timer = wl_event_loop_add_timer(loop,
304 pageflip_timeout,
305 output);
306
307 if (output->pageflip_timer == NULL) {
308 weston_log("creating drm pageflip timer failed: %m\n");
309 return -1;
310 }
311
312 return 0;
313}
314
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400315static void
316drm_output_set_cursor(struct drm_output *output);
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400317
Mario Kleinerf507ec32015-06-21 21:25:14 +0200318static void
319drm_output_update_msc(struct drm_output *output, unsigned int seq);
320
Jesse Barnes58ef3792012-02-23 09:45:49 -0500321static int
Daniel Stone08d4edf2017-04-04 17:54:34 +0100322drm_plane_crtc_supported(struct drm_output *output, struct drm_plane *plane)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500323{
Daniel Stone08d4edf2017-04-04 17:54:34 +0100324 return !!(plane->possible_crtcs & (1 << output->pipe));
Jesse Barnes58ef3792012-02-23 09:45:49 -0500325}
326
Daniel Stone72c0e1b2017-02-09 13:49:15 +0000327static struct drm_output *
328drm_output_find_by_crtc(struct drm_backend *b, uint32_t crtc_id)
329{
330 struct drm_output *output;
331
332 wl_list_for_each(output, &b->compositor->output_list, base.link) {
333 if (output->crtc_id == crtc_id)
334 return output;
335 }
336
337 wl_list_for_each(output, &b->compositor->pending_output_list,
338 base.link) {
339 if (output->crtc_id == crtc_id)
340 return output;
341 }
342
343 return NULL;
344}
345
Daniel Stonec0ec7592017-02-09 13:58:35 +0000346static struct drm_output *
347drm_output_find_by_connector(struct drm_backend *b, uint32_t connector_id)
348{
349 struct drm_output *output;
350
351 wl_list_for_each(output, &b->compositor->output_list, base.link) {
352 if (output->connector_id == connector_id)
353 return output;
354 }
355
356 wl_list_for_each(output, &b->compositor->pending_output_list,
357 base.link) {
358 if (output->connector_id == connector_id)
359 return output;
360 }
361
362 return NULL;
363}
364
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300365static void
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100366drm_fb_destroy(struct drm_fb *fb)
367{
368 if (fb->fb_id != 0)
369 drmModeRmFB(fb->fd, fb->fb_id);
370 weston_buffer_reference(&fb->buffer_ref, NULL);
371 free(fb);
372}
373
374static void
375drm_fb_destroy_dumb(struct drm_fb *fb)
376{
377 struct drm_mode_destroy_dumb destroy_arg;
378
379 assert(fb->type == BUFFER_PIXMAN_DUMB);
380
381 if (fb->map && fb->size > 0)
382 munmap(fb->map, fb->size);
383
384 memset(&destroy_arg, 0, sizeof(destroy_arg));
385 destroy_arg.handle = fb->handle;
386 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
387
388 drm_fb_destroy(fb);
389}
390
391static void
392drm_fb_destroy_gbm(struct gbm_bo *bo, void *data)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300393{
394 struct drm_fb *fb = data;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300395
Daniel Stonee4256832017-04-04 17:54:27 +0100396 assert(fb->type == BUFFER_GBM_SURFACE || fb->type == BUFFER_CLIENT ||
397 fb->type == BUFFER_CURSOR);
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100398 drm_fb_destroy(fb);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300399}
400
401static struct drm_fb *
Daniel Stonef214fdc2016-11-14 17:43:57 +0000402drm_fb_create_dumb(struct drm_backend *b, int width, int height,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300403 uint32_t format)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200404{
405 struct drm_fb *fb;
406 int ret;
407
408 struct drm_mode_create_dumb create_arg;
409 struct drm_mode_destroy_dumb destroy_arg;
410 struct drm_mode_map_dumb map_arg;
411
Peter Huttererf3d62272013-08-08 11:57:05 +1000412 fb = zalloc(sizeof *fb);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200413 if (!fb)
414 return NULL;
415
Daniel Stone6e7a9612017-04-04 17:54:26 +0100416 fb->refcnt = 1;
417
Daniel Stone0b70fa42017-04-04 17:54:23 +0100418 fb->format = pixel_format_get_info(format);
419 if (!fb->format) {
420 weston_log("failed to look up format 0x%lx\n",
421 (unsigned long) format);
422 goto err_fb;
423 }
424
425 if (!fb->format->depth || !fb->format->bpp) {
426 weston_log("format 0x%lx is not compatible with dumb buffers\n",
427 (unsigned long) format);
428 goto err_fb;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300429 }
430
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700431 memset(&create_arg, 0, sizeof create_arg);
Daniel Stone0b70fa42017-04-04 17:54:23 +0100432 create_arg.bpp = fb->format->bpp;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200433 create_arg.width = width;
434 create_arg.height = height;
435
Giulio Camuffo954f1832014-10-11 18:27:30 +0300436 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200437 if (ret)
438 goto err_fb;
439
Daniel Stonefc175a72017-04-04 17:54:22 +0100440 fb->type = BUFFER_PIXMAN_DUMB;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200441 fb->handle = create_arg.handle;
442 fb->stride = create_arg.pitch;
443 fb->size = create_arg.size;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000444 fb->width = width;
445 fb->height = height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300446 fb->fd = b->drm.fd;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200447
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300448 ret = -1;
449
450 if (!b->no_addfb2) {
Yong Bakos4b6321f2016-08-17 17:37:55 -0700451 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300452
453 handles[0] = fb->handle;
454 pitches[0] = fb->stride;
455 offsets[0] = 0;
456
457 ret = drmModeAddFB2(b->drm.fd, width, height,
Daniel Stone0b70fa42017-04-04 17:54:23 +0100458 fb->format->format,
459 handles, pitches, offsets,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300460 &fb->fb_id, 0);
461 if (ret) {
462 weston_log("addfb2 failed: %m\n");
463 b->no_addfb2 = 1;
464 }
465 }
466
467 if (ret) {
Daniel Stone0b70fa42017-04-04 17:54:23 +0100468 ret = drmModeAddFB(b->drm.fd, width, height,
469 fb->format->depth, fb->format->bpp,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300470 fb->stride, fb->handle, &fb->fb_id);
471 }
472
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200473 if (ret)
474 goto err_bo;
475
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700476 memset(&map_arg, 0, sizeof map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200477 map_arg.handle = fb->handle;
Chris Michaeleb2074a2013-05-01 21:26:02 -0400478 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200479 if (ret)
480 goto err_add_fb;
481
Chris Michael4a7ce1f2015-11-10 10:40:37 -0500482 fb->map = mmap(NULL, fb->size, PROT_WRITE,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300483 MAP_SHARED, b->drm.fd, map_arg.offset);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200484 if (fb->map == MAP_FAILED)
485 goto err_add_fb;
486
487 return fb;
488
489err_add_fb:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300490 drmModeRmFB(b->drm.fd, fb->fb_id);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200491err_bo:
492 memset(&destroy_arg, 0, sizeof(destroy_arg));
493 destroy_arg.handle = create_arg.handle;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300494 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200495err_fb:
496 free(fb);
497 return NULL;
498}
499
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200500static struct drm_fb *
Daniel Stone6e7a9612017-04-04 17:54:26 +0100501drm_fb_ref(struct drm_fb *fb)
502{
503 fb->refcnt++;
504 return fb;
505}
506
507static struct drm_fb *
Daniel Stonefc175a72017-04-04 17:54:22 +0100508drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend,
509 uint32_t format, enum drm_fb_type type)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300510{
511 struct drm_fb *fb = gbm_bo_get_user_data(bo);
Derek Foreman482ffdf2016-07-08 12:50:57 -0500512 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300513 int ret;
514
Daniel Stonefc175a72017-04-04 17:54:22 +0100515 if (fb) {
516 assert(fb->type == type);
Daniel Stone6e7a9612017-04-04 17:54:26 +0100517 return drm_fb_ref(fb);
Daniel Stonefc175a72017-04-04 17:54:22 +0100518 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300519
Bryce Harringtonde16d892014-11-20 22:21:57 -0800520 fb = zalloc(sizeof *fb);
521 if (fb == NULL)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200522 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300523
Daniel Stonefc175a72017-04-04 17:54:22 +0100524 fb->type = type;
Daniel Stone6e7a9612017-04-04 17:54:26 +0100525 fb->refcnt = 1;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300526 fb->bo = bo;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300527
Daniel Stonec8c917c2016-11-14 17:45:58 +0000528 fb->width = gbm_bo_get_width(bo);
529 fb->height = gbm_bo_get_height(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200530 fb->stride = gbm_bo_get_stride(bo);
531 fb->handle = gbm_bo_get_handle(bo).u32;
Daniel Stone0b70fa42017-04-04 17:54:23 +0100532 fb->format = pixel_format_get_info(format);
Daniel Stonec8c917c2016-11-14 17:45:58 +0000533 fb->size = fb->stride * fb->height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300534 fb->fd = backend->drm.fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300535
Daniel Stone0b70fa42017-04-04 17:54:23 +0100536 if (!fb->format) {
537 weston_log("couldn't look up format 0x%lx\n",
538 (unsigned long) format);
539 goto err_free;
540 }
541
Daniel Stonec8c917c2016-11-14 17:45:58 +0000542 if (backend->min_width > fb->width ||
543 fb->width > backend->max_width ||
544 backend->min_height > fb->height ||
545 fb->height > backend->max_height) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200546 weston_log("bo geometry out of bounds\n");
547 goto err_free;
548 }
549
550 ret = -1;
551
Giulio Camuffo954f1832014-10-11 18:27:30 +0300552 if (format && !backend->no_addfb2) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200553 handles[0] = fb->handle;
554 pitches[0] = fb->stride;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200555 offsets[0] = 0;
556
Daniel Stonec8c917c2016-11-14 17:45:58 +0000557 ret = drmModeAddFB2(backend->drm.fd, fb->width, fb->height,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200558 format, handles, pitches, offsets,
559 &fb->fb_id, 0);
560 if (ret) {
561 weston_log("addfb2 failed: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300562 backend->no_addfb2 = 1;
563 backend->sprites_are_broken = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200564 }
565 }
566
Daniel Stone0b70fa42017-04-04 17:54:23 +0100567 if (ret && fb->format->depth && fb->format->bpp)
Daniel Stonec8c917c2016-11-14 17:45:58 +0000568 ret = drmModeAddFB(backend->drm.fd, fb->width, fb->height,
Daniel Stone0b70fa42017-04-04 17:54:23 +0100569 fb->format->depth, fb->format->bpp,
570 fb->stride, fb->handle, &fb->fb_id);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200571
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300572 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200573 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200574 goto err_free;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300575 }
576
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100577 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_gbm);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300578
579 return fb;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200580
581err_free:
582 free(fb);
583 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300584}
585
586static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500587drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200588{
Pekka Paalanende685b82012-12-04 15:58:12 +0200589 assert(fb->buffer_ref.buffer == NULL);
Daniel Stonefc175a72017-04-04 17:54:22 +0100590 assert(fb->type == BUFFER_CLIENT);
Pekka Paalanende685b82012-12-04 15:58:12 +0200591 weston_buffer_reference(&fb->buffer_ref, buffer);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200592}
593
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200594static void
Daniel Stone05a5ac22017-04-04 17:54:25 +0100595drm_fb_unref(struct drm_fb *fb)
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200596{
597 if (!fb)
598 return;
599
Daniel Stone6e7a9612017-04-04 17:54:26 +0100600 assert(fb->refcnt > 0);
601 if (--fb->refcnt > 0)
602 return;
603
Daniel Stonefc175a72017-04-04 17:54:22 +0100604 switch (fb->type) {
605 case BUFFER_PIXMAN_DUMB:
Daniel Stone6e7a9612017-04-04 17:54:26 +0100606 drm_fb_destroy_dumb(fb);
Daniel Stonefc175a72017-04-04 17:54:22 +0100607 break;
Daniel Stonee4256832017-04-04 17:54:27 +0100608 case BUFFER_CURSOR:
Daniel Stonefc175a72017-04-04 17:54:22 +0100609 case BUFFER_CLIENT:
610 gbm_bo_destroy(fb->bo);
611 break;
612 case BUFFER_GBM_SURFACE:
Daniel Stone05a5ac22017-04-04 17:54:25 +0100613 gbm_surface_release_buffer(fb->gbm_surface, fb->bo);
Daniel Stonefc175a72017-04-04 17:54:22 +0100614 break;
615 default:
616 assert(NULL);
617 break;
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200618 }
619}
620
Daniel Stone90648872016-10-21 18:08:37 +0100621static int
622drm_view_transform_supported(struct weston_view *ev)
623{
624 return !ev->transform.enabled ||
625 (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
626}
627
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500628static uint32_t
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200629drm_output_check_scanout_format(struct drm_output *output,
630 struct weston_surface *es, struct gbm_bo *bo)
631{
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200632 uint32_t format;
633 pixman_region32_t r;
634
635 format = gbm_bo_get_format(bo);
636
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700637 if (format == GBM_FORMAT_ARGB8888) {
638 /* We can scanout an ARGB buffer if the surface's
639 * opaque region covers the whole output, but we have
640 * to use XRGB as the KMS format code. */
Kristian Høgsberg1be87e32014-01-17 14:22:41 -0800641 pixman_region32_init_rect(&r, 0, 0,
642 output->base.width,
643 output->base.height);
644 pixman_region32_subtract(&r, &r, &es->opaque);
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200645
646 if (!pixman_region32_not_empty(&r))
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500647 format = GBM_FORMAT_XRGB8888;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200648
649 pixman_region32_fini(&r);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500650 }
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700651
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100652 if (output->gbm_format == format)
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700653 return format;
654
655 return 0;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200656}
657
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400658static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200659drm_output_prepare_scanout_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500660 struct weston_view *ev)
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500661{
Armin Krezović545dba62016-08-05 15:54:18 +0200662 struct drm_backend *b = to_drm_backend(output->base.compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500663 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200664 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300665 struct gbm_bo *bo;
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500666 uint32_t format;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500667
Daniel Stone90648872016-10-21 18:08:37 +0100668 /* Don't import buffers which span multiple outputs. */
669 if (ev->output_mask != (1u << output->base.id))
670 return NULL;
671
Daniel Stone296d7a92016-10-21 18:05:37 +0100672 /* We use GBM to import buffers. */
673 if (b->gbm == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400674 return NULL;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500675
Daniel Stone296d7a92016-10-21 18:05:37 +0100676 if (buffer == NULL)
677 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +0100678 if (wl_shm_buffer_get(buffer->resource))
679 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +0100680
681 /* Make sure our view is exactly compatible with the output. */
682 if (ev->geometry.x != output->base.x ||
683 ev->geometry.y != output->base.y)
684 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +0100685 if (buffer->width != output->base.current_mode->width ||
686 buffer->height != output->base.current_mode->height)
687 return NULL;
688
Daniel Stone296d7a92016-10-21 18:05:37 +0100689 if (ev->transform.enabled)
690 return NULL;
Pekka Paalanen5580f222015-02-17 16:33:18 +0200691 if (ev->geometry.scissor_enabled)
692 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +0100693 if (viewport->buffer.transform != output->base.transform)
694 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +0100695 if (viewport->buffer.scale != output->base.current_scale)
696 return NULL;
697 if (!drm_view_transform_supported(ev))
698 return NULL;
699
700 if (ev->alpha != 1.0f)
701 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +0100702
Giulio Camuffo954f1832014-10-11 18:27:30 +0300703 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
Kristian Høgsberg63996462013-09-03 22:27:08 -0700704 buffer->resource, GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500705
Rob Bradford9b101872012-09-14 23:25:41 +0100706 /* Unable to use the buffer for scanout */
707 if (!bo)
708 return NULL;
709
Jason Ekstranda7af7042013-10-12 22:38:11 -0500710 format = drm_output_check_scanout_format(output, ev->surface, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500711 if (format == 0) {
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300712 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400713 return NULL;
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300714 }
715
Daniel Stone5bb8f582017-04-04 17:54:28 +0100716 output->fb_pending = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT);
717 if (!output->fb_pending) {
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300718 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400719 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300720 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500721
Daniel Stone5bb8f582017-04-04 17:54:28 +0100722 drm_fb_set_buffer(output->fb_pending, buffer);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500723
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400724 return &output->fb_plane;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500725}
726
Daniel Stone95d48a22017-04-04 17:54:30 +0100727static struct drm_fb *
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200728drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400729{
Armin Krezović545dba62016-08-05 15:54:18 +0200730 struct drm_backend *b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300731 struct gbm_bo *bo;
Daniel Stone95d48a22017-04-04 17:54:30 +0100732 struct drm_fb *ret;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400733
Giulio Camuffo954f1832014-10-11 18:27:30 +0300734 output->base.compositor->renderer->repaint_output(&output->base,
735 damage);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400736
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100737 bo = gbm_surface_lock_front_buffer(output->gbm_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300738 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +0200739 weston_log("failed to lock front buffer: %m\n");
Daniel Stone95d48a22017-04-04 17:54:30 +0100740 return NULL;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400741 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300742
Daniel Stone95d48a22017-04-04 17:54:30 +0100743 ret = drm_fb_get_from_bo(bo, b, output->gbm_format, BUFFER_GBM_SURFACE);
744 if (!ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200745 weston_log("failed to get drm_fb for bo\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100746 gbm_surface_release_buffer(output->gbm_surface, bo);
Daniel Stone95d48a22017-04-04 17:54:30 +0100747 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300748 }
Daniel Stone95d48a22017-04-04 17:54:30 +0100749 ret->gbm_surface = output->gbm_surface;
750
751 return ret;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400752}
753
Daniel Stone95d48a22017-04-04 17:54:30 +0100754static struct drm_fb *
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200755drm_output_render_pixman(struct drm_output *output, pixman_region32_t *damage)
756{
757 struct weston_compositor *ec = output->base.compositor;
758 pixman_region32_t total_damage, previous_damage;
759
760 pixman_region32_init(&total_damage);
761 pixman_region32_init(&previous_damage);
762
763 pixman_region32_copy(&previous_damage, damage);
764
765 pixman_region32_union(&total_damage, damage, &output->previous_damage);
766 pixman_region32_copy(&output->previous_damage, &previous_damage);
767
768 output->current_image ^= 1;
769
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200770 pixman_renderer_output_set_buffer(&output->base,
771 output->image[output->current_image]);
772
773 ec->renderer->repaint_output(&output->base, &total_damage);
774
775 pixman_region32_fini(&total_damage);
776 pixman_region32_fini(&previous_damage);
Daniel Stone95d48a22017-04-04 17:54:30 +0100777
778 return drm_fb_ref(output->dumb[output->current_image]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200779}
780
781static void
782drm_output_render(struct drm_output *output, pixman_region32_t *damage)
783{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300784 struct weston_compositor *c = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +0200785 struct drm_backend *b = to_drm_backend(c);
Daniel Stone95d48a22017-04-04 17:54:30 +0100786 struct drm_fb *fb;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200787
Daniel Stone4e84f7d2017-04-04 17:54:29 +0100788 /* If we already have a client buffer promoted to scanout, then we don't
789 * want to render. */
790 if (output->fb_pending)
791 return;
792
Giulio Camuffo954f1832014-10-11 18:27:30 +0300793 if (b->use_pixman)
Daniel Stone95d48a22017-04-04 17:54:30 +0100794 fb = drm_output_render_pixman(output, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200795 else
Daniel Stone95d48a22017-04-04 17:54:30 +0100796 fb = drm_output_render_gl(output, damage);
797
798 if (!fb)
799 return;
800 output->fb_pending = fb;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200801
Giulio Camuffo954f1832014-10-11 18:27:30 +0300802 pixman_region32_subtract(&c->primary_plane.damage,
803 &c->primary_plane.damage, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200804}
805
806static void
Richard Hughese7299962013-05-01 21:52:12 +0100807drm_output_set_gamma(struct weston_output *output_base,
808 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
809{
810 int rc;
Armin Krezović545dba62016-08-05 15:54:18 +0200811 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300812 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +0200813 to_drm_backend(output->base.compositor);
Richard Hughese7299962013-05-01 21:52:12 +0100814
815 /* check */
816 if (output_base->gamma_size != size)
817 return;
818 if (!output->original_crtc)
819 return;
820
Giulio Camuffo954f1832014-10-11 18:27:30 +0300821 rc = drmModeCrtcSetGamma(backend->drm.fd,
Richard Hughese7299962013-05-01 21:52:12 +0100822 output->crtc_id,
823 size, r, g, b);
824 if (rc)
825 weston_log("set gamma failed: %m\n");
826}
827
Bryce Harringtonada4f072015-06-30 13:25:46 -0700828/* Determine the type of vblank synchronization to use for the output.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200829 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700830 * The pipe parameter indicates which CRTC is in use. Knowing this, we
831 * can determine which vblank sequence type to use for it. Traditional
832 * cards had only two CRTCs, with CRTC 0 using no special flags, and
833 * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
834 * parameter indicates this.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200835 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700836 * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
837 * 0-31. If this is non-zero it indicates we're dealing with a
838 * multi-gpu situation and we need to calculate the vblank sync
839 * using DRM_BLANK_HIGH_CRTC_MASK.
840 */
Pekka Paalanenc8a1ff02015-07-02 15:06:08 +0300841static unsigned int
842drm_waitvblank_pipe(struct drm_output *output)
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200843{
844 if (output->pipe > 1)
845 return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
846 DRM_VBLANK_HIGH_CRTC_MASK;
847 else if (output->pipe > 0)
848 return DRM_VBLANK_SECONDARY;
849 else
850 return 0;
851}
852
David Herrmann1edf44c2013-10-22 17:11:26 +0200853static int
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500854drm_output_repaint(struct weston_output *output_base,
Daniel Stoneb1f166d2017-03-01 11:34:10 +0000855 pixman_region32_t *damage,
856 void *repaint_data)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100857{
Armin Krezović545dba62016-08-05 15:54:18 +0200858 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300859 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +0200860 to_drm_backend(output->base.compositor);
Daniel Stone08d4edf2017-04-04 17:54:34 +0100861 struct drm_plane *s;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400862 struct drm_mode *mode;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500863 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100864
Armin Krezović08368132016-09-30 14:11:05 +0200865 if (output->disable_pending || output->destroy_pending)
David Herrmann1edf44c2013-10-22 17:11:26 +0200866 return -1;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800867
Daniel Stonef30a18c2017-04-04 17:54:31 +0100868 assert(!output->fb_last);
869
Derek Foreman2cd87fe2017-04-13 13:48:48 -0500870 /* If disable_planes is set then assign_planes() wasn't
871 * called for this render, so we could still have a stale
872 * cursor plane set up.
873 */
874 if (output->base.disable_planes) {
875 output->cursor_view = NULL;
876 output->cursor_plane.x = INT32_MIN;
877 output->cursor_plane.y = INT32_MIN;
878 }
879
Daniel Stone4e84f7d2017-04-04 17:54:29 +0100880 drm_output_render(output, damage);
Daniel Stone5bb8f582017-04-04 17:54:28 +0100881 if (!output->fb_pending)
David Herrmann1edf44c2013-10-22 17:11:26 +0200882 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100883
Hardeningff39efa2013-09-18 23:56:35 +0200884 mode = container_of(output->base.current_mode, struct drm_mode, base);
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +0300885 if (output->state_invalid || !output->fb_current ||
Daniel Stone5bb8f582017-04-04 17:54:28 +0100886 output->fb_current->stride != output->fb_pending->stride) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300887 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Daniel Stone5bb8f582017-04-04 17:54:28 +0100888 output->fb_pending->fb_id, 0, 0,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400889 &output->connector_id, 1,
890 &mode->mode_info);
891 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200892 weston_log("set mode failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200893 goto err_pageflip;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400894 }
Ander Conselvan de Oliveira722a2d52013-06-04 16:24:05 +0300895 output_base->set_dpms(output_base, WESTON_DPMS_ON);
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +0300896
897 output->state_invalid = false;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200898 }
899
Giulio Camuffo954f1832014-10-11 18:27:30 +0300900 if (drmModePageFlip(backend->drm.fd, output->crtc_id,
Daniel Stone5bb8f582017-04-04 17:54:28 +0100901 output->fb_pending->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500902 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200903 weston_log("queueing pageflip failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200904 goto err_pageflip;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500905 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100906
Daniel Stonef30a18c2017-04-04 17:54:31 +0100907 output->fb_last = output->fb_current;
908 output->fb_current = output->fb_pending;
909 output->fb_pending = NULL;
910
Daniel Stone205c0a02017-04-04 17:54:33 +0100911 assert(!output->page_flip_pending);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300912 output->page_flip_pending = 1;
913
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000914 if (output->pageflip_timer)
915 wl_event_source_timer_update(output->pageflip_timer,
916 backend->pageflip_timeout);
917
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400918 drm_output_set_cursor(output);
919
Jesse Barnes58ef3792012-02-23 09:45:49 -0500920 /*
921 * Now, update all the sprite surfaces
922 */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300923 wl_list_for_each(s, &backend->sprite_list, link) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200924 uint32_t flags = 0, fb_id = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500925 drmVBlank vbl = {
926 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
927 .request.sequence = 1,
928 };
929
Daniel Stonef30a18c2017-04-04 17:54:31 +0100930 /* XXX: Set output much earlier, so we don't attempt to place
931 * planes on entirely the wrong output. */
Daniel Stone5bb8f582017-04-04 17:54:28 +0100932 if ((!s->fb_current && !s->fb_pending) ||
Daniel Stone08d4edf2017-04-04 17:54:34 +0100933 !drm_plane_crtc_supported(output, s))
Jesse Barnes58ef3792012-02-23 09:45:49 -0500934 continue;
935
Daniel Stone5bb8f582017-04-04 17:54:28 +0100936 if (s->fb_pending && !backend->sprites_hidden)
937 fb_id = s->fb_pending->fb_id;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200938
Giulio Camuffo954f1832014-10-11 18:27:30 +0300939 ret = drmModeSetPlane(backend->drm.fd, s->plane_id,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200940 output->crtc_id, fb_id, flags,
Jesse Barnes58ef3792012-02-23 09:45:49 -0500941 s->dest_x, s->dest_y,
942 s->dest_w, s->dest_h,
943 s->src_x, s->src_y,
944 s->src_w, s->src_h);
945 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +0200946 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500947 ret, strerror(errno));
948
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200949 vbl.request.type |= drm_waitvblank_pipe(output);
Rob Clark5ca1a472012-08-08 20:27:37 -0500950
Jesse Barnes58ef3792012-02-23 09:45:49 -0500951 /*
952 * Queue a vblank signal so we know when the surface
953 * becomes active on the display or has been replaced.
954 */
955 vbl.request.signal = (unsigned long)s;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300956 ret = drmWaitVBlank(backend->drm.fd, &vbl);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500957 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200958 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500959 ret, strerror(errno));
960 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300961
962 s->output = output;
Daniel Stonef30a18c2017-04-04 17:54:31 +0100963 s->fb_last = s->fb_current;
964 s->fb_current = s->fb_pending;
965 s->fb_pending = NULL;
Daniel Stone65d87d02017-04-04 17:54:32 +0100966 output->vblank_pending++;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500967 }
968
David Herrmann1edf44c2013-10-22 17:11:26 +0200969 return 0;
970
971err_pageflip:
Kristian Høgsbergb3955b02014-01-23 16:25:06 -0800972 output->cursor_view = NULL;
Daniel Stone5bb8f582017-04-04 17:54:28 +0100973 if (output->fb_pending) {
974 drm_fb_unref(output->fb_pending);
975 output->fb_pending = NULL;
David Herrmann1edf44c2013-10-22 17:11:26 +0200976 }
977
978 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400979}
980
981static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200982drm_output_start_repaint_loop(struct weston_output *output_base)
983{
Armin Krezović545dba62016-08-05 15:54:18 +0200984 struct drm_output *output = to_drm_output(output_base);
985 struct drm_backend *backend =
986 to_drm_backend(output_base->compositor);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200987 uint32_t fb_id;
Mario Kleinerf507ec32015-06-21 21:25:14 +0200988 struct timespec ts, tnow;
989 struct timespec vbl2now;
990 int64_t refresh_nsec;
991 int ret;
992 drmVBlank vbl = {
993 .request.type = DRM_VBLANK_RELATIVE,
994 .request.sequence = 0,
995 .request.signal = 0,
996 };
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300997
Armin Krezović08368132016-09-30 14:11:05 +0200998 if (output->disable_pending || output->destroy_pending)
Xiong Zhangabd5d472013-10-11 14:43:07 +0800999 return;
1000
Daniel Stone5bb8f582017-04-04 17:54:28 +01001001 if (!output->fb_current) {
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001002 /* We can't page flip if there's no mode set */
David Herrmann3c688c52013-10-22 17:11:25 +02001003 goto finish_frame;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001004 }
1005
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03001006 /* Need to smash all state in from scratch; current timings might not
1007 * be what we want, page flip might not work, etc.
1008 */
1009 if (output->state_invalid)
1010 goto finish_frame;
1011
Mario Kleinerf507ec32015-06-21 21:25:14 +02001012 /* Try to get current msc and timestamp via instant query */
1013 vbl.request.type |= drm_waitvblank_pipe(output);
1014 ret = drmWaitVBlank(backend->drm.fd, &vbl);
1015
1016 /* Error ret or zero timestamp means failure to get valid timestamp */
1017 if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
1018 ts.tv_sec = vbl.reply.tval_sec;
1019 ts.tv_nsec = vbl.reply.tval_usec * 1000;
1020
1021 /* Valid timestamp for most recent vblank - not stale?
1022 * Stale ts could happen on Linux 3.17+, so make sure it
1023 * is not older than 1 refresh duration since now.
1024 */
1025 weston_compositor_read_presentation_clock(backend->compositor,
1026 &tnow);
1027 timespec_sub(&vbl2now, &tnow, &ts);
1028 refresh_nsec =
1029 millihz_to_nsec(output->base.current_mode->refresh);
1030 if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
1031 drm_output_update_msc(output, vbl.reply.sequence);
1032 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001033 WP_PRESENTATION_FEEDBACK_INVALID);
Mario Kleinerf507ec32015-06-21 21:25:14 +02001034 return;
1035 }
1036 }
1037
1038 /* Immediate query didn't provide valid timestamp.
1039 * Use pageflip fallback.
1040 */
Daniel Stone5bb8f582017-04-04 17:54:28 +01001041 fb_id = output->fb_current->fb_id;
Jonas Ådahle5a12252013-04-05 23:07:11 +02001042
Daniel Stone205c0a02017-04-04 17:54:33 +01001043 assert(!output->page_flip_pending);
1044 assert(!output->fb_last);
1045
Giulio Camuffo954f1832014-10-11 18:27:30 +03001046 if (drmModePageFlip(backend->drm.fd, output->crtc_id, fb_id,
Jonas Ådahle5a12252013-04-05 23:07:11 +02001047 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
1048 weston_log("queueing pageflip failed: %m\n");
David Herrmann3c688c52013-10-22 17:11:25 +02001049 goto finish_frame;
Jonas Ådahle5a12252013-04-05 23:07:11 +02001050 }
David Herrmann3c688c52013-10-22 17:11:25 +02001051
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001052 if (output->pageflip_timer)
1053 wl_event_source_timer_update(output->pageflip_timer,
1054 backend->pageflip_timeout);
1055
Daniel Stone205c0a02017-04-04 17:54:33 +01001056 output->fb_last = drm_fb_ref(output->fb_current);
1057 output->page_flip_pending = 1;
1058
David Herrmann3c688c52013-10-22 17:11:25 +02001059 return;
1060
1061finish_frame:
1062 /* if we cannot page-flip, immediately finish frame */
Daniel Stone3615ce12017-03-01 11:34:05 +00001063 weston_output_finish_frame(output_base, NULL,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001064 WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001065}
1066
1067static void
Pekka Paalanen641307c2014-09-23 22:08:47 -04001068drm_output_update_msc(struct drm_output *output, unsigned int seq)
1069{
1070 uint64_t msc_hi = output->base.msc >> 32;
1071
1072 if (seq < (output->base.msc & 0xffffffff))
1073 msc_hi++;
1074
1075 output->base.msc = (msc_hi << 32) + seq;
1076}
1077
1078static void
Jesse Barnes58ef3792012-02-23 09:45:49 -05001079vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
1080 void *data)
1081{
Daniel Stone08d4edf2017-04-04 17:54:34 +01001082 struct drm_plane *s = (struct drm_plane *)data;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001083 struct drm_output *output = s->output;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001084 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001085 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
1086 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001087
Pekka Paalanen641307c2014-09-23 22:08:47 -04001088 drm_output_update_msc(output, frame);
Daniel Stone65d87d02017-04-04 17:54:32 +01001089 output->vblank_pending--;
1090 assert(output->vblank_pending >= 0);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001091
Daniel Stonef30a18c2017-04-04 17:54:31 +01001092 assert(s->fb_last || s->fb_current);
1093 drm_fb_unref(s->fb_last);
1094 s->fb_last = NULL;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001095
Daniel Stone65d87d02017-04-04 17:54:32 +01001096 if (!output->page_flip_pending && !output->vblank_pending) {
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001097 /* Stop the pageflip timer instead of rearming it here */
1098 if (output->pageflip_timer)
1099 wl_event_source_timer_update(output->pageflip_timer, 0);
1100
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001101 ts.tv_sec = sec;
1102 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02001103 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001104 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05001105}
1106
1107static void
Armin Krezović08368132016-09-30 14:11:05 +02001108drm_output_destroy(struct weston_output *base);
Xiong Zhangabd5d472013-10-11 14:43:07 +08001109
1110static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001111page_flip_handler(int fd, unsigned int frame,
1112 unsigned int sec, unsigned int usec, void *data)
1113{
Armin Krezović545dba62016-08-05 15:54:18 +02001114 struct drm_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001115 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001116 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
1117 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
1118 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001119
Pekka Paalanen641307c2014-09-23 22:08:47 -04001120 drm_output_update_msc(output, frame);
1121
Daniel Stone205c0a02017-04-04 17:54:33 +01001122 assert(output->page_flip_pending);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001123 output->page_flip_pending = 0;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001124
Daniel Stone205c0a02017-04-04 17:54:33 +01001125 drm_fb_unref(output->fb_last);
1126 output->fb_last = NULL;
1127
Xiong Zhangabd5d472013-10-11 14:43:07 +08001128 if (output->destroy_pending)
1129 drm_output_destroy(&output->base);
Armin Krezović08368132016-09-30 14:11:05 +02001130 else if (output->disable_pending)
1131 weston_output_disable(&output->base);
Xiong Zhangabd5d472013-10-11 14:43:07 +08001132 else if (!output->vblank_pending) {
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001133 /* Stop the pageflip timer instead of rearming it here */
1134 if (output->pageflip_timer)
1135 wl_event_source_timer_update(output->pageflip_timer, 0);
1136
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001137 ts.tv_sec = sec;
1138 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02001139 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03001140
1141 /* We can't call this from frame_notify, because the output's
1142 * repaint needed flag is cleared just after that */
1143 if (output->recorder)
1144 weston_output_schedule_repaint(&output->base);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001145 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001146}
1147
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001148static uint32_t
Daniel Stone08d4edf2017-04-04 17:54:34 +01001149drm_output_check_plane_format(struct drm_plane *p,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001150 struct weston_view *ev, struct gbm_bo *bo)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001151{
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001152 uint32_t i, format;
1153
1154 format = gbm_bo_get_format(bo);
1155
1156 if (format == GBM_FORMAT_ARGB8888) {
1157 pixman_region32_t r;
1158
Kristian Høgsberg63093a32013-03-01 14:29:16 -05001159 pixman_region32_init_rect(&r, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001160 ev->surface->width,
1161 ev->surface->height);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001162 pixman_region32_subtract(&r, &r, &ev->surface->opaque);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001163
1164 if (!pixman_region32_not_empty(&r))
1165 format = GBM_FORMAT_XRGB8888;
1166
1167 pixman_region32_fini(&r);
1168 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05001169
Daniel Stone08d4edf2017-04-04 17:54:34 +01001170 for (i = 0; i < p->count_formats; i++)
1171 if (p->formats[i] == format)
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001172 return format;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001173
1174 return 0;
1175}
1176
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001177static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001178drm_output_prepare_overlay_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001179 struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001180{
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001181 struct weston_compositor *ec = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02001182 struct drm_backend *b = to_drm_backend(ec);
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001183 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001184 struct wl_resource *buffer_resource;
Daniel Stone08d4edf2017-04-04 17:54:34 +01001185 struct drm_plane *p;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001186 struct linux_dmabuf_buffer *dmabuf;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001187 int found = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001188 struct gbm_bo *bo;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001189 pixman_region32_t dest_rect, src_rect;
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001190 pixman_box32_t *box, tbox;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001191 uint32_t format;
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001192 wl_fixed_t sx1, sy1, sx2, sy2;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001193
Giulio Camuffo954f1832014-10-11 18:27:30 +03001194 if (b->sprites_are_broken)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001195 return NULL;
Kristian Høgsberg65bec242012-03-05 19:57:35 -05001196
Daniel Stone296d7a92016-10-21 18:05:37 +01001197 /* Don't import buffers which span multiple outputs. */
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001198 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001199 return NULL;
Ander Conselvan de Oliveirad450b192012-06-26 17:09:12 +03001200
Daniel Stone296d7a92016-10-21 18:05:37 +01001201 /* We can only import GBM buffers. */
1202 if (b->gbm == NULL)
1203 return NULL;
1204
Jason Ekstranda7af7042013-10-12 22:38:11 -05001205 if (ev->surface->buffer_ref.buffer == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001206 return NULL;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001207 buffer_resource = ev->surface->buffer_ref.buffer->resource;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001208 if (wl_shm_buffer_get(buffer_resource))
Rob Clark702ffae2012-08-09 14:18:27 -05001209 return NULL;
1210
Daniel Stone296d7a92016-10-21 18:05:37 +01001211 if (viewport->buffer.transform != output->base.transform)
1212 return NULL;
1213 if (viewport->buffer.scale != output->base.current_scale)
1214 return NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001215 if (!drm_view_transform_supported(ev))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001216 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001217
Daniel Stone296d7a92016-10-21 18:05:37 +01001218 if (ev->alpha != 1.0f)
1219 return NULL;
1220
Daniel Stone08d4edf2017-04-04 17:54:34 +01001221 wl_list_for_each(p, &b->sprite_list, link) {
1222 if (!drm_plane_crtc_supported(output, p))
Jesse Barnes58ef3792012-02-23 09:45:49 -05001223 continue;
1224
Daniel Stone08d4edf2017-04-04 17:54:34 +01001225 if (!p->fb_pending) {
Jesse Barnes58ef3792012-02-23 09:45:49 -05001226 found = 1;
1227 break;
1228 }
1229 }
1230
1231 /* No sprites available */
1232 if (!found)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001233 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001234
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001235 if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) {
Bryce Harringtona3582072015-08-14 12:23:13 -07001236#ifdef HAVE_GBM_FD_IMPORT
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001237 /* XXX: TODO:
1238 *
1239 * Use AddFB2 directly, do not go via GBM.
1240 * Add support for multiplanar formats.
1241 * Both require refactoring in the DRM-backend to
1242 * support a mix of gbm_bos and drmfbs.
1243 */
1244 struct gbm_import_fd_data gbm_dmabuf = {
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001245 .fd = dmabuf->attributes.fd[0],
1246 .width = dmabuf->attributes.width,
1247 .height = dmabuf->attributes.height,
1248 .stride = dmabuf->attributes.stride[0],
1249 .format = dmabuf->attributes.format
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001250 };
1251
Micah Fedkec8890122017-02-01 15:28:23 -05001252 /* XXX: TODO:
1253 *
1254 * Currently the buffer is rejected if any dmabuf attribute
1255 * flag is set. This keeps us from passing an inverted /
1256 * interlaced / bottom-first buffer (or any other type that may
1257 * be added in the future) through to an overlay. Ultimately,
1258 * these types of buffers should be handled through buffer
1259 * transforms and not as spot-checks requiring specific
1260 * knowledge. */
1261 if (dmabuf->attributes.n_planes != 1 ||
1262 dmabuf->attributes.offset[0] != 0 ||
1263 dmabuf->attributes.flags)
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001264 return NULL;
1265
1266 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf,
1267 GBM_BO_USE_SCANOUT);
Bryce Harringtona3582072015-08-14 12:23:13 -07001268#else
1269 return NULL;
1270#endif
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001271 } else {
1272 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
1273 buffer_resource, GBM_BO_USE_SCANOUT);
1274 }
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001275 if (!bo)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001276 return NULL;
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001277
Daniel Stone08d4edf2017-04-04 17:54:34 +01001278 format = drm_output_check_plane_format(p, ev, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001279 if (format == 0) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001280 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001281 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001282 }
1283
Daniel Stone08d4edf2017-04-04 17:54:34 +01001284 p->fb_pending = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT);
1285 if (!p->fb_pending) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001286 gbm_bo_destroy(bo);
1287 return NULL;
1288 }
1289
Daniel Stone08d4edf2017-04-04 17:54:34 +01001290 drm_fb_set_buffer(p->fb_pending, ev->surface->buffer_ref.buffer);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001291
Jason Ekstranda7af7042013-10-12 22:38:11 -05001292 box = pixman_region32_extents(&ev->transform.boundingbox);
Daniel Stone08d4edf2017-04-04 17:54:34 +01001293 p->base.x = box->x1;
1294 p->base.y = box->y1;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001295
Jesse Barnes58ef3792012-02-23 09:45:49 -05001296 /*
1297 * Calculate the source & dest rects properly based on actual
Derek Foreman4b1a0a12014-09-10 15:37:33 -05001298 * position (note the caller has called weston_view_update_transform()
Jesse Barnes58ef3792012-02-23 09:45:49 -05001299 * for us already).
1300 */
1301 pixman_region32_init(&dest_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001302 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001303 &output->base.region);
1304 pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001305 box = pixman_region32_extents(&dest_rect);
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001306 tbox = weston_transformed_rect(output->base.width,
1307 output->base.height,
1308 output->base.transform,
1309 output->base.current_scale,
Alexander Larssond9a7bb72013-05-22 14:41:39 +02001310 *box);
Daniel Stone08d4edf2017-04-04 17:54:34 +01001311 p->dest_x = tbox.x1;
1312 p->dest_y = tbox.y1;
1313 p->dest_w = tbox.x2 - tbox.x1;
1314 p->dest_h = tbox.y2 - tbox.y1;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001315 pixman_region32_fini(&dest_rect);
1316
1317 pixman_region32_init(&src_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001318 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001319 &output->base.region);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001320 box = pixman_region32_extents(&src_rect);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001321
Jason Ekstranda7af7042013-10-12 22:38:11 -05001322 weston_view_from_global_fixed(ev,
1323 wl_fixed_from_int(box->x1),
1324 wl_fixed_from_int(box->y1),
1325 &sx1, &sy1);
1326 weston_view_from_global_fixed(ev,
1327 wl_fixed_from_int(box->x2),
1328 wl_fixed_from_int(box->y2),
1329 &sx2, &sy2);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001330
1331 if (sx1 < 0)
1332 sx1 = 0;
1333 if (sy1 < 0)
1334 sy1 = 0;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001335 if (sx2 > wl_fixed_from_int(ev->surface->width))
1336 sx2 = wl_fixed_from_int(ev->surface->width);
1337 if (sy2 > wl_fixed_from_int(ev->surface->height))
1338 sy2 = wl_fixed_from_int(ev->surface->height);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001339
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001340 tbox.x1 = sx1;
1341 tbox.y1 = sy1;
1342 tbox.x2 = sx2;
1343 tbox.y2 = sy2;
1344
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001345 tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width),
1346 wl_fixed_from_int(ev->surface->height),
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001347 viewport->buffer.transform,
1348 viewport->buffer.scale,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001349 tbox);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001350
Daniel Stone08d4edf2017-04-04 17:54:34 +01001351 p->src_x = tbox.x1 << 8;
1352 p->src_y = tbox.y1 << 8;
1353 p->src_w = (tbox.x2 - tbox.x1) << 8;
1354 p->src_h = (tbox.y2 - tbox.y1) << 8;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001355 pixman_region32_fini(&src_rect);
1356
Daniel Stone08d4edf2017-04-04 17:54:34 +01001357 return &p->base;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001358}
1359
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001360static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001361drm_output_prepare_cursor_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001362 struct weston_view *ev)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001363{
Armin Krezović545dba62016-08-05 15:54:18 +02001364 struct drm_backend *b = to_drm_backend(output->base.compositor);
Neil Robertsf37f82c2014-05-01 18:00:41 +01001365 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Derek Foreman6c19b692015-12-03 14:07:12 -06001366 struct wl_shm_buffer *shmbuf;
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001367 float x, y;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001368
Giulio Camuffo954f1832014-10-11 18:27:30 +03001369 if (b->cursors_are_broken)
Kristian Høgsberg8a015802012-08-09 17:19:23 -04001370 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001371
1372 if (output->cursor_view)
Pekka Paalanen5580f222015-02-17 16:33:18 +02001373 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001374
1375 /* Don't import buffers which span multiple outputs. */
1376 if (ev->output_mask != (1u << output->base.id))
1377 return NULL;
1378
1379 /* We use GBM to import SHM buffers. */
1380 if (b->gbm == NULL)
1381 return NULL;
1382
Derek Foreman6c19b692015-12-03 14:07:12 -06001383 if (ev->surface->buffer_ref.buffer == NULL)
1384 return NULL;
1385 shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource);
1386 if (!shmbuf)
1387 return NULL;
1388 if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888)
1389 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001390
1391 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
1392 return NULL;
1393 if (ev->transform.enabled &&
1394 (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE))
1395 return NULL;
1396 if (viewport->buffer.scale != output->base.current_scale)
1397 return NULL;
1398 if (ev->geometry.scissor_enabled)
1399 return NULL;
1400
Derek Foreman6c19b692015-12-03 14:07:12 -06001401 if (ev->surface->width > b->cursor_width ||
Daniel Stone70d337d2015-06-16 18:42:23 +01001402 ev->surface->height > b->cursor_height)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001403 return NULL;
1404
Jason Ekstranda7af7042013-10-12 22:38:11 -05001405 output->cursor_view = ev;
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001406 weston_view_to_global_float(ev, 0, 0, &x, &y);
1407 output->cursor_plane.x = x;
1408 output->cursor_plane.y = y;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001409
1410 return &output->cursor_plane;
1411}
1412
Pekka Paalanend0ead482014-06-16 12:05:40 +03001413/**
1414 * Update the image for the current cursor surface
1415 *
1416 * @param b DRM backend structure
1417 * @param bo GBM buffer object to write into
1418 * @param ev View to use for cursor image
1419 */
1420static void
1421cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo,
1422 struct weston_view *ev)
1423{
1424 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
1425 uint32_t buf[b->cursor_width * b->cursor_height];
1426 int32_t stride;
1427 uint8_t *s;
1428 int i;
1429
1430 assert(buffer && buffer->shm_buffer);
1431 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
1432 assert(ev->surface->width <= b->cursor_width);
1433 assert(ev->surface->height <= b->cursor_height);
1434
1435 memset(buf, 0, sizeof buf);
1436 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
1437 s = wl_shm_buffer_get_data(buffer->shm_buffer);
1438
1439 wl_shm_buffer_begin_access(buffer->shm_buffer);
1440 for (i = 0; i < ev->surface->height; i++)
1441 memcpy(buf + i * b->cursor_width,
1442 s + i * stride,
1443 ev->surface->width * 4);
1444 wl_shm_buffer_end_access(buffer->shm_buffer);
1445
1446 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
1447 weston_log("failed update cursor: %m\n");
1448}
1449
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001450static void
1451drm_output_set_cursor(struct drm_output *output)
1452{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001453 struct weston_view *ev = output->cursor_view;
Armin Krezović545dba62016-08-05 15:54:18 +02001454 struct drm_backend *b = to_drm_backend(output->base.compositor);
Pekka Paalanend0ead482014-06-16 12:05:40 +03001455 EGLint handle;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001456 struct gbm_bo *bo;
Derek Foremanbe428b32015-11-24 11:39:38 -06001457 float x, y;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001458
Jason Ekstranda7af7042013-10-12 22:38:11 -05001459 if (ev == NULL) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001460 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001461 return;
1462 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001463
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001464 if (pixman_region32_not_empty(&output->cursor_plane.damage)) {
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001465 pixman_region32_fini(&output->cursor_plane.damage);
1466 pixman_region32_init(&output->cursor_plane.damage);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001467 output->current_cursor ^= 1;
Daniel Stonee4256832017-04-04 17:54:27 +01001468 bo = output->gbm_cursor_fb[output->current_cursor]->bo;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001469
Pekka Paalanend0ead482014-06-16 12:05:40 +03001470 cursor_bo_update(b, bo, ev);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001471 handle = gbm_bo_get_handle(bo).s32;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001472 if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle,
1473 b->cursor_width, b->cursor_height)) {
Pekka Paalanenae29da22012-08-06 14:57:05 +03001474 weston_log("failed to set cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001475 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001476 }
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001477 }
1478
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001479 x = (output->cursor_plane.x - output->base.x) *
1480 output->base.current_scale;
1481 y = (output->cursor_plane.y - output->base.y) *
1482 output->base.current_scale;
Pekka Paalanen7eaed402015-11-27 14:20:58 +02001483
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001484 if (drmModeMoveCursor(b->drm.fd, output->crtc_id, x, y)) {
1485 weston_log("failed to move cursor: %m\n");
1486 b->cursors_are_broken = 1;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001487 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001488}
1489
Jesse Barnes58ef3792012-02-23 09:45:49 -05001490static void
Daniel Stoneb1f166d2017-03-01 11:34:10 +00001491drm_assign_planes(struct weston_output *output_base, void *repaint_data)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001492{
Armin Krezović545dba62016-08-05 15:54:18 +02001493 struct drm_backend *b = to_drm_backend(output_base->compositor);
1494 struct drm_output *output = to_drm_output(output_base);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001495 struct weston_view *ev, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001496 pixman_region32_t overlap, surface_overlap;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001497 struct weston_plane *primary, *next_plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001498
1499 /*
1500 * Find a surface for each sprite in the output using some heuristics:
1501 * 1) size
1502 * 2) frequency of update
1503 * 3) opacity (though some hw might support alpha blending)
1504 * 4) clipping (this can be fixed with color keys)
1505 *
1506 * The idea is to save on blitting since this should save power.
1507 * If we can get a large video surface on the sprite for example,
1508 * the main display surface may not need to update at all, and
1509 * the client buffer can be used directly for the sprite surface
1510 * as we do for flipping full screen surfaces.
1511 */
1512 pixman_region32_init(&overlap);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001513 primary = &output_base->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001514
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001515 output->cursor_view = NULL;
1516 output->cursor_plane.x = INT32_MIN;
1517 output->cursor_plane.y = INT32_MIN;
1518
Giulio Camuffo954f1832014-10-11 18:27:30 +03001519 wl_list_for_each_safe(ev, next, &output_base->compositor->view_list, link) {
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001520 struct weston_surface *es = ev->surface;
1521
1522 /* Test whether this buffer can ever go into a plane:
1523 * non-shm, or small enough to be a cursor.
1524 *
1525 * Also, keep a reference when using the pixman renderer.
1526 * That makes it possible to do a seamless switch to the GL
1527 * renderer and since the pixman renderer keeps a reference
1528 * to the buffer anyway, there is no side effects.
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001529 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001530 if (b->use_pixman ||
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001531 (es->buffer_ref.buffer &&
1532 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
Derek Foreman87430472015-10-15 10:24:48 -05001533 (ev->surface->width <= b->cursor_width &&
1534 ev->surface->height <= b->cursor_height))))
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001535 es->keep_buffer = true;
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001536 else
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001537 es->keep_buffer = false;
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001538
Jesse Barnes58ef3792012-02-23 09:45:49 -05001539 pixman_region32_init(&surface_overlap);
1540 pixman_region32_intersect(&surface_overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001541 &ev->transform.boundingbox);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001542
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001543 next_plane = NULL;
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001544 if (pixman_region32_not_empty(&surface_overlap))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001545 next_plane = primary;
1546 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001547 next_plane = drm_output_prepare_cursor_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001548 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001549 next_plane = drm_output_prepare_scanout_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001550 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001551 next_plane = drm_output_prepare_overlay_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001552 if (next_plane == NULL)
1553 next_plane = primary;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001554
Jason Ekstranda7af7042013-10-12 22:38:11 -05001555 weston_view_move_to_plane(ev, next_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001556
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001557 if (next_plane == primary)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001558 pixman_region32_union(&overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001559 &ev->transform.boundingbox);
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001560
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001561 if (next_plane == primary ||
1562 next_plane == &output->cursor_plane) {
1563 /* cursor plane involves a copy */
1564 ev->psf_flags = 0;
1565 } else {
1566 /* All other planes are a direct scanout of a
1567 * single client buffer.
1568 */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001569 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001570 }
1571
Jesse Barnes58ef3792012-02-23 09:45:49 -05001572 pixman_region32_fini(&surface_overlap);
1573 }
1574 pixman_region32_fini(&overlap);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001575}
1576
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001577/**
1578 * Find the closest-matching mode for a given target
1579 *
1580 * Given a target mode, find the most suitable mode amongst the output's
1581 * current mode list to use, preferring the current mode if possible, to
1582 * avoid an expensive mode switch.
1583 *
1584 * @param output DRM output
1585 * @param target_mode Mode to attempt to match
1586 * @returns Pointer to a mode from the output's mode list
1587 */
Alex Wub7b8bda2012-04-17 17:20:48 +08001588static struct drm_mode *
1589choose_mode (struct drm_output *output, struct weston_mode *target_mode)
1590{
1591 struct drm_mode *tmp_mode = NULL, *mode;
1592
Hardeningff39efa2013-09-18 23:56:35 +02001593 if (output->base.current_mode->width == target_mode->width &&
1594 output->base.current_mode->height == target_mode->height &&
1595 (output->base.current_mode->refresh == target_mode->refresh ||
Alex Wub7b8bda2012-04-17 17:20:48 +08001596 target_mode->refresh == 0))
Hardeningff39efa2013-09-18 23:56:35 +02001597 return (struct drm_mode *)output->base.current_mode;
Alex Wub7b8bda2012-04-17 17:20:48 +08001598
1599 wl_list_for_each(mode, &output->base.mode_list, base.link) {
1600 if (mode->mode_info.hdisplay == target_mode->width &&
1601 mode->mode_info.vdisplay == target_mode->height) {
Mario Kleiner872797c2015-06-21 21:25:09 +02001602 if (mode->base.refresh == target_mode->refresh ||
1603 target_mode->refresh == 0) {
Alex Wub7b8bda2012-04-17 17:20:48 +08001604 return mode;
Daniel Stonef556ebe2015-05-21 08:28:58 +01001605 } else if (!tmp_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001606 tmp_mode = mode;
1607 }
1608 }
1609
1610 return tmp_mode;
1611}
1612
1613static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001614drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00001615static void
1616drm_output_fini_egl(struct drm_output *output);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001617static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001618drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00001619static void
1620drm_output_fini_pixman(struct drm_output *output);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001621
1622static int
Alex Wub7b8bda2012-04-17 17:20:48 +08001623drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
1624{
1625 struct drm_output *output;
1626 struct drm_mode *drm_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001627 struct drm_backend *b;
Alex Wub7b8bda2012-04-17 17:20:48 +08001628
1629 if (output_base == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001630 weston_log("output is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001631 return -1;
1632 }
1633
1634 if (mode == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001635 weston_log("mode is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001636 return -1;
1637 }
1638
Armin Krezović545dba62016-08-05 15:54:18 +02001639 b = to_drm_backend(output_base->compositor);
1640 output = to_drm_output(output_base);
Alex Wub7b8bda2012-04-17 17:20:48 +08001641 drm_mode = choose_mode (output, mode);
1642
1643 if (!drm_mode) {
Martin Minarik6d118362012-06-07 18:01:59 +02001644 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +08001645 return -1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001646 }
1647
Hardeningff39efa2013-09-18 23:56:35 +02001648 if (&drm_mode->base == output->base.current_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001649 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001650
Hardeningff39efa2013-09-18 23:56:35 +02001651 output->base.current_mode->flags = 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001652
Hardeningff39efa2013-09-18 23:56:35 +02001653 output->base.current_mode = &drm_mode->base;
1654 output->base.current_mode->flags =
Alex Wub7b8bda2012-04-17 17:20:48 +08001655 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1656
Daniel Stonef30a18c2017-04-04 17:54:31 +01001657 /* XXX: This drops our current buffer too early, before we've started
1658 * displaying it. Ideally this should be much more atomic and
1659 * integrated with a full repaint cycle, rather than doing a
1660 * sledgehammer modeswitch first, and only later showing new
1661 * content.
1662 */
Daniel Stone5bb8f582017-04-04 17:54:28 +01001663 drm_fb_unref(output->fb_current);
Daniel Stonef30a18c2017-04-04 17:54:31 +01001664 assert(!output->fb_last);
1665 assert(!output->fb_pending);
1666 output->fb_last = output->fb_current = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +08001667
Giulio Camuffo954f1832014-10-11 18:27:30 +03001668 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001669 drm_output_fini_pixman(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001670 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001671 weston_log("failed to init output pixman state with "
1672 "new mode\n");
1673 return -1;
1674 }
1675 } else {
Daniel Stone3e661f72016-11-04 17:24:06 +00001676 drm_output_fini_egl(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001677 if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001678 weston_log("failed to init output egl state with "
1679 "new mode");
1680 return -1;
1681 }
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001682 }
1683
Alex Wub7b8bda2012-04-17 17:20:48 +08001684 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001685}
1686
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001687static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001688on_drm_input(int fd, uint32_t mask, void *data)
1689{
1690 drmEventContext evctx;
1691
1692 memset(&evctx, 0, sizeof evctx);
Emil Velikov863e66b2017-04-04 18:07:34 +01001693 evctx.version = 2;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001694 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001695 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001696 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001697
1698 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001699}
1700
1701static int
Daniel Stoneefa504f2016-12-19 16:48:20 +00001702init_kms_caps(struct drm_backend *b)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001703{
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001704 uint64_t cap;
Daniel Stoneefa504f2016-12-19 16:48:20 +00001705 int ret;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001706 clockid_t clk_id;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001707
Daniel Stoneefa504f2016-12-19 16:48:20 +00001708 weston_log("using %s\n", b->drm.filename);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001709
Daniel Stoneefa504f2016-12-19 16:48:20 +00001710 ret = drmGetCap(b->drm.fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001711 if (ret == 0 && cap == 1)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001712 clk_id = CLOCK_MONOTONIC;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001713 else
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001714 clk_id = CLOCK_REALTIME;
1715
Giulio Camuffo954f1832014-10-11 18:27:30 +03001716 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001717 weston_log("Error: failed to set presentation clock %d.\n",
1718 clk_id);
1719 return -1;
1720 }
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +02001721
Daniel Stoneefa504f2016-12-19 16:48:20 +00001722 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_WIDTH, &cap);
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001723 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001724 b->cursor_width = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001725 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001726 b->cursor_width = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001727
Daniel Stoneefa504f2016-12-19 16:48:20 +00001728 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_HEIGHT, &cap);
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001729 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001730 b->cursor_height = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001731 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001732 b->cursor_height = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001733
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001734 return 0;
1735}
1736
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001737static struct gbm_device *
1738create_gbm_device(int fd)
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001739{
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001740 struct gbm_device *gbm;
Alexandru DAMIANbe0ac5b2013-10-02 17:51:05 +01001741
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001742 gl_renderer = weston_load_module("gl-renderer.so",
1743 "gl_renderer_interface");
1744 if (!gl_renderer)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001745 return NULL;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001746
1747 /* GBM will load a dri driver, but even though they need symbols from
1748 * libglapi, in some version of Mesa they are not linked to it. Since
1749 * only the gl-renderer module links to it, the call above won't make
1750 * these symbols globally available, and loading the DRI driver fails.
1751 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
1752 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
1753
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001754 gbm = gbm_create_device(fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001755
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001756 return gbm;
1757}
1758
Bryce Harringtonc056a982015-05-19 15:25:18 -07001759/* When initializing EGL, if the preferred buffer format isn't available
Bryce Harringtonb9939982016-04-15 20:28:26 -07001760 * we may be able to substitute an ARGB format for an XRGB one.
Derek Foremanc4cfe852015-05-15 12:12:40 -05001761 *
1762 * This returns 0 if substitution isn't possible, but 0 might be a
1763 * legitimate format for other EGL platforms, so the caller is
1764 * responsible for checking for 0 before calling gl_renderer->create().
1765 *
1766 * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689
1767 * but it's entirely possible we'll see this again on other implementations.
1768 */
1769static int
1770fallback_format_for(uint32_t format)
1771{
1772 switch (format) {
1773 case GBM_FORMAT_XRGB8888:
1774 return GBM_FORMAT_ARGB8888;
1775 case GBM_FORMAT_XRGB2101010:
1776 return GBM_FORMAT_ARGB2101010;
1777 default:
1778 return 0;
1779 }
1780}
1781
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001782static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001783drm_backend_create_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001784{
Derek Foreman6d556372015-11-04 14:47:33 -06001785 EGLint format[3] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001786 b->gbm_format,
1787 fallback_format_for(b->gbm_format),
Derek Foreman6d556372015-11-04 14:47:33 -06001788 0,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001789 };
Derek Foreman6d556372015-11-04 14:47:33 -06001790 int n_formats = 2;
John Kåre Alsakeref591aa2013-03-02 12:27:39 +01001791
Derek Foremanc4cfe852015-05-15 12:12:40 -05001792 if (format[1])
Derek Foreman6d556372015-11-04 14:47:33 -06001793 n_formats = 3;
Miguel A. Vicodddc6702016-05-18 17:41:07 +02001794 if (gl_renderer->display_create(b->compositor,
1795 EGL_PLATFORM_GBM_KHR,
1796 (void *)b->gbm,
Miguel A. Vico41700e32016-05-18 17:47:59 +02001797 NULL,
Miguel A. Vicodddc6702016-05-18 17:41:07 +02001798 gl_renderer->opaque_attribs,
1799 format,
1800 n_formats) < 0) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001801 return -1;
1802 }
1803
1804 return 0;
1805}
1806
1807static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001808init_egl(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001809{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001810 b->gbm = create_gbm_device(b->drm.fd);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001811
Giulio Camuffo954f1832014-10-11 18:27:30 +03001812 if (!b->gbm)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001813 return -1;
1814
Giulio Camuffo954f1832014-10-11 18:27:30 +03001815 if (drm_backend_create_gl_renderer(b) < 0) {
1816 gbm_device_destroy(b->gbm);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001817 return -1;
1818 }
1819
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001820 return 0;
1821}
1822
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001823static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001824init_pixman(struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001825{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001826 return pixman_renderer_init(b->compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001827}
1828
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001829/**
Pekka Paalanenec272712014-06-05 11:22:25 +03001830 * Create a drm_plane for a hardware plane
1831 *
1832 * Creates one drm_plane structure for a hardware plane, and initialises its
1833 * properties and formats.
1834 *
1835 * This function does not add the plane to the list of usable planes in Weston
1836 * itself; the caller is responsible for this.
1837 *
1838 * Call drm_plane_destroy to clean up the plane.
1839 *
1840 * @param b DRM compositor backend
1841 * @param kplane DRM plane to create
1842 */
1843static struct drm_plane *
1844drm_plane_create(struct drm_backend *b, const drmModePlane *kplane)
1845{
1846 struct drm_plane *plane;
1847
1848 plane = zalloc(sizeof(*plane) + ((sizeof(uint32_t)) *
1849 kplane->count_formats));
1850 if (!plane) {
1851 weston_log("%s: out of memory\n", __func__);
1852 return NULL;
1853 }
1854
1855 plane->backend = b;
1856 plane->possible_crtcs = kplane->possible_crtcs;
1857 plane->plane_id = kplane->plane_id;
1858 plane->count_formats = kplane->count_formats;
1859 memcpy(plane->formats, kplane->formats,
1860 kplane->count_formats * sizeof(kplane->formats[0]));
1861
1862 weston_plane_init(&plane->base, b->compositor, 0, 0);
1863 wl_list_insert(&b->sprite_list, &plane->link);
1864
1865 return plane;
1866}
1867
1868/**
1869 * Destroy one DRM plane
1870 *
1871 * Destroy a DRM plane, removing it from screen and releasing its retained
1872 * buffers in the process. The counterpart to drm_plane_create.
1873 *
1874 * @param plane Plane to deallocate (will be freed)
1875 */
1876static void
1877drm_plane_destroy(struct drm_plane *plane)
1878{
1879 drmModeSetPlane(plane->backend->drm.fd, plane->plane_id, 0, 0, 0,
1880 0, 0, 0, 0, 0, 0, 0, 0);
1881 assert(!plane->fb_last);
1882 assert(!plane->fb_pending);
1883 drm_fb_unref(plane->fb_current);
1884 weston_plane_release(&plane->base);
1885 wl_list_remove(&plane->link);
1886 free(plane);
1887}
1888
1889/**
1890 * Initialise sprites (overlay planes)
1891 *
1892 * Walk the list of provided DRM planes, and add overlay planes.
1893 *
1894 * Call destroy_sprites to free these planes.
1895 *
1896 * @param b DRM compositor backend
1897 */
1898static void
1899create_sprites(struct drm_backend *b)
1900{
1901 drmModePlaneRes *kplane_res;
1902 drmModePlane *kplane;
1903 struct drm_plane *drm_plane;
1904 uint32_t i;
1905
1906 kplane_res = drmModeGetPlaneResources(b->drm.fd);
1907 if (!kplane_res) {
1908 weston_log("failed to get plane resources: %s\n",
1909 strerror(errno));
1910 return;
1911 }
1912
1913 for (i = 0; i < kplane_res->count_planes; i++) {
1914 kplane = drmModeGetPlane(b->drm.fd, kplane_res->planes[i]);
1915 if (!kplane)
1916 continue;
1917
1918 drm_plane = drm_plane_create(b, kplane);
1919 drmModeFreePlane(kplane);
1920 if (!drm_plane)
1921 continue;
1922
1923 weston_compositor_stack_plane(b->compositor, &drm_plane->base,
1924 &b->compositor->primary_plane);
1925 }
1926
1927 drmModeFreePlaneResources(kplane_res);
1928}
1929
1930/**
1931 * Clean up sprites (overlay planes)
1932 *
1933 * The counterpart to create_sprites.
1934 *
1935 * @param b DRM compositor backend
1936 */
1937static void
1938destroy_sprites(struct drm_backend *b)
1939{
1940 struct drm_plane *plane, *next;
1941
1942 wl_list_for_each_safe(plane, next, &b->sprite_list, link)
1943 drm_plane_destroy(plane);
1944}
1945
1946/**
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001947 * Add a mode to output's mode list
1948 *
1949 * Copy the supplied DRM mode into a Weston mode structure, and add it to the
1950 * output's mode list.
1951 *
1952 * @param output DRM output to add mode to
1953 * @param info DRM mode structure to add
1954 * @returns Newly-allocated Weston/DRM mode structure
1955 */
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001956static struct drm_mode *
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001957drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001958{
1959 struct drm_mode *mode;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001960 uint64_t refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001961
1962 mode = malloc(sizeof *mode);
1963 if (mode == NULL)
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001964 return NULL;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001965
1966 mode->base.flags = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02001967 mode->base.width = info->hdisplay;
1968 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001969
1970 /* Calculate higher precision (mHz) refresh rate */
1971 refresh = (info->clock * 1000000LL / info->htotal +
1972 info->vtotal / 2) / info->vtotal;
1973
1974 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1975 refresh *= 2;
1976 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1977 refresh /= 2;
1978 if (info->vscan > 1)
1979 refresh /= info->vscan;
1980
1981 mode->base.refresh = refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001982 mode->mode_info = *info;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04001983
1984 if (info->type & DRM_MODE_TYPE_PREFERRED)
1985 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1986
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001987 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1988
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001989 return mode;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001990}
1991
1992static int
1993drm_subpixel_to_wayland(int drm_value)
1994{
1995 switch (drm_value) {
1996 default:
1997 case DRM_MODE_SUBPIXEL_UNKNOWN:
1998 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1999 case DRM_MODE_SUBPIXEL_NONE:
2000 return WL_OUTPUT_SUBPIXEL_NONE;
2001 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
2002 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
2003 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
2004 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
2005 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
2006 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
2007 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
2008 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
2009 }
2010}
2011
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002012/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002013static uint32_t
2014drm_get_backlight(struct drm_output *output)
2015{
2016 long brightness, max_brightness, norm;
2017
2018 brightness = backlight_get_brightness(output->backlight);
2019 max_brightness = backlight_get_max_brightness(output->backlight);
2020
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002021 /* convert it on a scale of 0 to 255 */
2022 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002023
2024 return (uint32_t) norm;
2025}
2026
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002027/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002028static void
2029drm_set_backlight(struct weston_output *output_base, uint32_t value)
2030{
Armin Krezović545dba62016-08-05 15:54:18 +02002031 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002032 long max_brightness, new_brightness;
2033
2034 if (!output->backlight)
2035 return;
2036
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002037 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002038 return;
2039
2040 max_brightness = backlight_get_max_brightness(output->backlight);
2041
2042 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002043 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002044
2045 backlight_set_brightness(output->backlight, new_brightness);
2046}
2047
2048static drmModePropertyPtr
2049drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
2050{
2051 drmModePropertyPtr props;
2052 int i;
2053
2054 for (i = 0; i < connector->count_props; i++) {
2055 props = drmModeGetProperty(fd, connector->props[i]);
2056 if (!props)
2057 continue;
2058
2059 if (!strcmp(props->name, name))
2060 return props;
2061
2062 drmModeFreeProperty(props);
2063 }
2064
2065 return NULL;
2066}
2067
2068static void
2069drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
2070{
Armin Krezović545dba62016-08-05 15:54:18 +02002071 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002072 struct weston_compositor *ec = output_base->compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02002073 struct drm_backend *b = to_drm_backend(ec);
Daniel Stone36609c72015-06-18 07:49:02 +01002074 int ret;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002075
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +03002076 if (!output->dpms_prop)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002077 return;
2078
Daniel Stone36609c72015-06-18 07:49:02 +01002079 ret = drmModeConnectorSetProperty(b->drm.fd, output->connector_id,
2080 output->dpms_prop->prop_id, level);
2081 if (ret) {
2082 weston_log("DRM: DPMS: failed property set for %s\n",
2083 output->base.name);
2084 return;
2085 }
2086
2087 output->dpms = level;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002088}
2089
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002090static const char * const connector_type_names[] = {
Pekka Paalanen89c49b32015-08-19 15:25:57 +03002091 [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
2092 [DRM_MODE_CONNECTOR_VGA] = "VGA",
2093 [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
2094 [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
2095 [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
2096 [DRM_MODE_CONNECTOR_Composite] = "Composite",
2097 [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
2098 [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
2099 [DRM_MODE_CONNECTOR_Component] = "Component",
2100 [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
2101 [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
2102 [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
2103 [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
2104 [DRM_MODE_CONNECTOR_TV] = "TV",
2105 [DRM_MODE_CONNECTOR_eDP] = "eDP",
Pekka Paalanenab81f152015-08-24 14:27:07 +03002106#ifdef DRM_MODE_CONNECTOR_DSI
Pekka Paalanen89c49b32015-08-19 15:25:57 +03002107 [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
2108 [DRM_MODE_CONNECTOR_DSI] = "DSI",
Pekka Paalanenab81f152015-08-24 14:27:07 +03002109#endif
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04002110};
2111
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002112static char *
2113make_connector_name(const drmModeConnector *con)
2114{
2115 char name[32];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03002116 const char *type_name = NULL;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002117
2118 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
2119 type_name = connector_type_names[con->connector_type];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03002120
2121 if (!type_name)
2122 type_name = "UNNAMED";
2123
2124 snprintf(name, sizeof name, "%s-%d", type_name, con->connector_type_id);
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002125
2126 return strdup(name);
2127}
2128
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002129static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002130find_crtc_for_connector(struct drm_backend *b,
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002131 drmModeRes *resources, drmModeConnector *connector)
2132{
2133 drmModeEncoder *encoder;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002134 int i, j;
Daniel Stone75487c22017-01-16 14:33:38 +00002135 int ret = -1;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002136
2137 for (j = 0; j < connector->count_encoders; j++) {
Daniel Stone75487c22017-01-16 14:33:38 +00002138 uint32_t possible_crtcs, encoder_id, crtc_id;
2139
Giulio Camuffo954f1832014-10-11 18:27:30 +03002140 encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002141 if (encoder == NULL) {
2142 weston_log("Failed to get encoder.\n");
Daniel Stone75487c22017-01-16 14:33:38 +00002143 continue;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002144 }
Daniel Stone75487c22017-01-16 14:33:38 +00002145 encoder_id = encoder->encoder_id;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002146 possible_crtcs = encoder->possible_crtcs;
Daniel Stone75487c22017-01-16 14:33:38 +00002147 crtc_id = encoder->crtc_id;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002148 drmModeFreeEncoder(encoder);
2149
2150 for (i = 0; i < resources->count_crtcs; i++) {
Daniel Stone72c0e1b2017-02-09 13:49:15 +00002151 if (!(possible_crtcs & (1 << i)))
2152 continue;
2153
2154 if (drm_output_find_by_crtc(b, resources->crtcs[i]))
2155 continue;
2156
Daniel Stone75487c22017-01-16 14:33:38 +00002157 /* Try to preserve the existing
2158 * CRTC -> encoder -> connector routing; it makes
2159 * initialisation faster, and also since we have a
2160 * very dumb picking algorithm, may preserve a better
2161 * choice. */
2162 if (!connector->encoder_id ||
2163 (encoder_id == connector->encoder_id &&
2164 crtc_id == resources->crtcs[i]))
2165 return i;
2166
2167 ret = i;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002168 }
2169 }
2170
Daniel Stone75487c22017-01-16 14:33:38 +00002171 return ret;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002172}
2173
Daniel Stonee4256832017-04-04 17:54:27 +01002174static void drm_output_fini_cursor_egl(struct drm_output *output)
2175{
2176 unsigned int i;
2177
2178 for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) {
2179 drm_fb_unref(output->gbm_cursor_fb[i]);
2180 output->gbm_cursor_fb[i] = NULL;
2181 }
2182}
2183
2184static int
2185drm_output_init_cursor_egl(struct drm_output *output, struct drm_backend *b)
2186{
2187 unsigned int i;
2188
2189 for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) {
2190 struct gbm_bo *bo;
2191
2192 bo = gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
2193 GBM_FORMAT_ARGB8888,
2194 GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE);
2195 if (!bo)
2196 goto err;
2197
2198 output->gbm_cursor_fb[i] =
2199 drm_fb_get_from_bo(bo, b, GBM_FORMAT_ARGB8888,
2200 BUFFER_CURSOR);
2201 if (!output->gbm_cursor_fb[i]) {
2202 gbm_bo_destroy(bo);
2203 goto err;
2204 }
2205 }
2206
2207 return 0;
2208
2209err:
2210 weston_log("cursor buffers unavailable, using gl cursors\n");
2211 b->cursors_are_broken = 1;
2212 drm_output_fini_cursor_egl(output);
2213 return -1;
2214}
2215
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002216/* Init output state that depends on gl or gbm */
2217static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002218drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002219{
Derek Foremanc4cfe852015-05-15 12:12:40 -05002220 EGLint format[2] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002221 output->gbm_format,
2222 fallback_format_for(output->gbm_format),
Derek Foremanc4cfe852015-05-15 12:12:40 -05002223 };
Daniel Stonee4256832017-04-04 17:54:27 +01002224 int n_formats = 1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02002225
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002226 output->gbm_surface = gbm_surface_create(b->gbm,
Hardeningff39efa2013-09-18 23:56:35 +02002227 output->base.current_mode->width,
2228 output->base.current_mode->height,
Derek Foremanc4cfe852015-05-15 12:12:40 -05002229 format[0],
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002230 GBM_BO_USE_SCANOUT |
2231 GBM_BO_USE_RENDERING);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002232 if (!output->gbm_surface) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002233 weston_log("failed to create gbm surface\n");
2234 return -1;
2235 }
2236
Derek Foremanc4cfe852015-05-15 12:12:40 -05002237 if (format[1])
2238 n_formats = 2;
Miguel A. Vicoc095cde2016-05-18 17:43:00 +02002239 if (gl_renderer->output_window_create(&output->base,
2240 (EGLNativeWindowType)output->gbm_surface,
2241 output->gbm_surface,
2242 gl_renderer->opaque_attribs,
2243 format,
2244 n_formats) < 0) {
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02002245 weston_log("failed to create gl renderer output state\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002246 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002247 return -1;
2248 }
2249
Daniel Stonee4256832017-04-04 17:54:27 +01002250 drm_output_init_cursor_egl(output, b);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002251
2252 return 0;
2253}
2254
Daniel Stone3e661f72016-11-04 17:24:06 +00002255static void
2256drm_output_fini_egl(struct drm_output *output)
2257{
2258 gl_renderer->output_destroy(&output->base);
2259 gbm_surface_destroy(output->gbm_surface);
Daniel Stonee4256832017-04-04 17:54:27 +01002260 drm_output_fini_cursor_egl(output);
Daniel Stone3e661f72016-11-04 17:24:06 +00002261}
2262
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002263static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002264drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002265{
Hardeningff39efa2013-09-18 23:56:35 +02002266 int w = output->base.current_mode->width;
2267 int h = output->base.current_mode->height;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002268 uint32_t format = output->gbm_format;
2269 uint32_t pixman_format;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002270 unsigned int i;
2271
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002272 switch (format) {
2273 case GBM_FORMAT_XRGB8888:
2274 pixman_format = PIXMAN_x8r8g8b8;
2275 break;
2276 case GBM_FORMAT_RGB565:
2277 pixman_format = PIXMAN_r5g6b5;
2278 break;
2279 default:
2280 weston_log("Unsupported pixman format 0x%x\n", format);
2281 return -1;
2282 }
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002283
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002284 /* FIXME error checking */
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002285 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002286 output->dumb[i] = drm_fb_create_dumb(b, w, h, format);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002287 if (!output->dumb[i])
2288 goto err;
2289
2290 output->image[i] =
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002291 pixman_image_create_bits(pixman_format, w, h,
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002292 output->dumb[i]->map,
2293 output->dumb[i]->stride);
2294 if (!output->image[i])
2295 goto err;
2296 }
2297
2298 if (pixman_renderer_output_create(&output->base) < 0)
2299 goto err;
2300
2301 pixman_region32_init_rect(&output->previous_damage,
Alexander Larsson0b135062013-05-28 16:23:36 +02002302 output->base.x, output->base.y, output->base.width, output->base.height);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002303
2304 return 0;
2305
2306err:
2307 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
2308 if (output->dumb[i])
Daniel Stone6e7a9612017-04-04 17:54:26 +01002309 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002310 if (output->image[i])
2311 pixman_image_unref(output->image[i]);
2312
2313 output->dumb[i] = NULL;
2314 output->image[i] = NULL;
2315 }
2316
2317 return -1;
2318}
2319
2320static void
2321drm_output_fini_pixman(struct drm_output *output)
2322{
2323 unsigned int i;
2324
2325 pixman_renderer_output_destroy(&output->base);
2326 pixman_region32_fini(&output->previous_damage);
2327
2328 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002329 pixman_image_unref(output->image[i]);
Daniel Stone6e7a9612017-04-04 17:54:26 +01002330 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002331 output->dumb[i] = NULL;
2332 output->image[i] = NULL;
2333 }
2334}
2335
Richard Hughes2b2092a2013-04-24 14:58:02 +01002336static void
2337edid_parse_string(const uint8_t *data, char text[])
2338{
2339 int i;
2340 int replaced = 0;
2341
2342 /* this is always 12 bytes, but we can't guarantee it's null
2343 * terminated or not junk. */
2344 strncpy(text, (const char *) data, 12);
2345
Bryce Harrington9c7de162015-08-28 13:04:26 -07002346 /* guarantee our new string is null-terminated */
2347 text[12] = '\0';
2348
Richard Hughes2b2092a2013-04-24 14:58:02 +01002349 /* remove insane chars */
2350 for (i = 0; text[i] != '\0'; i++) {
2351 if (text[i] == '\n' ||
2352 text[i] == '\r') {
2353 text[i] = '\0';
2354 break;
2355 }
2356 }
2357
2358 /* ensure string is printable */
2359 for (i = 0; text[i] != '\0'; i++) {
2360 if (!isprint(text[i])) {
2361 text[i] = '-';
2362 replaced++;
2363 }
2364 }
2365
2366 /* if the string is random junk, ignore the string */
2367 if (replaced > 4)
2368 text[0] = '\0';
2369}
2370
2371#define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
2372#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
2373#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
2374#define EDID_OFFSET_DATA_BLOCKS 0x36
2375#define EDID_OFFSET_LAST_BLOCK 0x6c
2376#define EDID_OFFSET_PNPID 0x08
2377#define EDID_OFFSET_SERIAL 0x0c
2378
2379static int
2380edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
2381{
2382 int i;
2383 uint32_t serial_number;
2384
2385 /* check header */
2386 if (length < 128)
2387 return -1;
2388 if (data[0] != 0x00 || data[1] != 0xff)
2389 return -1;
2390
2391 /* decode the PNP ID from three 5 bit words packed into 2 bytes
2392 * /--08--\/--09--\
2393 * 7654321076543210
2394 * |\---/\---/\---/
2395 * R C1 C2 C3 */
2396 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
2397 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
2398 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
2399 edid->pnp_id[3] = '\0';
2400
2401 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
2402 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
2403 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
2404 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
2405 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
2406 if (serial_number > 0)
2407 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
2408
2409 /* parse EDID data */
2410 for (i = EDID_OFFSET_DATA_BLOCKS;
2411 i <= EDID_OFFSET_LAST_BLOCK;
2412 i += 18) {
2413 /* ignore pixel clock data */
2414 if (data[i] != 0)
2415 continue;
2416 if (data[i+2] != 0)
2417 continue;
2418
2419 /* any useful blocks? */
2420 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
2421 edid_parse_string(&data[i+5],
2422 edid->monitor_name);
2423 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
2424 edid_parse_string(&data[i+5],
2425 edid->serial_number);
2426 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
2427 edid_parse_string(&data[i+5],
2428 edid->eisa_id);
2429 }
2430 }
2431 return 0;
2432}
2433
2434static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002435find_and_parse_output_edid(struct drm_backend *b,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002436 struct drm_output *output,
2437 drmModeConnector *connector)
2438{
2439 drmModePropertyBlobPtr edid_blob = NULL;
2440 drmModePropertyPtr property;
2441 int i;
2442 int rc;
2443
2444 for (i = 0; i < connector->count_props && !edid_blob; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002445 property = drmModeGetProperty(b->drm.fd, connector->props[i]);
Richard Hughes2b2092a2013-04-24 14:58:02 +01002446 if (!property)
2447 continue;
2448 if ((property->flags & DRM_MODE_PROP_BLOB) &&
2449 !strcmp(property->name, "EDID")) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002450 edid_blob = drmModeGetPropertyBlob(b->drm.fd,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002451 connector->prop_values[i]);
2452 }
2453 drmModeFreeProperty(property);
2454 }
2455 if (!edid_blob)
2456 return;
2457
2458 rc = edid_parse(&output->edid,
2459 edid_blob->data,
2460 edid_blob->length);
2461 if (!rc) {
2462 weston_log("EDID data '%s', '%s', '%s'\n",
2463 output->edid.pnp_id,
2464 output->edid.monitor_name,
2465 output->edid.serial_number);
2466 if (output->edid.pnp_id[0] != '\0')
2467 output->base.make = output->edid.pnp_id;
2468 if (output->edid.monitor_name[0] != '\0')
2469 output->base.model = output->edid.monitor_name;
2470 if (output->edid.serial_number[0] != '\0')
2471 output->base.serial_number = output->edid.serial_number;
2472 }
2473 drmModeFreePropertyBlob(edid_blob);
2474}
2475
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002476
2477
2478static int
2479parse_modeline(const char *s, drmModeModeInfo *mode)
2480{
2481 char hsync[16];
2482 char vsync[16];
2483 float fclock;
2484
2485 mode->type = DRM_MODE_TYPE_USERDEF;
2486 mode->hskew = 0;
2487 mode->vscan = 0;
2488 mode->vrefresh = 0;
2489 mode->flags = 0;
2490
Rob Bradford307e09e2013-07-26 16:29:40 +01002491 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002492 &fclock,
2493 &mode->hdisplay,
2494 &mode->hsync_start,
2495 &mode->hsync_end,
2496 &mode->htotal,
2497 &mode->vdisplay,
2498 &mode->vsync_start,
2499 &mode->vsync_end,
2500 &mode->vtotal, hsync, vsync) != 11)
2501 return -1;
2502
2503 mode->clock = fclock * 1000;
2504 if (strcmp(hsync, "+hsync") == 0)
2505 mode->flags |= DRM_MODE_FLAG_PHSYNC;
2506 else if (strcmp(hsync, "-hsync") == 0)
2507 mode->flags |= DRM_MODE_FLAG_NHSYNC;
2508 else
2509 return -1;
2510
2511 if (strcmp(vsync, "+vsync") == 0)
2512 mode->flags |= DRM_MODE_FLAG_PVSYNC;
2513 else if (strcmp(vsync, "-vsync") == 0)
2514 mode->flags |= DRM_MODE_FLAG_NVSYNC;
2515 else
2516 return -1;
2517
Emmanuel Gil Peyrota62138b2016-05-02 22:40:11 +01002518 snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f",
2519 mode->hdisplay, mode->vdisplay, fclock);
2520
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002521 return 0;
2522}
2523
Rob Bradford66bd9f52013-06-25 18:56:42 +01002524static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002525setup_output_seat_constraint(struct drm_backend *b,
Rob Bradford66bd9f52013-06-25 18:56:42 +01002526 struct weston_output *output,
2527 const char *s)
2528{
2529 if (strcmp(s, "") != 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05002530 struct weston_pointer *pointer;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002531 struct udev_seat *seat;
2532
Giulio Camuffo954f1832014-10-11 18:27:30 +03002533 seat = udev_seat_get_named(&b->input, s);
Derek Foreman0720ea32015-07-15 13:00:35 -05002534 if (!seat)
2535 return;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002536
Derek Foreman0720ea32015-07-15 13:00:35 -05002537 seat->base.output = output;
2538
Derek Foreman1281a362015-07-31 16:55:32 -05002539 pointer = weston_seat_get_pointer(&seat->base);
2540 if (pointer)
2541 weston_pointer_clamp(pointer,
2542 &pointer->x,
2543 &pointer->y);
Rob Bradford66bd9f52013-06-25 18:56:42 +01002544 }
2545}
2546
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002547static int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002548parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format)
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002549{
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002550 int ret = 0;
2551
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002552 if (s == NULL)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002553 *gbm_format = default_value;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002554 else if (strcmp(s, "xrgb8888") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002555 *gbm_format = GBM_FORMAT_XRGB8888;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002556 else if (strcmp(s, "rgb565") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002557 *gbm_format = GBM_FORMAT_RGB565;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002558 else if (strcmp(s, "xrgb2101010") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002559 *gbm_format = GBM_FORMAT_XRGB2101010;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002560 else {
2561 weston_log("fatal: unrecognized pixel format: %s\n", s);
2562 ret = -1;
2563 }
2564
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002565 return ret;
2566}
2567
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002568/**
2569 * Choose suitable mode for an output
2570 *
2571 * Find the most suitable mode to use for initial setup (or reconfiguration on
2572 * hotplug etc) for a DRM output.
2573 *
2574 * @param output DRM output to choose mode for
2575 * @param kind Strategy and preference to use when choosing mode
2576 * @param width Desired width for this output
2577 * @param height Desired height for this output
2578 * @param current_mode Mode currently being displayed on this output
2579 * @param modeline Manually-entered mode (may be NULL)
2580 * @returns A mode from the output's mode list, or NULL if none available
2581 */
2582static struct drm_mode *
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002583drm_output_choose_initial_mode(struct drm_backend *backend,
2584 struct drm_output *output,
2585 enum weston_drm_backend_output_mode mode,
Armin Krezović08368132016-09-30 14:11:05 +02002586 const char *modeline,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002587 const drmModeModeInfo *current_mode)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002588{
2589 struct drm_mode *preferred = NULL;
2590 struct drm_mode *current = NULL;
2591 struct drm_mode *configured = NULL;
2592 struct drm_mode *best = NULL;
2593 struct drm_mode *drm_mode;
Armin Krezović08368132016-09-30 14:11:05 +02002594 drmModeModeInfo drm_modeline;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002595 int32_t width = 0;
2596 int32_t height = 0;
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01002597 uint32_t refresh = 0;
2598 int n;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002599
Armin Krezović08368132016-09-30 14:11:05 +02002600 if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) {
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01002601 n = sscanf(modeline, "%dx%d@%d", &width, &height, &refresh);
2602 if (n != 2 && n != 3) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002603 width = -1;
2604
Armin Krezović08368132016-09-30 14:11:05 +02002605 if (parse_modeline(modeline, &drm_modeline) == 0) {
2606 configured = drm_output_add_mode(output, &drm_modeline);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002607 if (!configured)
2608 return NULL;
2609 } else {
2610 weston_log("Invalid modeline \"%s\" for output %s\n",
Armin Krezović08368132016-09-30 14:11:05 +02002611 modeline, output->base.name);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002612 }
2613 }
2614 }
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002615
2616 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002617 if (width == drm_mode->base.width &&
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01002618 height == drm_mode->base.height &&
2619 (refresh == 0 || refresh == drm_mode->mode_info.vrefresh))
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002620 configured = drm_mode;
2621
comic fans7a5c5622016-03-17 14:29:27 +02002622 if (memcmp(current_mode, &drm_mode->mode_info,
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002623 sizeof *current_mode) == 0)
2624 current = drm_mode;
2625
2626 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
2627 preferred = drm_mode;
2628
2629 best = drm_mode;
2630 }
2631
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002632 if (current == NULL && current_mode->clock != 0) {
2633 current = drm_output_add_mode(output, current_mode);
2634 if (!current)
2635 return NULL;
2636 }
2637
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002638 if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002639 configured = current;
2640
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002641 if (configured)
2642 return configured;
2643
2644 if (preferred)
2645 return preferred;
2646
2647 if (current)
2648 return current;
2649
2650 if (best)
2651 return best;
2652
2653 weston_log("no available modes for %s\n", output->base.name);
2654 return NULL;
2655}
2656
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002657static int
2658connector_get_current_mode(drmModeConnector *connector, int drm_fd,
2659 drmModeModeInfo *mode)
2660{
2661 drmModeEncoder *encoder;
2662 drmModeCrtc *crtc;
2663
2664 /* Get the current mode on the crtc that's currently driving
2665 * this connector. */
2666 encoder = drmModeGetEncoder(drm_fd, connector->encoder_id);
2667 memset(mode, 0, sizeof *mode);
2668 if (encoder != NULL) {
2669 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
2670 drmModeFreeEncoder(encoder);
2671 if (crtc == NULL)
2672 return -1;
2673 if (crtc->mode_valid)
2674 *mode = crtc->mode;
2675 drmModeFreeCrtc(crtc);
2676 }
2677
2678 return 0;
2679}
2680
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002681static int
Armin Krezović08368132016-09-30 14:11:05 +02002682drm_output_set_mode(struct weston_output *base,
2683 enum weston_drm_backend_output_mode mode,
2684 const char *modeline)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002685{
Armin Krezović08368132016-09-30 14:11:05 +02002686 struct drm_output *output = to_drm_output(base);
2687 struct drm_backend *b = to_drm_backend(base->compositor);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002688
Armin Krezović445b41b2016-10-09 23:48:16 +02002689 struct drm_mode *current;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002690 drmModeModeInfo crtc_mode;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002691
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002692 output->base.make = "unknown";
2693 output->base.model = "unknown";
Richard Hughes2b2092a2013-04-24 14:58:02 +01002694 output->base.serial_number = "unknown";
Matt Roper361d2ad2011-08-29 13:52:23 -07002695
Armin Krezović08368132016-09-30 14:11:05 +02002696 if (connector_get_current_mode(output->connector, b->drm.fd, &crtc_mode) < 0)
Armin Krezović445b41b2016-10-09 23:48:16 +02002697 return -1;
David Herrmann0f0d54e2011-12-08 17:05:45 +01002698
Armin Krezović08368132016-09-30 14:11:05 +02002699 current = drm_output_choose_initial_mode(b, output, mode, modeline, &crtc_mode);
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002700 if (!current)
Armin Krezović445b41b2016-10-09 23:48:16 +02002701 return -1;
Armin Krezović08368132016-09-30 14:11:05 +02002702
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002703 output->base.current_mode = &current->base;
Hardeningff39efa2013-09-18 23:56:35 +02002704 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
Wang Quanxianacb805a2012-07-30 18:09:46 -04002705
Armin Krezović08368132016-09-30 14:11:05 +02002706 /* Set native_ fields, so weston_output_mode_switch_to_native() works */
2707 output->base.native_mode = output->base.current_mode;
2708 output->base.native_scale = output->base.current_scale;
2709
2710 output->base.mm_width = output->connector->mmWidth;
2711 output->base.mm_height = output->connector->mmHeight;
2712
2713 return 0;
Armin Krezović08368132016-09-30 14:11:05 +02002714}
2715
2716static void
2717drm_output_set_gbm_format(struct weston_output *base,
2718 const char *gbm_format)
2719{
2720 struct drm_output *output = to_drm_output(base);
2721 struct drm_backend *b = to_drm_backend(base->compositor);
2722
2723 if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1)
2724 output->gbm_format = b->gbm_format;
2725}
2726
2727static void
2728drm_output_set_seat(struct weston_output *base,
2729 const char *seat)
2730{
2731 struct drm_output *output = to_drm_output(base);
2732 struct drm_backend *b = to_drm_backend(base->compositor);
2733
2734 setup_output_seat_constraint(b, &output->base,
2735 seat ? seat : "");
2736}
2737
2738static int
2739drm_output_enable(struct weston_output *base)
2740{
2741 struct drm_output *output = to_drm_output(base);
2742 struct drm_backend *b = to_drm_backend(base->compositor);
2743 struct weston_mode *m;
2744
2745 output->dpms_prop = drm_get_prop(b->drm.fd, output->connector, "DPMS");
John Kåre Alsaker94659272012-11-13 19:10:18 +01002746
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00002747 if (b->pageflip_timeout)
2748 drm_output_pageflip_timer_create(output);
2749
Giulio Camuffo954f1832014-10-11 18:27:30 +03002750 if (b->use_pixman) {
2751 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002752 weston_log("Failed to init output pixman state\n");
Armin Krezović08368132016-09-30 14:11:05 +02002753 goto err_free;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002754 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03002755 } else if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002756 weston_log("Failed to init output gl state\n");
Armin Krezović08368132016-09-30 14:11:05 +02002757 goto err_free;
Kristian Høgsberg1d1e0a52012-10-21 13:29:26 -04002758 }
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04002759
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002760 if (output->backlight) {
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002761 weston_log("Initialized backlight, device %s\n",
2762 output->backlight->path);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002763 output->base.set_backlight = drm_set_backlight;
2764 output->base.backlight_current = drm_get_backlight(output);
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002765 } else {
2766 weston_log("Failed to initialize backlight\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002767 }
2768
Jonas Ådahle5a12252013-04-05 23:07:11 +02002769 output->base.start_repaint_loop = drm_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05002770 output->base.repaint = drm_output_repaint;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002771 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002772 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08002773 output->base.switch_mode = drm_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002774
Richard Hughese7299962013-05-01 21:52:12 +01002775 output->base.gamma_size = output->original_crtc->gamma_size;
2776 output->base.set_gamma = drm_output_set_gamma;
2777
Armin Krezović08368132016-09-30 14:11:05 +02002778 output->base.subpixel = drm_subpixel_to_wayland(output->connector->subpixel);
2779
2780 find_and_parse_output_edid(b, output, output->connector);
Armin Krezović9bcf4c52017-02-08 13:55:26 +01002781 if (output->connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
2782 output->connector->connector_type == DRM_MODE_CONNECTOR_eDP)
Pekka Paalanen0079a942017-03-24 15:46:23 +02002783 output->base.connection_internal = true;
Armin Krezović08368132016-09-30 14:11:05 +02002784
Derek Foremanb0427562016-02-05 15:55:20 -06002785 weston_plane_init(&output->cursor_plane, b->compositor,
2786 INT32_MIN, INT32_MIN);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002787 weston_plane_init(&output->fb_plane, b->compositor, 0, 0);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002788
Giulio Camuffo954f1832014-10-11 18:27:30 +03002789 weston_compositor_stack_plane(b->compositor, &output->cursor_plane, NULL);
2790 weston_compositor_stack_plane(b->compositor, &output->fb_plane,
2791 &b->compositor->primary_plane);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002792
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04002793 weston_log("Output %s, (connector %d, crtc %d)\n",
Richard Hughesafe690c2013-05-02 10:10:04 +01002794 output->base.name, output->connector_id, output->crtc_id);
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002795 wl_list_for_each(m, &output->base.mode_list, link)
U. Artie Eoffd3ed6cb2014-01-10 10:15:17 -08002796 weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n",
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002797 m->width, m->height, m->refresh / 1000.0,
2798 m->flags & WL_OUTPUT_MODE_PREFERRED ?
2799 ", preferred" : "",
2800 m->flags & WL_OUTPUT_MODE_CURRENT ?
2801 ", current" : "",
Armin Krezović08368132016-09-30 14:11:05 +02002802 output->connector->count_modes == 0 ?
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002803 ", built-in" : "");
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002804
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03002805 output->state_invalid = true;
2806
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002807 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01002808
2809err_free:
Armin Krezović08368132016-09-30 14:11:05 +02002810 drmModeFreeProperty(output->dpms_prop);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002811
David Herrmann0f0d54e2011-12-08 17:05:45 +01002812 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002813}
2814
Jesse Barnes58ef3792012-02-23 09:45:49 -05002815static void
Armin Krezović08368132016-09-30 14:11:05 +02002816drm_output_deinit(struct weston_output *base)
2817{
2818 struct drm_output *output = to_drm_output(base);
2819 struct drm_backend *b = to_drm_backend(base->compositor);
2820
Daniel Stonef30a18c2017-04-04 17:54:31 +01002821 /* output->fb_last and output->fb_pending must not be set here;
Daniel Stone6e7a9612017-04-04 17:54:26 +01002822 * destroy_pending/disable_pending exist to guarantee exactly this. */
Daniel Stonef30a18c2017-04-04 17:54:31 +01002823 assert(!output->fb_last);
Daniel Stone5bb8f582017-04-04 17:54:28 +01002824 assert(!output->fb_pending);
2825 drm_fb_unref(output->fb_current);
2826 output->fb_current = NULL;
Daniel Stone6e7a9612017-04-04 17:54:26 +01002827
Daniel Stone3e661f72016-11-04 17:24:06 +00002828 if (b->use_pixman)
Armin Krezović08368132016-09-30 14:11:05 +02002829 drm_output_fini_pixman(output);
Daniel Stone3e661f72016-11-04 17:24:06 +00002830 else
2831 drm_output_fini_egl(output);
Armin Krezović08368132016-09-30 14:11:05 +02002832
2833 weston_plane_release(&output->fb_plane);
2834 weston_plane_release(&output->cursor_plane);
2835
2836 drmModeFreeProperty(output->dpms_prop);
2837
2838 /* Turn off hardware cursor */
2839 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
2840}
2841
2842static void
2843drm_output_destroy(struct weston_output *base)
2844{
2845 struct drm_output *output = to_drm_output(base);
2846 struct drm_backend *b = to_drm_backend(base->compositor);
Armin Krezović445b41b2016-10-09 23:48:16 +02002847 struct drm_mode *drm_mode, *next;
Armin Krezović08368132016-09-30 14:11:05 +02002848 drmModeCrtcPtr origcrtc = output->original_crtc;
2849
2850 if (output->page_flip_pending) {
2851 output->destroy_pending = 1;
2852 weston_log("destroy output while page flip pending\n");
2853 return;
2854 }
2855
2856 if (output->base.enabled)
2857 drm_output_deinit(&output->base);
2858
Armin Krezović445b41b2016-10-09 23:48:16 +02002859 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
2860 base.link) {
2861 wl_list_remove(&drm_mode->base.link);
2862 free(drm_mode);
2863 }
2864
Armin Krezović08368132016-09-30 14:11:05 +02002865 if (origcrtc) {
2866 /* Restore original CRTC state */
2867 drmModeSetCrtc(b->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
2868 origcrtc->x, origcrtc->y,
2869 &output->connector_id, 1, &origcrtc->mode);
2870 drmModeFreeCrtc(origcrtc);
2871 }
2872
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00002873 if (output->pageflip_timer)
2874 wl_event_source_remove(output->pageflip_timer);
2875
Armin Krezović08368132016-09-30 14:11:05 +02002876 weston_output_destroy(&output->base);
2877
2878 drmModeFreeConnector(output->connector);
2879
2880 if (output->backlight)
2881 backlight_destroy(output->backlight);
2882
Armin Krezović08368132016-09-30 14:11:05 +02002883 free(output);
2884}
2885
2886static int
2887drm_output_disable(struct weston_output *base)
2888{
2889 struct drm_output *output = to_drm_output(base);
2890 struct drm_backend *b = to_drm_backend(base->compositor);
2891
2892 if (output->page_flip_pending) {
2893 output->disable_pending = 1;
2894 return -1;
2895 }
2896
2897 if (output->base.enabled)
2898 drm_output_deinit(&output->base);
2899
2900 output->disable_pending = 0;
2901
2902 weston_log("Disabling output %s\n", output->base.name);
2903 drmModeSetCrtc(b->drm.fd, output->crtc_id,
2904 0, 0, 0, 0, 0, NULL);
2905
2906 return 0;
2907}
2908
2909/**
2910 * Create a Weston output structure
2911 *
2912 * Given a DRM connector, create a matching drm_output structure and add it
2913 * to Weston's output list. It also takes ownership of the connector, which
2914 * is released when output is destroyed.
2915 *
2916 * @param b Weston backend structure
2917 * @param resources DRM resources for this device
2918 * @param connector DRM connector to use for this new output
2919 * @param drm_device udev device pointer
2920 * @returns 0 on success, or -1 on failure
2921 */
2922static int
2923create_output_for_connector(struct drm_backend *b,
2924 drmModeRes *resources,
2925 drmModeConnector *connector,
2926 struct udev_device *drm_device)
2927{
2928 struct drm_output *output;
Armin Krezović445b41b2016-10-09 23:48:16 +02002929 struct drm_mode *drm_mode;
Armin Krezović08368132016-09-30 14:11:05 +02002930 int i;
2931
2932 i = find_crtc_for_connector(b, resources, connector);
2933 if (i < 0) {
2934 weston_log("No usable crtc/encoder pair for connector.\n");
Armin Krezović445b41b2016-10-09 23:48:16 +02002935 goto err;
Armin Krezović08368132016-09-30 14:11:05 +02002936 }
2937
2938 output = zalloc(sizeof *output);
2939 if (output == NULL)
Armin Krezović445b41b2016-10-09 23:48:16 +02002940 goto err;
Armin Krezović08368132016-09-30 14:11:05 +02002941
2942 output->connector = connector;
2943 output->crtc_id = resources->crtcs[i];
2944 output->pipe = i;
2945 output->connector_id = connector->connector_id;
2946
2947 output->backlight = backlight_init(drm_device,
2948 connector->connector_type);
2949
Armin Krezović445b41b2016-10-09 23:48:16 +02002950 output->original_crtc = drmModeGetCrtc(b->drm.fd, output->crtc_id);
2951
Armin Krezović08368132016-09-30 14:11:05 +02002952 output->base.enable = drm_output_enable;
2953 output->base.destroy = drm_output_destroy;
2954 output->base.disable = drm_output_disable;
2955 output->base.name = make_connector_name(connector);
2956
2957 output->destroy_pending = 0;
2958 output->disable_pending = 0;
Armin Krezović08368132016-09-30 14:11:05 +02002959
Armin Krezović40087402016-09-30 14:11:12 +02002960 weston_output_init(&output->base, b->compositor);
Armin Krezović445b41b2016-10-09 23:48:16 +02002961
2962 wl_list_init(&output->base.mode_list);
2963
2964 for (i = 0; i < output->connector->count_modes; i++) {
2965 drm_mode = drm_output_add_mode(output, &output->connector->modes[i]);
2966 if (!drm_mode) {
2967 drm_output_destroy(&output->base);
2968 return -1;
2969 }
2970 }
2971
Armin Krezović08368132016-09-30 14:11:05 +02002972 weston_compositor_add_pending_output(&output->base, b->compositor);
2973
2974 return 0;
Armin Krezović445b41b2016-10-09 23:48:16 +02002975
2976err:
2977 drmModeFreeConnector(connector);
2978
2979 return -1;
Armin Krezović08368132016-09-30 14:11:05 +02002980}
2981
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002982static int
Ucan, Emre (ADITG/SW1)9a200d72017-02-02 14:06:56 +00002983create_outputs(struct drm_backend *b, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002984{
2985 drmModeConnector *connector;
2986 drmModeRes *resources;
2987 int i;
2988
Giulio Camuffo954f1832014-10-11 18:27:30 +03002989 resources = drmModeGetResources(b->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002990 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002991 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002992 return -1;
2993 }
2994
Giulio Camuffo954f1832014-10-11 18:27:30 +03002995 b->min_width = resources->min_width;
2996 b->max_width = resources->max_width;
2997 b->min_height = resources->min_height;
2998 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05002999
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003000 for (i = 0; i < resources->count_connectors; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03003001 connector = drmModeGetConnector(b->drm.fd,
Benjamin Franzke117483d2011-08-30 11:38:26 +02003002 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003003 if (connector == NULL)
3004 continue;
3005
3006 if (connector->connection == DRM_MODE_CONNECTED &&
Ucan, Emre (ADITG/SW1)9a200d72017-02-02 14:06:56 +00003007 (b->connector == 0 ||
3008 connector->connector_id == b->connector)) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03003009 if (create_output_for_connector(b, resources,
Armin Krezović445b41b2016-10-09 23:48:16 +02003010 connector, drm_device) < 0)
Benjamin Franzke439d9862011-10-07 08:20:53 +02003011 continue;
Armin Krezović08368132016-09-30 14:11:05 +02003012 } else {
3013 drmModeFreeConnector(connector);
Benjamin Franzke9eaee352011-08-02 13:03:54 +02003014 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003015 }
3016
Armin Krezović08368132016-09-30 14:11:05 +02003017 if (wl_list_empty(&b->compositor->output_list) &&
3018 wl_list_empty(&b->compositor->pending_output_list))
Martin Minarik6d118362012-06-07 18:01:59 +02003019 weston_log("No currently active connector found.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003020
3021 drmModeFreeResources(resources);
3022
3023 return 0;
3024}
3025
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003026static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03003027update_outputs(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003028{
3029 drmModeConnector *connector;
3030 drmModeRes *resources;
3031 struct drm_output *output, *next;
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003032 uint32_t *connected;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003033 int i;
3034
Giulio Camuffo954f1832014-10-11 18:27:30 +03003035 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003036 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02003037 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003038 return;
3039 }
3040
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003041 connected = calloc(resources->count_connectors, sizeof(uint32_t));
3042 if (!connected) {
3043 drmModeFreeResources(resources);
3044 return;
3045 }
3046
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003047 /* collect new connects */
3048 for (i = 0; i < resources->count_connectors; i++) {
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +00003049 uint32_t connector_id = resources->connectors[i];
Benjamin Franzke117483d2011-08-30 11:38:26 +02003050
Giulio Camuffo954f1832014-10-11 18:27:30 +03003051 connector = drmModeGetConnector(b->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01003052 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003053 continue;
3054
David Herrmann7551cff2011-12-08 17:05:43 +01003055 if (connector->connection != DRM_MODE_CONNECTED) {
3056 drmModeFreeConnector(connector);
3057 continue;
3058 }
3059
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +00003060 if (b->connector && (b->connector != connector_id)) {
3061 drmModeFreeConnector(connector);
3062 continue;
3063 }
3064
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003065 connected[i] = connector_id;
Benjamin Franzke117483d2011-08-30 11:38:26 +02003066
Daniel Stonec0ec7592017-02-09 13:58:35 +00003067 if (drm_output_find_by_connector(b, connector_id)) {
Armin Krezović08368132016-09-30 14:11:05 +02003068 drmModeFreeConnector(connector);
Daniel Stonec0ec7592017-02-09 13:58:35 +00003069 continue;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003070 }
Daniel Stonec0ec7592017-02-09 13:58:35 +00003071
3072 create_output_for_connector(b, resources,
3073 connector, drm_device);
3074 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003075 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003076
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003077 wl_list_for_each_safe(output, next, &b->compositor->output_list,
3078 base.link) {
3079 bool disconnected = true;
3080
3081 for (i = 0; i < resources->count_connectors; i++) {
3082 if (connected[i] == output->connector_id) {
3083 disconnected = false;
3084 break;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003085 }
3086 }
Armin Krezović08368132016-09-30 14:11:05 +02003087
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003088 if (!disconnected)
3089 continue;
3090
3091 weston_log("connector %d disconnected\n", output->connector_id);
3092 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003093 }
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003094
3095 wl_list_for_each_safe(output, next, &b->compositor->pending_output_list,
3096 base.link) {
3097 bool disconnected = true;
3098
3099 for (i = 0; i < resources->count_connectors; i++) {
3100 if (connected[i] == output->connector_id) {
3101 disconnected = false;
3102 break;
3103 }
3104 }
3105
3106 if (!disconnected)
3107 continue;
3108
3109 weston_log("connector %d disconnected\n", output->connector_id);
3110 drm_output_destroy(&output->base);
3111 }
3112
3113 free(connected);
3114 drmModeFreeResources(resources);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003115}
3116
3117static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003118udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003119{
David Herrmannd7488c22012-03-11 20:05:21 +01003120 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01003121 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01003122
3123 sysnum = udev_device_get_sysnum(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003124 if (!sysnum || atoi(sysnum) != b->drm.id)
David Herrmannd7488c22012-03-11 20:05:21 +01003125 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02003126
David Herrmann6ac52db2012-03-11 20:05:22 +01003127 val = udev_device_get_property_value(device, "HOTPLUG");
3128 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003129 return 0;
3130
David Herrmann6ac52db2012-03-11 20:05:22 +01003131 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003132}
3133
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003134static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003135udev_drm_event(int fd, uint32_t mask, void *data)
3136{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003137 struct drm_backend *b = data;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003138 struct udev_device *event;
3139
Giulio Camuffo954f1832014-10-11 18:27:30 +03003140 event = udev_monitor_receive_device(b->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02003141
Giulio Camuffo954f1832014-10-11 18:27:30 +03003142 if (udev_event_is_hotplug(b, event))
3143 update_outputs(b, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003144
3145 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003146
3147 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003148}
3149
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003150static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003151drm_restore(struct weston_compositor *ec)
3152{
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003153 weston_launcher_restore(ec->launcher);
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003154}
3155
3156static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003157drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003158{
Armin Krezović545dba62016-08-05 15:54:18 +02003159 struct drm_backend *b = to_drm_backend(ec);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003160
Giulio Camuffo954f1832014-10-11 18:27:30 +03003161 udev_input_destroy(&b->input);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003162
Giulio Camuffo954f1832014-10-11 18:27:30 +03003163 wl_event_source_remove(b->udev_drm_source);
3164 wl_event_source_remove(b->drm_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003165
Giulio Camuffo954f1832014-10-11 18:27:30 +03003166 destroy_sprites(b);
Kristian Høgsberg3d64a3e2013-05-10 12:36:04 -04003167
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03003168 weston_compositor_shutdown(ec);
3169
Giulio Camuffo954f1832014-10-11 18:27:30 +03003170 if (b->gbm)
3171 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003172
Giulio Camuffo954f1832014-10-11 18:27:30 +03003173 weston_launcher_destroy(ec->launcher);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003174
Giulio Camuffo954f1832014-10-11 18:27:30 +03003175 close(b->drm.fd);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003176 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003177}
3178
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003179static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003180session_notify(struct wl_listener *listener, void *data)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003181{
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003182 struct weston_compositor *compositor = data;
Armin Krezović545dba62016-08-05 15:54:18 +02003183 struct drm_backend *b = to_drm_backend(compositor);
Daniel Stone08d4edf2017-04-04 17:54:34 +01003184 struct drm_plane *sprite;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003185 struct drm_output *output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003186
Giulio Camuffo954f1832014-10-11 18:27:30 +03003187 if (compositor->session_active) {
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003188 weston_log("activating session\n");
Daniel Stonef33e1042016-11-05 08:10:13 +00003189 weston_compositor_wake(compositor);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003190 weston_compositor_damage_all(compositor);
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03003191
3192 wl_list_for_each(output, &compositor->output_list, base.link)
3193 output->state_invalid = true;
3194
Giulio Camuffo954f1832014-10-11 18:27:30 +03003195 udev_input_enable(&b->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003196 } else {
3197 weston_log("deactivating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03003198 udev_input_disable(&b->input);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04003199
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003200 weston_compositor_offscreen(compositor);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04003201
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05003202 /* If we have a repaint scheduled (either from a
3203 * pending pageflip or the idle handler), make sure we
3204 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003205 * vt switched away. The OFFSCREEN state will prevent
Abdur Rehman4dca0e12017-01-01 19:46:35 +05003206 * further attempts at repainting. When we switch
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05003207 * back, we schedule a repaint, which will process
3208 * pending frame callbacks. */
3209
Giulio Camuffo954f1832014-10-11 18:27:30 +03003210 wl_list_for_each(output, &compositor->output_list, base.link) {
Daniel Stone09a97e22017-03-01 11:34:06 +00003211 output->base.repaint_needed = false;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003212 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05003213 }
3214
Giulio Camuffo954f1832014-10-11 18:27:30 +03003215 output = container_of(compositor->output_list.next,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003216 struct drm_output, base.link);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05003217
Giulio Camuffo954f1832014-10-11 18:27:30 +03003218 wl_list_for_each(sprite, &b->sprite_list, link)
3219 drmModeSetPlane(b->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05003220 sprite->plane_id,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003221 output->crtc_id, 0, 0,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05003222 0, 0, 0, 0, 0, 0, 0, 0);
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003223 };
3224}
3225
Daniel Stoneefa504f2016-12-19 16:48:20 +00003226/**
3227 * Determines whether or not a device is capable of modesetting. If successful,
3228 * sets b->drm.fd and b->drm.filename to the opened device.
3229 */
3230static bool
3231drm_device_is_kms(struct drm_backend *b, struct udev_device *device)
3232{
3233 const char *filename = udev_device_get_devnode(device);
3234 const char *sysnum = udev_device_get_sysnum(device);
3235 drmModeRes *res;
3236 int id, fd;
3237
3238 if (!filename)
3239 return false;
3240
3241 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
3242 if (fd < 0)
3243 return false;
3244
3245 res = drmModeGetResources(fd);
3246 if (!res)
3247 goto out_fd;
3248
3249 if (res->count_crtcs <= 0 || res->count_connectors <= 0 ||
3250 res->count_encoders <= 0)
3251 goto out_res;
3252
3253 if (sysnum)
3254 id = atoi(sysnum);
3255 if (!sysnum || id < 0) {
3256 weston_log("couldn't get sysnum for device %s\n", filename);
3257 goto out_res;
3258 }
3259
3260 /* We can be called successfully on multiple devices; if we have,
3261 * clean up old entries. */
3262 if (b->drm.fd >= 0)
3263 weston_launcher_close(b->compositor->launcher, b->drm.fd);
3264 free(b->drm.filename);
3265
3266 b->drm.fd = fd;
3267 b->drm.id = id;
3268 b->drm.filename = strdup(filename);
3269
Sergi Granellceb59812017-03-28 12:44:04 +02003270 drmModeFreeResources(res);
3271
Daniel Stoneefa504f2016-12-19 16:48:20 +00003272 return true;
3273
3274out_res:
3275 drmModeFreeResources(res);
3276out_fd:
3277 weston_launcher_close(b->compositor->launcher, fd);
3278 return false;
3279}
3280
David Herrmann0af066f2012-10-29 19:21:16 +01003281/*
3282 * Find primary GPU
3283 * Some systems may have multiple DRM devices attached to a single seat. This
3284 * function loops over all devices and tries to find a PCI device with the
3285 * boot_vga sysfs attribute set to 1.
3286 * If no such device is found, the first DRM device reported by udev is used.
Daniel Stoneefa504f2016-12-19 16:48:20 +00003287 * Devices are also vetted to make sure they are are capable of modesetting,
3288 * rather than pure render nodes (GPU with no display), or pure
3289 * memory-allocation devices (VGEM).
David Herrmann0af066f2012-10-29 19:21:16 +01003290 */
3291static struct udev_device*
Giulio Camuffo954f1832014-10-11 18:27:30 +03003292find_primary_gpu(struct drm_backend *b, const char *seat)
David Herrmann0af066f2012-10-29 19:21:16 +01003293{
3294 struct udev_enumerate *e;
3295 struct udev_list_entry *entry;
3296 const char *path, *device_seat, *id;
3297 struct udev_device *device, *drm_device, *pci;
3298
Giulio Camuffo954f1832014-10-11 18:27:30 +03003299 e = udev_enumerate_new(b->udev);
David Herrmann0af066f2012-10-29 19:21:16 +01003300 udev_enumerate_add_match_subsystem(e, "drm");
3301 udev_enumerate_add_match_sysname(e, "card[0-9]*");
3302
3303 udev_enumerate_scan_devices(e);
3304 drm_device = NULL;
3305 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Daniel Stoneefa504f2016-12-19 16:48:20 +00003306 bool is_boot_vga = false;
3307
David Herrmann0af066f2012-10-29 19:21:16 +01003308 path = udev_list_entry_get_name(entry);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003309 device = udev_device_new_from_syspath(b->udev, path);
David Herrmann0af066f2012-10-29 19:21:16 +01003310 if (!device)
3311 continue;
3312 device_seat = udev_device_get_property_value(device, "ID_SEAT");
3313 if (!device_seat)
3314 device_seat = default_seat;
3315 if (strcmp(device_seat, seat)) {
3316 udev_device_unref(device);
3317 continue;
3318 }
3319
3320 pci = udev_device_get_parent_with_subsystem_devtype(device,
3321 "pci", NULL);
3322 if (pci) {
3323 id = udev_device_get_sysattr_value(pci, "boot_vga");
Daniel Stoneefa504f2016-12-19 16:48:20 +00003324 if (id && !strcmp(id, "1"))
3325 is_boot_vga = true;
David Herrmann0af066f2012-10-29 19:21:16 +01003326 }
3327
Daniel Stoneefa504f2016-12-19 16:48:20 +00003328 /* If we already have a modesetting-capable device, and this
3329 * device isn't our boot-VGA device, we aren't going to use
3330 * it. */
3331 if (!is_boot_vga && drm_device) {
David Herrmann0af066f2012-10-29 19:21:16 +01003332 udev_device_unref(device);
Daniel Stoneefa504f2016-12-19 16:48:20 +00003333 continue;
3334 }
3335
3336 /* Make sure this device is actually capable of modesetting;
3337 * if this call succeeds, b->drm.{fd,filename} will be set,
3338 * and any old values freed. */
3339 if (!drm_device_is_kms(b, device)) {
3340 udev_device_unref(device);
3341 continue;
3342 }
3343
3344 /* There can only be one boot_vga device, and we try to use it
3345 * at all costs. */
3346 if (is_boot_vga) {
3347 if (drm_device)
3348 udev_device_unref(drm_device);
3349 drm_device = device;
3350 break;
3351 }
3352
3353 /* Per the (!is_boot_vga && drm_device) test above, we only
3354 * trump existing saved devices with boot-VGA devices, so if
3355 * we end up here, this must be the first device we've seen. */
3356 assert(!drm_device);
3357 drm_device = device;
David Herrmann0af066f2012-10-29 19:21:16 +01003358 }
3359
Daniel Stoneefa504f2016-12-19 16:48:20 +00003360 /* If we're returning a device to use, we must have an open FD for
3361 * it. */
3362 assert(!!drm_device == (b->drm.fd >= 0));
3363
David Herrmann0af066f2012-10-29 19:21:16 +01003364 udev_enumerate_unref(e);
3365 return drm_device;
3366}
3367
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003368static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003369planes_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
3370 void *data)
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003371{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003372 struct drm_backend *b = data;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003373
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003374 switch (key) {
3375 case KEY_C:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003376 b->cursors_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003377 break;
3378 case KEY_V:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003379 b->sprites_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003380 break;
3381 case KEY_O:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003382 b->sprites_hidden ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003383 break;
3384 default:
3385 break;
3386 }
Ander Conselvan de Oliveira180f42a2012-11-21 15:11:37 +02003387}
3388
Kristian Høgsberg0eac34a2013-08-30 14:28:22 -07003389#ifdef BUILD_VAAPI_RECORDER
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003390static void
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003391recorder_destroy(struct drm_output *output)
3392{
3393 vaapi_recorder_destroy(output->recorder);
3394 output->recorder = NULL;
3395
3396 output->base.disable_planes--;
3397
3398 wl_list_remove(&output->recorder_frame_listener.link);
3399 weston_log("[libva recorder] done\n");
3400}
3401
3402static void
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003403recorder_frame_notify(struct wl_listener *listener, void *data)
3404{
3405 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003406 struct drm_backend *b;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003407 int fd, ret;
3408
3409 output = container_of(listener, struct drm_output,
3410 recorder_frame_listener);
Armin Krezović545dba62016-08-05 15:54:18 +02003411 b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003412
3413 if (!output->recorder)
3414 return;
3415
Daniel Stone5bb8f582017-04-04 17:54:28 +01003416 ret = drmPrimeHandleToFD(b->drm.fd, output->fb_current->handle,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003417 DRM_CLOEXEC, &fd);
3418 if (ret) {
3419 weston_log("[libva recorder] "
3420 "failed to create prime fd for front buffer\n");
3421 return;
3422 }
3423
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003424 ret = vaapi_recorder_frame(output->recorder, fd,
Daniel Stone5bb8f582017-04-04 17:54:28 +01003425 output->fb_current->stride);
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003426 if (ret < 0) {
3427 weston_log("[libva recorder] aborted: %m\n");
3428 recorder_destroy(output);
3429 }
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003430}
3431
3432static void *
Giulio Camuffo954f1832014-10-11 18:27:30 +03003433create_recorder(struct drm_backend *b, int width, int height,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003434 const char *filename)
3435{
3436 int fd;
3437 drm_magic_t magic;
3438
Giulio Camuffo954f1832014-10-11 18:27:30 +03003439 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003440 if (fd < 0)
3441 return NULL;
3442
3443 drmGetMagic(fd, &magic);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003444 drmAuthMagic(b->drm.fd, magic);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003445
3446 return vaapi_recorder_create(fd, width, height, filename);
3447}
3448
3449static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003450recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003451 void *data)
3452{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003453 struct drm_backend *b = data;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003454 struct drm_output *output;
3455 int width, height;
3456
Giulio Camuffo954f1832014-10-11 18:27:30 +03003457 output = container_of(b->compositor->output_list.next,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003458 struct drm_output, base.link);
3459
3460 if (!output->recorder) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01003461 if (output->gbm_format != GBM_FORMAT_XRGB8888) {
Ander Conselvan de Oliveira2ef1cd12014-05-06 16:49:06 +03003462 weston_log("failed to start vaapi recorder: "
3463 "output format not supported\n");
3464 return;
3465 }
3466
Hardeningff39efa2013-09-18 23:56:35 +02003467 width = output->base.current_mode->width;
3468 height = output->base.current_mode->height;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003469
3470 output->recorder =
Giulio Camuffo954f1832014-10-11 18:27:30 +03003471 create_recorder(b, width, height, "capture.h264");
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003472 if (!output->recorder) {
3473 weston_log("failed to create vaapi recorder\n");
3474 return;
3475 }
3476
3477 output->base.disable_planes++;
3478
3479 output->recorder_frame_listener.notify = recorder_frame_notify;
3480 wl_signal_add(&output->base.frame_signal,
3481 &output->recorder_frame_listener);
3482
3483 weston_output_schedule_repaint(&output->base);
3484
3485 weston_log("[libva recorder] initialized\n");
3486 } else {
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003487 recorder_destroy(output);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003488 }
3489}
3490#else
3491static void
Bryce Harrington4a8a3a12015-07-16 19:12:26 -07003492recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003493 void *data)
3494{
3495 weston_log("Compiled without libva support\n");
3496}
3497#endif
3498
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003499static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03003500switch_to_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003501{
3502 struct drm_output *output;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003503 bool dmabuf_support_inited;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003504
Giulio Camuffo954f1832014-10-11 18:27:30 +03003505 if (!b->use_pixman)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003506 return;
3507
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003508 dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf;
3509
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003510 weston_log("Switching to GL renderer\n");
3511
Giulio Camuffo954f1832014-10-11 18:27:30 +03003512 b->gbm = create_gbm_device(b->drm.fd);
3513 if (!b->gbm) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003514 weston_log("Failed to create gbm device. "
3515 "Aborting renderer switch\n");
3516 return;
3517 }
3518
Giulio Camuffo954f1832014-10-11 18:27:30 +03003519 wl_list_for_each(output, &b->compositor->output_list, base.link)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003520 pixman_renderer_output_destroy(&output->base);
3521
Giulio Camuffo954f1832014-10-11 18:27:30 +03003522 b->compositor->renderer->destroy(b->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003523
Giulio Camuffo954f1832014-10-11 18:27:30 +03003524 if (drm_backend_create_gl_renderer(b) < 0) {
3525 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003526 weston_log("Failed to create GL renderer. Quitting.\n");
3527 /* FIXME: we need a function to shutdown cleanly */
3528 assert(0);
3529 }
3530
Giulio Camuffo954f1832014-10-11 18:27:30 +03003531 wl_list_for_each(output, &b->compositor->output_list, base.link)
3532 drm_output_init_egl(output, b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003533
Giulio Camuffo954f1832014-10-11 18:27:30 +03003534 b->use_pixman = 0;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003535
3536 if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) {
3537 if (linux_dmabuf_setup(b->compositor) < 0)
3538 weston_log("Error: initializing dmabuf "
3539 "support failed.\n");
3540 }
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003541}
3542
3543static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003544renderer_switch_binding(struct weston_keyboard *keyboard, uint32_t time,
3545 uint32_t key, void *data)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003546{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003547 struct drm_backend *b =
Armin Krezović545dba62016-08-05 15:54:18 +02003548 to_drm_backend(keyboard->seat->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003549
Giulio Camuffo954f1832014-10-11 18:27:30 +03003550 switch_to_gl_renderer(b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003551}
3552
Armin Krezović08368132016-09-30 14:11:05 +02003553static const struct weston_drm_output_api api = {
3554 drm_output_set_mode,
3555 drm_output_set_gbm_format,
3556 drm_output_set_seat,
3557};
3558
Giulio Camuffo954f1832014-10-11 18:27:30 +03003559static struct drm_backend *
3560drm_backend_create(struct weston_compositor *compositor,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003561 struct weston_drm_backend_config *config)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003562{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003563 struct drm_backend *b;
David Herrmann0af066f2012-10-29 19:21:16 +01003564 struct udev_device *drm_device;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003565 struct wl_event_loop *loop;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003566 const char *seat_id = default_seat;
Armin Krezović08368132016-09-30 14:11:05 +02003567 int ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003568
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003569 weston_log("initializing drm backend\n");
3570
Giulio Camuffo954f1832014-10-11 18:27:30 +03003571 b = zalloc(sizeof *b);
3572 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003573 return NULL;
Daniel Stone725c2c32012-06-22 14:04:36 +01003574
Daniel Stoneefa504f2016-12-19 16:48:20 +00003575 b->drm.fd = -1;
3576
Pekka Paalanen68583832015-05-19 09:53:16 +03003577 /*
3578 * KMS support for hardware planes cannot properly synchronize
3579 * without nuclear page flip. Without nuclear/atomic, hw plane
3580 * and cursor plane updates would either tear or cause extra
3581 * waits for vblanks which means dropping the compositor framerate
Pekka Paalanen3f32a132015-09-07 15:38:43 +03003582 * to a fraction. For cursors, it's not so bad, so they are
3583 * enabled.
Pekka Paalanen68583832015-05-19 09:53:16 +03003584 *
3585 * These can be enabled again when nuclear/atomic support lands.
3586 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003587 b->sprites_are_broken = 1;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003588 b->compositor = compositor;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003589 b->use_pixman = config->use_pixman;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00003590 b->pageflip_timeout = config->pageflip_timeout;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003591
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003592 if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0)
3593 goto err_compositor;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003594
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003595 if (config->seat_id)
3596 seat_id = config->seat_id;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003597
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003598 /* Check if we run drm-backend using weston-launch */
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003599 compositor->launcher = weston_launcher_connect(compositor, config->tty,
3600 seat_id, true);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003601 if (compositor->launcher == NULL) {
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003602 weston_log("fatal: drm backend should be run "
Kristian Høgsbergb76237e2013-04-04 21:36:20 -04003603 "using weston-launch binary or as root\n");
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003604 goto err_compositor;
3605 }
3606
Giulio Camuffo954f1832014-10-11 18:27:30 +03003607 b->udev = udev_new();
3608 if (b->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003609 weston_log("failed to initialize udev context\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003610 goto err_launcher;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003611 }
3612
Giulio Camuffo954f1832014-10-11 18:27:30 +03003613 b->session_listener.notify = session_notify;
3614 wl_signal_add(&compositor->session_signal, &b->session_listener);
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05003615
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003616 drm_device = find_primary_gpu(b, seat_id);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04003617 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003618 weston_log("no drm device found\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003619 goto err_udev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003620 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003621
Daniel Stoneefa504f2016-12-19 16:48:20 +00003622 if (init_kms_caps(b) < 0) {
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003623 weston_log("failed to initialize kms\n");
3624 goto err_udev_dev;
3625 }
3626
Giulio Camuffo954f1832014-10-11 18:27:30 +03003627 if (b->use_pixman) {
3628 if (init_pixman(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003629 weston_log("failed to initialize pixman renderer\n");
3630 goto err_udev_dev;
3631 }
3632 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +03003633 if (init_egl(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003634 weston_log("failed to initialize egl\n");
3635 goto err_udev_dev;
3636 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003637 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05003638
Giulio Camuffo954f1832014-10-11 18:27:30 +03003639 b->base.destroy = drm_destroy;
3640 b->base.restore = drm_restore;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02003641
Bob Ham91880f12016-01-12 10:21:47 +00003642 weston_setup_vt_switch_bindings(compositor);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04003643
Giulio Camuffo954f1832014-10-11 18:27:30 +03003644 wl_list_init(&b->sprite_list);
3645 create_sprites(b);
Jesse Barnes58ef3792012-02-23 09:45:49 -05003646
Giulio Camuffo954f1832014-10-11 18:27:30 +03003647 if (udev_input_init(&b->input,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03003648 compositor, b->udev, seat_id,
3649 config->configure_device) < 0) {
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003650 weston_log("failed to create input devices\n");
3651 goto err_sprite;
3652 }
3653
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +00003654 b->connector = config->connector;
3655
Ucan, Emre (ADITG/SW1)9a200d72017-02-02 14:06:56 +00003656 if (create_outputs(b, drm_device) < 0) {
Daniel Stoneefa504f2016-12-19 16:48:20 +00003657 weston_log("failed to create output for %s\n", b->drm.filename);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003658 goto err_udev_input;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003659 }
3660
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003661 /* A this point we have some idea of whether or not we have a working
3662 * cursor plane. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003663 if (!b->cursors_are_broken)
3664 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003665
Giulio Camuffo954f1832014-10-11 18:27:30 +03003666 loop = wl_display_get_event_loop(compositor->wl_display);
3667 b->drm_source =
3668 wl_event_loop_add_fd(loop, b->drm.fd,
3669 WL_EVENT_READABLE, on_drm_input, b);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003670
Giulio Camuffo954f1832014-10-11 18:27:30 +03003671 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
3672 if (b->udev_monitor == NULL) {
Abdur Rehman4dca0e12017-01-01 19:46:35 +05003673 weston_log("failed to initialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003674 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003675 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03003676 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003677 "drm", NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003678 b->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02003679 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03003680 udev_monitor_get_fd(b->udev_monitor),
3681 WL_EVENT_READABLE, udev_drm_event, b);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003682
Giulio Camuffo954f1832014-10-11 18:27:30 +03003683 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003684 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003685 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003686 }
3687
Daniel Stonea96b93c2012-06-22 14:04:37 +01003688 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003689
Giulio Camuffo954f1832014-10-11 18:27:30 +03003690 weston_compositor_add_debug_binding(compositor, KEY_O,
3691 planes_binding, b);
3692 weston_compositor_add_debug_binding(compositor, KEY_C,
3693 planes_binding, b);
3694 weston_compositor_add_debug_binding(compositor, KEY_V,
3695 planes_binding, b);
3696 weston_compositor_add_debug_binding(compositor, KEY_Q,
3697 recorder_binding, b);
3698 weston_compositor_add_debug_binding(compositor, KEY_W,
3699 renderer_switch_binding, b);
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003700
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003701 if (compositor->renderer->import_dmabuf) {
3702 if (linux_dmabuf_setup(compositor) < 0)
3703 weston_log("Error: initializing dmabuf "
3704 "support failed.\n");
3705 }
3706
Giulio Camuffo954f1832014-10-11 18:27:30 +03003707 compositor->backend = &b->base;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003708
Armin Krezović08368132016-09-30 14:11:05 +02003709 ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME,
3710 &api, sizeof(api));
3711
3712 if (ret < 0) {
3713 weston_log("Failed to register output API.\n");
3714 goto err_udev_monitor;
3715 }
3716
Giulio Camuffo954f1832014-10-11 18:27:30 +03003717 return b;
Daniel Stonea96b93c2012-06-22 14:04:37 +01003718
3719err_udev_monitor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003720 wl_event_source_remove(b->udev_drm_source);
3721 udev_monitor_unref(b->udev_monitor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003722err_drm_source:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003723 wl_event_source_remove(b->drm_source);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003724err_udev_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003725 udev_input_destroy(&b->input);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003726err_sprite:
Emmanuel Gil Peyrotb8347e32016-05-02 22:40:13 +01003727 if (b->gbm)
3728 gbm_device_destroy(b->gbm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003729 destroy_sprites(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003730err_udev_dev:
3731 udev_device_unref(drm_device);
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003732err_launcher:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003733 weston_launcher_destroy(compositor->launcher);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003734err_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003735 udev_unref(b->udev);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003736err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003737 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003738 free(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003739 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003740}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003741
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003742static void
3743config_init_to_defaults(struct weston_drm_backend_config *config)
3744{
3745}
3746
Giulio Camuffo954f1832014-10-11 18:27:30 +03003747WL_EXPORT int
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01003748weston_backend_init(struct weston_compositor *compositor,
3749 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003750{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003751 struct drm_backend *b;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003752 struct weston_drm_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003753
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003754 if (config_base == NULL ||
3755 config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION ||
3756 config_base->struct_size > sizeof(struct weston_drm_backend_config)) {
3757 weston_log("drm backend config structure is invalid\n");
3758 return -1;
3759 }
Benjamin Franzke117483d2011-08-30 11:38:26 +02003760
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003761 config_init_to_defaults(&config);
3762 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003763
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003764 b = drm_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003765 if (b == NULL)
3766 return -1;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003767
Giulio Camuffo954f1832014-10-11 18:27:30 +03003768 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003769}